2019-10-03 15:22:08 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2019 Google LLC
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "include/core/SkColor.h"
|
2019-10-21 14:50:26 +00:00
|
|
|
#include "include/core/SkFontStyle.h"
|
2021-05-06 22:12:27 +00:00
|
|
|
#include "include/core/SkPictureRecorder.h"
|
2019-10-03 15:22:08 +00:00
|
|
|
#include "include/core/SkString.h"
|
|
|
|
|
|
|
|
#include "modules/skparagraph/include/DartTypes.h"
|
|
|
|
#include "modules/skparagraph/include/Paragraph.h"
|
|
|
|
#include "modules/skparagraph/include/TextStyle.h"
|
2020-08-04 20:21:09 +00:00
|
|
|
#include "modules/skparagraph/include/TypefaceFontProvider.h"
|
2019-10-03 15:22:08 +00:00
|
|
|
#include "modules/skparagraph/src/ParagraphBuilderImpl.h"
|
|
|
|
#include "modules/skparagraph/src/ParagraphImpl.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <emscripten.h>
|
|
|
|
#include <emscripten/bind.h>
|
2020-03-26 13:27:48 +00:00
|
|
|
#include "modules/canvaskit/WasmCommon.h"
|
2019-10-03 15:22:08 +00:00
|
|
|
|
|
|
|
using namespace emscripten;
|
|
|
|
|
|
|
|
namespace para = skia::textlayout;
|
|
|
|
|
2021-05-26 17:40:22 +00:00
|
|
|
SkColor4f toSkColor4f(WASMPointerF32 cPtr) {
|
2020-05-04 20:46:17 +00:00
|
|
|
float* fourFloats = reinterpret_cast<float*>(cPtr);
|
2020-10-02 22:24:13 +00:00
|
|
|
SkColor4f color = {fourFloats[0], fourFloats[1], fourFloats[2], fourFloats[3]};
|
2020-05-04 20:46:17 +00:00
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
2019-10-21 14:50:26 +00:00
|
|
|
struct SimpleFontStyle {
|
2020-10-02 22:24:13 +00:00
|
|
|
SkFontStyle::Slant slant;
|
2019-10-21 14:50:26 +00:00
|
|
|
SkFontStyle::Weight weight;
|
2020-10-02 22:24:13 +00:00
|
|
|
SkFontStyle::Width width;
|
2019-10-21 14:50:26 +00:00
|
|
|
};
|
|
|
|
|
2019-10-03 15:22:08 +00:00
|
|
|
struct SimpleTextStyle {
|
2021-05-26 17:40:22 +00:00
|
|
|
WASMPointerF32 colorPtr;
|
|
|
|
WASMPointerF32 foregroundColorPtr;
|
|
|
|
WASMPointerF32 backgroundColorPtr;
|
2019-10-03 15:22:08 +00:00
|
|
|
uint8_t decoration;
|
|
|
|
SkScalar decorationThickness;
|
2021-05-26 17:40:22 +00:00
|
|
|
WASMPointerF32 decorationColorPtr;
|
2020-10-02 22:24:13 +00:00
|
|
|
para::TextDecorationStyle decorationStyle;
|
|
|
|
para::TextBaseline textBaseline;
|
2019-10-21 14:50:26 +00:00
|
|
|
SkScalar fontSize;
|
2020-10-02 22:24:13 +00:00
|
|
|
SkScalar letterSpacing;
|
|
|
|
SkScalar wordSpacing;
|
|
|
|
SkScalar heightMultiplier;
|
2021-04-20 17:55:49 +00:00
|
|
|
bool halfLeading;
|
2021-05-26 17:40:22 +00:00
|
|
|
WASMPointerU8 localePtr;
|
2020-10-02 22:24:13 +00:00
|
|
|
int localeLen;
|
2019-10-21 14:50:26 +00:00
|
|
|
SimpleFontStyle fontStyle;
|
|
|
|
|
2021-05-26 17:40:22 +00:00
|
|
|
WASMPointerU8 fontFamiliesPtr;
|
2020-05-14 12:27:53 +00:00
|
|
|
int fontFamiliesLen;
|
2020-10-02 22:24:13 +00:00
|
|
|
|
|
|
|
int shadowLen;
|
2021-05-26 17:40:22 +00:00
|
|
|
WASMPointerF32 shadowColorsPtr;
|
|
|
|
WASMPointerF32 shadowOffsetsPtr;
|
|
|
|
WASMPointerF32 shadowBlurRadiiPtr;
|
2020-10-02 22:24:13 +00:00
|
|
|
|
|
|
|
int fontFeatureLen;
|
2021-05-26 17:40:22 +00:00
|
|
|
WASMPointerF32 fontFeatureNamesPtr;
|
|
|
|
WASMPointerF32 fontFeatureValuesPtr;
|
2019-10-03 15:22:08 +00:00
|
|
|
};
|
|
|
|
|
2020-10-02 22:24:13 +00:00
|
|
|
struct SimpleStrutStyle {
|
2021-05-26 17:40:22 +00:00
|
|
|
WASMPointerU32 fontFamiliesPtr;
|
2020-10-02 22:24:13 +00:00
|
|
|
int fontFamiliesLen;
|
|
|
|
SimpleFontStyle fontStyle;
|
|
|
|
SkScalar fontSize;
|
|
|
|
SkScalar heightMultiplier;
|
2021-04-20 17:55:49 +00:00
|
|
|
bool halfLeading;
|
2020-10-02 22:24:13 +00:00
|
|
|
SkScalar leading;
|
|
|
|
bool strutEnabled;
|
|
|
|
bool forceStrutHeight;
|
|
|
|
};
|
|
|
|
|
|
|
|
para::StrutStyle toStrutStyle(const SimpleStrutStyle& s) {
|
|
|
|
para::StrutStyle ss;
|
|
|
|
|
|
|
|
const char** fontFamilies = reinterpret_cast<const char**>(s.fontFamiliesPtr);
|
|
|
|
if (fontFamilies != nullptr) {
|
|
|
|
std::vector<SkString> ff;
|
|
|
|
for (int i = 0; i < s.fontFamiliesLen; i++) {
|
|
|
|
ff.emplace_back(fontFamilies[i]);
|
|
|
|
}
|
|
|
|
ss.setFontFamilies(ff);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkFontStyle fs(s.fontStyle.weight, s.fontStyle.width, s.fontStyle.slant);
|
|
|
|
ss.setFontStyle(fs);
|
|
|
|
|
|
|
|
if (s.fontSize != 0) {
|
|
|
|
ss.setFontSize(s.fontSize);
|
|
|
|
}
|
|
|
|
if (s.heightMultiplier != 0) {
|
|
|
|
ss.setHeight(s.heightMultiplier);
|
|
|
|
ss.setHeightOverride(true);
|
|
|
|
}
|
2021-04-20 17:55:49 +00:00
|
|
|
ss.setHalfLeading(s.halfLeading);
|
|
|
|
|
2020-10-02 22:24:13 +00:00
|
|
|
if (s.leading != 0) {
|
|
|
|
ss.setLeading(s.leading);
|
|
|
|
}
|
|
|
|
|
|
|
|
ss.setStrutEnabled(s.strutEnabled);
|
|
|
|
ss.setForceStrutHeight(s.forceStrutHeight);
|
|
|
|
|
|
|
|
return ss;
|
|
|
|
}
|
|
|
|
|
2019-10-03 15:22:08 +00:00
|
|
|
para::TextStyle toTextStyle(const SimpleTextStyle& s) {
|
|
|
|
para::TextStyle ts;
|
|
|
|
|
2020-10-02 22:24:13 +00:00
|
|
|
// textstyle.color doesn't support a 4f color, however the foreground and background fields
|
|
|
|
// below do.
|
2020-05-04 20:46:17 +00:00
|
|
|
ts.setColor(toSkColor4f(s.colorPtr).toSkColor());
|
2020-03-26 13:27:48 +00:00
|
|
|
|
|
|
|
// It is functionally important that these paints be unset when no value was provided.
|
2020-05-04 20:46:17 +00:00
|
|
|
if (s.foregroundColorPtr) {
|
2020-03-26 13:27:48 +00:00
|
|
|
SkPaint p1;
|
2020-05-04 20:46:17 +00:00
|
|
|
p1.setColor4f(toSkColor4f(s.foregroundColorPtr));
|
2020-03-26 13:27:48 +00:00
|
|
|
ts.setForegroundColor(p1);
|
2020-03-24 14:19:40 +00:00
|
|
|
}
|
2020-03-24 13:08:37 +00:00
|
|
|
|
2020-05-04 20:46:17 +00:00
|
|
|
if (s.backgroundColorPtr) {
|
2020-03-26 13:27:48 +00:00
|
|
|
SkPaint p2;
|
2020-05-04 20:46:17 +00:00
|
|
|
p2.setColor4f(toSkColor4f(s.backgroundColorPtr));
|
2020-03-26 13:27:48 +00:00
|
|
|
ts.setBackgroundColor(p2);
|
2020-03-24 14:19:40 +00:00
|
|
|
}
|
2019-10-03 15:22:08 +00:00
|
|
|
|
|
|
|
if (s.fontSize != 0) {
|
|
|
|
ts.setFontSize(s.fontSize);
|
|
|
|
}
|
2020-10-02 22:24:13 +00:00
|
|
|
if (s.letterSpacing != 0) {
|
|
|
|
ts.setLetterSpacing(s.letterSpacing);
|
|
|
|
}
|
|
|
|
if (s.wordSpacing != 0) {
|
|
|
|
ts.setWordSpacing(s.wordSpacing);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s.heightMultiplier != 0) {
|
|
|
|
ts.setHeight(s.heightMultiplier);
|
|
|
|
ts.setHeightOverride(true);
|
|
|
|
}
|
2019-10-03 15:22:08 +00:00
|
|
|
|
2021-04-20 17:55:49 +00:00
|
|
|
ts.setHalfLeading(s.halfLeading);
|
|
|
|
|
2019-10-03 15:22:08 +00:00
|
|
|
ts.setDecoration(para::TextDecoration(s.decoration));
|
2020-10-02 22:24:13 +00:00
|
|
|
ts.setDecorationStyle(s.decorationStyle);
|
2019-10-03 15:22:08 +00:00
|
|
|
if (s.decorationThickness != 0) {
|
|
|
|
ts.setDecorationThicknessMultiplier(s.decorationThickness);
|
|
|
|
}
|
2020-10-02 22:24:13 +00:00
|
|
|
if (s.decorationColorPtr) {
|
|
|
|
ts.setDecorationColor(toSkColor4f(s.decorationColorPtr).toSkColor());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s.localeLen > 0) {
|
|
|
|
const char* localePtr = reinterpret_cast<const char*>(s.localePtr);
|
|
|
|
SkString lStr(localePtr, s.localeLen);
|
|
|
|
ts.setLocale(lStr);
|
|
|
|
}
|
2019-10-03 15:22:08 +00:00
|
|
|
|
2020-05-14 12:27:53 +00:00
|
|
|
const char** fontFamilies = reinterpret_cast<const char**>(s.fontFamiliesPtr);
|
2020-10-02 22:24:13 +00:00
|
|
|
if (fontFamilies != nullptr) {
|
2019-10-03 15:22:08 +00:00
|
|
|
std::vector<SkString> ff;
|
2020-05-14 12:27:53 +00:00
|
|
|
for (int i = 0; i < s.fontFamiliesLen; i++) {
|
2019-10-03 15:22:08 +00:00
|
|
|
ff.emplace_back(fontFamilies[i]);
|
|
|
|
}
|
|
|
|
ts.setFontFamilies(ff);
|
|
|
|
}
|
|
|
|
|
2020-10-02 22:24:13 +00:00
|
|
|
ts.setTextBaseline(s.textBaseline);
|
|
|
|
|
2019-10-21 14:50:26 +00:00
|
|
|
SkFontStyle fs(s.fontStyle.weight, s.fontStyle.width, s.fontStyle.slant);
|
|
|
|
ts.setFontStyle(fs);
|
|
|
|
|
2020-10-02 22:24:13 +00:00
|
|
|
if (s.shadowLen > 0) {
|
|
|
|
const SkColor4f* colors = reinterpret_cast<const SkColor4f*>(s.shadowColorsPtr);
|
|
|
|
const SkPoint* offsets = reinterpret_cast<const SkPoint*>(s.shadowOffsetsPtr);
|
|
|
|
const float* blurRadii = reinterpret_cast<const float*>(s.shadowBlurRadiiPtr);
|
|
|
|
for (int i = 0; i < s.shadowLen; i++) {
|
|
|
|
para::TextShadow shadow(colors[i].toSkColor(), offsets[i], blurRadii[i]);
|
|
|
|
ts.addShadow(shadow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (s.fontFeatureLen > 0) {
|
|
|
|
const char** fontFeatureNames = reinterpret_cast<const char**>(s.fontFeatureNamesPtr);
|
|
|
|
const int* fontFeatureValues = reinterpret_cast<const int*>(s.fontFeatureValuesPtr);
|
|
|
|
for (int i = 0; i < s.fontFeatureLen; i++) {
|
|
|
|
// Font features names are 4-character simple strings.
|
|
|
|
SkString name(fontFeatureNames[i], 4);
|
|
|
|
ts.addFontFeature(name, fontFeatureValues[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-03 15:22:08 +00:00
|
|
|
return ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SimpleParagraphStyle {
|
2019-10-21 14:50:26 +00:00
|
|
|
bool disableHinting;
|
2021-05-26 17:40:22 +00:00
|
|
|
WASMPointerU8 ellipsisPtr;
|
2019-10-21 14:50:26 +00:00
|
|
|
size_t ellipsisLen;
|
2019-10-03 15:22:08 +00:00
|
|
|
SkScalar heightMultiplier;
|
|
|
|
size_t maxLines;
|
2019-10-21 14:50:26 +00:00
|
|
|
para::TextAlign textAlign;
|
|
|
|
para::TextDirection textDirection;
|
2021-04-22 17:53:35 +00:00
|
|
|
para::TextHeightBehavior textHeightBehavior;
|
2019-10-21 14:50:26 +00:00
|
|
|
SimpleTextStyle textStyle;
|
2020-10-02 22:24:13 +00:00
|
|
|
SimpleStrutStyle strutStyle;
|
2019-10-03 15:22:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
para::ParagraphStyle toParagraphStyle(const SimpleParagraphStyle& s) {
|
|
|
|
para::ParagraphStyle ps;
|
2019-10-21 14:50:26 +00:00
|
|
|
if (s.disableHinting) {
|
|
|
|
ps.turnHintingOff();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s.ellipsisLen > 0) {
|
|
|
|
const char* ellipsisPtr = reinterpret_cast<const char*>(s.ellipsisPtr);
|
|
|
|
SkString eStr(ellipsisPtr, s.ellipsisLen);
|
|
|
|
ps.setEllipsis(eStr);
|
|
|
|
}
|
|
|
|
ps.setTextAlign(s.textAlign);
|
|
|
|
ps.setTextDirection(s.textDirection);
|
2019-10-03 15:22:08 +00:00
|
|
|
auto ts = toTextStyle(s.textStyle);
|
|
|
|
ps.setTextStyle(ts);
|
2020-10-02 22:24:13 +00:00
|
|
|
auto ss = toStrutStyle(s.strutStyle);
|
|
|
|
ps.setStrutStyle(ss);
|
2019-10-03 15:22:08 +00:00
|
|
|
if (s.heightMultiplier != 0) {
|
|
|
|
ps.setHeight(s.heightMultiplier);
|
|
|
|
}
|
|
|
|
if (s.maxLines != 0) {
|
|
|
|
ps.setMaxLines(s.maxLines);
|
|
|
|
}
|
2021-04-22 17:53:35 +00:00
|
|
|
ps.setTextHeightBehavior(s.textHeightBehavior);
|
2019-10-03 15:22:08 +00:00
|
|
|
return ps;
|
|
|
|
}
|
|
|
|
|
2019-11-20 13:27:10 +00:00
|
|
|
struct SimpleTextBox {
|
|
|
|
SkRect rect;
|
|
|
|
// This isn't the most efficient way to represent this, but it is much easier to keep
|
|
|
|
// everything as floats when unpacking on the JS side.
|
|
|
|
// 0.0 = RTL, 1.0 = LTr
|
|
|
|
SkScalar direction;
|
|
|
|
};
|
|
|
|
|
2020-10-02 22:24:13 +00:00
|
|
|
Float32Array TextBoxesToFloat32Array(std::vector<para::TextBox> boxes) {
|
2019-11-20 13:27:10 +00:00
|
|
|
// Pack these text boxes into an array of n groups of 5 SkScalar (floats)
|
2019-10-03 15:22:08 +00:00
|
|
|
if (!boxes.size()) {
|
|
|
|
return emscripten::val::null();
|
|
|
|
}
|
2019-11-20 13:27:10 +00:00
|
|
|
SimpleTextBox* rects = new SimpleTextBox[boxes.size()];
|
2020-10-02 22:24:13 +00:00
|
|
|
for (int i = 0; i < boxes.size(); i++) {
|
2019-11-20 13:27:10 +00:00
|
|
|
rects[i].rect = boxes[i].rect;
|
|
|
|
if (boxes[i].direction == para::TextDirection::kRtl) {
|
|
|
|
rects[i].direction = 0;
|
|
|
|
} else {
|
|
|
|
rects[i].direction = 1;
|
|
|
|
}
|
2019-10-03 15:22:08 +00:00
|
|
|
}
|
|
|
|
float* fPtr = reinterpret_cast<float*>(rects);
|
|
|
|
// Of note: now that we have cast rects to float*, emscripten is smart enough to wrap this
|
|
|
|
// into a Float32Array for us.
|
2020-10-02 22:24:13 +00:00
|
|
|
return Float32Array(typed_memory_view(boxes.size() * 5, fPtr));
|
|
|
|
}
|
|
|
|
|
2020-10-08 19:58:12 +00:00
|
|
|
Float32Array GetRectsForRange(para::Paragraph& self,
|
2020-10-02 22:24:13 +00:00
|
|
|
unsigned start,
|
|
|
|
unsigned end,
|
|
|
|
para::RectHeightStyle heightStyle,
|
|
|
|
para::RectWidthStyle widthStyle) {
|
|
|
|
std::vector<para::TextBox> boxes = self.getRectsForRange(start, end, heightStyle, widthStyle);
|
|
|
|
return TextBoxesToFloat32Array(boxes);
|
|
|
|
}
|
|
|
|
|
2020-10-08 19:58:12 +00:00
|
|
|
Float32Array GetRectsForPlaceholders(para::Paragraph& self) {
|
2020-10-02 22:24:13 +00:00
|
|
|
std::vector<para::TextBox> boxes = self.getRectsForPlaceholders();
|
|
|
|
return TextBoxesToFloat32Array(boxes);
|
2019-10-03 15:22:08 +00:00
|
|
|
}
|
|
|
|
|
2020-11-04 14:46:22 +00:00
|
|
|
JSArray GetLineMetrics(para::Paragraph& self) {
|
|
|
|
std::vector<skia::textlayout::LineMetrics> metrics;
|
|
|
|
self.getLineMetrics(metrics);
|
|
|
|
JSArray result = emscripten::val::array();
|
|
|
|
for (auto metric : metrics) {
|
|
|
|
JSObject m = emscripten::val::object();
|
|
|
|
m.set("startIndex", metric.fStartIndex);
|
|
|
|
m.set("endIndex", metric.fEndIndex);
|
|
|
|
m.set("endExcludingWhitespaces", metric.fEndExcludingWhitespaces);
|
|
|
|
m.set("endIncludingNewline", metric.fEndIncludingNewline);
|
|
|
|
m.set("isHardBreak", metric.fHardBreak);
|
|
|
|
m.set("ascent", metric.fAscent);
|
|
|
|
m.set("descent", metric.fDescent);
|
|
|
|
m.set("height", metric.fHeight);
|
|
|
|
m.set("width", metric.fWidth);
|
|
|
|
m.set("left", metric.fLeft);
|
|
|
|
m.set("baseline", metric.fBaseline);
|
|
|
|
m.set("lineNumber", metric.fLineNumber);
|
|
|
|
result.call<void>("push", m);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-05-02 21:47:09 +00:00
|
|
|
/*
|
|
|
|
* Returns Lines[]
|
|
|
|
*/
|
|
|
|
JSArray GetShapedLines(para::Paragraph& self) {
|
|
|
|
struct LineAccumulate {
|
|
|
|
int lineNumber = -1; // deliberately -1 from starting value
|
|
|
|
uint32_t minOffset = 0xFFFFFFFF;
|
|
|
|
uint32_t maxOffset = 0;
|
|
|
|
float minAscent = 0;
|
|
|
|
float maxDescent = 0;
|
2021-05-03 17:51:30 +00:00
|
|
|
// not really accumulated, but definitely set
|
|
|
|
float baseline = 0;
|
2021-05-02 21:47:09 +00:00
|
|
|
|
|
|
|
void reset(int lineNumber) {
|
|
|
|
new (this) LineAccumulate;
|
|
|
|
this->lineNumber = lineNumber;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// where we accumulate our js output
|
|
|
|
JSArray jlines = emscripten::val::array();
|
|
|
|
JSObject jline = emscripten::val::null();
|
|
|
|
JSArray jruns = emscripten::val::null();
|
|
|
|
LineAccumulate accum;
|
|
|
|
|
|
|
|
self.visit([&](int lineNumber, const para::Paragraph::VisitorInfo* info) {
|
|
|
|
if (!info) {
|
2021-05-06 22:12:27 +00:00
|
|
|
if (!jline) return; // how???
|
2021-05-02 21:47:09 +00:00
|
|
|
// end of current line
|
|
|
|
JSObject range = emscripten::val::object();
|
|
|
|
range.set("first", accum.minOffset);
|
|
|
|
range.set("last", accum.maxOffset);
|
|
|
|
jline.set("textRange", range);
|
|
|
|
|
2021-05-03 17:51:30 +00:00
|
|
|
jline.set("top", accum.baseline + accum.minAscent);
|
|
|
|
jline.set("bottom", accum.baseline + accum.maxDescent);
|
|
|
|
jline.set("baseline", accum.baseline);
|
2021-05-02 21:47:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lineNumber != accum.lineNumber) {
|
|
|
|
SkASSERT(lineNumber == accum.lineNumber + 1); // assume monotonic
|
|
|
|
|
|
|
|
accum.reset(lineNumber);
|
|
|
|
jruns = emscripten::val::array();
|
|
|
|
|
2021-05-18 18:27:46 +00:00
|
|
|
jline = emscripten::val::object();
|
2021-05-02 21:47:09 +00:00
|
|
|
jline.set("runs", jruns);
|
|
|
|
// will assign textRange and metrics on end-of-line signal
|
|
|
|
|
|
|
|
jlines.call<void>("push", jline);
|
|
|
|
}
|
|
|
|
|
|
|
|
// append the run
|
|
|
|
const int N = info->count; // glyphs
|
|
|
|
const int N1 = N + 1; // positions, offsets have 1 extra (trailing) slot
|
|
|
|
|
|
|
|
JSObject jrun = emscripten::val::object();
|
|
|
|
|
2021-05-06 22:12:27 +00:00
|
|
|
jrun.set("flags", info->flags);
|
|
|
|
|
|
|
|
// TODO: figure out how to set a wrapped sk_sp<SkTypeface>
|
|
|
|
// jrun.set("typeface", info->font.getTypeface());
|
|
|
|
jrun.set("typeface", emscripten::val::null());
|
|
|
|
jrun.set("size", info->font.getSize());
|
2021-05-07 15:42:22 +00:00
|
|
|
if (info->font.getScaleX()) {
|
|
|
|
jrun.set("scaleX", info->font.getScaleX());
|
|
|
|
}
|
2021-05-06 22:12:27 +00:00
|
|
|
|
2021-05-18 17:52:19 +00:00
|
|
|
jrun.set("glyphs", MakeTypedArray(N, info->glyphs));
|
|
|
|
jrun.set("offsets", MakeTypedArray(N1, info->utf8Starts));
|
2021-05-02 21:47:09 +00:00
|
|
|
|
|
|
|
// we need to modify the positions, so make a temp copy
|
|
|
|
SkAutoSTMalloc<32, SkPoint> positions(N1);
|
|
|
|
for (int i = 0; i < N; ++i) {
|
|
|
|
positions.get()[i] = info->positions[i] + info->origin;
|
|
|
|
}
|
|
|
|
positions.get()[N] = { info->advanceX, positions.get()[N - 1].fY };
|
2021-05-18 17:52:19 +00:00
|
|
|
jrun.set("positions", MakeTypedArray(N1*2, (const float*)positions.get()));
|
2021-05-02 21:47:09 +00:00
|
|
|
|
|
|
|
jruns.call<void>("push", jrun);
|
|
|
|
|
|
|
|
// update accum
|
|
|
|
{ SkFontMetrics fm;
|
|
|
|
info->font.getMetrics(&fm);
|
|
|
|
|
|
|
|
accum.minAscent = std::min(accum.minAscent, fm.fAscent);
|
|
|
|
accum.maxDescent = std::max(accum.maxDescent, fm.fDescent);
|
2021-05-03 17:51:30 +00:00
|
|
|
accum.baseline = info->origin.fY;
|
2021-05-02 21:47:09 +00:00
|
|
|
|
|
|
|
accum.minOffset = std::min(accum.minOffset, info->utf8Starts[0]);
|
|
|
|
accum.maxOffset = std::max(accum.maxOffset, info->utf8Starts[N]);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
return jlines;
|
|
|
|
}
|
|
|
|
|
2019-10-03 15:22:08 +00:00
|
|
|
EMSCRIPTEN_BINDINGS(Paragraph) {
|
|
|
|
|
2020-10-08 19:58:12 +00:00
|
|
|
class_<para::Paragraph>("Paragraph")
|
2019-11-15 19:48:55 +00:00
|
|
|
.function("didExceedMaxLines", ¶::Paragraph::didExceedMaxLines)
|
|
|
|
.function("getAlphabeticBaseline", ¶::Paragraph::getAlphabeticBaseline)
|
2020-10-08 19:58:12 +00:00
|
|
|
.function("getGlyphPositionAtCoordinate", ¶::Paragraph::getGlyphPositionAtCoordinate)
|
2019-11-15 19:48:55 +00:00
|
|
|
.function("getHeight", ¶::Paragraph::getHeight)
|
|
|
|
.function("getIdeographicBaseline", ¶::Paragraph::getIdeographicBaseline)
|
2020-11-04 14:46:22 +00:00
|
|
|
.function("getLineMetrics", &GetLineMetrics)
|
2019-11-15 19:48:55 +00:00
|
|
|
.function("getLongestLine", ¶::Paragraph::getLongestLine)
|
|
|
|
.function("getMaxIntrinsicWidth", ¶::Paragraph::getMaxIntrinsicWidth)
|
|
|
|
.function("getMaxWidth", ¶::Paragraph::getMaxWidth)
|
|
|
|
.function("getMinIntrinsicWidth", ¶::Paragraph::getMinIntrinsicWidth)
|
2020-10-02 22:24:13 +00:00
|
|
|
.function("_getRectsForPlaceholders", &GetRectsForPlaceholders)
|
2020-10-05 18:11:28 +00:00
|
|
|
.function("_getRectsForRange", &GetRectsForRange)
|
2021-05-02 21:47:09 +00:00
|
|
|
.function("getShapedLines", &GetShapedLines)
|
2020-10-08 19:58:12 +00:00
|
|
|
.function("getWordBoundary", ¶::Paragraph::getWordBoundary)
|
|
|
|
.function("layout", ¶::Paragraph::layout);
|
2019-10-03 15:22:08 +00:00
|
|
|
|
|
|
|
class_<para::ParagraphBuilderImpl>("ParagraphBuilder")
|
2020-10-02 22:24:13 +00:00
|
|
|
.class_function(
|
|
|
|
"_Make",
|
|
|
|
optional_override([](SimpleParagraphStyle style, sk_sp<SkFontMgr> fontMgr)
|
|
|
|
-> std::unique_ptr<para::ParagraphBuilderImpl> {
|
|
|
|
auto fc = sk_make_sp<para::FontCollection>();
|
|
|
|
fc->setDefaultFontManager(fontMgr);
|
|
|
|
fc->enableFontFallback();
|
|
|
|
auto ps = toParagraphStyle(style);
|
|
|
|
auto pb = para::ParagraphBuilderImpl::make(ps, fc);
|
|
|
|
return std::unique_ptr<para::ParagraphBuilderImpl>(
|
|
|
|
static_cast<para::ParagraphBuilderImpl*>(pb.release()));
|
|
|
|
}),
|
|
|
|
allow_raw_pointers())
|
|
|
|
.class_function(
|
|
|
|
"_MakeFromFontProvider",
|
|
|
|
optional_override([](SimpleParagraphStyle style,
|
|
|
|
sk_sp<para::TypefaceFontProvider> fontProvider)
|
|
|
|
-> std::unique_ptr<para::ParagraphBuilderImpl> {
|
|
|
|
auto fc = sk_make_sp<para::FontCollection>();
|
|
|
|
fc->setDefaultFontManager(fontProvider);
|
|
|
|
fc->enableFontFallback();
|
|
|
|
auto ps = toParagraphStyle(style);
|
|
|
|
auto pb = para::ParagraphBuilderImpl::make(ps, fc);
|
|
|
|
return std::unique_ptr<para::ParagraphBuilderImpl>(
|
|
|
|
static_cast<para::ParagraphBuilderImpl*>(pb.release()));
|
|
|
|
}),
|
|
|
|
allow_raw_pointers())
|
2021-05-06 22:12:27 +00:00
|
|
|
.class_function(
|
|
|
|
"_ShapeText",
|
|
|
|
optional_override([](JSString jtext, JSArray jruns, float width) -> JSArray {
|
|
|
|
std::string textStorage = jtext.as<std::string>();
|
|
|
|
const char* text = textStorage.data();
|
|
|
|
size_t textCount = textStorage.size();
|
|
|
|
|
|
|
|
auto fc = sk_make_sp<para::FontCollection>();
|
|
|
|
fc->setDefaultFontManager(SkFontMgr::RefDefault());
|
|
|
|
fc->enableFontFallback();
|
|
|
|
|
|
|
|
para::ParagraphStyle pstyle;
|
|
|
|
{
|
|
|
|
// For the most part this is ignored, since we set an explicit TextStyle
|
|
|
|
// for all of our text runs, but it is required by SkParagraph.
|
|
|
|
para::TextStyle style;
|
|
|
|
style.setFontFamilies({SkString("sans-serif")});
|
|
|
|
style.setFontSize(32);
|
|
|
|
pstyle.setTextStyle(style);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto pb = para::ParagraphBuilder::make(pstyle, fc);
|
|
|
|
|
|
|
|
// tease apart the FontBlock runs
|
|
|
|
size_t runCount = jruns["length"].as<size_t>();
|
|
|
|
for (size_t i = 0; i < runCount; ++i) {
|
|
|
|
emscripten::val r = jruns[i];
|
|
|
|
|
|
|
|
para::TextStyle style;
|
|
|
|
style.setTypeface(r["typeface"].as< sk_sp<SkTypeface> >());
|
|
|
|
style.setFontSize(r["size"].as<float>());
|
|
|
|
|
|
|
|
const size_t subTextCount = r["length"].as<size_t>();
|
|
|
|
if (subTextCount > textCount) {
|
|
|
|
return emscripten::val("block runs exceed text length!");
|
|
|
|
}
|
|
|
|
|
|
|
|
pb->pushStyle(style);
|
|
|
|
pb->addText(text, subTextCount);
|
|
|
|
pb->pop();
|
|
|
|
|
|
|
|
text += subTextCount;
|
|
|
|
textCount -= subTextCount;
|
|
|
|
}
|
|
|
|
if (textCount != 0) {
|
|
|
|
return emscripten::val("Didn't have enough block runs to cover text");
|
|
|
|
}
|
|
|
|
|
|
|
|
auto pa = pb->Build();
|
|
|
|
pa->layout(width);
|
|
|
|
|
|
|
|
// workaround until this is fixed in SkParagraph
|
|
|
|
{
|
|
|
|
SkPictureRecorder rec;
|
|
|
|
pa->paint(rec.beginRecording({0,0,9999,9999}), 0, 0);
|
|
|
|
}
|
|
|
|
return GetShapedLines(*pa);
|
|
|
|
}),
|
|
|
|
allow_raw_pointers())
|
2020-10-02 22:24:13 +00:00
|
|
|
.function("addText",
|
|
|
|
optional_override([](para::ParagraphBuilderImpl& self, std::string text) {
|
|
|
|
return self.addText(text.c_str(), text.length());
|
|
|
|
}))
|
|
|
|
.function("build", ¶::ParagraphBuilderImpl::Build, allow_raw_pointers())
|
|
|
|
.function("pop", ¶::ParagraphBuilderImpl::pop)
|
2022-02-02 15:19:31 +00:00
|
|
|
.function("reset", ¶::ParagraphBuilderImpl::Reset, allow_raw_pointers())
|
2020-10-02 22:24:13 +00:00
|
|
|
.function("_pushStyle", optional_override([](para::ParagraphBuilderImpl& self,
|
|
|
|
SimpleTextStyle textStyle) {
|
|
|
|
auto ts = toTextStyle(textStyle);
|
|
|
|
self.pushStyle(ts);
|
|
|
|
}))
|
|
|
|
// A method of pushing a textStyle with paints instead of colors for foreground and
|
|
|
|
// background. Since SimpleTextStyle is a value object, it cannot contain paints, which
|
|
|
|
// are not primitives. This binding is here to accept them. Any color that is specified
|
|
|
|
// in the textStyle is overridden.
|
|
|
|
.function("_pushPaintStyle",
|
|
|
|
optional_override([](para::ParagraphBuilderImpl& self,
|
|
|
|
SimpleTextStyle textStyle, SkPaint foreground,
|
|
|
|
SkPaint background) {
|
|
|
|
auto ts = toTextStyle(textStyle);
|
|
|
|
ts.setForegroundColor(foreground);
|
|
|
|
ts.setBackgroundColor(background);
|
|
|
|
self.pushStyle(ts);
|
|
|
|
}))
|
|
|
|
.function("_addPlaceholder", optional_override([](para::ParagraphBuilderImpl& self,
|
|
|
|
SkScalar width,
|
|
|
|
SkScalar height,
|
|
|
|
para::PlaceholderAlignment alignment,
|
|
|
|
para::TextBaseline baseline,
|
|
|
|
SkScalar offset) {
|
|
|
|
para::PlaceholderStyle ps(width, height, alignment, baseline, offset);
|
|
|
|
self.addPlaceholder(ps);
|
|
|
|
}));
|
2019-10-03 15:22:08 +00:00
|
|
|
|
2020-08-04 20:21:09 +00:00
|
|
|
class_<para::TypefaceFontProvider, base<SkFontMgr>>("TypefaceFontProvider")
|
|
|
|
.smart_ptr<sk_sp<para::TypefaceFontProvider>>("sk_sp<TypefaceFontProvider>")
|
|
|
|
.class_function("Make", optional_override([]()-> sk_sp<para::TypefaceFontProvider> {
|
|
|
|
return sk_make_sp<para::TypefaceFontProvider>();
|
|
|
|
}))
|
|
|
|
.function("_registerFont", optional_override([](para::TypefaceFontProvider& self,
|
|
|
|
sk_sp<SkTypeface> typeface,
|
2021-05-26 17:40:22 +00:00
|
|
|
WASMPointerU8 familyPtr) {
|
2020-08-04 20:21:09 +00:00
|
|
|
const char* fPtr = reinterpret_cast<const char*>(familyPtr);
|
|
|
|
SkString fStr(fPtr);
|
|
|
|
self.registerTypeface(typeface, fStr);
|
|
|
|
}), allow_raw_pointers());
|
|
|
|
|
2019-10-03 15:22:08 +00:00
|
|
|
|
2020-10-06 12:24:00 +00:00
|
|
|
// These value objects make it easier to send data across the wire.
|
2019-10-03 15:22:08 +00:00
|
|
|
value_object<para::PositionWithAffinity>("PositionWithAffinity")
|
|
|
|
.field("pos", ¶::PositionWithAffinity::position)
|
|
|
|
.field("affinity", ¶::PositionWithAffinity::affinity);
|
|
|
|
|
2019-11-15 19:48:55 +00:00
|
|
|
value_object<SimpleFontStyle>("FontStyle")
|
2019-10-21 14:50:26 +00:00
|
|
|
.field("slant", &SimpleFontStyle::slant)
|
|
|
|
.field("weight", &SimpleFontStyle::weight)
|
|
|
|
.field("width", &SimpleFontStyle::width);
|
|
|
|
|
2019-10-03 15:22:08 +00:00
|
|
|
value_object<SimpleParagraphStyle>("ParagraphStyle")
|
2021-04-22 17:53:35 +00:00
|
|
|
.field("disableHinting", &SimpleParagraphStyle::disableHinting)
|
|
|
|
.field("_ellipsisPtr", &SimpleParagraphStyle::ellipsisPtr)
|
|
|
|
.field("_ellipsisLen", &SimpleParagraphStyle::ellipsisLen)
|
|
|
|
.field("heightMultiplier", &SimpleParagraphStyle::heightMultiplier)
|
|
|
|
.field("maxLines", &SimpleParagraphStyle::maxLines)
|
|
|
|
.field("textAlign", &SimpleParagraphStyle::textAlign)
|
|
|
|
.field("textDirection", &SimpleParagraphStyle::textDirection)
|
|
|
|
.field("textHeightBehavior", &SimpleParagraphStyle::textHeightBehavior)
|
|
|
|
.field("textStyle", &SimpleParagraphStyle::textStyle)
|
|
|
|
.field("strutStyle", &SimpleParagraphStyle::strutStyle);
|
2020-10-02 22:24:13 +00:00
|
|
|
|
|
|
|
value_object<SimpleStrutStyle>("StrutStyle")
|
|
|
|
.field("_fontFamiliesPtr", &SimpleStrutStyle::fontFamiliesPtr)
|
|
|
|
.field("_fontFamiliesLen", &SimpleStrutStyle::fontFamiliesLen)
|
|
|
|
.field("strutEnabled", &SimpleStrutStyle::strutEnabled)
|
|
|
|
.field("fontSize", &SimpleStrutStyle::fontSize)
|
|
|
|
.field("fontStyle", &SimpleStrutStyle::fontStyle)
|
|
|
|
.field("heightMultiplier", &SimpleStrutStyle::heightMultiplier)
|
2021-04-20 17:55:49 +00:00
|
|
|
.field("halfLeading", &SimpleStrutStyle::halfLeading)
|
2020-10-02 22:24:13 +00:00
|
|
|
.field("leading", &SimpleStrutStyle::leading)
|
|
|
|
.field("forceStrutHeight", &SimpleStrutStyle::forceStrutHeight);
|
2019-10-03 15:22:08 +00:00
|
|
|
|
|
|
|
value_object<SimpleTextStyle>("TextStyle")
|
2020-10-02 22:24:13 +00:00
|
|
|
.field("_colorPtr", &SimpleTextStyle::colorPtr)
|
|
|
|
.field("_foregroundColorPtr", &SimpleTextStyle::foregroundColorPtr)
|
|
|
|
.field("_backgroundColorPtr", &SimpleTextStyle::backgroundColorPtr)
|
|
|
|
.field("decoration", &SimpleTextStyle::decoration)
|
|
|
|
.field("decorationThickness", &SimpleTextStyle::decorationThickness)
|
|
|
|
.field("_decorationColorPtr", &SimpleTextStyle::decorationColorPtr)
|
|
|
|
.field("decorationStyle", &SimpleTextStyle::decorationStyle)
|
|
|
|
.field("_fontFamiliesPtr", &SimpleTextStyle::fontFamiliesPtr)
|
|
|
|
.field("_fontFamiliesLen", &SimpleTextStyle::fontFamiliesLen)
|
|
|
|
.field("fontSize", &SimpleTextStyle::fontSize)
|
|
|
|
.field("letterSpacing", &SimpleTextStyle::letterSpacing)
|
|
|
|
.field("wordSpacing", &SimpleTextStyle::wordSpacing)
|
|
|
|
.field("heightMultiplier", &SimpleTextStyle::heightMultiplier)
|
2021-04-20 17:55:49 +00:00
|
|
|
.field("halfLeading", &SimpleTextStyle::halfLeading)
|
2020-10-02 22:24:13 +00:00
|
|
|
.field("_localePtr", &SimpleTextStyle::localePtr)
|
|
|
|
.field("_localeLen", &SimpleTextStyle::localeLen)
|
|
|
|
.field("fontStyle", &SimpleTextStyle::fontStyle)
|
|
|
|
.field("_shadowLen", &SimpleTextStyle::shadowLen)
|
|
|
|
.field("_shadowColorsPtr", &SimpleTextStyle::shadowColorsPtr)
|
|
|
|
.field("_shadowOffsetsPtr", &SimpleTextStyle::shadowOffsetsPtr)
|
|
|
|
.field("_shadowBlurRadiiPtr", &SimpleTextStyle::shadowBlurRadiiPtr)
|
|
|
|
.field("_fontFeatureLen", &SimpleTextStyle::fontFeatureLen)
|
|
|
|
.field("_fontFeatureNamesPtr", &SimpleTextStyle::fontFeatureNamesPtr)
|
|
|
|
.field("_fontFeatureValuesPtr", &SimpleTextStyle::fontFeatureValuesPtr);
|
2019-10-03 15:22:08 +00:00
|
|
|
|
2019-11-15 19:48:55 +00:00
|
|
|
// The U stands for unsigned - we can't bind a generic/template object, so we have to specify it
|
|
|
|
// with the type we are using.
|
2020-10-06 12:24:00 +00:00
|
|
|
// TODO(kjlubick) make this a typedarray.
|
2019-11-15 19:48:55 +00:00
|
|
|
value_object<para::SkRange<size_t>>("URange")
|
|
|
|
.field("start", ¶::SkRange<size_t>::start)
|
|
|
|
.field("end", ¶::SkRange<size_t>::end);
|
|
|
|
|
2019-10-03 15:22:08 +00:00
|
|
|
// TextDecoration should be a const because they can be combined
|
|
|
|
constant("NoDecoration", int(para::TextDecoration::kNoDecoration));
|
|
|
|
constant("UnderlineDecoration", int(para::TextDecoration::kUnderline));
|
|
|
|
constant("OverlineDecoration", int(para::TextDecoration::kOverline));
|
|
|
|
constant("LineThroughDecoration", int(para::TextDecoration::kLineThrough));
|
|
|
|
}
|