drawnui-react 0.1.0-preview.34 → 0.1.0-preview.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -113,7 +113,11 @@ export declare class SkiaLabel extends SkiaControl {
113
113
  private lines;
114
114
  private mainFonts?;
115
115
  private readonly fontsCache;
116
- private readonly runCache;
116
+ /** Runs of a text per style (Segment), and the same runs carrying a span; cleared when the glyph spacing changes. */
117
+ private runCache;
118
+ private spanRunCache;
119
+ /** Wrap tokens of the text, kept while the label has not changed (NeedMeasure) at the same scale. */
120
+ private tokens?;
117
121
  /** CharacterSpacing in pixels at the measured scale: added after every glyph of a run (its Width includes them). */
118
122
  private glyphSpacing;
119
123
  private ResolveFonts;
@@ -127,7 +127,11 @@ export class SkiaLabel extends SkiaControl {
127
127
  lines = [];
128
128
  mainFonts;
129
129
  fontsCache = new Map();
130
+ /** Runs of a text per style (Segment), and the same runs carrying a span; cleared when the glyph spacing changes. */
130
131
  runCache = new Map();
132
+ spanRunCache = new WeakMap();
133
+ /** Wrap tokens of the text, kept while the label has not changed (NeedMeasure) at the same scale. */
134
+ tokens;
131
135
  /** CharacterSpacing in pixels at the measured scale: added after every glyph of a run (its Width includes them). */
132
136
  glyphSpacing = 0;
133
137
  ResolveFonts(family, weight, italic, sizePx) {
@@ -192,15 +196,20 @@ export class SkiaLabel extends SkiaControl {
192
196
  Segment(text, fonts, span) {
193
197
  if (text.length === 0)
194
198
  return [];
195
- const cacheKey = fonts.Key + " " + text;
196
- let cached = this.runCache.get(cacheKey);
199
+ let byText = this.runCache.get(fonts);
200
+ if (!byText) {
201
+ byText = new Map();
202
+ this.runCache.set(fonts, byText);
203
+ }
204
+ let cached = byText.get(text);
205
+ const cacheKey = text;
197
206
  if (!cached) {
198
207
  const main = fonts.Main;
199
208
  const fbs = fonts.Fallbacks;
200
209
  cached = [];
201
210
  text = SkiaLabel.DropIgnorables(text, main, fbs);
202
211
  if (text.length === 0) {
203
- this.runCache.set(cacheKey, cached);
212
+ byText.set(cacheKey, cached);
204
213
  return cached;
205
214
  }
206
215
  if (fbs.length === 0) {
@@ -231,9 +240,27 @@ export class SkiaLabel extends SkiaControl {
231
240
  const t = cps.slice(start).join("");
232
241
  cached.push({ Text: t, Font: current, Width: this.Advance(current, t), Fonts: fonts });
233
242
  }
234
- this.runCache.set(cacheKey, cached);
243
+ byText.set(cacheKey, cached);
244
+ }
245
+ if (!span)
246
+ return cached;
247
+ // the same runs carrying the span, made once per span, style and text
248
+ let bySpan = this.spanRunCache.get(span);
249
+ if (!bySpan) {
250
+ bySpan = new Map();
251
+ this.spanRunCache.set(span, bySpan);
252
+ }
253
+ let spanByText = bySpan.get(fonts);
254
+ if (!spanByText) {
255
+ spanByText = new Map();
256
+ bySpan.set(fonts, spanByText);
257
+ }
258
+ let withSpan = spanByText.get(cacheKey);
259
+ if (!withSpan) {
260
+ withSpan = cached.map((r) => ({ ...r, Span: span }));
261
+ spanByText.set(cacheKey, withSpan);
235
262
  }
236
- return span ? cached.map((r) => ({ ...r, Span: span })) : cached;
263
+ return withSpan;
237
264
  }
238
265
  Width(runs) { let w = 0; for (const r of runs)
239
266
  w += r.Width; return w; }
@@ -399,7 +426,10 @@ export class SkiaLabel extends SkiaControl {
399
426
  const sp = this.glyphSpacing;
400
427
  const out = [];
401
428
  const empty = (fonts) => { const l = this.NewLine(); l.Ascent = fonts.Ascent; l.Descent = fonts.Descent; return l; };
402
- for (const para of this.Tokenize(scale)) {
429
+ // the text's wrap tokens change only with the label (NeedMeasure) or the scale
430
+ if (this.NeedMeasure || !this.tokens || this.tokens.Scale !== scale)
431
+ this.tokens = { Scale: scale, Paragraphs: this.Tokenize(scale) };
432
+ for (const para of this.tokens.Paragraphs) {
403
433
  let line = this.NewLine();
404
434
  if (out.length > 0 && !para.LineBreak)
405
435
  line.Para = true;
@@ -653,7 +683,10 @@ export class SkiaLabel extends SkiaControl {
653
683
  const b = r.Font.getGlyphBounds([g[0], g[n - 1]]);
654
684
  return [x0 + r.Shaped.Positions[0] + b[0], x0 + r.Shaped.Positions[(n - 1) * 2] + b[6]];
655
685
  }
656
- const ids = r.Font.getGlyphIDs(r.Text);
686
+ // only the first and the last character matter
687
+ const first = String.fromCodePoint(r.Text.codePointAt(0));
688
+ const lastAt = r.Text.length - (r.Text.length > 1 && r.Text.charCodeAt(r.Text.length - 1) >= 0xDC00 && r.Text.charCodeAt(r.Text.length - 1) <= 0xDFFF ? 2 : 1);
689
+ const ids = r.Font.getGlyphIDs(first + r.Text.slice(lastAt));
657
690
  const n = ids.length;
658
691
  const b = r.Font.getGlyphBounds([ids[0], ids[n - 1]]);
659
692
  const lastAdvance = r.Font.getGlyphWidths([ids[n - 1]])[0];
@@ -683,7 +716,8 @@ export class SkiaLabel extends SkiaControl {
683
716
  const glyphSpacing = scale * (this.characterSpacing - 1);
684
717
  if (glyphSpacing !== this.glyphSpacing) {
685
718
  this.glyphSpacing = glyphSpacing;
686
- this.runCache.clear();
719
+ this.runCache = new Map();
720
+ this.spanRunCache = new WeakMap();
687
721
  }
688
722
  const px = this.padding.HorizontalThickness * scale, py = this.padding.VerticalThickness * scale;
689
723
  const extraH = this.EffectsExtra(scale).H;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drawnui-react",
3
- "version": "0.1.0-preview.34",
3
+ "version": "0.1.0-preview.35",
4
4
  "description": "DrawnUi for React: the DrawnUi (.NET) drawn-UI engine in TypeScript on CanvasKit (Skia WASM). Same controls, same PascalCase API as DrawnUi.Net.",
5
5
  "license": "MIT",
6
6
  "author": "Nick Kovalsky (AppoMobi)",