Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(182)

Side by Side Diff: Source/core/css/FontStyleMatcher.cpp

Issue 1270723002: Improve CSS Style matching spec compliance (Closed) Base URL: git@github.com:drott/blink-crosswalk.git@reimplementMatching
Patch Set: stdlib instead of MathExtras Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2007, 2008, 2011 Apple Inc. All rights reserved.
3 * Copyright (C) 2015 Google Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27
28 #include "config.h"
29 #include "core/css/FontStyleMatcher.h"
30
31 #include "core/css/CSSSegmentedFontFace.h"
32 #include "wtf/Assertions.h"
33
34 #include <stdlib.h>
35
36 namespace blink {
37
38 static inline unsigned stretchDistanceToDesired(FontTraits desired, FontTraits c andidate)
39 {
40 return abs(static_cast<int>(desired.stretch() - candidate.stretch()));
41 }
42
43 static inline unsigned styleScore(FontTraits desired, FontTraits candidate)
44 {
45 static_assert(FontStyleNormal == 0 && FontStyleItalic == 2,
46 "Enumeration values need to match lookup table.");
47 unsigned styleScoreLookupTable[][FontStyleItalic + 1] = {
48 // "If the value is normal, normal faces are checked first, then oblique faces, then italic faces."
49 // i.e. normal has the highest score, then oblique, then italic.
50 { 2, 1, 0 },
51 // "If the value is oblique, oblique faces are checked first, then itali c faces and then normal faces."
52 // i.e. normal gets the lowest score, oblique gets the highest, italic s econd best.
53 { 0, 2, 1 },
54 // "If the value of font-style is italic, italic faces are checked first , then oblique, then normal faces"
55 // i.e. normal gets the lowest score, oblique is second best, italic hig hest.
56 { 0, 1, 2 }
57 };
58
59 ASSERT_WITH_SECURITY_IMPLICATION(desired.style() < FontStyleItalic + 1);
60 ASSERT_WITH_SECURITY_IMPLICATION(candidate.style() < FontStyleItalic + 1);
61
62 return styleScoreLookupTable[desired.style()][candidate.style()];
63 }
64
65 static inline unsigned weightScore(FontTraits desired, FontTraits candidate)
66 {
67 static_assert(FontWeight100 == 0 && FontWeight900 - FontWeight100 == 8,
68 "Enumeration values need to match lookup table.");
69 static const unsigned scoreLookupSize = FontWeight900 + 1;
70 // https://drafts.csswg.org/css-fonts/#font-style-matching
71 // "..if the desired weight is available that face matches. "
72 static const unsigned weightScoreLookup[scoreLookupSize][scoreLookupSize] = {
73 // "If the desired weight is less than 400, weights below the desired
74 // weight are checked in descending order followed by weights above the
75 // desired weight in ascending order until a match is found."
76 { 9, 8, 7, 6, 5, 4, 3, 2, 1 }, // FontWeight100 desired
77 { 8, 9, 7, 6, 5, 4, 3, 2, 1 }, // FontWeight200 desired
78 { 7, 8, 9, 6, 5, 4, 3, 2, 1 }, // FontWeight300 desired
79
80 // "If the desired weight is 400, 500 is checked first and then the rule
81 // for desired weights less than 400 is used."
82 { 5, 6, 7, 9, 8, 4, 3, 2, 1 }, // FontWeight400 desired
83
84 // "If the desired weight is 500, 400 is checked first and then the rule
85 // for desired weights less than 400 is used."
86 { 5, 6, 7, 8, 9, 4, 3, 2, 1 }, // FontWeight500 desired
87
88 // "If the desired weight is greater than 500, weights above the desired
89 // weight are checked in ascending order followed by weights below the
90 // desired weight in descending order until a match is found."
91 { 1, 2, 3, 4, 5, 9, 8, 7, 6 }, // FontWeight600 desired
92 { 1, 2, 3, 4, 5, 6, 9, 8, 7 }, // FontWeight700 desired
93 { 1, 2, 3, 4, 5, 6, 7, 9, 8 }, // FontWeight800 desired
94 { 1, 2, 3, 4, 5, 6, 7, 8, 9 } // FontWeight900 desired
95 };
96
97 unsigned desiredScoresLookup = static_cast<unsigned>(desired.weight());
98 unsigned candidateScoreLookup = static_cast<unsigned>(candidate.weight());
99 ASSERT_WITH_SECURITY_IMPLICATION(desiredScoresLookup < scoreLookupSize);
100 ASSERT_WITH_SECURITY_IMPLICATION(candidateScoreLookup < scoreLookupSize);
101
102 return weightScoreLookup[desiredScoresLookup][candidateScoreLookup];
103 }
104
105 bool FontStyleMatcher::isCandidateBetter(CSSSegmentedFontFace *candidate, CSSSeg mentedFontFace *current)
106 {
107 const FontTraits& candidateTraits = candidate->traits();
108 const FontTraits& currentTraits = current->traits();
109
110 // According to CSS3 Fonts Font Style matching, there is a precedence for ma tching:
111 // A better stretch match wins over a better style match, a better style mat ch
112 // wins over a better weight match, where "better" means closer to the desir ed
113 // traits.
114 int stretchComparison = 0, styleComparison = 0, weightComparison = 0;
115
116 stretchComparison = stretchDistanceToDesired(m_fontTraits, candidateTraits) -
117 stretchDistanceToDesired(m_fontTraits, currentTraits);
118
119 if (stretchComparison > 0)
120 return false;
121 if (stretchComparison < 0)
122 return true;
123
124 styleComparison = styleScore(m_fontTraits, candidateTraits) -
125 styleScore(m_fontTraits, currentTraits);
126
127 if (styleComparison > 0)
128 return true;
129 if (styleComparison < 0)
130 return false;
131
132 weightComparison = weightScore(m_fontTraits, candidateTraits) -
133 weightScore(m_fontTraits, currentTraits);
134
135 if (weightComparison > 0)
136 return true;
137
138 return false;
139 }
140
141 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698