Xavier Phan | e29cdaf | 2020-03-26 16:15:14 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 8 | #include "modules/svg/include/SkSVGText.h" |
Xavier Phan | e29cdaf | 2020-03-26 16:15:14 +0000 | [diff] [blame] | 9 | |
Xavier Phan | e29cdaf | 2020-03-26 16:15:14 +0000 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
Tyler Freeman | e9663db | 2020-04-14 14:37:13 -0700 | [diff] [blame] | 11 | #include "include/core/SkFontStyle.h" |
| 12 | #include "include/core/SkString.h" |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 13 | #include "modules/svg/include/SkSVGRenderContext.h" |
| 14 | #include "modules/svg/include/SkSVGValue.h" |
Xavier Phan | e29cdaf | 2020-03-26 16:15:14 +0000 | [diff] [blame] | 15 | |
| 16 | SkSVGText::SkSVGText() : INHERITED(SkSVGTag::kText) {} |
| 17 | |
| 18 | void SkSVGText::setX(const SkSVGLength& x) { fX = x; } |
| 19 | |
| 20 | void SkSVGText::setY(const SkSVGLength& y) { fY = y; } |
| 21 | |
| 22 | void SkSVGText::setFontFamily(const SkSVGStringType& font_family) { |
Tyler Freeman | e9663db | 2020-04-14 14:37:13 -0700 | [diff] [blame] | 23 | if (fFontFamily != font_family) { |
| 24 | fFontFamily = font_family; |
| 25 | |
| 26 | this->loadFont(); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | void SkSVGText::loadFont() { |
| 31 | SkFontStyle style; |
| 32 | if (fFontWeight.equals("bold")) { |
| 33 | if (fFontStyle.equals("italic")) { |
| 34 | style = SkFontStyle::BoldItalic(); |
| 35 | } else { |
| 36 | style = SkFontStyle::Bold(); |
| 37 | } |
| 38 | } else if (fFontStyle.equals("italic")) { |
| 39 | style = SkFontStyle::Italic(); |
| 40 | } |
| 41 | |
| 42 | fTypeface = SkTypeface::MakeFromName(fFontFamily.c_str(), style); |
Xavier Phan | e29cdaf | 2020-03-26 16:15:14 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void SkSVGText::setFontSize(const SkSVGLength& size) { fFontSize = size; } |
| 46 | |
Tyler Freeman | e9663db | 2020-04-14 14:37:13 -0700 | [diff] [blame] | 47 | void SkSVGText::setFontStyle(const SkSVGStringType& font_style) { |
| 48 | if (fFontStyle != font_style) { |
| 49 | fFontStyle = font_style; |
| 50 | |
| 51 | this->loadFont(); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | void SkSVGText::setFontWeight(const SkSVGStringType& font_weight) { |
| 56 | if (fFontWeight != font_weight) { |
| 57 | fFontWeight = font_weight; |
| 58 | |
| 59 | this->loadFont(); |
| 60 | } |
| 61 | } |
| 62 | |
Xavier Phan | e29cdaf | 2020-03-26 16:15:14 +0000 | [diff] [blame] | 63 | void SkSVGText::setText(const SkSVGStringType& text) { fText = text; } |
| 64 | |
| 65 | void SkSVGText::setTextAnchor(const SkSVGStringType& text_anchor) { |
Florin Malita | a362669 | 2020-04-09 14:36:45 -0400 | [diff] [blame] | 66 | if (strcmp(text_anchor.c_str(), "start") == 0) { |
Xavier Phan | e29cdaf | 2020-03-26 16:15:14 +0000 | [diff] [blame] | 67 | fTextAlign = SkTextUtils::Align::kLeft_Align; |
Florin Malita | a362669 | 2020-04-09 14:36:45 -0400 | [diff] [blame] | 68 | } else if (strcmp(text_anchor.c_str(), "middle") == 0) { |
Xavier Phan | e29cdaf | 2020-03-26 16:15:14 +0000 | [diff] [blame] | 69 | fTextAlign = SkTextUtils::Align::kCenter_Align; |
Florin Malita | a362669 | 2020-04-09 14:36:45 -0400 | [diff] [blame] | 70 | } else if (strcmp(text_anchor.c_str(), "end") == 0) { |
Xavier Phan | e29cdaf | 2020-03-26 16:15:14 +0000 | [diff] [blame] | 71 | fTextAlign = SkTextUtils::Align::kRight_Align; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void SkSVGText::onDraw(SkCanvas* canvas, const SkSVGLengthContext& lctx, |
| 76 | const SkPaint& paint, SkPathFillType) const { |
| 77 | SkFont font(fTypeface, fFontSize.value()); |
Florin Malita | a362669 | 2020-04-09 14:36:45 -0400 | [diff] [blame] | 78 | SkTextUtils::DrawString(canvas, fText.c_str(), fX.value(), fY.value(), |
Xavier Phan | e29cdaf | 2020-03-26 16:15:14 +0000 | [diff] [blame] | 79 | font, paint, fTextAlign); |
| 80 | } |
| 81 | |
| 82 | SkPath SkSVGText::onAsPath(const SkSVGRenderContext& ctx) const { |
| 83 | SkPath path; |
| 84 | return path; |
| 85 | } |
| 86 | |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 87 | void SkSVGText::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) { |
Xavier Phan | e29cdaf | 2020-03-26 16:15:14 +0000 | [diff] [blame] | 88 | switch (attr) { |
| 89 | case SkSVGAttribute::kX: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 90 | if (const auto* x = v.as<SkSVGLengthValue>()) { |
Xavier Phan | e29cdaf | 2020-03-26 16:15:14 +0000 | [diff] [blame] | 91 | this->setX(*x); |
| 92 | } |
| 93 | break; |
| 94 | case SkSVGAttribute::kY: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 95 | if (const auto* y = v.as<SkSVGLengthValue>()) { |
Xavier Phan | e29cdaf | 2020-03-26 16:15:14 +0000 | [diff] [blame] | 96 | this->setY(*y); |
| 97 | } |
| 98 | break; |
| 99 | case SkSVGAttribute::kText: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 100 | if (const auto* text = v.as<SkSVGStringValue>()) { |
Xavier Phan | e29cdaf | 2020-03-26 16:15:14 +0000 | [diff] [blame] | 101 | this->setText(*text); |
| 102 | } |
| 103 | break; |
| 104 | case SkSVGAttribute::kTextAnchor: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 105 | if (const auto* text_anchor = v.as<SkSVGStringValue>()) { |
Xavier Phan | e29cdaf | 2020-03-26 16:15:14 +0000 | [diff] [blame] | 106 | this->setTextAnchor(*text_anchor); |
| 107 | } |
| 108 | break; |
| 109 | case SkSVGAttribute::kFontFamily: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 110 | if (const auto* font_family = v.as<SkSVGStringValue>()) { |
Xavier Phan | e29cdaf | 2020-03-26 16:15:14 +0000 | [diff] [blame] | 111 | this->setFontFamily(*font_family); |
| 112 | } |
| 113 | break; |
Tyler Freeman | e9663db | 2020-04-14 14:37:13 -0700 | [diff] [blame] | 114 | case SkSVGAttribute::kFontWeight: |
| 115 | if (const auto* font_weight = v.as<SkSVGStringValue>()) { |
| 116 | this->setFontWeight(*font_weight); |
| 117 | } |
| 118 | break; |
Xavier Phan | e29cdaf | 2020-03-26 16:15:14 +0000 | [diff] [blame] | 119 | case SkSVGAttribute::kFontSize: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 120 | if (const auto* font_size = v.as<SkSVGLengthValue>()) { |
Xavier Phan | e29cdaf | 2020-03-26 16:15:14 +0000 | [diff] [blame] | 121 | this->setFontSize(*font_size); |
| 122 | } |
| 123 | break; |
Tyler Freeman | e9663db | 2020-04-14 14:37:13 -0700 | [diff] [blame] | 124 | case SkSVGAttribute::kFontStyle: |
| 125 | if (const auto* font_style = v.as<SkSVGStringValue>()) { |
| 126 | this->setFontStyle(*font_style); |
| 127 | } |
| 128 | break; |
Xavier Phan | e29cdaf | 2020-03-26 16:15:14 +0000 | [diff] [blame] | 129 | default: |
| 130 | this->INHERITED::onSetAttribute(attr, v); |
| 131 | } |
| 132 | } |