98b9dfe048d87d95287ac8ee5be62054e746aeca
[WebKit-https.git] / Source / WebCore / rendering / style / RenderStyle.cpp
1 /*
2  * Copyright (C) 1999 Antti Koivisto (koivisto@kde.org)
3  * Copyright (C) 2004-2015 Apple Inc. All rights reserved.
4  * Copyright (C) 2011 Adobe Systems Incorporated. All rights reserved.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22
23 #include "config.h"
24 #include "RenderStyle.h"
25
26 #include "ContentData.h"
27 #include "CursorList.h"
28 #include "CSSPropertyNames.h"
29 #include "FloatRoundedRect.h"
30 #include "FontCascade.h"
31 #include "FontSelector.h"
32 #include "InlineTextBoxStyle.h"
33 #include "Pagination.h"
34 #include "QuotesData.h"
35 #include "RenderObject.h"
36 #include "ScaleTransformOperation.h"
37 #include "ShadowData.h"
38 #include "StyleImage.h"
39 #include "StyleInheritedData.h"
40 #include "StyleResolver.h"
41 #include "StyleScrollSnapPoints.h"
42 #include <wtf/MathExtras.h>
43 #include <wtf/StdLibExtras.h>
44 #include <algorithm>
45
46 #if ENABLE(IOS_TEXT_AUTOSIZING)
47 #include <wtf/text/StringHash.h>
48 #endif
49
50 #if ENABLE(TEXT_AUTOSIZING)
51 #include "TextAutosizer.h"
52 #endif
53
54 #if ENABLE(TOUCH_EVENTS)
55 #include "RenderTheme.h"
56 #endif
57
58 namespace WebCore {
59
60 struct SameSizeAsBorderValue {
61     float m_width;
62     RGBA32 m_color;
63     int m_restBits;
64 };
65
66 COMPILE_ASSERT(sizeof(BorderValue) == sizeof(SameSizeAsBorderValue), BorderValue_should_not_grow);
67
68 struct SameSizeAsRenderStyle : public RefCounted<SameSizeAsRenderStyle> {
69     void* dataRefs[7];
70     void* ownPtrs[1];
71     void* dataRefSvgStyle;
72     struct InheritedFlags {
73         unsigned m_bitfields[2];
74     } inherited_flags;
75
76     struct NonInheritedFlags {
77         uint64_t m_flags;
78     } noninherited_flags;
79 };
80
81 COMPILE_ASSERT(sizeof(RenderStyle) == sizeof(SameSizeAsRenderStyle), RenderStyle_should_stay_small);
82
83 inline RenderStyle& defaultStyle()
84 {
85     static RenderStyle& style = RenderStyle::createDefaultStyle().leakRef();
86     return style;
87 }
88
89 Ref<RenderStyle> RenderStyle::create()
90 {
91     return adoptRef(*new RenderStyle(defaultStyle()));
92 }
93
94 Ref<RenderStyle> RenderStyle::createDefaultStyle()
95 {
96     return adoptRef(*new RenderStyle(true));
97 }
98
99 Ref<RenderStyle> RenderStyle::createAnonymousStyleWithDisplay(const RenderStyle* parentStyle, EDisplay display)
100 {
101     auto newStyle = RenderStyle::create();
102     newStyle.get().inheritFrom(parentStyle);
103     newStyle.get().inheritUnicodeBidiFrom(parentStyle);
104     newStyle.get().setDisplay(display);
105     return newStyle;
106 }
107
108 Ref<RenderStyle> RenderStyle::clone(const RenderStyle* other)
109 {
110     return adoptRef(*new RenderStyle(*other));
111 }
112
113 Ref<RenderStyle> RenderStyle::createStyleInheritingFromPseudoStyle(const RenderStyle& pseudoStyle)
114 {
115     ASSERT(pseudoStyle.styleType() == BEFORE || pseudoStyle.styleType() == AFTER);
116
117     auto style = RenderStyle::create();
118     style.get().inheritFrom(&pseudoStyle);
119     return style;
120 }
121
122 ALWAYS_INLINE RenderStyle::RenderStyle(bool)
123     : m_box(StyleBoxData::create())
124     , visual(StyleVisualData::create())
125     , m_background(StyleBackgroundData::create())
126     , surround(StyleSurroundData::create())
127     , rareNonInheritedData(StyleRareNonInheritedData::create())
128     , rareInheritedData(StyleRareInheritedData::create())
129     , inherited(StyleInheritedData::create())
130     , m_svgStyle(SVGRenderStyle::create())
131 {
132     inherited_flags._empty_cells = initialEmptyCells();
133     inherited_flags._caption_side = initialCaptionSide();
134     inherited_flags._list_style_type = initialListStyleType();
135     inherited_flags._list_style_position = initialListStylePosition();
136     inherited_flags._visibility = initialVisibility();
137     inherited_flags._text_align = initialTextAlign();
138     inherited_flags._text_transform = initialTextTransform();
139     inherited_flags._text_decorations = initialTextDecoration();
140     inherited_flags._cursor_style = initialCursor();
141 #if ENABLE(CURSOR_VISIBILITY)
142     inherited_flags.m_cursorVisibility = initialCursorVisibility();
143 #endif
144     inherited_flags._direction = initialDirection();
145     inherited_flags._white_space = initialWhiteSpace();
146     inherited_flags._border_collapse = initialBorderCollapse();
147     inherited_flags.m_rtlOrdering = initialRTLOrdering();
148     inherited_flags._box_direction = initialBoxDirection();
149     inherited_flags.m_printColorAdjust = initialPrintColorAdjust();
150     inherited_flags._pointerEvents = initialPointerEvents();
151     inherited_flags._insideLink = NotInsideLink;
152     inherited_flags._insideDefaultButton = false;
153     inherited_flags.m_writingMode = initialWritingMode();
154
155     static_assert((sizeof(InheritedFlags) <= 8), "InheritedFlags does not grow");
156     static_assert((sizeof(NonInheritedFlags) <= 8), "NonInheritedFlags does not grow");
157 }
158
159 ALWAYS_INLINE RenderStyle::RenderStyle(const RenderStyle& o)
160     : RefCounted<RenderStyle>()
161     , m_box(o.m_box)
162     , visual(o.visual)
163     , m_background(o.m_background)
164     , surround(o.surround)
165     , rareNonInheritedData(o.rareNonInheritedData)
166     , rareInheritedData(o.rareInheritedData)
167     , inherited(o.inherited)
168     , m_svgStyle(o.m_svgStyle)
169     , inherited_flags(o.inherited_flags)
170     , noninherited_flags(o.noninherited_flags)
171 {
172 }
173
174 void RenderStyle::inheritFrom(const RenderStyle* inheritParent, IsAtShadowBoundary isAtShadowBoundary)
175 {
176     if (isAtShadowBoundary == AtShadowBoundary) {
177         // Even if surrounding content is user-editable, shadow DOM should act as a single unit, and not necessarily be editable
178         EUserModify currentUserModify = userModify();
179         rareInheritedData = inheritParent->rareInheritedData;
180         setUserModify(currentUserModify);
181     } else
182         rareInheritedData = inheritParent->rareInheritedData;
183     inherited = inheritParent->inherited;
184     inherited_flags = inheritParent->inherited_flags;
185
186     if (m_svgStyle != inheritParent->m_svgStyle)
187         m_svgStyle.access()->inheritFrom(inheritParent->m_svgStyle.get());
188 }
189
190 void RenderStyle::copyNonInheritedFrom(const RenderStyle* other)
191 {
192     m_box = other->m_box;
193     visual = other->visual;
194     m_background = other->m_background;
195     surround = other->surround;
196     rareNonInheritedData = other->rareNonInheritedData;
197     noninherited_flags.copyNonInheritedFrom(other->noninherited_flags);
198
199     if (m_svgStyle != other->m_svgStyle)
200         m_svgStyle.access()->copyNonInheritedFrom(other->m_svgStyle.get());
201
202     ASSERT(zoom() == initialZoom());
203 }
204
205 bool RenderStyle::operator==(const RenderStyle& o) const
206 {
207     // compare everything except the pseudoStyle pointer
208     return inherited_flags == o.inherited_flags
209         && noninherited_flags == o.noninherited_flags
210         && m_box == o.m_box
211         && visual == o.visual
212         && m_background == o.m_background
213         && surround == o.surround
214         && rareNonInheritedData == o.rareNonInheritedData
215         && rareInheritedData == o.rareInheritedData
216         && inherited == o.inherited
217         && m_svgStyle == o.m_svgStyle
218             ;
219 }
220
221 bool RenderStyle::isStyleAvailable() const
222 {
223     return this != StyleResolver::styleNotYetAvailable();
224 }
225
226 bool RenderStyle::hasUniquePseudoStyle() const
227 {
228     if (!m_cachedPseudoStyles || styleType() != NOPSEUDO)
229         return false;
230
231     for (size_t i = 0; i < m_cachedPseudoStyles->size(); ++i) {
232         RenderStyle* pseudoStyle = m_cachedPseudoStyles->at(i).get();
233         if (pseudoStyle->unique())
234             return true;
235     }
236
237     return false;
238 }
239
240 RenderStyle* RenderStyle::getCachedPseudoStyle(PseudoId pid) const
241 {
242     if (!m_cachedPseudoStyles || !m_cachedPseudoStyles->size())
243         return 0;
244
245     if (styleType() != NOPSEUDO) 
246         return 0;
247
248     for (size_t i = 0; i < m_cachedPseudoStyles->size(); ++i) {
249         RenderStyle* pseudoStyle = m_cachedPseudoStyles->at(i).get();
250         if (pseudoStyle->styleType() == pid)
251             return pseudoStyle;
252     }
253
254     return 0;
255 }
256
257 RenderStyle* RenderStyle::addCachedPseudoStyle(PassRefPtr<RenderStyle> pseudo)
258 {
259     if (!pseudo)
260         return 0;
261
262     ASSERT(pseudo->styleType() > NOPSEUDO);
263
264     RenderStyle* result = pseudo.get();
265
266     if (!m_cachedPseudoStyles)
267         m_cachedPseudoStyles = std::make_unique<PseudoStyleCache>();
268
269     m_cachedPseudoStyles->append(pseudo);
270
271     return result;
272 }
273
274 void RenderStyle::removeCachedPseudoStyle(PseudoId pid)
275 {
276     if (!m_cachedPseudoStyles)
277         return;
278     for (size_t i = 0; i < m_cachedPseudoStyles->size(); ++i) {
279         RenderStyle* pseudoStyle = m_cachedPseudoStyles->at(i).get();
280         if (pseudoStyle->styleType() == pid) {
281             m_cachedPseudoStyles->remove(i);
282             return;
283         }
284     }
285 }
286
287 bool RenderStyle::inheritedNotEqual(const RenderStyle* other) const
288 {
289     return inherited_flags != other->inherited_flags
290            || inherited != other->inherited
291            || m_svgStyle->inheritedNotEqual(other->m_svgStyle.get())
292            || rareInheritedData != other->rareInheritedData;
293 }
294
295 #if ENABLE(IOS_TEXT_AUTOSIZING)
296 inline unsigned computeFontHash(const FontCascade& font)
297 {
298     unsigned hashCodes[2] = {
299         CaseFoldingHash::hash(font.fontDescription().firstFamily().impl()),
300         static_cast<unsigned>(font.fontDescription().specifiedSize())
301     };
302     return StringHasher::computeHash(reinterpret_cast<UChar*>(hashCodes), 2 * sizeof(unsigned) / sizeof(UChar));
303 }
304
305 uint32_t RenderStyle::hashForTextAutosizing() const
306 {
307     // FIXME: Not a very smart hash. Could be improved upon. See <https://bugs.webkit.org/show_bug.cgi?id=121131>.
308     uint32_t hash = 0;
309     
310     hash ^= rareNonInheritedData->m_appearance;
311     hash ^= rareNonInheritedData->marginBeforeCollapse;
312     hash ^= rareNonInheritedData->marginAfterCollapse;
313     hash ^= rareNonInheritedData->lineClamp.value();
314     hash ^= rareInheritedData->overflowWrap;
315     hash ^= rareInheritedData->nbspMode;
316     hash ^= rareInheritedData->lineBreak;
317     hash ^= WTF::FloatHash<float>::hash(inherited->specifiedLineHeight.value());
318     hash ^= computeFontHash(inherited->fontCascade);
319     hash ^= inherited->horizontal_border_spacing;
320     hash ^= inherited->vertical_border_spacing;
321     hash ^= inherited_flags._box_direction;
322     hash ^= inherited_flags.m_rtlOrdering;
323     hash ^= noninherited_flags.position();
324     hash ^= noninherited_flags.floating();
325     hash ^= rareNonInheritedData->textOverflow;
326     hash ^= rareInheritedData->textSecurity;
327     return hash;
328 }
329
330 bool RenderStyle::equalForTextAutosizing(const RenderStyle* other) const
331 {
332     return rareNonInheritedData->m_appearance == other->rareNonInheritedData->m_appearance
333         && rareNonInheritedData->marginBeforeCollapse == other->rareNonInheritedData->marginBeforeCollapse
334         && rareNonInheritedData->marginAfterCollapse == other->rareNonInheritedData->marginAfterCollapse
335         && rareNonInheritedData->lineClamp == other->rareNonInheritedData->lineClamp
336         && rareInheritedData->textSizeAdjust == other->rareInheritedData->textSizeAdjust
337         && rareInheritedData->overflowWrap == other->rareInheritedData->overflowWrap
338         && rareInheritedData->nbspMode == other->rareInheritedData->nbspMode
339         && rareInheritedData->lineBreak == other->rareInheritedData->lineBreak
340         && rareInheritedData->textSecurity == other->rareInheritedData->textSecurity
341         && inherited->specifiedLineHeight == other->inherited->specifiedLineHeight
342         && inherited->fontCascade.equalForTextAutoSizing(other->inherited->fontCascade)
343         && inherited->horizontal_border_spacing == other->inherited->horizontal_border_spacing
344         && inherited->vertical_border_spacing == other->inherited->vertical_border_spacing
345         && inherited_flags._box_direction == other->inherited_flags._box_direction
346         && inherited_flags.m_rtlOrdering == other->inherited_flags.m_rtlOrdering
347         && noninherited_flags.position() == other->noninherited_flags.position()
348         && noninherited_flags.floating() == other->noninherited_flags.floating()
349         && rareNonInheritedData->textOverflow == other->rareNonInheritedData->textOverflow;
350 }
351 #endif // ENABLE(IOS_TEXT_AUTOSIZING)
352
353 bool RenderStyle::inheritedDataShared(const RenderStyle* other) const
354 {
355     // This is a fast check that only looks if the data structures are shared.
356     return inherited_flags == other->inherited_flags
357         && inherited.get() == other->inherited.get()
358         && m_svgStyle.get() == other->m_svgStyle.get()
359         && rareInheritedData.get() == other->rareInheritedData.get();
360 }
361
362 static bool positionChangeIsMovementOnly(const LengthBox& a, const LengthBox& b, const Length& width)
363 {
364     // If any unit types are different, then we can't guarantee
365     // that this was just a movement.
366     if (a.left().type() != b.left().type()
367         || a.right().type() != b.right().type()
368         || a.top().type() != b.top().type()
369         || a.bottom().type() != b.bottom().type())
370         return false;
371
372     // Only one unit can be non-auto in the horizontal direction and
373     // in the vertical direction.  Otherwise the adjustment of values
374     // is changing the size of the box.
375     if (!a.left().isIntrinsicOrAuto() && !a.right().isIntrinsicOrAuto())
376         return false;
377     if (!a.top().isIntrinsicOrAuto() && !a.bottom().isIntrinsicOrAuto())
378         return false;
379     // If our width is auto and left or right is specified then this 
380     // is not just a movement - we need to resize to our container.
381     if ((!a.left().isIntrinsicOrAuto() || !a.right().isIntrinsicOrAuto()) && width.isIntrinsicOrAuto())
382         return false;
383
384     // One of the units is fixed or percent in both directions and stayed
385     // that way in the new style.  Therefore all we are doing is moving.
386     return true;
387 }
388
389 inline bool RenderStyle::changeAffectsVisualOverflow(const RenderStyle& other) const
390 {
391     if (rareNonInheritedData.get() != other.rareNonInheritedData.get()
392         && !rareNonInheritedData->shadowDataEquivalent(*other.rareNonInheritedData.get()))
393         return true;
394
395     if (rareInheritedData.get() != other.rareInheritedData.get()
396         && !rareInheritedData->shadowDataEquivalent(*other.rareInheritedData.get()))
397         return true;
398
399     if (inherited_flags._text_decorations != other.inherited_flags._text_decorations
400         || visual->textDecoration != other.visual->textDecoration
401         || rareNonInheritedData->m_textDecorationStyle != other.rareNonInheritedData->m_textDecorationStyle) {
402         // Underlines are always drawn outside of their textbox bounds when text-underline-position: under;
403         // is specified. We can take an early out here.
404         if (textUnderlinePosition() == TextUnderlinePositionUnder
405             || other.textUnderlinePosition() == TextUnderlinePositionUnder)
406             return true;
407         return visualOverflowForDecorations(*this, nullptr) != visualOverflowForDecorations(other, nullptr);
408     }
409
410     return false;
411 }
412
413 bool RenderStyle::changeRequiresLayout(const RenderStyle& other, unsigned& changedContextSensitiveProperties) const
414 {
415     if (m_box->width() != other.m_box->width()
416         || m_box->minWidth() != other.m_box->minWidth()
417         || m_box->maxWidth() != other.m_box->maxWidth()
418         || m_box->height() != other.m_box->height()
419         || m_box->minHeight() != other.m_box->minHeight()
420         || m_box->maxHeight() != other.m_box->maxHeight())
421         return true;
422
423     if (m_box->verticalAlign() != other.m_box->verticalAlign() || noninherited_flags.verticalAlign() != other.noninherited_flags.verticalAlign())
424         return true;
425
426     if (m_box->boxSizing() != other.m_box->boxSizing())
427         return true;
428
429     if (surround->margin != other.surround->margin)
430         return true;
431
432     if (surround->padding != other.surround->padding)
433         return true;
434
435     // FIXME: We should add an optimized form of layout that just recomputes visual overflow.
436     if (changeAffectsVisualOverflow(other))
437         return true;
438
439     if (rareNonInheritedData.get() != other.rareNonInheritedData.get()) {
440         if (rareNonInheritedData->m_appearance != other.rareNonInheritedData->m_appearance
441             || rareNonInheritedData->marginBeforeCollapse != other.rareNonInheritedData->marginBeforeCollapse
442             || rareNonInheritedData->marginAfterCollapse != other.rareNonInheritedData->marginAfterCollapse
443             || rareNonInheritedData->lineClamp != other.rareNonInheritedData->lineClamp
444             || rareNonInheritedData->m_initialLetter != other.rareNonInheritedData->m_initialLetter
445             || rareNonInheritedData->textOverflow != other.rareNonInheritedData->textOverflow)
446             return true;
447
448         if (rareNonInheritedData->m_regionFragment != other.rareNonInheritedData->m_regionFragment)
449             return true;
450
451 #if ENABLE(CSS_SHAPES)
452         if (rareNonInheritedData->m_shapeMargin != other.rareNonInheritedData->m_shapeMargin)
453             return true;
454 #endif
455
456         if (rareNonInheritedData->m_deprecatedFlexibleBox.get() != other.rareNonInheritedData->m_deprecatedFlexibleBox.get()
457             && *rareNonInheritedData->m_deprecatedFlexibleBox.get() != *other.rareNonInheritedData->m_deprecatedFlexibleBox.get())
458             return true;
459
460         if (rareNonInheritedData->m_flexibleBox.get() != other.rareNonInheritedData->m_flexibleBox.get()
461             && *rareNonInheritedData->m_flexibleBox.get() != *other.rareNonInheritedData->m_flexibleBox.get())
462             return true;
463         if (rareNonInheritedData->m_order != other.rareNonInheritedData->m_order
464             || rareNonInheritedData->m_alignContent != other.rareNonInheritedData->m_alignContent
465             || rareNonInheritedData->m_alignItems != other.rareNonInheritedData->m_alignItems
466             || rareNonInheritedData->m_alignSelf != other.rareNonInheritedData->m_alignSelf
467             || rareNonInheritedData->m_justifyContent != other.rareNonInheritedData->m_justifyContent)
468             return true;
469
470         if (!rareNonInheritedData->reflectionDataEquivalent(*other.rareNonInheritedData.get()))
471             return true;
472
473         if (rareNonInheritedData->m_multiCol.get() != other.rareNonInheritedData->m_multiCol.get()
474             && *rareNonInheritedData->m_multiCol.get() != *other.rareNonInheritedData->m_multiCol.get())
475             return true;
476
477         if (rareNonInheritedData->m_transform.get() != other.rareNonInheritedData->m_transform.get()
478             && *rareNonInheritedData->m_transform.get() != *other.rareNonInheritedData->m_transform.get()) {
479             changedContextSensitiveProperties |= ContextSensitivePropertyTransform;
480             // Don't return; keep looking for another change
481         }
482
483 #if ENABLE(CSS_GRID_LAYOUT)
484         if (rareNonInheritedData->m_grid.get() != other.rareNonInheritedData->m_grid.get()
485             || rareNonInheritedData->m_gridItem.get() != other.rareNonInheritedData->m_gridItem.get())
486             return true;
487 #endif
488
489 #if ENABLE(DASHBOARD_SUPPORT)
490         // If regions change, trigger a relayout to re-calc regions.
491         if (rareNonInheritedData->m_dashboardRegions != other.rareNonInheritedData->m_dashboardRegions)
492             return true;
493 #endif
494     }
495
496     if (rareInheritedData.get() != other.rareInheritedData.get()) {
497         if (rareInheritedData->indent != other.rareInheritedData->indent
498 #if ENABLE(CSS3_TEXT)
499             || rareInheritedData->m_textAlignLast != other.rareInheritedData->m_textAlignLast
500             || rareInheritedData->m_textJustify != other.rareInheritedData->m_textJustify
501             || rareInheritedData->m_textIndentLine != other.rareInheritedData->m_textIndentLine
502 #endif
503             || rareInheritedData->m_effectiveZoom != other.rareInheritedData->m_effectiveZoom
504 #if ENABLE(IOS_TEXT_AUTOSIZING)
505             || rareInheritedData->textSizeAdjust != other.rareInheritedData->textSizeAdjust
506 #endif
507             || rareInheritedData->wordBreak != other.rareInheritedData->wordBreak
508             || rareInheritedData->overflowWrap != other.rareInheritedData->overflowWrap
509             || rareInheritedData->nbspMode != other.rareInheritedData->nbspMode
510             || rareInheritedData->lineBreak != other.rareInheritedData->lineBreak
511             || rareInheritedData->textSecurity != other.rareInheritedData->textSecurity
512             || rareInheritedData->hyphens != other.rareInheritedData->hyphens
513             || rareInheritedData->hyphenationLimitBefore != other.rareInheritedData->hyphenationLimitBefore
514             || rareInheritedData->hyphenationLimitAfter != other.rareInheritedData->hyphenationLimitAfter
515             || rareInheritedData->hyphenationString != other.rareInheritedData->hyphenationString
516             || rareInheritedData->locale != other.rareInheritedData->locale
517             || rareInheritedData->m_rubyPosition != other.rareInheritedData->m_rubyPosition
518             || rareInheritedData->textEmphasisMark != other.rareInheritedData->textEmphasisMark
519             || rareInheritedData->textEmphasisPosition != other.rareInheritedData->textEmphasisPosition
520             || rareInheritedData->textEmphasisCustomMark != other.rareInheritedData->textEmphasisCustomMark
521             || rareInheritedData->m_textOrientation != other.rareInheritedData->m_textOrientation
522             || rareInheritedData->m_tabSize != other.rareInheritedData->m_tabSize
523             || rareInheritedData->m_lineBoxContain != other.rareInheritedData->m_lineBoxContain
524             || rareInheritedData->m_lineGrid != other.rareInheritedData->m_lineGrid
525 #if ENABLE(CSS_IMAGE_ORIENTATION)
526             || rareInheritedData->m_imageOrientation != other.rareInheritedData->m_imageOrientation
527 #endif
528 #if ENABLE(CSS_IMAGE_RESOLUTION)
529             || rareInheritedData->m_imageResolutionSource != other.rareInheritedData->m_imageResolutionSource
530             || rareInheritedData->m_imageResolutionSnap != other.rareInheritedData->m_imageResolutionSnap
531             || rareInheritedData->m_imageResolution != other.rareInheritedData->m_imageResolution
532 #endif
533             || rareInheritedData->m_lineSnap != other.rareInheritedData->m_lineSnap
534             || rareInheritedData->m_lineAlign != other.rareInheritedData->m_lineAlign
535 #if ENABLE(ACCELERATED_OVERFLOW_SCROLLING)
536             || rareInheritedData->useTouchOverflowScrolling != other.rareInheritedData->useTouchOverflowScrolling
537 #endif
538             || rareInheritedData->listStyleImage != other.rareInheritedData->listStyleImage)
539             return true;
540
541         if (textStrokeWidth() != other.textStrokeWidth())
542             return true;
543     }
544
545 #if ENABLE(TEXT_AUTOSIZING)
546     if (visual->m_textAutosizingMultiplier != other.visual->m_textAutosizingMultiplier)
547         return true;
548 #endif
549
550     if (inherited->line_height != other.inherited->line_height
551 #if ENABLE(IOS_TEXT_AUTOSIZING)
552         || inherited->specifiedLineHeight != other.inherited->specifiedLineHeight
553 #endif
554         || inherited->fontCascade != other.inherited->fontCascade
555         || inherited->horizontal_border_spacing != other.inherited->horizontal_border_spacing
556         || inherited->vertical_border_spacing != other.inherited->vertical_border_spacing
557         || inherited_flags._box_direction != other.inherited_flags._box_direction
558         || inherited_flags.m_rtlOrdering != other.inherited_flags.m_rtlOrdering
559         || noninherited_flags.position() != other.noninherited_flags.position()
560         || noninherited_flags.floating() != other.noninherited_flags.floating()
561         || noninherited_flags.originalDisplay() != other.noninherited_flags.originalDisplay())
562         return true;
563
564
565     if ((noninherited_flags.effectiveDisplay()) >= TABLE) {
566         if (inherited_flags._border_collapse != other.inherited_flags._border_collapse
567             || inherited_flags._empty_cells != other.inherited_flags._empty_cells
568             || inherited_flags._caption_side != other.inherited_flags._caption_side
569             || noninherited_flags.tableLayout() != other.noninherited_flags.tableLayout())
570             return true;
571
572         // In the collapsing border model, 'hidden' suppresses other borders, while 'none'
573         // does not, so these style differences can be width differences.
574         if (inherited_flags._border_collapse
575             && ((borderTopStyle() == BHIDDEN && other.borderTopStyle() == BNONE)
576                 || (borderTopStyle() == BNONE && other.borderTopStyle() == BHIDDEN)
577                 || (borderBottomStyle() == BHIDDEN && other.borderBottomStyle() == BNONE)
578                 || (borderBottomStyle() == BNONE && other.borderBottomStyle() == BHIDDEN)
579                 || (borderLeftStyle() == BHIDDEN && other.borderLeftStyle() == BNONE)
580                 || (borderLeftStyle() == BNONE && other.borderLeftStyle() == BHIDDEN)
581                 || (borderRightStyle() == BHIDDEN && other.borderRightStyle() == BNONE)
582                 || (borderRightStyle() == BNONE && other.borderRightStyle() == BHIDDEN)))
583             return true;
584     }
585
586     if (noninherited_flags.effectiveDisplay() == LIST_ITEM) {
587         if (inherited_flags._list_style_type != other.inherited_flags._list_style_type
588             || inherited_flags._list_style_position != other.inherited_flags._list_style_position)
589             return true;
590     }
591
592     if (inherited_flags._text_align != other.inherited_flags._text_align
593         || inherited_flags._text_transform != other.inherited_flags._text_transform
594         || inherited_flags._direction != other.inherited_flags._direction
595         || inherited_flags._white_space != other.inherited_flags._white_space
596         || noninherited_flags.clear() != other.noninherited_flags.clear()
597         || noninherited_flags.unicodeBidi() != other.noninherited_flags.unicodeBidi())
598         return true;
599
600     // Check block flow direction.
601     if (inherited_flags.m_writingMode != other.inherited_flags.m_writingMode)
602         return true;
603
604     // Check text combine mode.
605     if (rareNonInheritedData->m_textCombine != other.rareNonInheritedData->m_textCombine)
606         return true;
607
608     // Overflow returns a layout hint.
609     if (noninherited_flags.overflowX() != other.noninherited_flags.overflowX()
610         || noninherited_flags.overflowY() != other.noninherited_flags.overflowY())
611         return true;
612
613     // If our border widths change, then we need to layout.  Other changes to borders
614     // only necessitate a repaint.
615     if (borderLeftWidth() != other.borderLeftWidth()
616         || borderTopWidth() != other.borderTopWidth()
617         || borderBottomWidth() != other.borderBottomWidth()
618         || borderRightWidth() != other.borderRightWidth())
619         return true;
620
621     // If the counter directives change, trigger a relayout to re-calculate counter values and rebuild the counter node tree.
622     const CounterDirectiveMap* mapA = rareNonInheritedData->m_counterDirectives.get();
623     const CounterDirectiveMap* mapB = other.rareNonInheritedData->m_counterDirectives.get();
624     if (!(mapA == mapB || (mapA && mapB && *mapA == *mapB)))
625         return true;
626
627     if ((visibility() == COLLAPSE) != (other.visibility() == COLLAPSE))
628         return true;
629
630     if (rareNonInheritedData->hasOpacity() != other.rareNonInheritedData->hasOpacity()) {
631         // FIXME: We would like to use SimplifiedLayout here, but we can't quite do that yet.
632         // We need to make sure SimplifiedLayout can operate correctly on RenderInlines (we will need
633         // to add a selfNeedsSimplifiedLayout bit in order to not get confused and taint every line).
634         // In addition we need to solve the floating object issue when layers come and go. Right now
635         // a full layout is necessary to keep floating object lists sane.
636         return true;
637     }
638
639     if (rareNonInheritedData->hasFilters() != other.rareNonInheritedData->hasFilters())
640         return true;
641
642 #if ENABLE(FILTERS_LEVEL_2)
643     if (rareNonInheritedData->hasBackdropFilters() != other.rareNonInheritedData->hasBackdropFilters())
644         return true;
645 #endif
646
647     const QuotesData* quotesDataA = rareInheritedData->quotes.get();
648     const QuotesData* quotesDataB = other.rareInheritedData->quotes.get();
649     if (!(quotesDataA == quotesDataB || (quotesDataA && quotesDataB && *quotesDataA == *quotesDataB)))
650         return true;
651
652     if (position() != StaticPosition) {
653         if (surround->offset != other.surround->offset) {
654             // FIXME: We would like to use SimplifiedLayout for relative positioning, but we can't quite do that yet.
655             // We need to make sure SimplifiedLayout can operate correctly on RenderInlines (we will need
656             // to add a selfNeedsSimplifiedLayout bit in order to not get confused and taint every line).
657             if (position() != AbsolutePosition)
658                 return true;
659
660             // Optimize for the case where a positioned layer is moving but not changing size.
661             if (!positionChangeIsMovementOnly(surround->offset, other.surround->offset, m_box->width()))
662                 return true;
663         }
664     }
665     
666     return false;
667 }
668
669 bool RenderStyle::changeRequiresPositionedLayoutOnly(const RenderStyle& other, unsigned&) const
670 {
671     if (position() == StaticPosition)
672         return false;
673
674     if (surround->offset != other.surround->offset) {
675         // Optimize for the case where a positioned layer is moving but not changing size.
676         if (position() == AbsolutePosition && positionChangeIsMovementOnly(surround->offset, other.surround->offset, m_box->width()))
677             return true;
678     }
679     
680     return false;
681 }
682
683 bool RenderStyle::changeRequiresLayerRepaint(const RenderStyle& other, unsigned& changedContextSensitiveProperties) const
684 {
685     // StyleResolver has ensured that zIndex is non-auto only if it's applicable.
686     if (m_box->zIndex() != other.m_box->zIndex() || m_box->hasAutoZIndex() != other.m_box->hasAutoZIndex())
687         return true;
688
689     if (position() != StaticPosition) {
690         if (visual->clip != other.visual->clip || visual->hasClip != other.visual->hasClip) {
691             changedContextSensitiveProperties |= ContextSensitivePropertyClipRect;
692             return true;
693         }
694     }
695
696 #if ENABLE(CSS_COMPOSITING)
697     if (rareNonInheritedData->m_effectiveBlendMode != other.rareNonInheritedData->m_effectiveBlendMode)
698         return true;
699 #endif
700
701     if (rareNonInheritedData->opacity != other.rareNonInheritedData->opacity) {
702         changedContextSensitiveProperties |= ContextSensitivePropertyOpacity;
703         // Don't return; keep looking for another change.
704     }
705
706     if (rareNonInheritedData->m_filter.get() != other.rareNonInheritedData->m_filter.get()
707         && *rareNonInheritedData->m_filter.get() != *other.rareNonInheritedData->m_filter.get()) {
708         changedContextSensitiveProperties |= ContextSensitivePropertyFilter;
709         // Don't return; keep looking for another change.
710     }
711
712     if (rareNonInheritedData->m_mask != other.rareNonInheritedData->m_mask
713         || rareNonInheritedData->m_maskBoxImage != other.rareNonInheritedData->m_maskBoxImage)
714         return true;
715
716     return false;
717 }
718
719 bool RenderStyle::changeRequiresRepaint(const RenderStyle& other, unsigned& changedContextSensitiveProperties) const
720 {
721     if (inherited_flags._visibility != other.inherited_flags._visibility
722         || inherited_flags.m_printColorAdjust != other.inherited_flags.m_printColorAdjust
723         || inherited_flags._insideLink != other.inherited_flags._insideLink
724         || inherited_flags._insideDefaultButton != other.inherited_flags._insideDefaultButton
725         || surround->border != other.surround->border
726         || !m_background->isEquivalentForPainting(*other.m_background)
727         || rareInheritedData->userModify != other.rareInheritedData->userModify
728         || rareInheritedData->userSelect != other.rareInheritedData->userSelect
729         || rareNonInheritedData->userDrag != other.rareNonInheritedData->userDrag
730         || rareNonInheritedData->m_borderFit != other.rareNonInheritedData->m_borderFit
731         || rareNonInheritedData->m_objectFit != other.rareNonInheritedData->m_objectFit
732         || rareInheritedData->m_imageRendering != other.rareInheritedData->m_imageRendering)
733         return true;
734
735 #if ENABLE(CSS_SHAPES)
736     if (rareNonInheritedData->m_shapeOutside != other.rareNonInheritedData->m_shapeOutside)
737         return true;
738 #endif
739
740     // FIXME: this should probably be moved to changeRequiresLayerRepaint().
741     if (rareNonInheritedData->m_clipPath != other.rareNonInheritedData->m_clipPath) {
742         changedContextSensitiveProperties |= ContextSensitivePropertyClipPath;
743         // Don't return; keep looking for another change.
744     }
745
746     return false;
747 }
748
749 bool RenderStyle::changeRequiresRepaintIfTextOrBorderOrOutline(const RenderStyle& other, unsigned&) const
750 {
751     if (inherited->color != other.inherited->color
752         || inherited_flags._text_decorations != other.inherited_flags._text_decorations
753         || visual->textDecoration != other.visual->textDecoration
754         || rareNonInheritedData->m_textDecorationStyle != other.rareNonInheritedData->m_textDecorationStyle
755         || rareNonInheritedData->m_textDecorationColor != other.rareNonInheritedData->m_textDecorationColor
756         || rareInheritedData->m_textDecorationSkip != other.rareInheritedData->m_textDecorationSkip
757         || rareInheritedData->textFillColor != other.rareInheritedData->textFillColor
758         || rareInheritedData->textStrokeColor != other.rareInheritedData->textStrokeColor
759         || rareInheritedData->textEmphasisColor != other.rareInheritedData->textEmphasisColor
760         || rareInheritedData->textEmphasisFill != other.rareInheritedData->textEmphasisFill)
761         return true;
762
763     return false;
764 }
765
766 bool RenderStyle::changeRequiresRecompositeLayer(const RenderStyle& other, unsigned&) const
767 {
768     if (rareNonInheritedData.get() != other.rareNonInheritedData.get()) {
769         if (rareNonInheritedData->m_transformStyle3D != other.rareNonInheritedData->m_transformStyle3D
770             || rareNonInheritedData->m_backfaceVisibility != other.rareNonInheritedData->m_backfaceVisibility
771             || rareNonInheritedData->m_perspective != other.rareNonInheritedData->m_perspective
772             || rareNonInheritedData->m_perspectiveOriginX != other.rareNonInheritedData->m_perspectiveOriginX
773             || rareNonInheritedData->m_perspectiveOriginY != other.rareNonInheritedData->m_perspectiveOriginY)
774             return true;
775     }
776
777     return false;
778 }
779
780 StyleDifference RenderStyle::diff(const RenderStyle& other, unsigned& changedContextSensitiveProperties) const
781 {
782     changedContextSensitiveProperties = ContextSensitivePropertyNone;
783
784     StyleDifference svgChange = StyleDifferenceEqual;
785     if (m_svgStyle != other.m_svgStyle) {
786         svgChange = m_svgStyle->diff(other.m_svgStyle.get());
787         if (svgChange == StyleDifferenceLayout)
788             return svgChange;
789     }
790
791     if (changeRequiresLayout(other, changedContextSensitiveProperties))
792         return StyleDifferenceLayout;
793
794     // SVGRenderStyle::diff() might have returned StyleDifferenceRepaint, eg. if fill changes.
795     // If eg. the font-size changed at the same time, we're not allowed to return StyleDifferenceRepaint,
796     // but have to return StyleDifferenceLayout, that's why  this if branch comes after all branches
797     // that are relevant for SVG and might return StyleDifferenceLayout.
798     if (svgChange != StyleDifferenceEqual)
799         return svgChange;
800
801     if (changeRequiresPositionedLayoutOnly(other, changedContextSensitiveProperties))
802         return StyleDifferenceLayoutPositionedMovementOnly;
803
804     if (changeRequiresLayerRepaint(other, changedContextSensitiveProperties))
805         return StyleDifferenceRepaintLayer;
806
807     if (changeRequiresRepaint(other, changedContextSensitiveProperties))
808         return StyleDifferenceRepaint;
809
810     if (changeRequiresRecompositeLayer(other, changedContextSensitiveProperties))
811         return StyleDifferenceRecompositeLayer;
812
813     if (changeRequiresRepaintIfTextOrBorderOrOutline(other, changedContextSensitiveProperties))
814         return StyleDifferenceRepaintIfTextOrBorderOrOutline;
815
816     // Cursors are not checked, since they will be set appropriately in response to mouse events,
817     // so they don't need to cause any repaint or layout.
818
819     // Animations don't need to be checked either.  We always set the new style on the RenderObject, so we will get a chance to fire off
820     // the resulting transition properly.
821     return StyleDifferenceEqual;
822 }
823
824 bool RenderStyle::diffRequiresLayerRepaint(const RenderStyle& style, bool isComposited) const
825 {
826     unsigned changedContextSensitiveProperties = 0;
827
828     if (changeRequiresRepaint(style, changedContextSensitiveProperties))
829         return true;
830
831     if (isComposited && changeRequiresLayerRepaint(style, changedContextSensitiveProperties))
832         return changedContextSensitiveProperties & ContextSensitivePropertyClipRect;
833
834     return false;
835 }
836     
837 void RenderStyle::setMaskImage(const Vector<RefPtr<MaskImageOperation>>& ops)
838 {
839     FillLayer* curLayer = &rareNonInheritedData.access()->m_mask;
840     while (curLayer) {
841         curLayer->setMaskImage(nullptr);
842         curLayer = curLayer->next();
843     }
844
845     curLayer = &rareNonInheritedData.access()->m_mask;
846     FillLayer* prevLayer = nullptr;
847     for (auto& maskImage : ops) {
848         if (!curLayer) {
849             prevLayer->setNext(std::make_unique<FillLayer>(MaskFillLayer));
850             curLayer = prevLayer->next();
851         }
852
853         curLayer->setMaskImage(maskImage);
854         prevLayer = curLayer;
855         curLayer = curLayer->next();
856     }
857 }
858
859 void RenderStyle::setClip(Length top, Length right, Length bottom, Length left)
860 {
861     StyleVisualData* data = visual.access();
862     data->clip.m_top = top;
863     data->clip.m_right = right;
864     data->clip.m_bottom = bottom;
865     data->clip.m_left = left;
866 }
867
868 void RenderStyle::addCursor(PassRefPtr<StyleImage> image, const IntPoint& hotSpot)
869 {
870     if (!rareInheritedData.access()->cursorData)
871         rareInheritedData.access()->cursorData = CursorList::create();
872     rareInheritedData.access()->cursorData->append(CursorData(image, hotSpot));
873 }
874
875 void RenderStyle::setCursorList(PassRefPtr<CursorList> other)
876 {
877     rareInheritedData.access()->cursorData = other;
878 }
879
880 void RenderStyle::setQuotes(PassRefPtr<QuotesData> q)
881 {
882     if (rareInheritedData->quotes == q || (rareInheritedData->quotes && q && *rareInheritedData->quotes == *q))
883         return;
884
885     rareInheritedData.access()->quotes = q;
886 }
887
888 void RenderStyle::clearCursorList()
889 {
890     if (rareInheritedData->cursorData)
891         rareInheritedData.access()->cursorData = 0;
892 }
893
894 void RenderStyle::clearContent()
895 {
896     if (rareNonInheritedData->m_content)
897         rareNonInheritedData.access()->m_content = nullptr;
898 }
899
900 void RenderStyle::appendContent(std::unique_ptr<ContentData> contentData)
901 {
902     auto& content = rareNonInheritedData.access()->m_content;
903     ContentData* lastContent = content.get();
904     while (lastContent && lastContent->next())
905         lastContent = lastContent->next();
906
907     if (lastContent)
908         lastContent->setNext(WTF::move(contentData));
909     else
910         content = WTF::move(contentData);
911 }
912
913 void RenderStyle::setContent(PassRefPtr<StyleImage> image, bool add)
914 {
915     if (!image)
916         return;
917         
918     if (add) {
919         appendContent(std::make_unique<ImageContentData>(image));
920         return;
921     }
922
923     rareNonInheritedData.access()->m_content = std::make_unique<ImageContentData>(image);
924     if (!rareNonInheritedData.access()->m_altText.isNull())
925         rareNonInheritedData.access()->m_content->setAltText(rareNonInheritedData.access()->m_altText);
926 }
927
928 void RenderStyle::setContent(const String& string, bool add)
929 {
930     auto& content = rareNonInheritedData.access()->m_content;
931     if (add) {
932         ContentData* lastContent = content.get();
933         while (lastContent && lastContent->next())
934             lastContent = lastContent->next();
935
936         if (lastContent) {
937             // We attempt to merge with the last ContentData if possible.
938             if (is<TextContentData>(*lastContent)) {
939                 TextContentData& textContent = downcast<TextContentData>(*lastContent);
940                 textContent.setText(textContent.text() + string);
941             } else
942                 lastContent->setNext(std::make_unique<TextContentData>(string));
943
944             if (!rareNonInheritedData.access()->m_altText.isNull())
945                 lastContent->setAltText(rareNonInheritedData.access()->m_altText);
946             return;
947         }
948     }
949
950     content = std::make_unique<TextContentData>(string);
951     if (!rareNonInheritedData.access()->m_altText.isNull())
952         content->setAltText(rareNonInheritedData.access()->m_altText);
953 }
954
955 void RenderStyle::setContent(std::unique_ptr<CounterContent> counter, bool add)
956 {
957     if (!counter)
958         return;
959
960     if (add) {
961         appendContent(std::make_unique<CounterContentData>(WTF::move(counter)));
962         return;
963     }
964
965     rareNonInheritedData.access()->m_content = std::make_unique<CounterContentData>(WTF::move(counter));
966 }
967
968 void RenderStyle::setContent(QuoteType quote, bool add)
969 {
970     if (add) {
971         appendContent(std::make_unique<QuoteContentData>(quote));
972         return;
973     }
974
975     rareNonInheritedData.access()->m_content = std::make_unique<QuoteContentData>(quote);
976 }
977
978 void RenderStyle::setContentAltText(const String& string)
979 {
980     rareNonInheritedData.access()->m_altText = string;
981     
982     if (rareNonInheritedData.access()->m_content)
983         rareNonInheritedData.access()->m_content->setAltText(string);
984 }
985
986 const String& RenderStyle::contentAltText() const
987 {
988     return rareNonInheritedData->m_altText;
989 }
990
991 // FIXME: use affectedByTransformOrigin().
992 static inline bool requireTransformOrigin(const Vector<RefPtr<TransformOperation>>& transformOperations, RenderStyle::ApplyTransformOrigin applyOrigin)
993 {
994     // transform-origin brackets the transform with translate operations.
995     // Optimize for the case where the only transform is a translation, since the transform-origin is irrelevant
996     // in that case.
997     if (applyOrigin != RenderStyle::IncludeTransformOrigin)
998         return false;
999
1000     for (auto& operation : transformOperations) {
1001         TransformOperation::OperationType type = operation->type();
1002         if (type != TransformOperation::TRANSLATE_X
1003             && type != TransformOperation::TRANSLATE_Y
1004             && type != TransformOperation::TRANSLATE 
1005             && type != TransformOperation::TRANSLATE_Z // FIXME: doesn't this depend on transform origin?
1006             && type != TransformOperation::TRANSLATE_3D)
1007             return true;
1008     }
1009
1010     return false;
1011 }
1012
1013 void RenderStyle::applyTransform(TransformationMatrix& transform, const FloatRect& boundingBox, ApplyTransformOrigin applyOrigin) const
1014 {
1015     auto& operations = rareNonInheritedData->m_transform->m_operations.operations();
1016     bool applyTransformOrigin = requireTransformOrigin(operations, applyOrigin);
1017
1018     float offsetX = transformOriginX().isPercentNotCalculated() ? boundingBox.x() : 0;
1019     float offsetY = transformOriginY().isPercentNotCalculated() ? boundingBox.y() : 0;
1020
1021     if (applyTransformOrigin) {
1022         transform.translate3d(floatValueForLength(transformOriginX(), boundingBox.width()) + offsetX,
1023                               floatValueForLength(transformOriginY(), boundingBox.height()) + offsetY,
1024                               transformOriginZ());
1025     }
1026
1027     for (auto& operation : operations)
1028         operation->apply(transform, boundingBox.size());
1029
1030     if (applyTransformOrigin) {
1031         transform.translate3d(-floatValueForLength(transformOriginX(), boundingBox.width()) - offsetX,
1032                               -floatValueForLength(transformOriginY(), boundingBox.height()) - offsetY,
1033                               -transformOriginZ());
1034     }
1035 }
1036
1037 void RenderStyle::setPageScaleTransform(float scale)
1038 {
1039     if (scale == 1)
1040         return;
1041     TransformOperations transform;
1042     transform.operations().append(ScaleTransformOperation::create(scale, scale, ScaleTransformOperation::SCALE));
1043     setTransform(transform);
1044     setTransformOriginX(Length(0, Fixed));
1045     setTransformOriginY(Length(0, Fixed));
1046 }
1047
1048 void RenderStyle::setTextShadow(std::unique_ptr<ShadowData> shadowData, bool add)
1049 {
1050     ASSERT(!shadowData || (!shadowData->spread() && shadowData->style() == Normal));
1051
1052     StyleRareInheritedData* rareData = rareInheritedData.access();
1053     if (!add) {
1054         rareData->textShadow = WTF::move(shadowData);
1055         return;
1056     }
1057
1058     shadowData->setNext(WTF::move(rareData->textShadow));
1059     rareData->textShadow = WTF::move(shadowData);
1060 }
1061
1062 void RenderStyle::setBoxShadow(std::unique_ptr<ShadowData> shadowData, bool add)
1063 {
1064     StyleRareNonInheritedData* rareData = rareNonInheritedData.access();
1065     if (!add) {
1066         rareData->m_boxShadow = WTF::move(shadowData);
1067         return;
1068     }
1069
1070     shadowData->setNext(WTF::move(rareData->m_boxShadow));
1071     rareData->m_boxShadow = WTF::move(shadowData);
1072 }
1073
1074 static RoundedRect::Radii calcRadiiFor(const BorderData& border, const LayoutSize& size)
1075 {
1076     return RoundedRect::Radii(
1077         LayoutSize(valueForLength(border.topLeft().width(), size.width()),
1078             valueForLength(border.topLeft().height(), size.height())),
1079         LayoutSize(valueForLength(border.topRight().width(), size.width()),
1080             valueForLength(border.topRight().height(), size.height())),
1081         LayoutSize(valueForLength(border.bottomLeft().width(), size.width()),
1082             valueForLength(border.bottomLeft().height(), size.height())),
1083         LayoutSize(valueForLength(border.bottomRight().width(), size.width()),
1084             valueForLength(border.bottomRight().height(), size.height())));
1085 }
1086
1087 StyleImage* RenderStyle::listStyleImage() const { return rareInheritedData->listStyleImage.get(); }
1088 void RenderStyle::setListStyleImage(PassRefPtr<StyleImage> v)
1089 {
1090     if (rareInheritedData->listStyleImage != v)
1091         rareInheritedData.access()->listStyleImage = v;
1092 }
1093
1094 Color RenderStyle::color() const { return inherited->color; }
1095 Color RenderStyle::visitedLinkColor() const { return inherited->visitedLinkColor; }
1096 void RenderStyle::setColor(const Color& v) { SET_VAR(inherited, color, v); }
1097 void RenderStyle::setVisitedLinkColor(const Color& v) { SET_VAR(inherited, visitedLinkColor, v); }
1098
1099 short RenderStyle::horizontalBorderSpacing() const { return inherited->horizontal_border_spacing; }
1100 short RenderStyle::verticalBorderSpacing() const { return inherited->vertical_border_spacing; }
1101 void RenderStyle::setHorizontalBorderSpacing(short v) { SET_VAR(inherited, horizontal_border_spacing, v); }
1102 void RenderStyle::setVerticalBorderSpacing(short v) { SET_VAR(inherited, vertical_border_spacing, v); }
1103
1104 RoundedRect RenderStyle::getRoundedBorderFor(const LayoutRect& borderRect, bool includeLogicalLeftEdge, bool includeLogicalRightEdge) const
1105 {
1106     RoundedRect roundedRect(borderRect);
1107     if (hasBorderRadius()) {
1108         RoundedRect::Radii radii = calcRadiiFor(surround->border, borderRect.size());
1109         radii.scale(calcBorderRadiiConstraintScaleFor(borderRect, radii));
1110         roundedRect.includeLogicalEdges(radii, isHorizontalWritingMode(), includeLogicalLeftEdge, includeLogicalRightEdge);
1111     }
1112     return roundedRect;
1113 }
1114
1115 RoundedRect RenderStyle::getRoundedInnerBorderFor(const LayoutRect& borderRect, bool includeLogicalLeftEdge, bool includeLogicalRightEdge) const
1116 {
1117     bool horizontal = isHorizontalWritingMode();
1118
1119     LayoutUnit leftWidth = (!horizontal || includeLogicalLeftEdge) ? borderLeftWidth() : 0;
1120     LayoutUnit rightWidth = (!horizontal || includeLogicalRightEdge) ? borderRightWidth() : 0;
1121     LayoutUnit topWidth = (horizontal || includeLogicalLeftEdge) ? borderTopWidth() : 0;
1122     LayoutUnit bottomWidth = (horizontal || includeLogicalRightEdge) ? borderBottomWidth() : 0;
1123
1124     return getRoundedInnerBorderFor(borderRect, topWidth, bottomWidth, leftWidth, rightWidth, includeLogicalLeftEdge, includeLogicalRightEdge);
1125 }
1126
1127 RoundedRect RenderStyle::getRoundedInnerBorderFor(const LayoutRect& borderRect, LayoutUnit topWidth, LayoutUnit bottomWidth,
1128     LayoutUnit leftWidth, LayoutUnit rightWidth, bool includeLogicalLeftEdge, bool includeLogicalRightEdge) const
1129 {
1130     LayoutRect innerRect(borderRect.x() + leftWidth, 
1131                borderRect.y() + topWidth, 
1132                borderRect.width() - leftWidth - rightWidth, 
1133                borderRect.height() - topWidth - bottomWidth);
1134
1135     RoundedRect roundedRect(innerRect);
1136
1137     if (hasBorderRadius()) {
1138         RoundedRect::Radii radii = getRoundedBorderFor(borderRect).radii();
1139         radii.shrink(topWidth, bottomWidth, leftWidth, rightWidth);
1140         roundedRect.includeLogicalEdges(radii, isHorizontalWritingMode(), includeLogicalLeftEdge, includeLogicalRightEdge);
1141     }
1142     return roundedRect;
1143 }
1144
1145 static bool allLayersAreFixed(const FillLayer* layer)
1146 {
1147     bool allFixed = true;
1148     
1149     for (const FillLayer* currLayer = layer; currLayer; currLayer = currLayer->next())
1150         allFixed &= (currLayer->image() && currLayer->attachment() == FixedBackgroundAttachment);
1151
1152     return layer && allFixed;
1153 }
1154
1155 bool RenderStyle::hasEntirelyFixedBackground() const
1156 {
1157     return allLayersAreFixed(backgroundLayers());
1158 }
1159
1160 const CounterDirectiveMap* RenderStyle::counterDirectives() const
1161 {
1162     return rareNonInheritedData->m_counterDirectives.get();
1163 }
1164
1165 CounterDirectiveMap& RenderStyle::accessCounterDirectives()
1166 {
1167     auto& map = rareNonInheritedData.access()->m_counterDirectives;
1168     if (!map)
1169         map = std::make_unique<CounterDirectiveMap>();
1170     return *map;
1171 }
1172
1173 const CounterDirectives RenderStyle::getCounterDirectives(const AtomicString& identifier) const
1174 {
1175     if (const CounterDirectiveMap* directives = counterDirectives())
1176         return directives->get(identifier);
1177     return CounterDirectives();
1178 }
1179
1180 const AtomicString& RenderStyle::hyphenString() const
1181 {
1182     ASSERT(hyphens() != HyphensNone);
1183
1184     const AtomicString& hyphenationString = rareInheritedData.get()->hyphenationString;
1185     if (!hyphenationString.isNull())
1186         return hyphenationString;
1187
1188     // FIXME: This should depend on locale.
1189     DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, hyphenMinusString, (&hyphenMinus, 1));
1190     DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, hyphenString, (&hyphen, 1));
1191     return fontCascade().primaryFont().glyphForCharacter(hyphen) ? hyphenString : hyphenMinusString;
1192 }
1193
1194 const AtomicString& RenderStyle::textEmphasisMarkString() const
1195 {
1196     switch (textEmphasisMark()) {
1197     case TextEmphasisMarkNone:
1198         return nullAtom;
1199     case TextEmphasisMarkCustom:
1200         return textEmphasisCustomMark();
1201     case TextEmphasisMarkDot: {
1202         DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, filledDotString, (&bullet, 1));
1203         DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, openDotString, (&whiteBullet, 1));
1204         return textEmphasisFill() == TextEmphasisFillFilled ? filledDotString : openDotString;
1205     }
1206     case TextEmphasisMarkCircle: {
1207         DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, filledCircleString, (&blackCircle, 1));
1208         DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, openCircleString, (&whiteCircle, 1));
1209         return textEmphasisFill() == TextEmphasisFillFilled ? filledCircleString : openCircleString;
1210     }
1211     case TextEmphasisMarkDoubleCircle: {
1212         DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, filledDoubleCircleString, (&fisheye, 1));
1213         DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, openDoubleCircleString, (&bullseye, 1));
1214         return textEmphasisFill() == TextEmphasisFillFilled ? filledDoubleCircleString : openDoubleCircleString;
1215     }
1216     case TextEmphasisMarkTriangle: {
1217         DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, filledTriangleString, (&blackUpPointingTriangle, 1));
1218         DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, openTriangleString, (&whiteUpPointingTriangle, 1));
1219         return textEmphasisFill() == TextEmphasisFillFilled ? filledTriangleString : openTriangleString;
1220     }
1221     case TextEmphasisMarkSesame: {
1222         DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, filledSesameString, (&sesameDot, 1));
1223         DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, openSesameString, (&whiteSesameDot, 1));
1224         return textEmphasisFill() == TextEmphasisFillFilled ? filledSesameString : openSesameString;
1225     }
1226     case TextEmphasisMarkAuto:
1227         ASSERT_NOT_REACHED();
1228         return nullAtom;
1229     }
1230
1231     ASSERT_NOT_REACHED();
1232     return nullAtom;
1233 }
1234
1235 #if ENABLE(DASHBOARD_SUPPORT)
1236 const Vector<StyleDashboardRegion>& RenderStyle::initialDashboardRegions()
1237 {
1238     DEPRECATED_DEFINE_STATIC_LOCAL(Vector<StyleDashboardRegion>, emptyList, ());
1239     return emptyList;
1240 }
1241
1242 const Vector<StyleDashboardRegion>& RenderStyle::noneDashboardRegions()
1243 {
1244     DEPRECATED_DEFINE_STATIC_LOCAL(Vector<StyleDashboardRegion>, noneList, ());
1245     static bool noneListInitialized = false;
1246
1247     if (!noneListInitialized) {
1248         StyleDashboardRegion region;
1249         region.label = "";
1250         region.offset.m_top  = Length();
1251         region.offset.m_right = Length();
1252         region.offset.m_bottom = Length();
1253         region.offset.m_left = Length();
1254         region.type = StyleDashboardRegion::None;
1255         noneList.append(region);
1256         noneListInitialized = true;
1257     }
1258     return noneList;
1259 }
1260 #endif
1261
1262 void RenderStyle::adjustAnimations()
1263 {
1264     AnimationList* animationList = rareNonInheritedData->m_animations.get();
1265     if (!animationList)
1266         return;
1267
1268     // Get rid of empty animations and anything beyond them
1269     for (size_t i = 0; i < animationList->size(); ++i) {
1270         if (animationList->animation(i).isEmpty()) {
1271             animationList->resize(i);
1272             break;
1273         }
1274     }
1275
1276     if (animationList->isEmpty()) {
1277         clearAnimations();
1278         return;
1279     }
1280
1281     // Repeat patterns into layers that don't have some properties set.
1282     animationList->fillUnsetProperties();
1283 }
1284
1285 void RenderStyle::adjustTransitions()
1286 {
1287     AnimationList* transitionList = rareNonInheritedData->m_transitions.get();
1288     if (!transitionList)
1289         return;
1290
1291     // Get rid of empty transitions and anything beyond them
1292     for (size_t i = 0; i < transitionList->size(); ++i) {
1293         if (transitionList->animation(i).isEmpty()) {
1294             transitionList->resize(i);
1295             break;
1296         }
1297     }
1298
1299     if (transitionList->isEmpty()) {
1300         clearTransitions();
1301         return;
1302     }
1303
1304     // Repeat patterns into layers that don't have some properties set.
1305     transitionList->fillUnsetProperties();
1306
1307     // Make sure there are no duplicate properties. This is an O(n^2) algorithm
1308     // but the lists tend to be very short, so it is probably ok
1309     for (size_t i = 0; i < transitionList->size(); ++i) {
1310         for (size_t j = i+1; j < transitionList->size(); ++j) {
1311             if (transitionList->animation(i).property() == transitionList->animation(j).property()) {
1312                 // toss i
1313                 transitionList->remove(i);
1314                 j = i;
1315             }
1316         }
1317     }
1318 }
1319
1320 AnimationList& RenderStyle::ensureAnimations()
1321 {
1322     if (!rareNonInheritedData.access()->m_animations)
1323         rareNonInheritedData.access()->m_animations = std::make_unique<AnimationList>();
1324     return *rareNonInheritedData->m_animations;
1325 }
1326
1327 AnimationList& RenderStyle::ensureTransitions()
1328 {
1329     if (!rareNonInheritedData.access()->m_transitions)
1330         rareNonInheritedData.access()->m_transitions = std::make_unique<AnimationList>();
1331     return *rareNonInheritedData->m_transitions;
1332 }
1333
1334 const Animation* RenderStyle::transitionForProperty(CSSPropertyID property) const
1335 {
1336     if (transitions()) {
1337         for (size_t i = 0; i < transitions()->size(); ++i) {
1338             const Animation& p = transitions()->animation(i);
1339             if (p.animationMode() == Animation::AnimateAll || p.property() == property) {
1340                 return &p;
1341             }
1342         }
1343     }
1344     return 0;
1345 }
1346
1347 const FontCascade& RenderStyle::fontCascade() const { return inherited->fontCascade; }
1348 const FontMetrics& RenderStyle::fontMetrics() const { return inherited->fontCascade.fontMetrics(); }
1349 const FontDescription& RenderStyle::fontDescription() const { return inherited->fontCascade.fontDescription(); }
1350 float RenderStyle::specifiedFontSize() const { return fontDescription().specifiedSize(); }
1351 float RenderStyle::computedFontSize() const { return fontDescription().computedSize(); }
1352 int RenderStyle::fontSize() const { return inherited->fontCascade.pixelSize(); }
1353
1354 const Length& RenderStyle::wordSpacing() const { return rareInheritedData->wordSpacing; }
1355 float RenderStyle::letterSpacing() const { return inherited->fontCascade.letterSpacing(); }
1356
1357 bool RenderStyle::setFontDescription(const FontDescription& v)
1358 {
1359     if (inherited->fontCascade.fontDescription() != v) {
1360         inherited.access()->fontCascade = FontCascade(v, inherited->fontCascade.letterSpacing(), inherited->fontCascade.wordSpacing());
1361         return true;
1362     }
1363     return false;
1364 }
1365
1366 #if ENABLE(IOS_TEXT_AUTOSIZING)
1367 const Length& RenderStyle::specifiedLineHeight() const { return inherited->specifiedLineHeight; }
1368 void RenderStyle::setSpecifiedLineHeight(Length v) { SET_VAR(inherited, specifiedLineHeight, v); }
1369 #else
1370 const Length& RenderStyle::specifiedLineHeight() const { return inherited->line_height; }
1371 #endif
1372
1373 Length RenderStyle::lineHeight() const
1374 {
1375     const Length& lh = inherited->line_height;
1376 #if ENABLE(TEXT_AUTOSIZING)
1377     // Unlike fontDescription().computedSize() and hence fontSize(), this is
1378     // recalculated on demand as we only store the specified line height.
1379     // FIXME: Should consider scaling the fixed part of any calc expressions
1380     // too, though this involves messily poking into CalcExpressionLength.
1381     float multiplier = textAutosizingMultiplier();
1382     if (multiplier > 1 && lh.isFixed())
1383         return Length(TextAutosizer::computeAutosizedFontSize(lh.value(), multiplier), Fixed);
1384 #endif
1385     return lh;
1386 }
1387 void RenderStyle::setLineHeight(Length specifiedLineHeight) { SET_VAR(inherited, line_height, specifiedLineHeight); }
1388
1389 int RenderStyle::computedLineHeight() const
1390 {
1391     const Length& lh = lineHeight();
1392
1393     // Negative value means the line height is not set. Use the font's built-in spacing.
1394     if (lh.isNegative())
1395         return fontMetrics().lineSpacing();
1396
1397     if (lh.isPercent())
1398         return minimumValueForLength(lh, fontSize());
1399
1400     return lh.value();
1401 }
1402
1403 void RenderStyle::setWordSpacing(Length value)
1404 {
1405     float fontWordSpacing;
1406     switch (value.type()) {
1407     case Auto:
1408         fontWordSpacing = 0;
1409         break;
1410     case Percent:
1411         fontWordSpacing = value.percent() * fontCascade().spaceWidth() / 100;
1412         break;
1413     case Fixed:
1414         fontWordSpacing = value.value();
1415         break;
1416     case Calculated:
1417         fontWordSpacing = value.nonNanCalculatedValue(maxValueForCssLength);
1418         break;
1419     default:
1420         ASSERT_NOT_REACHED();
1421         fontWordSpacing = 0;
1422         break;
1423     }
1424     inherited.access()->fontCascade.setWordSpacing(fontWordSpacing);
1425     rareInheritedData.access()->wordSpacing = WTF::move(value);
1426 }
1427
1428 void RenderStyle::setLetterSpacing(float v) { inherited.access()->fontCascade.setLetterSpacing(v); }
1429
1430 void RenderStyle::setFontSize(float size)
1431 {
1432     // size must be specifiedSize if Text Autosizing is enabled, but computedSize if text
1433     // zoom is enabled (if neither is enabled it's irrelevant as they're probably the same).
1434
1435     ASSERT(std::isfinite(size));
1436     if (!std::isfinite(size) || size < 0)
1437         size = 0;
1438     else
1439         size = std::min(maximumAllowedFontSize, size);
1440
1441     FontSelector* currentFontSelector = fontCascade().fontSelector();
1442     FontDescription desc(fontDescription());
1443     desc.setSpecifiedSize(size);
1444     desc.setComputedSize(size);
1445
1446 #if ENABLE(TEXT_AUTOSIZING)
1447     float multiplier = textAutosizingMultiplier();
1448     if (multiplier > 1) {
1449         float autosizedFontSize = TextAutosizer::computeAutosizedFontSize(size, multiplier);
1450         desc.setComputedSize(min(maximumAllowedFontSize, autosizedFontSize));
1451     }
1452 #endif
1453
1454     setFontDescription(desc);
1455     fontCascade().update(currentFontSelector);
1456 }
1457
1458 void RenderStyle::getShadowExtent(const ShadowData* shadow, LayoutUnit &top, LayoutUnit &right, LayoutUnit &bottom, LayoutUnit &left) const
1459 {
1460     top = 0;
1461     right = 0;
1462     bottom = 0;
1463     left = 0;
1464
1465     for ( ; shadow; shadow = shadow->next()) {
1466         if (shadow->style() == Inset)
1467             continue;
1468
1469         int extentAndSpread = shadow->paintingExtent() + shadow->spread();
1470         top = std::min<LayoutUnit>(top, shadow->y() - extentAndSpread);
1471         right = std::max<LayoutUnit>(right, shadow->x() + extentAndSpread);
1472         bottom = std::max<LayoutUnit>(bottom, shadow->y() + extentAndSpread);
1473         left = std::min<LayoutUnit>(left, shadow->x() - extentAndSpread);
1474     }
1475 }
1476
1477 LayoutBoxExtent RenderStyle::getShadowInsetExtent(const ShadowData* shadow) const
1478 {
1479     LayoutUnit top = 0;
1480     LayoutUnit right = 0;
1481     LayoutUnit bottom = 0;
1482     LayoutUnit left = 0;
1483
1484     for ( ; shadow; shadow = shadow->next()) {
1485         if (shadow->style() == Normal)
1486             continue;
1487
1488         int extentAndSpread = shadow->paintingExtent() + shadow->spread();
1489         top = std::max<LayoutUnit>(top, shadow->y() + extentAndSpread);
1490         right = std::min<LayoutUnit>(right, shadow->x() - extentAndSpread);
1491         bottom = std::min<LayoutUnit>(bottom, shadow->y() - extentAndSpread);
1492         left = std::max<LayoutUnit>(left, shadow->x() + extentAndSpread);
1493     }
1494
1495     return LayoutBoxExtent(top, right, bottom, left);
1496 }
1497
1498 void RenderStyle::getShadowHorizontalExtent(const ShadowData* shadow, LayoutUnit &left, LayoutUnit &right) const
1499 {
1500     left = 0;
1501     right = 0;
1502
1503     for ( ; shadow; shadow = shadow->next()) {
1504         if (shadow->style() == Inset)
1505             continue;
1506
1507         int extentAndSpread = shadow->paintingExtent() + shadow->spread();
1508         left = std::min<LayoutUnit>(left, shadow->x() - extentAndSpread);
1509         right = std::max<LayoutUnit>(right, shadow->x() + extentAndSpread);
1510     }
1511 }
1512
1513 void RenderStyle::getShadowVerticalExtent(const ShadowData* shadow, LayoutUnit &top, LayoutUnit &bottom) const
1514 {
1515     top = 0;
1516     bottom = 0;
1517
1518     for ( ; shadow; shadow = shadow->next()) {
1519         if (shadow->style() == Inset)
1520             continue;
1521
1522         int extentAndSpread = shadow->paintingExtent() + shadow->spread();
1523         top = std::min<LayoutUnit>(top, shadow->y() - extentAndSpread);
1524         bottom = std::max<LayoutUnit>(bottom, shadow->y() + extentAndSpread);
1525     }
1526 }
1527
1528 Color RenderStyle::colorIncludingFallback(int colorProperty, bool visitedLink) const
1529 {
1530     Color result;
1531     EBorderStyle borderStyle = BNONE;
1532     switch (colorProperty) {
1533     case CSSPropertyBackgroundColor:
1534         return visitedLink ? visitedLinkBackgroundColor() : backgroundColor(); // Background color doesn't fall back.
1535     case CSSPropertyBorderLeftColor:
1536         result = visitedLink ? visitedLinkBorderLeftColor() : borderLeftColor();
1537         borderStyle = borderLeftStyle();
1538         break;
1539     case CSSPropertyBorderRightColor:
1540         result = visitedLink ? visitedLinkBorderRightColor() : borderRightColor();
1541         borderStyle = borderRightStyle();
1542         break;
1543     case CSSPropertyBorderTopColor:
1544         result = visitedLink ? visitedLinkBorderTopColor() : borderTopColor();
1545         borderStyle = borderTopStyle();
1546         break;
1547     case CSSPropertyBorderBottomColor:
1548         result = visitedLink ? visitedLinkBorderBottomColor() : borderBottomColor();
1549         borderStyle = borderBottomStyle();
1550         break;
1551     case CSSPropertyColor:
1552         result = visitedLink ? visitedLinkColor() : color();
1553         break;
1554     case CSSPropertyOutlineColor:
1555         result = visitedLink ? visitedLinkOutlineColor() : outlineColor();
1556         break;
1557     case CSSPropertyColumnRuleColor:
1558         result = visitedLink ? visitedLinkColumnRuleColor() : columnRuleColor();
1559         break;
1560     case CSSPropertyWebkitTextDecorationColor:
1561         // Text decoration color fallback is handled in RenderObject::decorationColor.
1562         return visitedLink ? visitedLinkTextDecorationColor() : textDecorationColor();
1563     case CSSPropertyWebkitTextEmphasisColor:
1564         result = visitedLink ? visitedLinkTextEmphasisColor() : textEmphasisColor();
1565         break;
1566     case CSSPropertyWebkitTextFillColor:
1567         result = visitedLink ? visitedLinkTextFillColor() : textFillColor();
1568         break;
1569     case CSSPropertyWebkitTextStrokeColor:
1570         result = visitedLink ? visitedLinkTextStrokeColor() : textStrokeColor();
1571         break;
1572     default:
1573         ASSERT_NOT_REACHED();
1574         break;
1575     }
1576
1577     if (!result.isValid()) {
1578         if (!visitedLink && (borderStyle == INSET || borderStyle == OUTSET || borderStyle == RIDGE || borderStyle == GROOVE))
1579             result.setRGB(238, 238, 238);
1580         else
1581             result = visitedLink ? visitedLinkColor() : color();
1582     }
1583     return result;
1584 }
1585
1586 Color RenderStyle::visitedDependentColor(int colorProperty) const
1587 {
1588     Color unvisitedColor = colorIncludingFallback(colorProperty, false);
1589     if (insideLink() != InsideVisitedLink)
1590         return unvisitedColor;
1591
1592     Color visitedColor = colorIncludingFallback(colorProperty, true);
1593
1594     // Text decoration color validity is preserved (checked in RenderObject::decorationColor).
1595     if (colorProperty == CSSPropertyWebkitTextDecorationColor)
1596         return visitedColor;
1597
1598     // FIXME: Technically someone could explicitly specify the color transparent, but for now we'll just
1599     // assume that if the background color is transparent that it wasn't set. Note that it's weird that
1600     // we're returning unvisited info for a visited link, but given our restriction that the alpha values
1601     // have to match, it makes more sense to return the unvisited background color if specified than it
1602     // does to return black. This behavior matches what Firefox 4 does as well.
1603     if (colorProperty == CSSPropertyBackgroundColor && visitedColor == Color::transparent)
1604         return unvisitedColor;
1605
1606     // Take the alpha from the unvisited color, but get the RGB values from the visited color.
1607     return Color(visitedColor.red(), visitedColor.green(), visitedColor.blue(), unvisitedColor.alpha());
1608 }
1609
1610 const BorderValue& RenderStyle::borderBefore() const
1611 {
1612     switch (writingMode()) {
1613     case TopToBottomWritingMode:
1614         return borderTop();
1615     case BottomToTopWritingMode:
1616         return borderBottom();
1617     case LeftToRightWritingMode:
1618         return borderLeft();
1619     case RightToLeftWritingMode:
1620         return borderRight();
1621     }
1622     ASSERT_NOT_REACHED();
1623     return borderTop();
1624 }
1625
1626 const BorderValue& RenderStyle::borderAfter() const
1627 {
1628     switch (writingMode()) {
1629     case TopToBottomWritingMode:
1630         return borderBottom();
1631     case BottomToTopWritingMode:
1632         return borderTop();
1633     case LeftToRightWritingMode:
1634         return borderRight();
1635     case RightToLeftWritingMode:
1636         return borderLeft();
1637     }
1638     ASSERT_NOT_REACHED();
1639     return borderBottom();
1640 }
1641
1642 const BorderValue& RenderStyle::borderStart() const
1643 {
1644     if (isHorizontalWritingMode())
1645         return isLeftToRightDirection() ? borderLeft() : borderRight();
1646     return isLeftToRightDirection() ? borderTop() : borderBottom();
1647 }
1648
1649 const BorderValue& RenderStyle::borderEnd() const
1650 {
1651     if (isHorizontalWritingMode())
1652         return isLeftToRightDirection() ? borderRight() : borderLeft();
1653     return isLeftToRightDirection() ? borderBottom() : borderTop();
1654 }
1655
1656 float RenderStyle::borderBeforeWidth() const
1657 {
1658     switch (writingMode()) {
1659     case TopToBottomWritingMode:
1660         return borderTopWidth();
1661     case BottomToTopWritingMode:
1662         return borderBottomWidth();
1663     case LeftToRightWritingMode:
1664         return borderLeftWidth();
1665     case RightToLeftWritingMode:
1666         return borderRightWidth();
1667     }
1668     ASSERT_NOT_REACHED();
1669     return borderTopWidth();
1670 }
1671
1672 float RenderStyle::borderAfterWidth() const
1673 {
1674     switch (writingMode()) {
1675     case TopToBottomWritingMode:
1676         return borderBottomWidth();
1677     case BottomToTopWritingMode:
1678         return borderTopWidth();
1679     case LeftToRightWritingMode:
1680         return borderRightWidth();
1681     case RightToLeftWritingMode:
1682         return borderLeftWidth();
1683     }
1684     ASSERT_NOT_REACHED();
1685     return borderBottomWidth();
1686 }
1687
1688 float RenderStyle::borderStartWidth() const
1689 {
1690     if (isHorizontalWritingMode())
1691         return isLeftToRightDirection() ? borderLeftWidth() : borderRightWidth();
1692     return isLeftToRightDirection() ? borderTopWidth() : borderBottomWidth();
1693 }
1694
1695 float RenderStyle::borderEndWidth() const
1696 {
1697     if (isHorizontalWritingMode())
1698         return isLeftToRightDirection() ? borderRightWidth() : borderLeftWidth();
1699     return isLeftToRightDirection() ? borderBottomWidth() : borderTopWidth();
1700 }
1701
1702 void RenderStyle::setMarginStart(Length margin)
1703 {
1704     if (isHorizontalWritingMode()) {
1705         if (isLeftToRightDirection())
1706             setMarginLeft(margin);
1707         else
1708             setMarginRight(margin);
1709     } else {
1710         if (isLeftToRightDirection())
1711             setMarginTop(margin);
1712         else
1713             setMarginBottom(margin);
1714     }
1715 }
1716
1717 void RenderStyle::setMarginEnd(Length margin)
1718 {
1719     if (isHorizontalWritingMode()) {
1720         if (isLeftToRightDirection())
1721             setMarginRight(margin);
1722         else
1723             setMarginLeft(margin);
1724     } else {
1725         if (isLeftToRightDirection())
1726             setMarginBottom(margin);
1727         else
1728             setMarginTop(margin);
1729     }
1730 }
1731
1732 TextEmphasisMark RenderStyle::textEmphasisMark() const
1733 {
1734     TextEmphasisMark mark = static_cast<TextEmphasisMark>(rareInheritedData->textEmphasisMark);
1735     if (mark != TextEmphasisMarkAuto)
1736         return mark;
1737
1738     if (isHorizontalWritingMode())
1739         return TextEmphasisMarkDot;
1740
1741     return TextEmphasisMarkSesame;
1742 }
1743
1744 #if ENABLE(TOUCH_EVENTS)
1745 Color RenderStyle::initialTapHighlightColor()
1746 {
1747     return RenderTheme::tapHighlightColor();
1748 }
1749 #endif
1750
1751 LayoutBoxExtent RenderStyle::imageOutsets(const NinePieceImage& image) const
1752 {
1753     return LayoutBoxExtent(NinePieceImage::computeOutset(image.outset().top(), borderTopWidth()),
1754                            NinePieceImage::computeOutset(image.outset().right(), borderRightWidth()),
1755                            NinePieceImage::computeOutset(image.outset().bottom(), borderBottomWidth()),
1756                            NinePieceImage::computeOutset(image.outset().left(), borderLeftWidth()));
1757 }
1758
1759 void RenderStyle::getFontAndGlyphOrientation(FontOrientation& fontOrientation, NonCJKGlyphOrientation& glyphOrientation)
1760 {
1761     if (isHorizontalWritingMode()) {
1762         fontOrientation = Horizontal;
1763         glyphOrientation = NonCJKGlyphOrientationVerticalRight;
1764         return;
1765     }
1766
1767     switch (textOrientation()) {
1768     case TextOrientationVerticalRight:
1769         fontOrientation = Vertical;
1770         glyphOrientation = NonCJKGlyphOrientationVerticalRight;
1771         return;
1772     case TextOrientationUpright:
1773         fontOrientation = Vertical;
1774         glyphOrientation = NonCJKGlyphOrientationUpright;
1775         return;
1776     case TextOrientationSideways:
1777         if (writingMode() == LeftToRightWritingMode) {
1778             // FIXME: This should map to sideways-left, which is not supported yet.
1779             fontOrientation = Vertical;
1780             glyphOrientation = NonCJKGlyphOrientationVerticalRight;
1781             return;
1782         }
1783         fontOrientation = Horizontal;
1784         glyphOrientation = NonCJKGlyphOrientationVerticalRight;
1785         return;
1786     case TextOrientationSidewaysRight:
1787         fontOrientation = Horizontal;
1788         glyphOrientation = NonCJKGlyphOrientationVerticalRight;
1789         return;
1790     default:
1791         ASSERT_NOT_REACHED();
1792         fontOrientation = Horizontal;
1793         glyphOrientation = NonCJKGlyphOrientationVerticalRight;
1794         return;
1795     }
1796 }
1797
1798 void RenderStyle::setBorderImageSource(PassRefPtr<StyleImage> image)
1799 {
1800     if (surround->border.m_image.image() == image.get())
1801         return;
1802     surround.access()->border.m_image.setImage(image);
1803 }
1804
1805 void RenderStyle::setBorderImageSlices(LengthBox slices)
1806 {
1807     if (surround->border.m_image.imageSlices() == slices)
1808         return;
1809     surround.access()->border.m_image.setImageSlices(slices);
1810 }
1811
1812 void RenderStyle::setBorderImageWidth(LengthBox slices)
1813 {
1814     if (surround->border.m_image.borderSlices() == slices)
1815         return;
1816     surround.access()->border.m_image.setBorderSlices(slices);
1817 }
1818
1819 void RenderStyle::setBorderImageOutset(LengthBox outset)
1820 {
1821     if (surround->border.m_image.outset() == outset)
1822         return;
1823     surround.access()->border.m_image.setOutset(outset);
1824 }
1825
1826 void RenderStyle::setColumnStylesFromPaginationMode(const Pagination::Mode& paginationMode)
1827 {
1828     if (paginationMode == Pagination::Unpaginated)
1829         return;
1830     
1831     setColumnFill(ColumnFillAuto);
1832     
1833     switch (paginationMode) {
1834     case Pagination::LeftToRightPaginated:
1835         setColumnAxis(HorizontalColumnAxis);
1836         if (isHorizontalWritingMode())
1837             setColumnProgression(isLeftToRightDirection() ? NormalColumnProgression : ReverseColumnProgression);
1838         else
1839             setColumnProgression(isFlippedBlocksWritingMode() ? ReverseColumnProgression : NormalColumnProgression);
1840         break;
1841     case Pagination::RightToLeftPaginated:
1842         setColumnAxis(HorizontalColumnAxis);
1843         if (isHorizontalWritingMode())
1844             setColumnProgression(isLeftToRightDirection() ? ReverseColumnProgression : NormalColumnProgression);
1845         else
1846             setColumnProgression(isFlippedBlocksWritingMode() ? NormalColumnProgression : ReverseColumnProgression);
1847         break;
1848     case Pagination::TopToBottomPaginated:
1849         setColumnAxis(VerticalColumnAxis);
1850         if (isHorizontalWritingMode())
1851             setColumnProgression(isFlippedBlocksWritingMode() ? ReverseColumnProgression : NormalColumnProgression);
1852         else
1853             setColumnProgression(isLeftToRightDirection() ? NormalColumnProgression : ReverseColumnProgression);
1854         break;
1855     case Pagination::BottomToTopPaginated:
1856         setColumnAxis(VerticalColumnAxis);
1857         if (isHorizontalWritingMode())
1858             setColumnProgression(isFlippedBlocksWritingMode() ? NormalColumnProgression : ReverseColumnProgression);
1859         else
1860             setColumnProgression(isLeftToRightDirection() ? ReverseColumnProgression : NormalColumnProgression);
1861         break;
1862     case Pagination::Unpaginated:
1863         ASSERT_NOT_REACHED();
1864         break;
1865     }
1866 }
1867
1868 #if ENABLE(CSS_SCROLL_SNAP)
1869 LengthSize RenderStyle::initialScrollSnapDestination()
1870 {
1871     return defaultScrollSnapDestination();
1872 }
1873
1874 Vector<LengthSize> RenderStyle::initialScrollSnapCoordinates()
1875 {
1876     return Vector<LengthSize>();
1877 }
1878
1879 const ScrollSnapPoints* RenderStyle::scrollSnapPointsX() const
1880 {
1881     return rareNonInheritedData->m_scrollSnapPoints->xPoints.get();
1882 }
1883
1884 const ScrollSnapPoints* RenderStyle::scrollSnapPointsY() const
1885 {
1886     return rareNonInheritedData->m_scrollSnapPoints->yPoints.get();
1887 }
1888
1889 const LengthSize& RenderStyle::scrollSnapDestination() const
1890 {
1891     return rareNonInheritedData->m_scrollSnapPoints->destination;
1892 }
1893
1894 const Vector<LengthSize>& RenderStyle::scrollSnapCoordinates() const
1895 {
1896     return rareNonInheritedData->m_scrollSnapPoints->coordinates;
1897 }
1898
1899 void RenderStyle::setScrollSnapPointsX(std::unique_ptr<ScrollSnapPoints> points)
1900 {
1901     if (rareNonInheritedData->m_scrollSnapPoints->xPoints.get() == points.get())
1902         return;
1903     rareNonInheritedData.access()->m_scrollSnapPoints.access()->xPoints = WTF::move(points);
1904 }
1905
1906 void RenderStyle::setScrollSnapPointsY(std::unique_ptr<ScrollSnapPoints> points)
1907 {
1908     if (rareNonInheritedData->m_scrollSnapPoints->yPoints.get() == points.get())
1909         return;
1910     rareNonInheritedData.access()->m_scrollSnapPoints.access()->yPoints = WTF::move(points);
1911 }
1912
1913 void RenderStyle::setScrollSnapDestination(LengthSize destination)
1914 {
1915     if (rareNonInheritedData->m_scrollSnapPoints->destination == destination)
1916         return;
1917     rareNonInheritedData.access()->m_scrollSnapPoints.access()->destination = WTF::move(destination);
1918 }
1919
1920 void RenderStyle::setScrollSnapCoordinates(Vector<LengthSize> coordinates)
1921 {
1922     if (rareNonInheritedData->m_scrollSnapPoints->coordinates == coordinates)
1923         return;
1924     rareNonInheritedData.access()->m_scrollSnapPoints.access()->coordinates = WTF::move(coordinates);
1925 }
1926
1927 #endif
1928
1929 } // namespace WebCore