File: ShapeOutsideInfo.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (443 lines) | stat: -rw-r--r-- 16,565 bytes parent folder | download
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
 * Copyright (C) 2012 Adobe Systems Incorporated. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer.
 * 2. Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "core/layout/shapes/ShapeOutsideInfo.h"

#include "core/inspector/ConsoleMessage.h"
#include "core/layout/FloatingObjects.h"
#include "core/layout/LayoutBlockFlow.h"
#include "core/layout/LayoutBox.h"
#include "core/layout/LayoutImage.h"
#include "core/layout/api/LineLayoutBlockFlow.h"
#include "platform/LengthFunctions.h"
#include "public/platform/Platform.h"
#include "wtf/AutoReset.h"
#include <memory>

namespace blink {

CSSBoxType referenceBox(const ShapeValue& shapeValue) {
  if (shapeValue.cssBox() == BoxMissing)
    return MarginBox;
  return shapeValue.cssBox();
}

void ShapeOutsideInfo::setReferenceBoxLogicalSize(
    LayoutSize newReferenceBoxLogicalSize) {
  bool isHorizontalWritingMode =
      m_layoutBox.containingBlock()->style()->isHorizontalWritingMode();
  switch (referenceBox(*m_layoutBox.style()->shapeOutside())) {
    case MarginBox:
      if (isHorizontalWritingMode)
        newReferenceBoxLogicalSize.expand(m_layoutBox.marginWidth(),
                                          m_layoutBox.marginHeight());
      else
        newReferenceBoxLogicalSize.expand(m_layoutBox.marginHeight(),
                                          m_layoutBox.marginWidth());
      break;
    case BorderBox:
      break;
    case PaddingBox:
      if (isHorizontalWritingMode)
        newReferenceBoxLogicalSize.shrink(m_layoutBox.borderWidth(),
                                          m_layoutBox.borderHeight());
      else
        newReferenceBoxLogicalSize.shrink(m_layoutBox.borderHeight(),
                                          m_layoutBox.borderWidth());
      break;
    case ContentBox:
      if (isHorizontalWritingMode)
        newReferenceBoxLogicalSize.shrink(m_layoutBox.borderAndPaddingWidth(),
                                          m_layoutBox.borderAndPaddingHeight());
      else
        newReferenceBoxLogicalSize.shrink(m_layoutBox.borderAndPaddingHeight(),
                                          m_layoutBox.borderAndPaddingWidth());
      break;
    case BoxMissing:
      ASSERT_NOT_REACHED();
      break;
  }

  newReferenceBoxLogicalSize.clampNegativeToZero();

  if (m_referenceBoxLogicalSize == newReferenceBoxLogicalSize)
    return;
  markShapeAsDirty();
  m_referenceBoxLogicalSize = newReferenceBoxLogicalSize;
}

static bool checkShapeImageOrigin(Document& document,
                                  const StyleImage& styleImage) {
  if (styleImage.isGeneratedImage())
    return true;

  ASSERT(styleImage.cachedImage());
  ImageResourceContent& imageResource = *(styleImage.cachedImage());
  if (imageResource.isAccessAllowed(document.getSecurityOrigin()))
    return true;

  const KURL& url = imageResource.url();
  String urlString = url.isNull() ? "''" : url.elidedString();
  document.addConsoleMessage(
      ConsoleMessage::create(SecurityMessageSource, ErrorMessageLevel,
                             "Unsafe attempt to load URL " + urlString + "."));

  return false;
}

static LayoutRect getShapeImageMarginRect(
    const LayoutBox& layoutBox,
    const LayoutSize& referenceBoxLogicalSize) {
  LayoutPoint marginBoxOrigin(
      -layoutBox.marginLogicalLeft() - layoutBox.borderAndPaddingLogicalLeft(),
      -layoutBox.marginBefore() - layoutBox.borderBefore() -
          layoutBox.paddingBefore());
  LayoutSize marginBoxSizeDelta(
      layoutBox.marginLogicalWidth() + layoutBox.borderAndPaddingLogicalWidth(),
      layoutBox.marginLogicalHeight() +
          layoutBox.borderAndPaddingLogicalHeight());
  LayoutSize marginRectSize(referenceBoxLogicalSize + marginBoxSizeDelta);
  marginRectSize.clampNegativeToZero();
  return LayoutRect(marginBoxOrigin, marginRectSize);
}

static bool isValidRasterShapeRect(const LayoutRect& rect) {
  static double maxImageSizeBytes = 0;
  if (!maxImageSizeBytes) {
    size_t size32MaxBytes =
        0xFFFFFFFF / 4;  // Some platforms don't limit maxDecodedImageBytes.
    maxImageSizeBytes =
        std::min(size32MaxBytes, Platform::current()->maxDecodedImageBytes());
  }
  return (rect.width().toFloat() * rect.height().toFloat() * 4.0) <
         maxImageSizeBytes;
}

std::unique_ptr<Shape> ShapeOutsideInfo::createShapeForImage(
    StyleImage* styleImage,
    float shapeImageThreshold,
    WritingMode writingMode,
    float margin) const {
  const LayoutSize& imageSize =
      styleImage->imageSize(m_layoutBox, m_layoutBox.style()->effectiveZoom(),
                            m_referenceBoxLogicalSize);

  const LayoutRect& marginRect =
      getShapeImageMarginRect(m_layoutBox, m_referenceBoxLogicalSize);
  const LayoutRect& imageRect =
      (m_layoutBox.isLayoutImage())
          ? toLayoutImage(m_layoutBox).replacedContentRect()
          : LayoutRect(LayoutPoint(), imageSize);

  if (!isValidRasterShapeRect(marginRect) ||
      !isValidRasterShapeRect(imageRect)) {
    m_layoutBox.document().addConsoleMessage(
        ConsoleMessage::create(RenderingMessageSource, ErrorMessageLevel,
                               "The shape-outside image is too large."));
    return Shape::createEmptyRasterShape(writingMode, margin);
  }

  ASSERT(!styleImage->isPendingImage());
  RefPtr<Image> image =
      styleImage->image(m_layoutBox, flooredIntSize(imageSize),
                        m_layoutBox.style()->effectiveZoom());

  return Shape::createRasterShape(image.get(), shapeImageThreshold, imageRect,
                                  marginRect, writingMode, margin);
}

const Shape& ShapeOutsideInfo::computedShape() const {
  if (Shape* shape = m_shape.get())
    return *shape;

  AutoReset<bool> isInComputingShape(&m_isComputingShape, true);

  const ComputedStyle& style = *m_layoutBox.style();
  ASSERT(m_layoutBox.containingBlock());
  const ComputedStyle& containingBlockStyle =
      *m_layoutBox.containingBlock()->style();

  WritingMode writingMode = containingBlockStyle.getWritingMode();
  // Make sure contentWidth is not negative. This can happen when containing
  // block has a vertical scrollbar and its content is smaller than the
  // scrollbar width.
  LayoutUnit maximumValue =
      m_layoutBox.containingBlock()
          ? std::max(LayoutUnit(),
                     m_layoutBox.containingBlock()->contentWidth())
          : LayoutUnit();
  float margin = floatValueForLength(m_layoutBox.style()->shapeMargin(),
                                     maximumValue.toFloat());

  float shapeImageThreshold = style.shapeImageThreshold();
  ASSERT(style.shapeOutside());
  const ShapeValue& shapeValue = *style.shapeOutside();

  switch (shapeValue.type()) {
    case ShapeValue::Shape:
      ASSERT(shapeValue.shape());
      m_shape = Shape::createShape(
          shapeValue.shape(), m_referenceBoxLogicalSize, writingMode, margin);
      break;
    case ShapeValue::Image:
      ASSERT(shapeValue.isImageValid());
      m_shape = createShapeForImage(shapeValue.image(), shapeImageThreshold,
                                    writingMode, margin);
      break;
    case ShapeValue::Box: {
      const FloatRoundedRect& shapeRect = style.getRoundedBorderFor(
          LayoutRect(LayoutPoint(), m_referenceBoxLogicalSize),
          m_layoutBox.view());
      m_shape = Shape::createLayoutBoxShape(shapeRect, writingMode, margin);
      break;
    }
  }

  ASSERT(m_shape);
  return *m_shape;
}

inline LayoutUnit borderBeforeInWritingMode(const LayoutBox& layoutBox,
                                            WritingMode writingMode) {
  switch (writingMode) {
    case WritingMode::kHorizontalTb:
      return LayoutUnit(layoutBox.borderTop());
    case WritingMode::kVerticalLr:
      return LayoutUnit(layoutBox.borderLeft());
    case WritingMode::kVerticalRl:
      return LayoutUnit(layoutBox.borderRight());
  }

  ASSERT_NOT_REACHED();
  return LayoutUnit(layoutBox.borderBefore());
}

inline LayoutUnit borderAndPaddingBeforeInWritingMode(
    const LayoutBox& layoutBox,
    WritingMode writingMode) {
  switch (writingMode) {
    case WritingMode::kHorizontalTb:
      return layoutBox.borderTop() + layoutBox.paddingTop();
    case WritingMode::kVerticalLr:
      return layoutBox.borderLeft() + layoutBox.paddingLeft();
    case WritingMode::kVerticalRl:
      return layoutBox.borderRight() + layoutBox.paddingRight();
  }

  ASSERT_NOT_REACHED();
  return layoutBox.borderAndPaddingBefore();
}

LayoutUnit ShapeOutsideInfo::logicalTopOffset() const {
  switch (referenceBox(*m_layoutBox.style()->shapeOutside())) {
    case MarginBox:
      return -m_layoutBox.marginBefore(m_layoutBox.containingBlock()->style());
    case BorderBox:
      return LayoutUnit();
    case PaddingBox:
      return borderBeforeInWritingMode(
          m_layoutBox,
          m_layoutBox.containingBlock()->style()->getWritingMode());
    case ContentBox:
      return borderAndPaddingBeforeInWritingMode(
          m_layoutBox,
          m_layoutBox.containingBlock()->style()->getWritingMode());
    case BoxMissing:
      break;
  }

  ASSERT_NOT_REACHED();
  return LayoutUnit();
}

inline LayoutUnit borderStartWithStyleForWritingMode(
    const LayoutBox& layoutBox,
    const ComputedStyle* style) {
  if (style->isHorizontalWritingMode()) {
    if (style->isLeftToRightDirection())
      return LayoutUnit(layoutBox.borderLeft());

    return LayoutUnit(layoutBox.borderRight());
  }
  if (style->isLeftToRightDirection())
    return LayoutUnit(layoutBox.borderTop());

  return LayoutUnit(layoutBox.borderBottom());
}

inline LayoutUnit borderAndPaddingStartWithStyleForWritingMode(
    const LayoutBox& layoutBox,
    const ComputedStyle* style) {
  if (style->isHorizontalWritingMode()) {
    if (style->isLeftToRightDirection())
      return layoutBox.borderLeft() + layoutBox.paddingLeft();

    return layoutBox.borderRight() + layoutBox.paddingRight();
  }
  if (style->isLeftToRightDirection())
    return layoutBox.borderTop() + layoutBox.paddingTop();

  return layoutBox.borderBottom() + layoutBox.paddingBottom();
}

LayoutUnit ShapeOutsideInfo::logicalLeftOffset() const {
  switch (referenceBox(*m_layoutBox.style()->shapeOutside())) {
    case MarginBox:
      return -m_layoutBox.marginStart(m_layoutBox.containingBlock()->style());
    case BorderBox:
      return LayoutUnit();
    case PaddingBox:
      return borderStartWithStyleForWritingMode(
          m_layoutBox, m_layoutBox.containingBlock()->style());
    case ContentBox:
      return borderAndPaddingStartWithStyleForWritingMode(
          m_layoutBox, m_layoutBox.containingBlock()->style());
    case BoxMissing:
      break;
  }

  ASSERT_NOT_REACHED();
  return LayoutUnit();
}

bool ShapeOutsideInfo::isEnabledFor(const LayoutBox& box) {
  ShapeValue* shapeValue = box.style()->shapeOutside();
  if (!box.isFloating() || !shapeValue)
    return false;

  switch (shapeValue->type()) {
    case ShapeValue::Shape:
      return shapeValue->shape();
    case ShapeValue::Image:
      return shapeValue->isImageValid() &&
             checkShapeImageOrigin(box.document(), *(shapeValue->image()));
    case ShapeValue::Box:
      return true;
  }

  return false;
}

ShapeOutsideDeltas ShapeOutsideInfo::computeDeltasForContainingBlockLine(
    const LineLayoutBlockFlow& containingBlock,
    const FloatingObject& floatingObject,
    LayoutUnit lineTop,
    LayoutUnit lineHeight) {
  ASSERT(lineHeight >= 0);

  LayoutUnit borderBoxTop = containingBlock.logicalTopForFloat(floatingObject) +
                            containingBlock.marginBeforeForChild(m_layoutBox);
  LayoutUnit borderBoxLineTop = lineTop - borderBoxTop;

  if (isShapeDirty() ||
      !m_shapeOutsideDeltas.isForLine(borderBoxLineTop, lineHeight)) {
    LayoutUnit referenceBoxLineTop = borderBoxLineTop - logicalTopOffset();
    LayoutUnit floatMarginBoxWidth = std::max(
        containingBlock.logicalWidthForFloat(floatingObject), LayoutUnit());

    if (computedShape().lineOverlapsShapeMarginBounds(referenceBoxLineTop,
                                                      lineHeight)) {
      LineSegment segment = computedShape().getExcludedInterval(
          (borderBoxLineTop - logicalTopOffset()),
          std::min(lineHeight, shapeLogicalBottom() - borderBoxLineTop));
      if (segment.isValid) {
        LayoutUnit logicalLeftMargin =
            containingBlock.style()->isLeftToRightDirection()
                ? containingBlock.marginStartForChild(m_layoutBox)
                : containingBlock.marginEndForChild(m_layoutBox);
        LayoutUnit rawLeftMarginBoxDelta(
            segment.logicalLeft + logicalLeftOffset() + logicalLeftMargin);
        LayoutUnit leftMarginBoxDelta = clampTo<LayoutUnit>(
            rawLeftMarginBoxDelta, LayoutUnit(), floatMarginBoxWidth);

        LayoutUnit logicalRightMargin =
            containingBlock.style()->isLeftToRightDirection()
                ? containingBlock.marginEndForChild(m_layoutBox)
                : containingBlock.marginStartForChild(m_layoutBox);
        LayoutUnit rawRightMarginBoxDelta(
            segment.logicalRight + logicalLeftOffset() -
            containingBlock.logicalWidthForChild(m_layoutBox) -
            logicalRightMargin);
        LayoutUnit rightMarginBoxDelta = clampTo<LayoutUnit>(
            rawRightMarginBoxDelta, -floatMarginBoxWidth, LayoutUnit());

        m_shapeOutsideDeltas =
            ShapeOutsideDeltas(leftMarginBoxDelta, rightMarginBoxDelta, true,
                               borderBoxLineTop, lineHeight);
        return m_shapeOutsideDeltas;
      }
    }

    // Lines that do not overlap the shape should act as if the float
    // wasn't there for layout purposes. So we set the deltas to remove the
    // entire width of the float.
    m_shapeOutsideDeltas =
        ShapeOutsideDeltas(floatMarginBoxWidth, -floatMarginBoxWidth, false,
                           borderBoxLineTop, lineHeight);
  }

  return m_shapeOutsideDeltas;
}

LayoutRect ShapeOutsideInfo::computedShapePhysicalBoundingBox() const {
  LayoutRect physicalBoundingBox =
      computedShape().shapeMarginLogicalBoundingBox();
  physicalBoundingBox.setX(physicalBoundingBox.x() + logicalLeftOffset());

  if (m_layoutBox.style()->isFlippedBlocksWritingMode())
    physicalBoundingBox.setY(m_layoutBox.logicalHeight() -
                             physicalBoundingBox.maxY());
  else
    physicalBoundingBox.setY(physicalBoundingBox.y() + logicalTopOffset());

  if (!m_layoutBox.style()->isHorizontalWritingMode())
    physicalBoundingBox = physicalBoundingBox.transposedRect();
  else
    physicalBoundingBox.setY(physicalBoundingBox.y() + logicalTopOffset());

  return physicalBoundingBox;
}

FloatPoint ShapeOutsideInfo::shapeToLayoutObjectPoint(FloatPoint point) const {
  FloatPoint result = FloatPoint(point.x() + logicalLeftOffset(),
                                 point.y() + logicalTopOffset());
  if (m_layoutBox.style()->isFlippedBlocksWritingMode())
    result.setY(m_layoutBox.logicalHeight() - result.y());
  if (!m_layoutBox.style()->isHorizontalWritingMode())
    result = result.transposedPoint();
  return result;
}

FloatSize ShapeOutsideInfo::shapeToLayoutObjectSize(FloatSize size) const {
  if (!m_layoutBox.style()->isHorizontalWritingMode())
    return size.transposedSize();
  return size;
}

}  // namespace blink