Text Autosizing: Multiply large fonts less, as they are already more legible.
[WebKit-https.git] / Source / WebCore / rendering / style / RenderStyle.cpp
1 /*
2  * Copyright (C) 1999 Antti Koivisto (koivisto@kde.org)
3  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 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 "Font.h"
30 #include "FontSelector.h"
31 #include "MemoryInstrumentation.h"
32 #include "QuotesData.h"
33 #include "RenderArena.h"
34 #include "RenderObject.h"
35 #include "ScaleTransformOperation.h"
36 #include "ShadowData.h"
37 #include "StyleImage.h"
38 #include "StyleInheritedData.h"
39 #include "StyleResolver.h"
40 #if ENABLE(TOUCH_EVENTS)
41 #include "RenderTheme.h"
42 #endif
43 #include <wtf/StdLibExtras.h>
44 #include <algorithm>
45
46 #if ENABLE(TEXT_AUTOSIZING)
47 #include "TextAutosizer.h"
48 #endif
49
50 using namespace std;
51
52 namespace WebCore {
53
54 struct SameSizeAsBorderValue {
55     RGBA32 m_color;
56     unsigned m_width;
57 };
58
59 COMPILE_ASSERT(sizeof(BorderValue) == sizeof(SameSizeAsBorderValue), BorderValue_should_not_grow);
60
61 struct SameSizeAsRenderStyle : public RefCounted<SameSizeAsRenderStyle> {
62     unsigned m_bitfields;
63
64     void* dataRefs[7];
65     void* ownPtrs[1];
66 #if ENABLE(SVG)
67     void* dataRefSvgStyle;
68 #endif
69     struct InheritedFlags {
70         unsigned m_bitfields[2];
71     } inherited_flags;
72
73     struct NonInheritedFlags {
74         unsigned m_bitfields[2];
75     } noninherited_flags;
76 };
77
78 COMPILE_ASSERT(sizeof(RenderStyle) == sizeof(SameSizeAsRenderStyle), RenderStyle_should_stay_small);
79
80 inline RenderStyle* defaultStyle()
81 {
82     static RenderStyle* s_defaultStyle = RenderStyle::createDefaultStyle().leakRef();
83     return s_defaultStyle;
84 }
85
86 PassRefPtr<RenderStyle> RenderStyle::create()
87 {
88     return adoptRef(new RenderStyle());
89 }
90
91 PassRefPtr<RenderStyle> RenderStyle::createDefaultStyle()
92 {
93     return adoptRef(new RenderStyle(true));
94 }
95
96 PassRefPtr<RenderStyle> RenderStyle::createAnonymousStyleWithDisplay(const RenderStyle* parentStyle, EDisplay display)
97 {
98     RefPtr<RenderStyle> newStyle = RenderStyle::create();
99     newStyle->inheritFrom(parentStyle);
100     newStyle->inheritUnicodeBidiFrom(parentStyle);
101     newStyle->setDisplay(display);
102     return newStyle;
103 }
104
105 PassRefPtr<RenderStyle> RenderStyle::clone(const RenderStyle* other)
106 {
107     return adoptRef(new RenderStyle(*other));
108 }
109
110 ALWAYS_INLINE RenderStyle::RenderStyle()
111     : m_box(defaultStyle()->m_box)
112     , visual(defaultStyle()->visual)
113     , m_background(defaultStyle()->m_background)
114     , surround(defaultStyle()->surround)
115     , rareNonInheritedData(defaultStyle()->rareNonInheritedData)
116     , rareInheritedData(defaultStyle()->rareInheritedData)
117     , inherited(defaultStyle()->inherited)
118 #if ENABLE(SVG)
119     , m_svgStyle(defaultStyle()->m_svgStyle)
120 #endif
121 {
122     setBitDefaults(); // Would it be faster to copy this from the default style?
123     COMPILE_ASSERT((sizeof(InheritedFlags) <= 8), InheritedFlags_does_not_grow);
124     COMPILE_ASSERT((sizeof(NonInheritedFlags) <= 8), NonInheritedFlags_does_not_grow);
125 }
126
127 ALWAYS_INLINE RenderStyle::RenderStyle(bool)
128 {
129     setBitDefaults();
130
131     m_box.init();
132     visual.init();
133     m_background.init();
134     surround.init();
135     rareNonInheritedData.init();
136     rareNonInheritedData.access()->m_deprecatedFlexibleBox.init();
137     rareNonInheritedData.access()->m_flexibleBox.init();
138     rareNonInheritedData.access()->m_marquee.init();
139     rareNonInheritedData.access()->m_multiCol.init();
140     rareNonInheritedData.access()->m_transform.init();
141 #if ENABLE(CSS_FILTERS)
142     rareNonInheritedData.access()->m_filter.init();
143 #endif
144     rareNonInheritedData.access()->m_grid.init();
145     rareNonInheritedData.access()->m_gridItem.init();
146     rareInheritedData.init();
147     inherited.init();
148 #if ENABLE(SVG)
149     m_svgStyle.init();
150 #endif
151 }
152
153 ALWAYS_INLINE RenderStyle::RenderStyle(const RenderStyle& o)
154     : RefCounted<RenderStyle>()
155     , m_box(o.m_box)
156     , visual(o.visual)
157     , m_background(o.m_background)
158     , surround(o.surround)
159     , rareNonInheritedData(o.rareNonInheritedData)
160     , rareInheritedData(o.rareInheritedData)
161     , inherited(o.inherited)
162 #if ENABLE(SVG)
163     , m_svgStyle(o.m_svgStyle)
164 #endif
165     , inherited_flags(o.inherited_flags)
166     , noninherited_flags(o.noninherited_flags)
167 {
168 }
169
170 void RenderStyle::inheritFrom(const RenderStyle* inheritParent, IsAtShadowBoundary isAtShadowBoundary)
171 {
172     if (isAtShadowBoundary == AtShadowBoundary) {
173         // Even if surrounding content is user-editable, shadow DOM should act as a single unit, and not necessarily be editable
174         EUserModify currentUserModify = userModify();
175         rareInheritedData = inheritParent->rareInheritedData;
176         setUserModify(currentUserModify);
177     } else
178         rareInheritedData = inheritParent->rareInheritedData;
179     inherited = inheritParent->inherited;
180     inherited_flags = inheritParent->inherited_flags;
181 #if ENABLE(SVG)
182     if (m_svgStyle != inheritParent->m_svgStyle)
183         m_svgStyle.access()->inheritFrom(inheritParent->m_svgStyle.get());
184 #endif
185 }
186
187 void RenderStyle::copyNonInheritedFrom(const RenderStyle* other)
188 {
189     m_box = other->m_box;
190     visual = other->visual;
191     m_background = other->m_background;
192     surround = other->surround;
193     rareNonInheritedData = other->rareNonInheritedData;
194     // The flags are copied one-by-one because noninherited_flags contains a bunch of stuff other than real style data.
195     noninherited_flags._effectiveDisplay = other->noninherited_flags._effectiveDisplay;
196     noninherited_flags._originalDisplay = other->noninherited_flags._originalDisplay;
197     noninherited_flags._overflowX = other->noninherited_flags._overflowX;
198     noninherited_flags._overflowY = other->noninherited_flags._overflowY;
199     noninherited_flags._vertical_align = other->noninherited_flags._vertical_align;
200     noninherited_flags._clear = other->noninherited_flags._clear;
201     noninherited_flags._position = other->noninherited_flags._position;
202     noninherited_flags._floating = other->noninherited_flags._floating;
203     noninherited_flags._table_layout = other->noninherited_flags._table_layout;
204     noninherited_flags._unicodeBidi = other->noninherited_flags._unicodeBidi;
205     noninherited_flags._page_break_before = other->noninherited_flags._page_break_before;
206     noninherited_flags._page_break_after = other->noninherited_flags._page_break_after;
207     noninherited_flags._page_break_inside = other->noninherited_flags._page_break_inside;
208 #if ENABLE(SVG)
209     if (m_svgStyle != other->m_svgStyle)
210         m_svgStyle.access()->copyNonInheritedFrom(other->m_svgStyle.get());
211 #endif
212     ASSERT(zoom() == initialZoom());
213 }
214
215 bool RenderStyle::operator==(const RenderStyle& o) const
216 {
217     // compare everything except the pseudoStyle pointer
218     return inherited_flags == o.inherited_flags
219         && noninherited_flags == o.noninherited_flags
220         && m_box == o.m_box
221         && visual == o.visual
222         && m_background == o.m_background
223         && surround == o.surround
224         && rareNonInheritedData == o.rareNonInheritedData
225         && rareInheritedData == o.rareInheritedData
226         && inherited == o.inherited
227 #if ENABLE(SVG)
228         && m_svgStyle == o.m_svgStyle
229 #endif
230             ;
231 }
232
233 bool RenderStyle::isStyleAvailable() const
234 {
235     return this != StyleResolver::styleNotYetAvailable();
236 }
237
238 static inline int pseudoBit(PseudoId pseudo)
239 {
240     return 1 << (pseudo - 1);
241 }
242
243 bool RenderStyle::hasAnyPublicPseudoStyles() const
244 {
245     return PUBLIC_PSEUDOID_MASK & noninherited_flags._pseudoBits;
246 }
247
248 bool RenderStyle::hasPseudoStyle(PseudoId pseudo) const
249 {
250     ASSERT(pseudo > NOPSEUDO);
251     ASSERT(pseudo < FIRST_INTERNAL_PSEUDOID);
252     return pseudoBit(pseudo) & noninherited_flags._pseudoBits;
253 }
254
255 void RenderStyle::setHasPseudoStyle(PseudoId pseudo)
256 {
257     ASSERT(pseudo > NOPSEUDO);
258     ASSERT(pseudo < FIRST_INTERNAL_PSEUDOID);
259     noninherited_flags._pseudoBits |= pseudoBit(pseudo);
260 }
261
262 RenderStyle* RenderStyle::getCachedPseudoStyle(PseudoId pid) const
263 {
264     if (!m_cachedPseudoStyles || !m_cachedPseudoStyles->size())
265         return 0;
266
267     if (styleType() != NOPSEUDO) 
268         return 0;
269
270     for (size_t i = 0; i < m_cachedPseudoStyles->size(); ++i) {
271         RenderStyle* pseudoStyle = m_cachedPseudoStyles->at(i).get();
272         if (pseudoStyle->styleType() == pid)
273             return pseudoStyle;
274     }
275
276     return 0;
277 }
278
279 RenderStyle* RenderStyle::addCachedPseudoStyle(PassRefPtr<RenderStyle> pseudo)
280 {
281     if (!pseudo)
282         return 0;
283
284     ASSERT(pseudo->styleType() > NOPSEUDO);
285
286     RenderStyle* result = pseudo.get();
287
288     if (!m_cachedPseudoStyles)
289         m_cachedPseudoStyles = adoptPtr(new PseudoStyleCache);
290
291     m_cachedPseudoStyles->append(pseudo);
292
293     return result;
294 }
295
296 void RenderStyle::removeCachedPseudoStyle(PseudoId pid)
297 {
298     if (!m_cachedPseudoStyles)
299         return;
300     for (size_t i = 0; i < m_cachedPseudoStyles->size(); ++i) {
301         RenderStyle* pseudoStyle = m_cachedPseudoStyles->at(i).get();
302         if (pseudoStyle->styleType() == pid) {
303             m_cachedPseudoStyles->remove(i);
304             return;
305         }
306     }
307 }
308
309 bool RenderStyle::inheritedNotEqual(const RenderStyle* other) const
310 {
311     return inherited_flags != other->inherited_flags
312            || inherited != other->inherited
313 #if ENABLE(SVG)
314            || m_svgStyle->inheritedNotEqual(other->m_svgStyle.get())
315 #endif
316            || rareInheritedData != other->rareInheritedData;
317 }
318
319 bool RenderStyle::inheritedDataShared(const RenderStyle* other) const
320 {
321     // This is a fast check that only looks if the data structures are shared.
322     return inherited_flags == other->inherited_flags
323         && inherited.get() == other->inherited.get()
324 #if ENABLE(SVG)
325         && m_svgStyle.get() == other->m_svgStyle.get()
326 #endif
327         && rareInheritedData.get() == other->rareInheritedData.get();
328 }
329
330 static bool positionedObjectMoved(const LengthBox& a, const LengthBox& b)
331 {
332     // If any unit types are different, then we can't guarantee
333     // that this was just a movement.
334     if (a.left().type() != b.left().type()
335         || a.right().type() != b.right().type()
336         || a.top().type() != b.top().type()
337         || a.bottom().type() != b.bottom().type())
338         return false;
339
340     // Only one unit can be non-auto in the horizontal direction and
341     // in the vertical direction.  Otherwise the adjustment of values
342     // is changing the size of the box.
343     if (!a.left().isIntrinsicOrAuto() && !a.right().isIntrinsicOrAuto())
344         return false;
345     if (!a.top().isIntrinsicOrAuto() && !a.bottom().isIntrinsicOrAuto())
346         return false;
347
348     // One of the units is fixed or percent in both directions and stayed
349     // that way in the new style.  Therefore all we are doing is moving.
350     return true;
351 }
352
353 StyleDifference RenderStyle::diff(const RenderStyle* other, unsigned& changedContextSensitiveProperties) const
354 {
355     changedContextSensitiveProperties = ContextSensitivePropertyNone;
356
357 #if ENABLE(SVG)
358     StyleDifference svgChange = StyleDifferenceEqual;
359     if (m_svgStyle != other->m_svgStyle) {
360         svgChange = m_svgStyle->diff(other->m_svgStyle.get());
361         if (svgChange == StyleDifferenceLayout)
362             return svgChange;
363     }
364 #endif
365
366     if (m_box->width() != other->m_box->width()
367         || m_box->minWidth() != other->m_box->minWidth()
368         || m_box->maxWidth() != other->m_box->maxWidth()
369         || m_box->height() != other->m_box->height()
370         || m_box->minHeight() != other->m_box->minHeight()
371         || m_box->maxHeight() != other->m_box->maxHeight())
372         return StyleDifferenceLayout;
373
374     if (m_box->verticalAlign() != other->m_box->verticalAlign() || noninherited_flags._vertical_align != other->noninherited_flags._vertical_align)
375         return StyleDifferenceLayout;
376
377     if (m_box->boxSizing() != other->m_box->boxSizing())
378         return StyleDifferenceLayout;
379
380     if (surround->margin != other->surround->margin)
381         return StyleDifferenceLayout;
382
383     if (surround->padding != other->surround->padding)
384         return StyleDifferenceLayout;
385
386     if (rareNonInheritedData.get() != other->rareNonInheritedData.get()) {
387         if (rareNonInheritedData->m_appearance != other->rareNonInheritedData->m_appearance 
388             || rareNonInheritedData->marginBeforeCollapse != other->rareNonInheritedData->marginBeforeCollapse
389             || rareNonInheritedData->marginAfterCollapse != other->rareNonInheritedData->marginAfterCollapse
390             || rareNonInheritedData->lineClamp != other->rareNonInheritedData->lineClamp
391             || rareNonInheritedData->textOverflow != other->rareNonInheritedData->textOverflow)
392             return StyleDifferenceLayout;
393
394         if (rareNonInheritedData->m_regionOverflow != other->rareNonInheritedData->m_regionOverflow)
395             return StyleDifferenceLayout;
396
397         if (rareNonInheritedData->m_wrapFlow != other->rareNonInheritedData->m_wrapFlow
398             || rareNonInheritedData->m_wrapThrough != other->rareNonInheritedData->m_wrapThrough
399             || rareNonInheritedData->m_wrapMargin != other->rareNonInheritedData->m_wrapMargin
400             || rareNonInheritedData->m_wrapPadding != other->rareNonInheritedData->m_wrapPadding)
401             return StyleDifferenceLayout;
402
403         if (rareNonInheritedData->m_deprecatedFlexibleBox.get() != other->rareNonInheritedData->m_deprecatedFlexibleBox.get()
404             && *rareNonInheritedData->m_deprecatedFlexibleBox.get() != *other->rareNonInheritedData->m_deprecatedFlexibleBox.get())
405             return StyleDifferenceLayout;
406
407         if (rareNonInheritedData->m_flexibleBox.get() != other->rareNonInheritedData->m_flexibleBox.get()
408             && *rareNonInheritedData->m_flexibleBox.get() != *other->rareNonInheritedData->m_flexibleBox.get())
409             return StyleDifferenceLayout;
410         if (rareNonInheritedData->m_order != other->rareNonInheritedData->m_order
411             || rareNonInheritedData->m_alignContent != other->rareNonInheritedData->m_alignContent
412             || rareNonInheritedData->m_alignItems != other->rareNonInheritedData->m_alignItems
413             || rareNonInheritedData->m_alignSelf != other->rareNonInheritedData->m_alignSelf
414             || rareNonInheritedData->m_justifyContent != other->rareNonInheritedData->m_justifyContent)
415             return StyleDifferenceLayout;
416
417         // FIXME: We should add an optimized form of layout that just recomputes visual overflow.
418         if (!rareNonInheritedData->shadowDataEquivalent(*other->rareNonInheritedData.get()))
419             return StyleDifferenceLayout;
420
421         if (!rareNonInheritedData->reflectionDataEquivalent(*other->rareNonInheritedData.get()))
422             return StyleDifferenceLayout;
423
424         if (rareNonInheritedData->m_multiCol.get() != other->rareNonInheritedData->m_multiCol.get()
425             && *rareNonInheritedData->m_multiCol.get() != *other->rareNonInheritedData->m_multiCol.get())
426             return StyleDifferenceLayout;
427
428         if (rareNonInheritedData->m_transform.get() != other->rareNonInheritedData->m_transform.get()
429             && *rareNonInheritedData->m_transform.get() != *other->rareNonInheritedData->m_transform.get()) {
430 #if USE(ACCELERATED_COMPOSITING)
431             changedContextSensitiveProperties |= ContextSensitivePropertyTransform;
432             // Don't return; keep looking for another change
433 #else
434             return StyleDifferenceLayout;
435 #endif
436         }
437
438         if (rareNonInheritedData->m_grid.get() != other->rareNonInheritedData->m_grid.get()
439             && rareNonInheritedData->m_gridItem.get() != other->rareNonInheritedData->m_gridItem.get())
440             return StyleDifferenceLayout;
441
442 #if !USE(ACCELERATED_COMPOSITING)
443         if (rareNonInheritedData.get() != other->rareNonInheritedData.get()) {
444             if (rareNonInheritedData->m_transformStyle3D != other->rareNonInheritedData->m_transformStyle3D
445                 || rareNonInheritedData->m_backfaceVisibility != other->rareNonInheritedData->m_backfaceVisibility
446                 || rareNonInheritedData->m_perspective != other->rareNonInheritedData->m_perspective
447                 || rareNonInheritedData->m_perspectiveOriginX != other->rareNonInheritedData->m_perspectiveOriginX
448                 || rareNonInheritedData->m_perspectiveOriginY != other->rareNonInheritedData->m_perspectiveOriginY)
449                 return StyleDifferenceLayout;
450         }
451 #endif
452
453 #if ENABLE(DASHBOARD_SUPPORT) || ENABLE(WIDGET_REGION)
454         // If regions change, trigger a relayout to re-calc regions.
455         if (rareNonInheritedData->m_dashboardRegions != other->rareNonInheritedData->m_dashboardRegions)
456             return StyleDifferenceLayout;
457 #endif
458     }
459
460     if (rareInheritedData.get() != other->rareInheritedData.get()) {
461         if (rareInheritedData->highlight != other->rareInheritedData->highlight
462             || rareInheritedData->indent != other->rareInheritedData->indent
463             || rareInheritedData->m_effectiveZoom != other->rareInheritedData->m_effectiveZoom
464             || rareInheritedData->textSizeAdjust != other->rareInheritedData->textSizeAdjust
465             || rareInheritedData->wordBreak != other->rareInheritedData->wordBreak
466             || rareInheritedData->wordWrap != other->rareInheritedData->wordWrap
467             || rareInheritedData->nbspMode != other->rareInheritedData->nbspMode
468             || rareInheritedData->khtmlLineBreak != other->rareInheritedData->khtmlLineBreak
469             || rareInheritedData->textSecurity != other->rareInheritedData->textSecurity
470             || rareInheritedData->hyphens != other->rareInheritedData->hyphens
471             || rareInheritedData->hyphenationLimitBefore != other->rareInheritedData->hyphenationLimitBefore
472             || rareInheritedData->hyphenationLimitAfter != other->rareInheritedData->hyphenationLimitAfter
473             || rareInheritedData->hyphenationString != other->rareInheritedData->hyphenationString
474             || rareInheritedData->locale != other->rareInheritedData->locale
475             || rareInheritedData->textEmphasisMark != other->rareInheritedData->textEmphasisMark
476             || rareInheritedData->textEmphasisPosition != other->rareInheritedData->textEmphasisPosition
477             || rareInheritedData->textEmphasisCustomMark != other->rareInheritedData->textEmphasisCustomMark
478             || rareInheritedData->m_tabSize != other->rareInheritedData->m_tabSize
479             || rareInheritedData->m_lineBoxContain != other->rareInheritedData->m_lineBoxContain
480             || rareInheritedData->m_lineGrid != other->rareInheritedData->m_lineGrid
481 #if ENABLE(CSS_IMAGE_RESOLUTION)
482             || rareInheritedData->m_imageResolutionSource != other->rareInheritedData->m_imageResolutionSource
483             || rareInheritedData->m_imageResolutionSnap != other->rareInheritedData->m_imageResolutionSnap
484             || rareInheritedData->m_imageResolution != other->rareInheritedData->m_imageResolution
485 #endif
486             || rareInheritedData->m_lineSnap != other->rareInheritedData->m_lineSnap
487             || rareInheritedData->m_lineAlign != other->rareInheritedData->m_lineAlign)
488             return StyleDifferenceLayout;
489
490         if (!rareInheritedData->shadowDataEquivalent(*other->rareInheritedData.get()))
491             return StyleDifferenceLayout;
492
493         if (textStrokeWidth() != other->textStrokeWidth())
494             return StyleDifferenceLayout;
495     }
496
497 #if ENABLE(TEXT_AUTOSIZING)
498     if (visual->m_textAutosizingMultiplier != other->visual->m_textAutosizingMultiplier)
499         return StyleDifferenceLayout;
500 #endif
501
502     if (inherited->line_height != other->inherited->line_height
503         || inherited->list_style_image != other->inherited->list_style_image
504         || inherited->font != other->inherited->font
505         || inherited->horizontal_border_spacing != other->inherited->horizontal_border_spacing
506         || inherited->vertical_border_spacing != other->inherited->vertical_border_spacing
507         || inherited_flags._box_direction != other->inherited_flags._box_direction
508         || inherited_flags.m_rtlOrdering != other->inherited_flags.m_rtlOrdering
509         || noninherited_flags._position != other->noninherited_flags._position
510         || noninherited_flags._floating != other->noninherited_flags._floating
511         || noninherited_flags._originalDisplay != other->noninherited_flags._originalDisplay)
512         return StyleDifferenceLayout;
513
514
515     if (((int)noninherited_flags._effectiveDisplay) >= TABLE) {
516         if (inherited_flags._border_collapse != other->inherited_flags._border_collapse
517             || inherited_flags._empty_cells != other->inherited_flags._empty_cells
518             || inherited_flags._caption_side != other->inherited_flags._caption_side
519             || noninherited_flags._table_layout != other->noninherited_flags._table_layout)
520             return StyleDifferenceLayout;
521
522         // In the collapsing border model, 'hidden' suppresses other borders, while 'none'
523         // does not, so these style differences can be width differences.
524         if (inherited_flags._border_collapse
525             && ((borderTopStyle() == BHIDDEN && other->borderTopStyle() == BNONE)
526                 || (borderTopStyle() == BNONE && other->borderTopStyle() == BHIDDEN)
527                 || (borderBottomStyle() == BHIDDEN && other->borderBottomStyle() == BNONE)
528                 || (borderBottomStyle() == BNONE && other->borderBottomStyle() == BHIDDEN)
529                 || (borderLeftStyle() == BHIDDEN && other->borderLeftStyle() == BNONE)
530                 || (borderLeftStyle() == BNONE && other->borderLeftStyle() == BHIDDEN)
531                 || (borderRightStyle() == BHIDDEN && other->borderRightStyle() == BNONE)
532                 || (borderRightStyle() == BNONE && other->borderRightStyle() == BHIDDEN)))
533             return StyleDifferenceLayout;
534     }
535
536     if (noninherited_flags._effectiveDisplay == LIST_ITEM) {
537         if (inherited_flags._list_style_type != other->inherited_flags._list_style_type
538             || inherited_flags._list_style_position != other->inherited_flags._list_style_position)
539             return StyleDifferenceLayout;
540     }
541
542     if (inherited_flags._text_align != other->inherited_flags._text_align
543         || inherited_flags._text_transform != other->inherited_flags._text_transform
544         || inherited_flags._direction != other->inherited_flags._direction
545         || inherited_flags._white_space != other->inherited_flags._white_space
546         || noninherited_flags._clear != other->noninherited_flags._clear
547         || noninherited_flags._unicodeBidi != other->noninherited_flags._unicodeBidi)
548         return StyleDifferenceLayout;
549
550     // Check block flow direction.
551     if (inherited_flags.m_writingMode != other->inherited_flags.m_writingMode)
552         return StyleDifferenceLayout;
553
554     // Check text combine mode.
555     if (rareNonInheritedData->m_textCombine != other->rareNonInheritedData->m_textCombine)
556         return StyleDifferenceLayout;
557
558     // Overflow returns a layout hint.
559     if (noninherited_flags._overflowX != other->noninherited_flags._overflowX
560         || noninherited_flags._overflowY != other->noninherited_flags._overflowY)
561         return StyleDifferenceLayout;
562
563     // If our border widths change, then we need to layout.  Other changes to borders
564     // only necessitate a repaint.
565     if (borderLeftWidth() != other->borderLeftWidth()
566         || borderTopWidth() != other->borderTopWidth()
567         || borderBottomWidth() != other->borderBottomWidth()
568         || borderRightWidth() != other->borderRightWidth())
569         return StyleDifferenceLayout;
570
571     // If the counter directives change, trigger a relayout to re-calculate counter values and rebuild the counter node tree.
572     const CounterDirectiveMap* mapA = rareNonInheritedData->m_counterDirectives.get();
573     const CounterDirectiveMap* mapB = other->rareNonInheritedData->m_counterDirectives.get();
574     if (!(mapA == mapB || (mapA && mapB && *mapA == *mapB)))
575         return StyleDifferenceLayout;
576
577     if ((visibility() == COLLAPSE) != (other->visibility() == COLLAPSE))
578         return StyleDifferenceLayout;
579
580     if ((rareNonInheritedData->opacity == 1 && other->rareNonInheritedData->opacity < 1)
581         || (rareNonInheritedData->opacity < 1 && other->rareNonInheritedData->opacity == 1)) {
582         // FIXME: We would like to use SimplifiedLayout here, but we can't quite do that yet.
583         // We need to make sure SimplifiedLayout can operate correctly on RenderInlines (we will need
584         // to add a selfNeedsSimplifiedLayout bit in order to not get confused and taint every line).
585         // In addition we need to solve the floating object issue when layers come and go. Right now
586         // a full layout is necessary to keep floating object lists sane.
587         return StyleDifferenceLayout;
588     }
589
590     if (!QuotesData::equals(rareInheritedData->quotes.get(), other->rareInheritedData->quotes.get()))
591         return StyleDifferenceLayout;
592
593 #if ENABLE(SVG)
594     // SVGRenderStyle::diff() might have returned StyleDifferenceRepaint, eg. if fill changes.
595     // If eg. the font-size changed at the same time, we're not allowed to return StyleDifferenceRepaint,
596     // but have to return StyleDifferenceLayout, that's why  this if branch comes after all branches
597     // that are relevant for SVG and might return StyleDifferenceLayout.
598     if (svgChange != StyleDifferenceEqual)
599         return svgChange;
600 #endif
601
602     // Make sure these left/top/right/bottom checks stay below all layout checks and above
603     // all visible checks.
604     if (position() != StaticPosition) {
605         if (surround->offset != other->surround->offset) {
606              // Optimize for the case where a positioned layer is moving but not changing size.
607             if (position() == AbsolutePosition && positionedObjectMoved(surround->offset, other->surround->offset))
608                 return StyleDifferenceLayoutPositionedMovementOnly;
609
610             // FIXME: We would like to use SimplifiedLayout for relative positioning, but we can't quite do that yet.
611             // We need to make sure SimplifiedLayout can operate correctly on RenderInlines (we will need
612             // to add a selfNeedsSimplifiedLayout bit in order to not get confused and taint every line).
613             return StyleDifferenceLayout;
614         } else if (m_box->zIndex() != other->m_box->zIndex() || m_box->hasAutoZIndex() != other->m_box->hasAutoZIndex()
615                  || visual->clip != other->visual->clip || visual->hasClip != other->visual->hasClip)
616             return StyleDifferenceRepaintLayer;
617     }
618     
619 #if ENABLE(CSS_COMPOSITING)
620     if (rareNonInheritedData->m_effectiveBlendMode != other->rareNonInheritedData->m_effectiveBlendMode)
621         return StyleDifferenceRepaintLayer;
622 #endif
623
624     if (rareNonInheritedData->opacity != other->rareNonInheritedData->opacity) {
625 #if USE(ACCELERATED_COMPOSITING)
626         changedContextSensitiveProperties |= ContextSensitivePropertyOpacity;
627         // Don't return; keep looking for another change.
628 #else
629         return StyleDifferenceRepaintLayer;
630 #endif
631     }
632
633 #if ENABLE(CSS_FILTERS)
634     if (rareNonInheritedData->m_filter.get() != other->rareNonInheritedData->m_filter.get()
635         && *rareNonInheritedData->m_filter.get() != *other->rareNonInheritedData->m_filter.get()) {
636 #if USE(ACCELERATED_COMPOSITING)
637         changedContextSensitiveProperties |= ContextSensitivePropertyFilter;
638         // Don't return; keep looking for another change.
639 #else
640         return StyleDifferenceRepaintLayer;
641 #endif
642     }
643 #endif
644
645     if (rareNonInheritedData->m_mask != other->rareNonInheritedData->m_mask
646         || rareNonInheritedData->m_maskBoxImage != other->rareNonInheritedData->m_maskBoxImage)
647         return StyleDifferenceRepaintLayer;
648
649     if (inherited->color != other->inherited->color
650         || inherited_flags._visibility != other->inherited_flags._visibility
651         || inherited_flags._text_decorations != other->inherited_flags._text_decorations
652         || inherited_flags.m_printColorAdjust != other->inherited_flags.m_printColorAdjust
653         || inherited_flags._insideLink != other->inherited_flags._insideLink
654         || surround->border != other->surround->border
655         || *m_background.get() != *other->m_background.get()
656         || visual->textDecoration != other->visual->textDecoration
657         || rareInheritedData->userModify != other->rareInheritedData->userModify
658         || rareInheritedData->userSelect != other->rareInheritedData->userSelect
659         || rareNonInheritedData->userDrag != other->rareNonInheritedData->userDrag
660         || rareNonInheritedData->m_borderFit != other->rareNonInheritedData->m_borderFit
661 #if ENABLE(CSS3_TEXT_DECORATION)
662         || rareNonInheritedData->m_textDecorationStyle != other->rareNonInheritedData->m_textDecorationStyle
663 #endif // CSS3_TEXT_DECORATION
664         || rareInheritedData->textFillColor != other->rareInheritedData->textFillColor
665         || rareInheritedData->textStrokeColor != other->rareInheritedData->textStrokeColor
666         || rareInheritedData->textEmphasisColor != other->rareInheritedData->textEmphasisColor
667         || rareInheritedData->textEmphasisFill != other->rareInheritedData->textEmphasisFill
668         || rareInheritedData->m_imageRendering != other->rareInheritedData->m_imageRendering)
669         return StyleDifferenceRepaint;
670         
671         // FIXME: The current spec is being reworked to remove dependencies between exclusions and affected 
672         // content. There's a proposal to use floats instead. In that case, wrap-shape should actually relayout 
673         // the parent container. For sure, I will have to revisit this code, but for now I've added this in order 
674         // to avoid having diff() == StyleDifferenceEqual where wrap-shapes actually differ.
675         // Tracking bug: https://bugs.webkit.org/show_bug.cgi?id=62991
676         if (rareNonInheritedData->m_wrapShapeInside != other->rareNonInheritedData->m_wrapShapeInside
677             || rareNonInheritedData->m_wrapShapeOutside != other->rareNonInheritedData->m_wrapShapeOutside)
678             return StyleDifferenceRepaint;
679
680 #if USE(ACCELERATED_COMPOSITING)
681     if (rareNonInheritedData.get() != other->rareNonInheritedData.get()) {
682         if (rareNonInheritedData->m_transformStyle3D != other->rareNonInheritedData->m_transformStyle3D
683             || rareNonInheritedData->m_backfaceVisibility != other->rareNonInheritedData->m_backfaceVisibility
684             || rareNonInheritedData->m_perspective != other->rareNonInheritedData->m_perspective
685             || rareNonInheritedData->m_perspectiveOriginX != other->rareNonInheritedData->m_perspectiveOriginX
686             || rareNonInheritedData->m_perspectiveOriginY != other->rareNonInheritedData->m_perspectiveOriginY)
687             return StyleDifferenceRecompositeLayer;
688     }
689 #endif
690
691     // Cursors are not checked, since they will be set appropriately in response to mouse events,
692     // so they don't need to cause any repaint or layout.
693
694     // 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
695     // the resulting transition properly.
696     return StyleDifferenceEqual;
697 }
698
699 void RenderStyle::setClip(Length top, Length right, Length bottom, Length left)
700 {
701     StyleVisualData* data = visual.access();
702     data->clip.m_top = top;
703     data->clip.m_right = right;
704     data->clip.m_bottom = bottom;
705     data->clip.m_left = left;
706 }
707
708 void RenderStyle::addCursor(PassRefPtr<StyleImage> image, const IntPoint& hotSpot)
709 {
710     if (!rareInheritedData.access()->cursorData)
711         rareInheritedData.access()->cursorData = CursorList::create();
712     rareInheritedData.access()->cursorData->append(CursorData(image, hotSpot));
713 }
714
715 void RenderStyle::setCursorList(PassRefPtr<CursorList> other)
716 {
717     rareInheritedData.access()->cursorData = other;
718 }
719
720 void RenderStyle::setQuotes(PassRefPtr<QuotesData> q)
721 {
722     if (QuotesData::equals(rareInheritedData->quotes.get(), q.get()))
723         return;
724     rareInheritedData.access()->quotes = q;
725 }
726
727 void RenderStyle::clearCursorList()
728 {
729     if (rareInheritedData->cursorData)
730         rareInheritedData.access()->cursorData = 0;
731 }
732
733 void RenderStyle::clearContent()
734 {
735     if (rareNonInheritedData->m_content)
736         rareNonInheritedData.access()->m_content = nullptr;
737 }
738
739 void RenderStyle::appendContent(PassOwnPtr<ContentData> contentData)
740 {
741     OwnPtr<ContentData>& content = rareNonInheritedData.access()->m_content;
742     ContentData* lastContent = content.get();
743     while (lastContent && lastContent->next())
744         lastContent = lastContent->next();
745
746     if (lastContent)
747         lastContent->setNext(contentData);
748     else
749         content = contentData;
750 }
751
752 void RenderStyle::setContent(PassRefPtr<StyleImage> image, bool add)
753 {
754     if (!image)
755         return;
756         
757     if (add) {
758         appendContent(ContentData::create(image));
759         return;
760     }
761
762     rareNonInheritedData.access()->m_content = ContentData::create(image);
763 }
764
765 void RenderStyle::setContent(const String& string, bool add)
766 {
767     OwnPtr<ContentData>& content = rareNonInheritedData.access()->m_content;
768     if (add) {
769         ContentData* lastContent = content.get();
770         while (lastContent && lastContent->next())
771             lastContent = lastContent->next();
772
773         if (lastContent) {
774             // We attempt to merge with the last ContentData if possible.
775             if (lastContent->isText()) {
776                 TextContentData* textContent = static_cast<TextContentData*>(lastContent);
777                 String text = textContent->text();
778                 text += string;
779                 textContent->setText(text);
780             } else
781                 lastContent->setNext(ContentData::create(string));
782
783             return;
784         }
785     }
786
787     content = ContentData::create(string);
788 }
789
790 void RenderStyle::setContent(PassOwnPtr<CounterContent> counter, bool add)
791 {
792     if (!counter)
793         return;
794
795     if (add) {
796         appendContent(ContentData::create(counter));
797         return;
798     }
799
800     rareNonInheritedData.access()->m_content = ContentData::create(counter);
801 }
802
803 void RenderStyle::setContent(QuoteType quote, bool add)
804 {
805     if (add) {
806         appendContent(ContentData::create(quote));
807         return;
808     }
809
810     rareNonInheritedData.access()->m_content = ContentData::create(quote);
811 }
812     
813 inline bool requireTransformOrigin(const Vector<RefPtr<TransformOperation> >& transformOperations, RenderStyle::ApplyTransformOrigin applyOrigin)
814 {
815     // transform-origin brackets the transform with translate operations.
816     // Optimize for the case where the only transform is a translation, since the transform-origin is irrelevant
817     // in that case.
818     if (applyOrigin != RenderStyle::IncludeTransformOrigin)
819         return false;
820
821     unsigned size = transformOperations.size();
822     for (unsigned i = 0; i < size; ++i) {
823         TransformOperation::OperationType type = transformOperations[i]->getOperationType();
824         if (type != TransformOperation::TRANSLATE_X
825             && type != TransformOperation::TRANSLATE_Y
826             && type != TransformOperation::TRANSLATE 
827             && type != TransformOperation::TRANSLATE_Z
828             && type != TransformOperation::TRANSLATE_3D)
829             return true;
830     }
831     
832     return false;
833 }
834
835 void RenderStyle::applyTransform(TransformationMatrix& transform, const LayoutSize& borderBoxSize, ApplyTransformOrigin applyOrigin) const
836 {
837     // FIXME: when subpixel layout is supported (bug 71143) the body of this function could be replaced by
838     // applyTransform(transform, FloatRect(FloatPoint(), borderBoxSize), applyOrigin);
839     
840     const Vector<RefPtr<TransformOperation> >& transformOperations = rareNonInheritedData->m_transform->m_operations.operations();
841     bool applyTransformOrigin = requireTransformOrigin(transformOperations, applyOrigin);
842
843     if (applyTransformOrigin)
844         transform.translate3d(floatValueForLength(transformOriginX(), borderBoxSize.width()), floatValueForLength(transformOriginY(), borderBoxSize.height()), transformOriginZ());
845
846     unsigned size = transformOperations.size();
847     for (unsigned i = 0; i < size; ++i)
848         transformOperations[i]->apply(transform, borderBoxSize);
849
850     if (applyTransformOrigin)
851         transform.translate3d(-floatValueForLength(transformOriginX(), borderBoxSize.width()), -floatValueForLength(transformOriginY(), borderBoxSize.height()), -transformOriginZ()); 
852 }
853
854 void RenderStyle::applyTransform(TransformationMatrix& transform, const FloatRect& boundingBox, ApplyTransformOrigin applyOrigin) const
855 {
856     const Vector<RefPtr<TransformOperation> >& transformOperations = rareNonInheritedData->m_transform->m_operations.operations();
857     bool applyTransformOrigin = requireTransformOrigin(transformOperations, applyOrigin);
858     
859     float offsetX = transformOriginX().type() == Percent ? boundingBox.x() : 0;
860     float offsetY = transformOriginY().type() == Percent ? boundingBox.y() : 0;
861     
862     if (applyTransformOrigin) {
863         transform.translate3d(floatValueForLength(transformOriginX(), boundingBox.width()) + offsetX,
864                               floatValueForLength(transformOriginY(), boundingBox.height()) + offsetY,
865                               transformOriginZ());
866     }
867     
868     unsigned size = transformOperations.size();
869     for (unsigned i = 0; i < size; ++i)
870         transformOperations[i]->apply(transform, boundingBox.size());
871     
872     if (applyTransformOrigin) {
873         transform.translate3d(-floatValueForLength(transformOriginX(), boundingBox.width()) - offsetX,
874                               -floatValueForLength(transformOriginY(), boundingBox.height()) - offsetY,
875                               -transformOriginZ());
876     }
877 }
878
879 void RenderStyle::setPageScaleTransform(float scale)
880 {
881     if (scale == 1)
882         return;
883     TransformOperations transform;
884     transform.operations().append(ScaleTransformOperation::create(scale, scale, ScaleTransformOperation::SCALE));
885     setTransform(transform);
886     setTransformOriginX(Length(0, Fixed));
887     setTransformOriginY(Length(0, Fixed));
888 }
889
890 void RenderStyle::setTextShadow(PassOwnPtr<ShadowData> shadowData, bool add)
891 {
892     ASSERT(!shadowData || (!shadowData->spread() && shadowData->style() == Normal));
893
894     StyleRareInheritedData* rareData = rareInheritedData.access();
895     if (!add) {
896         rareData->textShadow = shadowData;
897         return;
898     }
899
900     shadowData->setNext(rareData->textShadow.release());
901     rareData->textShadow = shadowData;
902 }
903
904 void RenderStyle::setBoxShadow(PassOwnPtr<ShadowData> shadowData, bool add)
905 {
906     StyleRareNonInheritedData* rareData = rareNonInheritedData.access();
907     if (!add) {
908         rareData->m_boxShadow = shadowData;
909         return;
910     }
911
912     shadowData->setNext(rareData->m_boxShadow.release());
913     rareData->m_boxShadow = shadowData;
914 }
915
916 static RoundedRect::Radii calcRadiiFor(const BorderData& border, IntSize size, RenderView* renderView)
917 {
918     return RoundedRect::Radii(
919         IntSize(valueForLength(border.topLeft().width(), size.width(), renderView), 
920                 valueForLength(border.topLeft().height(), size.height(), renderView)),
921         IntSize(valueForLength(border.topRight().width(), size.width(), renderView),
922                 valueForLength(border.topRight().height(), size.height(), renderView)),
923         IntSize(valueForLength(border.bottomLeft().width(), size.width(), renderView), 
924                 valueForLength(border.bottomLeft().height(), size.height(), renderView)),
925         IntSize(valueForLength(border.bottomRight().width(), size.width(), renderView), 
926                 valueForLength(border.bottomRight().height(), size.height(), renderView)));
927 }
928
929 static float calcConstraintScaleFor(const IntRect& rect, const RoundedRect::Radii& radii)
930 {
931     // Constrain corner radii using CSS3 rules:
932     // http://www.w3.org/TR/css3-background/#the-border-radius
933     
934     float factor = 1;
935     unsigned radiiSum;
936
937     // top
938     radiiSum = static_cast<unsigned>(radii.topLeft().width()) + static_cast<unsigned>(radii.topRight().width()); // Casts to avoid integer overflow.
939     if (radiiSum > static_cast<unsigned>(rect.width()))
940         factor = min(static_cast<float>(rect.width()) / radiiSum, factor);
941
942     // bottom
943     radiiSum = static_cast<unsigned>(radii.bottomLeft().width()) + static_cast<unsigned>(radii.bottomRight().width());
944     if (radiiSum > static_cast<unsigned>(rect.width()))
945         factor = min(static_cast<float>(rect.width()) / radiiSum, factor);
946     
947     // left
948     radiiSum = static_cast<unsigned>(radii.topLeft().height()) + static_cast<unsigned>(radii.bottomLeft().height());
949     if (radiiSum > static_cast<unsigned>(rect.height()))
950         factor = min(static_cast<float>(rect.height()) / radiiSum, factor);
951     
952     // right
953     radiiSum = static_cast<unsigned>(radii.topRight().height()) + static_cast<unsigned>(radii.bottomRight().height());
954     if (radiiSum > static_cast<unsigned>(rect.height()))
955         factor = min(static_cast<float>(rect.height()) / radiiSum, factor);
956     
957     ASSERT(factor <= 1);
958     return factor;
959 }
960
961 StyleImage* RenderStyle::listStyleImage() const { return inherited->list_style_image.get(); }
962 void RenderStyle::setListStyleImage(PassRefPtr<StyleImage> v)
963 {
964     if (inherited->list_style_image != v)
965         inherited.access()->list_style_image = v;
966 }
967
968 Color RenderStyle::color() const { return inherited->color; }
969 Color RenderStyle::visitedLinkColor() const { return inherited->visitedLinkColor; }
970 void RenderStyle::setColor(const Color& v) { SET_VAR(inherited, color, v) };
971 void RenderStyle::setVisitedLinkColor(const Color& v) { SET_VAR(inherited, visitedLinkColor, v) }
972
973 short RenderStyle::horizontalBorderSpacing() const { return inherited->horizontal_border_spacing; }
974 short RenderStyle::verticalBorderSpacing() const { return inherited->vertical_border_spacing; }
975 void RenderStyle::setHorizontalBorderSpacing(short v) { SET_VAR(inherited, horizontal_border_spacing, v) }
976 void RenderStyle::setVerticalBorderSpacing(short v) { SET_VAR(inherited, vertical_border_spacing, v) }
977
978 RoundedRect RenderStyle::getRoundedBorderFor(const LayoutRect& borderRect, RenderView* renderView, bool includeLogicalLeftEdge, bool includeLogicalRightEdge) const
979 {
980     IntRect snappedBorderRect(pixelSnappedIntRect(borderRect));
981     RoundedRect roundedRect(snappedBorderRect);
982     if (hasBorderRadius()) {
983         RoundedRect::Radii radii = calcRadiiFor(surround->border, snappedBorderRect.size(), renderView);
984         radii.scale(calcConstraintScaleFor(snappedBorderRect, radii));
985         roundedRect.includeLogicalEdges(radii, isHorizontalWritingMode(), includeLogicalLeftEdge, includeLogicalRightEdge);
986     }
987     return roundedRect;
988 }
989
990 RoundedRect RenderStyle::getRoundedInnerBorderFor(const LayoutRect& borderRect, bool includeLogicalLeftEdge, bool includeLogicalRightEdge) const
991 {
992     bool horizontal = isHorizontalWritingMode();
993
994     LayoutUnit leftWidth = (!horizontal || includeLogicalLeftEdge) ? borderLeftWidth() : 0;
995     LayoutUnit rightWidth = (!horizontal || includeLogicalRightEdge) ? borderRightWidth() : 0;
996     LayoutUnit topWidth = (horizontal || includeLogicalLeftEdge) ? borderTopWidth() : 0;
997     LayoutUnit bottomWidth = (horizontal || includeLogicalRightEdge) ? borderBottomWidth() : 0;
998
999     return getRoundedInnerBorderFor(borderRect, topWidth, bottomWidth, leftWidth, rightWidth, includeLogicalLeftEdge, includeLogicalRightEdge);
1000 }
1001
1002 RoundedRect RenderStyle::getRoundedInnerBorderFor(const LayoutRect& borderRect,
1003     LayoutUnit topWidth, LayoutUnit bottomWidth, LayoutUnit leftWidth, LayoutUnit rightWidth, bool includeLogicalLeftEdge, bool includeLogicalRightEdge) const
1004 {
1005     LayoutRect innerRect(borderRect.x() + leftWidth, 
1006                borderRect.y() + topWidth, 
1007                borderRect.width() - leftWidth - rightWidth, 
1008                borderRect.height() - topWidth - bottomWidth);
1009
1010     RoundedRect roundedRect(pixelSnappedIntRect(innerRect));
1011
1012     if (hasBorderRadius()) {
1013         RoundedRect::Radii radii = getRoundedBorderFor(borderRect).radii();
1014         radii.shrink(topWidth, bottomWidth, leftWidth, rightWidth);
1015         roundedRect.includeLogicalEdges(radii, isHorizontalWritingMode(), includeLogicalLeftEdge, includeLogicalRightEdge);
1016     }
1017     return roundedRect;
1018 }
1019
1020 const CounterDirectiveMap* RenderStyle::counterDirectives() const
1021 {
1022     return rareNonInheritedData->m_counterDirectives.get();
1023 }
1024
1025 CounterDirectiveMap& RenderStyle::accessCounterDirectives()
1026 {
1027     OwnPtr<CounterDirectiveMap>& map = rareNonInheritedData.access()->m_counterDirectives;
1028     if (!map)
1029         map = adoptPtr(new CounterDirectiveMap);
1030     return *map;
1031 }
1032
1033 const AtomicString& RenderStyle::hyphenString() const
1034 {
1035     ASSERT(hyphens() != HyphensNone);
1036
1037     const AtomicString& hyphenationString = rareInheritedData.get()->hyphenationString;
1038     if (!hyphenationString.isNull())
1039         return hyphenationString;
1040
1041     // FIXME: This should depend on locale.
1042     DEFINE_STATIC_LOCAL(AtomicString, hyphenMinusString, (&hyphenMinus, 1));
1043     DEFINE_STATIC_LOCAL(AtomicString, hyphenString, (&hyphen, 1));
1044     return font().primaryFontHasGlyphForCharacter(hyphen) ? hyphenString : hyphenMinusString;
1045 }
1046
1047 const AtomicString& RenderStyle::textEmphasisMarkString() const
1048 {
1049     switch (textEmphasisMark()) {
1050     case TextEmphasisMarkNone:
1051         return nullAtom;
1052     case TextEmphasisMarkCustom:
1053         return textEmphasisCustomMark();
1054     case TextEmphasisMarkDot: {
1055         DEFINE_STATIC_LOCAL(AtomicString, filledDotString, (&bullet, 1));
1056         DEFINE_STATIC_LOCAL(AtomicString, openDotString, (&whiteBullet, 1));
1057         return textEmphasisFill() == TextEmphasisFillFilled ? filledDotString : openDotString;
1058     }
1059     case TextEmphasisMarkCircle: {
1060         DEFINE_STATIC_LOCAL(AtomicString, filledCircleString, (&blackCircle, 1));
1061         DEFINE_STATIC_LOCAL(AtomicString, openCircleString, (&whiteCircle, 1));
1062         return textEmphasisFill() == TextEmphasisFillFilled ? filledCircleString : openCircleString;
1063     }
1064     case TextEmphasisMarkDoubleCircle: {
1065         DEFINE_STATIC_LOCAL(AtomicString, filledDoubleCircleString, (&fisheye, 1));
1066         DEFINE_STATIC_LOCAL(AtomicString, openDoubleCircleString, (&bullseye, 1));
1067         return textEmphasisFill() == TextEmphasisFillFilled ? filledDoubleCircleString : openDoubleCircleString;
1068     }
1069     case TextEmphasisMarkTriangle: {
1070         DEFINE_STATIC_LOCAL(AtomicString, filledTriangleString, (&blackUpPointingTriangle, 1));
1071         DEFINE_STATIC_LOCAL(AtomicString, openTriangleString, (&whiteUpPointingTriangle, 1));
1072         return textEmphasisFill() == TextEmphasisFillFilled ? filledTriangleString : openTriangleString;
1073     }
1074     case TextEmphasisMarkSesame: {
1075         DEFINE_STATIC_LOCAL(AtomicString, filledSesameString, (&sesameDot, 1));
1076         DEFINE_STATIC_LOCAL(AtomicString, openSesameString, (&whiteSesameDot, 1));
1077         return textEmphasisFill() == TextEmphasisFillFilled ? filledSesameString : openSesameString;
1078     }
1079     case TextEmphasisMarkAuto:
1080         ASSERT_NOT_REACHED();
1081         return nullAtom;
1082     }
1083
1084     ASSERT_NOT_REACHED();
1085     return nullAtom;
1086 }
1087
1088 #if ENABLE(DASHBOARD_SUPPORT) || ENABLE(WIDGET_REGION)
1089 const Vector<StyleDashboardRegion>& RenderStyle::initialDashboardRegions()
1090 {
1091     DEFINE_STATIC_LOCAL(Vector<StyleDashboardRegion>, emptyList, ());
1092     return emptyList;
1093 }
1094
1095 const Vector<StyleDashboardRegion>& RenderStyle::noneDashboardRegions()
1096 {
1097     DEFINE_STATIC_LOCAL(Vector<StyleDashboardRegion>, noneList, ());
1098     static bool noneListInitialized = false;
1099
1100     if (!noneListInitialized) {
1101         StyleDashboardRegion region;
1102         region.label = "";
1103         region.offset.m_top  = Length();
1104         region.offset.m_right = Length();
1105         region.offset.m_bottom = Length();
1106         region.offset.m_left = Length();
1107         region.type = StyleDashboardRegion::None;
1108         noneList.append(region);
1109         noneListInitialized = true;
1110     }
1111     return noneList;
1112 }
1113 #endif
1114
1115 void RenderStyle::adjustAnimations()
1116 {
1117     AnimationList* animationList = rareNonInheritedData->m_animations.get();
1118     if (!animationList)
1119         return;
1120
1121     // Get rid of empty animations and anything beyond them
1122     for (size_t i = 0; i < animationList->size(); ++i) {
1123         if (animationList->animation(i)->isEmpty()) {
1124             animationList->resize(i);
1125             break;
1126         }
1127     }
1128
1129     if (animationList->isEmpty()) {
1130         clearAnimations();
1131         return;
1132     }
1133
1134     // Repeat patterns into layers that don't have some properties set.
1135     animationList->fillUnsetProperties();
1136 }
1137
1138 void RenderStyle::adjustTransitions()
1139 {
1140     AnimationList* transitionList = rareNonInheritedData->m_transitions.get();
1141     if (!transitionList)
1142         return;
1143
1144     // Get rid of empty transitions and anything beyond them
1145     for (size_t i = 0; i < transitionList->size(); ++i) {
1146         if (transitionList->animation(i)->isEmpty()) {
1147             transitionList->resize(i);
1148             break;
1149         }
1150     }
1151
1152     if (transitionList->isEmpty()) {
1153         clearTransitions();
1154         return;
1155     }
1156
1157     // Repeat patterns into layers that don't have some properties set.
1158     transitionList->fillUnsetProperties();
1159
1160     // Make sure there are no duplicate properties. This is an O(n^2) algorithm
1161     // but the lists tend to be very short, so it is probably ok
1162     for (size_t i = 0; i < transitionList->size(); ++i) {
1163         for (size_t j = i+1; j < transitionList->size(); ++j) {
1164             if (transitionList->animation(i)->property() == transitionList->animation(j)->property()) {
1165                 // toss i
1166                 transitionList->remove(i);
1167                 j = i;
1168             }
1169         }
1170     }
1171 }
1172
1173 AnimationList* RenderStyle::accessAnimations()
1174 {
1175     if (!rareNonInheritedData.access()->m_animations)
1176         rareNonInheritedData.access()->m_animations = adoptPtr(new AnimationList());
1177     return rareNonInheritedData->m_animations.get();
1178 }
1179
1180 AnimationList* RenderStyle::accessTransitions()
1181 {
1182     if (!rareNonInheritedData.access()->m_transitions)
1183         rareNonInheritedData.access()->m_transitions = adoptPtr(new AnimationList());
1184     return rareNonInheritedData->m_transitions.get();
1185 }
1186
1187 const Animation* RenderStyle::transitionForProperty(CSSPropertyID property) const
1188 {
1189     if (transitions()) {
1190         for (size_t i = 0; i < transitions()->size(); ++i) {
1191             const Animation* p = transitions()->animation(i);
1192             if (p->animationMode() == Animation::AnimateAll || p->property() == property) {
1193                 return p;
1194             }
1195         }
1196     }
1197     return 0;
1198 }
1199
1200 const Font& RenderStyle::font() const { return inherited->font; }
1201 const FontMetrics& RenderStyle::fontMetrics() const { return inherited->font.fontMetrics(); }
1202 const FontDescription& RenderStyle::fontDescription() const { return inherited->font.fontDescription(); }
1203 float RenderStyle::specifiedFontSize() const { return fontDescription().specifiedSize(); }
1204 float RenderStyle::computedFontSize() const { return fontDescription().computedSize(); }
1205 int RenderStyle::fontSize() const { return inherited->font.pixelSize(); }
1206
1207 int RenderStyle::wordSpacing() const { return inherited->font.wordSpacing(); }
1208 int RenderStyle::letterSpacing() const { return inherited->font.letterSpacing(); }
1209
1210 bool RenderStyle::setFontDescription(const FontDescription& v)
1211 {
1212     if (inherited->font.fontDescription() != v) {
1213         inherited.access()->font = Font(v, inherited->font.letterSpacing(), inherited->font.wordSpacing());
1214         return true;
1215     }
1216     return false;
1217 }
1218
1219 Length RenderStyle::specifiedLineHeight() const { return inherited->line_height; }
1220 Length RenderStyle::lineHeight() const
1221 {
1222     const Length& lh = inherited->line_height;
1223 #if ENABLE(TEXT_AUTOSIZING)
1224     // Unlike fontDescription().computedSize() and hence fontSize(), this is
1225     // recalculated on demand as we only store the specified line height.
1226     // FIXME: Should consider scaling the fixed part of any calc expressions
1227     // too, though this involves messily poking into CalcExpressionLength.
1228     float multiplier = textAutosizingMultiplier();
1229     if (multiplier > 1 && lh.isFixed())
1230         return Length(TextAutosizer::computeAutosizedFontSize(lh.value(), multiplier), Fixed);
1231 #endif
1232     return lh;
1233 }
1234 void RenderStyle::setLineHeight(Length specifiedLineHeight) { SET_VAR(inherited, line_height, specifiedLineHeight); }
1235
1236 int RenderStyle::computedLineHeight(RenderView* renderView) const
1237 {
1238     const Length& lh = lineHeight();
1239
1240     // Negative value means the line height is not set. Use the font's built-in spacing.
1241     if (lh.isNegative())
1242         return fontMetrics().lineSpacing();
1243
1244     if (lh.isPercent())
1245         return minimumValueForLength(lh, fontSize());
1246
1247     if (lh.isViewportPercentage())
1248         return valueForLength(lh, 0, renderView);
1249
1250     return lh.value();
1251 }
1252
1253 void RenderStyle::setWordSpacing(int v) { inherited.access()->font.setWordSpacing(v); }
1254 void RenderStyle::setLetterSpacing(int v) { inherited.access()->font.setLetterSpacing(v); }
1255
1256 void RenderStyle::setFontSize(float size)
1257 {
1258     // size must be specifiedSize if Text Autosizing is enabled, but computedSize if text
1259     // zoom is enabled (if neither is enabled it's irrelevant as they're probably the same).
1260
1261     FontSelector* currentFontSelector = font().fontSelector();
1262     FontDescription desc(fontDescription());
1263     desc.setSpecifiedSize(size);
1264     desc.setComputedSize(size);
1265
1266 #if ENABLE(TEXT_AUTOSIZING)
1267     float multiplier = textAutosizingMultiplier();
1268     if (multiplier > 1) {
1269         desc.setComputedSize(TextAutosizer::computeAutosizedFontSize(size, multiplier));
1270     }
1271 #endif
1272
1273     setFontDescription(desc);
1274     font().update(currentFontSelector);
1275 }
1276
1277 void RenderStyle::getShadowExtent(const ShadowData* shadow, LayoutUnit &top, LayoutUnit &right, LayoutUnit &bottom, LayoutUnit &left) const
1278 {
1279     top = 0;
1280     right = 0;
1281     bottom = 0;
1282     left = 0;
1283
1284     for ( ; shadow; shadow = shadow->next()) {
1285         if (shadow->style() == Inset)
1286             continue;
1287         int blurAndSpread = shadow->blur() + shadow->spread();
1288
1289         top = min<LayoutUnit>(top, shadow->y() - blurAndSpread);
1290         right = max<LayoutUnit>(right, shadow->x() + blurAndSpread);
1291         bottom = max<LayoutUnit>(bottom, shadow->y() + blurAndSpread);
1292         left = min<LayoutUnit>(left, shadow->x() - blurAndSpread);
1293     }
1294 }
1295
1296 void RenderStyle::getShadowHorizontalExtent(const ShadowData* shadow, LayoutUnit &left, LayoutUnit &right) const
1297 {
1298     left = 0;
1299     right = 0;
1300
1301     for ( ; shadow; shadow = shadow->next()) {
1302         if (shadow->style() == Inset)
1303             continue;
1304         int blurAndSpread = shadow->blur() + shadow->spread();
1305
1306         left = min<LayoutUnit>(left, shadow->x() - blurAndSpread);
1307         right = max<LayoutUnit>(right, shadow->x() + blurAndSpread);
1308     }
1309 }
1310
1311 void RenderStyle::getShadowVerticalExtent(const ShadowData* shadow, LayoutUnit &top, LayoutUnit &bottom) const
1312 {
1313     top = 0;
1314     bottom = 0;
1315
1316     for ( ; shadow; shadow = shadow->next()) {
1317         if (shadow->style() == Inset)
1318             continue;
1319         int blurAndSpread = shadow->blur() + shadow->spread();
1320
1321         top = min<LayoutUnit>(top, shadow->y() - blurAndSpread);
1322         bottom = max<LayoutUnit>(bottom, shadow->y() + blurAndSpread);
1323     }
1324 }
1325
1326 Color RenderStyle::colorIncludingFallback(int colorProperty, bool visitedLink) const
1327 {
1328     Color result;
1329     EBorderStyle borderStyle = BNONE;
1330     switch (colorProperty) {
1331     case CSSPropertyBackgroundColor:
1332         return visitedLink ? rareNonInheritedData->m_visitedLinkBackgroundColor : backgroundColor(); // Background color doesn't fall back.
1333     case CSSPropertyBorderLeftColor:
1334         result = visitedLink ? rareNonInheritedData->m_visitedLinkBorderLeftColor : borderLeftColor();
1335         borderStyle = borderLeftStyle();
1336         break;
1337     case CSSPropertyBorderRightColor:
1338         result = visitedLink ? rareNonInheritedData->m_visitedLinkBorderRightColor : borderRightColor();
1339         borderStyle = borderRightStyle();
1340         break;
1341     case CSSPropertyBorderTopColor:
1342         result = visitedLink ? rareNonInheritedData->m_visitedLinkBorderTopColor : borderTopColor();
1343         borderStyle = borderTopStyle();
1344         break;
1345     case CSSPropertyBorderBottomColor:
1346         result = visitedLink ? rareNonInheritedData->m_visitedLinkBorderBottomColor : borderBottomColor();
1347         borderStyle = borderBottomStyle();
1348         break;
1349     case CSSPropertyColor:
1350         result = visitedLink ? inherited->visitedLinkColor : color();
1351         break;
1352     case CSSPropertyOutlineColor:
1353         result = visitedLink ? rareNonInheritedData->m_visitedLinkOutlineColor : outlineColor();
1354         break;
1355     case CSSPropertyWebkitColumnRuleColor:
1356         result = visitedLink ? rareNonInheritedData->m_multiCol->m_visitedLinkColumnRuleColor : columnRuleColor();
1357         break;
1358     case CSSPropertyWebkitTextEmphasisColor:
1359         result = visitedLink ? rareInheritedData->visitedLinkTextEmphasisColor : textEmphasisColor();
1360         break;
1361     case CSSPropertyWebkitTextFillColor:
1362         result = visitedLink ? rareInheritedData->visitedLinkTextFillColor : textFillColor();
1363         break;
1364     case CSSPropertyWebkitTextStrokeColor:
1365         result = visitedLink ? rareInheritedData->visitedLinkTextStrokeColor : textStrokeColor();
1366         break;
1367     default:
1368         ASSERT_NOT_REACHED();
1369         break;
1370     }
1371
1372     if (!result.isValid()) {
1373         if (!visitedLink && (borderStyle == INSET || borderStyle == OUTSET || borderStyle == RIDGE || borderStyle == GROOVE))
1374             result.setRGB(238, 238, 238);
1375         else
1376             result = visitedLink ? inherited->visitedLinkColor : color();
1377     }
1378     return result;
1379 }
1380
1381 Color RenderStyle::visitedDependentColor(int colorProperty) const
1382 {
1383     Color unvisitedColor = colorIncludingFallback(colorProperty, false);
1384     if (insideLink() != InsideVisitedLink)
1385         return unvisitedColor;
1386
1387     Color visitedColor = colorIncludingFallback(colorProperty, true);
1388
1389     // FIXME: Technically someone could explicitly specify the color transparent, but for now we'll just
1390     // assume that if the background color is transparent that it wasn't set. Note that it's weird that
1391     // we're returning unvisited info for a visited link, but given our restriction that the alpha values
1392     // have to match, it makes more sense to return the unvisited background color if specified than it
1393     // does to return black. This behavior matches what Firefox 4 does as well.
1394     if (colorProperty == CSSPropertyBackgroundColor && visitedColor == Color::transparent)
1395         return unvisitedColor;
1396
1397     // Take the alpha from the unvisited color, but get the RGB values from the visited color.
1398     return Color(visitedColor.red(), visitedColor.green(), visitedColor.blue(), unvisitedColor.alpha());
1399 }
1400
1401 Length RenderStyle::logicalWidth() const
1402 {
1403     if (isHorizontalWritingMode())
1404         return width();
1405     return height();
1406 }
1407
1408 Length RenderStyle::logicalHeight() const
1409 {
1410     if (isHorizontalWritingMode())
1411         return height();
1412     return width();
1413 }
1414
1415 Length RenderStyle::logicalMinWidth() const
1416 {
1417     if (isHorizontalWritingMode())
1418         return minWidth();
1419     return minHeight();
1420 }
1421
1422 Length RenderStyle::logicalMaxWidth() const
1423 {
1424     if (isHorizontalWritingMode())
1425         return maxWidth();
1426     return maxHeight();
1427 }
1428
1429 Length RenderStyle::logicalMinHeight() const
1430 {
1431     if (isHorizontalWritingMode())
1432         return minHeight();
1433     return minWidth();
1434 }
1435
1436 Length RenderStyle::logicalMaxHeight() const
1437 {
1438     if (isHorizontalWritingMode())
1439         return maxHeight();
1440     return maxWidth();
1441 }
1442
1443 const BorderValue& RenderStyle::borderBefore() const
1444 {
1445     switch (writingMode()) {
1446     case TopToBottomWritingMode:
1447         return borderTop();
1448     case BottomToTopWritingMode:
1449         return borderBottom();
1450     case LeftToRightWritingMode:
1451         return borderLeft();
1452     case RightToLeftWritingMode:
1453         return borderRight();
1454     }
1455     ASSERT_NOT_REACHED();
1456     return borderTop();
1457 }
1458
1459 const BorderValue& RenderStyle::borderAfter() const
1460 {
1461     switch (writingMode()) {
1462     case TopToBottomWritingMode:
1463         return borderBottom();
1464     case BottomToTopWritingMode:
1465         return borderTop();
1466     case LeftToRightWritingMode:
1467         return borderRight();
1468     case RightToLeftWritingMode:
1469         return borderLeft();
1470     }
1471     ASSERT_NOT_REACHED();
1472     return borderBottom();
1473 }
1474
1475 const BorderValue& RenderStyle::borderStart() const
1476 {
1477     if (isHorizontalWritingMode())
1478         return isLeftToRightDirection() ? borderLeft() : borderRight();
1479     return isLeftToRightDirection() ? borderTop() : borderBottom();
1480 }
1481
1482 const BorderValue& RenderStyle::borderEnd() const
1483 {
1484     if (isHorizontalWritingMode())
1485         return isLeftToRightDirection() ? borderRight() : borderLeft();
1486     return isLeftToRightDirection() ? borderBottom() : borderTop();
1487 }
1488
1489 unsigned short RenderStyle::borderBeforeWidth() const
1490 {
1491     switch (writingMode()) {
1492     case TopToBottomWritingMode:
1493         return borderTopWidth();
1494     case BottomToTopWritingMode:
1495         return borderBottomWidth();
1496     case LeftToRightWritingMode:
1497         return borderLeftWidth();
1498     case RightToLeftWritingMode:
1499         return borderRightWidth();
1500     }
1501     ASSERT_NOT_REACHED();
1502     return borderTopWidth();
1503 }
1504
1505 unsigned short RenderStyle::borderAfterWidth() const
1506 {
1507     switch (writingMode()) {
1508     case TopToBottomWritingMode:
1509         return borderBottomWidth();
1510     case BottomToTopWritingMode:
1511         return borderTopWidth();
1512     case LeftToRightWritingMode:
1513         return borderRightWidth();
1514     case RightToLeftWritingMode:
1515         return borderLeftWidth();
1516     }
1517     ASSERT_NOT_REACHED();
1518     return borderBottomWidth();
1519 }
1520
1521 unsigned short RenderStyle::borderStartWidth() const
1522 {
1523     if (isHorizontalWritingMode())
1524         return isLeftToRightDirection() ? borderLeftWidth() : borderRightWidth();
1525     return isLeftToRightDirection() ? borderTopWidth() : borderBottomWidth();
1526 }
1527
1528 unsigned short RenderStyle::borderEndWidth() const
1529 {
1530     if (isHorizontalWritingMode())
1531         return isLeftToRightDirection() ? borderRightWidth() : borderLeftWidth();
1532     return isLeftToRightDirection() ? borderBottomWidth() : borderTopWidth();
1533 }
1534
1535 void RenderStyle::setMarginStart(Length margin)
1536 {
1537     if (isHorizontalWritingMode()) {
1538         if (isLeftToRightDirection())
1539             setMarginLeft(margin);
1540         else
1541             setMarginRight(margin);
1542     } else {
1543         if (isLeftToRightDirection())
1544             setMarginTop(margin);
1545         else
1546             setMarginBottom(margin);
1547     }
1548 }
1549
1550 void RenderStyle::setMarginEnd(Length margin)
1551 {
1552     if (isHorizontalWritingMode()) {
1553         if (isLeftToRightDirection())
1554             setMarginRight(margin);
1555         else
1556             setMarginLeft(margin);
1557     } else {
1558         if (isLeftToRightDirection())
1559             setMarginBottom(margin);
1560         else
1561             setMarginTop(margin);
1562     }
1563 }
1564
1565 TextEmphasisMark RenderStyle::textEmphasisMark() const
1566 {
1567     TextEmphasisMark mark = static_cast<TextEmphasisMark>(rareInheritedData->textEmphasisMark);
1568     if (mark != TextEmphasisMarkAuto)
1569         return mark;
1570
1571     if (isHorizontalWritingMode())
1572         return TextEmphasisMarkDot;
1573
1574     return TextEmphasisMarkSesame;
1575 }
1576
1577 #if ENABLE(TOUCH_EVENTS)
1578 Color RenderStyle::initialTapHighlightColor()
1579 {
1580     return RenderTheme::tapHighlightColor();
1581 }
1582 #endif
1583
1584 LayoutBoxExtent RenderStyle::imageOutsets(const NinePieceImage& image) const
1585 {
1586     return LayoutBoxExtent(NinePieceImage::computeOutset(image.outset().top(), borderTopWidth()),
1587                            NinePieceImage::computeOutset(image.outset().right(), borderRightWidth()),
1588                            NinePieceImage::computeOutset(image.outset().bottom(), borderBottomWidth()),
1589                            NinePieceImage::computeOutset(image.outset().left(), borderLeftWidth()));
1590 }
1591
1592 void RenderStyle::reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
1593 {
1594     MemoryClassInfo info(memoryObjectInfo, this, MemoryInstrumentation::CSS);
1595     info.addMember(m_box);
1596     info.addMember(visual);
1597     // FIXME: m_background contains RefPtr<StyleImage> that might need to be instrumented.
1598     info.addMember(m_background);
1599     // FIXME: surrond contains some fields e.g. BorderData that might need to be instrumented.
1600     info.addMember(surround);
1601     info.addInstrumentedMember(rareNonInheritedData);
1602     info.addInstrumentedMember(rareInheritedData);
1603     // FIXME: inherited contains StyleImage and Font fields that might need to be instrumented.
1604     info.addMember(inherited);
1605     if (m_cachedPseudoStyles)
1606         info.addVectorPtr(m_cachedPseudoStyles.get());
1607 #if ENABLE(SVG)
1608     info.addMember(m_svgStyle);
1609 #endif
1610 }
1611
1612 } // namespace WebCore