2020-05-14 12:27:53 +00:00
|
|
|
describe('Paragraph Behavior', function() {
|
2020-04-03 19:11:42 +00:00
|
|
|
let container;
|
2019-10-03 15:22:08 +00:00
|
|
|
|
|
|
|
let notoSerifFontBuffer = null;
|
|
|
|
// This font is known to support kerning
|
|
|
|
const notoSerifFontLoaded = fetch('/assets/NotoSerif-Regular.ttf').then(
|
|
|
|
(response) => response.arrayBuffer()).then(
|
|
|
|
(buffer) => {
|
|
|
|
notoSerifFontBuffer = buffer;
|
|
|
|
});
|
|
|
|
|
2019-10-21 14:50:26 +00:00
|
|
|
let notoSerifBoldItalicFontBuffer = null;
|
|
|
|
const notoSerifBoldItalicFontLoaded = fetch('/assets/NotoSerif-BoldItalic.ttf').then(
|
|
|
|
(response) => response.arrayBuffer()).then(
|
|
|
|
(buffer) => {
|
|
|
|
notoSerifBoldItalicFontBuffer = buffer;
|
|
|
|
});
|
|
|
|
|
2019-10-03 15:22:08 +00:00
|
|
|
let emojiFontBuffer = null;
|
|
|
|
const emojiFontLoaded = fetch('/assets/NotoColorEmoji.ttf').then(
|
|
|
|
(response) => response.arrayBuffer()).then(
|
|
|
|
(buffer) => {
|
|
|
|
emojiFontBuffer = buffer;
|
|
|
|
});
|
|
|
|
|
2020-10-02 22:24:13 +00:00
|
|
|
let robotoFontBuffer = null;
|
|
|
|
const robotoFontLoaded = fetch('/assets/Roboto-Regular.otf').then(
|
|
|
|
(response) => response.arrayBuffer()).then(
|
|
|
|
(buffer) => {
|
|
|
|
robotoFontBuffer = buffer;
|
|
|
|
});
|
|
|
|
|
2020-04-03 19:11:42 +00:00
|
|
|
beforeEach(async () => {
|
|
|
|
await LoadCanvasKit;
|
|
|
|
await notoSerifFontLoaded;
|
|
|
|
await notoSerifBoldItalicFontLoaded;
|
|
|
|
await emojiFontLoaded;
|
|
|
|
container = document.createElement('div');
|
|
|
|
container.innerHTML = `
|
|
|
|
<canvas width=600 height=600 id=test></canvas>
|
|
|
|
<canvas width=600 height=600 id=report></canvas>`;
|
|
|
|
document.body.appendChild(container);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
document.body.removeChild(container);
|
|
|
|
});
|
|
|
|
|
|
|
|
gm('paragraph_basic', (canvas) => {
|
2020-10-07 20:09:22 +00:00
|
|
|
const paint = new CanvasKit.Paint();
|
2020-04-03 19:11:42 +00:00
|
|
|
|
|
|
|
paint.setColor(CanvasKit.RED);
|
|
|
|
paint.setStyle(CanvasKit.PaintStyle.Stroke);
|
|
|
|
|
2020-10-07 20:09:22 +00:00
|
|
|
const fontMgr = CanvasKit.FontMgr.FromData(notoSerifFontBuffer);
|
2020-04-03 19:11:42 +00:00
|
|
|
expect(fontMgr.countFamilies()).toEqual(1);
|
|
|
|
expect(fontMgr.getFamilyName(0)).toEqual('Noto Serif');
|
|
|
|
|
|
|
|
const wrapTo = 200;
|
|
|
|
|
|
|
|
const paraStyle = new CanvasKit.ParagraphStyle({
|
|
|
|
textStyle: {
|
|
|
|
color: CanvasKit.BLACK,
|
2019-10-21 14:50:26 +00:00
|
|
|
fontFamilies: ['Noto Serif'],
|
2020-04-03 19:11:42 +00:00
|
|
|
fontSize: 20,
|
|
|
|
},
|
|
|
|
textAlign: CanvasKit.TextAlign.Center,
|
|
|
|
maxLines: 8,
|
|
|
|
ellipsis: '.._.',
|
|
|
|
});
|
|
|
|
|
|
|
|
const builder = CanvasKit.ParagraphBuilder.Make(paraStyle, fontMgr);
|
|
|
|
builder.addText('VAVAVAVAVAVAVA\nVAVA\n');
|
|
|
|
|
|
|
|
const blueText = new CanvasKit.TextStyle({
|
|
|
|
backgroundColor: CanvasKit.Color(234, 208, 232), // light pink
|
|
|
|
color: CanvasKit.Color(48, 37, 199),
|
|
|
|
fontFamilies: ['Noto Serif'],
|
|
|
|
decoration: CanvasKit.LineThroughDecoration,
|
|
|
|
decorationThickness: 1.5, // multiplier based on font size
|
|
|
|
fontSize: 24,
|
|
|
|
});
|
2020-06-11 17:48:16 +00:00
|
|
|
builder.pushStyle(blueText);
|
2020-04-03 19:11:42 +00:00
|
|
|
builder.addText(`Gosh I hope this wraps at some point, it is such a long line.`)
|
|
|
|
builder.pop();
|
|
|
|
builder.addText(` I'm done with the blue now. `)
|
|
|
|
builder.addText(`Now I hope we should stop before we get 8 lines tall. `);
|
|
|
|
const paragraph = builder.build();
|
|
|
|
|
|
|
|
paragraph.layout(wrapTo);
|
|
|
|
|
|
|
|
expect(paragraph.didExceedMaxLines()).toBeTruthy();
|
|
|
|
expect(paragraph.getAlphabeticBaseline()).toBeCloseTo(21.377, 3);
|
|
|
|
expect(paragraph.getHeight()).toEqual(240);
|
|
|
|
expect(paragraph.getIdeographicBaseline()).toBeCloseTo(27.236, 3);
|
|
|
|
expect(paragraph.getLongestLine()).toBeCloseTo(193.820, 3);
|
|
|
|
expect(paragraph.getMaxIntrinsicWidth()).toBeCloseTo(1444.250, 3);
|
|
|
|
expect(paragraph.getMaxWidth()).toEqual(200);
|
|
|
|
expect(paragraph.getMinIntrinsicWidth()).toBeCloseTo(172.360, 3);
|
|
|
|
expect(paragraph.getWordBoundary(8)).toEqual({
|
|
|
|
start: 0,
|
|
|
|
end: 14,
|
|
|
|
});
|
|
|
|
expect(paragraph.getWordBoundary(25)).toEqual({
|
|
|
|
start: 25,
|
|
|
|
end: 26,
|
|
|
|
});
|
|
|
|
|
2020-11-04 14:46:22 +00:00
|
|
|
|
|
|
|
const lineMetrics = paragraph.getLineMetrics();
|
|
|
|
expect(lineMetrics.length).toEqual(8); // 8 lines worth of metrics
|
|
|
|
const flm = lineMetrics[0]; // First Line Metric
|
|
|
|
expect(flm.startIndex).toEqual(0);
|
|
|
|
expect(flm.endIndex).toEqual(15);
|
|
|
|
expect(flm.endExcludingWhitespaces).toEqual(14);
|
|
|
|
expect(flm.endIncludingNewline).toEqual(15);
|
|
|
|
expect(flm.lineNumber).toEqual(0);
|
|
|
|
expect(flm.isHardBreak).toEqual(true);
|
|
|
|
expect(flm.ascent).toBeCloseTo(21.377, 3);
|
|
|
|
expect(flm.descent).toBeCloseTo(5.859, 3);
|
|
|
|
expect(flm.height).toBeCloseTo(27.000, 3);
|
|
|
|
expect(flm.width).toBeCloseTo(172.360, 3);
|
|
|
|
expect(flm.left).toBeCloseTo(13.818, 3);
|
|
|
|
expect(flm.baseline).toBeCloseTo(21.141, 3);
|
|
|
|
|
2020-10-02 22:24:13 +00:00
|
|
|
canvas.clear(CanvasKit.WHITE);
|
2020-04-03 19:11:42 +00:00
|
|
|
canvas.drawRect(CanvasKit.LTRBRect(10, 10, wrapTo+10, 230), paint);
|
|
|
|
canvas.drawParagraph(paragraph, 10, 10);
|
|
|
|
|
|
|
|
paint.delete();
|
|
|
|
fontMgr.delete();
|
|
|
|
paragraph.delete();
|
|
|
|
builder.delete();
|
2019-10-03 15:22:08 +00:00
|
|
|
});
|
|
|
|
|
2020-06-11 17:48:16 +00:00
|
|
|
gm('paragraph_foreground_and_background_color', (canvas) => {
|
2020-10-07 20:09:22 +00:00
|
|
|
const fontMgr = CanvasKit.FontMgr.FromData(notoSerifFontBuffer);
|
2020-06-11 17:48:16 +00:00
|
|
|
expect(fontMgr.countFamilies()).toEqual(1);
|
|
|
|
expect(fontMgr.getFamilyName(0)).toEqual('Noto Serif');
|
|
|
|
|
|
|
|
const wrapTo = 200;
|
|
|
|
|
|
|
|
const paraStyle = new CanvasKit.ParagraphStyle({
|
|
|
|
textStyle: {
|
|
|
|
foregroundColor: CanvasKit.Color4f(1.0, 0, 0, 0.8),
|
|
|
|
backgroundColor: CanvasKit.Color4f(0, 0, 1.0, 0.8),
|
|
|
|
// color should default to black
|
|
|
|
fontFamilies: ['Noto Serif'],
|
|
|
|
fontSize: 20,
|
|
|
|
},
|
|
|
|
|
|
|
|
textAlign: CanvasKit.TextAlign.Center,
|
|
|
|
});
|
|
|
|
const builder = CanvasKit.ParagraphBuilder.Make(paraStyle, fontMgr);
|
|
|
|
builder.addText(
|
|
|
|
'This text has a red foregroundColor and a blue backgroundColor.');
|
|
|
|
const paragraph = builder.build();
|
|
|
|
paragraph.layout(300);
|
|
|
|
|
2020-10-02 22:24:13 +00:00
|
|
|
canvas.clear(CanvasKit.WHITE);
|
2020-06-11 17:48:16 +00:00
|
|
|
canvas.drawParagraph(paragraph, 10, 10);
|
|
|
|
|
2020-08-04 13:06:54 +00:00
|
|
|
fontMgr.delete();
|
|
|
|
paragraph.delete();
|
|
|
|
builder.delete();
|
|
|
|
});
|
|
|
|
|
|
|
|
gm('paragraph_foreground_stroke_paint', (canvas) => {
|
2020-10-07 20:09:22 +00:00
|
|
|
const fontMgr = CanvasKit.FontMgr.FromData(notoSerifFontBuffer);
|
2020-08-04 13:06:54 +00:00
|
|
|
expect(fontMgr.countFamilies()).toEqual(1);
|
|
|
|
expect(fontMgr.getFamilyName(0)).toEqual('Noto Serif');
|
|
|
|
|
|
|
|
const wrapTo = 200;
|
|
|
|
|
|
|
|
const textStyle = {
|
|
|
|
fontFamilies: ['Noto Serif'],
|
|
|
|
fontSize: 40,
|
|
|
|
};
|
|
|
|
const paraStyle = new CanvasKit.ParagraphStyle({
|
|
|
|
textStyle: textStyle,
|
|
|
|
textAlign: CanvasKit.TextAlign.Center,
|
|
|
|
});
|
|
|
|
const builder = CanvasKit.ParagraphBuilder.Make(paraStyle, fontMgr);
|
|
|
|
|
2020-10-07 20:09:22 +00:00
|
|
|
const fg = new CanvasKit.Paint();
|
2020-08-04 13:06:54 +00:00
|
|
|
fg.setColor(CanvasKit.BLACK);
|
|
|
|
fg.setStyle(CanvasKit.PaintStyle.Stroke);
|
|
|
|
|
2020-10-07 20:09:22 +00:00
|
|
|
const bg = new CanvasKit.Paint();
|
2020-08-04 13:06:54 +00:00
|
|
|
bg.setColor(CanvasKit.TRANSPARENT);
|
|
|
|
|
|
|
|
builder.pushPaintStyle(textStyle, fg, bg);
|
|
|
|
builder.addText(
|
|
|
|
'This text is stroked in black and has no fill');
|
|
|
|
const paragraph = builder.build();
|
|
|
|
paragraph.layout(300);
|
|
|
|
|
2020-10-02 22:24:13 +00:00
|
|
|
canvas.clear(CanvasKit.WHITE);
|
2020-08-04 13:06:54 +00:00
|
|
|
canvas.drawParagraph(paragraph, 10, 10);
|
|
|
|
// Again 5px to the right so you can tell the fill is transparent
|
|
|
|
canvas.drawParagraph(paragraph, 15, 10);
|
|
|
|
|
|
|
|
fg.delete();
|
|
|
|
bg.delete();
|
2020-06-11 17:48:16 +00:00
|
|
|
fontMgr.delete();
|
|
|
|
paragraph.delete();
|
|
|
|
builder.delete();
|
|
|
|
});
|
|
|
|
|
2020-10-02 22:24:13 +00:00
|
|
|
gm('paragraph_letter_word_spacing', (canvas) => {
|
2020-10-07 20:09:22 +00:00
|
|
|
const fontMgr = CanvasKit.FontMgr.FromData(notoSerifFontBuffer);
|
2020-10-02 22:24:13 +00:00
|
|
|
expect(fontMgr.countFamilies()).toEqual(1);
|
|
|
|
expect(fontMgr.getFamilyName(0)).toEqual('Noto Serif');
|
|
|
|
|
|
|
|
const wrapTo = 200;
|
|
|
|
|
|
|
|
const paraStyle = new CanvasKit.ParagraphStyle({
|
|
|
|
textStyle: {
|
|
|
|
// color should default to black
|
|
|
|
fontFamilies: ['Noto Serif'],
|
|
|
|
fontSize: 20,
|
|
|
|
letterSpacing: 5,
|
|
|
|
wordSpacing: 10,
|
|
|
|
},
|
|
|
|
|
|
|
|
textAlign: CanvasKit.TextAlign.Center,
|
|
|
|
});
|
|
|
|
const builder = CanvasKit.ParagraphBuilder.Make(paraStyle, fontMgr);
|
|
|
|
builder.addText(
|
|
|
|
'This text should have a lot of space between the letters and words.');
|
|
|
|
const paragraph = builder.build();
|
|
|
|
paragraph.layout(300);
|
|
|
|
|
|
|
|
canvas.clear(CanvasKit.WHITE);
|
|
|
|
canvas.drawParagraph(paragraph, 10, 10);
|
|
|
|
|
|
|
|
fontMgr.delete();
|
|
|
|
paragraph.delete();
|
|
|
|
builder.delete();
|
|
|
|
});
|
|
|
|
|
|
|
|
gm('paragraph_shadows', (canvas) => {
|
2020-10-07 20:09:22 +00:00
|
|
|
const fontMgr = CanvasKit.FontMgr.FromData(notoSerifFontBuffer);
|
2020-10-02 22:24:13 +00:00
|
|
|
expect(fontMgr.countFamilies()).toEqual(1);
|
|
|
|
expect(fontMgr.getFamilyName(0)).toEqual('Noto Serif');
|
|
|
|
|
|
|
|
const wrapTo = 200;
|
|
|
|
|
|
|
|
const paraStyle = new CanvasKit.ParagraphStyle({
|
|
|
|
textStyle: {
|
|
|
|
color: CanvasKit.WHITE,
|
|
|
|
fontFamilies: ['Noto Serif'],
|
|
|
|
fontSize: 20,
|
|
|
|
shadows: [{color: CanvasKit.BLACK, blurRadius: 15},
|
|
|
|
{color: CanvasKit.RED, blurRadius: 5, offset: [10, 10]}],
|
|
|
|
},
|
|
|
|
|
|
|
|
textAlign: CanvasKit.TextAlign.Center,
|
|
|
|
});
|
|
|
|
const builder = CanvasKit.ParagraphBuilder.Make(paraStyle, fontMgr);
|
|
|
|
builder.addText('This text should have a shadow behind it.');
|
|
|
|
const paragraph = builder.build();
|
|
|
|
paragraph.layout(300);
|
|
|
|
|
|
|
|
canvas.clear(CanvasKit.WHITE);
|
|
|
|
canvas.drawParagraph(paragraph, 10, 10);
|
|
|
|
|
|
|
|
fontMgr.delete();
|
|
|
|
paragraph.delete();
|
|
|
|
builder.delete();
|
|
|
|
});
|
|
|
|
|
|
|
|
gm('paragraph_strut_style', (canvas) => {
|
2020-10-07 20:09:22 +00:00
|
|
|
const fontMgr = CanvasKit.FontMgr.FromData(robotoFontBuffer);
|
2020-10-02 22:24:13 +00:00
|
|
|
expect(fontMgr.countFamilies()).toEqual(1);
|
|
|
|
expect(fontMgr.getFamilyName(0)).toEqual('Roboto');
|
|
|
|
|
|
|
|
// The lines in this paragraph should have the same height despite the third
|
|
|
|
// line having a larger font size.
|
|
|
|
const paraStrutStyle = new CanvasKit.ParagraphStyle({
|
|
|
|
textStyle: {
|
|
|
|
fontFamilies: ['Roboto'],
|
|
|
|
color: CanvasKit.BLACK,
|
|
|
|
},
|
|
|
|
strutStyle: {
|
|
|
|
strutEnabled: true,
|
|
|
|
fontFamilies: ['Roboto'],
|
|
|
|
fontSize: 28,
|
|
|
|
heightMultiplier: 1.5,
|
|
|
|
forceStrutHeight: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const paraStyle = new CanvasKit.ParagraphStyle({
|
|
|
|
textStyle: {
|
|
|
|
fontFamilies: ['Roboto'],
|
|
|
|
color: CanvasKit.BLACK,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const roboto28Style = new CanvasKit.TextStyle({
|
|
|
|
color: CanvasKit.BLACK,
|
|
|
|
fontFamilies: ['Roboto'],
|
|
|
|
fontSize: 28,
|
|
|
|
});
|
|
|
|
const roboto32Style = new CanvasKit.TextStyle({
|
|
|
|
color: CanvasKit.BLACK,
|
|
|
|
fontFamilies: ['Roboto'],
|
|
|
|
fontSize: 32,
|
|
|
|
});
|
|
|
|
const builder = CanvasKit.ParagraphBuilder.Make(paraStrutStyle, fontMgr);
|
|
|
|
builder.pushStyle(roboto28Style);
|
|
|
|
builder.addText('This paragraph\n');
|
|
|
|
builder.pushStyle(roboto32Style);
|
|
|
|
builder.addText('is using\n');
|
|
|
|
builder.pop();
|
|
|
|
builder.pushStyle(roboto28Style);
|
|
|
|
builder.addText('a strut style!\n');
|
|
|
|
builder.pop();
|
|
|
|
builder.pop();
|
|
|
|
|
|
|
|
const builder2 = CanvasKit.ParagraphBuilder.Make(paraStyle, fontMgr);
|
|
|
|
builder2.pushStyle(roboto28Style);
|
|
|
|
builder2.addText('This paragraph\n');
|
|
|
|
builder2.pushStyle(roboto32Style);
|
|
|
|
builder2.addText('is not using\n');
|
|
|
|
builder2.pop();
|
|
|
|
builder2.pushStyle(roboto28Style);
|
|
|
|
builder2.addText('a strut style!\n');
|
|
|
|
builder2.pop();
|
|
|
|
builder2.pop();
|
|
|
|
|
|
|
|
const paragraph = builder.build();
|
|
|
|
paragraph.layout(300);
|
|
|
|
|
|
|
|
const paragraph2 = builder2.build();
|
|
|
|
paragraph2.layout(300);
|
|
|
|
|
|
|
|
canvas.clear(CanvasKit.WHITE);
|
|
|
|
canvas.drawParagraph(paragraph, 10, 10);
|
|
|
|
canvas.drawParagraph(paragraph2, 220, 10);
|
|
|
|
|
|
|
|
fontMgr.delete();
|
|
|
|
paragraph.delete();
|
|
|
|
builder.delete();
|
|
|
|
});
|
|
|
|
|
|
|
|
gm('paragraph_font_features', (canvas) => {
|
2020-10-07 20:09:22 +00:00
|
|
|
const fontMgr = CanvasKit.FontMgr.FromData(robotoFontBuffer);
|
2020-10-02 22:24:13 +00:00
|
|
|
expect(fontMgr.countFamilies()).toEqual(1);
|
|
|
|
expect(fontMgr.getFamilyName(0)).toEqual('Roboto');
|
|
|
|
|
|
|
|
|
|
|
|
const paraStyle = new CanvasKit.ParagraphStyle({
|
|
|
|
textStyle: {
|
|
|
|
color: CanvasKit.BLACK,
|
|
|
|
fontFamilies: ['Roboto'],
|
|
|
|
fontSize: 30,
|
|
|
|
fontFeatures: [{name: 'smcp', value: 1}]
|
|
|
|
},
|
|
|
|
textAlign: CanvasKit.TextAlign.Center,
|
|
|
|
});
|
|
|
|
const builder = CanvasKit.ParagraphBuilder.Make(paraStyle, fontMgr);
|
|
|
|
builder.addText('This Text Should Be In Small Caps');
|
|
|
|
const paragraph = builder.build();
|
|
|
|
paragraph.layout(300);
|
|
|
|
|
|
|
|
canvas.clear(CanvasKit.WHITE);
|
|
|
|
canvas.drawParagraph(paragraph, 10, 10);
|
|
|
|
|
|
|
|
fontMgr.delete();
|
|
|
|
paragraph.delete();
|
|
|
|
builder.delete();
|
|
|
|
});
|
|
|
|
|
|
|
|
gm('paragraph_placeholders', (canvas) => {
|
2020-10-07 20:09:22 +00:00
|
|
|
const fontMgr = CanvasKit.FontMgr.FromData(robotoFontBuffer);
|
2020-10-02 22:24:13 +00:00
|
|
|
expect(fontMgr.countFamilies()).toEqual(1);
|
|
|
|
expect(fontMgr.getFamilyName(0)).toEqual('Roboto');
|
|
|
|
|
|
|
|
|
|
|
|
const paraStyle = new CanvasKit.ParagraphStyle({
|
|
|
|
textStyle: {
|
|
|
|
color: CanvasKit.BLACK,
|
|
|
|
fontFamilies: ['Roboto'],
|
|
|
|
fontSize: 20,
|
|
|
|
},
|
|
|
|
textAlign: CanvasKit.TextAlign.Center,
|
|
|
|
});
|
|
|
|
const builder = CanvasKit.ParagraphBuilder.Make(paraStyle, fontMgr);
|
|
|
|
builder.addText('There should be ');
|
|
|
|
builder.addPlaceholder(10, 10, CanvasKit.PlaceholderAlignment.AboveBaseline,
|
|
|
|
CanvasKit.TextBaseline.Ideographic);
|
|
|
|
builder.addText('a space in this sentence.\n');
|
|
|
|
|
|
|
|
builder.addText('There should be ');
|
|
|
|
builder.addPlaceholder(10, 10, CanvasKit.PlaceholderAlignment.BelowBaseline,
|
|
|
|
CanvasKit.TextBaseline.Ideographic);
|
|
|
|
builder.addText('a dropped space in this sentence.\n');
|
|
|
|
|
|
|
|
builder.addText('There should be ');
|
|
|
|
builder.addPlaceholder(10, 10, null, null, 20);
|
|
|
|
builder.addText('an offset space in this sentence.\n');
|
|
|
|
const paragraph = builder.build();
|
|
|
|
paragraph.layout(300);
|
|
|
|
|
|
|
|
let rects = paragraph.getRectsForPlaceholders();
|
|
|
|
|
|
|
|
canvas.clear(CanvasKit.WHITE);
|
|
|
|
canvas.drawParagraph(paragraph, 10, 10);
|
|
|
|
|
|
|
|
for (const rect of rects) {
|
2020-10-07 20:09:22 +00:00
|
|
|
const p = new CanvasKit.Paint();
|
2020-10-02 22:24:13 +00:00
|
|
|
p.setColor(CanvasKit.Color(0, 0, 255));
|
|
|
|
p.setStyle(CanvasKit.PaintStyle.Stroke);
|
|
|
|
// Account for the (10, 10) offset when we painted the paragraph.
|
|
|
|
const placeholder =
|
|
|
|
CanvasKit.LTRBRect(rect[0]+10,rect[1]+10,rect[2]+10,rect[3]+10);
|
|
|
|
canvas.drawRect(placeholder, p);
|
|
|
|
p.delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
fontMgr.delete();
|
|
|
|
paragraph.delete();
|
|
|
|
builder.delete();
|
|
|
|
});
|
|
|
|
|
2020-06-11 17:48:16 +00:00
|
|
|
// loosely based on SkParagraph_GetRectsForRangeParagraph test in c++ code.
|
2020-04-03 19:11:42 +00:00
|
|
|
gm('paragraph_rects', (canvas) => {
|
2020-10-07 20:09:22 +00:00
|
|
|
const fontMgr = CanvasKit.FontMgr.FromData(notoSerifFontBuffer);
|
2020-04-03 19:11:42 +00:00
|
|
|
|
|
|
|
const wrapTo = 550;
|
|
|
|
const hStyle = CanvasKit.RectHeightStyle.Max;
|
|
|
|
const wStyle = CanvasKit.RectWidthStyle.Tight;
|
|
|
|
|
2020-06-11 17:48:16 +00:00
|
|
|
const mallocedColor = CanvasKit.Malloc(Float32Array, 4);
|
|
|
|
mallocedColor.toTypedArray().set([0.9, 0.1, 0.1, 1.0]);
|
|
|
|
|
2020-04-03 19:11:42 +00:00
|
|
|
const paraStyle = new CanvasKit.ParagraphStyle({
|
|
|
|
textStyle: {
|
2020-06-11 17:48:16 +00:00
|
|
|
color: mallocedColor,
|
2020-04-03 19:11:42 +00:00
|
|
|
fontFamilies: ['Noto Serif'],
|
|
|
|
fontSize: 50,
|
|
|
|
},
|
|
|
|
textAlign: CanvasKit.TextAlign.Left,
|
|
|
|
maxLines: 10,
|
|
|
|
});
|
|
|
|
const builder = CanvasKit.ParagraphBuilder.Make(paraStyle, fontMgr);
|
|
|
|
builder.addText('12345, \"67890\" 12345 67890 12345 67890 12345 67890 12345 67890 12345 67890 12345');
|
|
|
|
const paragraph = builder.build();
|
2020-06-11 17:48:16 +00:00
|
|
|
CanvasKit.Free(mallocedColor);
|
2020-04-03 19:11:42 +00:00
|
|
|
|
|
|
|
paragraph.layout(wrapTo);
|
|
|
|
|
|
|
|
const ranges = [
|
|
|
|
{
|
|
|
|
start: 0,
|
|
|
|
end: 0,
|
|
|
|
expectedNum: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
start: 0,
|
|
|
|
end: 1,
|
|
|
|
expectedNum: 1,
|
|
|
|
color: CanvasKit.Color(200, 0, 200),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
start: 2,
|
|
|
|
end: 8,
|
|
|
|
expectedNum: 1,
|
|
|
|
color: CanvasKit.Color(255, 0, 0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
start: 8,
|
|
|
|
end: 21,
|
|
|
|
expectedNum: 1,
|
|
|
|
color: CanvasKit.Color(0, 255, 0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
start: 30,
|
|
|
|
end: 100,
|
|
|
|
expectedNum: 4,
|
|
|
|
color: CanvasKit.Color(0, 0, 255),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
start: 19,
|
|
|
|
end: 22,
|
|
|
|
expectedNum: 1,
|
|
|
|
color: CanvasKit.Color(0, 200, 200),
|
2019-10-03 15:22:08 +00:00
|
|
|
}
|
2020-04-03 19:11:42 +00:00
|
|
|
];
|
2020-10-02 22:24:13 +00:00
|
|
|
canvas.clear(CanvasKit.WHITE);
|
2020-04-03 19:11:42 +00:00
|
|
|
// Move it down a bit so we can see the rects that go above 0,0
|
|
|
|
canvas.translate(10, 10);
|
|
|
|
canvas.drawParagraph(paragraph, 0, 0);
|
|
|
|
|
|
|
|
for (const test of ranges) {
|
|
|
|
let rects = paragraph.getRectsForRange(test.start, test.end, hStyle, wStyle);
|
|
|
|
expect(Array.isArray(rects)).toEqual(true);
|
|
|
|
expect(rects.length).toEqual(test.expectedNum);
|
|
|
|
|
|
|
|
for (const rect of rects) {
|
2020-05-29 15:06:19 +00:00
|
|
|
expect(rect.direction.value).toEqual(CanvasKit.TextDirection.LTR.value);
|
2020-10-07 20:09:22 +00:00
|
|
|
const p = new CanvasKit.Paint();
|
2020-04-03 19:11:42 +00:00
|
|
|
p.setColor(test.color);
|
|
|
|
p.setStyle(CanvasKit.PaintStyle.Stroke);
|
|
|
|
canvas.drawRect(rect, p);
|
|
|
|
p.delete();
|
2019-10-03 15:22:08 +00:00
|
|
|
}
|
2020-04-03 19:11:42 +00:00
|
|
|
}
|
2021-04-08 12:49:00 +00:00
|
|
|
expect(CanvasKit.RectHeightStyle.Strut).toBeTruthy();
|
2019-10-03 15:22:08 +00:00
|
|
|
|
2020-04-03 19:11:42 +00:00
|
|
|
fontMgr.delete();
|
|
|
|
paragraph.delete();
|
|
|
|
builder.delete();
|
2019-10-03 15:22:08 +00:00
|
|
|
});
|
|
|
|
|
2020-04-03 19:11:42 +00:00
|
|
|
gm('paragraph_emoji', (canvas) => {
|
2020-10-07 20:09:22 +00:00
|
|
|
const fontMgr = CanvasKit.FontMgr.FromData([notoSerifFontBuffer, emojiFontBuffer]);
|
2020-04-03 19:11:42 +00:00
|
|
|
expect(fontMgr.countFamilies()).toEqual(2);
|
|
|
|
expect(fontMgr.getFamilyName(0)).toEqual('Noto Serif');
|
|
|
|
expect(fontMgr.getFamilyName(1)).toEqual('Noto Color Emoji');
|
|
|
|
|
|
|
|
const wrapTo = 450;
|
|
|
|
|
|
|
|
const paraStyle = new CanvasKit.ParagraphStyle({
|
|
|
|
textStyle: {
|
2019-10-28 13:05:54 +00:00
|
|
|
color: CanvasKit.BLACK,
|
2020-04-03 19:11:42 +00:00
|
|
|
// Put text first, otherwise the "emoji space" is used and that looks bad.
|
|
|
|
fontFamilies: ['Noto Serif', 'Noto Color Emoji'],
|
|
|
|
fontSize: 30,
|
|
|
|
},
|
|
|
|
textAlign: CanvasKit.TextAlign.Left,
|
|
|
|
maxLines: 10,
|
|
|
|
});
|
2019-10-03 15:22:08 +00:00
|
|
|
|
2020-04-03 19:11:42 +00:00
|
|
|
const textStyle = new CanvasKit.TextStyle({
|
|
|
|
color: CanvasKit.BLACK,
|
2020-05-14 12:27:53 +00:00
|
|
|
// The number 4 matches an emoji and looks strange w/o this additional style.
|
|
|
|
fontFamilies: ['Noto Serif'],
|
|
|
|
fontSize: 30,
|
2020-04-03 19:11:42 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const builder = CanvasKit.ParagraphBuilder.Make(paraStyle, fontMgr);
|
|
|
|
builder.pushStyle(textStyle);
|
|
|
|
builder.addText('4 flags on following line:\n');
|
|
|
|
builder.pop();
|
|
|
|
builder.addText(`🏳️🌈 🇮🇹 🇱🇷 🇺🇸\n`);
|
|
|
|
builder.addText('Rainbow Italy Liberia USA\n\n');
|
|
|
|
builder.addText('Emoji below should wrap:\n');
|
|
|
|
builder.addText(`🍕🍔🍟🥝🍱🕶🎩👩👩👦👩👩👧👧👩👩👦👩👩👧👧👩👩👦👩👩👧👧👩👩👦👩👩👧👧👩👩👦👩👩👧👧👩👩👦👩👩👧👧👩👩👦👩👩👧👧`);
|
|
|
|
const paragraph = builder.build();
|
|
|
|
|
|
|
|
paragraph.layout(wrapTo);
|
|
|
|
|
2020-10-02 22:24:13 +00:00
|
|
|
canvas.clear(CanvasKit.WHITE);
|
2020-04-03 19:11:42 +00:00
|
|
|
canvas.drawParagraph(paragraph, 10, 10);
|
|
|
|
|
2020-10-07 20:09:22 +00:00
|
|
|
const paint = new CanvasKit.Paint();
|
2020-04-03 19:11:42 +00:00
|
|
|
paint.setColor(CanvasKit.RED);
|
|
|
|
paint.setStyle(CanvasKit.PaintStyle.Stroke);
|
|
|
|
canvas.drawRect(CanvasKit.LTRBRect(10, 10, wrapTo+10, wrapTo+10), paint);
|
|
|
|
|
|
|
|
fontMgr.delete();
|
|
|
|
paint.delete();
|
|
|
|
builder.delete();
|
|
|
|
paragraph.delete();
|
2019-10-03 15:22:08 +00:00
|
|
|
});
|
|
|
|
|
2020-04-03 19:11:42 +00:00
|
|
|
gm('paragraph_hits', (canvas) => {
|
2020-10-07 20:09:22 +00:00
|
|
|
const fontMgr = CanvasKit.FontMgr.FromData([notoSerifFontBuffer]);
|
2020-04-03 19:11:42 +00:00
|
|
|
|
|
|
|
const wrapTo = 300;
|
|
|
|
|
|
|
|
const paraStyle = new CanvasKit.ParagraphStyle({
|
|
|
|
textStyle: {
|
|
|
|
color: CanvasKit.BLACK,
|
|
|
|
fontFamilies: ['Noto Serif'],
|
|
|
|
fontSize: 50,
|
|
|
|
},
|
|
|
|
textAlign: CanvasKit.TextAlign.Left,
|
|
|
|
maxLines: 10,
|
|
|
|
});
|
|
|
|
const builder = CanvasKit.ParagraphBuilder.Make(paraStyle, fontMgr);
|
|
|
|
builder.addText('UNCOPYRIGHTABLE');
|
|
|
|
const paragraph = builder.build();
|
|
|
|
|
|
|
|
paragraph.layout(wrapTo);
|
|
|
|
|
2020-10-02 22:24:13 +00:00
|
|
|
canvas.clear(CanvasKit.WHITE);
|
2020-04-03 19:11:42 +00:00
|
|
|
canvas.translate(10, 10);
|
|
|
|
canvas.drawParagraph(paragraph, 0, 0);
|
|
|
|
|
2020-10-07 20:09:22 +00:00
|
|
|
const paint = new CanvasKit.Paint();
|
2019-10-21 14:50:26 +00:00
|
|
|
|
2020-04-03 19:11:42 +00:00
|
|
|
paint.setColor(CanvasKit.Color(255, 0, 0));
|
|
|
|
paint.setStyle(CanvasKit.PaintStyle.Fill);
|
|
|
|
canvas.drawCircle(20, 30, 3, paint);
|
2019-10-21 14:50:26 +00:00
|
|
|
|
2020-04-03 19:11:42 +00:00
|
|
|
paint.setColor(CanvasKit.Color(0, 0, 255));
|
|
|
|
canvas.drawCircle(80, 90, 3, paint);
|
2019-10-21 14:50:26 +00:00
|
|
|
|
2020-04-03 19:11:42 +00:00
|
|
|
paint.setColor(CanvasKit.Color(0, 255, 0));
|
|
|
|
canvas.drawCircle(280, 2, 3, paint);
|
2019-10-21 14:50:26 +00:00
|
|
|
|
2020-04-03 19:11:42 +00:00
|
|
|
let posU = paragraph.getGlyphPositionAtCoordinate(20, 30);
|
|
|
|
expect(posU).toEqual({
|
|
|
|
pos: 1,
|
|
|
|
affinity: CanvasKit.Affinity.Upstream
|
|
|
|
});
|
|
|
|
let posA = paragraph.getGlyphPositionAtCoordinate(80, 90);
|
|
|
|
expect(posA).toEqual({
|
|
|
|
pos: 11,
|
|
|
|
affinity: CanvasKit.Affinity.Downstream
|
|
|
|
});
|
|
|
|
let posG = paragraph.getGlyphPositionAtCoordinate(280, 2);
|
|
|
|
expect(posG).toEqual({
|
|
|
|
pos: 9,
|
|
|
|
affinity: CanvasKit.Affinity.Upstream
|
|
|
|
});
|
|
|
|
|
|
|
|
builder.delete();
|
|
|
|
paragraph.delete();
|
|
|
|
paint.delete();
|
|
|
|
fontMgr.delete();
|
|
|
|
});
|
|
|
|
|
|
|
|
gm('paragraph_styles', (canvas) => {
|
2020-10-07 20:09:22 +00:00
|
|
|
const paint = new CanvasKit.Paint();
|
2019-10-21 14:50:26 +00:00
|
|
|
|
2020-04-03 19:11:42 +00:00
|
|
|
paint.setColor(CanvasKit.RED);
|
|
|
|
paint.setStyle(CanvasKit.PaintStyle.Stroke);
|
2019-10-21 14:50:26 +00:00
|
|
|
|
2020-10-07 20:09:22 +00:00
|
|
|
const fontMgr = CanvasKit.FontMgr.FromData(notoSerifFontBuffer, notoSerifBoldItalicFontBuffer);
|
2020-04-03 19:11:42 +00:00
|
|
|
|
|
|
|
const wrapTo = 250;
|
|
|
|
|
|
|
|
const paraStyle = new CanvasKit.ParagraphStyle({
|
|
|
|
textStyle: {
|
2019-10-21 14:50:26 +00:00
|
|
|
fontFamilies: ['Noto Serif'],
|
|
|
|
fontSize: 20,
|
|
|
|
fontStyle: {
|
2020-04-03 19:11:42 +00:00
|
|
|
weight: CanvasKit.FontWeight.Light,
|
2019-10-21 14:50:26 +00:00
|
|
|
}
|
2020-04-03 19:11:42 +00:00
|
|
|
},
|
|
|
|
textDirection: CanvasKit.TextDirection.RTL,
|
|
|
|
disableHinting: true,
|
|
|
|
});
|
2019-10-21 14:50:26 +00:00
|
|
|
|
2020-04-03 19:11:42 +00:00
|
|
|
const builder = CanvasKit.ParagraphBuilder.Make(paraStyle, fontMgr);
|
|
|
|
builder.addText('Default text\n');
|
|
|
|
|
|
|
|
const boldItalic = new CanvasKit.TextStyle({
|
|
|
|
color: CanvasKit.RED,
|
|
|
|
fontFamilies: ['Noto Serif'],
|
|
|
|
fontSize: 20,
|
|
|
|
fontStyle: {
|
|
|
|
weight: CanvasKit.FontWeight.Bold,
|
|
|
|
width: CanvasKit.FontWidth.Expanded,
|
|
|
|
slant: CanvasKit.FontSlant.Italic,
|
|
|
|
}
|
|
|
|
});
|
2020-05-14 12:27:53 +00:00
|
|
|
builder.pushStyle(boldItalic);
|
|
|
|
builder.addText(`Bold, Expanded, Italic\n`);
|
2020-04-03 19:11:42 +00:00
|
|
|
builder.pop();
|
|
|
|
builder.addText(`back to normal`);
|
|
|
|
const paragraph = builder.build();
|
|
|
|
|
|
|
|
paragraph.layout(wrapTo);
|
|
|
|
|
2020-10-02 22:24:13 +00:00
|
|
|
canvas.clear(CanvasKit.WHITE);
|
2020-08-04 20:21:09 +00:00
|
|
|
|
|
|
|
canvas.drawRect(CanvasKit.LTRBRect(10, 10, wrapTo+10, wrapTo+10), paint);
|
|
|
|
canvas.drawParagraph(paragraph, 10, 10);
|
|
|
|
|
|
|
|
paint.delete();
|
|
|
|
paragraph.delete();
|
|
|
|
builder.delete();
|
|
|
|
fontMgr.delete();
|
|
|
|
});
|
|
|
|
|
|
|
|
gm('paragraph_font_provider', (canvas) => {
|
2020-10-07 20:09:22 +00:00
|
|
|
const paint = new CanvasKit.Paint();
|
2020-08-04 20:21:09 +00:00
|
|
|
|
|
|
|
paint.setColor(CanvasKit.RED);
|
|
|
|
paint.setStyle(CanvasKit.PaintStyle.Stroke);
|
|
|
|
|
|
|
|
// Register Noto Serif as 'sans-serif'.
|
2020-10-06 12:24:00 +00:00
|
|
|
const fontSrc = CanvasKit.TypefaceFontProvider.Make();
|
|
|
|
fontSrc.registerFont(notoSerifFontBuffer, 'sans-serif');
|
|
|
|
fontSrc.registerFont(notoSerifBoldItalicFontBuffer, 'sans-serif');
|
2020-08-04 20:21:09 +00:00
|
|
|
|
|
|
|
const wrapTo = 250;
|
|
|
|
|
|
|
|
const paraStyle = new CanvasKit.ParagraphStyle({
|
|
|
|
textStyle: {
|
|
|
|
fontFamilies: ['sans-serif'],
|
|
|
|
fontSize: 20,
|
|
|
|
fontStyle: {
|
|
|
|
weight: CanvasKit.FontWeight.Light,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
textDirection: CanvasKit.TextDirection.RTL,
|
|
|
|
disableHinting: true,
|
|
|
|
});
|
|
|
|
|
2020-10-06 12:24:00 +00:00
|
|
|
const builder = CanvasKit.ParagraphBuilder.MakeFromFontProvider(paraStyle, fontSrc);
|
2020-08-04 20:21:09 +00:00
|
|
|
builder.addText('Default text\n');
|
|
|
|
|
|
|
|
const boldItalic = new CanvasKit.TextStyle({
|
|
|
|
color: CanvasKit.RED,
|
|
|
|
fontFamilies: ['sans-serif'],
|
|
|
|
fontSize: 20,
|
|
|
|
fontStyle: {
|
|
|
|
weight: CanvasKit.FontWeight.Bold,
|
|
|
|
width: CanvasKit.FontWidth.Expanded,
|
|
|
|
slant: CanvasKit.FontSlant.Italic,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
builder.pushStyle(boldItalic);
|
|
|
|
builder.addText(`Bold, Expanded, Italic\n`);
|
|
|
|
builder.pop();
|
|
|
|
builder.addText(`back to normal`);
|
|
|
|
const paragraph = builder.build();
|
|
|
|
|
|
|
|
paragraph.layout(wrapTo);
|
|
|
|
|
2020-10-02 22:24:13 +00:00
|
|
|
canvas.clear(CanvasKit.WHITE);
|
2020-08-04 20:21:09 +00:00
|
|
|
|
2020-04-03 19:11:42 +00:00
|
|
|
canvas.drawRect(CanvasKit.LTRBRect(10, 10, wrapTo+10, wrapTo+10), paint);
|
|
|
|
canvas.drawParagraph(paragraph, 10, 10);
|
|
|
|
|
|
|
|
paint.delete();
|
|
|
|
paragraph.delete();
|
|
|
|
builder.delete();
|
2020-10-06 12:24:00 +00:00
|
|
|
fontSrc.delete();
|
2020-04-03 19:11:42 +00:00
|
|
|
});
|
2019-10-21 14:50:26 +00:00
|
|
|
|
2020-10-02 22:24:13 +00:00
|
|
|
gm('paragraph_text_styles', (canvas) => {
|
2020-10-07 20:09:22 +00:00
|
|
|
const paint = new CanvasKit.Paint();
|
2020-10-02 22:24:13 +00:00
|
|
|
|
|
|
|
paint.setColor(CanvasKit.GREEN);
|
|
|
|
paint.setStyle(CanvasKit.PaintStyle.Stroke);
|
|
|
|
|
2020-10-07 20:09:22 +00:00
|
|
|
const fontMgr = CanvasKit.FontMgr.FromData(notoSerifFontBuffer);
|
2020-10-02 22:24:13 +00:00
|
|
|
expect(fontMgr.countFamilies()).toEqual(1);
|
|
|
|
expect(fontMgr.getFamilyName(0)).toEqual('Noto Serif');
|
|
|
|
|
|
|
|
const wrapTo = 200;
|
|
|
|
|
|
|
|
const paraStyle = new CanvasKit.ParagraphStyle({
|
|
|
|
textStyle: {
|
|
|
|
color: CanvasKit.BLACK,
|
|
|
|
fontFamilies: ['Noto Serif'],
|
|
|
|
fontSize: 20,
|
|
|
|
decoration: CanvasKit.UnderlineDecoration,
|
|
|
|
decorationThickness: 1.5, // multiplier based on font size
|
|
|
|
decorationStyle: CanvasKit.DecorationStyle.Wavy,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const builder = CanvasKit.ParagraphBuilder.Make(paraStyle, fontMgr);
|
|
|
|
builder.addText('VAVAVAVAVAVAVA\nVAVA\n');
|
|
|
|
|
|
|
|
const blueText = new CanvasKit.TextStyle({
|
|
|
|
backgroundColor: CanvasKit.Color(234, 208, 232), // light pink
|
|
|
|
color: CanvasKit.Color(48, 37, 199),
|
|
|
|
fontFamilies: ['Noto Serif'],
|
|
|
|
textBaseline: CanvasKit.TextBaseline.Ideographic,
|
|
|
|
decoration: CanvasKit.LineThroughDecoration,
|
|
|
|
decorationThickness: 1.5, // multiplier based on font size
|
|
|
|
});
|
|
|
|
builder.pushStyle(blueText);
|
|
|
|
builder.addText(`Gosh I hope this wraps at some point, it is such a long line.`);
|
|
|
|
builder.pop();
|
|
|
|
builder.addText(` I'm done with the blue now. `);
|
|
|
|
builder.addText(`Now I hope we should stop before we get 8 lines tall. `);
|
|
|
|
const paragraph = builder.build();
|
|
|
|
|
|
|
|
paragraph.layout(wrapTo);
|
|
|
|
|
|
|
|
expect(paragraph.getAlphabeticBaseline()).toBeCloseTo(21.377, 3);
|
|
|
|
expect(paragraph.getHeight()).toEqual(227);
|
|
|
|
expect(paragraph.getIdeographicBaseline()).toBeCloseTo(27.236, 3);
|
|
|
|
expect(paragraph.getLongestLine()).toBeCloseTo(195.664, 3);
|
|
|
|
expect(paragraph.getMaxIntrinsicWidth()).toBeCloseTo(1167.140, 3);
|
|
|
|
expect(paragraph.getMaxWidth()).toEqual(200);
|
|
|
|
expect(paragraph.getMinIntrinsicWidth()).toBeCloseTo(172.360, 3);
|
|
|
|
// Check "VAVAVAVAVAVAVA"
|
|
|
|
expect(paragraph.getWordBoundary(8)).toEqual({
|
|
|
|
start: 0,
|
|
|
|
end: 14,
|
|
|
|
});
|
|
|
|
// Check "I"
|
|
|
|
expect(paragraph.getWordBoundary(25)).toEqual({
|
|
|
|
start: 25,
|
|
|
|
end: 26,
|
|
|
|
});
|
|
|
|
|
|
|
|
canvas.clear(CanvasKit.WHITE);
|
|
|
|
canvas.drawRect(CanvasKit.LTRBRect(10, 10, wrapTo+10, 230), paint);
|
|
|
|
canvas.drawParagraph(paragraph, 10, 10);
|
|
|
|
|
|
|
|
paint.delete();
|
|
|
|
fontMgr.delete();
|
|
|
|
paragraph.delete();
|
|
|
|
builder.delete();
|
|
|
|
});
|
|
|
|
|
2021-04-20 17:55:49 +00:00
|
|
|
gm('paragraph_text_styles_mixed_leading_distribution', (canvas) => {
|
|
|
|
const fontMgr = CanvasKit.FontMgr.FromData(notoSerifFontBuffer);
|
|
|
|
expect(fontMgr.countFamilies()).toEqual(1);
|
|
|
|
expect(fontMgr.getFamilyName(0)).toEqual('Noto Serif');
|
|
|
|
|
|
|
|
const wrapTo = 200;
|
|
|
|
|
|
|
|
const paraStyle = new CanvasKit.ParagraphStyle({
|
|
|
|
textStyle: {
|
|
|
|
color: CanvasKit.BLACK,
|
|
|
|
backgroundColor: CanvasKit.Color(234, 208, 232), // light pink
|
|
|
|
fontFamilies: ['Noto Serif'],
|
|
|
|
fontSize: 10,
|
|
|
|
heightMultiplier: 10,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const builder = CanvasKit.ParagraphBuilder.Make(paraStyle, fontMgr);
|
|
|
|
builder.addText('Not half leading');
|
|
|
|
|
|
|
|
const halfLeadingText = new CanvasKit.TextStyle({
|
|
|
|
color: CanvasKit.Color(48, 37, 199),
|
|
|
|
backgroundColor: CanvasKit.Color(234, 208, 232), // light pink
|
|
|
|
fontFamilies: ['Noto Serif'],
|
|
|
|
fontSize: 10,
|
|
|
|
heightMultiplier: 10,
|
|
|
|
halfLeading: true,
|
|
|
|
});
|
|
|
|
builder.pushStyle(halfLeadingText);
|
|
|
|
builder.addText('Half Leading Text');
|
|
|
|
const paragraph = builder.build();
|
|
|
|
|
|
|
|
paragraph.layout(wrapTo);
|
|
|
|
canvas.clear(CanvasKit.WHITE);
|
|
|
|
canvas.drawParagraph(paragraph, 0, 0);
|
|
|
|
|
|
|
|
fontMgr.delete();
|
|
|
|
paragraph.delete();
|
|
|
|
builder.delete();
|
|
|
|
});
|
|
|
|
|
2020-04-03 19:11:42 +00:00
|
|
|
it('should not crash if we omit font family on pushed textStyle', () => {
|
|
|
|
const surface = CanvasKit.MakeCanvasSurface('test');
|
2020-05-14 12:27:53 +00:00
|
|
|
expect(surface).toBeTruthy('Could not make surface');
|
2019-10-21 14:50:26 +00:00
|
|
|
|
2020-04-03 19:11:42 +00:00
|
|
|
const canvas = surface.getCanvas();
|
2020-10-07 20:09:22 +00:00
|
|
|
const paint = new CanvasKit.Paint();
|
2019-10-21 14:50:26 +00:00
|
|
|
|
2020-04-03 19:11:42 +00:00
|
|
|
paint.setColor(CanvasKit.RED);
|
|
|
|
paint.setStyle(CanvasKit.PaintStyle.Stroke);
|
2019-10-21 14:50:26 +00:00
|
|
|
|
2020-10-07 20:09:22 +00:00
|
|
|
const fontMgr = CanvasKit.FontMgr.FromData(notoSerifFontBuffer, notoSerifBoldItalicFontBuffer);
|
2020-04-03 19:11:42 +00:00
|
|
|
|
|
|
|
const wrapTo = 250;
|
|
|
|
|
|
|
|
const paraStyle = new CanvasKit.ParagraphStyle({
|
|
|
|
textStyle: {
|
|
|
|
fontFamilies: ['Noto Serif'],
|
|
|
|
fontSize: 20,
|
|
|
|
},
|
|
|
|
textDirection: CanvasKit.TextDirection.RTL,
|
|
|
|
disableHinting: true,
|
|
|
|
});
|
2019-11-20 13:27:10 +00:00
|
|
|
|
2020-04-03 19:11:42 +00:00
|
|
|
const builder = CanvasKit.ParagraphBuilder.Make(paraStyle, fontMgr);
|
|
|
|
builder.addText('Default text\n');
|
2019-11-20 13:27:10 +00:00
|
|
|
|
2020-04-03 19:11:42 +00:00
|
|
|
const boldItalic = new CanvasKit.TextStyle({
|
|
|
|
fontStyle: {
|
|
|
|
weight: CanvasKit.FontWeight.Bold,
|
|
|
|
slant: CanvasKit.FontSlant.Italic,
|
|
|
|
}
|
|
|
|
});
|
2020-06-11 17:48:16 +00:00
|
|
|
builder.pushStyle(boldItalic);
|
2020-04-03 19:11:42 +00:00
|
|
|
builder.addText(`Bold, Italic\n`); // doesn't show up, but we don't crash
|
|
|
|
builder.pop();
|
|
|
|
builder.addText(`back to normal`);
|
|
|
|
const paragraph = builder.build();
|
2019-11-20 13:27:10 +00:00
|
|
|
|
2020-04-03 19:11:42 +00:00
|
|
|
paragraph.layout(wrapTo);
|
2019-11-20 13:27:10 +00:00
|
|
|
|
2020-10-02 22:24:13 +00:00
|
|
|
canvas.clear(CanvasKit.WHITE);
|
2020-04-03 19:11:42 +00:00
|
|
|
canvas.drawRect(CanvasKit.LTRBRect(10, 10, wrapTo+10, wrapTo+10), paint);
|
|
|
|
canvas.drawParagraph(paragraph, 10, 10);
|
2019-11-20 13:27:10 +00:00
|
|
|
|
2020-04-03 19:11:42 +00:00
|
|
|
surface.flush();
|
2019-11-20 13:27:10 +00:00
|
|
|
|
2020-04-03 19:11:42 +00:00
|
|
|
paragraph.delete();
|
|
|
|
builder.delete();
|
|
|
|
paint.delete();
|
|
|
|
fontMgr.delete();
|
2019-11-20 13:27:10 +00:00
|
|
|
});
|
|
|
|
|
2020-04-03 19:11:42 +00:00
|
|
|
it('should not crash if we omit font family on paragraph style', () => {
|
|
|
|
const surface = CanvasKit.MakeCanvasSurface('test');
|
2020-06-11 17:48:16 +00:00
|
|
|
expect(surface).toBeTruthy('Could not make surface');
|
2019-11-20 13:27:10 +00:00
|
|
|
|
2020-04-03 19:11:42 +00:00
|
|
|
const canvas = surface.getCanvas();
|
2020-10-07 20:09:22 +00:00
|
|
|
const paint = new CanvasKit.Paint();
|
2019-11-20 13:27:10 +00:00
|
|
|
|
2020-04-03 19:11:42 +00:00
|
|
|
paint.setColor(CanvasKit.RED);
|
|
|
|
paint.setStyle(CanvasKit.PaintStyle.Stroke);
|
2019-11-20 13:27:10 +00:00
|
|
|
|
2020-10-07 20:09:22 +00:00
|
|
|
const fontMgr = CanvasKit.FontMgr.FromData(notoSerifFontBuffer, notoSerifBoldItalicFontBuffer);
|
2019-11-20 13:27:10 +00:00
|
|
|
|
2020-04-03 19:11:42 +00:00
|
|
|
const wrapTo = 250;
|
2019-11-20 13:27:10 +00:00
|
|
|
|
2020-04-03 19:11:42 +00:00
|
|
|
const paraStyle = new CanvasKit.ParagraphStyle({
|
|
|
|
textStyle: {
|
|
|
|
fontSize: 20,
|
|
|
|
},
|
|
|
|
textDirection: CanvasKit.TextDirection.RTL,
|
|
|
|
disableHinting: true,
|
|
|
|
});
|
2019-11-20 13:27:10 +00:00
|
|
|
|
2020-04-03 19:11:42 +00:00
|
|
|
const builder = CanvasKit.ParagraphBuilder.Make(paraStyle, fontMgr);
|
|
|
|
builder.addText('Default text\n');
|
|
|
|
|
|
|
|
const boldItalic = new CanvasKit.TextStyle({
|
|
|
|
fontStyle: {
|
|
|
|
weight: CanvasKit.FontWeight.Bold,
|
|
|
|
slant: CanvasKit.FontSlant.Italic,
|
|
|
|
}
|
|
|
|
});
|
2020-06-11 17:48:16 +00:00
|
|
|
builder.pushStyle(boldItalic);
|
2020-04-03 19:11:42 +00:00
|
|
|
builder.addText(`Bold, Italic\n`);
|
|
|
|
builder.pop();
|
|
|
|
builder.addText(`back to normal`);
|
|
|
|
const paragraph = builder.build();
|
|
|
|
|
|
|
|
paragraph.layout(wrapTo);
|
|
|
|
|
2020-10-02 22:24:13 +00:00
|
|
|
canvas.clear(CanvasKit.WHITE);
|
2020-04-03 19:11:42 +00:00
|
|
|
canvas.drawRect(CanvasKit.LTRBRect(10, 10, wrapTo+10, wrapTo+10), paint);
|
|
|
|
canvas.drawParagraph(paragraph, 10, 10);
|
|
|
|
|
|
|
|
surface.flush();
|
|
|
|
|
|
|
|
paragraph.delete();
|
|
|
|
paint.delete();
|
|
|
|
fontMgr.delete();
|
|
|
|
builder.delete();
|
2019-11-20 13:27:10 +00:00
|
|
|
});
|
2019-10-03 15:22:08 +00:00
|
|
|
});
|