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" |
Florin Malita | 39fe8c8 | 2020-10-20 10:43:03 -0400 | [diff] [blame] | 11 | #include "include/core/SkFontMgr.h" |
Tyler Freeman | e9663db | 2020-04-14 14:37:13 -0700 | [diff] [blame] | 12 | #include "include/core/SkFontStyle.h" |
| 13 | #include "include/core/SkString.h" |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 14 | #include "modules/svg/include/SkSVGRenderContext.h" |
| 15 | #include "modules/svg/include/SkSVGValue.h" |
Xavier Phan | e29cdaf | 2020-03-26 16:15:14 +0000 | [diff] [blame] | 16 | |
| 17 | SkSVGText::SkSVGText() : INHERITED(SkSVGTag::kText) {} |
| 18 | |
Florin Malita | 39fe8c8 | 2020-10-20 10:43:03 -0400 | [diff] [blame] | 19 | SkFont SkSVGText::resolveFont(const SkSVGRenderContext& ctx) const { |
| 20 | auto weight = [](const SkSVGFontWeight& w) { |
| 21 | switch (w.type()) { |
| 22 | case SkSVGFontWeight::Type::k100: return SkFontStyle::kThin_Weight; |
| 23 | case SkSVGFontWeight::Type::k200: return SkFontStyle::kExtraLight_Weight; |
| 24 | case SkSVGFontWeight::Type::k300: return SkFontStyle::kLight_Weight; |
| 25 | case SkSVGFontWeight::Type::k400: return SkFontStyle::kNormal_Weight; |
| 26 | case SkSVGFontWeight::Type::k500: return SkFontStyle::kMedium_Weight; |
| 27 | case SkSVGFontWeight::Type::k600: return SkFontStyle::kSemiBold_Weight; |
| 28 | case SkSVGFontWeight::Type::k700: return SkFontStyle::kBold_Weight; |
| 29 | case SkSVGFontWeight::Type::k800: return SkFontStyle::kExtraBold_Weight; |
| 30 | case SkSVGFontWeight::Type::k900: return SkFontStyle::kBlack_Weight; |
| 31 | case SkSVGFontWeight::Type::kNormal: return SkFontStyle::kNormal_Weight; |
| 32 | case SkSVGFontWeight::Type::kBold: return SkFontStyle::kBold_Weight; |
| 33 | case SkSVGFontWeight::Type::kBolder: return SkFontStyle::kExtraBold_Weight; |
| 34 | case SkSVGFontWeight::Type::kLighter: return SkFontStyle::kLight_Weight; |
| 35 | case SkSVGFontWeight::Type::kInherit: { |
| 36 | SkASSERT(false); |
| 37 | return SkFontStyle::kNormal_Weight; |
| 38 | } |
| 39 | } |
| 40 | SkUNREACHABLE; |
| 41 | }; |
| 42 | |
| 43 | auto slant = [](const SkSVGFontStyle& s) { |
| 44 | switch (s.type()) { |
| 45 | case SkSVGFontStyle::Type::kNormal: return SkFontStyle::kUpright_Slant; |
| 46 | case SkSVGFontStyle::Type::kItalic: return SkFontStyle::kItalic_Slant; |
| 47 | case SkSVGFontStyle::Type::kOblique: return SkFontStyle::kOblique_Slant; |
| 48 | case SkSVGFontStyle::Type::kInherit: { |
| 49 | SkASSERT(false); |
| 50 | return SkFontStyle::kUpright_Slant; |
| 51 | } |
| 52 | } |
| 53 | SkUNREACHABLE; |
| 54 | }; |
| 55 | |
| 56 | const auto& family = ctx.presentationContext().fInherited.fFontFamily->family(); |
| 57 | const SkFontStyle style(weight(*ctx.presentationContext().fInherited.fFontWeight), |
| 58 | SkFontStyle::kNormal_Width, |
| 59 | slant(*ctx.presentationContext().fInherited.fFontStyle)); |
| 60 | |
| 61 | const auto size = |
| 62 | ctx.lengthContext().resolve(ctx.presentationContext().fInherited.fFontSize->size(), |
| 63 | SkSVGLengthContext::LengthType::kVertical); |
| 64 | |
| 65 | // TODO: allow clients to pass an external fontmgr. |
| 66 | SkFont font(SkTypeface::MakeFromName(family.c_str(), style), size); |
| 67 | font.setHinting(SkFontHinting::kNone); |
| 68 | font.setSubpixel(true); |
| 69 | font.setLinearMetrics(true); |
| 70 | font.setBaselineSnap(false); |
| 71 | font.setEdging(SkFont::Edging::kAntiAlias); |
| 72 | |
| 73 | return font; |
| 74 | } |
| 75 | |
| 76 | void SkSVGText::onRender(const SkSVGRenderContext& ctx) const { |
| 77 | const auto font = this->resolveFont(ctx); |
| 78 | |
Florin Malita | 056385b | 2020-10-27 22:57:56 -0400 | [diff] [blame^] | 79 | const auto text_align = [](const SkSVGTextAnchor& anchor) { |
| 80 | switch (anchor.type()) { |
| 81 | case SkSVGTextAnchor::Type::kStart : return SkTextUtils::Align::kLeft_Align; |
| 82 | case SkSVGTextAnchor::Type::kMiddle: return SkTextUtils::Align::kCenter_Align; |
| 83 | case SkSVGTextAnchor::Type::kEnd : return SkTextUtils::Align::kRight_Align; |
| 84 | case SkSVGTextAnchor::Type::kInherit: |
| 85 | SkASSERT(false); |
| 86 | return SkTextUtils::Align::kLeft_Align; |
| 87 | } |
| 88 | SkUNREACHABLE; |
| 89 | }; |
| 90 | |
| 91 | const auto align = text_align(*ctx.presentationContext().fInherited.fTextAnchor); |
Florin Malita | 39fe8c8 | 2020-10-20 10:43:03 -0400 | [diff] [blame] | 92 | if (const SkPaint* fillPaint = ctx.fillPaint()) { |
| 93 | SkTextUtils::DrawString(ctx.canvas(), fText.c_str(), fX.value(), fY.value(), font, |
Florin Malita | 056385b | 2020-10-27 22:57:56 -0400 | [diff] [blame^] | 94 | *fillPaint, align); |
Florin Malita | 39fe8c8 | 2020-10-20 10:43:03 -0400 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | if (const SkPaint* strokePaint = ctx.strokePaint()) { |
| 98 | SkTextUtils::DrawString(ctx.canvas(), fText.c_str(), fX.value(), fY.value(), font, |
Florin Malita | 056385b | 2020-10-27 22:57:56 -0400 | [diff] [blame^] | 99 | *strokePaint, align); |
Florin Malita | 39fe8c8 | 2020-10-20 10:43:03 -0400 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | void SkSVGText::appendChild(sk_sp<SkSVGNode>) { |
| 104 | // TODO |
Xavier Phan | e29cdaf | 2020-03-26 16:15:14 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | SkPath SkSVGText::onAsPath(const SkSVGRenderContext& ctx) const { |
| 108 | SkPath path; |
| 109 | return path; |
| 110 | } |
| 111 | |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 112 | void SkSVGText::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) { |
Xavier Phan | e29cdaf | 2020-03-26 16:15:14 +0000 | [diff] [blame] | 113 | switch (attr) { |
| 114 | case SkSVGAttribute::kX: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 115 | if (const auto* x = v.as<SkSVGLengthValue>()) { |
Xavier Phan | e29cdaf | 2020-03-26 16:15:14 +0000 | [diff] [blame] | 116 | this->setX(*x); |
| 117 | } |
| 118 | break; |
| 119 | case SkSVGAttribute::kY: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 120 | if (const auto* y = v.as<SkSVGLengthValue>()) { |
Xavier Phan | e29cdaf | 2020-03-26 16:15:14 +0000 | [diff] [blame] | 121 | this->setY(*y); |
| 122 | } |
| 123 | break; |
| 124 | case SkSVGAttribute::kText: |
Florin Malita | f4403e7 | 2020-04-10 14:14:04 +0000 | [diff] [blame] | 125 | if (const auto* text = v.as<SkSVGStringValue>()) { |
Xavier Phan | e29cdaf | 2020-03-26 16:15:14 +0000 | [diff] [blame] | 126 | this->setText(*text); |
| 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 | } |