Skip to content

Commit 2271109

Browse files
committed
refactor(masonry): [brick] seperate text measurement utility
Signed-off-by: karan-palan <[email protected]>
1 parent 6461ec7 commit 2271109

File tree

2 files changed

+53
-22
lines changed

2 files changed

+53
-22
lines changed

modules/masonry/src/brick/model/model.ts

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,7 @@ import type {
1515
import type { TConnectionPoints as TCP } from '../../tree/model/model';
1616
import { generateBrickData } from '../utils/path';
1717
import type { TInputUnion } from '../utils/path';
18-
19-
// Text measurement utility
20-
function measureTextWidth(text: string, fontSize: number = 16): number {
21-
// Create a canvas element for text measurement
22-
const canvas = document.createElement('canvas');
23-
const context = canvas.getContext('2d')!;
24-
context.font = `${fontSize}px sans-serif`;
25-
return context.measureText(text).width;
26-
}
27-
28-
// Fallback for server-side rendering or when canvas is not available
29-
function estimateTextWidth(text: string): number {
30-
return Math.max(text.length * 8, 40); // Minimum width of 40
31-
}
32-
33-
function getLabelWidth(label: string): number {
34-
try {
35-
return measureTextWidth(label, 16) + 8; // Add 8px padding
36-
} catch {
37-
return estimateTextWidth(label);
38-
}
39-
}
18+
import { getLabelWidth } from '../utils/textMeasurement';
4019

4120
export abstract class BrickModel implements IBrick {
4221
protected _uuid: string;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Text measurement utilities for brick rendering
3+
*/
4+
5+
/**
6+
* Measure text width and height using canvas API
7+
*/
8+
export function measureTextWidth(text: string, fontSize: number = 16): number {
9+
// Create a canvas element for text measurement
10+
const canvas = document.createElement('canvas');
11+
const context = canvas.getContext('2d')!;
12+
context.font = `${fontSize}px sans-serif`;
13+
return context.measureText(text).width;
14+
}
15+
16+
/**
17+
* Fallback for server-side rendering or when canvas is not available
18+
*/
19+
export function estimateTextWidth(text: string): number {
20+
return Math.max(text.length * 8, 40); // Minimum width of 40
21+
}
22+
23+
/**
24+
* Get label width with padding, with fallback for SSR
25+
*/
26+
export function getLabelWidth(label: string): number {
27+
try {
28+
return measureTextWidth(label, 16) + 8;
29+
} catch {
30+
return estimateTextWidth(label);
31+
}
32+
}
33+
34+
/**
35+
* Comprehensive label measurement including width, height, ascent, and descent
36+
*/
37+
export function measureLabel(label: string, fontSize: number) {
38+
const canvas = document.createElement('canvas');
39+
const ctx = canvas.getContext('2d')!;
40+
ctx.font = `${fontSize}px sans-serif`;
41+
const m = ctx.measureText(label);
42+
const ascent = m.actualBoundingBoxAscent ?? fontSize * 0.8;
43+
const descent = m.actualBoundingBoxDescent ?? fontSize * 0.2;
44+
const height = ascent + descent;
45+
46+
return {
47+
w: m.width + 8,
48+
h: height,
49+
ascent,
50+
descent,
51+
};
52+
}

0 commit comments

Comments
 (0)