blob: 2ac8fe5b18ab7c7bd39303971095e8bbfa408ada [file] [log] [blame]
Xavier Phane29cdaf2020-03-26 16:15:14 +00001/*
2 * Copyright 2019 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "experimental/svg/model/SkSVGText.h"
9
10#include "experimental/svg/model/SkSVGRenderContext.h"
11#include "experimental/svg/model/SkSVGValue.h"
12#include "include/core/SkCanvas.h"
13
14SkSVGText::SkSVGText() : INHERITED(SkSVGTag::kText) {}
15
16void SkSVGText::setX(const SkSVGLength& x) { fX = x; }
17
18void SkSVGText::setY(const SkSVGLength& y) { fY = y; }
19
20void SkSVGText::setFontFamily(const SkSVGStringType& font_family) {
21 fTypeface =
Florin Malitaa3626692020-04-09 14:36:45 -040022 SkTypeface::MakeFromName(font_family.c_str(), SkFontStyle());
Xavier Phane29cdaf2020-03-26 16:15:14 +000023}
24
25void SkSVGText::setFontSize(const SkSVGLength& size) { fFontSize = size; }
26
27void SkSVGText::setText(const SkSVGStringType& text) { fText = text; }
28
29void SkSVGText::setTextAnchor(const SkSVGStringType& text_anchor) {
Florin Malitaa3626692020-04-09 14:36:45 -040030 if (strcmp(text_anchor.c_str(), "start") == 0) {
Xavier Phane29cdaf2020-03-26 16:15:14 +000031 fTextAlign = SkTextUtils::Align::kLeft_Align;
Florin Malitaa3626692020-04-09 14:36:45 -040032 } else if (strcmp(text_anchor.c_str(), "middle") == 0) {
Xavier Phane29cdaf2020-03-26 16:15:14 +000033 fTextAlign = SkTextUtils::Align::kCenter_Align;
Florin Malitaa3626692020-04-09 14:36:45 -040034 } else if (strcmp(text_anchor.c_str(), "end") == 0) {
Xavier Phane29cdaf2020-03-26 16:15:14 +000035 fTextAlign = SkTextUtils::Align::kRight_Align;
36 }
37}
38
39void SkSVGText::onDraw(SkCanvas* canvas, const SkSVGLengthContext& lctx,
40 const SkPaint& paint, SkPathFillType) const {
41 SkFont font(fTypeface, fFontSize.value());
Florin Malitaa3626692020-04-09 14:36:45 -040042 SkTextUtils::DrawString(canvas, fText.c_str(), fX.value(), fY.value(),
Xavier Phane29cdaf2020-03-26 16:15:14 +000043 font, paint, fTextAlign);
44}
45
46SkPath SkSVGText::onAsPath(const SkSVGRenderContext& ctx) const {
47 SkPath path;
48 return path;
49}
50
51void SkSVGText::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
52 switch (attr) {
53 case SkSVGAttribute::kX:
54 if (const auto* x = v.as<SkSVGLengthValue>()) {
55 this->setX(*x);
56 }
57 break;
58 case SkSVGAttribute::kY:
59 if (const auto* y = v.as<SkSVGLengthValue>()) {
60 this->setY(*y);
61 }
62 break;
63 case SkSVGAttribute::kText:
64 if (const auto* text = v.as<SkSVGStringValue>()) {
65 this->setText(*text);
66 }
67 break;
68 case SkSVGAttribute::kTextAnchor:
69 if (const auto* text_anchor = v.as<SkSVGStringValue>()) {
70 this->setTextAnchor(*text_anchor);
71 }
72 break;
73 case SkSVGAttribute::kFontFamily:
74 if (const auto* font_family = v.as<SkSVGStringValue>()) {
75 this->setFontFamily(*font_family);
76 }
77 break;
78 case SkSVGAttribute::kFontSize:
79 if (const auto* font_size = v.as<SkSVGLengthValue>()) {
80 this->setFontSize(*font_size);
81 }
82 break;
83 default:
84 this->INHERITED::onSetAttribute(attr, v);
85 }
86}