@hirokisakabe/pom 0.1.3 → 0.1.4

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 +1 @@
1
- {"version":3,"file":"measureText.d.ts","sourceRoot":"","sources":["../../src/calcYogaLayout/measureText.ts"],"names":[],"mappings":"AAEA,KAAK,cAAc,GAAG;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,CAAC,EAAE,QAAQ,GAAG,MAAM,GAAG,MAAM,CAAC;IACxC,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AAKF;;GAEG;AACH,wBAAgB,WAAW,CACzB,IAAI,EAAE,MAAM,EACZ,UAAU,EAAE,MAAM,EAClB,IAAI,EAAE,cAAc,GACnB;IACD,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;CAClB,CAoCA"}
1
+ {"version":3,"file":"measureText.d.ts","sourceRoot":"","sources":["../../src/calcYogaLayout/measureText.ts"],"names":[],"mappings":"AAEA,KAAK,cAAc,GAAG;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,CAAC,EAAE,QAAQ,GAAG,MAAM,GAAG,MAAM,CAAC;IACxC,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AAKF;;GAEG;AACH,wBAAgB,WAAW,CACzB,IAAI,EAAE,MAAM,EACZ,UAAU,EAAE,MAAM,EAClB,IAAI,EAAE,cAAc,GACnB;IACD,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;CAClB,CA+CA"}
@@ -6,28 +6,37 @@ const ctx = canvas.getContext("2d");
6
6
  */
7
7
  export function measureText(text, maxWidthPx, opts) {
8
8
  applyFontStyle(opts);
9
- const words = splitForWrap(text);
9
+ // まず改行で段落に分割
10
+ const paragraphs = text.split("\n");
10
11
  const lines = [];
11
- let current = "";
12
- let currentWidth = 0;
13
- for (const word of words) {
14
- const candidate = current ? current + word : word;
15
- const w = ctx.measureText(candidate).width;
16
- if (w <= maxWidthPx || !current) {
17
- // まだ詰められる
18
- current = candidate;
19
- currentWidth = w;
12
+ for (const paragraph of paragraphs) {
13
+ // 空の段落(連続した改行)も1行としてカウント
14
+ if (paragraph === "") {
15
+ lines.push({ widthPx: 0 });
16
+ continue;
20
17
  }
21
- else {
22
- // 折り返す
18
+ const words = splitForWrap(paragraph);
19
+ let current = "";
20
+ let currentWidth = 0;
21
+ for (const word of words) {
22
+ const candidate = current ? current + word : word;
23
+ const w = ctx.measureText(candidate).width;
24
+ if (w <= maxWidthPx || !current) {
25
+ // まだ詰められる
26
+ current = candidate;
27
+ currentWidth = w;
28
+ }
29
+ else {
30
+ // 折り返す
31
+ lines.push({ widthPx: currentWidth });
32
+ current = word;
33
+ currentWidth = ctx.measureText(word).width;
34
+ }
35
+ }
36
+ if (current) {
23
37
  lines.push({ widthPx: currentWidth });
24
- current = word;
25
- currentWidth = ctx.measureText(word).width;
26
38
  }
27
39
  }
28
- if (current) {
29
- lines.push({ widthPx: currentWidth });
30
- }
31
40
  const lineHeightRatio = opts.lineHeight ?? 1.3;
32
41
  const lineHeightPx = opts.fontSizePx * lineHeightRatio;
33
42
  const widthPx = lines.length ? Math.max(...lines.map((l) => l.widthPx)) : 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hirokisakabe/pom",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "PowerPoint Object Model - A declarative TypeScript library for creating PowerPoint presentations",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",