1/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 2004-2005 Allan Sandfeld Jensen (kde@carewolf.com)
4 * Copyright (C) 2006, 2007 Nicholas Shanks (webkit@nickshanks.com)
5 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Apple Inc. All rights reserved.
6 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
7 * Copyright (C) 2007, 2008 Eric Seidel <eric@webkit.org>
8 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
9 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
10 * Copyright (C) Research In Motion Limited 2011. All rights reserved.
11 * Copyright (C) 2012 Google Inc. All rights reserved.
12 *
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Library General Public
15 * License as published by the Free Software Foundation; either
16 * version 2 of the License, or (at your option) any later version.
17 *
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Library General Public License for more details.
22 *
23 * You should have received a copy of the GNU Library General Public License
24 * along with this library; see the file COPYING.LIB. If not, write to
25 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 * Boston, MA 02110-1301, USA.
27 */
28
29#include "config.h"
30#include "StyleResolveForDocument.h"
31
32#include "CSSFontSelector.h"
33#include "ConstantPropertyMap.h"
34#include "Document.h"
35#include "FontCascade.h"
36#include "Frame.h"
37#include "FrameView.h"
38#include "HTMLIFrameElement.h"
39#include "LocaleToScriptMapping.h"
40#include "NodeRenderStyle.h"
41#include "Page.h"
42#include "RenderObject.h"
43#include "RenderStyle.h"
44#include "RenderView.h"
45#include "Settings.h"
46#include "StyleFontSizeFunctions.h"
47#include "StyleResolver.h"
48
49namespace WebCore {
50
51namespace Style {
52
53RenderStyle resolveForDocument(const Document& document)
54{
55 ASSERT(document.hasLivingRenderTree());
56
57 RenderView& renderView = *document.renderView();
58
59 auto documentStyle = RenderStyle::create();
60
61 documentStyle.setDisplay(DisplayType::Block);
62 documentStyle.setRTLOrdering(document.visuallyOrdered() ? Order::Visual : Order::Logical);
63 documentStyle.setZoom(!document.printing() ? renderView.frame().pageZoomFactor() : 1);
64 documentStyle.setPageScaleTransform(renderView.frame().frameScaleFactor());
65 FontCascadeDescription documentFontDescription = documentStyle.fontDescription();
66 documentFontDescription.setLocale(document.contentLanguage());
67 documentStyle.setFontDescription(WTFMove(documentFontDescription));
68
69 // This overrides any -webkit-user-modify inherited from the parent iframe.
70 documentStyle.setUserModify(document.inDesignMode() ? UserModify::ReadWrite : UserModify::ReadOnly);
71#if PLATFORM(IOS_FAMILY)
72 if (document.inDesignMode())
73 documentStyle.setTextSizeAdjust(TextSizeAdjustment(NoTextSizeAdjustment));
74#endif
75
76 Element* docElement = document.documentElement();
77 RenderObject* docElementRenderer = docElement ? docElement->renderer() : nullptr;
78 if (docElementRenderer) {
79 // Use the direction and writing-mode of the body to set the
80 // viewport's direction and writing-mode unless the property is set on the document element.
81 // If there is no body, then use the document element.
82 auto* body = document.bodyOrFrameset();
83 RenderObject* bodyRenderer = body ? body->renderer() : nullptr;
84 if (bodyRenderer && !docElementRenderer->style().hasExplicitlySetWritingMode())
85 documentStyle.setWritingMode(bodyRenderer->style().writingMode());
86 else
87 documentStyle.setWritingMode(docElementRenderer->style().writingMode());
88 if (bodyRenderer && !docElementRenderer->style().hasExplicitlySetDirection())
89 documentStyle.setDirection(bodyRenderer->style().direction());
90 else
91 documentStyle.setDirection(docElementRenderer->style().direction());
92 }
93
94 const Pagination& pagination = renderView.frameView().pagination();
95 if (pagination.mode != Pagination::Unpaginated) {
96 documentStyle.setColumnStylesFromPaginationMode(pagination.mode);
97 documentStyle.setColumnGap(GapLength(Length((int) pagination.gap, Fixed)));
98 if (renderView.multiColumnFlow())
99 renderView.updateColumnProgressionFromStyle(documentStyle);
100 if (renderView.page().paginationLineGridEnabled()) {
101 documentStyle.setLineGrid("-webkit-default-pagination-grid");
102 documentStyle.setLineSnap(LineSnap::Contain);
103 }
104 }
105
106 const Settings& settings = renderView.frame().settings();
107
108 FontCascadeDescription fontDescription;
109 fontDescription.setLocale(document.contentLanguage());
110 fontDescription.setRenderingMode(settings.fontRenderingMode());
111 fontDescription.setOneFamily(standardFamily);
112 fontDescription.setShouldAllowUserInstalledFonts(settings.shouldAllowUserInstalledFonts() ? AllowUserInstalledFonts::Yes : AllowUserInstalledFonts::No);
113
114 fontDescription.setKeywordSizeFromIdentifier(CSSValueMedium);
115 int size = fontSizeForKeyword(CSSValueMedium, false, document);
116 fontDescription.setSpecifiedSize(size);
117 bool useSVGZoomRules = document.isSVGDocument();
118 fontDescription.setComputedSize(computedFontSizeFromSpecifiedSize(size, fontDescription.isAbsoluteSize(), useSVGZoomRules, &documentStyle, document));
119
120 FontOrientation fontOrientation;
121 NonCJKGlyphOrientation glyphOrientation;
122 std::tie(fontOrientation, glyphOrientation) = documentStyle.fontAndGlyphOrientation();
123 fontDescription.setOrientation(fontOrientation);
124 fontDescription.setNonCJKGlyphOrientation(glyphOrientation);
125
126 documentStyle.setFontDescription(WTFMove(fontDescription));
127
128 documentStyle.fontCascade().update(&const_cast<Document&>(document).fontSelector());
129
130 for (auto& it : document.constantProperties().values())
131 documentStyle.setInheritedCustomPropertyValue(it.key, makeRef(it.value.get()));
132
133 return documentStyle;
134}
135
136}
137}
138