OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2007, 2008, 2011 Apple Inc. All rights reserved. | 2 * Copyright (C) 2007, 2008, 2011 Apple Inc. All rights reserved. |
3 * Copyright (C) 2015 Google Inc. All rights reserved. | 3 * Copyright (C) 2015 Google Inc. All rights reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
7 * are met: | 7 * are met: |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 24 matching lines...) Expand all Loading... |
35 | 35 |
36 static inline unsigned stretchDistanceToDesired(FontTraits desired, | 36 static inline unsigned stretchDistanceToDesired(FontTraits desired, |
37 FontTraits candidate) { | 37 FontTraits candidate) { |
38 return abs(static_cast<int>(desired.stretch() - candidate.stretch())); | 38 return abs(static_cast<int>(desired.stretch() - candidate.stretch())); |
39 } | 39 } |
40 | 40 |
41 static inline unsigned styleScore(FontTraits desired, FontTraits candidate) { | 41 static inline unsigned styleScore(FontTraits desired, FontTraits candidate) { |
42 static_assert(FontStyleNormal == 0 && FontStyleItalic == 2, | 42 static_assert(FontStyleNormal == 0 && FontStyleItalic == 2, |
43 "Enumeration values need to match lookup table."); | 43 "Enumeration values need to match lookup table."); |
44 unsigned styleScoreLookupTable[][FontStyleItalic + 1] = { | 44 unsigned styleScoreLookupTable[][FontStyleItalic + 1] = { |
45 // "If the value is normal, normal faces are checked first, then oblique f
aces, then italic faces." | 45 // "If the value is normal, normal faces are checked first, then oblique |
| 46 // faces, then italic faces." |
46 // i.e. normal has the highest score, then oblique, then italic. | 47 // i.e. normal has the highest score, then oblique, then italic. |
47 {2, 1, 0}, | 48 {2, 1, 0}, |
48 // "If the value is oblique, oblique faces are checked first, then italic
faces and then normal faces." | 49 // "If the value is oblique, oblique faces are checked first, then italic |
49 // i.e. normal gets the lowest score, oblique gets the highest, italic sec
ond best. | 50 // faces and then normal faces." |
| 51 // i.e. normal gets the lowest score, oblique gets the highest, italic |
| 52 // second best. |
50 {0, 2, 1}, | 53 {0, 2, 1}, |
51 // "If the value of font-style is italic, italic faces are checked first,
then oblique, then normal faces" | 54 // "If the value of font-style is italic, italic faces are checked first, |
52 // i.e. normal gets the lowest score, oblique is second best, italic highe
st. | 55 // then oblique, then normal faces" |
| 56 // i.e. normal gets the lowest score, oblique is second best, italic |
| 57 // highest. |
53 {0, 1, 2}}; | 58 {0, 1, 2}}; |
54 | 59 |
55 ASSERT_WITH_SECURITY_IMPLICATION(desired.style() < FontStyleItalic + 1); | 60 ASSERT_WITH_SECURITY_IMPLICATION(desired.style() < FontStyleItalic + 1); |
56 ASSERT_WITH_SECURITY_IMPLICATION(candidate.style() < FontStyleItalic + 1); | 61 ASSERT_WITH_SECURITY_IMPLICATION(candidate.style() < FontStyleItalic + 1); |
57 | 62 |
58 return styleScoreLookupTable[desired.style()][candidate.style()]; | 63 return styleScoreLookupTable[desired.style()][candidate.style()]; |
59 } | 64 } |
60 | 65 |
61 static inline unsigned weightScore(FontTraits desired, FontTraits candidate) { | 66 static inline unsigned weightScore(FontTraits desired, FontTraits candidate) { |
62 static_assert(FontWeight100 == 0 && FontWeight900 - FontWeight100 == 8, | 67 static_assert(FontWeight100 == 0 && FontWeight900 - FontWeight100 == 8, |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 ASSERT_WITH_SECURITY_IMPLICATION(candidateScoreLookup < scoreLookupSize); | 100 ASSERT_WITH_SECURITY_IMPLICATION(candidateScoreLookup < scoreLookupSize); |
96 | 101 |
97 return weightScoreLookup[desiredScoresLookup][candidateScoreLookup]; | 102 return weightScoreLookup[desiredScoresLookup][candidateScoreLookup]; |
98 } | 103 } |
99 | 104 |
100 bool FontStyleMatcher::isCandidateBetter(CSSSegmentedFontFace* candidate, | 105 bool FontStyleMatcher::isCandidateBetter(CSSSegmentedFontFace* candidate, |
101 CSSSegmentedFontFace* current) { | 106 CSSSegmentedFontFace* current) { |
102 const FontTraits& candidateTraits = candidate->traits(); | 107 const FontTraits& candidateTraits = candidate->traits(); |
103 const FontTraits& currentTraits = current->traits(); | 108 const FontTraits& currentTraits = current->traits(); |
104 | 109 |
105 // According to CSS3 Fonts Font Style matching, there is a precedence for matc
hing: | 110 // According to CSS3 Fonts Font Style matching, there is a precedence for |
| 111 // matching: |
106 // A better stretch match wins over a better style match, a better style match | 112 // A better stretch match wins over a better style match, a better style match |
107 // wins over a better weight match, where "better" means closer to the desired | 113 // wins over a better weight match, where "better" means closer to the desired |
108 // traits. | 114 // traits. |
109 int stretchComparison = 0, styleComparison = 0, weightComparison = 0; | 115 int stretchComparison = 0, styleComparison = 0, weightComparison = 0; |
110 | 116 |
111 stretchComparison = stretchDistanceToDesired(m_fontTraits, candidateTraits) - | 117 stretchComparison = stretchDistanceToDesired(m_fontTraits, candidateTraits) - |
112 stretchDistanceToDesired(m_fontTraits, currentTraits); | 118 stretchDistanceToDesired(m_fontTraits, currentTraits); |
113 | 119 |
114 if (stretchComparison > 0) | 120 if (stretchComparison > 0) |
115 return false; | 121 return false; |
(...skipping 11 matching lines...) Expand all Loading... |
127 weightComparison = weightScore(m_fontTraits, candidateTraits) - | 133 weightComparison = weightScore(m_fontTraits, candidateTraits) - |
128 weightScore(m_fontTraits, currentTraits); | 134 weightScore(m_fontTraits, currentTraits); |
129 | 135 |
130 if (weightComparison > 0) | 136 if (weightComparison > 0) |
131 return true; | 137 return true; |
132 | 138 |
133 return false; | 139 return false; |
134 } | 140 } |
135 | 141 |
136 } // namespace blink | 142 } // namespace blink |
OLD | NEW |