blob: 332d1f3f9ad600265e98c1a389fd5825adcef7e9 [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
284EMSCRIPTEN_BINDINGS(Paragraph) {
285
Kevin Lubicka96e0802020-10-08 15:58:12 -0400286 class_<para::Paragraph>("Paragraph")
Kevin Lubick04912672019-11-15 14:48:55 -0500287 .function("didExceedMaxLines", &para::Paragraph::didExceedMaxLines)
288 .function("getAlphabeticBaseline", &para::Paragraph::getAlphabeticBaseline)
Kevin Lubicka96e0802020-10-08 15:58:12 -0400289 .function("getGlyphPositionAtCoordinate", &para::Paragraph::getGlyphPositionAtCoordinate)
Kevin Lubick04912672019-11-15 14:48:55 -0500290 .function("getHeight", &para::Paragraph::getHeight)
291 .function("getIdeographicBaseline", &para::Paragraph::getIdeographicBaseline)
Kevin Lubicka96e0802020-10-08 15:58:12 -0400292 .function("getLineMetrics", &para::Paragraph::getLineMetrics)
Kevin Lubick04912672019-11-15 14:48:55 -0500293 .function("getLongestLine", &para::Paragraph::getLongestLine)
294 .function("getMaxIntrinsicWidth", &para::Paragraph::getMaxIntrinsicWidth)
295 .function("getMaxWidth", &para::Paragraph::getMaxWidth)
296 .function("getMinIntrinsicWidth", &para::Paragraph::getMinIntrinsicWidth)
Harry Terkelsen223ffcd2020-10-02 15:24:13 -0700297 .function("_getRectsForPlaceholders", &GetRectsForPlaceholders)
Kevin Lubick05162812020-10-05 14:11:28 -0400298 .function("_getRectsForRange", &GetRectsForRange)
Kevin Lubicka96e0802020-10-08 15:58:12 -0400299 .function("getWordBoundary", &para::Paragraph::getWordBoundary)
300 .function("layout", &para::Paragraph::layout);
Kevin Lubick369f6a52019-10-03 11:22:08 -0400301
302 class_<para::ParagraphBuilderImpl>("ParagraphBuilder")
Harry Terkelsen223ffcd2020-10-02 15:24:13 -0700303 .class_function(
304 "_Make",
305 optional_override([](SimpleParagraphStyle style, sk_sp<SkFontMgr> fontMgr)
306 -> std::unique_ptr<para::ParagraphBuilderImpl> {
307 auto fc = sk_make_sp<para::FontCollection>();
308 fc->setDefaultFontManager(fontMgr);
309 fc->enableFontFallback();
310 auto ps = toParagraphStyle(style);
311 auto pb = para::ParagraphBuilderImpl::make(ps, fc);
312 return std::unique_ptr<para::ParagraphBuilderImpl>(
313 static_cast<para::ParagraphBuilderImpl*>(pb.release()));
314 }),
315 allow_raw_pointers())
316 .class_function(
317 "_MakeFromFontProvider",
318 optional_override([](SimpleParagraphStyle style,
319 sk_sp<para::TypefaceFontProvider> fontProvider)
320 -> std::unique_ptr<para::ParagraphBuilderImpl> {
321 auto fc = sk_make_sp<para::FontCollection>();
322 fc->setDefaultFontManager(fontProvider);
323 fc->enableFontFallback();
324 auto ps = toParagraphStyle(style);
325 auto pb = para::ParagraphBuilderImpl::make(ps, fc);
326 return std::unique_ptr<para::ParagraphBuilderImpl>(
327 static_cast<para::ParagraphBuilderImpl*>(pb.release()));
328 }),
329 allow_raw_pointers())
330 .function("addText",
331 optional_override([](para::ParagraphBuilderImpl& self, std::string text) {
332 return self.addText(text.c_str(), text.length());
333 }))
334 .function("build", &para::ParagraphBuilderImpl::Build, allow_raw_pointers())
335 .function("pop", &para::ParagraphBuilderImpl::pop)
336 .function("_pushStyle", optional_override([](para::ParagraphBuilderImpl& self,
337 SimpleTextStyle textStyle) {
338 auto ts = toTextStyle(textStyle);
339 self.pushStyle(ts);
340 }))
341 // A method of pushing a textStyle with paints instead of colors for foreground and
342 // background. Since SimpleTextStyle is a value object, it cannot contain paints, which
343 // are not primitives. This binding is here to accept them. Any color that is specified
344 // in the textStyle is overridden.
345 .function("_pushPaintStyle",
346 optional_override([](para::ParagraphBuilderImpl& self,
347 SimpleTextStyle textStyle, SkPaint foreground,
348 SkPaint background) {
349 auto ts = toTextStyle(textStyle);
350 ts.setForegroundColor(foreground);
351 ts.setBackgroundColor(background);
352 self.pushStyle(ts);
353 }))
354 .function("_addPlaceholder", optional_override([](para::ParagraphBuilderImpl& self,
355 SkScalar width,
356 SkScalar height,
357 para::PlaceholderAlignment alignment,
358 para::TextBaseline baseline,
359 SkScalar offset) {
360 para::PlaceholderStyle ps(width, height, alignment, baseline, offset);
361 self.addPlaceholder(ps);
362 }));
Kevin Lubick369f6a52019-10-03 11:22:08 -0400363
Harry Terkelsen10f019c2020-08-04 13:21:09 -0700364 class_<para::TypefaceFontProvider, base<SkFontMgr>>("TypefaceFontProvider")
365 .smart_ptr<sk_sp<para::TypefaceFontProvider>>("sk_sp<TypefaceFontProvider>")
366 .class_function("Make", optional_override([]()-> sk_sp<para::TypefaceFontProvider> {
367 return sk_make_sp<para::TypefaceFontProvider>();
368 }))
369 .function("_registerFont", optional_override([](para::TypefaceFontProvider& self,
370 sk_sp<SkTypeface> typeface,
371 uintptr_t familyPtr) {
372 const char* fPtr = reinterpret_cast<const char*>(familyPtr);
373 SkString fStr(fPtr);
374 self.registerTypeface(typeface, fStr);
375 }), allow_raw_pointers());
376
Kevin Lubick369f6a52019-10-03 11:22:08 -0400377
378 enum_<para::Affinity>("Affinity")
379 .value("Upstream", para::Affinity::kUpstream)
380 .value("Downstream", para::Affinity::kDownstream);
381
Kevin Lubickc488fd32020-10-06 08:24:00 -0400382 enum_<para::TextDecorationStyle>("DecorationStyle")
383 .value("Solid", para::TextDecorationStyle::kSolid)
384 .value("Double", para::TextDecorationStyle::kDouble)
385 .value("Dotted", para::TextDecorationStyle::kDotted)
386 .value("Dashed", para::TextDecorationStyle::kDashed)
387 .value("Wavy", para::TextDecorationStyle::kWavy);
388
Kevin Lubickd3b1fe62019-10-21 10:50:26 -0400389 enum_<SkFontStyle::Slant>("FontSlant")
390 .value("Upright", SkFontStyle::Slant::kUpright_Slant)
391 .value("Italic", SkFontStyle::Slant::kItalic_Slant)
392 .value("Oblique", SkFontStyle::Slant::kOblique_Slant);
393
394 enum_<SkFontStyle::Weight>("FontWeight")
395 .value("Invisible", SkFontStyle::Weight::kInvisible_Weight)
396 .value("Thin", SkFontStyle::Weight::kThin_Weight)
397 .value("ExtraLight", SkFontStyle::Weight::kExtraLight_Weight)
398 .value("Light", SkFontStyle::Weight::kLight_Weight)
399 .value("Normal", SkFontStyle::Weight::kNormal_Weight)
400 .value("Medium", SkFontStyle::Weight::kMedium_Weight)
401 .value("SemiBold", SkFontStyle::Weight::kSemiBold_Weight)
402 .value("Bold", SkFontStyle::Weight::kBold_Weight)
403 .value("ExtraBold", SkFontStyle::Weight::kExtraBold_Weight)
404 .value("Black" , SkFontStyle::Weight::kBlack_Weight)
405 .value("ExtraBlack", SkFontStyle::Weight::kExtraBlack_Weight);
406
407 enum_<SkFontStyle::Width>("FontWidth")
408 .value("UltraCondensed", SkFontStyle::Width::kUltraCondensed_Width)
409 .value("ExtraCondensed", SkFontStyle::Width::kExtraCondensed_Width)
410 .value("Condensed", SkFontStyle::Width::kCondensed_Width)
411 .value("SemiCondensed", SkFontStyle::Width::kSemiCondensed_Width)
412 .value("Normal", SkFontStyle::Width::kNormal_Width)
413 .value("SemiExpanded", SkFontStyle::Width::kSemiExpanded_Width)
414 .value("Expanded", SkFontStyle::Width::kExpanded_Width)
415 .value("ExtraExpanded", SkFontStyle::Width::kExtraExpanded_Width)
416 .value("UltraExpanded", SkFontStyle::Width::kUltraExpanded_Width);
417
Kevin Lubickc488fd32020-10-06 08:24:00 -0400418 enum_<para::PlaceholderAlignment>("PlaceholderAlignment")
419 .value("Baseline", para::PlaceholderAlignment::kBaseline)
420 .value("AboveBaseline", para::PlaceholderAlignment::kAboveBaseline)
421 .value("BelowBaseline", para::PlaceholderAlignment::kBelowBaseline)
422 .value("Top", para::PlaceholderAlignment::kTop)
423 .value("Bottom", para::PlaceholderAlignment::kBottom)
424 .value("Middle", para::PlaceholderAlignment::kMiddle);
425
Kevin Lubick369f6a52019-10-03 11:22:08 -0400426 enum_<para::RectHeightStyle>("RectHeightStyle")
Kevin Lubick4a5f4f22019-11-20 08:27:10 -0500427 .value("Tight", para::RectHeightStyle::kTight)
428 .value("Max", para::RectHeightStyle::kMax)
429 .value("IncludeLineSpacingMiddle", para::RectHeightStyle::kIncludeLineSpacingMiddle)
430 .value("IncludeLineSpacingTop", para::RectHeightStyle::kIncludeLineSpacingTop)
431 .value("IncludeLineSpacingBottom", para::RectHeightStyle::kIncludeLineSpacingBottom);
Kevin Lubick369f6a52019-10-03 11:22:08 -0400432
433 enum_<para::RectWidthStyle>("RectWidthStyle")
434 .value("Tight", para::RectWidthStyle::kTight)
435 .value("Max", para::RectWidthStyle::kMax);
436
437 enum_<para::TextAlign>("TextAlign")
438 .value("Left", para::TextAlign::kLeft)
439 .value("Right", para::TextAlign::kRight)
440 .value("Center", para::TextAlign::kCenter)
441 .value("Justify", para::TextAlign::kJustify)
442 .value("Start", para::TextAlign::kStart)
443 .value("End", para::TextAlign::kEnd);
444
Harry Terkelsen223ffcd2020-10-02 15:24:13 -0700445 enum_<para::TextBaseline>("TextBaseline")
446 .value("Alphabetic", para::TextBaseline::kAlphabetic)
447 .value("Ideographic", para::TextBaseline::kIdeographic);
Kevin Lubick369f6a52019-10-03 11:22:08 -0400448
Kevin Lubickc488fd32020-10-06 08:24:00 -0400449 enum_<para::TextDirection>("TextDirection")
450 .value("LTR", para::TextDirection::kLtr)
451 .value("RTL", para::TextDirection::kRtl);
452
453 // These value objects make it easier to send data across the wire.
Kevin Lubick369f6a52019-10-03 11:22:08 -0400454 value_object<para::PositionWithAffinity>("PositionWithAffinity")
455 .field("pos", &para::PositionWithAffinity::position)
456 .field("affinity", &para::PositionWithAffinity::affinity);
457
Kevin Lubick04912672019-11-15 14:48:55 -0500458 value_object<SimpleFontStyle>("FontStyle")
Kevin Lubickd3b1fe62019-10-21 10:50:26 -0400459 .field("slant", &SimpleFontStyle::slant)
460 .field("weight", &SimpleFontStyle::weight)
461 .field("width", &SimpleFontStyle::width);
462
Kevin Lubick369f6a52019-10-03 11:22:08 -0400463 value_object<SimpleParagraphStyle>("ParagraphStyle")
Kevin Lubickd3b1fe62019-10-21 10:50:26 -0400464 .field("disableHinting", &SimpleParagraphStyle::disableHinting)
465 .field("_ellipsisPtr", &SimpleParagraphStyle::ellipsisPtr)
466 .field("_ellipsisLen", &SimpleParagraphStyle::ellipsisLen)
Kevin Lubick369f6a52019-10-03 11:22:08 -0400467 .field("heightMultiplier", &SimpleParagraphStyle::heightMultiplier)
468 .field("maxLines", &SimpleParagraphStyle::maxLines)
469 .field("textAlign", &SimpleParagraphStyle::textAlign)
Kevin Lubickd3b1fe62019-10-21 10:50:26 -0400470 .field("textDirection", &SimpleParagraphStyle::textDirection)
Harry Terkelsen223ffcd2020-10-02 15:24:13 -0700471 .field("textStyle", &SimpleParagraphStyle::textStyle)
472 .field("strutStyle", &SimpleParagraphStyle::strutStyle);
473
474 value_object<SimpleStrutStyle>("StrutStyle")
475 .field("_fontFamiliesPtr", &SimpleStrutStyle::fontFamiliesPtr)
476 .field("_fontFamiliesLen", &SimpleStrutStyle::fontFamiliesLen)
477 .field("strutEnabled", &SimpleStrutStyle::strutEnabled)
478 .field("fontSize", &SimpleStrutStyle::fontSize)
479 .field("fontStyle", &SimpleStrutStyle::fontStyle)
480 .field("heightMultiplier", &SimpleStrutStyle::heightMultiplier)
481 .field("leading", &SimpleStrutStyle::leading)
482 .field("forceStrutHeight", &SimpleStrutStyle::forceStrutHeight);
Kevin Lubick369f6a52019-10-03 11:22:08 -0400483
484 value_object<SimpleTextStyle>("TextStyle")
Harry Terkelsen223ffcd2020-10-02 15:24:13 -0700485 .field("_colorPtr", &SimpleTextStyle::colorPtr)
486 .field("_foregroundColorPtr", &SimpleTextStyle::foregroundColorPtr)
487 .field("_backgroundColorPtr", &SimpleTextStyle::backgroundColorPtr)
488 .field("decoration", &SimpleTextStyle::decoration)
489 .field("decorationThickness", &SimpleTextStyle::decorationThickness)
490 .field("_decorationColorPtr", &SimpleTextStyle::decorationColorPtr)
491 .field("decorationStyle", &SimpleTextStyle::decorationStyle)
492 .field("_fontFamiliesPtr", &SimpleTextStyle::fontFamiliesPtr)
493 .field("_fontFamiliesLen", &SimpleTextStyle::fontFamiliesLen)
494 .field("fontSize", &SimpleTextStyle::fontSize)
495 .field("letterSpacing", &SimpleTextStyle::letterSpacing)
496 .field("wordSpacing", &SimpleTextStyle::wordSpacing)
497 .field("heightMultiplier", &SimpleTextStyle::heightMultiplier)
498 .field("_localePtr", &SimpleTextStyle::localePtr)
499 .field("_localeLen", &SimpleTextStyle::localeLen)
500 .field("fontStyle", &SimpleTextStyle::fontStyle)
501 .field("_shadowLen", &SimpleTextStyle::shadowLen)
502 .field("_shadowColorsPtr", &SimpleTextStyle::shadowColorsPtr)
503 .field("_shadowOffsetsPtr", &SimpleTextStyle::shadowOffsetsPtr)
504 .field("_shadowBlurRadiiPtr", &SimpleTextStyle::shadowBlurRadiiPtr)
505 .field("_fontFeatureLen", &SimpleTextStyle::fontFeatureLen)
506 .field("_fontFeatureNamesPtr", &SimpleTextStyle::fontFeatureNamesPtr)
507 .field("_fontFeatureValuesPtr", &SimpleTextStyle::fontFeatureValuesPtr);
Kevin Lubick369f6a52019-10-03 11:22:08 -0400508
Kevin Lubick04912672019-11-15 14:48:55 -0500509 // The U stands for unsigned - we can't bind a generic/template object, so we have to specify it
510 // with the type we are using.
Kevin Lubickc488fd32020-10-06 08:24:00 -0400511 // TODO(kjlubick) make this a typedarray.
Kevin Lubick04912672019-11-15 14:48:55 -0500512 value_object<para::SkRange<size_t>>("URange")
513 .field("start", &para::SkRange<size_t>::start)
514 .field("end", &para::SkRange<size_t>::end);
515
Kevin Lubick369f6a52019-10-03 11:22:08 -0400516 // TextDecoration should be a const because they can be combined
517 constant("NoDecoration", int(para::TextDecoration::kNoDecoration));
518 constant("UnderlineDecoration", int(para::TextDecoration::kUnderline));
519 constant("OverlineDecoration", int(para::TextDecoration::kOverline));
520 constant("LineThroughDecoration", int(para::TextDecoration::kLineThrough));
521}