Expand font-weight and font-stretch to take any number
https://bugs.webkit.org/show_bug.cgi?id=168889
Reviewed by Simon Fraser.
Source/WebCore:
This patch expands parsing support for font-weight and font-stretch so that they can accept
numbers in addition to the keyword values they accepted previously. Eventually, these numbers
will be used as inputs to select variation fonts; however, in this patch, the values are just
parsed and held in our internal data model. This means that this patch doesn't actually change
the general behavior of WebKit; instead, it parses new values, outputs them (via
getComputedStyle() and similar functions), and provides them as input to our preexisting font
selection algorithm.
This patch doesn't actually modify anything algorithmically, and therefore, it doesn't include
any additional tests for the font selection algorithm. Instead, it tests the parsing of the
newly accepted values (both input and output).
Because our internal data model needs to hold numbers now, this patch also completely eliminates
the FontWidth, FontItalic, and FontTraitsMask enum values in favor of FontSelectionValues (which
can hold numbers instead of individual enum values). Therefore, every place which we were using
these enum values is migrated in this patch to using FontSelectionValues instead.
Tests: fast/text/font-selection-font-face-parse.html
fast/text/font-selection-font-loading-api-parse.html
fast/text/font-style-parse.html
fast/text/font-weight-parse.html
* accessibility/AccessibilityRenderObject.cpp: Migrate off of old enum types.
(WebCore::AccessibilityRenderObject::hasBoldFont):
(WebCore::AccessibilityRenderObject::hasItalicFont):
(WebCore::AccessibilityRenderObject::hasPlainText):
* accessibility/atk/WebKitAccessibleInterfaceText.cpp: Ditto.
(getAttributeSetForAccessibilityObject):
* css/CSSComputedStyleDeclaration.cpp: Output newly parsed values.
(WebCore::fontWeightFromStyle):
(WebCore::fontStretchFromStyle):
(WebCore::fontStyleFromStyle):
(WebCore::fontShorthandValueForSelectionProperties):
(WebCore::ComputedStyleExtractor::propertyValue):
* css/CSSFontFace.cpp: Migrate off old enum types.
(WebCore::calculateWeightRange):
(WebCore::CSSFontFace::setWeight):
(WebCore::calculateStretchRange):
(WebCore::CSSFontFace::setStretch):
(WebCore::calculateStyleRange):
(WebCore::CSSFontFace::setStyle):
(WebCore::CSSFontFace::calculateStyleMask): Deleted.
(WebCore::CSSFontFace::calculateWeightMask): Deleted.
(WebCore::CSSFontFace::calculateStretch): Deleted.
* css/CSSFontFace.h:
* css/CSSFontFaceSet.cpp: Migrate off old enum types.
(WebCore::CSSFontFaceSet::ensureLocalFontFacesForFamilyRegistered):
(WebCore::calculateWeightValue):
(WebCore::calculateStretchValue):
(WebCore::calculateStyleValue):
(WebCore::computeFontSelectionRequest):
(WebCore::CSSFontFaceSet::matchingFaces):
(WebCore::CSSFontFaceSet::fontFace):
(WebCore::computeFontTraitsMask): Deleted.
(WebCore::computeFontStretch): Deleted.
* css/CSSFontFaceSet.h:
* css/CSSFontSelector.cpp: Migrate off old enum types.
(WebCore::CSSFontSelector::fontRangesForFamily):
* css/CSSPrimitiveValueMappings.h: font-weight and font-style are no longer
keyword properties.
(WebCore::CSSPrimitiveValue::operator FontWeight): Deleted.
(WebCore::CSSPrimitiveValue::operator FontItalic): Deleted.
* css/CSSProperties.json: Use the same infrastructure for all three of the
font selection properties.
* css/CSSSegmentedFontFace.cpp: Migrate off old enum types.
(WebCore::CSSSegmentedFontFace::fontRanges):
* css/CSSValueKeywords.in: The 100-based font-weight values are now represented
as real numbers.
* css/FontFace.cpp: Update the string output functions for the new values.
(WebCore::rangeIsSingleValue):
(WebCore::FontFace::style):
(WebCore::FontFace::weight):
(WebCore::FontFace::stretch):
* css/StyleBuilderConverter.h: Use CSSValue conversion functions to convert
to our new data model.
(WebCore::StyleBuilderConverter::convertFontWeight):
(WebCore::StyleBuilderConverter::convertFontStretch):
(WebCore::StyleBuilderConverter::convertFontStyle):
* css/StyleBuilderCustom.h: font-weight is no longer a completely custom
property.
(WebCore::StyleBuilderCustom::applyInitialFontWeight): Deleted.
(WebCore::StyleBuilderCustom::applyInheritFontWeight): Deleted.
(WebCore::StyleBuilderCustom::applyValueFontWeight): Deleted.
* css/parser/CSSParserFastPaths.cpp: font-style is no longer a keyword property.
(WebCore::CSSParserFastPaths::isValidKeywordPropertyAndValue):
(WebCore::CSSParserFastPaths::isKeywordPropertyID):
* css/parser/CSSPropertyParser.cpp: Parse the new values in the font selection
properties.
(WebCore::consumeFontWeightKeywordValue):
(WebCore::consumeFontWeightCSS21):
(WebCore::consumeFontWeight):
(WebCore::consumeFontStyleKeywordValue):
(WebCore::consumeFontStyle):
(WebCore::CSSPropertyParser::parseSingleValue):
(WebCore::CSSPropertyParser::parseFontFaceDescriptor):
(WebCore::CSSPropertyParser::consumeSystemFont):
(WebCore::CSSPropertyParser::consumeFont):
* editing/EditingStyle.cpp: Migrate off old enum types.
(WebCore::fontWeightIsBold):
* editing/mac/EditorMac.mm: Ditto.
(WebCore::Editor::applyFontStyles):
* platform/graphics/FontCache.h: Unify our font selection values into
their enclosing struct, FontSelectionCapabilities. Also, clean up utility
functions.
* platform/graphics/FontCascade.cpp: Migrate off old enum types.
(WebCore::FontCascade::FontCascade):
* platform/graphics/FontCascade.h: Ditto.
(WebCore::FontCascade::italic):
(WebCore::FontCascade::weight):
* platform/graphics/FontDescription.cpp: Ditto.
(WebCore::FontDescription::FontDescription):
(WebCore::FontCascadeDescription::lighterWeight):
(WebCore::FontCascadeDescription::bolderWeight):
(WebCore::FontDescription::traitsMask): Deleted.
* platform/graphics/FontDescription.h: Migrate off old enum types.
(WebCore::FontDescription::italic):
(WebCore::FontDescription::stretch):
(WebCore::FontDescription::weight):
(WebCore::FontDescription::fontSelectionRequest):
(WebCore::FontDescription::computedPixelSize):
(WebCore::FontDescription::setItalic):
(WebCore::FontDescription::setStretch):
(WebCore::FontDescription::setIsItalic):
(WebCore::FontDescription::setWeight):
(WebCore::FontDescription::operator==):
(WebCore::FontCascadeDescription::lighterWeight):
(WebCore::FontCascadeDescription::bolderWeight):
(WebCore::FontCascadeDescription::initialItalic):
(WebCore::FontCascadeDescription::initialWeight):
(WebCore::FontCascadeDescription::initialStretch):
* platform/graphics/FontSelectionAlgorithm.cpp: Delete code which was only
present to convert from the old enum types to the new FontSelectionRequest
type.
(WebCore::fontSelectionRequestForTraitsMask): Deleted.
(WebCore::initialFontSelectionCapabilitiesForTraitsMask): Deleted.
(WebCore::fontSelectionCapabilitiesForTraitsMask): Deleted.
* platform/graphics/FontSelectionAlgorithm.h: Migrate to new named functions
for special values.
(WebCore::isItalic):
(WebCore::boldWeightValue):
(WebCore::normalWeightValue):
(WebCore::isFontWeightBold):
(WebCore::normalStretchValue):
(WebCore::FontSelectionRequest::FontSelectionRequest):
* platform/graphics/cocoa/FontCacheCoreText.cpp: Migrate all platforms which
compile this file to know about FontDatabase. This simplifies the interaction
between @font-face fonts and installed fonts. Also, migrate off old enum types.
(WebCore::stretchFromCoreTextTraits):
(WebCore::fontWeightFromCoreText):
(WebCore::capabilitiesForFontDescriptor):
(WebCore::FontCache::getFontSelectionCapabilitiesInFamily):
(WebCore::platformFontLookupWithFamily):
(WebCore::invalidateFontCache):
(WebCore::fontWithFamily):
(WebCore::FontCache::createFontPlatformData):
(WebCore::lookupFallbackFont):
(WebCore::toTraitsMask): Deleted.
(WebCore::isFontWeightBold): Deleted.
(WebCore::toCoreTextFontWeight): Deleted.
(): Deleted.
(WebCore::FontDatabase::capabilitiesForFontDescriptor): Deleted.
(WebCore::calculateFontSelectionRequest): Deleted.
* platform/graphics/freetype/FontCacheFreeType.cpp: Migrate off old enum types.
(WebCore::FontCache::getFontSelectionCapabilitiesInFamily):
(WebCore::fontWeightToFontconfigWeight):
(): Deleted.
* platform/graphics/freetype/FontPlatformDataFreeType.cpp: Ditto.
(WebCore::FontPlatformData::FontPlatformData):
* platform/graphics/ios/FontCacheIOS.mm: Ditto.
(WebCore::baseSystemFontDescriptor):
(WebCore::systemFontModificationAttributes):
(WebCore::systemFontDescriptor):
(WebCore::platformFontWithFamilySpecialCase):
(WebCore::FontCache::weightOfCTFont): Deleted.
* platform/graphics/mac/FontCacheMac.mm: Ditto.
(WebCore::toNSFontWeight):
(WebCore::platformFontWithFamilySpecialCase):
* platform/graphics/win/FontCacheWin.cpp: Ditto.
(WebCore::toGDIFontWeight):
(WebCore::traitsInFamilyEnumProc):
(WebCore::FontCache::getFontSelectionCapabilitiesInFamily):
(): Deleted.
* platform/mock/MockRealtimeVideoSource.cpp: Ditto.
(WebCore::MockRealtimeVideoSource::applySize):
* platform/text/TextFlags.h: Delete old enum types.
* platform/win/DragImageWin.cpp: Migrate off old enum types.
(WebCore::dragLabelFont):
* rendering/RenderEmbeddedObject.cpp: Ditto.
(WebCore::RenderEmbeddedObject::getReplacementTextGeometry):
* rendering/RenderThemeGtk.cpp: Ditto.
(WebCore::RenderThemeGtk::updateCachedSystemFontDescription):
* rendering/RenderThemeIOS.mm: Ditto.
(WebCore::RenderThemeIOS::updateCachedSystemFontDescription):
* rendering/RenderThemeMac.mm: Ditto.
(WebCore::toFontWeight):
* rendering/RenderThemeWin.cpp: Ditto.
(WebCore::fillFontDescription):
Source/WebKit/mac:
Migrate off old enum types.
* WebCoreSupport/PopupMenuMac.mm:
(PopupMenuMac::populate):
* WebView/WebHTMLView.mm:
(fontNameForDescription):
* WebView/WebView.mm:
(-[WebView updateTextTouchBar]):
Source/WebKit/win:
Migrate off old enum types.
* WebKitGraphics.cpp:
(makeFont):
Source/WebKit2:
Migrate off old enum types.
* WebProcess/WebPage/WebPage.cpp:
(WebKit::WebPage::editorState):
Tools:
Migrate off old enum types.
* TestWebKitAPI/Tests/WebCore/FontCache.cpp:
(TestWebKitAPI::createPlatformFont):
(TestWebKitAPI::compareFonts):
(TestWebKitAPI::TEST_F):
LayoutTests:
* fast/text/font-selection-font-face-parse-expected.txt: Added.
* fast/text/font-selection-font-face-parse.html: Added.
* fast/text/font-selection-font-loading-api-parse-expected.txt: Added.
* fast/text/font-selection-font-loading-api-parse.html: Added.
* fast/text/font-stretch-parse-expected.txt:
* fast/text/font-stretch-parse.html:
* fast/text/font-style-parse-expected.txt: Copied from LayoutTests/fast/text/font-stretch-parse-expected.txt.
* fast/text/font-style-parse.html: Added.
* fast/text/font-weight-parse-expected.txt: Copied from LayoutTests/fast/text/font-stretch-parse-expected.txt.
* fast/text/font-weight-parse.html: Added.
git-svn-id: http://svn.webkit.org/repository/webkit/trunk@213464 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/css/FontFace.cpp b/Source/WebCore/css/FontFace.cpp
index 4c2fc59..1178387 100644
--- a/Source/WebCore/css/FontFace.cpp
+++ b/Source/WebCore/css/FontFace.cpp
@@ -163,12 +163,11 @@
if (style.isEmpty())
return Exception { SYNTAX_ERR };
- bool success = false;
- if (auto value = parseString(style, CSSPropertyFontStyle))
- success = m_backing->setStyle(*value);
- if (!success)
- return Exception { SYNTAX_ERR };
- return { };
+ if (auto value = parseString(style, CSSPropertyFontStyle)) {
+ m_backing->setStyle(*value);
+ return { };
+ }
+ return Exception { SYNTAX_ERR };
}
ExceptionOr<void> FontFace::setWeight(const String& weight)
@@ -176,12 +175,11 @@
if (weight.isEmpty())
return Exception { SYNTAX_ERR };
- bool success = false;
- if (auto value = parseString(weight, CSSPropertyFontWeight))
- success = m_backing->setWeight(*value);
- if (!success)
- return Exception { SYNTAX_ERR };
- return { };
+ if (auto value = parseString(weight, CSSPropertyFontWeight)) {
+ m_backing->setWeight(*value);
+ return { };
+ }
+ return Exception { SYNTAX_ERR };
}
ExceptionOr<void> FontFace::setStretch(const String& stretch)
@@ -189,12 +187,11 @@
if (stretch.isEmpty())
return Exception { SYNTAX_ERR };
- bool success = false;
- if (auto value = parseString(stretch, CSSPropertyFontStretch))
- success = m_backing->setStretch(*value);
- if (!success)
- return Exception { SYNTAX_ERR };
- return { };
+ if (auto value = parseString(stretch, CSSPropertyFontStretch)) {
+ m_backing->setStretch(*value);
+ return { };
+ }
+ return Exception { SYNTAX_ERR };
}
ExceptionOr<void> FontFace::setUnicodeRange(const String& unicodeRange)
@@ -283,44 +280,55 @@
return m_backing->families()->cssText();
}
+static inline bool rangeIsSingleValue(FontSelectionRange range, FontSelectionValue value)
+{
+ return range.minimum == value && range.maximum == value;
+};
+
String FontFace::style() const
{
m_backing->updateStyleIfNeeded();
- switch (m_backing->traitsMask() & FontStyleMask) {
- case FontStyleNormalMask:
- return String("normal", String::ConstructFromLiteral);
- case FontStyleItalicMask:
- return String("italic", String::ConstructFromLiteral);
+ auto style = m_backing->italic();
+
+ if (rangeIsSingleValue(style, italicValue()))
+ return ASCIILiteral("italic");
+ if (rangeIsSingleValue(style, normalItalicValue()))
+ return ASCIILiteral("normal");
+
+ if (style.minimum == style.maximum) {
+ auto value = static_cast<float>(style.minimum);
+ if (value >= 0) {
+ auto floored = std::floor(value);
+ if (floored == value)
+ return String::format("oblique %ddeg", static_cast<int>(floored));
+ }
+ return String::format("oblique %fdeg", static_cast<float>(style.minimum));
}
- ASSERT_NOT_REACHED();
- return String("normal", String::ConstructFromLiteral);
+
+ return String::format("oblique %fdeg-%fdeg", static_cast<float>(style.minimum), static_cast<float>(style.maximum));
}
String FontFace::weight() const
{
m_backing->updateStyleIfNeeded();
- switch (m_backing->traitsMask() & FontWeightMask) {
- case FontWeight100Mask:
- return String("100", String::ConstructFromLiteral);
- case FontWeight200Mask:
- return String("200", String::ConstructFromLiteral);
- case FontWeight300Mask:
- return String("300", String::ConstructFromLiteral);
- case FontWeight400Mask:
- return String("normal", String::ConstructFromLiteral);
- case FontWeight500Mask:
- return String("500", String::ConstructFromLiteral);
- case FontWeight600Mask:
- return String("600", String::ConstructFromLiteral);
- case FontWeight700Mask:
- return String("bold", String::ConstructFromLiteral);
- case FontWeight800Mask:
- return String("800", String::ConstructFromLiteral);
- case FontWeight900Mask:
- return String("900", String::ConstructFromLiteral);
+ auto weight = m_backing->weight();
+
+ if (rangeIsSingleValue(weight, normalWeightValue()))
+ return ASCIILiteral("normal");
+ if (rangeIsSingleValue(weight, boldWeightValue()))
+ return ASCIILiteral("bold");
+
+ if (weight.minimum == weight.maximum) {
+ auto value = static_cast<float>(weight.minimum);
+ if (value >= 0) {
+ auto floored = std::floor(value);
+ if (floored == value)
+ return String::format("%d", static_cast<int>(floored));
+ }
+ return String::format("%f", static_cast<float>(weight.minimum));
}
- ASSERT_NOT_REACHED();
- return String("normal", String::ConstructFromLiteral);
+
+ return String::format("%f-%f", static_cast<float>(weight.minimum), static_cast<float>(weight.maximum));
}
String FontFace::stretch() const
@@ -328,30 +336,36 @@
m_backing->updateStyleIfNeeded();
auto stretch = m_backing->stretch();
- auto rangeIsSingleValue = [](FontSelectionRange range, FontSelectionValue value) -> bool {
- return range.minimum == value && range.maximum == value;
- };
-
- if (rangeIsSingleValue(stretch, FontSelectionValue(50)))
+ if (rangeIsSingleValue(stretch, ultraCondensedStretchValue()))
return ASCIILiteral("ultra-condensed");
- if (rangeIsSingleValue(stretch, FontSelectionValue(62.5f)))
+ if (rangeIsSingleValue(stretch, extraCondensedStretchValue()))
return ASCIILiteral("extra-condensed");
- if (rangeIsSingleValue(stretch, FontSelectionValue(75)))
+ if (rangeIsSingleValue(stretch, condensedStretchValue()))
return ASCIILiteral("condensed");
- if (rangeIsSingleValue(stretch, FontSelectionValue(87.5f)))
+ if (rangeIsSingleValue(stretch, semiCondensedStretchValue()))
return ASCIILiteral("semi-condensed");
- if (rangeIsSingleValue(stretch, FontSelectionValue(100)))
+ if (rangeIsSingleValue(stretch, normalStretchValue()))
return ASCIILiteral("normal");
- if (rangeIsSingleValue(stretch, FontSelectionValue(112.5f)))
+ if (rangeIsSingleValue(stretch, semiExpandedStretchValue()))
return ASCIILiteral("semi-expanded");
- if (rangeIsSingleValue(stretch, FontSelectionValue(125)))
+ if (rangeIsSingleValue(stretch, expandedStretchValue()))
return ASCIILiteral("expanded");
- if (rangeIsSingleValue(stretch, FontSelectionValue(150)))
+ if (rangeIsSingleValue(stretch, extraExpandedStretchValue()))
return ASCIILiteral("extra-expanded");
- if (rangeIsSingleValue(stretch, FontSelectionValue(200)))
+ if (rangeIsSingleValue(stretch, ultraExpandedStretchValue()))
return ASCIILiteral("ultra-expanded");
- return String::format("%f-%f", static_cast<float>(stretch.minimum), static_cast<float>(stretch.maximum));
+ if (stretch.minimum == stretch.maximum) {
+ auto value = static_cast<float>(stretch.minimum);
+ if (value >= 0) {
+ auto floored = std::floor(value);
+ if (floored == value)
+ return String::format("%d%%", static_cast<int>(floored));
+ }
+ return String::format("%f%%", static_cast<float>(stretch.minimum));
+ }
+
+ return String::format("%f%%-%f%%", static_cast<float>(stretch.minimum), static_cast<float>(stretch.maximum));
}
String FontFace::unicodeRange() const