blob: 39d519d69eb7e865681df45d8f2203329cec2fb2 [file] [log] [blame]
Kevin Lubick369f6a52019-10-03 11:22:08 -04001/*
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 Lubickd3b1fe62019-10-21 10:50:26 -04009#include "include/core/SkFontStyle.h"
Kevin Lubick369f6a52019-10-03 11:22:08 -040010#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 Terkelsen10f019c2020-08-04 13:21:09 -070016#include "modules/skparagraph/include/TypefaceFontProvider.h"
Kevin Lubick369f6a52019-10-03 11:22:08 -040017#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 Nifonge5d32542020-03-26 09:27:48 -040025#include "modules/canvaskit/WasmCommon.h"
Kevin Lubick369f6a52019-10-03 11:22:08 -040026
27using namespace emscripten;
28
29namespace para = skia::textlayout;
30
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -040031SkColor4f toSkColor4f(uintptr_t /* float* */ cPtr) {
32 float* fourFloats = reinterpret_cast<float*>(cPtr);
Harry Terkelsen223ffcd2020-10-02 15:24:13 -070033 SkColor4f color = {fourFloats[0], fourFloats[1], fourFloats[2], fourFloats[3]};
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -040034 return color;
35}
36
Kevin Lubickd3b1fe62019-10-21 10:50:26 -040037struct SimpleFontStyle {
Harry Terkelsen223ffcd2020-10-02 15:24:13 -070038 SkFontStyle::Slant slant;
Kevin Lubickd3b1fe62019-10-21 10:50:26 -040039 SkFontStyle::Weight weight;
Harry Terkelsen223ffcd2020-10-02 15:24:13 -070040 SkFontStyle::Width width;
Kevin Lubickd3b1fe62019-10-21 10:50:26 -040041};
42
Kevin Lubick369f6a52019-10-03 11:22:08 -040043struct SimpleTextStyle {
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -040044 uintptr_t /* float* */ colorPtr;
45 uintptr_t /* float* */ foregroundColorPtr;
46 uintptr_t /* float* */ backgroundColorPtr;
Kevin Lubick369f6a52019-10-03 11:22:08 -040047 uint8_t decoration;
Kevin Lubick369f6a52019-10-03 11:22:08 -040048 SkScalar decorationThickness;
Harry Terkelsen223ffcd2020-10-02 15:24:13 -070049 uintptr_t /* float* */ decorationColorPtr;
50 para::TextDecorationStyle decorationStyle;
51 para::TextBaseline textBaseline;
Kevin Lubickd3b1fe62019-10-21 10:50:26 -040052 SkScalar fontSize;
Harry Terkelsen223ffcd2020-10-02 15:24:13 -070053 SkScalar letterSpacing;
54 SkScalar wordSpacing;
55 SkScalar heightMultiplier;
56 uintptr_t /* const char* */ localePtr;
57 int localeLen;
Kevin Lubickd3b1fe62019-10-21 10:50:26 -040058 SimpleFontStyle fontStyle;
Kevin Lubickd3b1fe62019-10-21 10:50:26 -040059
Kevin Lubick0c8884b2020-05-14 08:27:53 -040060 uintptr_t /* const char** */ fontFamiliesPtr;
61 int fontFamiliesLen;
Harry Terkelsen223ffcd2020-10-02 15:24:13 -070062
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 Lubick369f6a52019-10-03 11:22:08 -040071};
72
Harry Terkelsen223ffcd2020-10-02 15:24:13 -070073struct 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
84para::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 Lubick369f6a52019-10-03 11:22:08 -0400116para::TextStyle toTextStyle(const SimpleTextStyle& s) {
117 para::TextStyle ts;
Nathaniel Nifonge5d32542020-03-26 09:27:48 -0400118
Harry Terkelsen223ffcd2020-10-02 15:24:13 -0700119 // textstyle.color doesn't support a 4f color, however the foreground and background fields
120 // below do.
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400121 ts.setColor(toSkColor4f(s.colorPtr).toSkColor());
Nathaniel Nifonge5d32542020-03-26 09:27:48 -0400122
Nathaniel Nifonge5d32542020-03-26 09:27:48 -0400123 // It is functionally important that these paints be unset when no value was provided.
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400124 if (s.foregroundColorPtr) {
Nathaniel Nifonge5d32542020-03-26 09:27:48 -0400125 SkPaint p1;
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400126 p1.setColor4f(toSkColor4f(s.foregroundColorPtr));
Nathaniel Nifonge5d32542020-03-26 09:27:48 -0400127 ts.setForegroundColor(p1);
Robert Phillipscb77eab2020-03-24 14:19:40 +0000128 }
Kevin Lubick369f6a52019-10-03 11:22:08 -0400129
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400130 if (s.backgroundColorPtr) {
Nathaniel Nifonge5d32542020-03-26 09:27:48 -0400131 SkPaint p2;
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400132 p2.setColor4f(toSkColor4f(s.backgroundColorPtr));
Nathaniel Nifonge5d32542020-03-26 09:27:48 -0400133 ts.setBackgroundColor(p2);
Robert Phillipscb77eab2020-03-24 14:19:40 +0000134 }
Kevin Lubick369f6a52019-10-03 11:22:08 -0400135
136 if (s.fontSize != 0) {
137 ts.setFontSize(s.fontSize);
138 }
Harry Terkelsen223ffcd2020-10-02 15:24:13 -0700139 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 Lubick369f6a52019-10-03 11:22:08 -0400150
151 ts.setDecoration(para::TextDecoration(s.decoration));
Harry Terkelsen223ffcd2020-10-02 15:24:13 -0700152 ts.setDecorationStyle(s.decorationStyle);
Kevin Lubick369f6a52019-10-03 11:22:08 -0400153 if (s.decorationThickness != 0) {
154 ts.setDecorationThicknessMultiplier(s.decorationThickness);
155 }
Harry Terkelsen223ffcd2020-10-02 15:24:13 -0700156 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 Lubick369f6a52019-10-03 11:22:08 -0400165
Kevin Lubick0c8884b2020-05-14 08:27:53 -0400166 const char** fontFamilies = reinterpret_cast<const char**>(s.fontFamiliesPtr);
Harry Terkelsen223ffcd2020-10-02 15:24:13 -0700167 if (fontFamilies != nullptr) {
Kevin Lubick369f6a52019-10-03 11:22:08 -0400168 std::vector<SkString> ff;
Kevin Lubick0c8884b2020-05-14 08:27:53 -0400169 for (int i = 0; i < s.fontFamiliesLen; i++) {
Kevin Lubick369f6a52019-10-03 11:22:08 -0400170 ff.emplace_back(fontFamilies[i]);
171 }
172 ts.setFontFamilies(ff);
173 }
174
Harry Terkelsen223ffcd2020-10-02 15:24:13 -0700175 ts.setTextBaseline(s.textBaseline);
176
Kevin Lubickd3b1fe62019-10-21 10:50:26 -0400177 SkFontStyle fs(s.fontStyle.weight, s.fontStyle.width, s.fontStyle.slant);
178 ts.setFontStyle(fs);
179
Harry Terkelsen223ffcd2020-10-02 15:24:13 -0700180 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 Lubick369f6a52019-10-03 11:22:08 -0400201 return ts;
202}
203
204struct SimpleParagraphStyle {
Kevin Lubickd3b1fe62019-10-21 10:50:26 -0400205 bool disableHinting;
206 uintptr_t /* const char* */ ellipsisPtr;
207 size_t ellipsisLen;
Kevin Lubick369f6a52019-10-03 11:22:08 -0400208 SkScalar heightMultiplier;
Kevin Lubick369f6a52019-10-03 11:22:08 -0400209 size_t maxLines;
Kevin Lubickd3b1fe62019-10-21 10:50:26 -0400210 para::TextAlign textAlign;
211 para::TextDirection textDirection;
212 SimpleTextStyle textStyle;
Harry Terkelsen223ffcd2020-10-02 15:24:13 -0700213 SimpleStrutStyle strutStyle;
Kevin Lubick369f6a52019-10-03 11:22:08 -0400214};
215
216para::ParagraphStyle toParagraphStyle(const SimpleParagraphStyle& s) {
217 para::ParagraphStyle ps;
Kevin Lubickd3b1fe62019-10-21 10:50:26 -0400218 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 Lubick369f6a52019-10-03 11:22:08 -0400229 auto ts = toTextStyle(s.textStyle);
230 ps.setTextStyle(ts);
Harry Terkelsen223ffcd2020-10-02 15:24:13 -0700231 auto ss = toStrutStyle(s.strutStyle);
232 ps.setStrutStyle(ss);
Kevin Lubick369f6a52019-10-03 11:22:08 -0400233 if (s.heightMultiplier != 0) {
234 ps.setHeight(s.heightMultiplier);
235 }
Kevin Lubick369f6a52019-10-03 11:22:08 -0400236 if (s.maxLines != 0) {
237 ps.setMaxLines(s.maxLines);
238 }
239 return ps;
240}
241
Kevin Lubick4a5f4f22019-11-20 08:27:10 -0500242struct 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 Terkelsen223ffcd2020-10-02 15:24:13 -0700250Float32Array TextBoxesToFloat32Array(std::vector<para::TextBox> boxes) {
Kevin Lubick4a5f4f22019-11-20 08:27:10 -0500251 // Pack these text boxes into an array of n groups of 5 SkScalar (floats)
Kevin Lubick369f6a52019-10-03 11:22:08 -0400252 if (!boxes.size()) {
253 return emscripten::val::null();
254 }
Kevin Lubick4a5f4f22019-11-20 08:27:10 -0500255 SimpleTextBox* rects = new SimpleTextBox[boxes.size()];
Harry Terkelsen223ffcd2020-10-02 15:24:13 -0700256 for (int i = 0; i < boxes.size(); i++) {
Kevin Lubick4a5f4f22019-11-20 08:27:10 -0500257 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 Lubick369f6a52019-10-03 11:22:08 -0400263 }
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 Terkelsen223ffcd2020-10-02 15:24:13 -0700267 return Float32Array(typed_memory_view(boxes.size() * 5, fPtr));
268}
269
Kevin Lubicka96e0802020-10-08 15:58:12 -0400270Float32Array GetRectsForRange(para::Paragraph& self,
Harry Terkelsen223ffcd2020-10-02 15:24:13 -0700271 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 Lubicka96e0802020-10-08 15:58:12 -0400279Float32Array GetRectsForPlaceholders(para::Paragraph& self) {
Harry Terkelsen223ffcd2020-10-02 15:24:13 -0700280 std::vector<para::TextBox> boxes = self.getRectsForPlaceholders();
281 return TextBoxesToFloat32Array(boxes);
Kevin Lubick369f6a52019-10-03 11:22:08 -0400282}
283
Kevin Lubicke677f4a2020-11-04 09:46:22 -0500284JSArray 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 Lubick369f6a52019-10-03 11:22:08 -0400307EMSCRIPTEN_BINDINGS(Paragraph) {
308
Kevin Lubicka96e0802020-10-08 15:58:12 -0400309 class_<para::Paragraph>("Paragraph")
Kevin Lubick04912672019-11-15 14:48:55 -0500310 .function("didExceedMaxLines", &para::Paragraph::didExceedMaxLines)
311 .function("getAlphabeticBaseline", &para::Paragraph::getAlphabeticBaseline)
Kevin Lubicka96e0802020-10-08 15:58:12 -0400312 .function("getGlyphPositionAtCoordinate", &para::Paragraph::getGlyphPositionAtCoordinate)
Kevin Lubick04912672019-11-15 14:48:55 -0500313 .function("getHeight", &para::Paragraph::getHeight)
314 .function("getIdeographicBaseline", &para::Paragraph::getIdeographicBaseline)
Kevin Lubicke677f4a2020-11-04 09:46:22 -0500315 .function("getLineMetrics", &GetLineMetrics)
Kevin Lubick04912672019-11-15 14:48:55 -0500316 .function("getLongestLine", &para::Paragraph::getLongestLine)
317 .function("getMaxIntrinsicWidth", &para::Paragraph::getMaxIntrinsicWidth)
318 .function("getMaxWidth", &para::Paragraph::getMaxWidth)
319 .function("getMinIntrinsicWidth", &para::Paragraph::getMinIntrinsicWidth)
Harry Terkelsen223ffcd2020-10-02 15:24:13 -0700320 .function("_getRectsForPlaceholders", &GetRectsForPlaceholders)
Kevin Lubick05162812020-10-05 14:11:28 -0400321 .function("_getRectsForRange", &GetRectsForRange)
Kevin Lubicka96e0802020-10-08 15:58:12 -0400322 .function("getWordBoundary", &para::Paragraph::getWordBoundary)
323 .function("layout", &para::Paragraph::layout);
Kevin Lubick369f6a52019-10-03 11:22:08 -0400324
325 class_<para::ParagraphBuilderImpl>("ParagraphBuilder")
Harry Terkelsen223ffcd2020-10-02 15:24:13 -0700326 .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", &para::ParagraphBuilderImpl::Build, allow_raw_pointers())
358 .function("pop", &para::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 Lubick369f6a52019-10-03 11:22:08 -0400386
Harry Terkelsen10f019c2020-08-04 13:21:09 -0700387 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 Lubick369f6a52019-10-03 11:22:08 -0400400
401 enum_<para::Affinity>("Affinity")
402 .value("Upstream", para::Affinity::kUpstream)
403 .value("Downstream", para::Affinity::kDownstream);
404
Kevin Lubickc488fd32020-10-06 08:24:00 -0400405 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 Lubickd3b1fe62019-10-21 10:50:26 -0400412 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 Lubickc488fd32020-10-06 08:24:00 -0400441 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 Lubick369f6a52019-10-03 11:22:08 -0400449 enum_<para::RectHeightStyle>("RectHeightStyle")
Kevin Lubick4a5f4f22019-11-20 08:27:10 -0500450 .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 Lubick369f6a52019-10-03 11:22:08 -0400455
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 Terkelsen223ffcd2020-10-02 15:24:13 -0700468 enum_<para::TextBaseline>("TextBaseline")
469 .value("Alphabetic", para::TextBaseline::kAlphabetic)
470 .value("Ideographic", para::TextBaseline::kIdeographic);
Kevin Lubick369f6a52019-10-03 11:22:08 -0400471
Kevin Lubickc488fd32020-10-06 08:24:00 -0400472 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 Lubick369f6a52019-10-03 11:22:08 -0400477 value_object<para::PositionWithAffinity>("PositionWithAffinity")
478 .field("pos", &para::PositionWithAffinity::position)
479 .field("affinity", &para::PositionWithAffinity::affinity);
480
Kevin Lubick04912672019-11-15 14:48:55 -0500481 value_object<SimpleFontStyle>("FontStyle")
Kevin Lubickd3b1fe62019-10-21 10:50:26 -0400482 .field("slant", &SimpleFontStyle::slant)
483 .field("weight", &SimpleFontStyle::weight)
484 .field("width", &SimpleFontStyle::width);
485
Kevin Lubick369f6a52019-10-03 11:22:08 -0400486 value_object<SimpleParagraphStyle>("ParagraphStyle")
Kevin Lubickd3b1fe62019-10-21 10:50:26 -0400487 .field("disableHinting", &SimpleParagraphStyle::disableHinting)
488 .field("_ellipsisPtr", &SimpleParagraphStyle::ellipsisPtr)
489 .field("_ellipsisLen", &SimpleParagraphStyle::ellipsisLen)
Kevin Lubick369f6a52019-10-03 11:22:08 -0400490 .field("heightMultiplier", &SimpleParagraphStyle::heightMultiplier)
491 .field("maxLines", &SimpleParagraphStyle::maxLines)
492 .field("textAlign", &SimpleParagraphStyle::textAlign)
Kevin Lubickd3b1fe62019-10-21 10:50:26 -0400493 .field("textDirection", &SimpleParagraphStyle::textDirection)
Harry Terkelsen223ffcd2020-10-02 15:24:13 -0700494 .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 Lubick369f6a52019-10-03 11:22:08 -0400506
507 value_object<SimpleTextStyle>("TextStyle")
Harry Terkelsen223ffcd2020-10-02 15:24:13 -0700508 .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 Lubick369f6a52019-10-03 11:22:08 -0400531
Kevin Lubick04912672019-11-15 14:48:55 -0500532 // 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 Lubickc488fd32020-10-06 08:24:00 -0400534 // TODO(kjlubick) make this a typedarray.
Kevin Lubick04912672019-11-15 14:48:55 -0500535 value_object<para::SkRange<size_t>>("URange")
536 .field("start", &para::SkRange<size_t>::start)
537 .field("end", &para::SkRange<size_t>::end);
538
Kevin Lubick369f6a52019-10-03 11:22:08 -0400539 // 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}