summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/layout/svg/svg_layout_support.h
blob: dbc43c74fee3215065ad73c32ff7f7d65f143269 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
 * Copyright (C) 2007 Rob Buis <buis@kde.org>
 * Copyright (C) 2007 Nikolas Zimmermann <zimmermann@kde.org>
 * Copyright (C) 2007 Eric Seidel <eric@webkit.org>
 * Copyright (C) 2009 Google, Inc.  All rights reserved.
 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_LAYOUT_SVG_SVG_LAYOUT_SUPPORT_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_LAYOUT_SVG_SVG_LAYOUT_SUPPORT_H_

#include "third_party/blink/renderer/core/layout/layout_object.h"
#include "third_party/blink/renderer/core/style/svg_computed_style_defs.h"
#include "third_party/blink/renderer/platform/graphics/dash_array.h"
#include "third_party/blink/renderer/platform/transforms/affine_transform.h"
#include "third_party/blink/renderer/platform/wtf/allocator.h"

namespace blink {

class AffineTransform;
class FloatPoint;
class FloatRect;
class LayoutRect;
class LayoutGeometryMap;
class LayoutBoxModelObject;
class LayoutObject;
class ComputedStyle;
class LayoutSVGRoot;
class SVGLengthContext;
class StrokeData;
class TransformState;

class CORE_EXPORT SVGLayoutSupport {
  STATIC_ONLY(SVGLayoutSupport);

 public:
  // Shares child layouting code between
  // LayoutSVGRoot/LayoutSVG(Hidden)Container
  static void LayoutChildren(LayoutObject*,
                             bool force_layout,
                             bool screen_scaling_factor_changed,
                             bool layout_size_changed);

  // Layout resources used by this node.
  static void LayoutResourcesIfNeeded(const LayoutObject&);

  // Helper function determining whether overflow is hidden.
  static bool IsOverflowHidden(const LayoutObject&);
  static bool IsOverflowHidden(const ComputedStyle&);

  // Adjusts the visualRect in combination with filter, clipper and masker
  // in local coordinates.
  static void AdjustVisualRectWithResources(const LayoutObject&, FloatRect&);

  // Determine if the LayoutObject references a filter resource object.
  static bool HasFilterResource(const LayoutObject&);

  // Determines whether the passed point lies in a clipping area
  static bool PointInClippingArea(const LayoutObject&, const FloatPoint&);

  // Transform |pointInParent| to |object|'s user-space and check if it is
  // within the clipping area. Returns false if the transform is singular or
  // the point is outside the clipping area.
  static bool TransformToUserSpaceAndCheckClipping(
      const LayoutObject&,
      const AffineTransform& local_transform,
      const FloatPoint& point_in_parent,
      FloatPoint& local_point);

  static void ComputeContainerBoundingBoxes(const LayoutObject* container,
                                            FloatRect& object_bounding_box,
                                            bool& object_bounding_box_valid,
                                            FloatRect& stroke_bounding_box,
                                            FloatRect& local_visual_rect);

  // Important functions used by nearly all SVG layoutObjects centralizing
  // coordinate transformations / visual rect calculations
  static FloatRect LocalVisualRect(const LayoutObject&);
  static LayoutRect VisualRectInAncestorSpace(
      const LayoutObject&,
      const LayoutBoxModelObject& ancestor);
  static LayoutRect TransformVisualRect(const LayoutObject&,
                                        const AffineTransform&,
                                        const FloatRect&);
  static bool MapToVisualRectInAncestorSpace(
      const LayoutObject&,
      const LayoutBoxModelObject* ancestor,
      const FloatRect& local_visual_rect,
      LayoutRect& result_rect,
      VisualRectFlags = kDefaultVisualRectFlags);
  static void MapLocalToAncestor(const LayoutObject*,
                                 const LayoutBoxModelObject* ancestor,
                                 TransformState&,
                                 MapCoordinatesFlags);
  static void MapAncestorToLocal(const LayoutObject&,
                                 const LayoutBoxModelObject* ancestor,
                                 TransformState&,
                                 MapCoordinatesFlags);
  static const LayoutObject* PushMappingToContainer(
      const LayoutObject*,
      const LayoutBoxModelObject* ancestor_to_stop_at,
      LayoutGeometryMap&);

  // Shared between SVG layoutObjects and resources.
  static void ApplyStrokeStyleToStrokeData(StrokeData&,
                                           const ComputedStyle&,
                                           const LayoutObject&,
                                           float dash_scale_factor);

  static DashArray ResolveSVGDashArray(const SVGDashArray&,
                                       const ComputedStyle&,
                                       const SVGLengthContext&);

  // Determines if any ancestor has adjusted the scale factor.
  static bool ScreenScaleFactorChanged(const LayoutObject*);

  // Determines if any ancestor's layout size has changed.
  static bool LayoutSizeOfNearestViewportChanged(const LayoutObject*);

  // FIXME: These methods do not belong here.
  static const LayoutSVGRoot* FindTreeRootObject(const LayoutObject*);

  // Helper method for determining if a LayoutObject marked as text (isText()==
  // true) can/will be laid out as part of a <text>.
  static bool IsLayoutableTextNode(const LayoutObject*);

  // Determines whether a svg node should isolate or not based on ComputedStyle.
  static bool WillIsolateBlendingDescendantsForStyle(const ComputedStyle&);
  static bool WillIsolateBlendingDescendantsForObject(const LayoutObject*);
  template <typename LayoutObjectType>
  static bool ComputeHasNonIsolatedBlendingDescendants(const LayoutObjectType*);
  static bool IsIsolationRequired(const LayoutObject*);

  static AffineTransform DeprecatedCalculateTransformToLayer(
      const LayoutObject*);
  static float CalculateScreenFontSizeScalingFactor(const LayoutObject*);

  static LayoutObject* FindClosestLayoutSVGText(const LayoutObject*,
                                                const FloatPoint&);

 private:
  static void UpdateObjectBoundingBox(FloatRect& object_bounding_box,
                                      bool& object_bounding_box_valid,
                                      LayoutObject* other,
                                      FloatRect other_bounding_box);
};

class SubtreeContentTransformScope {
  STACK_ALLOCATED();

 public:
  SubtreeContentTransformScope(const AffineTransform&);
  ~SubtreeContentTransformScope();

  static AffineTransform CurrentContentTransformation() {
    return AffineTransform(current_content_transformation_);
  }

 private:
  static AffineTransform::Transform current_content_transformation_;
  AffineTransform saved_content_transformation_;
};

// The following enumeration is used to optimize cases where the scale is known
// to be invariant (see: LayoutSVGContainer::layout and LayoutSVGroot). The
// value 'Full' can be used in the general case when the scale change is
// unknown, or known to change.
enum class SVGTransformChange {
  kNone,
  kScaleInvariant,
  kFull,
};

// Helper for computing ("classifying") a change to a transform using the
// categoies defined above.
class SVGTransformChangeDetector {
  STACK_ALLOCATED();

 public:
  explicit SVGTransformChangeDetector(const AffineTransform& previous)
      : previous_transform_(previous) {}

  SVGTransformChange ComputeChange(const AffineTransform& current) {
    if (previous_transform_ == current)
      return SVGTransformChange::kNone;
    if (ScaleReference(previous_transform_) == ScaleReference(current))
      return SVGTransformChange::kScaleInvariant;
    return SVGTransformChange::kFull;
  }

 private:
  static std::pair<double, double> ScaleReference(
      const AffineTransform& transform) {
    return std::make_pair(transform.XScaleSquared(), transform.YScaleSquared());
  }
  AffineTransform previous_transform_;
};

template <typename LayoutObjectType>
bool SVGLayoutSupport::ComputeHasNonIsolatedBlendingDescendants(
    const LayoutObjectType* object) {
  for (LayoutObject* child = object->FirstChild(); child;
       child = child->NextSibling()) {
    if (child->IsBlendingAllowed() && child->Style()->HasBlendMode())
      return true;
    if (child->HasNonIsolatedBlendingDescendants() &&
        !WillIsolateBlendingDescendantsForObject(child))
      return true;
  }
  return false;
}

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_LAYOUT_SVG_SVG_LAYOUT_SUPPORT_H_