Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "core/css/properties/CSSShorthandPropertyAPIFont.h" | |
| 6 | |
| 7 #include "core/css/CSSFontFamilyValue.h" | |
| 8 #include "core/css/CSSIdentifierValue.h" | |
| 9 #include "core/css/CSSPrimitiveValueMappings.h" | |
| 10 #include "core/css/parser/CSSParserContext.h" | |
| 11 #include "core/css/parser/CSSParserFastPaths.h" | |
| 12 #include "core/css/parser/CSSPropertyParserHelpers.h" | |
| 13 #include "core/css/properties/CSSPropertyFontUtils.h" | |
| 14 #include "platform/fonts/FontTraits.h" | |
| 15 | |
| 16 namespace blink { | |
| 17 | |
| 18 class CSSParserLocalContext; | |
|
Bugs Nash
2017/06/16 03:51:51
not necessary as guaranteed to be in header file
Jia
2017/06/19 03:54:41
Done.
| |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 bool ConsumeSystemFont(bool important, | |
| 23 CSSParserTokenRange& range, | |
| 24 HeapVector<CSSProperty, 256>& properties) { | |
| 25 CSSValueID system_font_id = range.ConsumeIncludingWhitespace().Id(); | |
| 26 DCHECK_GE(system_font_id, CSSValueCaption); | |
| 27 DCHECK_LE(system_font_id, CSSValueStatusBar); | |
| 28 if (!range.AtEnd()) | |
| 29 return false; | |
| 30 | |
| 31 FontStyle font_style = kFontStyleNormal; | |
| 32 FontWeight font_weight = kFontWeightNormal; | |
| 33 float font_size = 0; | |
| 34 AtomicString font_family; | |
| 35 LayoutTheme::GetTheme().SystemFont(system_font_id, font_style, font_weight, | |
| 36 font_size, font_family); | |
| 37 | |
| 38 CSSPropertyParserHelpers::AddProperty( | |
| 39 CSSPropertyFontStyle, CSSPropertyFont, | |
| 40 *CSSIdentifierValue::Create( | |
| 41 font_style == kFontStyleItalic ? CSSValueItalic : CSSValueNormal), | |
| 42 important, false /* implicit */, properties); | |
|
Bugs Nash
2017/06/16 03:19:01
avoid passing literal bools. I would either change
Jia
2017/06/19 03:54:41
I can change the signature of AddProperty and repl
Bugs Nash
2017/06/19 04:29:18
I wouldn't worry about changing the important bool
| |
| 43 CSSPropertyParserHelpers::AddProperty( | |
| 44 CSSPropertyFontWeight, CSSPropertyFont, | |
| 45 *CSSIdentifierValue::Create(font_weight), important, false /* implicit */, | |
| 46 properties); | |
| 47 CSSPropertyParserHelpers::AddProperty( | |
| 48 CSSPropertyFontSize, CSSPropertyFont, | |
| 49 *CSSPrimitiveValue::Create(font_size, | |
| 50 CSSPrimitiveValue::UnitType::kPixels), | |
| 51 important, false /* implicit */, properties); | |
| 52 | |
| 53 CSSValueList* font_family_list = CSSValueList::CreateCommaSeparated(); | |
| 54 font_family_list->Append(*CSSFontFamilyValue::Create(font_family)); | |
| 55 CSSPropertyParserHelpers::AddProperty(CSSPropertyFontFamily, CSSPropertyFont, | |
| 56 *font_family_list, important, | |
| 57 false /* implicit */, properties); | |
| 58 | |
| 59 CSSPropertyParserHelpers::AddProperty( | |
| 60 CSSPropertyFontStretch, CSSPropertyFont, | |
| 61 *CSSIdentifierValue::Create(CSSValueNormal), important, | |
| 62 false /* implicit */, properties); | |
| 63 CSSPropertyParserHelpers::AddProperty( | |
| 64 CSSPropertyFontVariantCaps, CSSPropertyFont, | |
| 65 *CSSIdentifierValue::Create(CSSValueNormal), important, | |
| 66 false /* implicit */, properties); | |
| 67 CSSPropertyParserHelpers::AddProperty( | |
| 68 CSSPropertyFontVariantLigatures, CSSPropertyFont, | |
| 69 *CSSIdentifierValue::Create(CSSValueNormal), important, | |
| 70 false /* implicit */, properties); | |
| 71 CSSPropertyParserHelpers::AddProperty( | |
| 72 CSSPropertyFontVariantNumeric, CSSPropertyFont, | |
| 73 *CSSIdentifierValue::Create(CSSValueNormal), important, | |
| 74 false /* implicit */, properties); | |
| 75 CSSPropertyParserHelpers::AddProperty( | |
| 76 CSSPropertyLineHeight, CSSPropertyFont, | |
| 77 *CSSIdentifierValue::Create(CSSValueNormal), important, | |
| 78 false /* implicit */, properties); | |
| 79 return true; | |
| 80 } | |
| 81 | |
| 82 bool ConsumeFont(bool important, | |
| 83 CSSParserTokenRange& range, | |
| 84 const CSSParserContext* context, | |
| 85 HeapVector<CSSProperty, 256>& properties) { | |
| 86 DCHECK(context); | |
| 87 | |
| 88 // Let's check if there is an inherit or initial somewhere in the shorthand. | |
| 89 CSSParserTokenRange range_copy = range; | |
| 90 while (!range_copy.AtEnd()) { | |
| 91 CSSValueID id = range_copy.ConsumeIncludingWhitespace().Id(); | |
| 92 if (id == CSSValueInherit || id == CSSValueInitial) | |
| 93 return false; | |
| 94 } | |
| 95 // Optional font-style, font-variant, font-stretch and font-weight. | |
| 96 CSSIdentifierValue* font_style = nullptr; | |
| 97 CSSIdentifierValue* font_variant_caps = nullptr; | |
| 98 CSSIdentifierValue* font_weight = nullptr; | |
| 99 CSSIdentifierValue* font_stretch = nullptr; | |
| 100 while (!range.AtEnd()) { | |
| 101 CSSValueID id = range.Peek().Id(); | |
| 102 if (!font_style && CSSParserFastPaths::IsValidKeywordPropertyAndValue( | |
| 103 CSSPropertyFontStyle, id, context->Mode())) { | |
| 104 font_style = CSSPropertyParserHelpers::ConsumeIdent(range); | |
| 105 continue; | |
| 106 } | |
| 107 if (!font_variant_caps && | |
| 108 (id == CSSValueNormal || id == CSSValueSmallCaps)) { | |
| 109 // Font variant in the shorthand is particular, it only accepts normal or | |
| 110 // small-caps. | |
| 111 // See https://drafts.csswg.org/css-fonts/#propdef-font | |
| 112 font_variant_caps = CSSPropertyFontUtils::ConsumeFontVariantCSS21(range); | |
| 113 if (font_variant_caps) | |
| 114 continue; | |
| 115 } | |
| 116 if (!font_weight) { | |
| 117 font_weight = CSSPropertyFontUtils::ConsumeFontWeight(range); | |
| 118 if (font_weight) | |
| 119 continue; | |
| 120 } | |
| 121 if (!font_stretch && CSSParserFastPaths::IsValidKeywordPropertyAndValue( | |
| 122 CSSPropertyFontStretch, id, context->Mode())) | |
| 123 font_stretch = CSSPropertyParserHelpers::ConsumeIdent(range); | |
| 124 else | |
| 125 break; | |
| 126 } | |
| 127 | |
| 128 if (range.AtEnd()) | |
| 129 return false; | |
| 130 | |
| 131 CSSPropertyParserHelpers::AddProperty( | |
| 132 CSSPropertyFontStyle, CSSPropertyFont, | |
| 133 font_style ? *font_style : *CSSIdentifierValue::Create(CSSValueNormal), | |
| 134 important, false /* implicit */, properties); | |
| 135 CSSPropertyParserHelpers::AddProperty( | |
| 136 CSSPropertyFontVariantCaps, CSSPropertyFont, | |
| 137 font_variant_caps ? *font_variant_caps | |
| 138 : *CSSIdentifierValue::Create(CSSValueNormal), | |
| 139 important, false /* implicit */, properties); | |
| 140 CSSPropertyParserHelpers::AddProperty( | |
| 141 CSSPropertyFontVariantLigatures, CSSPropertyFont, | |
| 142 *CSSIdentifierValue::Create(CSSValueNormal), important, | |
| 143 false /* implicit */, properties); | |
| 144 CSSPropertyParserHelpers::AddProperty( | |
| 145 CSSPropertyFontVariantNumeric, CSSPropertyFont, | |
| 146 *CSSIdentifierValue::Create(CSSValueNormal), important, | |
| 147 false /* implicit */, properties); | |
| 148 | |
| 149 CSSPropertyParserHelpers::AddProperty( | |
| 150 CSSPropertyFontWeight, CSSPropertyFont, | |
| 151 font_weight ? *font_weight : *CSSIdentifierValue::Create(CSSValueNormal), | |
| 152 important, false /* implicit */, properties); | |
| 153 CSSPropertyParserHelpers::AddProperty( | |
| 154 CSSPropertyFontStretch, CSSPropertyFont, | |
| 155 font_stretch ? *font_stretch | |
| 156 : *CSSIdentifierValue::Create(CSSValueNormal), | |
| 157 important, false /* implicit */, properties); | |
| 158 | |
| 159 // Now a font size _must_ come. | |
| 160 CSSValue* font_size = | |
| 161 CSSPropertyFontUtils::ConsumeFontSize(range, context->Mode()); | |
| 162 if (!font_size || range.AtEnd()) | |
| 163 return false; | |
| 164 | |
| 165 CSSPropertyParserHelpers::AddProperty(CSSPropertyFontSize, CSSPropertyFont, | |
| 166 *font_size, important, | |
| 167 false /* implicit */, properties); | |
| 168 | |
| 169 if (CSSPropertyParserHelpers::ConsumeSlashIncludingWhitespace(range)) { | |
| 170 CSSValue* line_height = | |
| 171 CSSPropertyFontUtils::ConsumeLineHeight(range, context->Mode()); | |
| 172 if (!line_height) | |
| 173 return false; | |
| 174 CSSPropertyParserHelpers::AddProperty( | |
| 175 CSSPropertyLineHeight, CSSPropertyFont, *line_height, important, | |
| 176 false /* implicit */, properties); | |
| 177 } else { | |
| 178 CSSPropertyParserHelpers::AddProperty( | |
| 179 CSSPropertyLineHeight, CSSPropertyFont, | |
| 180 *CSSIdentifierValue::Create(CSSValueNormal), important, | |
| 181 false /* implicit */, properties); | |
| 182 } | |
| 183 | |
| 184 // Font family must come now. | |
| 185 CSSValue* parsed_family_value = | |
| 186 CSSPropertyFontUtils::ConsumeFontFamily(range); | |
| 187 if (!parsed_family_value) | |
| 188 return false; | |
| 189 | |
| 190 CSSPropertyParserHelpers::AddProperty(CSSPropertyFontFamily, CSSPropertyFont, | |
| 191 *parsed_family_value, important, | |
| 192 false /* implicit */, properties); | |
| 193 | |
| 194 // FIXME: http://www.w3.org/TR/2011/WD-css3-fonts-20110324/#font-prop requires | |
| 195 // that "font-stretch", "font-size-adjust", and "font-kerning" be reset to | |
| 196 // their initial values but we don't seem to support them at the moment. They | |
| 197 // should also be added here once implemented. | |
| 198 return range.AtEnd(); | |
| 199 } | |
| 200 | |
| 201 } // namespace | |
| 202 | |
| 203 bool CSSShorthandPropertyAPIFont::parseShorthand( | |
| 204 bool important, | |
| 205 CSSParserTokenRange& range, | |
| 206 const CSSParserContext* context, | |
| 207 const CSSParserLocalContext&, | |
| 208 HeapVector<CSSProperty, 256>& properties) { | |
| 209 const CSSParserToken& token = range.Peek(); | |
| 210 if (token.Id() >= CSSValueCaption && token.Id() <= CSSValueStatusBar) | |
| 211 return ConsumeSystemFont(important, range, properties); | |
| 212 return ConsumeFont(important, range, context, properties); | |
| 213 } | |
| 214 } // namespace blink | |
| OLD | NEW |