[svg] Parse text attributes
Convert font-family, font-size, font-style and font-weight to
presentation attributes, and add parsing utils.
Bug: skia:10840
Change-Id: I1acdb59bc95fe46e67ed0f499dd0732420016663
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/328436
Commit-Queue: Florin Malita <fmalita@google.com>
Reviewed-by: Tyler Denniston <tdenniston@google.com>
diff --git a/modules/svg/src/SkSVGAttribute.cpp b/modules/svg/src/SkSVGAttribute.cpp
index 5bdb65b..0ccea49 100644
--- a/modules/svg/src/SkSVGAttribute.cpp
+++ b/modules/svg/src/SkSVGAttribute.cpp
@@ -28,5 +28,10 @@
result.fColor.set(SkSVGColorType(SK_ColorBLACK));
+ result.fFontFamily.init("Sans");
+ result.fFontStyle.init(SkSVGFontStyle::Type::kNormal);
+ result.fFontSize.init(SkSVGLength(24));
+ result.fFontWeight.init(SkSVGFontWeight(SkSVGFontWeight::Type::kNormal));
+
return result;
}
diff --git a/modules/svg/src/SkSVGAttributeParser.cpp b/modules/svg/src/SkSVGAttributeParser.cpp
index 9914456..18acdf4 100644
--- a/modules/svg/src/SkSVGAttributeParser.cpp
+++ b/modules/svg/src/SkSVGAttributeParser.cpp
@@ -33,6 +33,17 @@
} // namespace
+template <typename T, typename TArray>
+bool SkSVGAttributeParser::parseEnumMap(const TArray& arr, T* result) {
+ for (size_t i = 0; i < SK_ARRAY_COUNT(arr); ++i) {
+ if (this->parseExpectedStringToken(std::get<0>(arr[i]))) {
+ *result = std::get<1>(arr[i]);
+ return true;
+ }
+ }
+ return false;
+}
+
SkSVGAttributeParser::SkSVGAttributeParser(const char attributeString[])
: fCurPos(attributeString) {}
@@ -710,3 +721,90 @@
return parsedValue && this->parseEOSToken();
}
+
+// https://www.w3.org/TR/SVG11/text.html#FontFamilyProperty
+bool SkSVGAttributeParser::parseFontFamily(SkSVGFontFamily* family) {
+ bool parsedValue = false;
+ if (this->parseExpectedStringToken("inherit")) {
+ *family = SkSVGFontFamily();
+ parsedValue = true;
+ } else {
+ // The spec allows specifying a comma-separated list for explicit fallback order.
+ // For now, we only use the first entry and rely on the font manager to handle fallback.
+ const auto* comma = strchr(fCurPos, ',');
+ auto family_name = comma ? SkString(fCurPos, comma - fCurPos)
+ : SkString(fCurPos);
+ *family = SkSVGFontFamily(family_name.c_str());
+ fCurPos += strlen(fCurPos);
+ parsedValue = true;
+ }
+
+ return parsedValue && this->parseEOSToken();
+}
+
+// https://www.w3.org/TR/SVG11/text.html#FontSizeProperty
+bool SkSVGAttributeParser::parseFontSize(SkSVGFontSize* size) {
+ bool parsedValue = false;
+ if (this->parseExpectedStringToken("inherit")) {
+ *size = SkSVGFontSize();
+ parsedValue = true;
+ } else {
+ SkSVGLength length;
+ if (this->parseLength(&length)) {
+ *size = SkSVGFontSize(length);
+ parsedValue = true;
+ }
+ }
+
+ return parsedValue && this->parseEOSToken();
+}
+
+// https://www.w3.org/TR/SVG11/text.html#FontStyleProperty
+bool SkSVGAttributeParser::parseFontStyle(SkSVGFontStyle* style) {
+ static constexpr std::tuple<const char*, SkSVGFontStyle::Type> gStyleMap[] = {
+ { "normal" , SkSVGFontStyle::Type::kNormal },
+ { "italic" , SkSVGFontStyle::Type::kItalic },
+ { "oblique", SkSVGFontStyle::Type::kOblique },
+ { "inherit", SkSVGFontStyle::Type::kInherit },
+ };
+
+ bool parsedValue = false;
+ SkSVGFontStyle::Type type;
+
+ if (this->parseEnumMap(gStyleMap, &type)) {
+ *style = SkSVGFontStyle(type);
+ parsedValue = true;
+ }
+
+ return parsedValue && this->parseEOSToken();
+}
+
+// https://www.w3.org/TR/SVG11/text.html#FontWeightProperty
+bool SkSVGAttributeParser::parseFontWeight(SkSVGFontWeight* weight) {
+ static constexpr std::tuple<const char*, SkSVGFontWeight::Type> gWeightMap[] = {
+ { "normal" , SkSVGFontWeight::Type::kNormal },
+ { "bold" , SkSVGFontWeight::Type::kBold },
+ { "bolder" , SkSVGFontWeight::Type::kBolder },
+ { "lighter", SkSVGFontWeight::Type::kLighter },
+ { "100" , SkSVGFontWeight::Type::k100 },
+ { "200" , SkSVGFontWeight::Type::k200 },
+ { "300" , SkSVGFontWeight::Type::k300 },
+ { "400" , SkSVGFontWeight::Type::k400 },
+ { "500" , SkSVGFontWeight::Type::k500 },
+ { "600" , SkSVGFontWeight::Type::k600 },
+ { "700" , SkSVGFontWeight::Type::k700 },
+ { "800" , SkSVGFontWeight::Type::k800 },
+ { "900" , SkSVGFontWeight::Type::k900 },
+ { "inherit", SkSVGFontWeight::Type::kInherit },
+ };
+
+ bool parsedValue = false;
+ SkSVGFontWeight::Type type;
+
+ if (this->parseEnumMap(gWeightMap, &type)) {
+ *weight = SkSVGFontWeight(type);
+ parsedValue = true;
+ }
+
+ return parsedValue && this->parseEOSToken();
+}
diff --git a/modules/svg/src/SkSVGDOM.cpp b/modules/svg/src/SkSVGDOM.cpp
index c833a6a..8ab5ade 100644
--- a/modules/svg/src/SkSVGDOM.cpp
+++ b/modules/svg/src/SkSVGDOM.cpp
@@ -248,6 +248,54 @@
return true;
}
+bool SetFontFamilyAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
+ const char* stringValue) {
+ SkSVGFontFamily family;
+ SkSVGAttributeParser parser(stringValue);
+ if (!parser.parseFontFamily(&family)) {
+ return false;
+ }
+
+ node->setAttribute(attr, SkSVGFontFamilyValue(family));
+ return true;
+}
+
+bool SetFontSizeAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
+ const char* stringValue) {
+ SkSVGFontSize size;
+ SkSVGAttributeParser parser(stringValue);
+ if (!parser.parseFontSize(&size)) {
+ return false;
+ }
+
+ node->setAttribute(attr, SkSVGFontSizeValue(size));
+ return true;
+}
+
+bool SetFontStyleAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
+ const char* stringValue) {
+ SkSVGFontStyle style;
+ SkSVGAttributeParser parser(stringValue);
+ if (!parser.parseFontStyle(&style)) {
+ return false;
+ }
+
+ node->setAttribute(attr, SkSVGFontStyleValue(style));
+ return true;
+}
+
+bool SetFontWeightAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
+ const char* stringValue) {
+ SkSVGFontWeight weight;
+ SkSVGAttributeParser parser(stringValue);
+ if (!parser.parseFontWeight(&weight)) {
+ return false;
+ }
+
+ node->setAttribute(attr, SkSVGFontWeightValue(weight));
+ return true;
+}
+
SkString TrimmedString(const char* first, const char* last) {
SkASSERT(first);
SkASSERT(last);
@@ -335,10 +383,10 @@
{ "fill" , { SkSVGAttribute::kFill , SetPaintAttribute }},
{ "fill-opacity" , { SkSVGAttribute::kFillOpacity , SetNumberAttribute }},
{ "fill-rule" , { SkSVGAttribute::kFillRule , SetFillRuleAttribute }},
- { "font-family" , { SkSVGAttribute::kFontFamily , SetStringAttribute }},
- { "font-size" , { SkSVGAttribute::kFontSize , SetLengthAttribute }},
- { "font-style" , { SkSVGAttribute::kFontStyle , SetStringAttribute }},
- { "font-weight" , { SkSVGAttribute::kFontWeight , SetStringAttribute }},
+ { "font-family" , { SkSVGAttribute::kFontFamily , SetFontFamilyAttribute }},
+ { "font-size" , { SkSVGAttribute::kFontSize , SetFontSizeAttribute }},
+ { "font-style" , { SkSVGAttribute::kFontStyle , SetFontStyleAttribute }},
+ { "font-weight" , { SkSVGAttribute::kFontWeight , SetFontWeightAttribute }},
// focal point x & y
{ "fx" , { SkSVGAttribute::kFx , SetLengthAttribute }},
{ "fy" , { SkSVGAttribute::kFy , SetLengthAttribute }},
@@ -404,8 +452,8 @@
ConstructionContext(const ConstructionContext& other, const sk_sp<SkSVGNode>& newParent)
: fParent(newParent.get()), fIDMapper(other.fIDMapper) {}
- const SkSVGNode* fParent;
- SkSVGIDMapper* fIDMapper;
+ SkSVGNode* fParent;
+ SkSVGIDMapper* fIDMapper;
};
bool set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value) {
@@ -452,7 +500,10 @@
if (elemType == SkDOM::kText_Type) {
SkASSERT(dom.countChildren(xmlNode) == 0);
- // TODO: text handling
+ // TODO: add type conversion helper to SkSVGNode
+ if (ctx.fParent->tag() == SkSVGTag::kText) {
+ static_cast<SkSVGText*>(ctx.fParent)->setText(SkString(dom.getName(xmlNode)));
+ }
return nullptr;
}
diff --git a/modules/svg/src/SkSVGNode.cpp b/modules/svg/src/SkSVGNode.cpp
index d634722..ce05aa5 100644
--- a/modules/svg/src/SkSVGNode.cpp
+++ b/modules/svg/src/SkSVGNode.cpp
@@ -169,6 +169,26 @@
this->setFillRule(*fillRule);
}
break;
+ case SkSVGAttribute::kFontFamily:
+ if (const SkSVGFontFamilyValue* family = v.as<SkSVGFontFamilyValue>()) {
+ this->setFontFamily(*family);
+ }
+ break;
+ case SkSVGAttribute::kFontSize:
+ if (const SkSVGFontSizeValue* size = v.as<SkSVGFontSizeValue>()) {
+ this->setFontSize(*size);
+ }
+ break;
+ case SkSVGAttribute::kFontStyle:
+ if (const SkSVGFontStyleValue* style = v.as<SkSVGFontStyleValue>()) {
+ this->setFontStyle(*style);
+ }
+ break;
+ case SkSVGAttribute::kFontWeight:
+ if (const SkSVGFontWeightValue* style = v.as<SkSVGFontWeightValue>()) {
+ this->setFontWeight(*style);
+ }
+ break;
case SkSVGAttribute::kOpacity:
if (const SkSVGNumberValue* opacity = v.as<SkSVGNumberValue>()) {
this->setOpacity(*opacity);
diff --git a/modules/svg/src/SkSVGRenderContext.cpp b/modules/svg/src/SkSVGRenderContext.cpp
index 4607f67..759c5cf 100644
--- a/modules/svg/src/SkSVGRenderContext.cpp
+++ b/modules/svg/src/SkSVGRenderContext.cpp
@@ -274,6 +274,34 @@
// Not part of the SkPaint state; applied via 'currentColor' color value
}
+template <>
+void commitToPaint<SkSVGAttribute::kFontFamily>(const SkSVGPresentationAttributes&,
+ const SkSVGRenderContext&,
+ SkSVGPresentationContext*) {
+ // Not part of the SkPaint state; applied at render time.
+}
+
+template <>
+void commitToPaint<SkSVGAttribute::kFontSize>(const SkSVGPresentationAttributes&,
+ const SkSVGRenderContext&,
+ SkSVGPresentationContext*) {
+ // Not part of the SkPaint state; applied at render time.
+}
+
+template <>
+void commitToPaint<SkSVGAttribute::kFontStyle>(const SkSVGPresentationAttributes&,
+ const SkSVGRenderContext&,
+ SkSVGPresentationContext*) {
+ // Not part of the SkPaint state; applied at render time.
+}
+
+template <>
+void commitToPaint<SkSVGAttribute::kFontWeight>(const SkSVGPresentationAttributes&,
+ const SkSVGRenderContext&,
+ SkSVGPresentationContext*) {
+ // Not part of the SkPaint state; applied at render time.
+}
+
} // namespace
SkSVGPresentationContext::SkSVGPresentationContext()
@@ -351,6 +379,10 @@
ApplyLazyInheritedAttribute(Fill);
ApplyLazyInheritedAttribute(FillOpacity);
ApplyLazyInheritedAttribute(FillRule);
+ ApplyLazyInheritedAttribute(FontFamily);
+ ApplyLazyInheritedAttribute(FontSize);
+ ApplyLazyInheritedAttribute(FontStyle);
+ ApplyLazyInheritedAttribute(FontWeight);
ApplyLazyInheritedAttribute(ClipRule);
ApplyLazyInheritedAttribute(Stroke);
ApplyLazyInheritedAttribute(StrokeDashOffset);
diff --git a/modules/svg/src/SkSVGText.cpp b/modules/svg/src/SkSVGText.cpp
index b2d8704..4dde8b8 100644
--- a/modules/svg/src/SkSVGText.cpp
+++ b/modules/svg/src/SkSVGText.cpp
@@ -8,6 +8,7 @@
#include "modules/svg/include/SkSVGText.h"
#include "include/core/SkCanvas.h"
+#include "include/core/SkFontMgr.h"
#include "include/core/SkFontStyle.h"
#include "include/core/SkString.h"
#include "modules/svg/include/SkSVGRenderContext.h"
@@ -19,47 +20,6 @@
void SkSVGText::setY(const SkSVGLength& y) { fY = y; }
-void SkSVGText::setFontFamily(const SkSVGStringType& font_family) {
- if (fFontFamily != font_family) {
- fFontFamily = font_family;
-
- this->loadFont();
- }
-}
-
-void SkSVGText::loadFont() {
- SkFontStyle style;
- if (fFontWeight.equals("bold")) {
- if (fFontStyle.equals("italic")) {
- style = SkFontStyle::BoldItalic();
- } else {
- style = SkFontStyle::Bold();
- }
- } else if (fFontStyle.equals("italic")) {
- style = SkFontStyle::Italic();
- }
-
- fTypeface = SkTypeface::MakeFromName(fFontFamily.c_str(), style);
-}
-
-void SkSVGText::setFontSize(const SkSVGLength& size) { fFontSize = size; }
-
-void SkSVGText::setFontStyle(const SkSVGStringType& font_style) {
- if (fFontStyle != font_style) {
- fFontStyle = font_style;
-
- this->loadFont();
- }
-}
-
-void SkSVGText::setFontWeight(const SkSVGStringType& font_weight) {
- if (fFontWeight != font_weight) {
- fFontWeight = font_weight;
-
- this->loadFont();
- }
-}
-
void SkSVGText::setText(const SkSVGStringType& text) { fText = text; }
void SkSVGText::setTextAnchor(const SkSVGStringType& text_anchor) {
@@ -72,11 +32,79 @@
}
}
-void SkSVGText::onDraw(SkCanvas* canvas, const SkSVGLengthContext& lctx,
- const SkPaint& paint, SkPathFillType) const {
- SkFont font(fTypeface, fFontSize.value());
- SkTextUtils::DrawString(canvas, fText.c_str(), fX.value(), fY.value(),
- font, paint, fTextAlign);
+SkFont SkSVGText::resolveFont(const SkSVGRenderContext& ctx) const {
+ auto weight = [](const SkSVGFontWeight& w) {
+ switch (w.type()) {
+ case SkSVGFontWeight::Type::k100: return SkFontStyle::kThin_Weight;
+ case SkSVGFontWeight::Type::k200: return SkFontStyle::kExtraLight_Weight;
+ case SkSVGFontWeight::Type::k300: return SkFontStyle::kLight_Weight;
+ case SkSVGFontWeight::Type::k400: return SkFontStyle::kNormal_Weight;
+ case SkSVGFontWeight::Type::k500: return SkFontStyle::kMedium_Weight;
+ case SkSVGFontWeight::Type::k600: return SkFontStyle::kSemiBold_Weight;
+ case SkSVGFontWeight::Type::k700: return SkFontStyle::kBold_Weight;
+ case SkSVGFontWeight::Type::k800: return SkFontStyle::kExtraBold_Weight;
+ case SkSVGFontWeight::Type::k900: return SkFontStyle::kBlack_Weight;
+ case SkSVGFontWeight::Type::kNormal: return SkFontStyle::kNormal_Weight;
+ case SkSVGFontWeight::Type::kBold: return SkFontStyle::kBold_Weight;
+ case SkSVGFontWeight::Type::kBolder: return SkFontStyle::kExtraBold_Weight;
+ case SkSVGFontWeight::Type::kLighter: return SkFontStyle::kLight_Weight;
+ case SkSVGFontWeight::Type::kInherit: {
+ SkASSERT(false);
+ return SkFontStyle::kNormal_Weight;
+ }
+ }
+ SkUNREACHABLE;
+ };
+
+ auto slant = [](const SkSVGFontStyle& s) {
+ switch (s.type()) {
+ case SkSVGFontStyle::Type::kNormal: return SkFontStyle::kUpright_Slant;
+ case SkSVGFontStyle::Type::kItalic: return SkFontStyle::kItalic_Slant;
+ case SkSVGFontStyle::Type::kOblique: return SkFontStyle::kOblique_Slant;
+ case SkSVGFontStyle::Type::kInherit: {
+ SkASSERT(false);
+ return SkFontStyle::kUpright_Slant;
+ }
+ }
+ SkUNREACHABLE;
+ };
+
+ const auto& family = ctx.presentationContext().fInherited.fFontFamily->family();
+ const SkFontStyle style(weight(*ctx.presentationContext().fInherited.fFontWeight),
+ SkFontStyle::kNormal_Width,
+ slant(*ctx.presentationContext().fInherited.fFontStyle));
+
+ const auto size =
+ ctx.lengthContext().resolve(ctx.presentationContext().fInherited.fFontSize->size(),
+ SkSVGLengthContext::LengthType::kVertical);
+
+ // TODO: allow clients to pass an external fontmgr.
+ SkFont font(SkTypeface::MakeFromName(family.c_str(), style), size);
+ font.setHinting(SkFontHinting::kNone);
+ font.setSubpixel(true);
+ font.setLinearMetrics(true);
+ font.setBaselineSnap(false);
+ font.setEdging(SkFont::Edging::kAntiAlias);
+
+ return font;
+}
+
+void SkSVGText::onRender(const SkSVGRenderContext& ctx) const {
+ const auto font = this->resolveFont(ctx);
+
+ if (const SkPaint* fillPaint = ctx.fillPaint()) {
+ SkTextUtils::DrawString(ctx.canvas(), fText.c_str(), fX.value(), fY.value(), font,
+ *fillPaint, fTextAlign);
+ }
+
+ if (const SkPaint* strokePaint = ctx.strokePaint()) {
+ SkTextUtils::DrawString(ctx.canvas(), fText.c_str(), fX.value(), fY.value(), font,
+ *strokePaint, fTextAlign);
+ }
+}
+
+void SkSVGText::appendChild(sk_sp<SkSVGNode>) {
+ // TODO
}
SkPath SkSVGText::onAsPath(const SkSVGRenderContext& ctx) const {
@@ -106,25 +134,6 @@
this->setTextAnchor(*text_anchor);
}
break;
- case SkSVGAttribute::kFontFamily:
- if (const auto* font_family = v.as<SkSVGStringValue>()) {
- this->setFontFamily(*font_family);
- }
- break;
- case SkSVGAttribute::kFontWeight:
- if (const auto* font_weight = v.as<SkSVGStringValue>()) {
- this->setFontWeight(*font_weight);
- }
- break;
- case SkSVGAttribute::kFontSize:
- if (const auto* font_size = v.as<SkSVGLengthValue>()) {
- this->setFontSize(*font_size);
- }
- break;
- case SkSVGAttribute::kFontStyle:
- if (const auto* font_style = v.as<SkSVGStringValue>()) {
- this->setFontStyle(*font_style);
- }
break;
default:
this->INHERITED::onSetAttribute(attr, v);