Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google LLC |
| 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 "include/core/SkColor.h" |
Kevin Lubick | d3b1fe6 | 2019-10-21 10:50:26 -0400 | [diff] [blame] | 9 | #include "include/core/SkFontStyle.h" |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 10 | #include "include/core/SkString.h" |
| 11 | |
| 12 | #include "modules/skparagraph/include/DartTypes.h" |
| 13 | #include "modules/skparagraph/include/Paragraph.h" |
| 14 | #include "modules/skparagraph/include/ParagraphBuilder.h" |
| 15 | #include "modules/skparagraph/include/TextStyle.h" |
Harry Terkelsen | 10f019c | 2020-08-04 13:21:09 -0700 | [diff] [blame] | 16 | #include "modules/skparagraph/include/TypefaceFontProvider.h" |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 17 | #include "modules/skparagraph/src/ParagraphBuilderImpl.h" |
| 18 | #include "modules/skparagraph/src/ParagraphImpl.h" |
| 19 | |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include <emscripten.h> |
| 24 | #include <emscripten/bind.h> |
Nathaniel Nifong | e5d3254 | 2020-03-26 09:27:48 -0400 | [diff] [blame] | 25 | #include "modules/canvaskit/WasmCommon.h" |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 26 | |
| 27 | using namespace emscripten; |
| 28 | |
| 29 | namespace para = skia::textlayout; |
| 30 | |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 31 | SkColor4f toSkColor4f(uintptr_t /* float* */ cPtr) { |
| 32 | float* fourFloats = reinterpret_cast<float*>(cPtr); |
Harry Terkelsen | 223ffcd | 2020-10-02 15:24:13 -0700 | [diff] [blame] | 33 | SkColor4f color = {fourFloats[0], fourFloats[1], fourFloats[2], fourFloats[3]}; |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 34 | return color; |
| 35 | } |
| 36 | |
Kevin Lubick | d3b1fe6 | 2019-10-21 10:50:26 -0400 | [diff] [blame] | 37 | struct SimpleFontStyle { |
Harry Terkelsen | 223ffcd | 2020-10-02 15:24:13 -0700 | [diff] [blame] | 38 | SkFontStyle::Slant slant; |
Kevin Lubick | d3b1fe6 | 2019-10-21 10:50:26 -0400 | [diff] [blame] | 39 | SkFontStyle::Weight weight; |
Harry Terkelsen | 223ffcd | 2020-10-02 15:24:13 -0700 | [diff] [blame] | 40 | SkFontStyle::Width width; |
Kevin Lubick | d3b1fe6 | 2019-10-21 10:50:26 -0400 | [diff] [blame] | 41 | }; |
| 42 | |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 43 | struct SimpleTextStyle { |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 44 | uintptr_t /* float* */ colorPtr; |
| 45 | uintptr_t /* float* */ foregroundColorPtr; |
| 46 | uintptr_t /* float* */ backgroundColorPtr; |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 47 | uint8_t decoration; |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 48 | SkScalar decorationThickness; |
Harry Terkelsen | 223ffcd | 2020-10-02 15:24:13 -0700 | [diff] [blame] | 49 | uintptr_t /* float* */ decorationColorPtr; |
| 50 | para::TextDecorationStyle decorationStyle; |
| 51 | para::TextBaseline textBaseline; |
Kevin Lubick | d3b1fe6 | 2019-10-21 10:50:26 -0400 | [diff] [blame] | 52 | SkScalar fontSize; |
Harry Terkelsen | 223ffcd | 2020-10-02 15:24:13 -0700 | [diff] [blame] | 53 | SkScalar letterSpacing; |
| 54 | SkScalar wordSpacing; |
| 55 | SkScalar heightMultiplier; |
| 56 | uintptr_t /* const char* */ localePtr; |
| 57 | int localeLen; |
Kevin Lubick | d3b1fe6 | 2019-10-21 10:50:26 -0400 | [diff] [blame] | 58 | SimpleFontStyle fontStyle; |
Kevin Lubick | d3b1fe6 | 2019-10-21 10:50:26 -0400 | [diff] [blame] | 59 | |
Kevin Lubick | 0c8884b | 2020-05-14 08:27:53 -0400 | [diff] [blame] | 60 | uintptr_t /* const char** */ fontFamiliesPtr; |
| 61 | int fontFamiliesLen; |
Harry Terkelsen | 223ffcd | 2020-10-02 15:24:13 -0700 | [diff] [blame] | 62 | |
| 63 | int shadowLen; |
| 64 | uintptr_t /* SkColor4f* */ shadowColorsPtr; |
| 65 | uintptr_t /* SkPoint* */ shadowOffsetsPtr; |
| 66 | uintptr_t /* float* */ shadowBlurRadiiPtr; |
| 67 | |
| 68 | int fontFeatureLen; |
| 69 | uintptr_t /* float* */ fontFeatureNamesPtr; |
| 70 | uintptr_t /* float* */ fontFeatureValuesPtr; |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 71 | }; |
| 72 | |
Harry Terkelsen | 223ffcd | 2020-10-02 15:24:13 -0700 | [diff] [blame] | 73 | struct SimpleStrutStyle { |
| 74 | uintptr_t /* const char** */ fontFamiliesPtr; |
| 75 | int fontFamiliesLen; |
| 76 | SimpleFontStyle fontStyle; |
| 77 | SkScalar fontSize; |
| 78 | SkScalar heightMultiplier; |
| 79 | SkScalar leading; |
| 80 | bool strutEnabled; |
| 81 | bool forceStrutHeight; |
| 82 | }; |
| 83 | |
| 84 | para::StrutStyle toStrutStyle(const SimpleStrutStyle& s) { |
| 85 | para::StrutStyle ss; |
| 86 | |
| 87 | const char** fontFamilies = reinterpret_cast<const char**>(s.fontFamiliesPtr); |
| 88 | if (fontFamilies != nullptr) { |
| 89 | std::vector<SkString> ff; |
| 90 | for (int i = 0; i < s.fontFamiliesLen; i++) { |
| 91 | ff.emplace_back(fontFamilies[i]); |
| 92 | } |
| 93 | ss.setFontFamilies(ff); |
| 94 | } |
| 95 | |
| 96 | SkFontStyle fs(s.fontStyle.weight, s.fontStyle.width, s.fontStyle.slant); |
| 97 | ss.setFontStyle(fs); |
| 98 | |
| 99 | if (s.fontSize != 0) { |
| 100 | ss.setFontSize(s.fontSize); |
| 101 | } |
| 102 | if (s.heightMultiplier != 0) { |
| 103 | ss.setHeight(s.heightMultiplier); |
| 104 | ss.setHeightOverride(true); |
| 105 | } |
| 106 | if (s.leading != 0) { |
| 107 | ss.setLeading(s.leading); |
| 108 | } |
| 109 | |
| 110 | ss.setStrutEnabled(s.strutEnabled); |
| 111 | ss.setForceStrutHeight(s.forceStrutHeight); |
| 112 | |
| 113 | return ss; |
| 114 | } |
| 115 | |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 116 | para::TextStyle toTextStyle(const SimpleTextStyle& s) { |
| 117 | para::TextStyle ts; |
Nathaniel Nifong | e5d3254 | 2020-03-26 09:27:48 -0400 | [diff] [blame] | 118 | |
Harry Terkelsen | 223ffcd | 2020-10-02 15:24:13 -0700 | [diff] [blame] | 119 | // textstyle.color doesn't support a 4f color, however the foreground and background fields |
| 120 | // below do. |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 121 | ts.setColor(toSkColor4f(s.colorPtr).toSkColor()); |
Nathaniel Nifong | e5d3254 | 2020-03-26 09:27:48 -0400 | [diff] [blame] | 122 | |
Nathaniel Nifong | e5d3254 | 2020-03-26 09:27:48 -0400 | [diff] [blame] | 123 | // It is functionally important that these paints be unset when no value was provided. |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 124 | if (s.foregroundColorPtr) { |
Nathaniel Nifong | e5d3254 | 2020-03-26 09:27:48 -0400 | [diff] [blame] | 125 | SkPaint p1; |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 126 | p1.setColor4f(toSkColor4f(s.foregroundColorPtr)); |
Nathaniel Nifong | e5d3254 | 2020-03-26 09:27:48 -0400 | [diff] [blame] | 127 | ts.setForegroundColor(p1); |
Robert Phillips | cb77eab | 2020-03-24 14:19:40 +0000 | [diff] [blame] | 128 | } |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 129 | |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 130 | if (s.backgroundColorPtr) { |
Nathaniel Nifong | e5d3254 | 2020-03-26 09:27:48 -0400 | [diff] [blame] | 131 | SkPaint p2; |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 132 | p2.setColor4f(toSkColor4f(s.backgroundColorPtr)); |
Nathaniel Nifong | e5d3254 | 2020-03-26 09:27:48 -0400 | [diff] [blame] | 133 | ts.setBackgroundColor(p2); |
Robert Phillips | cb77eab | 2020-03-24 14:19:40 +0000 | [diff] [blame] | 134 | } |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 135 | |
| 136 | if (s.fontSize != 0) { |
| 137 | ts.setFontSize(s.fontSize); |
| 138 | } |
Harry Terkelsen | 223ffcd | 2020-10-02 15:24:13 -0700 | [diff] [blame] | 139 | if (s.letterSpacing != 0) { |
| 140 | ts.setLetterSpacing(s.letterSpacing); |
| 141 | } |
| 142 | if (s.wordSpacing != 0) { |
| 143 | ts.setWordSpacing(s.wordSpacing); |
| 144 | } |
| 145 | |
| 146 | if (s.heightMultiplier != 0) { |
| 147 | ts.setHeight(s.heightMultiplier); |
| 148 | ts.setHeightOverride(true); |
| 149 | } |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 150 | |
| 151 | ts.setDecoration(para::TextDecoration(s.decoration)); |
Harry Terkelsen | 223ffcd | 2020-10-02 15:24:13 -0700 | [diff] [blame] | 152 | ts.setDecorationStyle(s.decorationStyle); |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 153 | if (s.decorationThickness != 0) { |
| 154 | ts.setDecorationThicknessMultiplier(s.decorationThickness); |
| 155 | } |
Harry Terkelsen | 223ffcd | 2020-10-02 15:24:13 -0700 | [diff] [blame] | 156 | if (s.decorationColorPtr) { |
| 157 | ts.setDecorationColor(toSkColor4f(s.decorationColorPtr).toSkColor()); |
| 158 | } |
| 159 | |
| 160 | if (s.localeLen > 0) { |
| 161 | const char* localePtr = reinterpret_cast<const char*>(s.localePtr); |
| 162 | SkString lStr(localePtr, s.localeLen); |
| 163 | ts.setLocale(lStr); |
| 164 | } |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 165 | |
Kevin Lubick | 0c8884b | 2020-05-14 08:27:53 -0400 | [diff] [blame] | 166 | const char** fontFamilies = reinterpret_cast<const char**>(s.fontFamiliesPtr); |
Harry Terkelsen | 223ffcd | 2020-10-02 15:24:13 -0700 | [diff] [blame] | 167 | if (fontFamilies != nullptr) { |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 168 | std::vector<SkString> ff; |
Kevin Lubick | 0c8884b | 2020-05-14 08:27:53 -0400 | [diff] [blame] | 169 | for (int i = 0; i < s.fontFamiliesLen; i++) { |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 170 | ff.emplace_back(fontFamilies[i]); |
| 171 | } |
| 172 | ts.setFontFamilies(ff); |
| 173 | } |
| 174 | |
Harry Terkelsen | 223ffcd | 2020-10-02 15:24:13 -0700 | [diff] [blame] | 175 | ts.setTextBaseline(s.textBaseline); |
| 176 | |
Kevin Lubick | d3b1fe6 | 2019-10-21 10:50:26 -0400 | [diff] [blame] | 177 | SkFontStyle fs(s.fontStyle.weight, s.fontStyle.width, s.fontStyle.slant); |
| 178 | ts.setFontStyle(fs); |
| 179 | |
Harry Terkelsen | 223ffcd | 2020-10-02 15:24:13 -0700 | [diff] [blame] | 180 | if (s.shadowLen > 0) { |
| 181 | const SkColor4f* colors = reinterpret_cast<const SkColor4f*>(s.shadowColorsPtr); |
| 182 | const SkPoint* offsets = reinterpret_cast<const SkPoint*>(s.shadowOffsetsPtr); |
| 183 | const float* blurRadii = reinterpret_cast<const float*>(s.shadowBlurRadiiPtr); |
| 184 | for (int i = 0; i < s.shadowLen; i++) { |
| 185 | para::TextShadow shadow(colors[i].toSkColor(), offsets[i], blurRadii[i]); |
| 186 | ts.addShadow(shadow); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | |
| 191 | if (s.fontFeatureLen > 0) { |
| 192 | const char** fontFeatureNames = reinterpret_cast<const char**>(s.fontFeatureNamesPtr); |
| 193 | const int* fontFeatureValues = reinterpret_cast<const int*>(s.fontFeatureValuesPtr); |
| 194 | for (int i = 0; i < s.fontFeatureLen; i++) { |
| 195 | // Font features names are 4-character simple strings. |
| 196 | SkString name(fontFeatureNames[i], 4); |
| 197 | ts.addFontFeature(name, fontFeatureValues[i]); |
| 198 | } |
| 199 | } |
| 200 | |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 201 | return ts; |
| 202 | } |
| 203 | |
| 204 | struct SimpleParagraphStyle { |
Kevin Lubick | d3b1fe6 | 2019-10-21 10:50:26 -0400 | [diff] [blame] | 205 | bool disableHinting; |
| 206 | uintptr_t /* const char* */ ellipsisPtr; |
| 207 | size_t ellipsisLen; |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 208 | SkScalar heightMultiplier; |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 209 | size_t maxLines; |
Kevin Lubick | d3b1fe6 | 2019-10-21 10:50:26 -0400 | [diff] [blame] | 210 | para::TextAlign textAlign; |
| 211 | para::TextDirection textDirection; |
| 212 | SimpleTextStyle textStyle; |
Harry Terkelsen | 223ffcd | 2020-10-02 15:24:13 -0700 | [diff] [blame] | 213 | SimpleStrutStyle strutStyle; |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | para::ParagraphStyle toParagraphStyle(const SimpleParagraphStyle& s) { |
| 217 | para::ParagraphStyle ps; |
Kevin Lubick | d3b1fe6 | 2019-10-21 10:50:26 -0400 | [diff] [blame] | 218 | if (s.disableHinting) { |
| 219 | ps.turnHintingOff(); |
| 220 | } |
| 221 | |
| 222 | if (s.ellipsisLen > 0) { |
| 223 | const char* ellipsisPtr = reinterpret_cast<const char*>(s.ellipsisPtr); |
| 224 | SkString eStr(ellipsisPtr, s.ellipsisLen); |
| 225 | ps.setEllipsis(eStr); |
| 226 | } |
| 227 | ps.setTextAlign(s.textAlign); |
| 228 | ps.setTextDirection(s.textDirection); |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 229 | auto ts = toTextStyle(s.textStyle); |
| 230 | ps.setTextStyle(ts); |
Harry Terkelsen | 223ffcd | 2020-10-02 15:24:13 -0700 | [diff] [blame] | 231 | auto ss = toStrutStyle(s.strutStyle); |
| 232 | ps.setStrutStyle(ss); |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 233 | if (s.heightMultiplier != 0) { |
| 234 | ps.setHeight(s.heightMultiplier); |
| 235 | } |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 236 | if (s.maxLines != 0) { |
| 237 | ps.setMaxLines(s.maxLines); |
| 238 | } |
| 239 | return ps; |
| 240 | } |
| 241 | |
Kevin Lubick | 4a5f4f2 | 2019-11-20 08:27:10 -0500 | [diff] [blame] | 242 | struct SimpleTextBox { |
| 243 | SkRect rect; |
| 244 | // This isn't the most efficient way to represent this, but it is much easier to keep |
| 245 | // everything as floats when unpacking on the JS side. |
| 246 | // 0.0 = RTL, 1.0 = LTr |
| 247 | SkScalar direction; |
| 248 | }; |
| 249 | |
Harry Terkelsen | 223ffcd | 2020-10-02 15:24:13 -0700 | [diff] [blame] | 250 | Float32Array TextBoxesToFloat32Array(std::vector<para::TextBox> boxes) { |
Kevin Lubick | 4a5f4f2 | 2019-11-20 08:27:10 -0500 | [diff] [blame] | 251 | // Pack these text boxes into an array of n groups of 5 SkScalar (floats) |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 252 | if (!boxes.size()) { |
| 253 | return emscripten::val::null(); |
| 254 | } |
Kevin Lubick | 4a5f4f2 | 2019-11-20 08:27:10 -0500 | [diff] [blame] | 255 | SimpleTextBox* rects = new SimpleTextBox[boxes.size()]; |
Harry Terkelsen | 223ffcd | 2020-10-02 15:24:13 -0700 | [diff] [blame] | 256 | for (int i = 0; i < boxes.size(); i++) { |
Kevin Lubick | 4a5f4f2 | 2019-11-20 08:27:10 -0500 | [diff] [blame] | 257 | rects[i].rect = boxes[i].rect; |
| 258 | if (boxes[i].direction == para::TextDirection::kRtl) { |
| 259 | rects[i].direction = 0; |
| 260 | } else { |
| 261 | rects[i].direction = 1; |
| 262 | } |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 263 | } |
| 264 | float* fPtr = reinterpret_cast<float*>(rects); |
| 265 | // Of note: now that we have cast rects to float*, emscripten is smart enough to wrap this |
| 266 | // into a Float32Array for us. |
Harry Terkelsen | 223ffcd | 2020-10-02 15:24:13 -0700 | [diff] [blame] | 267 | return Float32Array(typed_memory_view(boxes.size() * 5, fPtr)); |
| 268 | } |
| 269 | |
Kevin Lubick | a96e080 | 2020-10-08 15:58:12 -0400 | [diff] [blame] | 270 | Float32Array GetRectsForRange(para::Paragraph& self, |
Harry Terkelsen | 223ffcd | 2020-10-02 15:24:13 -0700 | [diff] [blame] | 271 | unsigned start, |
| 272 | unsigned end, |
| 273 | para::RectHeightStyle heightStyle, |
| 274 | para::RectWidthStyle widthStyle) { |
| 275 | std::vector<para::TextBox> boxes = self.getRectsForRange(start, end, heightStyle, widthStyle); |
| 276 | return TextBoxesToFloat32Array(boxes); |
| 277 | } |
| 278 | |
Kevin Lubick | a96e080 | 2020-10-08 15:58:12 -0400 | [diff] [blame] | 279 | Float32Array GetRectsForPlaceholders(para::Paragraph& self) { |
Harry Terkelsen | 223ffcd | 2020-10-02 15:24:13 -0700 | [diff] [blame] | 280 | std::vector<para::TextBox> boxes = self.getRectsForPlaceholders(); |
| 281 | return TextBoxesToFloat32Array(boxes); |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 282 | } |
| 283 | |
Kevin Lubick | e677f4a | 2020-11-04 09:46:22 -0500 | [diff] [blame] | 284 | JSArray GetLineMetrics(para::Paragraph& self) { |
| 285 | std::vector<skia::textlayout::LineMetrics> metrics; |
| 286 | self.getLineMetrics(metrics); |
| 287 | JSArray result = emscripten::val::array(); |
| 288 | for (auto metric : metrics) { |
| 289 | JSObject m = emscripten::val::object(); |
| 290 | m.set("startIndex", metric.fStartIndex); |
| 291 | m.set("endIndex", metric.fEndIndex); |
| 292 | m.set("endExcludingWhitespaces", metric.fEndExcludingWhitespaces); |
| 293 | m.set("endIncludingNewline", metric.fEndIncludingNewline); |
| 294 | m.set("isHardBreak", metric.fHardBreak); |
| 295 | m.set("ascent", metric.fAscent); |
| 296 | m.set("descent", metric.fDescent); |
| 297 | m.set("height", metric.fHeight); |
| 298 | m.set("width", metric.fWidth); |
| 299 | m.set("left", metric.fLeft); |
| 300 | m.set("baseline", metric.fBaseline); |
| 301 | m.set("lineNumber", metric.fLineNumber); |
| 302 | result.call<void>("push", m); |
| 303 | } |
| 304 | return result; |
| 305 | } |
| 306 | |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 307 | EMSCRIPTEN_BINDINGS(Paragraph) { |
| 308 | |
Kevin Lubick | a96e080 | 2020-10-08 15:58:12 -0400 | [diff] [blame] | 309 | class_<para::Paragraph>("Paragraph") |
Kevin Lubick | 0491267 | 2019-11-15 14:48:55 -0500 | [diff] [blame] | 310 | .function("didExceedMaxLines", ¶::Paragraph::didExceedMaxLines) |
| 311 | .function("getAlphabeticBaseline", ¶::Paragraph::getAlphabeticBaseline) |
Kevin Lubick | a96e080 | 2020-10-08 15:58:12 -0400 | [diff] [blame] | 312 | .function("getGlyphPositionAtCoordinate", ¶::Paragraph::getGlyphPositionAtCoordinate) |
Kevin Lubick | 0491267 | 2019-11-15 14:48:55 -0500 | [diff] [blame] | 313 | .function("getHeight", ¶::Paragraph::getHeight) |
| 314 | .function("getIdeographicBaseline", ¶::Paragraph::getIdeographicBaseline) |
Kevin Lubick | e677f4a | 2020-11-04 09:46:22 -0500 | [diff] [blame] | 315 | .function("getLineMetrics", &GetLineMetrics) |
Kevin Lubick | 0491267 | 2019-11-15 14:48:55 -0500 | [diff] [blame] | 316 | .function("getLongestLine", ¶::Paragraph::getLongestLine) |
| 317 | .function("getMaxIntrinsicWidth", ¶::Paragraph::getMaxIntrinsicWidth) |
| 318 | .function("getMaxWidth", ¶::Paragraph::getMaxWidth) |
| 319 | .function("getMinIntrinsicWidth", ¶::Paragraph::getMinIntrinsicWidth) |
Harry Terkelsen | 223ffcd | 2020-10-02 15:24:13 -0700 | [diff] [blame] | 320 | .function("_getRectsForPlaceholders", &GetRectsForPlaceholders) |
Kevin Lubick | 0516281 | 2020-10-05 14:11:28 -0400 | [diff] [blame] | 321 | .function("_getRectsForRange", &GetRectsForRange) |
Kevin Lubick | a96e080 | 2020-10-08 15:58:12 -0400 | [diff] [blame] | 322 | .function("getWordBoundary", ¶::Paragraph::getWordBoundary) |
| 323 | .function("layout", ¶::Paragraph::layout); |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 324 | |
| 325 | class_<para::ParagraphBuilderImpl>("ParagraphBuilder") |
Harry Terkelsen | 223ffcd | 2020-10-02 15:24:13 -0700 | [diff] [blame] | 326 | .class_function( |
| 327 | "_Make", |
| 328 | optional_override([](SimpleParagraphStyle style, sk_sp<SkFontMgr> fontMgr) |
| 329 | -> std::unique_ptr<para::ParagraphBuilderImpl> { |
| 330 | auto fc = sk_make_sp<para::FontCollection>(); |
| 331 | fc->setDefaultFontManager(fontMgr); |
| 332 | fc->enableFontFallback(); |
| 333 | auto ps = toParagraphStyle(style); |
| 334 | auto pb = para::ParagraphBuilderImpl::make(ps, fc); |
| 335 | return std::unique_ptr<para::ParagraphBuilderImpl>( |
| 336 | static_cast<para::ParagraphBuilderImpl*>(pb.release())); |
| 337 | }), |
| 338 | allow_raw_pointers()) |
| 339 | .class_function( |
| 340 | "_MakeFromFontProvider", |
| 341 | optional_override([](SimpleParagraphStyle style, |
| 342 | sk_sp<para::TypefaceFontProvider> fontProvider) |
| 343 | -> std::unique_ptr<para::ParagraphBuilderImpl> { |
| 344 | auto fc = sk_make_sp<para::FontCollection>(); |
| 345 | fc->setDefaultFontManager(fontProvider); |
| 346 | fc->enableFontFallback(); |
| 347 | auto ps = toParagraphStyle(style); |
| 348 | auto pb = para::ParagraphBuilderImpl::make(ps, fc); |
| 349 | return std::unique_ptr<para::ParagraphBuilderImpl>( |
| 350 | static_cast<para::ParagraphBuilderImpl*>(pb.release())); |
| 351 | }), |
| 352 | allow_raw_pointers()) |
| 353 | .function("addText", |
| 354 | optional_override([](para::ParagraphBuilderImpl& self, std::string text) { |
| 355 | return self.addText(text.c_str(), text.length()); |
| 356 | })) |
| 357 | .function("build", ¶::ParagraphBuilderImpl::Build, allow_raw_pointers()) |
| 358 | .function("pop", ¶::ParagraphBuilderImpl::pop) |
| 359 | .function("_pushStyle", optional_override([](para::ParagraphBuilderImpl& self, |
| 360 | SimpleTextStyle textStyle) { |
| 361 | auto ts = toTextStyle(textStyle); |
| 362 | self.pushStyle(ts); |
| 363 | })) |
| 364 | // A method of pushing a textStyle with paints instead of colors for foreground and |
| 365 | // background. Since SimpleTextStyle is a value object, it cannot contain paints, which |
| 366 | // are not primitives. This binding is here to accept them. Any color that is specified |
| 367 | // in the textStyle is overridden. |
| 368 | .function("_pushPaintStyle", |
| 369 | optional_override([](para::ParagraphBuilderImpl& self, |
| 370 | SimpleTextStyle textStyle, SkPaint foreground, |
| 371 | SkPaint background) { |
| 372 | auto ts = toTextStyle(textStyle); |
| 373 | ts.setForegroundColor(foreground); |
| 374 | ts.setBackgroundColor(background); |
| 375 | self.pushStyle(ts); |
| 376 | })) |
| 377 | .function("_addPlaceholder", optional_override([](para::ParagraphBuilderImpl& self, |
| 378 | SkScalar width, |
| 379 | SkScalar height, |
| 380 | para::PlaceholderAlignment alignment, |
| 381 | para::TextBaseline baseline, |
| 382 | SkScalar offset) { |
| 383 | para::PlaceholderStyle ps(width, height, alignment, baseline, offset); |
| 384 | self.addPlaceholder(ps); |
| 385 | })); |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 386 | |
Harry Terkelsen | 10f019c | 2020-08-04 13:21:09 -0700 | [diff] [blame] | 387 | class_<para::TypefaceFontProvider, base<SkFontMgr>>("TypefaceFontProvider") |
| 388 | .smart_ptr<sk_sp<para::TypefaceFontProvider>>("sk_sp<TypefaceFontProvider>") |
| 389 | .class_function("Make", optional_override([]()-> sk_sp<para::TypefaceFontProvider> { |
| 390 | return sk_make_sp<para::TypefaceFontProvider>(); |
| 391 | })) |
| 392 | .function("_registerFont", optional_override([](para::TypefaceFontProvider& self, |
| 393 | sk_sp<SkTypeface> typeface, |
| 394 | uintptr_t familyPtr) { |
| 395 | const char* fPtr = reinterpret_cast<const char*>(familyPtr); |
| 396 | SkString fStr(fPtr); |
| 397 | self.registerTypeface(typeface, fStr); |
| 398 | }), allow_raw_pointers()); |
| 399 | |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 400 | |
| 401 | enum_<para::Affinity>("Affinity") |
| 402 | .value("Upstream", para::Affinity::kUpstream) |
| 403 | .value("Downstream", para::Affinity::kDownstream); |
| 404 | |
Kevin Lubick | c488fd3 | 2020-10-06 08:24:00 -0400 | [diff] [blame] | 405 | enum_<para::TextDecorationStyle>("DecorationStyle") |
| 406 | .value("Solid", para::TextDecorationStyle::kSolid) |
| 407 | .value("Double", para::TextDecorationStyle::kDouble) |
| 408 | .value("Dotted", para::TextDecorationStyle::kDotted) |
| 409 | .value("Dashed", para::TextDecorationStyle::kDashed) |
| 410 | .value("Wavy", para::TextDecorationStyle::kWavy); |
| 411 | |
Kevin Lubick | d3b1fe6 | 2019-10-21 10:50:26 -0400 | [diff] [blame] | 412 | enum_<SkFontStyle::Slant>("FontSlant") |
| 413 | .value("Upright", SkFontStyle::Slant::kUpright_Slant) |
| 414 | .value("Italic", SkFontStyle::Slant::kItalic_Slant) |
| 415 | .value("Oblique", SkFontStyle::Slant::kOblique_Slant); |
| 416 | |
| 417 | enum_<SkFontStyle::Weight>("FontWeight") |
| 418 | .value("Invisible", SkFontStyle::Weight::kInvisible_Weight) |
| 419 | .value("Thin", SkFontStyle::Weight::kThin_Weight) |
| 420 | .value("ExtraLight", SkFontStyle::Weight::kExtraLight_Weight) |
| 421 | .value("Light", SkFontStyle::Weight::kLight_Weight) |
| 422 | .value("Normal", SkFontStyle::Weight::kNormal_Weight) |
| 423 | .value("Medium", SkFontStyle::Weight::kMedium_Weight) |
| 424 | .value("SemiBold", SkFontStyle::Weight::kSemiBold_Weight) |
| 425 | .value("Bold", SkFontStyle::Weight::kBold_Weight) |
| 426 | .value("ExtraBold", SkFontStyle::Weight::kExtraBold_Weight) |
| 427 | .value("Black" , SkFontStyle::Weight::kBlack_Weight) |
| 428 | .value("ExtraBlack", SkFontStyle::Weight::kExtraBlack_Weight); |
| 429 | |
| 430 | enum_<SkFontStyle::Width>("FontWidth") |
| 431 | .value("UltraCondensed", SkFontStyle::Width::kUltraCondensed_Width) |
| 432 | .value("ExtraCondensed", SkFontStyle::Width::kExtraCondensed_Width) |
| 433 | .value("Condensed", SkFontStyle::Width::kCondensed_Width) |
| 434 | .value("SemiCondensed", SkFontStyle::Width::kSemiCondensed_Width) |
| 435 | .value("Normal", SkFontStyle::Width::kNormal_Width) |
| 436 | .value("SemiExpanded", SkFontStyle::Width::kSemiExpanded_Width) |
| 437 | .value("Expanded", SkFontStyle::Width::kExpanded_Width) |
| 438 | .value("ExtraExpanded", SkFontStyle::Width::kExtraExpanded_Width) |
| 439 | .value("UltraExpanded", SkFontStyle::Width::kUltraExpanded_Width); |
| 440 | |
Kevin Lubick | c488fd3 | 2020-10-06 08:24:00 -0400 | [diff] [blame] | 441 | enum_<para::PlaceholderAlignment>("PlaceholderAlignment") |
| 442 | .value("Baseline", para::PlaceholderAlignment::kBaseline) |
| 443 | .value("AboveBaseline", para::PlaceholderAlignment::kAboveBaseline) |
| 444 | .value("BelowBaseline", para::PlaceholderAlignment::kBelowBaseline) |
| 445 | .value("Top", para::PlaceholderAlignment::kTop) |
| 446 | .value("Bottom", para::PlaceholderAlignment::kBottom) |
| 447 | .value("Middle", para::PlaceholderAlignment::kMiddle); |
| 448 | |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 449 | enum_<para::RectHeightStyle>("RectHeightStyle") |
Kevin Lubick | 4a5f4f2 | 2019-11-20 08:27:10 -0500 | [diff] [blame] | 450 | .value("Tight", para::RectHeightStyle::kTight) |
| 451 | .value("Max", para::RectHeightStyle::kMax) |
| 452 | .value("IncludeLineSpacingMiddle", para::RectHeightStyle::kIncludeLineSpacingMiddle) |
| 453 | .value("IncludeLineSpacingTop", para::RectHeightStyle::kIncludeLineSpacingTop) |
| 454 | .value("IncludeLineSpacingBottom", para::RectHeightStyle::kIncludeLineSpacingBottom); |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 455 | |
| 456 | enum_<para::RectWidthStyle>("RectWidthStyle") |
| 457 | .value("Tight", para::RectWidthStyle::kTight) |
| 458 | .value("Max", para::RectWidthStyle::kMax); |
| 459 | |
| 460 | enum_<para::TextAlign>("TextAlign") |
| 461 | .value("Left", para::TextAlign::kLeft) |
| 462 | .value("Right", para::TextAlign::kRight) |
| 463 | .value("Center", para::TextAlign::kCenter) |
| 464 | .value("Justify", para::TextAlign::kJustify) |
| 465 | .value("Start", para::TextAlign::kStart) |
| 466 | .value("End", para::TextAlign::kEnd); |
| 467 | |
Harry Terkelsen | 223ffcd | 2020-10-02 15:24:13 -0700 | [diff] [blame] | 468 | enum_<para::TextBaseline>("TextBaseline") |
| 469 | .value("Alphabetic", para::TextBaseline::kAlphabetic) |
| 470 | .value("Ideographic", para::TextBaseline::kIdeographic); |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 471 | |
Kevin Lubick | c488fd3 | 2020-10-06 08:24:00 -0400 | [diff] [blame] | 472 | enum_<para::TextDirection>("TextDirection") |
| 473 | .value("LTR", para::TextDirection::kLtr) |
| 474 | .value("RTL", para::TextDirection::kRtl); |
| 475 | |
| 476 | // These value objects make it easier to send data across the wire. |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 477 | value_object<para::PositionWithAffinity>("PositionWithAffinity") |
| 478 | .field("pos", ¶::PositionWithAffinity::position) |
| 479 | .field("affinity", ¶::PositionWithAffinity::affinity); |
| 480 | |
Kevin Lubick | 0491267 | 2019-11-15 14:48:55 -0500 | [diff] [blame] | 481 | value_object<SimpleFontStyle>("FontStyle") |
Kevin Lubick | d3b1fe6 | 2019-10-21 10:50:26 -0400 | [diff] [blame] | 482 | .field("slant", &SimpleFontStyle::slant) |
| 483 | .field("weight", &SimpleFontStyle::weight) |
| 484 | .field("width", &SimpleFontStyle::width); |
| 485 | |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 486 | value_object<SimpleParagraphStyle>("ParagraphStyle") |
Kevin Lubick | d3b1fe6 | 2019-10-21 10:50:26 -0400 | [diff] [blame] | 487 | .field("disableHinting", &SimpleParagraphStyle::disableHinting) |
| 488 | .field("_ellipsisPtr", &SimpleParagraphStyle::ellipsisPtr) |
| 489 | .field("_ellipsisLen", &SimpleParagraphStyle::ellipsisLen) |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 490 | .field("heightMultiplier", &SimpleParagraphStyle::heightMultiplier) |
| 491 | .field("maxLines", &SimpleParagraphStyle::maxLines) |
| 492 | .field("textAlign", &SimpleParagraphStyle::textAlign) |
Kevin Lubick | d3b1fe6 | 2019-10-21 10:50:26 -0400 | [diff] [blame] | 493 | .field("textDirection", &SimpleParagraphStyle::textDirection) |
Harry Terkelsen | 223ffcd | 2020-10-02 15:24:13 -0700 | [diff] [blame] | 494 | .field("textStyle", &SimpleParagraphStyle::textStyle) |
| 495 | .field("strutStyle", &SimpleParagraphStyle::strutStyle); |
| 496 | |
| 497 | value_object<SimpleStrutStyle>("StrutStyle") |
| 498 | .field("_fontFamiliesPtr", &SimpleStrutStyle::fontFamiliesPtr) |
| 499 | .field("_fontFamiliesLen", &SimpleStrutStyle::fontFamiliesLen) |
| 500 | .field("strutEnabled", &SimpleStrutStyle::strutEnabled) |
| 501 | .field("fontSize", &SimpleStrutStyle::fontSize) |
| 502 | .field("fontStyle", &SimpleStrutStyle::fontStyle) |
| 503 | .field("heightMultiplier", &SimpleStrutStyle::heightMultiplier) |
| 504 | .field("leading", &SimpleStrutStyle::leading) |
| 505 | .field("forceStrutHeight", &SimpleStrutStyle::forceStrutHeight); |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 506 | |
| 507 | value_object<SimpleTextStyle>("TextStyle") |
Harry Terkelsen | 223ffcd | 2020-10-02 15:24:13 -0700 | [diff] [blame] | 508 | .field("_colorPtr", &SimpleTextStyle::colorPtr) |
| 509 | .field("_foregroundColorPtr", &SimpleTextStyle::foregroundColorPtr) |
| 510 | .field("_backgroundColorPtr", &SimpleTextStyle::backgroundColorPtr) |
| 511 | .field("decoration", &SimpleTextStyle::decoration) |
| 512 | .field("decorationThickness", &SimpleTextStyle::decorationThickness) |
| 513 | .field("_decorationColorPtr", &SimpleTextStyle::decorationColorPtr) |
| 514 | .field("decorationStyle", &SimpleTextStyle::decorationStyle) |
| 515 | .field("_fontFamiliesPtr", &SimpleTextStyle::fontFamiliesPtr) |
| 516 | .field("_fontFamiliesLen", &SimpleTextStyle::fontFamiliesLen) |
| 517 | .field("fontSize", &SimpleTextStyle::fontSize) |
| 518 | .field("letterSpacing", &SimpleTextStyle::letterSpacing) |
| 519 | .field("wordSpacing", &SimpleTextStyle::wordSpacing) |
| 520 | .field("heightMultiplier", &SimpleTextStyle::heightMultiplier) |
| 521 | .field("_localePtr", &SimpleTextStyle::localePtr) |
| 522 | .field("_localeLen", &SimpleTextStyle::localeLen) |
| 523 | .field("fontStyle", &SimpleTextStyle::fontStyle) |
| 524 | .field("_shadowLen", &SimpleTextStyle::shadowLen) |
| 525 | .field("_shadowColorsPtr", &SimpleTextStyle::shadowColorsPtr) |
| 526 | .field("_shadowOffsetsPtr", &SimpleTextStyle::shadowOffsetsPtr) |
| 527 | .field("_shadowBlurRadiiPtr", &SimpleTextStyle::shadowBlurRadiiPtr) |
| 528 | .field("_fontFeatureLen", &SimpleTextStyle::fontFeatureLen) |
| 529 | .field("_fontFeatureNamesPtr", &SimpleTextStyle::fontFeatureNamesPtr) |
| 530 | .field("_fontFeatureValuesPtr", &SimpleTextStyle::fontFeatureValuesPtr); |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 531 | |
Kevin Lubick | 0491267 | 2019-11-15 14:48:55 -0500 | [diff] [blame] | 532 | // The U stands for unsigned - we can't bind a generic/template object, so we have to specify it |
| 533 | // with the type we are using. |
Kevin Lubick | c488fd3 | 2020-10-06 08:24:00 -0400 | [diff] [blame] | 534 | // TODO(kjlubick) make this a typedarray. |
Kevin Lubick | 0491267 | 2019-11-15 14:48:55 -0500 | [diff] [blame] | 535 | value_object<para::SkRange<size_t>>("URange") |
| 536 | .field("start", ¶::SkRange<size_t>::start) |
| 537 | .field("end", ¶::SkRange<size_t>::end); |
| 538 | |
Kevin Lubick | 369f6a5 | 2019-10-03 11:22:08 -0400 | [diff] [blame] | 539 | // TextDecoration should be a const because they can be combined |
| 540 | constant("NoDecoration", int(para::TextDecoration::kNoDecoration)); |
| 541 | constant("UnderlineDecoration", int(para::TextDecoration::kUnderline)); |
| 542 | constant("OverlineDecoration", int(para::TextDecoration::kOverline)); |
| 543 | constant("LineThroughDecoration", int(para::TextDecoration::kLineThrough)); |
| 544 | } |