terminal-png 0.1.6 → 0.1.8

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,4 +1,5 @@
1
1
  import { FONT_FACE_CSS } from "./font.js";
2
+ const segmenter = new Intl.Segmenter("en", { granularity: "grapheme" });
2
3
  const ANSI_16_PALETTE = [
3
4
  "#282a2e",
4
5
  "#D74E6F",
@@ -117,7 +118,6 @@ function measureLines(lines) {
117
118
  return Math.max(...lines.map((line) => displayWidth(line.map((run) => run.text).join("")) * CHARACTER_WIDTH), 0);
118
119
  }
119
120
  function displayWidth(text, startColumn = 0) {
120
- const segmenter = new Intl.Segmenter();
121
121
  let column = startColumn;
122
122
  for (const { segment } of segmenter.segment(text)) {
123
123
  if (segment === "\t") {
@@ -165,13 +165,38 @@ function renderLines(lines, textStartX, textStartY, lineHeightPx) {
165
165
  return lines
166
166
  .map((line, index) => {
167
167
  const y = formatNumber(textStartY + (index * lineHeightPx));
168
+ let column = 0;
168
169
  if (line.length === 0) {
169
170
  return `<text x="${formatNumber(textStartX)}" y="${y}" xml:space="preserve"/>`;
170
171
  }
171
172
  return [
172
173
  renderBackgrounds(line, textStartX, textStartY + (index * lineHeightPx) - lineHeightPx, lineHeightPx),
173
174
  `<text x="${formatNumber(textStartX)}" y="${y}" xml:space="preserve">`,
174
- line.map(renderRun).join(""),
175
+ line.flatMap((run) => {
176
+ const spans = [];
177
+ let text = "";
178
+ let start = column;
179
+ for (const { segment } of segmenter.segment(run.text)) {
180
+ const width = displayWidth(segment, column);
181
+ if (width !== 1 || Array.from(segment).length !== 1) {
182
+ if (text.length > 0)
183
+ spans.push(renderRun({ ...run, text }, textStartX + start * CHARACTER_WIDTH));
184
+ text = "";
185
+ if (segment !== "\t" && width > 0) {
186
+ spans.push(renderRun({ ...run, text: segment }, textStartX + column * CHARACTER_WIDTH));
187
+ }
188
+ column += width;
189
+ start = column;
190
+ }
191
+ else {
192
+ text += segment;
193
+ column += 1;
194
+ }
195
+ }
196
+ if (text.length > 0)
197
+ spans.push(renderRun({ ...run, text }, textStartX + start * CHARACTER_WIDTH));
198
+ return spans;
199
+ }).join(""),
175
200
  "</text>"
176
201
  ].join("");
177
202
  })
@@ -190,7 +215,7 @@ function renderBackgrounds(line, startX, y, height) {
190
215
  }
191
216
  return rectangles.join("");
192
217
  }
193
- function renderRun(run) {
218
+ function renderRun(run, x) {
194
219
  const attributes = ['xml:space="preserve"'];
195
220
  const color = resolveForegroundColor(run);
196
221
  const textDecorations = [];
@@ -216,6 +241,8 @@ function renderRun(run) {
216
241
  attributes.push('opacity="0.7"');
217
242
  }
218
243
  const text = run.conceal ? " ".repeat(displayWidth(run.text)) : run.text;
244
+ const positions = [...segmenter.segment(run.text)].map((_, index) => formatNumber(x + index * CHARACTER_WIDTH));
245
+ attributes.push(`x="${positions.join(" ")}"`);
219
246
  return `<tspan ${attributes.join(" ")}>${escapeXmlText(text)}</tspan>`;
220
247
  }
221
248
  function renderWindowControls() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "terminal-png",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "license": "MIT",
5
5
  "description": "Render PNG images from ANSI terminal output.",
6
6
  "type": "module",