Skip to content

Commit

Permalink
SVG: Added font-size and font-weight attributes to Text node
Browse files Browse the repository at this point in the history
Change-Id: Ic929cb0119aefae490110648df6e9ea57700c6db
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/281838
Reviewed-by: Ben Wagner <bungeman@google.com>
Reviewed-by: Florin Malita <fmalita@chromium.org>
Commit-Queue: Florin Malita <fmalita@chromium.org>
  • Loading branch information
odbol authored and Skia Commit-Bot committed Apr 22, 2020
1 parent 2432d06 commit e9663db
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
2 changes: 2 additions & 0 deletions experimental/svg/model/SkSVGAttribute.h
Expand Up @@ -25,6 +25,8 @@ enum class SkSVGAttribute {
kFillRule,
kFontFamily,
kFontSize,
kFontStyle,
kFontWeight,
kFx, // <radialGradient>: focal point x position
kFy, // <radialGradient>: focal point y position
kGradientTransform,
Expand Down
2 changes: 2 additions & 0 deletions experimental/svg/model/SkSVGDOM.cpp
Expand Up @@ -337,6 +337,8 @@ SortedDictionaryEntry<AttrParseInfo> gAttributeParseInfo[] = {
{ "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 }},
// focal point x & y
{ "fx" , { SkSVGAttribute::kFx , SetLengthAttribute }},
{ "fy" , { SkSVGAttribute::kFy , SetLengthAttribute }},
Expand Down
50 changes: 48 additions & 2 deletions experimental/svg/model/SkSVGText.cpp
Expand Up @@ -10,6 +10,8 @@
#include "experimental/svg/model/SkSVGRenderContext.h"
#include "experimental/svg/model/SkSVGValue.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkFontStyle.h"
#include "include/core/SkString.h"

SkSVGText::SkSVGText() : INHERITED(SkSVGTag::kText) {}

Expand All @@ -18,12 +20,46 @@ void SkSVGText::setX(const SkSVGLength& x) { fX = x; }
void SkSVGText::setY(const SkSVGLength& y) { fY = y; }

void SkSVGText::setFontFamily(const SkSVGStringType& font_family) {
fTypeface =
SkTypeface::MakeFromName(font_family.c_str(), SkFontStyle());
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) {
Expand Down Expand Up @@ -75,11 +111,21 @@ void SkSVGText::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
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);
}
Expand Down
7 changes: 7 additions & 0 deletions experimental/svg/model/SkSVGText.h
Expand Up @@ -25,6 +25,8 @@ class SkSVGText final : public SkSVGShape {
void setY(const SkSVGLength&);
void setFontFamily(const SkSVGStringType&);
void setFontSize(const SkSVGLength&);
void setFontStyle(const SkSVGStringType&);
void setFontWeight(const SkSVGStringType&);
void setText(const SkSVGStringType&);
void setTextAnchor(const SkSVGStringType&);

Expand All @@ -36,13 +38,18 @@ class SkSVGText final : public SkSVGShape {

SkPath onAsPath(const SkSVGRenderContext&) const override;

void loadFont();

private:
SkSVGText();
SkSVGLength fX = SkSVGLength(0);
SkSVGLength fY = SkSVGLength(0);
SkSVGStringType fText;
sk_sp<SkTypeface> fTypeface;
SkSVGLength fFontSize;
SkSVGStringType fFontFamily;
SkSVGStringType fFontStyle;
SkSVGStringType fFontWeight;
SkTextUtils::Align fTextAlign = SkTextUtils::Align::kLeft_Align;
typedef SkSVGShape INHERITED;
};
Expand Down

0 comments on commit e9663db

Please sign in to comment.