blob: ec88569b7a7520a011e2c7bc1ca0d7bc362b6696 [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"
9#include "include/core/SkString.h"
10
11#include "modules/skparagraph/include/DartTypes.h"
12#include "modules/skparagraph/include/Paragraph.h"
13#include "modules/skparagraph/include/ParagraphBuilder.h"
14#include "modules/skparagraph/include/TextStyle.h"
15#include "modules/skparagraph/src/ParagraphBuilderImpl.h"
16#include "modules/skparagraph/src/ParagraphImpl.h"
17
18#include <string>
19#include <vector>
20
21#include <emscripten.h>
22#include <emscripten/bind.h>
23#include "modules/canvaskit/WasmAliases.h"
24
25using namespace emscripten;
26
27namespace para = skia::textlayout;
28
29struct SimpleTextStyle {
30 SkColor color;
31 SkColor foregroundColor;
32 SkColor backgroundColor;
33 uint8_t decoration;
34 SkScalar fontSize;
35 SkScalar decorationThickness;
36 uintptr_t /* const char** */ fontFamilies;
37 int numFontFamilies;
38};
39
40para::TextStyle toTextStyle(const SimpleTextStyle& s) {
41 para::TextStyle ts;
42 if (s.color != 0) {
43 ts.setColor(s.color);
44 }
45
46 if (s.foregroundColor != 0) {
47 SkPaint p;
48 p.setColor(s.foregroundColor);
49 ts.setForegroundColor(p);
50 }
51
52 if (s.backgroundColor != 0) {
53 SkPaint p;
54 p.setColor(s.backgroundColor);
55 ts.setBackgroundColor(p);
56 }
57
58 if (s.fontSize != 0) {
59 ts.setFontSize(s.fontSize);
60 }
61
62 ts.setDecoration(para::TextDecoration(s.decoration));
63 if (s.decorationThickness != 0) {
64 ts.setDecorationThicknessMultiplier(s.decorationThickness);
65 }
66
67 const char** fontFamilies = reinterpret_cast<const char**>(s.fontFamilies);
68 if (s.numFontFamilies > 0 && fontFamilies != nullptr) {
69 std::vector<SkString> ff;
70 for (int i = 0; i< s.numFontFamilies; i++) {
71 ff.emplace_back(fontFamilies[i]);
72 }
73 ts.setFontFamilies(ff);
74 }
75
76 return ts;
77}
78
79struct SimpleParagraphStyle {
80 SimpleTextStyle textStyle;
81 SkScalar heightMultiplier;
82 para::TextAlign textAlign;
83 size_t maxLines;
84};
85
86para::ParagraphStyle toParagraphStyle(const SimpleParagraphStyle& s) {
87 para::ParagraphStyle ps;
88 auto ts = toTextStyle(s.textStyle);
89 ps.setTextStyle(ts);
90 if (s.heightMultiplier != 0) {
91 ps.setHeight(s.heightMultiplier);
92 }
93 ps.setTextAlign(s.textAlign);
94 if (s.maxLines != 0) {
95 ps.setMaxLines(s.maxLines);
96 }
97 return ps;
98}
99
100Float32Array GetRectsForRange(para::ParagraphImpl& self, unsigned start, unsigned end,
101 para::RectHeightStyle heightStyle, para::RectWidthStyle widthStyle) {
102 std::vector<para::TextBox> boxes = self.getRectsForRange(start, end, heightStyle, widthStyle);
103 // Pack these text boxes into an array of n groups of 4 SkScalar (floats)
104 if (!boxes.size()) {
105 return emscripten::val::null();
106 }
107 SkRect* rects = new SkRect[boxes.size()];
108 for (int i = 0; i< boxes.size(); i++) {
109 rects[i] = boxes[i].rect;
110 }
111 float* fPtr = reinterpret_cast<float*>(rects);
112 // Of note: now that we have cast rects to float*, emscripten is smart enough to wrap this
113 // into a Float32Array for us.
114 return Float32Array(typed_memory_view(boxes.size()*4, fPtr));
115}
116
117EMSCRIPTEN_BINDINGS(Paragraph) {
118
119 class_<para::Paragraph>("Paragraph");
120
121 // This "base<>" tells Emscripten that ParagraphImpl is a Paragraph and can get substituted
122 // in properly in drawParagraph. However, Emscripten will not let us bind pure virtual methods
123 // so we have to "expose" the ParagraphImpl and its methods.
124 class_<para::ParagraphImpl, base<para::Paragraph>>("ParagraphImpl")
125 .function("_getRectsForRange", &GetRectsForRange)
126 .function("getGlyphPositionAtCoordinate", &para::ParagraphImpl::getGlyphPositionAtCoordinate)
127 .function("layout", &para::ParagraphImpl::layout);
128
129 class_<para::ParagraphBuilderImpl>("ParagraphBuilder")
130 .class_function("Make", optional_override([](SimpleParagraphStyle style,
131 sk_sp<SkFontMgr> fontMgr)-> para::ParagraphBuilderImpl {
132 auto fc = sk_make_sp<para::FontCollection>();
133 fc->setDefaultFontManager(fontMgr);
134 auto ps = toParagraphStyle(style);
135 para::ParagraphBuilderImpl pbi(ps, fc);
136 return pbi;
137 }), allow_raw_pointers())
138 .function("addText", optional_override([](para::ParagraphBuilderImpl& self, std::string text) {
139 return self.addText(text.c_str(), text.length());
140 }))
141 .function("build", &para::ParagraphBuilderImpl::Build, allow_raw_pointers())
142 .function("pop", &para::ParagraphBuilderImpl::pop)
143 .function("pushStyle", optional_override([](para::ParagraphBuilderImpl& self,
144 SimpleTextStyle textStyle) {
145 auto ts = toTextStyle(textStyle);
146 self.pushStyle(ts);
147 }));
148
149
150 enum_<para::Affinity>("Affinity")
151 .value("Upstream", para::Affinity::kUpstream)
152 .value("Downstream", para::Affinity::kDownstream);
153
154 enum_<para::RectHeightStyle>("RectHeightStyle")
155 .value("Tight", para::RectHeightStyle::kTight)
156 .value("Max", para::RectHeightStyle::kMax);
157
158 enum_<para::RectWidthStyle>("RectWidthStyle")
159 .value("Tight", para::RectWidthStyle::kTight)
160 .value("Max", para::RectWidthStyle::kMax);
161
162 enum_<para::TextAlign>("TextAlign")
163 .value("Left", para::TextAlign::kLeft)
164 .value("Right", para::TextAlign::kRight)
165 .value("Center", para::TextAlign::kCenter)
166 .value("Justify", para::TextAlign::kJustify)
167 .value("Start", para::TextAlign::kStart)
168 .value("End", para::TextAlign::kEnd);
169
170
171 value_object<para::PositionWithAffinity>("PositionWithAffinity")
172 .field("pos", &para::PositionWithAffinity::position)
173 .field("affinity", &para::PositionWithAffinity::affinity);
174
175 value_object<SimpleParagraphStyle>("ParagraphStyle")
176 .field("heightMultiplier", &SimpleParagraphStyle::heightMultiplier)
177 .field("maxLines", &SimpleParagraphStyle::maxLines)
178 .field("textAlign", &SimpleParagraphStyle::textAlign)
179 .field("textStyle", &SimpleParagraphStyle::textStyle);
180
181 value_object<SimpleTextStyle>("TextStyle")
182 .field("backgroundColor", &SimpleTextStyle::backgroundColor)
183 .field("color", &SimpleTextStyle::color)
184 .field("decoration", &SimpleTextStyle::decoration)
185 .field("decorationThickness", &SimpleTextStyle::decorationThickness)
186 .field("_fontFamilies", &SimpleTextStyle::fontFamilies)
187 .field("fontSize", &SimpleTextStyle::fontSize)
188 .field("foregroundColor", &SimpleTextStyle::foregroundColor)
189 .field("_numFontFamilies", &SimpleTextStyle::numFontFamilies);
190
191 // TextDecoration should be a const because they can be combined
192 constant("NoDecoration", int(para::TextDecoration::kNoDecoration));
193 constant("UnderlineDecoration", int(para::TextDecoration::kUnderline));
194 constant("OverlineDecoration", int(para::TextDecoration::kOverline));
195 constant("LineThroughDecoration", int(para::TextDecoration::kLineThrough));
196}