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 "StyleFontSizeFunctions.h"
31
32#include "CSSValueKeywords.h"
33#include "Document.h"
34#include "Frame.h"
35#include "RenderStyle.h"
36#include "Settings.h"
37
38namespace WebCore {
39
40namespace Style {
41
42enum class MinimumFontSizeRule {
43 None,
44 Absolute,
45 AbsoluteAndRelative
46};
47
48static float computedFontSizeFromSpecifiedSize(float specifiedSize, bool isAbsoluteSize, float zoomFactor, MinimumFontSizeRule minimumSizeRule, const Settings& settings)
49{
50 // Text with a 0px font size should not be visible and therefore needs to be
51 // exempt from minimum font size rules. Acid3 relies on this for pixel-perfect
52 // rendering. This is also compatible with other browsers that have minimum
53 // font size settings (e.g. Firefox).
54 if (fabsf(specifiedSize) < std::numeric_limits<float>::epsilon())
55 return 0.0f;
56
57 // We support two types of minimum font size. The first is a hard override that applies to
58 // all fonts. This is "minSize." The second type of minimum font size is a "smart minimum"
59 // that is applied only when the Web page can't know what size it really asked for, e.g.,
60 // when it uses logical sizes like "small" or expresses the font-size as a percentage of
61 // the user's default font setting.
62
63 // With the smart minimum, we never want to get smaller than the minimum font size to keep fonts readable.
64 // However we always allow the page to set an explicit pixel size that is smaller,
65 // since sites will mis-render otherwise (e.g., http://www.gamespot.com with a 9px minimum).
66
67 if (minimumSizeRule == MinimumFontSizeRule::None)
68 return specifiedSize;
69
70 int minSize = settings.minimumFontSize();
71 int minLogicalSize = settings.minimumLogicalFontSize();
72 float zoomedSize = specifiedSize * zoomFactor;
73
74 // Apply the hard minimum first. We only apply the hard minimum if after zooming we're still too small.
75 zoomedSize = std::max(zoomedSize, static_cast<float>(minSize));
76
77 // Now apply the smart minimum. This minimum is also only applied if we're still too small
78 // after zooming. The font size must either be relative to the user default or the original size
79 // must have been acceptable. In other words, we only apply the smart minimum whenever we're positive
80 // doing so won't disrupt the layout.
81 if (minimumSizeRule == MinimumFontSizeRule::AbsoluteAndRelative && (specifiedSize >= minLogicalSize || !isAbsoluteSize))
82 zoomedSize = std::max(zoomedSize, static_cast<float>(minLogicalSize));
83
84 // Also clamp to a reasonable maximum to prevent insane font sizes from causing crashes on various
85 // platforms. (I'm looking at you, Windows.)
86 return std::min(maximumAllowedFontSize, zoomedSize);
87}
88
89float computedFontSizeFromSpecifiedSize(float specifiedSize, bool isAbsoluteSize, bool useSVGZoomRules, const RenderStyle* style, const Document& document)
90{
91 float zoomFactor = 1.0f;
92 if (!useSVGZoomRules) {
93 zoomFactor = style->effectiveZoom();
94 Frame* frame = document.frame();
95 if (frame && style->textZoom() != TextZoom::Reset)
96 zoomFactor *= frame->textZoomFactor();
97 }
98 return computedFontSizeFromSpecifiedSize(specifiedSize, isAbsoluteSize, zoomFactor, useSVGZoomRules ? MinimumFontSizeRule::None : MinimumFontSizeRule::AbsoluteAndRelative, document.settings());
99}
100
101float computedFontSizeFromSpecifiedSizeForSVGInlineText(float specifiedSize, bool isAbsoluteSize, float zoomFactor, const Document& document)
102{
103 return computedFontSizeFromSpecifiedSize(specifiedSize, isAbsoluteSize, zoomFactor, MinimumFontSizeRule::Absolute, document.settings());
104}
105
106const int fontSizeTableMax = 16;
107const int fontSizeTableMin = 9;
108const int totalKeywords = 8;
109
110// WinIE/Nav4 table for font sizes. Designed to match the legacy font mapping system of HTML.
111static const int quirksFontSizeTable[fontSizeTableMax - fontSizeTableMin + 1][totalKeywords] =
112{
113 { 9, 9, 9, 9, 11, 14, 18, 28 },
114 { 9, 9, 9, 10, 12, 15, 20, 31 },
115 { 9, 9, 9, 11, 13, 17, 22, 34 },
116 { 9, 9, 10, 12, 14, 18, 24, 37 },
117 { 9, 9, 10, 13, 16, 20, 26, 40 }, // fixed font default (13)
118 { 9, 9, 11, 14, 17, 21, 28, 42 },
119 { 9, 10, 12, 15, 17, 23, 30, 45 },
120 { 9, 10, 13, 16, 18, 24, 32, 48 } // proportional font default (16)
121};
122// HTML 1 2 3 4 5 6 7
123// CSS xxs xs s m l xl xxl
124// |
125// user pref
126
127// Strict mode table matches MacIE and Mozilla's settings exactly.
128static const int strictFontSizeTable[fontSizeTableMax - fontSizeTableMin + 1][totalKeywords] =
129{
130 { 9, 9, 9, 9, 11, 14, 18, 27 },
131 { 9, 9, 9, 10, 12, 15, 20, 30 },
132 { 9, 9, 10, 11, 13, 17, 22, 33 },
133 { 9, 9, 10, 12, 14, 18, 24, 36 },
134 { 9, 10, 12, 13, 16, 20, 26, 39 }, // fixed font default (13)
135 { 9, 10, 12, 14, 17, 21, 28, 42 },
136 { 9, 10, 13, 15, 18, 23, 30, 45 },
137 { 9, 10, 13, 16, 18, 24, 32, 48 } // proportional font default (16)
138};
139// HTML 1 2 3 4 5 6 7
140// CSS xxs xs s m l xl xxl
141// |
142// user pref
143
144// For values outside the range of the table, we use Todd Fahrner's suggested scale
145// factors for each keyword value.
146static const float fontSizeFactors[totalKeywords] = { 0.60f, 0.75f, 0.89f, 1.0f, 1.2f, 1.5f, 2.0f, 3.0f };
147
148float fontSizeForKeyword(unsigned keywordID, bool shouldUseFixedDefaultSize, const Document& document)
149{
150 bool quirksMode = document.inQuirksMode();
151 int mediumSize = shouldUseFixedDefaultSize ? document.settings().defaultFixedFontSize() : document.settings().defaultFontSize();
152 if (mediumSize >= fontSizeTableMin && mediumSize <= fontSizeTableMax) {
153 // Look up the entry in the table.
154 int row = mediumSize - fontSizeTableMin;
155 int col = (keywordID - CSSValueXxSmall);
156 return quirksMode ? quirksFontSizeTable[row][col] : strictFontSizeTable[row][col];
157 }
158
159 // Value is outside the range of the table. Apply the scale factor instead.
160 float minLogicalSize = std::max(document.settings().minimumLogicalFontSize(), 1);
161 return std::max(fontSizeFactors[keywordID - CSSValueXxSmall] * mediumSize, minLogicalSize);
162}
163
164template<typename T>
165static int findNearestLegacyFontSize(int pixelFontSize, const T* table, int multiplier)
166{
167 // Ignore table[0] because xx-small does not correspond to any legacy font size.
168 for (int i = 1; i < totalKeywords - 1; i++) {
169 if (pixelFontSize * 2 < (table[i] + table[i + 1]) * multiplier)
170 return i;
171 }
172 return totalKeywords - 1;
173}
174
175int legacyFontSizeForPixelSize(int pixelFontSize, bool shouldUseFixedDefaultSize, const Document& document)
176{
177 bool quirksMode = document.inQuirksMode();
178 int mediumSize = shouldUseFixedDefaultSize ? document.settings().defaultFixedFontSize() : document.settings().defaultFontSize();
179 if (mediumSize >= fontSizeTableMin && mediumSize <= fontSizeTableMax) {
180 int row = mediumSize - fontSizeTableMin;
181 return findNearestLegacyFontSize<int>(pixelFontSize, quirksMode ? quirksFontSizeTable[row] : strictFontSizeTable[row], 1);
182 }
183
184 return findNearestLegacyFontSize<float>(pixelFontSize, fontSizeFactors, mediumSize);
185}
186
187}
188}
189