MCPcopy Index your code
hub / github.com/midrender/revideo / measureString

Function measureString

packages/2d/src/lib/code/CodeMetrics.ts:10–43  ·  view source on GitHub ↗
(
  context: CanvasRenderingContext2D,
  monoWidth: number,
  value: string,
)

Source from the content-addressed store, hash-verified

8}
9
10export function measureString(
11 context: CanvasRenderingContext2D,
12 monoWidth: number,
13 value: string,
14): CodeMetrics {
15 const lines = value.split('\n');
16 const lastLine = lines[lines.length - 1];
17 const firstWidth = Math.round(
18 context.measureText(lines[0]).width / monoWidth,
19 );
20 let lastWidth = firstWidth;
21 let maxWidth = firstWidth;
22
23 for (let i = 1; i < lines.length; i++) {
24 const line = lines[i];
25 const width = Math.round(context.measureText(line).width / monoWidth);
26 if (width > maxWidth) {
27 maxWidth = width;
28 }
29 }
30
31 if (lines.length > 0) {
32 lastWidth = Math.round(context.measureText(lastLine).width / monoWidth);
33 }
34
35 return {
36 content: value,
37 newRows: lines.length - 1,
38 endColumn: lastLine.length,
39 firstWidth,
40 maxWidth,
41 lastWidth,
42 };
43}
44
45export function isCodeMetrics(value: any): value is CodeMetrics {
46 return value?.content !== undefined;

Callers 1

parseCodeFragmentFunction · 0.90

Calls 1

splitMethod · 0.45

Tested by

no test coverage detected