| 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 "modules/skottie/src/text/TextValue.h" |
| 9 | |
| 10 | #include "modules/skottie/src/SkottieJson.h" |
| 11 | #include "modules/skottie/src/SkottiePriv.h" |
| 12 | #include "modules/skottie/src/SkottieValue.h" |
| 13 | |
| 14 | namespace skottie::internal { |
| 15 | |
| 16 | bool Parse(const skjson::Value& jv, const internal::AnimationBuilder& abuilder, TextValue* v) { |
| 17 | const skjson::ObjectValue* jtxt = jv; |
| 18 | if (!jtxt) { |
| 19 | return false; |
| 20 | } |
| 21 | |
| 22 | const skjson::StringValue* font_name = (*jtxt)["f" ]; |
| 23 | const skjson::StringValue* text = (*jtxt)["t" ]; |
| 24 | const skjson::NumberValue* text_size = (*jtxt)["s" ]; |
| 25 | const skjson::NumberValue* line_height = (*jtxt)["lh" ]; |
| 26 | if (!font_name || !text || !text_size || !line_height) { |
| 27 | return false; |
| 28 | } |
| 29 | |
| 30 | const auto* font = abuilder.findFont(SkString(font_name->begin(), font_name->size())); |
| 31 | if (!font) { |
| 32 | abuilder.log(Logger::Level::kError, nullptr, "Unknown font: \"%s\"." , font_name->begin()); |
| 33 | return false; |
| 34 | } |
| 35 | |
| 36 | v->fText.set(text->begin(), text->size()); |
| 37 | v->fTextSize = **text_size; |
| 38 | v->fLineHeight = **line_height; |
| 39 | v->fTypeface = font->fTypeface; |
| 40 | v->fAscent = font->fAscentPct * -0.01f * v->fTextSize; // negative ascent per SkFontMetrics |
| 41 | |
| 42 | static constexpr SkTextUtils::Align gAlignMap[] = { |
| 43 | SkTextUtils::kLeft_Align, // 'j': 0 |
| 44 | SkTextUtils::kRight_Align, // 'j': 1 |
| 45 | SkTextUtils::kCenter_Align // 'j': 2 |
| 46 | }; |
| 47 | v->fHAlign = gAlignMap[std::min<size_t>(ParseDefault<size_t>((*jtxt)["j" ], 0), |
| 48 | SK_ARRAY_COUNT(gAlignMap))]; |
| 49 | |
| 50 | // Optional text box size. |
| 51 | if (const skjson::ArrayValue* jsz = (*jtxt)["sz" ]) { |
| 52 | if (jsz->size() == 2) { |
| 53 | v->fBox.setWH(ParseDefault<SkScalar>((*jsz)[0], 0), |
| 54 | ParseDefault<SkScalar>((*jsz)[1], 0)); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // Optional text box position. |
| 59 | if (const skjson::ArrayValue* jps = (*jtxt)["ps" ]) { |
| 60 | if (jps->size() == 2) { |
| 61 | v->fBox.offset(ParseDefault<SkScalar>((*jps)[0], 0), |
| 62 | ParseDefault<SkScalar>((*jps)[1], 0)); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | static constexpr Shaper::ResizePolicy gResizeMap[] = { |
| 67 | Shaper::ResizePolicy::kNone, // 'rs': 0 |
| 68 | Shaper::ResizePolicy::kScaleToFit, // 'rs': 1 |
| 69 | Shaper::ResizePolicy::kDownscaleToFit, // 'rs': 2 |
| 70 | }; |
| 71 | // TODO: remove "sk_rs" support after migrating clients. |
| 72 | v->fResize = gResizeMap[std::min(std::max(ParseDefault<size_t>((*jtxt)[ "rs" ], 0), |
| 73 | ParseDefault<size_t>((*jtxt)["sk_rs" ], 0)), |
| 74 | SK_ARRAY_COUNT(gResizeMap))]; |
| 75 | |
| 76 | // In point mode, the text is baseline-aligned. |
| 77 | v->fVAlign = v->fBox.isEmpty() ? Shaper::VAlign::kTopBaseline |
| 78 | : Shaper::VAlign::kTop; |
| 79 | |
| 80 | static constexpr Shaper::VAlign gVAlignMap[] = { |
| 81 | Shaper::VAlign::kVisualTop, // 'vj': 0 |
| 82 | Shaper::VAlign::kVisualCenter, // 'vj': 1 |
| 83 | Shaper::VAlign::kVisualBottom, // 'vj': 2 |
| 84 | }; |
| 85 | size_t vj; |
| 86 | if (skottie::Parse((*jtxt)[ "vj" ], &vj) || |
| 87 | skottie::Parse((*jtxt)["sk_vj" ], &vj)) { // TODO: remove after migrating clients. |
| 88 | if (vj < SK_ARRAY_COUNT(gVAlignMap)) { |
| 89 | v->fVAlign = gVAlignMap[vj]; |
| 90 | } else { |
| 91 | // Legacy sk_vj values. |
| 92 | // TODO: remove after clients update. |
| 93 | switch (vj) { |
| 94 | case 3: |
| 95 | // 'sk_vj': 3 -> kVisualCenter/kScaleToFit |
| 96 | v->fVAlign = Shaper::VAlign::kVisualCenter; |
| 97 | v->fResize = Shaper::ResizePolicy::kScaleToFit; |
| 98 | break; |
| 99 | case 4: |
| 100 | // 'sk_vj': 4 -> kVisualCenter/kDownscaleToFit |
| 101 | v->fVAlign = Shaper::VAlign::kVisualCenter; |
| 102 | v->fResize = Shaper::ResizePolicy::kDownscaleToFit; |
| 103 | break; |
| 104 | default: |
| 105 | abuilder.log(Logger::Level::kWarning, nullptr, |
| 106 | "Ignoring unknown 'vj' value: %zu" , vj); |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | if (v->fResize != Shaper::ResizePolicy::kNone && v->fBox.isEmpty()) { |
| 113 | abuilder.log(Logger::Level::kWarning, jtxt, "Auto-scaled text requires a paragraph box." ); |
| 114 | v->fResize = Shaper::ResizePolicy::kNone; |
| 115 | } |
| 116 | |
| 117 | const auto& parse_color = [] (const skjson::ArrayValue* jcolor, |
| 118 | SkColor* c) { |
| 119 | if (!jcolor) { |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | VectorValue color_vec; |
| 124 | if (!skottie::Parse(*jcolor, &color_vec)) { |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | *c = color_vec; |
| 129 | return true; |
| 130 | }; |
| 131 | |
| 132 | v->fHasFill = parse_color((*jtxt)["fc" ], &v->fFillColor); |
| 133 | v->fHasStroke = parse_color((*jtxt)["sc" ], &v->fStrokeColor); |
| 134 | |
| 135 | if (v->fHasStroke) { |
| 136 | v->fStrokeWidth = ParseDefault((*jtxt)["sw" ], 1.0f); |
| 137 | v->fPaintOrder = ParseDefault((*jtxt)["of" ], true) |
| 138 | ? TextPaintOrder::kFillStroke |
| 139 | : TextPaintOrder::kStrokeFill; |
| 140 | } |
| 141 | |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | } // namespace skottie::internal |
| 146 | |