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 | |
| 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 | |
| 14 | SkSVGText::SkSVGText() : INHERITED(SkSVGTag::kText) {} |
| 15 | |
| 16 | void SkSVGText::setX(const SkSVGLength& x) { fX = x; } |
| 17 | |
| 18 | void SkSVGText::setY(const SkSVGLength& y) { fY = y; } |
| 19 | |
| 20 | void SkSVGText::setFontFamily(const SkSVGStringType& font_family) { |
| 21 | fTypeface = |
| 22 | SkTypeface::MakeFromName(font_family.value().c_str(), SkFontStyle()); |
| 23 | } |
| 24 | |
| 25 | void SkSVGText::setFontSize(const SkSVGLength& size) { fFontSize = size; } |
| 26 | |
| 27 | void SkSVGText::setText(const SkSVGStringType& text) { fText = text; } |
| 28 | |
| 29 | void SkSVGText::setTextAnchor(const SkSVGStringType& text_anchor) { |
| 30 | if (strcmp(text_anchor.value().c_str(), "start") == 0) { |
| 31 | fTextAlign = SkTextUtils::Align::kLeft_Align; |
| 32 | } else if (strcmp(text_anchor.value().c_str(), "middle") == 0) { |
| 33 | fTextAlign = SkTextUtils::Align::kCenter_Align; |
| 34 | } else if (strcmp(text_anchor.value().c_str(), "end") == 0) { |
| 35 | fTextAlign = SkTextUtils::Align::kRight_Align; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | void SkSVGText::onDraw(SkCanvas* canvas, const SkSVGLengthContext& lctx, |
| 40 | const SkPaint& paint, SkPathFillType) const { |
| 41 | SkFont font(fTypeface, fFontSize.value()); |
| 42 | SkTextUtils::DrawString(canvas, fText.value().c_str(), fX.value(), fY.value(), |
| 43 | font, paint, fTextAlign); |
| 44 | } |
| 45 | |
| 46 | SkPath SkSVGText::onAsPath(const SkSVGRenderContext& ctx) const { |
| 47 | SkPath path; |
| 48 | return path; |
| 49 | } |
| 50 | |
| 51 | void 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 | } |