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