@nasser-sw/fabric 7.0.1-beta12 → 7.0.1-beta13

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.
@@ -1,1320 +1,1389 @@
1
- import type { TClassProperties, TOptions } from '../typedefs';
2
- import { IText } from './IText/IText';
3
- import { classRegistry } from '../ClassRegistry';
4
- import { createTextboxDefaultControls } from '../controls/commonControls';
5
- import { JUSTIFY, JUSTIFY_CENTER } from './Text/constants';
6
- import type { TextStyleDeclaration } from './Text/StyledText';
7
- import type { SerializedITextProps, ITextProps } from './IText/IText';
8
- import type { ITextEvents } from './IText/ITextBehavior';
9
- import type { TextLinesInfo } from './Text/Text';
10
- import type { Control } from '../controls/Control';
11
- import { fontLacksEnglishGlyphsCached } from '../text/measure';
12
- import { layoutText } from '../text/layout';
13
-
14
- // @TODO: Many things here are configuration related and shouldn't be on the class nor prototype
15
- // regexes, list of properties that are not suppose to change by instances, magic consts.
16
- // this will be a separated effort
17
- export const textboxDefaultValues: Partial<TClassProperties<Textbox>> = {
18
- minWidth: 20,
19
- dynamicMinWidth: 2,
20
- lockScalingFlip: true,
21
- noScaleCache: false,
22
- _wordJoiners: /[ \t\r]/,
23
- splitByGrapheme: false,
24
- };
25
-
26
- export type GraphemeData = {
27
- wordsData: {
28
- word: string[];
29
- width: number;
30
- }[][];
31
- largestWordWidth: number;
32
- };
33
-
34
- export type StyleMap = Record<string, { line: number; offset: number }>;
35
-
36
- // @TODO this is not complete
37
- interface UniqueTextboxProps {
38
- minWidth: number;
39
- splitByGrapheme: boolean;
40
- dynamicMinWidth: number;
41
- _wordJoiners: RegExp;
42
- }
43
-
44
- export interface SerializedTextboxProps
45
- extends SerializedITextProps,
46
- Pick<UniqueTextboxProps, 'minWidth' | 'splitByGrapheme'> {}
47
-
48
- export interface TextboxProps extends ITextProps, UniqueTextboxProps {}
49
-
50
- /**
51
- * Textbox class, based on IText, allows the user to resize the text rectangle
52
- * and wraps lines automatically. Textboxes have their Y scaling locked, the
53
- * user can only change width. Height is adjusted automatically based on the
54
- * wrapping of lines.
55
- */
56
- export class Textbox<
57
- Props extends TOptions<TextboxProps> = Partial<TextboxProps>,
58
- SProps extends SerializedTextboxProps = SerializedTextboxProps,
59
- EventSpec extends ITextEvents = ITextEvents,
60
- >
61
- extends IText<Props, SProps, EventSpec>
62
- implements UniqueTextboxProps
63
- {
64
- /**
65
- * Minimum width of textbox, in pixels.
66
- * @type Number
67
- */
68
- declare minWidth: number;
69
-
70
- /**
71
- * Minimum calculated width of a textbox, in pixels.
72
- * fixed to 2 so that an empty textbox cannot go to 0
73
- * and is still selectable without text.
74
- * @type Number
75
- */
76
- declare dynamicMinWidth: number;
77
-
78
- /**
79
- * Use this boolean property in order to split strings that have no white space concept.
80
- * this is a cheap way to help with chinese/japanese
81
- * @type Boolean
82
- * @since 2.6.0
83
- */
84
- declare splitByGrapheme: boolean;
85
-
86
- declare _wordJoiners: RegExp;
87
-
88
- declare _styleMap: StyleMap;
89
-
90
- declare isWrapping: boolean;
91
-
92
- static type = 'Textbox';
93
-
94
- static textLayoutProperties = [...IText.textLayoutProperties, 'width'];
95
-
96
- static ownDefaults = textboxDefaultValues;
97
-
98
- static getDefaults(): Record<string, any> {
99
- return {
100
- ...super.getDefaults(),
101
- ...Textbox.ownDefaults,
102
- };
103
- }
104
-
105
- /**
106
- * Constructor
107
- * @param {String} text Text string
108
- * @param {Object} [options] Options object
109
- */
110
- constructor(text: string, options?: Props) {
111
- super(text, { ...Textbox.ownDefaults, ...options } as Props);
112
- this.initializeEventListeners();
113
- }
114
-
115
- /**
116
- * Creates the default control object.
117
- * If you prefer to have on instance of controls shared among all objects
118
- * make this function return an empty object and add controls to the ownDefaults object
119
- */
120
- static createControls(): { controls: Record<string, Control> } {
121
- return { controls: createTextboxDefaultControls() };
122
- }
123
-
124
- /**
125
- * Unlike superclass's version of this function, Textbox does not update
126
- * its width.
127
- * @private
128
- * @override
129
- */
130
- initDimensions() {
131
- if (!this.initialized) {
132
- this.initialized = true;
133
- }
134
-
135
- // Prevent rapid recalculations during moves
136
- if ((this as any)._usingBrowserWrapping) {
137
- const now = Date.now();
138
- const lastCall = (this as any)._lastInitDimensionsTime || 0;
139
- const isRapidCall = now - lastCall < 100;
140
- const isDuringLoading = (this as any)._jsonLoading || !(this as any)._browserWrapInitialized;
141
-
142
- if (isRapidCall && !isDuringLoading) {
143
- return;
144
- }
145
- (this as any)._lastInitDimensionsTime = now;
146
- }
147
-
148
- // Skip if nothing changed
149
- const currentState = `${this.text}|${this.width}|${this.fontSize}|${this.fontFamily}|${this.textAlign}`;
150
- if ((this as any)._lastDimensionState === currentState && this._textLines && this._textLines.length > 0) {
151
- return;
152
- }
153
- (this as any)._lastDimensionState = currentState;
154
-
155
- // Use advanced layout if enabled
156
- if (this.enableAdvancedLayout) {
157
- return this.initDimensionsAdvanced();
158
- }
159
-
160
- this.isEditing && this.initDelayedCursor();
161
- this._clearCache();
162
- // clear dynamicMinWidth as it will be different after we re-wrap line
163
- this.dynamicMinWidth = 0;
164
- // wrap lines
165
- const splitTextResult = this._splitText();
166
- this._styleMap = this._generateStyleMap(splitTextResult);
167
-
168
- // For browser wrapping, ensure _textLines is set from browser results
169
- if ((this as any)._usingBrowserWrapping && splitTextResult && splitTextResult.lines) {
170
- this._textLines = splitTextResult.lines.map(line => line.split(''));
171
-
172
- // Store justify measurements and browser height
173
- const justifyMeasurements = (splitTextResult as any).justifySpaceMeasurements;
174
- if (justifyMeasurements) {
175
- (this._styleMap as any).justifySpaceMeasurements = justifyMeasurements;
176
- }
177
-
178
- const actualHeight = (splitTextResult as any).actualBrowserHeight;
179
- if (actualHeight) {
180
- (this as any)._actualBrowserHeight = actualHeight;
181
- }
182
- }
183
- // Don't auto-resize width when using browser wrapping to prevent width increases during moves
184
- if (!((this as any)._usingBrowserWrapping) && this.dynamicMinWidth > this.width) {
185
- this._set('width', this.dynamicMinWidth);
186
- }
187
-
188
- // For browser wrapping fonts (like STV), ensure minimum width for new textboxes
189
- // since these fonts can't measure English characters properly
190
- if ((this as any)._usingBrowserWrapping && this.width < 50) {
191
- console.log(`🔤 BROWSER WRAP: Font ${this.fontFamily} has width ${this.width}px, setting to 300px for usability`);
192
- this.width = 300;
193
- }
194
-
195
- // Mark browser wrapping as initialized when complete
196
- if ((this as any)._usingBrowserWrapping) {
197
- (this as any)._browserWrapInitialized = true;
198
- }
199
-
200
- if (this.textAlign.includes(JUSTIFY)) {
201
- // For browser wrapping fonts, apply browser-calculated justify spaces
202
- if ((this as any)._usingBrowserWrapping) {
203
- console.log('🔤 BROWSER WRAP: Applying browser-calculated justify spaces');
204
- this._applyBrowserJustifySpaces();
205
- return;
206
- }
207
-
208
- // Don't apply justify alignment during drag operations to prevent snapping
209
- const now = Date.now();
210
- const lastDragTime = (this as any)._lastInitDimensionsTime || 0;
211
- const isDuringDrag = now - lastDragTime < 200; // 200ms window for drag detection
212
-
213
- if (isDuringDrag) {
214
- console.log('🔤 Skipping justify during drag operation to prevent snapping');
215
- return;
216
- }
217
-
218
- // For non-browser-wrapping fonts, use Fabric's justify system
219
- // once text is measured we need to make space fatter to make justified text.
220
- // Ensure __charBounds exists and fonts are ready before applying justify
221
- if (this.__charBounds && this.__charBounds.length > 0) {
222
- // Check if font is ready for accurate justify calculations
223
- const fontReady = this._isFontReady ? this._isFontReady() : true;
224
- if (fontReady) {
225
- this.enlargeSpaces();
226
- } else {
227
- console.warn('⚠️ Textbox: Font not ready for justify, deferring enlargeSpaces');
228
- // Defer justify calculation until font is ready
229
- this._scheduleJustifyAfterFontLoad();
230
- }
231
- } else {
232
- console.warn('⚠️ Textbox: __charBounds not ready for justify alignment, deferring enlargeSpaces');
233
- // Defer the justify calculation until the next frame
234
- setTimeout(() => {
235
- if (this.__charBounds && this.__charBounds.length > 0 && this.enlargeSpaces) {
236
- console.log('🔧 Applying deferred Textbox justify alignment');
237
- this.enlargeSpaces();
238
- this.canvas?.requestRenderAll();
239
- }
240
- }, 0);
241
- }
242
- }
243
- // Calculate height - use Fabric's calculation for proper text rendering space
244
- if ((this as any)._usingBrowserWrapping && this._textLines && this._textLines.length > 0) {
245
- const actualBrowserHeight = (this as any)._actualBrowserHeight;
246
- const oldHeight = this.height;
247
- // Use Fabric's height calculation since it knows how much space text rendering needs
248
- this.height = this.calcTextHeight();
249
-
250
- // Force canvas refresh and control update if height changed significantly
251
- if (Math.abs(this.height - oldHeight) > 1) {
252
- this.setCoords();
253
- this.canvas?.requestRenderAll();
254
-
255
- // DEBUG: Log exact positioning details
256
- console.log(`🎯 POSITIONING DEBUG:`);
257
- console.log(` Textbox height: ${this.height}px`);
258
- console.log(` Textbox top: ${this.top}px`);
259
- console.log(` Textbox left: ${this.left}px`);
260
- console.log(` Text lines: ${this._textLines?.length || 0}`);
261
- console.log(` Font size: ${this.fontSize}px`);
262
- console.log(` Line height: ${this.lineHeight || 1.16}`);
263
- console.log(` Calculated line height: ${this.fontSize * (this.lineHeight || 1.16)}px`);
264
- console.log(` _getTopOffset(): ${this._getTopOffset()}px`);
265
- console.log(` calcTextHeight(): ${this.calcTextHeight()}px`);
266
- console.log(` Browser height: ${actualBrowserHeight}px`);
267
- console.log(` Height difference: ${this.height - this.calcTextHeight()}px`);
268
- }
269
- } else {
270
- this.height = this.calcTextHeight();
271
- }
272
- }
273
-
274
- /**
275
- * Schedule justify calculation after font loads (Textbox-specific)
276
- * @private
277
- */
278
- _scheduleJustifyAfterFontLoad(): void {
279
- if (typeof document === 'undefined' || !('fonts' in document)) {
280
- return;
281
- }
282
-
283
- // Only schedule if not already waiting
284
- if ((this as any)._fontJustifyScheduled) {
285
- return;
286
- }
287
- (this as any)._fontJustifyScheduled = true;
288
-
289
- const fontSpec = `${this.fontSize}px ${this.fontFamily}`;
290
- document.fonts.load(fontSpec).then(() => {
291
- (this as any)._fontJustifyScheduled = false;
292
- console.log('🔧 Textbox: Font loaded, applying justify alignment');
293
-
294
- // Re-run initDimensions to ensure proper justify calculation
295
- this.initDimensions();
296
- this.canvas?.requestRenderAll();
297
- }).catch(() => {
298
- (this as any)._fontJustifyScheduled = false;
299
- console.warn('⚠️ Textbox: Font loading failed, justify may be incorrect');
300
- });
301
- }
302
-
303
- /**
304
- * Advanced dimensions calculation using new layout engine
305
- * @private
306
- */
307
- initDimensionsAdvanced() {
308
- if (!this.initialized) {
309
- return;
310
- }
311
-
312
- this.isEditing && this.initDelayedCursor();
313
- this._clearCache();
314
- this.dynamicMinWidth = 0;
315
-
316
- // Use new layout engine
317
- const layout = layoutText({
318
- text: this.text,
319
- width: this.width,
320
- height: this.height,
321
- wrap: this.wrap || 'word',
322
- align: (this as any)._mapTextAlignToAlign(this.textAlign),
323
- ellipsis: this.ellipsis || false,
324
- fontSize: this.fontSize,
325
- lineHeight: this.lineHeight,
326
- letterSpacing: this.letterSpacing || 0,
327
- charSpacing: this.charSpacing,
328
- direction: this.direction === 'inherit' ? 'ltr' : this.direction,
329
- fontFamily: this.fontFamily,
330
- fontStyle: this.fontStyle,
331
- fontWeight: this.fontWeight,
332
- verticalAlign: this.verticalAlign || 'top',
333
- });
334
-
335
- // Update dynamic minimum width based on layout
336
- if (layout.lines.length > 0) {
337
- const maxLineWidth = Math.max(...layout.lines.map(line => line.width));
338
- this.dynamicMinWidth = Math.max(this.minWidth, maxLineWidth);
339
- }
340
-
341
- // Adjust width if needed (preserving Textbox behavior)
342
- if (this.dynamicMinWidth > this.width) {
343
- this._set('width', this.dynamicMinWidth);
344
- // Re-layout with new width
345
- const newLayout = layoutText({
346
- ...(this as any)._getAdvancedLayoutOptions(),
347
- width: this.width,
348
- });
349
- this.height = newLayout.totalHeight;
350
- (this as any)._convertLayoutToLegacyFormat(newLayout);
351
- } else {
352
- this.height = layout.totalHeight;
353
- (this as any)._convertLayoutToLegacyFormat(layout);
354
- }
355
-
356
- // Generate style map for compatibility
357
- this._styleMap = this._generateStyleMapFromLayout(layout);
358
- this.dirty = true;
359
- }
360
-
361
- /**
362
- * Generate style map from new layout format
363
- * @private
364
- */
365
- _generateStyleMapFromLayout(layout: any): StyleMap {
366
- const map: StyleMap = {};
367
- let realLineCount = 0;
368
- let charCount = 0;
369
-
370
- layout.lines.forEach((line: any, i: number) => {
371
- if (line.text.includes('\n') && i > 0) {
372
- realLineCount++;
373
- }
374
-
375
- map[i] = { line: realLineCount, offset: 0 };
376
- charCount += line.graphemes.length;
377
-
378
- if (i < layout.lines.length - 1) {
379
- charCount += 1; // newline character
380
- }
381
- });
382
-
383
- return map;
384
- }
385
-
386
- /**
387
- * Generate an object that translates the style object so that it is
388
- * broken up by visual lines (new lines and automatic wrapping).
389
- * The original text styles object is broken up by actual lines (new lines only),
390
- * which is only sufficient for Text / IText
391
- * @private
392
- */
393
- _generateStyleMap(textInfo: TextLinesInfo): StyleMap {
394
- let realLineCount = 0,
395
- realLineCharCount = 0,
396
- charCount = 0;
397
- const map: StyleMap = {};
398
-
399
- for (let i = 0; i < textInfo.graphemeLines.length; i++) {
400
- if (textInfo.graphemeText[charCount] === '\n' && i > 0) {
401
- realLineCharCount = 0;
402
- charCount++;
403
- realLineCount++;
404
- } else if (
405
- !this.splitByGrapheme &&
406
- this._reSpaceAndTab.test(textInfo.graphemeText[charCount]) &&
407
- i > 0
408
- ) {
409
- // this case deals with space's that are removed from end of lines when wrapping
410
- realLineCharCount++;
411
- charCount++;
412
- }
413
-
414
- map[i] = { line: realLineCount, offset: realLineCharCount };
415
-
416
- charCount += textInfo.graphemeLines[i].length;
417
- realLineCharCount += textInfo.graphemeLines[i].length;
418
- }
419
-
420
- return map;
421
- }
422
-
423
- /**
424
- * Returns true if object has a style property or has it on a specified line
425
- * @param {Number} lineIndex
426
- * @return {Boolean}
427
- */
428
- styleHas(property: keyof TextStyleDeclaration, lineIndex: number): boolean {
429
- if (this._styleMap && !this.isWrapping) {
430
- const map = this._styleMap[lineIndex];
431
- if (map) {
432
- lineIndex = map.line;
433
- }
434
- }
435
- return super.styleHas(property, lineIndex);
436
- }
437
-
438
- /**
439
- * Returns true if object has no styling or no styling in a line
440
- * @param {Number} lineIndex , lineIndex is on wrapped lines.
441
- * @return {Boolean}
442
- */
443
- isEmptyStyles(lineIndex: number): boolean {
444
- if (!this.styles) {
445
- return true;
446
- }
447
- let offset = 0,
448
- nextLineIndex = lineIndex + 1,
449
- nextOffset: number,
450
- shouldLimit = false;
451
- const map = this._styleMap[lineIndex],
452
- mapNextLine = this._styleMap[lineIndex + 1];
453
- if (map) {
454
- lineIndex = map.line;
455
- offset = map.offset;
456
- }
457
- if (mapNextLine) {
458
- nextLineIndex = mapNextLine.line;
459
- shouldLimit = nextLineIndex === lineIndex;
460
- nextOffset = mapNextLine.offset;
461
- }
462
- const obj =
463
- typeof lineIndex === 'undefined'
464
- ? this.styles
465
- : { line: this.styles[lineIndex] };
466
- for (const p1 in obj) {
467
- for (const p2 in obj[p1]) {
468
- const p2Number = parseInt(p2, 10);
469
- if (p2Number >= offset && (!shouldLimit || p2Number < nextOffset!)) {
470
- for (const p3 in obj[p1][p2]) {
471
- return false;
472
- }
473
- }
474
- }
475
- }
476
- return true;
477
- }
478
-
479
- /**
480
- * @protected
481
- * @param {Number} lineIndex
482
- * @param {Number} charIndex
483
- * @return {TextStyleDeclaration} a style object reference to the existing one or a new empty object when undefined
484
- */
485
- _getStyleDeclaration(
486
- lineIndex: number,
487
- charIndex: number,
488
- ): TextStyleDeclaration {
489
- if (this._styleMap && !this.isWrapping) {
490
- const map = this._styleMap[lineIndex];
491
- if (!map) {
492
- return {};
493
- }
494
- lineIndex = map.line;
495
- charIndex = map.offset + charIndex;
496
- }
497
- return super._getStyleDeclaration(lineIndex, charIndex);
498
- }
499
-
500
- /**
501
- * @param {Number} lineIndex
502
- * @param {Number} charIndex
503
- * @param {Object} style
504
- * @private
505
- */
506
- protected _setStyleDeclaration(
507
- lineIndex: number,
508
- charIndex: number,
509
- style: object,
510
- ) {
511
- const map = this._styleMap[lineIndex];
512
- super._setStyleDeclaration(map.line, map.offset + charIndex, style);
513
- }
514
-
515
- /**
516
- * @param {Number} lineIndex
517
- * @param {Number} charIndex
518
- * @private
519
- */
520
- protected _deleteStyleDeclaration(lineIndex: number, charIndex: number) {
521
- const map = this._styleMap[lineIndex];
522
- super._deleteStyleDeclaration(map.line, map.offset + charIndex);
523
- }
524
-
525
- /**
526
- * probably broken need a fix
527
- * Returns the real style line that correspond to the wrapped lineIndex line
528
- * Used just to verify if the line does exist or not.
529
- * @param {Number} lineIndex
530
- * @returns {Boolean} if the line exists or not
531
- * @private
532
- */
533
- protected _getLineStyle(lineIndex: number): boolean {
534
- const map = this._styleMap[lineIndex];
535
- return !!this.styles[map.line];
536
- }
537
-
538
- /**
539
- * Set the line style to an empty object so that is initialized
540
- * @param {Number} lineIndex
541
- * @param {Object} style
542
- * @private
543
- */
544
- protected _setLineStyle(lineIndex: number) {
545
- const map = this._styleMap[lineIndex];
546
- super._setLineStyle(map.line);
547
- }
548
-
549
- /**
550
- * Wraps text using the 'width' property of Textbox. First this function
551
- * splits text on newlines, so we preserve newlines entered by the user.
552
- * Then it wraps each line using the width of the Textbox by calling
553
- * _wrapLine().
554
- * @param {Array} lines The string array of text that is split into lines
555
- * @param {Number} desiredWidth width you want to wrap to
556
- * @returns {Array} Array of lines
557
- */
558
- _wrapText(lines: string[], desiredWidth: number): string[][] {
559
- this.isWrapping = true;
560
- // extract all thewords and the widths to optimally wrap lines.
561
- const data = this.getGraphemeDataForRender(lines);
562
- const wrapped: string[][] = [];
563
- for (let i = 0; i < data.wordsData.length; i++) {
564
- wrapped.push(...this._wrapLine(i, desiredWidth, data));
565
- }
566
- this.isWrapping = false;
567
- return wrapped;
568
- }
569
-
570
- /**
571
- * For each line of text terminated by an hard line stop,
572
- * measure each word width and extract the largest word from all.
573
- * The returned words here are the one that at the end will be rendered.
574
- * @param {string[]} lines the lines we need to measure
575
- *
576
- */
577
- getGraphemeDataForRender(lines: string[]): GraphemeData {
578
- const splitByGrapheme = this.splitByGrapheme,
579
- infix = splitByGrapheme ? '' : ' ';
580
-
581
- let largestWordWidth = 0;
582
-
583
- const data = lines.map((line, lineIndex) => {
584
- let offset = 0;
585
- const wordsOrGraphemes = splitByGrapheme
586
- ? this.graphemeSplit(line)
587
- : this.wordSplit(line);
588
-
589
- if (wordsOrGraphemes.length === 0) {
590
- return [{ word: [], width: 0 }];
591
- }
592
-
593
- return wordsOrGraphemes.map((word: string) => {
594
- // if using splitByGrapheme words are already in graphemes.
595
- const graphemeArray = splitByGrapheme
596
- ? [word]
597
- : this.graphemeSplit(word);
598
- const width = this._measureWord(graphemeArray, lineIndex, offset);
599
- largestWordWidth = Math.max(width, largestWordWidth);
600
- offset += graphemeArray.length + infix.length;
601
- return { word: graphemeArray, width };
602
- });
603
- });
604
-
605
- return {
606
- wordsData: data,
607
- largestWordWidth,
608
- };
609
- }
610
-
611
- /**
612
- * Helper function to measure a string of text, given its lineIndex and charIndex offset
613
- * It gets called when charBounds are not available yet.
614
- * Override if necessary
615
- * Use with {@link Textbox#wordSplit}
616
- *
617
- * @param {CanvasRenderingContext2D} ctx
618
- * @param {String} text
619
- * @param {number} lineIndex
620
- * @param {number} charOffset
621
- * @returns {number}
622
- */
623
- _measureWord(word: string[], lineIndex: number, charOffset = 0): number {
624
- let width = 0,
625
- prevGrapheme;
626
- const skipLeft = true;
627
- for (let i = 0, len = word.length; i < len; i++) {
628
- const box = this._getGraphemeBox(
629
- word[i],
630
- lineIndex,
631
- i + charOffset,
632
- prevGrapheme,
633
- skipLeft,
634
- );
635
- width += box.kernedWidth;
636
- prevGrapheme = word[i];
637
- }
638
- return width;
639
- }
640
-
641
- /**
642
- * Override this method to customize word splitting
643
- * Use with {@link Textbox#_measureWord}
644
- * @param {string} value
645
- * @returns {string[]} array of words
646
- */
647
- wordSplit(value: string): string[] {
648
- return value.split(this._wordJoiners);
649
- }
650
-
651
- /**
652
- * Wraps a line of text using the width of the Textbox as desiredWidth
653
- * and leveraging the known width o words from GraphemeData
654
- * @private
655
- * @param {Number} lineIndex
656
- * @param {Number} desiredWidth width you want to wrap the line to
657
- * @param {GraphemeData} graphemeData an object containing all the lines' words width.
658
- * @param {Number} reservedSpace space to remove from wrapping for custom functionalities
659
- * @returns {Array} Array of line(s) into which the given text is wrapped
660
- * to.
661
- */
662
- _wrapLine(
663
- lineIndex: number,
664
- desiredWidth: number,
665
- { largestWordWidth, wordsData }: GraphemeData,
666
- reservedSpace = 0,
667
- ): string[][] {
668
- const additionalSpace = this._getWidthOfCharSpacing(),
669
- splitByGrapheme = this.splitByGrapheme,
670
- graphemeLines = [],
671
- infix = splitByGrapheme ? '' : ' ';
672
-
673
- let lineWidth = 0,
674
- line: string[] = [],
675
- // spaces in different languages?
676
- offset = 0,
677
- infixWidth = 0,
678
- lineJustStarted = true;
679
-
680
- desiredWidth -= reservedSpace;
681
-
682
- const maxWidth = Math.max(
683
- desiredWidth,
684
- largestWordWidth,
685
- this.dynamicMinWidth,
686
- );
687
- // layout words
688
- const data = wordsData[lineIndex];
689
- offset = 0;
690
- let i;
691
- for (i = 0; i < data.length; i++) {
692
- const { word, width: wordWidth } = data[i];
693
- offset += word.length;
694
-
695
- // Predictive wrapping: check if adding this word would exceed the width
696
- const potentialLineWidth = lineWidth + infixWidth + wordWidth - additionalSpace;
697
- // Use exact width to match overlay editor behavior
698
- const conservativeMaxWidth = maxWidth; // No artificial buffer
699
-
700
- // Debug logging for wrapping decisions
701
- const currentLineText = line.join('');
702
- console.log(`🔧 FABRIC WRAP CHECK: "${data[i].word}" -> potential: ${potentialLineWidth.toFixed(1)}px vs limit: ${conservativeMaxWidth.toFixed(1)}px`);
703
-
704
- if (potentialLineWidth > conservativeMaxWidth && !lineJustStarted) {
705
- // This word would exceed the width, wrap before adding it
706
- console.log(`🔧 FABRIC WRAP! Line: "${currentLineText}" (${lineWidth.toFixed(1)}px)`);
707
- graphemeLines.push(line);
708
- line = [];
709
- lineWidth = wordWidth; // Start new line with just this word
710
- lineJustStarted = true;
711
- } else {
712
- // Word fits, add it to current line
713
- lineWidth = potentialLineWidth + additionalSpace;
714
- }
715
-
716
- if (!lineJustStarted && !splitByGrapheme) {
717
- line.push(infix);
718
- }
719
- line = line.concat(word);
720
-
721
- // Debug: show current line after adding word
722
- console.log(`🔧 FABRIC AFTER ADD: Line now: "${line.join('')}" (${line.length} chars)`);
723
-
724
-
725
- infixWidth = splitByGrapheme
726
- ? 0
727
- : this._measureWord([infix], lineIndex, offset);
728
- offset++;
729
- lineJustStarted = false;
730
- }
731
-
732
- i && graphemeLines.push(line);
733
-
734
- // TODO: this code is probably not necessary anymore.
735
- // it can be moved out of this function since largestWordWidth is now
736
- // known in advance
737
- // Don't modify dynamicMinWidth when using browser wrapping to prevent width increases
738
- if (!((this as any)._usingBrowserWrapping) && largestWordWidth + reservedSpace > this.dynamicMinWidth) {
739
- console.log(`🔧 FABRIC updating dynamicMinWidth: ${this.dynamicMinWidth} -> ${largestWordWidth - additionalSpace + reservedSpace}`);
740
- this.dynamicMinWidth = largestWordWidth - additionalSpace + reservedSpace;
741
- } else if ((this as any)._usingBrowserWrapping) {
742
- console.log(`🔤 BROWSER WRAP: Skipping dynamicMinWidth update to prevent width increase`);
743
- }
744
-
745
- // Debug: show final wrapped lines
746
- console.log(`🔧 FABRIC FINAL LINES: ${graphemeLines.length} lines`);
747
- graphemeLines.forEach((line, i) => {
748
- console.log(` Line ${i + 1}: "${line.join('')}" (${line.length} chars)`);
749
- });
750
-
751
- return graphemeLines;
752
- }
753
-
754
- /**
755
- * Detect if the text line is ended with an hard break
756
- * text and itext do not have wrapping, return false
757
- * @param {Number} lineIndex text to split
758
- * @return {Boolean}
759
- */
760
- isEndOfWrapping(lineIndex: number): boolean {
761
- if (!this._styleMap[lineIndex + 1]) {
762
- // is last line, return true;
763
- return true;
764
- }
765
- if (this._styleMap[lineIndex + 1].line !== this._styleMap[lineIndex].line) {
766
- // this is last line before a line break, return true;
767
- return true;
768
- }
769
- return false;
770
- }
771
-
772
- /**
773
- * Detect if a line has a linebreak and so we need to account for it when moving
774
- * and counting style.
775
- * This is important only for splitByGrapheme at the end of wrapping.
776
- * If we are not wrapping the offset is always 1
777
- * @return Number
778
- */
779
- missingNewlineOffset(lineIndex: number, skipWrapping?: boolean): 0 | 1 {
780
- if (this.splitByGrapheme && !skipWrapping) {
781
- return this.isEndOfWrapping(lineIndex) ? 1 : 0;
782
- }
783
- return 1;
784
- }
785
-
786
- /**
787
- * Gets lines of text to render in the Textbox. This function calculates
788
- * text wrapping on the fly every time it is called.
789
- * @param {String} text text to split
790
- * @returns {Array} Array of lines in the Textbox.
791
- * @override
792
- */
793
- _splitTextIntoLines(text: string) {
794
- // Check if we need browser wrapping using smart font detection
795
- const needsBrowserWrapping = this.fontFamily && fontLacksEnglishGlyphsCached(this.fontFamily);
796
-
797
- if (needsBrowserWrapping) {
798
- // Cache key based on text content, width, font properties, AND text alignment
799
- const textHash = text.length + text.slice(0, 50); // Include text content in cache key
800
- const cacheKey = `${textHash}|${this.width}|${this.fontSize}|${this.fontFamily}|${this.textAlign}`;
801
-
802
- // Check if we have a cached result and nothing has changed
803
- if ((this as any)._browserWrapCache && (this as any)._browserWrapCache.key === cacheKey) {
804
- const cachedResult = (this as any)._browserWrapCache.result;
805
-
806
- // For justify alignment, ensure we have the measurements
807
- if (this.textAlign.includes('justify') && !(cachedResult as any).justifySpaceMeasurements) {
808
- // Fall through to recalculate
809
- } else {
810
- return cachedResult;
811
- }
812
- }
813
-
814
- const result = this._splitTextIntoLinesWithBrowser(text);
815
-
816
- // Cache the result
817
- (this as any)._browserWrapCache = { key: cacheKey, result };
818
-
819
- // Mark that we used browser wrapping to prevent dynamicMinWidth modifications
820
- (this as any)._usingBrowserWrapping = true;
821
-
822
- return result;
823
- }
824
-
825
- // Clear the browser wrapping flag when using regular wrapping
826
- (this as any)._usingBrowserWrapping = false;
827
-
828
- // Default Fabric wrapping for other fonts
829
- const newText = super._splitTextIntoLines(text),
830
- graphemeLines = this._wrapText(newText.lines, this.width),
831
- lines = new Array(graphemeLines.length);
832
- for (let i = 0; i < graphemeLines.length; i++) {
833
- lines[i] = graphemeLines[i].join('');
834
- }
835
- newText.lines = lines;
836
- newText.graphemeLines = graphemeLines;
837
- return newText;
838
- }
839
-
840
- /**
841
- * Use browser's native text wrapping for accurate handling of fonts without English glyphs
842
- * @private
843
- */
844
- _splitTextIntoLinesWithBrowser(text: string) {
845
- if (typeof document === 'undefined') {
846
- // Fallback to regular wrapping in Node.js
847
- return this._splitTextIntoLinesDefault(text);
848
- }
849
-
850
- // Create a hidden element that mimics the overlay editor
851
- const testElement = document.createElement('div');
852
- testElement.style.position = 'absolute';
853
- testElement.style.left = '-9999px';
854
- testElement.style.visibility = 'hidden';
855
- testElement.style.fontSize = `${this.fontSize}px`;
856
- testElement.style.fontFamily = `"${this.fontFamily}"`;
857
- testElement.style.fontWeight = String(this.fontWeight || 'normal');
858
- testElement.style.fontStyle = String(this.fontStyle || 'normal');
859
- testElement.style.lineHeight = String(this.lineHeight || 1.16);
860
-
861
- testElement.style.width = `${this.width}px`;
862
-
863
- testElement.style.direction = this.direction || 'ltr';
864
- testElement.style.whiteSpace = 'pre-wrap';
865
- testElement.style.wordBreak = 'normal';
866
- testElement.style.overflowWrap = 'break-word';
867
-
868
- // Set browser-native text alignment (including justify)
869
- if (this.textAlign.includes('justify')) {
870
- testElement.style.textAlign = 'justify';
871
- testElement.style.textAlignLast = 'auto'; // Let browser decide last line alignment
872
- } else {
873
- testElement.style.textAlign = this.textAlign;
874
- }
875
-
876
- testElement.textContent = text;
877
-
878
- document.body.appendChild(testElement);
879
-
880
- // Get the browser's natural line breaks
881
- const range = document.createRange();
882
- const lines: string[] = [];
883
- const graphemeLines: string[][] = [];
884
-
885
- try {
886
- // Simple approach: split by measuring character positions
887
- const textNode = testElement.firstChild;
888
- if (textNode && textNode.nodeType === Node.TEXT_NODE) {
889
- let currentLineStart = 0;
890
- const textLength = text.length;
891
- let previousBottom = 0;
892
-
893
- for (let i = 0; i <= textLength; i++) {
894
- range.setStart(textNode, currentLineStart);
895
- range.setEnd(textNode, i);
896
- const rect = range.getBoundingClientRect();
897
-
898
- if (i > currentLineStart && (rect.bottom > previousBottom + 5 || i === textLength)) {
899
- // New line detected or end of text
900
- const lineEnd = i === textLength ? i : i - 1;
901
- const lineText = text.substring(currentLineStart, lineEnd).trim();
902
- if (lineText) {
903
- lines.push(lineText);
904
- // Convert to graphemes for compatibility
905
- const graphemeLine = lineText.split('');
906
- graphemeLines.push(graphemeLine);
907
- }
908
- currentLineStart = lineEnd;
909
- previousBottom = rect.bottom;
910
- }
911
- }
912
- }
913
- } catch (error) {
914
- console.warn('Browser wrapping failed, using fallback:', error);
915
- document.body.removeChild(testElement);
916
- return this._splitTextIntoLinesDefault(text);
917
- }
918
-
919
- // Extract actual browser height BEFORE removing element
920
- const actualBrowserHeight = testElement.scrollHeight;
921
- const offsetHeight = testElement.offsetHeight;
922
- const clientHeight = testElement.clientHeight;
923
- const boundingRect = testElement.getBoundingClientRect();
924
-
925
- console.log(`🔤 Browser element measurements:`);
926
- console.log(` scrollHeight: ${actualBrowserHeight}px (content + padding + hidden overflow)`);
927
- console.log(` offsetHeight: ${offsetHeight}px (content + padding + border)`);
928
- console.log(` clientHeight: ${clientHeight}px (content + padding, no border/scrollbar)`);
929
- console.log(` boundingRect.height: ${boundingRect.height}px (actual rendered height)`);
930
- console.log(` Font size: ${this.fontSize}px, Line height: ${this.lineHeight || 1.16}, Lines: ${lines.length}`);
931
-
932
- // For justify alignment, extract space measurements from browser BEFORE removing element
933
- let justifySpaceMeasurements = null;
934
- if (this.textAlign.includes('justify')) {
935
- justifySpaceMeasurements = this._extractJustifySpaceMeasurements(testElement, lines);
936
- }
937
-
938
- document.body.removeChild(testElement);
939
-
940
- console.log(`🔤 Browser wrapping result: ${lines.length} lines`);
941
-
942
- // Try different height measurements to find the most accurate
943
- let bestHeight = actualBrowserHeight;
944
-
945
- // If scrollHeight and offsetHeight differ significantly, investigate
946
- if (Math.abs(actualBrowserHeight - offsetHeight) > 2) {
947
- console.log(`🔤 Height discrepancy detected: scrollHeight=${actualBrowserHeight}px vs offsetHeight=${offsetHeight}px`);
948
- }
949
-
950
- // Consider using boundingRect height if it's larger (sometimes more accurate for visible content)
951
- if (boundingRect.height > bestHeight) {
952
- console.log(`🔤 Using boundingRect height (${boundingRect.height}px) instead of scrollHeight (${bestHeight}px)`);
953
- bestHeight = boundingRect.height;
954
- }
955
-
956
- // Font-specific height adjustments for accurate bounding box
957
- let adjustedHeight = bestHeight;
958
-
959
- // Fonts without English glyphs need additional height buffer due to different font metrics
960
- const lacksEnglishGlyphs = fontLacksEnglishGlyphsCached(this.fontFamily);
961
- if (lacksEnglishGlyphs) {
962
- const glyphBuffer = this.fontSize * 0.25; // 25% of font size for non-English fonts
963
- adjustedHeight = bestHeight + glyphBuffer;
964
- console.log(`🔤 Non-English font detected (${this.fontFamily}): Adding ${glyphBuffer}px buffer (${bestHeight}px + ${glyphBuffer}px = ${adjustedHeight}px)`);
965
- } else {
966
- console.log(`🔤 Standard font (${this.fontFamily}): Using browser height directly (${bestHeight}px)`);
967
- }
968
-
969
- return {
970
- _unwrappedLines: [text.split('')],
971
- lines: lines,
972
- graphemeText: text.split(''),
973
- graphemeLines: graphemeLines,
974
- justifySpaceMeasurements: justifySpaceMeasurements,
975
- actualBrowserHeight: adjustedHeight,
976
- };
977
- }
978
-
979
-
980
-
981
-
982
- /**
983
- * Extract justify space measurements from browser
984
- * @private
985
- */
986
- _extractJustifySpaceMeasurements(element: HTMLElement, lines: string[]) {
987
- console.log(`🔤 Extracting browser justify space measurements for ${lines.length} lines`);
988
-
989
- // For now, we'll use a simplified approach:
990
- // Apply uniform space expansion to match the line width
991
- const spaceWidths: number[][] = [];
992
-
993
- lines.forEach((line, lineIndex) => {
994
- const lineSpaces: number[] = [];
995
- const spaceCount = (line.match(/\s/g) || []).length;
996
-
997
- if (spaceCount > 0 && lineIndex < lines.length - 1) { // Don't justify last line
998
- // Calculate how much space expansion is needed
999
- const normalSpaceWidth = 6.4; // Default space width for STV font
1000
- const lineWidth = this.width;
1001
-
1002
- // Estimate natural line width
1003
- const charCount = line.length - spaceCount;
1004
- const avgCharWidth = 12; // Approximate for STV font
1005
- const naturalWidth = charCount * avgCharWidth + spaceCount * normalSpaceWidth;
1006
-
1007
- // Calculate expanded space width
1008
- const remainingSpace = lineWidth - (charCount * avgCharWidth);
1009
- const expandedSpaceWidth = remainingSpace / spaceCount;
1010
-
1011
- console.log(`🔤 Line ${lineIndex}: ${spaceCount} spaces, natural: ${normalSpaceWidth}px -> justified: ${expandedSpaceWidth.toFixed(1)}px`);
1012
-
1013
- // Fill array with expanded space widths for this line
1014
- for (let i = 0; i < spaceCount; i++) {
1015
- lineSpaces.push(expandedSpaceWidth);
1016
- }
1017
- }
1018
-
1019
- spaceWidths.push(lineSpaces);
1020
- });
1021
-
1022
- return spaceWidths;
1023
- }
1024
-
1025
- /**
1026
- * Apply browser-calculated justify space measurements
1027
- * @private
1028
- */
1029
- _applyBrowserJustifySpaces() {
1030
- if (!this._textLines || !this.__charBounds) {
1031
- console.warn('🔤 BROWSER JUSTIFY: _textLines or __charBounds not ready');
1032
- return;
1033
- }
1034
-
1035
- // Get space measurements from browser wrapping result
1036
- const styleMap = this._styleMap as any;
1037
- if (!styleMap || !styleMap.justifySpaceMeasurements) {
1038
- console.warn('🔤 BROWSER JUSTIFY: No justify space measurements available');
1039
- return;
1040
- }
1041
-
1042
- const spaceWidths = styleMap.justifySpaceMeasurements as number[][];
1043
- console.log('🔤 BROWSER JUSTIFY: Applying space measurements to __charBounds');
1044
-
1045
- // Apply space widths to character bounds
1046
- this._textLines.forEach((line, lineIndex) => {
1047
- if (!this.__charBounds || !this.__charBounds[lineIndex] || !spaceWidths[lineIndex]) return;
1048
-
1049
- const lineBounds = this.__charBounds[lineIndex];
1050
- const lineSpaceWidths = spaceWidths[lineIndex];
1051
- let spaceIndex = 0;
1052
-
1053
- for (let charIndex = 0; charIndex < line.length; charIndex++) {
1054
- if (/\s/.test(line[charIndex]) && spaceIndex < lineSpaceWidths.length) {
1055
- const expandedWidth = lineSpaceWidths[spaceIndex];
1056
- if (lineBounds[charIndex]) {
1057
- const oldWidth = lineBounds[charIndex].width;
1058
- lineBounds[charIndex].width = expandedWidth;
1059
- console.log(`🔤 Line ${lineIndex} space ${spaceIndex}: ${oldWidth.toFixed(1)}px -> ${expandedWidth.toFixed(1)}px`);
1060
- }
1061
- spaceIndex++;
1062
- }
1063
- }
1064
- });
1065
- }
1066
-
1067
- /**
1068
- * Fallback to default Fabric wrapping
1069
- * @private
1070
- */
1071
- _splitTextIntoLinesDefault(text: string) {
1072
- const newText = super._splitTextIntoLines(text),
1073
- graphemeLines = this._wrapText(newText.lines, this.width),
1074
- lines = new Array(graphemeLines.length);
1075
- for (let i = 0; i < graphemeLines.length; i++) {
1076
- lines[i] = graphemeLines[i].join('');
1077
- }
1078
- newText.lines = lines;
1079
- newText.graphemeLines = graphemeLines;
1080
- return newText;
1081
- }
1082
-
1083
- getMinWidth() {
1084
- return Math.max(this.minWidth, this.dynamicMinWidth);
1085
- }
1086
-
1087
- _removeExtraneousStyles() {
1088
- const linesToKeep = new Map();
1089
- for (const prop in this._styleMap) {
1090
- const propNumber = parseInt(prop, 10);
1091
- if (this._textLines[propNumber]) {
1092
- const lineIndex = this._styleMap[prop].line;
1093
- linesToKeep.set(`${lineIndex}`, true);
1094
- }
1095
- }
1096
- for (const prop in this.styles) {
1097
- if (!linesToKeep.has(prop)) {
1098
- delete this.styles[prop];
1099
- }
1100
- }
1101
- }
1102
-
1103
- /**
1104
- * Initialize event listeners for safety snap functionality
1105
- * @private
1106
- */
1107
- private initializeEventListeners(): void {
1108
- // Track which side is being used for resize to handle position compensation
1109
- let resizeOrigin: 'left' | 'right' | null = null;
1110
-
1111
- // Detect resize origin during resizing
1112
- this.on('resizing', (e: any) => {
1113
- // Check transform origin to determine which side is being resized
1114
- if (e.transform) {
1115
- const { originX } = e.transform;
1116
- // originX tells us which side is the anchor - opposite side is being dragged
1117
- resizeOrigin = originX === 'right' ? 'left' : originX === 'left' ? 'right' : null;
1118
- } else if (e.originX) {
1119
- const { originX } = e;
1120
- resizeOrigin = originX === 'right' ? 'left' : originX === 'left' ? 'right' : null;
1121
- }
1122
- });
1123
-
1124
- // Only trigger safety snap after resize is complete (not during)
1125
- // Use 'modified' event which fires after user releases the mouse
1126
- this.on('modified', () => {
1127
- const currentResizeOrigin = resizeOrigin; // Capture the value before reset
1128
- // Small delay to ensure text layout is updated
1129
- setTimeout(() => this.safetySnapWidth(currentResizeOrigin), 10);
1130
- resizeOrigin = null; // Reset after capturing
1131
- });
1132
-
1133
- // Also listen to canvas-level modified event as backup
1134
- this.canvas?.on('object:modified', (e) => {
1135
- if (e.target === this) {
1136
- const currentResizeOrigin = resizeOrigin; // Capture the value before reset
1137
- setTimeout(() => this.safetySnapWidth(currentResizeOrigin), 10);
1138
- resizeOrigin = null; // Reset after capturing
1139
- }
1140
- });
1141
- }
1142
-
1143
- /**
1144
- * Safety snap to prevent glyph clipping after manual resize.
1145
- * Similar to Polotno - checks if any glyphs are too close to edges
1146
- * and automatically expands width if needed.
1147
- * @private
1148
- * @param resizeOrigin - Which side was used for resizing ('left' or 'right')
1149
- */
1150
- private safetySnapWidth(resizeOrigin?: 'left' | 'right' | null): void {
1151
- // For Textbox objects, we always want to check for clipping regardless of isWrapping flag
1152
- if (!this._textLines || this.type.toLowerCase() !== 'textbox' || this._textLines.length === 0) {
1153
- return;
1154
- }
1155
-
1156
- const lineCount = this._textLines.length;
1157
- if (lineCount === 0) return;
1158
-
1159
- // Check all lines, not just the last one
1160
- let maxActualLineWidth = 0; // Actual measured width without buffers
1161
- let maxRequiredWidth = 0; // Width including RTL buffer
1162
-
1163
- for (let i = 0; i < lineCount; i++) {
1164
- const lineText = this._textLines[i].join(''); // Convert grapheme array to string
1165
- const lineWidth = this.getLineWidth(i);
1166
- maxActualLineWidth = Math.max(maxActualLineWidth, lineWidth);
1167
-
1168
- // RTL detection - regex for Arabic, Hebrew, and other RTL characters
1169
- const rtlRegex = /[\u0590-\u05FF\u0600-\u06FF\u0750-\u077F\uFB50-\uFDFF\uFE70-\uFEFF]/;
1170
- if (rtlRegex.test(lineText)) {
1171
- // Add minimal RTL compensation buffer - just enough to prevent clipping
1172
- const rtlBuffer = (this.fontSize || 16) * 0.15; // 15% of font size (much smaller)
1173
- maxRequiredWidth = Math.max(maxRequiredWidth, lineWidth + rtlBuffer);
1174
- } else {
1175
- maxRequiredWidth = Math.max(maxRequiredWidth, lineWidth);
1176
- }
1177
- }
1178
-
1179
- // Safety margin - how close glyphs can get before we snap
1180
- const safetyThreshold = 2; // px - very subtle trigger
1181
-
1182
- if (maxRequiredWidth > this.width - safetyThreshold) {
1183
- // Set width to exactly what's needed + minimal safety margin
1184
- const newWidth = maxRequiredWidth + 1; // Add just 1px safety margin
1185
-
1186
- // Store original position before width change
1187
- const originalLeft = this.left;
1188
- const originalTop = this.top;
1189
- const widthIncrease = newWidth - this.width;
1190
-
1191
- // Change width
1192
- this.set('width', newWidth);
1193
-
1194
- // Force text layout recalculation
1195
- this.initDimensions();
1196
-
1197
- // Only compensate position when resizing from left handle
1198
- // Right handle resize doesn't shift the text position
1199
- if (resizeOrigin === 'left') {
1200
- // When resizing from left, the expansion pushes text right
1201
- // Compensate by moving the textbox left by the width increase
1202
- this.set({
1203
- 'left': originalLeft - widthIncrease,
1204
- 'top': originalTop
1205
- });
1206
- }
1207
-
1208
- this.setCoords();
1209
-
1210
- // Also refresh the overlay editor if it exists
1211
- if ((this as any).__overlayEditor) {
1212
- setTimeout(() => {
1213
- (this as any).__overlayEditor.refresh();
1214
- }, 0);
1215
- }
1216
-
1217
- this.canvas?.requestRenderAll();
1218
- }
1219
- }
1220
-
1221
- /**
1222
- * Fix character selection mismatch after JSON loading for browser-wrapped fonts
1223
- * @private
1224
- */
1225
- _fixCharacterMappingAfterJsonLoad(): void {
1226
- if ((this as any)._usingBrowserWrapping) {
1227
- // Clear all cached states to force fresh text layout calculation
1228
- (this as any)._browserWrapCache = null;
1229
- (this as any)._lastDimensionState = null;
1230
-
1231
- // Force complete re-initialization
1232
- this.initDimensions();
1233
- this._forceClearCache = true;
1234
-
1235
- // Ensure canvas refresh
1236
- this.setCoords();
1237
- if (this.canvas) {
1238
- this.canvas.requestRenderAll();
1239
- }
1240
- }
1241
- }
1242
-
1243
- /**
1244
- * Force complete textbox re-initialization (useful after JSON loading)
1245
- * Overrides Text version with Textbox-specific logic
1246
- */
1247
- forceTextReinitialization(): void {
1248
- console.log('🔄 Force reinitializing Textbox object');
1249
-
1250
- // CRITICAL: Ensure textbox is marked as initialized
1251
- this.initialized = true;
1252
-
1253
- // Clear all caches and force dirty state
1254
- this._clearCache();
1255
- this.dirty = true;
1256
- this.dynamicMinWidth = 0;
1257
-
1258
- // Force isEditing false to ensure clean state
1259
- this.isEditing = false;
1260
-
1261
- console.log(' → Set initialized=true, dirty=true, cleared caches');
1262
-
1263
- // Re-initialize dimensions (this will handle justify properly)
1264
- this.initDimensions();
1265
-
1266
- // Double-check that justify was applied by checking space widths
1267
- if (this.textAlign.includes('justify') && this.__charBounds) {
1268
- setTimeout(() => {
1269
- // Verify justify was applied by checking if space widths vary
1270
- let hasVariableSpaces = false;
1271
- this.__charBounds.forEach((lineBounds, i) => {
1272
- if (lineBounds && this._textLines && this._textLines[i]) {
1273
- const spaces = lineBounds.filter((bound, j) => /\s/.test(this._textLines[i][j]));
1274
- if (spaces.length > 1) {
1275
- const firstSpaceWidth = spaces[0].width;
1276
- hasVariableSpaces = spaces.some(space => Math.abs(space.width - firstSpaceWidth) > 0.1);
1277
- }
1278
- }
1279
- });
1280
-
1281
- if (!hasVariableSpaces && this.__charBounds.length > 0) {
1282
- console.warn(' ⚠️ Justify spaces still uniform - forcing enlargeSpaces again');
1283
- if (this.enlargeSpaces) {
1284
- this.enlargeSpaces();
1285
- }
1286
- } else {
1287
- console.log(' ✅ Justify spaces properly expanded');
1288
- }
1289
-
1290
- // Ensure height is recalculated - use browser height if available
1291
- if ((this as any)._usingBrowserWrapping && (this as any)._actualBrowserHeight) {
1292
- this.height = (this as any)._actualBrowserHeight;
1293
- console.log(`🔤 JUSTIFY: Preserved browser height: ${this.height}px`);
1294
- } else {
1295
- this.height = this.calcTextHeight();
1296
- console.log(`🔧 JUSTIFY: Used calcTextHeight: ${this.height}px`);
1297
- }
1298
- this.canvas?.requestRenderAll();
1299
- }, 10);
1300
- }
1301
- }
1302
-
1303
- /**
1304
- * Returns object representation of an instance
1305
- * @param {Array} [propertiesToInclude] Any properties that you might want to additionally include in the output
1306
- * @return {Object} object representation of an instance
1307
- */
1308
- toObject<
1309
- T extends Omit<Props & TClassProperties<this>, keyof SProps>,
1310
- K extends keyof T = never,
1311
- >(propertiesToInclude: K[] = []): Pick<T, K> & SProps {
1312
- return super.toObject<T, K>([
1313
- 'minWidth',
1314
- 'splitByGrapheme',
1315
- ...propertiesToInclude,
1316
- ] as K[]);
1317
- }
1318
- }
1319
-
1320
- classRegistry.setClass(Textbox);
1
+ import type { TClassProperties, TOptions } from '../typedefs';
2
+ import { IText } from './IText/IText';
3
+ import { classRegistry } from '../ClassRegistry';
4
+ import { createTextboxDefaultControls } from '../controls/commonControls';
5
+ import { JUSTIFY, JUSTIFY_CENTER } from './Text/constants';
6
+ import type { TextStyleDeclaration } from './Text/StyledText';
7
+ import type { SerializedITextProps, ITextProps } from './IText/IText';
8
+ import type { ITextEvents } from './IText/ITextBehavior';
9
+ import type { TextLinesInfo } from './Text/Text';
10
+ import type { Control } from '../controls/Control';
11
+ import { fontLacksEnglishGlyphsCached } from '../text/measure';
12
+ import { layoutText } from '../text/layout';
13
+
14
+ // @TODO: Many things here are configuration related and shouldn't be on the class nor prototype
15
+ // regexes, list of properties that are not suppose to change by instances, magic consts.
16
+ // this will be a separated effort
17
+ export const textboxDefaultValues: Partial<TClassProperties<Textbox>> = {
18
+ minWidth: 20,
19
+ dynamicMinWidth: 2,
20
+ lockScalingFlip: true,
21
+ noScaleCache: false,
22
+ _wordJoiners: /[ \t\r]/,
23
+ splitByGrapheme: false,
24
+ };
25
+
26
+ export type GraphemeData = {
27
+ wordsData: {
28
+ word: string[];
29
+ width: number;
30
+ }[][];
31
+ largestWordWidth: number;
32
+ };
33
+
34
+ export type StyleMap = Record<string, { line: number; offset: number }>;
35
+
36
+ // @TODO this is not complete
37
+ interface UniqueTextboxProps {
38
+ minWidth: number;
39
+ splitByGrapheme: boolean;
40
+ dynamicMinWidth: number;
41
+ _wordJoiners: RegExp;
42
+ }
43
+
44
+ export interface SerializedTextboxProps
45
+ extends SerializedITextProps,
46
+ Pick<UniqueTextboxProps, 'minWidth' | 'splitByGrapheme'> {}
47
+
48
+ export interface TextboxProps extends ITextProps, UniqueTextboxProps {}
49
+
50
+ /**
51
+ * Textbox class, based on IText, allows the user to resize the text rectangle
52
+ * and wraps lines automatically. Textboxes have their Y scaling locked, the
53
+ * user can only change width. Height is adjusted automatically based on the
54
+ * wrapping of lines.
55
+ */
56
+ export class Textbox<
57
+ Props extends TOptions<TextboxProps> = Partial<TextboxProps>,
58
+ SProps extends SerializedTextboxProps = SerializedTextboxProps,
59
+ EventSpec extends ITextEvents = ITextEvents,
60
+ >
61
+ extends IText<Props, SProps, EventSpec>
62
+ implements UniqueTextboxProps
63
+ {
64
+ /**
65
+ * Minimum width of textbox, in pixels.
66
+ * @type Number
67
+ */
68
+ declare minWidth: number;
69
+
70
+ /**
71
+ * Minimum calculated width of a textbox, in pixels.
72
+ * fixed to 2 so that an empty textbox cannot go to 0
73
+ * and is still selectable without text.
74
+ * @type Number
75
+ */
76
+ declare dynamicMinWidth: number;
77
+
78
+ /**
79
+ * Use this boolean property in order to split strings that have no white space concept.
80
+ * this is a cheap way to help with chinese/japanese
81
+ * @type Boolean
82
+ * @since 2.6.0
83
+ */
84
+ declare splitByGrapheme: boolean;
85
+
86
+ declare _wordJoiners: RegExp;
87
+
88
+ declare _styleMap: StyleMap;
89
+
90
+ declare isWrapping: boolean;
91
+
92
+ static type = 'Textbox';
93
+
94
+ static textLayoutProperties = [...IText.textLayoutProperties, 'width'];
95
+
96
+ static ownDefaults = textboxDefaultValues;
97
+
98
+ static getDefaults(): Record<string, any> {
99
+ return {
100
+ ...super.getDefaults(),
101
+ ...Textbox.ownDefaults,
102
+ };
103
+ }
104
+
105
+ /**
106
+ * Constructor
107
+ * @param {String} text Text string
108
+ * @param {Object} [options] Options object
109
+ */
110
+ constructor(text: string, options?: Props) {
111
+ super(text, { ...Textbox.ownDefaults, ...options } as Props);
112
+ this.initializeEventListeners();
113
+ }
114
+
115
+ /**
116
+ * Creates the default control object.
117
+ * If you prefer to have on instance of controls shared among all objects
118
+ * make this function return an empty object and add controls to the ownDefaults object
119
+ */
120
+ static createControls(): { controls: Record<string, Control> } {
121
+ return { controls: createTextboxDefaultControls() };
122
+ }
123
+
124
+ /**
125
+ * Unlike superclass's version of this function, Textbox does not update
126
+ * its width.
127
+ * @private
128
+ * @override
129
+ */
130
+ initDimensions() {
131
+ if (!this.initialized) {
132
+ this.initialized = true;
133
+ }
134
+
135
+ // Prevent rapid recalculations during moves
136
+ if ((this as any)._usingBrowserWrapping) {
137
+ const now = Date.now();
138
+ const lastCall = (this as any)._lastInitDimensionsTime || 0;
139
+ const isRapidCall = now - lastCall < 100;
140
+ const isDuringLoading = (this as any)._jsonLoading || !(this as any)._browserWrapInitialized;
141
+
142
+ if (isRapidCall && !isDuringLoading) {
143
+ return;
144
+ }
145
+ (this as any)._lastInitDimensionsTime = now;
146
+ }
147
+
148
+ // Skip if nothing changed
149
+ const currentState = `${this.text}|${this.width}|${this.fontSize}|${this.fontFamily}|${this.textAlign}`;
150
+ if ((this as any)._lastDimensionState === currentState && this._textLines && this._textLines.length > 0) {
151
+ return;
152
+ }
153
+ (this as any)._lastDimensionState = currentState;
154
+
155
+ // Use advanced layout if enabled
156
+ if (this.enableAdvancedLayout) {
157
+ return this.initDimensionsAdvanced();
158
+ }
159
+
160
+ this.isEditing && this.initDelayedCursor();
161
+ this._clearCache();
162
+ // clear dynamicMinWidth as it will be different after we re-wrap line
163
+ this.dynamicMinWidth = 0;
164
+ // wrap lines
165
+ const splitTextResult = this._splitText();
166
+ this._styleMap = this._generateStyleMap(splitTextResult);
167
+
168
+ // For browser wrapping, ensure _textLines is set from browser results
169
+ if ((this as any)._usingBrowserWrapping && splitTextResult && splitTextResult.lines) {
170
+ this._textLines = splitTextResult.lines.map(line => line.split(''));
171
+
172
+ // Store justify measurements and browser height
173
+ const justifyMeasurements = (splitTextResult as any).justifySpaceMeasurements;
174
+ if (justifyMeasurements) {
175
+ (this._styleMap as any).justifySpaceMeasurements = justifyMeasurements;
176
+ }
177
+
178
+ const actualHeight = (splitTextResult as any).actualBrowserHeight;
179
+ if (actualHeight) {
180
+ (this as any)._actualBrowserHeight = actualHeight;
181
+ }
182
+ }
183
+ // Don't auto-resize width when using browser wrapping to prevent width increases during moves
184
+ if (!((this as any)._usingBrowserWrapping) && this.dynamicMinWidth > this.width) {
185
+ this._set('width', this.dynamicMinWidth);
186
+ }
187
+
188
+ // For browser wrapping fonts (like STV), ensure minimum width for new textboxes
189
+ // since these fonts can't measure English characters properly
190
+ if ((this as any)._usingBrowserWrapping && this.width < 50) {
191
+ console.log(`🔤 BROWSER WRAP: Font ${this.fontFamily} has width ${this.width}px, setting to 300px for usability`);
192
+ this.width = 300;
193
+ }
194
+
195
+ // Mark browser wrapping as initialized when complete
196
+ if ((this as any)._usingBrowserWrapping) {
197
+ (this as any)._browserWrapInitialized = true;
198
+ }
199
+
200
+ this.calcTextWidth();
201
+
202
+ if (this.textAlign.includes(JUSTIFY)) {
203
+ // For browser wrapping fonts, apply browser-calculated justify spaces
204
+ if ((this as any)._usingBrowserWrapping) {
205
+ console.log('🔤 BROWSER WRAP: Applying browser-calculated justify spaces');
206
+ this._applyBrowserJustifySpaces();
207
+ return;
208
+ }
209
+
210
+ // Don't apply justify alignment during drag operations to prevent snapping
211
+ const now = Date.now();
212
+ const lastDragTime = (this as any)._lastInitDimensionsTime || 0;
213
+ const isDuringDrag = now - lastDragTime < 200; // 200ms window for drag detection
214
+
215
+ if (isDuringDrag) {
216
+ console.log('🔤 Skipping justify during drag operation to prevent snapping');
217
+ return;
218
+ }
219
+
220
+ // For non-browser-wrapping fonts, use Fabric's justify system
221
+ // once text is measured we need to make space fatter to make justified text.
222
+ // Ensure __charBounds exists and fonts are ready before applying justify
223
+ if (this.__charBounds && this.__charBounds.length > 0) {
224
+ // Check if font is ready for accurate justify calculations
225
+ const fontReady = this._isFontReady ? this._isFontReady() : true;
226
+ if (fontReady) {
227
+ this.enlargeSpaces();
228
+ } else {
229
+ console.warn('⚠️ Textbox: Font not ready for justify, deferring enlargeSpaces');
230
+ // Defer justify calculation until font is ready
231
+ this._scheduleJustifyAfterFontLoad();
232
+ }
233
+ } else {
234
+ console.warn('⚠️ Textbox: __charBounds not ready for justify alignment, deferring enlargeSpaces');
235
+ // Defer the justify calculation until the next frame
236
+ setTimeout(() => {
237
+ if (this.__charBounds && this.__charBounds.length > 0 && this.enlargeSpaces) {
238
+ console.log('🔧 Applying deferred Textbox justify alignment');
239
+ this.enlargeSpaces();
240
+ this.canvas?.requestRenderAll();
241
+ }
242
+ }, 0);
243
+ }
244
+ }
245
+ // Calculate height - use Fabric's calculation for proper text rendering space
246
+ if ((this as any)._usingBrowserWrapping && this._textLines && this._textLines.length > 0) {
247
+ const actualBrowserHeight = (this as any)._actualBrowserHeight;
248
+ const oldHeight = this.height;
249
+ // Use Fabric's height calculation since it knows how much space text rendering needs
250
+ this.height = this.calcTextHeight();
251
+
252
+ // Force canvas refresh and control update if height changed significantly
253
+ if (Math.abs(this.height - oldHeight) > 1) {
254
+ this.setCoords();
255
+ this.canvas?.requestRenderAll();
256
+
257
+ // DEBUG: Log exact positioning details
258
+ console.log(`🎯 POSITIONING DEBUG:`);
259
+ console.log(` Textbox height: ${this.height}px`);
260
+ console.log(` Textbox top: ${this.top}px`);
261
+ console.log(` Textbox left: ${this.left}px`);
262
+ console.log(` Text lines: ${this._textLines?.length || 0}`);
263
+ console.log(` Font size: ${this.fontSize}px`);
264
+ console.log(` Line height: ${this.lineHeight || 1.16}`);
265
+ console.log(` Calculated line height: ${this.fontSize * (this.lineHeight || 1.16)}px`);
266
+ console.log(` _getTopOffset(): ${this._getTopOffset()}px`);
267
+ console.log(` calcTextHeight(): ${this.calcTextHeight()}px`);
268
+ console.log(` Browser height: ${actualBrowserHeight}px`);
269
+ console.log(` Height difference: ${this.height - this.calcTextHeight()}px`);
270
+ }
271
+ } else {
272
+ this.height = this.calcTextHeight();
273
+ }
274
+
275
+ // Double-check that justify was applied by checking space widths
276
+ if (this.textAlign.includes('justify') && this.__charBounds) {
277
+ setTimeout(() => {
278
+ // Verify justify was applied by checking if space widths vary
279
+ let hasVariableSpaces = false;
280
+ this.__charBounds.forEach((lineBounds, i) => {
281
+ if (lineBounds && this._textLines && this._textLines[i]) {
282
+ const spaces = lineBounds.filter((bound, j) => /\s/.test(this._textLines[i][j]));
283
+ if (spaces.length > 1) {
284
+ const firstSpaceWidth = spaces[0].width;
285
+ hasVariableSpaces = spaces.some(space => Math.abs(space.width - firstSpaceWidth) > 0.1);
286
+ }
287
+ }
288
+ });
289
+
290
+ if (!hasVariableSpaces && this.__charBounds.length > 0) {
291
+ console.warn(' ⚠️ Justify spaces still uniform - forcing enlargeSpaces again');
292
+ if (this.enlargeSpaces) {
293
+ this.enlargeSpaces();
294
+ this.canvas?.requestRenderAll();
295
+ }
296
+ } else {
297
+ console.log(' ✅ Justify spaces properly expanded');
298
+ }
299
+ }, 10);
300
+ }
301
+ }
302
+
303
+ /**
304
+ * Schedule justify calculation after font loads (Textbox-specific)
305
+ * @private
306
+ */
307
+ _scheduleJustifyAfterFontLoad(): void {
308
+ if (typeof document === 'undefined' || !('fonts' in document)) {
309
+ return;
310
+ }
311
+
312
+ // Only schedule if not already waiting
313
+ if ((this as any)._fontJustifyScheduled) {
314
+ return;
315
+ }
316
+ (this as any)._fontJustifyScheduled = true;
317
+
318
+ const fontSpec = `${this.fontSize}px ${this.fontFamily}`;
319
+ document.fonts.load(fontSpec).then(() => {
320
+ (this as any)._fontJustifyScheduled = false;
321
+ console.log('🔧 Textbox: Font loaded, applying justify alignment');
322
+
323
+ // Re-run initDimensions to ensure proper justify calculation
324
+ this.initDimensions();
325
+ this.canvas?.requestRenderAll();
326
+ }).catch(() => {
327
+ (this as any)._fontJustifyScheduled = false;
328
+ console.warn('⚠️ Textbox: Font loading failed, justify may be incorrect');
329
+ });
330
+ }
331
+
332
+ /**
333
+ * Advanced dimensions calculation using new layout engine
334
+ * @private
335
+ */
336
+ initDimensionsAdvanced() {
337
+ if (!this.initialized) {
338
+ return;
339
+ }
340
+
341
+ this.isEditing && this.initDelayedCursor();
342
+ this._clearCache();
343
+ this.dynamicMinWidth = 0;
344
+
345
+ // Use new layout engine
346
+ const layout = layoutText({
347
+ text: this.text,
348
+ width: this.width,
349
+ height: this.height,
350
+ wrap: this.wrap || 'word',
351
+ align: (this as any)._mapTextAlignToAlign(this.textAlign),
352
+ ellipsis: this.ellipsis || false,
353
+ fontSize: this.fontSize,
354
+ lineHeight: this.lineHeight,
355
+ letterSpacing: this.letterSpacing || 0,
356
+ charSpacing: this.charSpacing,
357
+ direction: this.direction === 'inherit' ? 'ltr' : this.direction,
358
+ fontFamily: this.fontFamily,
359
+ fontStyle: this.fontStyle,
360
+ fontWeight: this.fontWeight,
361
+ verticalAlign: this.verticalAlign || 'top',
362
+ });
363
+
364
+ // Update dynamic minimum width based on layout
365
+ if (layout.lines.length > 0) {
366
+ const maxLineWidth = Math.max(...layout.lines.map(line => line.width));
367
+ this.dynamicMinWidth = Math.max(this.minWidth, maxLineWidth);
368
+ }
369
+
370
+ // Adjust width if needed (preserving Textbox behavior)
371
+ if (this.dynamicMinWidth > this.width) {
372
+ this._set('width', this.dynamicMinWidth);
373
+ // Re-layout with new width
374
+ const newLayout = layoutText({
375
+ ...(this as any)._getAdvancedLayoutOptions(),
376
+ width: this.width,
377
+ });
378
+ this.height = newLayout.totalHeight;
379
+ (this as any)._convertLayoutToLegacyFormat(newLayout);
380
+ } else {
381
+ this.height = layout.totalHeight;
382
+ (this as any)._convertLayoutToLegacyFormat(layout);
383
+ }
384
+
385
+ // Generate style map for compatibility
386
+ this._styleMap = this._generateStyleMapFromLayout(layout);
387
+ this.dirty = true;
388
+ }
389
+
390
+ /**
391
+ * Generate style map from new layout format
392
+ * @private
393
+ */
394
+ _generateStyleMapFromLayout(layout: any): StyleMap {
395
+ const map: StyleMap = {};
396
+ let realLineCount = 0;
397
+ let charCount = 0;
398
+
399
+ layout.lines.forEach((line: any, i: number) => {
400
+ if (line.text.includes('\n') && i > 0) {
401
+ realLineCount++;
402
+ }
403
+
404
+ map[i] = { line: realLineCount, offset: 0 };
405
+ charCount += line.graphemes.length;
406
+
407
+ if (i < layout.lines.length - 1) {
408
+ charCount += 1; // newline character
409
+ }
410
+ });
411
+
412
+ return map;
413
+ }
414
+
415
+ /**
416
+ * Generate an object that translates the style object so that it is
417
+ * broken up by visual lines (new lines and automatic wrapping).
418
+ * The original text styles object is broken up by actual lines (new lines only),
419
+ * which is only sufficient for Text / IText
420
+ * @private
421
+ */
422
+ _generateStyleMap(textInfo: TextLinesInfo): StyleMap {
423
+ let realLineCount = 0,
424
+ realLineCharCount = 0,
425
+ charCount = 0;
426
+ const map: StyleMap = {};
427
+
428
+ for (let i = 0; i < textInfo.graphemeLines.length; i++) {
429
+ if (textInfo.graphemeText[charCount] === '\n' && i > 0) {
430
+ realLineCharCount = 0;
431
+ charCount++;
432
+ realLineCount++;
433
+ } else if (
434
+ !this.splitByGrapheme &&
435
+ this._reSpaceAndTab.test(textInfo.graphemeText[charCount]) &&
436
+ i > 0
437
+ ) {
438
+ // this case deals with space's that are removed from end of lines when wrapping
439
+ realLineCharCount++;
440
+ charCount++;
441
+ }
442
+
443
+ map[i] = { line: realLineCount, offset: realLineCharCount };
444
+
445
+ charCount += textInfo.graphemeLines[i].length;
446
+ realLineCharCount += textInfo.graphemeLines[i].length;
447
+ }
448
+
449
+ return map;
450
+ }
451
+
452
+ /**
453
+ * Returns true if object has a style property or has it on a specified line
454
+ * @param {Number} lineIndex
455
+ * @return {Boolean}
456
+ */
457
+ styleHas(property: keyof TextStyleDeclaration, lineIndex: number): boolean {
458
+ if (this._styleMap && !this.isWrapping) {
459
+ const map = this._styleMap[lineIndex];
460
+ if (map) {
461
+ lineIndex = map.line;
462
+ }
463
+ }
464
+ return super.styleHas(property, lineIndex);
465
+ }
466
+
467
+ /**
468
+ * Returns true if object has no styling or no styling in a line
469
+ * @param {Number} lineIndex , lineIndex is on wrapped lines.
470
+ * @return {Boolean}
471
+ */
472
+ isEmptyStyles(lineIndex: number): boolean {
473
+ if (!this.styles) {
474
+ return true;
475
+ }
476
+ let offset = 0,
477
+ nextLineIndex = lineIndex + 1,
478
+ nextOffset: number,
479
+ shouldLimit = false;
480
+ const map = this._styleMap[lineIndex],
481
+ mapNextLine = this._styleMap[lineIndex + 1];
482
+ if (map) {
483
+ lineIndex = map.line;
484
+ offset = map.offset;
485
+ }
486
+ if (mapNextLine) {
487
+ nextLineIndex = mapNextLine.line;
488
+ shouldLimit = nextLineIndex === lineIndex;
489
+ nextOffset = mapNextLine.offset;
490
+ }
491
+ const obj =
492
+ typeof lineIndex === 'undefined'
493
+ ? this.styles
494
+ : { line: this.styles[lineIndex] };
495
+ for (const p1 in obj) {
496
+ for (const p2 in obj[p1]) {
497
+ const p2Number = parseInt(p2, 10);
498
+ if (p2Number >= offset && (!shouldLimit || p2Number < nextOffset!)) {
499
+ for (const p3 in obj[p1][p2]) {
500
+ return false;
501
+ }
502
+ }
503
+ }
504
+ }
505
+ return true;
506
+ }
507
+
508
+ /**
509
+ * @protected
510
+ * @param {Number} lineIndex
511
+ * @param {Number} charIndex
512
+ * @return {TextStyleDeclaration} a style object reference to the existing one or a new empty object when undefined
513
+ */
514
+ _getStyleDeclaration(
515
+ lineIndex: number,
516
+ charIndex: number,
517
+ ): TextStyleDeclaration {
518
+ if (this._styleMap && !this.isWrapping) {
519
+ const map = this._styleMap[lineIndex];
520
+ if (!map) {
521
+ return {};
522
+ }
523
+ lineIndex = map.line;
524
+ charIndex = map.offset + charIndex;
525
+ }
526
+ return super._getStyleDeclaration(lineIndex, charIndex);
527
+ }
528
+
529
+ /**
530
+ * @param {Number} lineIndex
531
+ * @param {Number} charIndex
532
+ * @param {Object} style
533
+ * @private
534
+ */
535
+ protected _setStyleDeclaration(
536
+ lineIndex: number,
537
+ charIndex: number,
538
+ style: object,
539
+ ) {
540
+ const map = this._styleMap[lineIndex];
541
+ super._setStyleDeclaration(map.line, map.offset + charIndex, style);
542
+ }
543
+
544
+ /**
545
+ * @param {Number} lineIndex
546
+ * @param {Number} charIndex
547
+ * @private
548
+ */
549
+ protected _deleteStyleDeclaration(lineIndex: number, charIndex: number) {
550
+ const map = this._styleMap[lineIndex];
551
+ super._deleteStyleDeclaration(map.line, map.offset + charIndex);
552
+ }
553
+
554
+ /**
555
+ * probably broken need a fix
556
+ * Returns the real style line that correspond to the wrapped lineIndex line
557
+ * Used just to verify if the line does exist or not.
558
+ * @param {Number} lineIndex
559
+ * @returns {Boolean} if the line exists or not
560
+ * @private
561
+ */
562
+ protected _getLineStyle(lineIndex: number): boolean {
563
+ const map = this._styleMap[lineIndex];
564
+ return !!this.styles[map.line];
565
+ }
566
+
567
+ /**
568
+ * Set the line style to an empty object so that is initialized
569
+ * @param {Number} lineIndex
570
+ * @param {Object} style
571
+ * @private
572
+ */
573
+ protected _setLineStyle(lineIndex: number) {
574
+ const map = this._styleMap[lineIndex];
575
+ super._setLineStyle(map.line);
576
+ }
577
+
578
+ /**
579
+ * Wraps text using the 'width' property of Textbox. First this function
580
+ * splits text on newlines, so we preserve newlines entered by the user.
581
+ * Then it wraps each line using the width of the Textbox by calling
582
+ * _wrapLine().
583
+ * @param {Array} lines The string array of text that is split into lines
584
+ * @param {Number} desiredWidth width you want to wrap to
585
+ * @returns {Array} Array of lines
586
+ */
587
+ _wrapText(lines: string[], desiredWidth: number): string[][] {
588
+ this.isWrapping = true;
589
+ // extract all thewords and the widths to optimally wrap lines.
590
+ const data = this.getGraphemeDataForRender(lines);
591
+ const wrapped: string[][] = [];
592
+ for (let i = 0; i < data.wordsData.length; i++) {
593
+ wrapped.push(...this._wrapLine(i, desiredWidth, data));
594
+ }
595
+ this.isWrapping = false;
596
+ return wrapped;
597
+ }
598
+
599
+ /**
600
+ * For each line of text terminated by an hard line stop,
601
+ * measure each word width and extract the largest word from all.
602
+ * The returned words here are the one that at the end will be rendered.
603
+ * @param {string[]} lines the lines we need to measure
604
+ *
605
+ */
606
+ getGraphemeDataForRender(lines: string[]): GraphemeData {
607
+ const splitByGrapheme = this.splitByGrapheme,
608
+ infix = splitByGrapheme ? '' : ' ';
609
+
610
+ let largestWordWidth = 0;
611
+
612
+ const data = lines.map((line, lineIndex) => {
613
+ let offset = 0;
614
+ const wordsOrGraphemes = splitByGrapheme
615
+ ? this.graphemeSplit(line)
616
+ : this.wordSplit(line);
617
+
618
+ if (wordsOrGraphemes.length === 0) {
619
+ return [{ word: [], width: 0 }];
620
+ }
621
+
622
+ return wordsOrGraphemes.map((word: string) => {
623
+ // if using splitByGrapheme words are already in graphemes.
624
+ const graphemeArray = splitByGrapheme
625
+ ? [word]
626
+ : this.graphemeSplit(word);
627
+ const width = this._measureWord(graphemeArray, lineIndex, offset);
628
+ largestWordWidth = Math.max(width, largestWordWidth);
629
+ offset += graphemeArray.length + infix.length;
630
+ return { word: graphemeArray, width };
631
+ });
632
+ });
633
+
634
+ return {
635
+ wordsData: data,
636
+ largestWordWidth,
637
+ };
638
+ }
639
+
640
+ /**
641
+ * Helper function to measure a string of text, given its lineIndex and charIndex offset
642
+ * It gets called when charBounds are not available yet.
643
+ * Override if necessary
644
+ * Use with {@link Textbox#wordSplit}
645
+ *
646
+ * @param {CanvasRenderingContext2D} ctx
647
+ * @param {String} text
648
+ * @param {number} lineIndex
649
+ * @param {number} charOffset
650
+ * @returns {number}
651
+ */
652
+ _measureWord(word: string[], lineIndex: number, charOffset = 0): number {
653
+ let width = 0,
654
+ prevGrapheme;
655
+ const skipLeft = true;
656
+ for (let i = 0, len = word.length; i < len; i++) {
657
+ const box = this._getGraphemeBox(
658
+ word[i],
659
+ lineIndex,
660
+ i + charOffset,
661
+ prevGrapheme,
662
+ skipLeft,
663
+ );
664
+ width += box.kernedWidth;
665
+ prevGrapheme = word[i];
666
+ }
667
+ return width;
668
+ }
669
+
670
+ /**
671
+ * Override this method to customize word splitting
672
+ * Use with {@link Textbox#_measureWord}
673
+ * @param {string} value
674
+ * @returns {string[]} array of words
675
+ */
676
+ wordSplit(value: string): string[] {
677
+ return value.split(this._wordJoiners);
678
+ }
679
+
680
+ /**
681
+ * Wraps a line of text using the width of the Textbox as desiredWidth
682
+ * and leveraging the known width o words from GraphemeData
683
+ * @private
684
+ * @param {Number} lineIndex
685
+ * @param {Number} desiredWidth width you want to wrap the line to
686
+ * @param {GraphemeData} graphemeData an object containing all the lines' words width.
687
+ * @param {Number} reservedSpace space to remove from wrapping for custom functionalities
688
+ * @returns {Array} Array of line(s) into which the given text is wrapped
689
+ * to.
690
+ */
691
+ _wrapLine(
692
+ lineIndex: number,
693
+ desiredWidth: number,
694
+ { largestWordWidth, wordsData }: GraphemeData,
695
+ reservedSpace = 0,
696
+ ): string[][] {
697
+ const additionalSpace = this._getWidthOfCharSpacing(),
698
+ splitByGrapheme = this.splitByGrapheme,
699
+ graphemeLines = [],
700
+ infix = splitByGrapheme ? '' : ' ';
701
+
702
+ let lineWidth = 0,
703
+ line: string[] = [],
704
+ // spaces in different languages?
705
+ offset = 0,
706
+ infixWidth = 0,
707
+ lineJustStarted = true;
708
+
709
+ desiredWidth -= reservedSpace;
710
+
711
+ const maxWidth = Math.max(
712
+ desiredWidth,
713
+ largestWordWidth,
714
+ this.dynamicMinWidth,
715
+ );
716
+ // layout words
717
+ const data = wordsData[lineIndex];
718
+ offset = 0;
719
+ let i;
720
+ for (i = 0; i < data.length; i++) {
721
+ const { word, width: wordWidth } = data[i];
722
+ offset += word.length;
723
+
724
+ // Predictive wrapping: check if adding this word would exceed the width
725
+ const potentialLineWidth = lineWidth + infixWidth + wordWidth - additionalSpace;
726
+ // Use exact width to match overlay editor behavior
727
+ const conservativeMaxWidth = maxWidth; // No artificial buffer
728
+
729
+ // Debug logging for wrapping decisions
730
+ const currentLineText = line.join('');
731
+ console.log(`🔧 FABRIC WRAP CHECK: "${data[i].word}" -> potential: ${potentialLineWidth.toFixed(1)}px vs limit: ${conservativeMaxWidth.toFixed(1)}px`);
732
+
733
+ if (potentialLineWidth > conservativeMaxWidth && !lineJustStarted) {
734
+ // This word would exceed the width, wrap before adding it
735
+ console.log(`🔧 FABRIC WRAP! Line: "${currentLineText}" (${lineWidth.toFixed(1)}px)`);
736
+ graphemeLines.push(line);
737
+ line = [];
738
+ lineWidth = wordWidth; // Start new line with just this word
739
+ lineJustStarted = true;
740
+ } else {
741
+ // Word fits, add it to current line
742
+ lineWidth = potentialLineWidth + additionalSpace;
743
+ }
744
+
745
+ if (!lineJustStarted && !splitByGrapheme) {
746
+ line.push(infix);
747
+ }
748
+ line = line.concat(word);
749
+
750
+ // Debug: show current line after adding word
751
+ console.log(`🔧 FABRIC AFTER ADD: Line now: "${line.join('')}" (${line.length} chars)`);
752
+
753
+
754
+ infixWidth = splitByGrapheme
755
+ ? 0
756
+ : this._measureWord([infix], lineIndex, offset);
757
+ offset++;
758
+ lineJustStarted = false;
759
+ }
760
+
761
+ i && graphemeLines.push(line);
762
+
763
+ // TODO: this code is probably not necessary anymore.
764
+ // it can be moved out of this function since largestWordWidth is now
765
+ // known in advance
766
+ // Don't modify dynamicMinWidth when using browser wrapping to prevent width increases
767
+ if (!((this as any)._usingBrowserWrapping) && largestWordWidth + reservedSpace > this.dynamicMinWidth) {
768
+ console.log(`🔧 FABRIC updating dynamicMinWidth: ${this.dynamicMinWidth} -> ${largestWordWidth - additionalSpace + reservedSpace}`);
769
+ this.dynamicMinWidth = largestWordWidth - additionalSpace + reservedSpace;
770
+ } else if ((this as any)._usingBrowserWrapping) {
771
+ console.log(`🔤 BROWSER WRAP: Skipping dynamicMinWidth update to prevent width increase`);
772
+ }
773
+
774
+ // Debug: show final wrapped lines
775
+ console.log(`🔧 FABRIC FINAL LINES: ${graphemeLines.length} lines`);
776
+ graphemeLines.forEach((line, i) => {
777
+ console.log(` Line ${i + 1}: "${line.join('')}" (${line.length} chars)`);
778
+ });
779
+
780
+ return graphemeLines;
781
+ }
782
+
783
+ /**
784
+ * Detect if the text line is ended with an hard break
785
+ * text and itext do not have wrapping, return false
786
+ * @param {Number} lineIndex text to split
787
+ * @return {Boolean}
788
+ */
789
+ isEndOfWrapping(lineIndex: number): boolean {
790
+ if (!this._styleMap[lineIndex + 1]) {
791
+ // is last line, return true;
792
+ return true;
793
+ }
794
+ if (this._styleMap[lineIndex + 1].line !== this._styleMap[lineIndex].line) {
795
+ // this is last line before a line break, return true;
796
+ return true;
797
+ }
798
+ return false;
799
+ }
800
+
801
+ /**
802
+ * Detect if a line has a linebreak and so we need to account for it when moving
803
+ * and counting style.
804
+ * This is important only for splitByGrapheme at the end of wrapping.
805
+ * If we are not wrapping the offset is always 1
806
+ * @return Number
807
+ */
808
+ missingNewlineOffset(lineIndex: number, skipWrapping?: boolean): 0 | 1 {
809
+ if (this.splitByGrapheme && !skipWrapping) {
810
+ return this.isEndOfWrapping(lineIndex) ? 1 : 0;
811
+ }
812
+ return 1;
813
+ }
814
+
815
+ /**
816
+ * Gets lines of text to render in the Textbox. This function calculates
817
+ * text wrapping on the fly every time it is called.
818
+ * @param {String} text text to split
819
+ * @returns {Array} Array of lines in the Textbox.
820
+ * @override
821
+ */
822
+ _splitTextIntoLines(text: string) {
823
+ // Check if we need browser wrapping using smart font detection
824
+ const needsBrowserWrapping = this.fontFamily && fontLacksEnglishGlyphsCached(this.fontFamily);
825
+
826
+ if (needsBrowserWrapping) {
827
+ // Cache key based on text content, width, font properties, AND text alignment
828
+ const textHash = text.length + text.slice(0, 50); // Include text content in cache key
829
+ const cacheKey = `${textHash}|${this.width}|${this.fontSize}|${this.fontFamily}|${this.textAlign}`;
830
+
831
+ // Check if we have a cached result and nothing has changed
832
+ if ((this as any)._browserWrapCache && (this as any)._browserWrapCache.key === cacheKey) {
833
+ const cachedResult = (this as any)._browserWrapCache.result;
834
+
835
+ // For justify alignment, ensure we have the measurements
836
+ if (this.textAlign.includes('justify') && !(cachedResult as any).justifySpaceMeasurements) {
837
+ // Fall through to recalculate
838
+ } else {
839
+ return cachedResult;
840
+ }
841
+ }
842
+
843
+ const result = this._splitTextIntoLinesWithBrowser(text);
844
+
845
+ // Cache the result
846
+ (this as any)._browserWrapCache = { key: cacheKey, result };
847
+
848
+ // Mark that we used browser wrapping to prevent dynamicMinWidth modifications
849
+ (this as any)._usingBrowserWrapping = true;
850
+
851
+ return result;
852
+ }
853
+
854
+ // Clear the browser wrapping flag when using regular wrapping
855
+ (this as any)._usingBrowserWrapping = false;
856
+
857
+ // Default Fabric wrapping for other fonts
858
+ const newText = super._splitTextIntoLines(text),
859
+ graphemeLines = this._wrapText(newText.lines, this.width),
860
+ lines = new Array(graphemeLines.length);
861
+ for (let i = 0; i < graphemeLines.length; i++) {
862
+ lines[i] = graphemeLines[i].join('');
863
+ }
864
+ newText.lines = lines;
865
+ newText.graphemeLines = graphemeLines;
866
+ return newText;
867
+ }
868
+
869
+ /**
870
+ * Use browser's native text wrapping for accurate handling of fonts without English glyphs
871
+ * @private
872
+ */
873
+ _splitTextIntoLinesWithBrowser(text: string) {
874
+ if (typeof document === 'undefined') {
875
+ // Fallback to regular wrapping in Node.js
876
+ return this._splitTextIntoLinesDefault(text);
877
+ }
878
+
879
+ // Create a hidden element that mimics the overlay editor
880
+ const testElement = document.createElement('div');
881
+ testElement.style.position = 'absolute';
882
+ testElement.style.left = '-9999px';
883
+ testElement.style.visibility = 'hidden';
884
+ testElement.style.fontSize = `${this.fontSize}px`;
885
+ testElement.style.fontFamily = `"${this.fontFamily}"`;
886
+ testElement.style.fontWeight = String(this.fontWeight || 'normal');
887
+ testElement.style.fontStyle = String(this.fontStyle || 'normal');
888
+ testElement.style.lineHeight = String(this.lineHeight || 1.16);
889
+
890
+ testElement.style.width = `${this.width}px`;
891
+
892
+ testElement.style.direction = this.direction || 'ltr';
893
+ testElement.style.whiteSpace = 'pre-wrap';
894
+ testElement.style.wordBreak = 'normal';
895
+ testElement.style.overflowWrap = 'break-word';
896
+
897
+ // Set browser-native text alignment (including justify)
898
+ if (this.textAlign.includes('justify')) {
899
+ testElement.style.textAlign = 'justify';
900
+ testElement.style.textAlignLast = 'auto'; // Let browser decide last line alignment
901
+ } else {
902
+ testElement.style.textAlign = this.textAlign;
903
+ }
904
+
905
+ testElement.textContent = text;
906
+
907
+ document.body.appendChild(testElement);
908
+
909
+ // Get the browser's natural line breaks
910
+ const range = document.createRange();
911
+ const lines: string[] = [];
912
+ const graphemeLines: string[][] = [];
913
+
914
+ try {
915
+ // Simple approach: split by measuring character positions
916
+ const textNode = testElement.firstChild;
917
+ if (textNode && textNode.nodeType === Node.TEXT_NODE) {
918
+ let currentLineStart = 0;
919
+ const textLength = text.length;
920
+ let previousBottom = 0;
921
+
922
+ for (let i = 0; i <= textLength; i++) {
923
+ range.setStart(textNode, currentLineStart);
924
+ range.setEnd(textNode, i);
925
+ const rect = range.getBoundingClientRect();
926
+
927
+ if (i > currentLineStart && (rect.bottom > previousBottom + 5 || i === textLength)) {
928
+ // New line detected or end of text
929
+ const lineEnd = i === textLength ? i : i - 1;
930
+ const lineText = text.substring(currentLineStart, lineEnd).trim();
931
+ if (lineText) {
932
+ lines.push(lineText);
933
+ // Convert to graphemes for compatibility
934
+ const graphemeLine = lineText.split('');
935
+ graphemeLines.push(graphemeLine);
936
+ }
937
+ currentLineStart = lineEnd;
938
+ previousBottom = rect.bottom;
939
+ }
940
+ }
941
+ }
942
+ } catch (error) {
943
+ console.warn('Browser wrapping failed, using fallback:', error);
944
+ document.body.removeChild(testElement);
945
+ return this._splitTextIntoLinesDefault(text);
946
+ }
947
+
948
+ // Extract actual browser height BEFORE removing element
949
+ const actualBrowserHeight = testElement.scrollHeight;
950
+ const offsetHeight = testElement.offsetHeight;
951
+ const clientHeight = testElement.clientHeight;
952
+ const boundingRect = testElement.getBoundingClientRect();
953
+
954
+ console.log(`🔤 Browser element measurements:`);
955
+ console.log(` scrollHeight: ${actualBrowserHeight}px (content + padding + hidden overflow)`);
956
+ console.log(` offsetHeight: ${offsetHeight}px (content + padding + border)`);
957
+ console.log(` clientHeight: ${clientHeight}px (content + padding, no border/scrollbar)`);
958
+ console.log(` boundingRect.height: ${boundingRect.height}px (actual rendered height)`);
959
+ console.log(` Font size: ${this.fontSize}px, Line height: ${this.lineHeight || 1.16}, Lines: ${lines.length}`);
960
+
961
+ // For justify alignment, extract space measurements from browser BEFORE removing element
962
+ let justifySpaceMeasurements = null;
963
+ if (this.textAlign.includes('justify')) {
964
+ justifySpaceMeasurements = this._extractJustifySpaceMeasurements(testElement, lines);
965
+ }
966
+
967
+ document.body.removeChild(testElement);
968
+
969
+ console.log(`🔤 Browser wrapping result: ${lines.length} lines`);
970
+
971
+ // Try different height measurements to find the most accurate
972
+ let bestHeight = actualBrowserHeight;
973
+
974
+ // If scrollHeight and offsetHeight differ significantly, investigate
975
+ if (Math.abs(actualBrowserHeight - offsetHeight) > 2) {
976
+ console.log(`🔤 Height discrepancy detected: scrollHeight=${actualBrowserHeight}px vs offsetHeight=${offsetHeight}px`);
977
+ }
978
+
979
+ // Consider using boundingRect height if it's larger (sometimes more accurate for visible content)
980
+ if (boundingRect.height > bestHeight) {
981
+ console.log(`🔤 Using boundingRect height (${boundingRect.height}px) instead of scrollHeight (${bestHeight}px)`);
982
+ bestHeight = boundingRect.height;
983
+ }
984
+
985
+ // Font-specific height adjustments for accurate bounding box
986
+ let adjustedHeight = bestHeight;
987
+
988
+ // Fonts without English glyphs need additional height buffer due to different font metrics
989
+ const lacksEnglishGlyphs = fontLacksEnglishGlyphsCached(this.fontFamily);
990
+ if (lacksEnglishGlyphs) {
991
+ const glyphBuffer = this.fontSize * 0.25; // 25% of font size for non-English fonts
992
+ adjustedHeight = bestHeight + glyphBuffer;
993
+ console.log(`🔤 Non-English font detected (${this.fontFamily}): Adding ${glyphBuffer}px buffer (${bestHeight}px + ${glyphBuffer}px = ${adjustedHeight}px)`);
994
+ } else {
995
+ console.log(`🔤 Standard font (${this.fontFamily}): Using browser height directly (${bestHeight}px)`);
996
+ }
997
+
998
+ return {
999
+ _unwrappedLines: [text.split('')],
1000
+ lines: lines,
1001
+ graphemeText: text.split(''),
1002
+ graphemeLines: graphemeLines,
1003
+ justifySpaceMeasurements: justifySpaceMeasurements,
1004
+ actualBrowserHeight: adjustedHeight,
1005
+ };
1006
+ }
1007
+
1008
+
1009
+
1010
+
1011
+ /**
1012
+ * Extract justify space measurements from browser
1013
+ * @private
1014
+ */
1015
+ _extractJustifySpaceMeasurements(element: HTMLElement, lines: string[]) {
1016
+ console.log(`🔤 Extracting browser justify space measurements for ${lines.length} lines`);
1017
+
1018
+ // For now, we'll use a simplified approach:
1019
+ // Apply uniform space expansion to match the line width
1020
+ const spaceWidths: number[][] = [];
1021
+
1022
+ lines.forEach((line, lineIndex) => {
1023
+ const lineSpaces: number[] = [];
1024
+ const spaceCount = (line.match(/\s/g) || []).length;
1025
+
1026
+ if (spaceCount > 0 && lineIndex < lines.length - 1) { // Don't justify last line
1027
+ // Calculate how much space expansion is needed
1028
+ const normalSpaceWidth = 6.4; // Default space width for STV font
1029
+ const lineWidth = this.width;
1030
+
1031
+ // Estimate natural line width
1032
+ const charCount = line.length - spaceCount;
1033
+ const avgCharWidth = 12; // Approximate for STV font
1034
+ const naturalWidth = charCount * avgCharWidth + spaceCount * normalSpaceWidth;
1035
+
1036
+ // Calculate expanded space width
1037
+ const remainingSpace = lineWidth - (charCount * avgCharWidth);
1038
+ const expandedSpaceWidth = remainingSpace / spaceCount;
1039
+
1040
+ console.log(`🔤 Line ${lineIndex}: ${spaceCount} spaces, natural: ${normalSpaceWidth}px -> justified: ${expandedSpaceWidth.toFixed(1)}px`);
1041
+
1042
+ // Fill array with expanded space widths for this line
1043
+ for (let i = 0; i < spaceCount; i++) {
1044
+ lineSpaces.push(expandedSpaceWidth);
1045
+ }
1046
+ }
1047
+
1048
+ spaceWidths.push(lineSpaces);
1049
+ });
1050
+
1051
+ return spaceWidths;
1052
+ }
1053
+
1054
+ /**
1055
+ * Apply browser-calculated justify space measurements
1056
+ * @private
1057
+ */
1058
+ _applyBrowserJustifySpaces() {
1059
+ if (!this._textLines || !this.__charBounds) {
1060
+ console.warn('🔤 BROWSER JUSTIFY: _textLines or __charBounds not ready');
1061
+ return;
1062
+ }
1063
+
1064
+ // Get space measurements from browser wrapping result
1065
+ const styleMap = this._styleMap as any;
1066
+ if (!styleMap || !styleMap.justifySpaceMeasurements) {
1067
+ console.warn('🔤 BROWSER JUSTIFY: No justify space measurements available');
1068
+ return;
1069
+ }
1070
+
1071
+ const spaceWidths = styleMap.justifySpaceMeasurements as number[][];
1072
+ console.log('🔤 BROWSER JUSTIFY: Applying space measurements to __charBounds');
1073
+
1074
+ // Apply space widths to character bounds
1075
+ this._textLines.forEach((line, lineIndex) => {
1076
+ if (!this.__charBounds || !this.__charBounds[lineIndex] || !spaceWidths[lineIndex]) return;
1077
+
1078
+ const lineBounds = this.__charBounds[lineIndex];
1079
+ const lineSpaceWidths = spaceWidths[lineIndex];
1080
+ let spaceIndex = 0;
1081
+ let accumulatedSpace = 0;
1082
+ const isRtl = this.direction === 'rtl';
1083
+ const spaceDiffs = [];
1084
+
1085
+ // First, calculate the difference for each space
1086
+ for (let charIndex = 0; charIndex < line.length; charIndex++) {
1087
+ if (/\s/.test(line[charIndex]) && spaceIndex < lineSpaceWidths.length) {
1088
+ const charBound = lineBounds[charIndex];
1089
+ if (charBound) {
1090
+ spaceDiffs.push(lineSpaceWidths[spaceIndex] - charBound.width);
1091
+ }
1092
+ spaceIndex++;
1093
+ }
1094
+ }
1095
+
1096
+ spaceIndex = 0;
1097
+ let remainingDiff = spaceDiffs.reduce((a, b) => a + b, 0);
1098
+
1099
+ for (let charIndex = 0; charIndex < line.length; charIndex++) {
1100
+ const charBound = lineBounds[charIndex];
1101
+ if (!charBound) continue;
1102
+
1103
+ if (isRtl) {
1104
+ charBound.left += remainingDiff;
1105
+ } else {
1106
+ charBound.left += accumulatedSpace;
1107
+ }
1108
+
1109
+ if (/\s/.test(line[charIndex]) && spaceIndex < spaceDiffs.length) {
1110
+ const diff = spaceDiffs[spaceIndex];
1111
+ const oldWidth = charBound.width;
1112
+ charBound.width += diff;
1113
+ charBound.kernedWidth += diff;
1114
+ console.log(`🔤 Line ${lineIndex} space ${spaceIndex}: ${oldWidth.toFixed(1)}px -> ${charBound.width.toFixed(1)}px`);
1115
+
1116
+ if (isRtl) {
1117
+ remainingDiff -= diff;
1118
+ } else {
1119
+ accumulatedSpace += diff;
1120
+ }
1121
+ spaceIndex++;
1122
+ }
1123
+ }
1124
+ // also need to update the last charBound
1125
+ const lastBound = lineBounds[line.length];
1126
+ if (lastBound) {
1127
+ if (isRtl) {
1128
+ lastBound.left += remainingDiff;
1129
+ } else {
1130
+ lastBound.left += accumulatedSpace;
1131
+ }
1132
+ }
1133
+ });
1134
+ }
1135
+
1136
+ /**
1137
+ * Fallback to default Fabric wrapping
1138
+ * @private
1139
+ */
1140
+ _splitTextIntoLinesDefault(text: string) {
1141
+ const newText = super._splitTextIntoLines(text),
1142
+ graphemeLines = this._wrapText(newText.lines, this.width),
1143
+ lines = new Array(graphemeLines.length);
1144
+ for (let i = 0; i < graphemeLines.length; i++) {
1145
+ lines[i] = graphemeLines[i].join('');
1146
+ }
1147
+ newText.lines = lines;
1148
+ newText.graphemeLines = graphemeLines;
1149
+ return newText;
1150
+ }
1151
+
1152
+ getMinWidth() {
1153
+ return Math.max(this.minWidth, this.dynamicMinWidth);
1154
+ }
1155
+
1156
+ _removeExtraneousStyles() {
1157
+ const linesToKeep = new Map();
1158
+ for (const prop in this._styleMap) {
1159
+ const propNumber = parseInt(prop, 10);
1160
+ if (this._textLines[propNumber]) {
1161
+ const lineIndex = this._styleMap[prop].line;
1162
+ linesToKeep.set(`${lineIndex}`, true);
1163
+ }
1164
+ }
1165
+ for (const prop in this.styles) {
1166
+ if (!linesToKeep.has(prop)) {
1167
+ delete this.styles[prop];
1168
+ }
1169
+ }
1170
+ }
1171
+
1172
+ /**
1173
+ * Initialize event listeners for safety snap functionality
1174
+ * @private
1175
+ */
1176
+ private initializeEventListeners(): void {
1177
+ // Track which side is being used for resize to handle position compensation
1178
+ let resizeOrigin: 'left' | 'right' | null = null;
1179
+
1180
+ // Detect resize origin during resizing
1181
+ this.on('resizing', (e: any) => {
1182
+ // Check transform origin to determine which side is being resized
1183
+ if (e.transform) {
1184
+ const { originX } = e.transform;
1185
+ // originX tells us which side is the anchor - opposite side is being dragged
1186
+ resizeOrigin = originX === 'right' ? 'left' : originX === 'left' ? 'right' : null;
1187
+ } else if (e.originX) {
1188
+ const { originX } = e;
1189
+ resizeOrigin = originX === 'right' ? 'left' : originX === 'left' ? 'right' : null;
1190
+ }
1191
+ });
1192
+
1193
+ // Only trigger safety snap after resize is complete (not during)
1194
+ // Use 'modified' event which fires after user releases the mouse
1195
+ this.on('modified', () => {
1196
+ const currentResizeOrigin = resizeOrigin; // Capture the value before reset
1197
+ // Small delay to ensure text layout is updated
1198
+ setTimeout(() => this.safetySnapWidth(currentResizeOrigin), 10);
1199
+ resizeOrigin = null; // Reset after capturing
1200
+ });
1201
+
1202
+ // Also listen to canvas-level modified event as backup
1203
+ this.canvas?.on('object:modified', (e) => {
1204
+ if (e.target === this) {
1205
+ const currentResizeOrigin = resizeOrigin; // Capture the value before reset
1206
+ setTimeout(() => this.safetySnapWidth(currentResizeOrigin), 10);
1207
+ resizeOrigin = null; // Reset after capturing
1208
+ }
1209
+ });
1210
+ }
1211
+
1212
+ /**
1213
+ * Safety snap to prevent glyph clipping after manual resize.
1214
+ * Similar to Polotno - checks if any glyphs are too close to edges
1215
+ * and automatically expands width if needed.
1216
+ * @private
1217
+ * @param resizeOrigin - Which side was used for resizing ('left' or 'right')
1218
+ */
1219
+ private safetySnapWidth(resizeOrigin?: 'left' | 'right' | null): void {
1220
+ // For Textbox objects, we always want to check for clipping regardless of isWrapping flag
1221
+ if (!this._textLines || this.type.toLowerCase() !== 'textbox' || this._textLines.length === 0) {
1222
+ return;
1223
+ }
1224
+
1225
+ const lineCount = this._textLines.length;
1226
+ if (lineCount === 0) return;
1227
+
1228
+ // Check all lines, not just the last one
1229
+ let maxActualLineWidth = 0; // Actual measured width without buffers
1230
+ let maxRequiredWidth = 0; // Width including RTL buffer
1231
+
1232
+ for (let i = 0; i < lineCount; i++) {
1233
+ const lineText = this._textLines[i].join(''); // Convert grapheme array to string
1234
+ const lineWidth = this.getLineWidth(i);
1235
+ maxActualLineWidth = Math.max(maxActualLineWidth, lineWidth);
1236
+
1237
+ // RTL detection - regex for Arabic, Hebrew, and other RTL characters
1238
+ const rtlRegex = /[\u0590-\u05FF\u0600-\u06FF\u0750-\u077F\uFB50-\uFDFF\uFE70-\uFEFF]/;
1239
+ if (rtlRegex.test(lineText)) {
1240
+ // Add minimal RTL compensation buffer - just enough to prevent clipping
1241
+ const rtlBuffer = (this.fontSize || 16) * 0.15; // 15% of font size (much smaller)
1242
+ maxRequiredWidth = Math.max(maxRequiredWidth, lineWidth + rtlBuffer);
1243
+ } else {
1244
+ maxRequiredWidth = Math.max(maxRequiredWidth, lineWidth);
1245
+ }
1246
+ }
1247
+
1248
+ // Safety margin - how close glyphs can get before we snap
1249
+ const safetyThreshold = 2; // px - very subtle trigger
1250
+
1251
+ if (maxRequiredWidth > this.width - safetyThreshold) {
1252
+ // Set width to exactly what's needed + minimal safety margin
1253
+ const newWidth = maxRequiredWidth + 1; // Add just 1px safety margin
1254
+
1255
+ // Store original position before width change
1256
+ const originalLeft = this.left;
1257
+ const originalTop = this.top;
1258
+ const widthIncrease = newWidth - this.width;
1259
+
1260
+ // Change width
1261
+ this.set('width', newWidth);
1262
+
1263
+ // Force text layout recalculation
1264
+ this.initDimensions();
1265
+
1266
+ // Only compensate position when resizing from left handle
1267
+ // Right handle resize doesn't shift the text position
1268
+ if (resizeOrigin === 'left') {
1269
+ // When resizing from left, the expansion pushes text right
1270
+ // Compensate by moving the textbox left by the width increase
1271
+ this.set({
1272
+ 'left': originalLeft - widthIncrease,
1273
+ 'top': originalTop
1274
+ });
1275
+ }
1276
+
1277
+ this.setCoords();
1278
+
1279
+ // Also refresh the overlay editor if it exists
1280
+ if ((this as any).__overlayEditor) {
1281
+ setTimeout(() => {
1282
+ (this as any).__overlayEditor.refresh();
1283
+ }, 0);
1284
+ }
1285
+
1286
+ this.canvas?.requestRenderAll();
1287
+ }
1288
+ }
1289
+
1290
+ /**
1291
+ * Fix character selection mismatch after JSON loading for browser-wrapped fonts
1292
+ * @private
1293
+ */
1294
+ _fixCharacterMappingAfterJsonLoad(): void {
1295
+ if ((this as any)._usingBrowserWrapping) {
1296
+ // Clear all cached states to force fresh text layout calculation
1297
+ (this as any)._browserWrapCache = null;
1298
+ (this as any)._lastDimensionState = null;
1299
+
1300
+ // Force complete re-initialization
1301
+ this.initDimensions();
1302
+ this._forceClearCache = true;
1303
+
1304
+ // Ensure canvas refresh
1305
+ this.setCoords();
1306
+ if (this.canvas) {
1307
+ this.canvas.requestRenderAll();
1308
+ }
1309
+ }
1310
+ }
1311
+
1312
+ /**
1313
+ * Force complete textbox re-initialization (useful after JSON loading)
1314
+ * Overrides Text version with Textbox-specific logic
1315
+ */
1316
+ forceTextReinitialization(): void {
1317
+ console.log('🔄 Force reinitializing Textbox object');
1318
+
1319
+ // CRITICAL: Ensure textbox is marked as initialized
1320
+ this.initialized = true;
1321
+
1322
+ // Clear all caches and force dirty state
1323
+ this._clearCache();
1324
+ this.dirty = true;
1325
+ this.dynamicMinWidth = 0;
1326
+
1327
+ // Force isEditing false to ensure clean state
1328
+ this.isEditing = false;
1329
+
1330
+ console.log(' → Set initialized=true, dirty=true, cleared caches');
1331
+
1332
+ // Re-initialize dimensions (this will handle justify properly)
1333
+ this.initDimensions();
1334
+
1335
+ // Double-check that justify was applied by checking space widths
1336
+ if (this.textAlign.includes('justify') && this.__charBounds) {
1337
+ setTimeout(() => {
1338
+ // Verify justify was applied by checking if space widths vary
1339
+ let hasVariableSpaces = false;
1340
+ this.__charBounds.forEach((lineBounds, i) => {
1341
+ if (lineBounds && this._textLines && this._textLines[i]) {
1342
+ const spaces = lineBounds.filter((bound, j) => /\s/.test(this._textLines[i][j]));
1343
+ if (spaces.length > 1) {
1344
+ const firstSpaceWidth = spaces[0].width;
1345
+ hasVariableSpaces = spaces.some(space => Math.abs(space.width - firstSpaceWidth) > 0.1);
1346
+ }
1347
+ }
1348
+ });
1349
+
1350
+ if (!hasVariableSpaces && this.__charBounds.length > 0) {
1351
+ console.warn(' ⚠️ Justify spaces still uniform - forcing enlargeSpaces again');
1352
+ if (this.enlargeSpaces) {
1353
+ this.enlargeSpaces();
1354
+ }
1355
+ } else {
1356
+ console.log(' ✅ Justify spaces properly expanded');
1357
+ }
1358
+
1359
+ // Ensure height is recalculated - use browser height if available
1360
+ if ((this as any)._usingBrowserWrapping && (this as any)._actualBrowserHeight) {
1361
+ this.height = (this as any)._actualBrowserHeight;
1362
+ console.log(`🔤 JUSTIFY: Preserved browser height: ${this.height}px`);
1363
+ } else {
1364
+ this.height = this.calcTextHeight();
1365
+ console.log(`🔧 JUSTIFY: Used calcTextHeight: ${this.height}px`);
1366
+ }
1367
+ this.canvas?.requestRenderAll();
1368
+ }, 10);
1369
+ }
1370
+ }
1371
+
1372
+ /**
1373
+ * Returns object representation of an instance
1374
+ * @param {Array} [propertiesToInclude] Any properties that you might want to additionally include in the output
1375
+ * @return {Object} object representation of an instance
1376
+ */
1377
+ toObject<
1378
+ T extends Omit<Props & TClassProperties<this>, keyof SProps>,
1379
+ K extends keyof T = never,
1380
+ >(propertiesToInclude: K[] = []): Pick<T, K> & SProps {
1381
+ return super.toObject<T, K>([
1382
+ 'minWidth',
1383
+ 'splitByGrapheme',
1384
+ ...propertiesToInclude,
1385
+ ] as K[]);
1386
+ }
1387
+ }
1388
+
1389
+ classRegistry.setClass(Textbox);