@xynogen/pix-display 0.4.3 → 0.4.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xynogen/pix-display",
3
- "version": "0.4.3",
3
+ "version": "0.4.4",
4
4
  "description": "Pi core extension — inline chips, thinking blocks, and framed code rendering",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
package/src/image.ts CHANGED
@@ -7,9 +7,14 @@
7
7
  */
8
8
  import { basename } from "node:path";
9
9
  import { FG_DIM, RST } from "@xynogen/pix-pretty/ansi";
10
- import { getCapabilities, getImageDimensions, Image } from "@xynogen/pix-pretty/tui";
10
+ import {
11
+ getCapabilities,
12
+ getCellDimensions,
13
+ getImageDimensions,
14
+ Image,
15
+ } from "@xynogen/pix-pretty/tui";
11
16
  import { readFileSync, statSync } from "@xynogen/pix-runtime/fs";
12
- import { localFilePath } from "@xynogen/pix-runtime/paths";
17
+ import { localFilePath, requireFrom } from "@xynogen/pix-runtime/paths";
13
18
 
14
19
  const IMAGE = /^::image\[([^\]\n]*)\](?:\{([^}\n]*)\})?\s*$/;
15
20
  // Read synchronously during render, so bound the bytes one directive can load.
@@ -83,6 +88,7 @@ export function renderImage(image: ImageDirective, width: number): string[] {
83
88
  }
84
89
 
85
90
  type RowOptions = { maxWidthCells: number; maxHeightCells?: number; filename?: string };
91
+ type Dimensions = { widthPx: number; heightPx: number };
86
92
 
87
93
  /** Image rows through Pi's `Image`; blank rows become NBSP so Markdown keeps the height. */
88
94
  export function renderImageRows(
@@ -91,12 +97,84 @@ export function renderImageRows(
91
97
  width: number,
92
98
  options: RowOptions,
93
99
  ): string[] {
94
- const dims = getImageDimensions(data, mime) ?? undefined;
95
- return new Image(data, mime, { fallbackColor: (s) => `${FG_DIM}${s}${RST}` }, options, dims)
100
+ const source = getImageDimensions(data, mime) ?? undefined;
101
+ const png =
102
+ source && getCapabilities().images ? fitToCells(data, source, width, options) : undefined;
103
+ const [shown, type] = png ? [png, "image/png"] : [data, mime];
104
+ const dims = getImageDimensions(shown, type) ?? undefined;
105
+ return new Image(shown, type, { fallbackColor: (s) => `${FG_DIM}${s}${RST}` }, options, dims)
96
106
  .render(width)
97
107
  .map((row) => row || NBSP);
98
108
  }
99
109
 
110
+ type PhotonImage = { get_bytes(): Uint8Array; free(): void };
111
+ type Photon = {
112
+ PhotonImage: { new_from_byteslice(bytes: Uint8Array): PhotonImage };
113
+ resize(image: PhotonImage, width: number, height: number, filter: number): PhotonImage;
114
+ };
115
+ // photon's SamplingFilter.Lanczos3, the filter Pi's own image resize uses.
116
+ const LANCZOS3 = 5;
117
+ let photon: Photon | null | undefined;
118
+
119
+ /** Pi's photon-node (a Pi dependency, not ours): the running CLI's copy, else the peer install's. */
120
+ function loadPhoton(): Photon | null {
121
+ if (photon !== undefined) return photon;
122
+ photon = null;
123
+ const hosts = [
124
+ () => process.argv[1] ?? "",
125
+ // ESM-only package (no `require` export): resolve it the ESM way.
126
+ () => import.meta.resolve("@earendil-works/pi-coding-agent"),
127
+ ];
128
+ for (const host of hosts) {
129
+ try {
130
+ // SAFETY: photon-node is CommonJS with this API; Pi's utils/photon.ts makes the same calls.
131
+ photon = requireFrom(host())("@silvia-odwyer/photon-node") as Photon;
132
+ break;
133
+ } catch {
134
+ // Not resolvable from this host: try the next one.
135
+ }
136
+ }
137
+ return photon;
138
+ }
139
+
140
+ /**
141
+ * The image as a PNG no larger than the pixels its cells show, or undefined when it
142
+ * already fits or cannot be resized. A terminal shows no more than that anyway, and
143
+ * herdr drops a Kitty image whose decoded RGBA exceeds its transfer budget (~30 MB).
144
+ * The box mirrors Pi's `Image`: `width - 2` columns, square-ish rows by default. One
145
+ * extra cell each way absorbs callers' rounding, so a raster sized to its cells stays.
146
+ * ponytail: synchronous (render is sync) and no EXIF rotation; a huge photo costs ~1 s
147
+ * once per cache entry. Upgrade path: Pi's async `resizeImage` plus a transcript redraw.
148
+ */
149
+ function fitToCells(data: string, source: Dimensions, width: number, options: RowOptions) {
150
+ const cell = getCellDimensions();
151
+ const cols = Math.max(1, Math.min(width - 2, options.maxWidthCells));
152
+ const rows = options.maxHeightCells ?? Math.ceil((cols * cell.widthPx) / cell.heightPx);
153
+ if (source.widthPx <= (cols + 1) * cell.widthPx && source.heightPx <= (rows + 1) * cell.heightPx)
154
+ return undefined;
155
+ const scale = Math.min(
156
+ (cols * cell.widthPx) / source.widthPx,
157
+ (rows * cell.heightPx) / source.heightPx,
158
+ );
159
+ const lib = loadPhoton();
160
+ if (!lib) return undefined;
161
+ let image: PhotonImage | undefined;
162
+ let out: PhotonImage | undefined;
163
+ try {
164
+ image = lib.PhotonImage.new_from_byteslice(Buffer.from(data, "base64"));
165
+ const w = Math.max(1, Math.round(source.widthPx * scale));
166
+ const h = Math.max(1, Math.round(source.heightPx * scale));
167
+ out = lib.resize(image, w, h, LANCZOS3);
168
+ return Buffer.from(out.get_bytes()).toString("base64");
169
+ } catch {
170
+ // Undecodable source: show it unchanged and let Pi's Image fall back.
171
+ return undefined;
172
+ } finally {
173
+ out?.free();
174
+ image?.free();
175
+ }
176
+ }
177
+
100
178
  // Space between an image and its frame: columns left/right, rows above/below.
101
179
  const PAD_X = 2;
102
180
  const PAD_Y = 1;
package/src/math.ts CHANGED
@@ -194,8 +194,9 @@ export function renderMath(tex: string, width: number): string[] | undefined {
194
194
  const rows = Math.max(1, Math.min(MAX_ROWS, Math.round(math.heightEx * ROWS_PER_EX)));
195
195
  const cols = Math.ceil((rows * cell.heightPx * math.widthEx) / math.heightEx / cell.widthPx);
196
196
  if (cols > width) return undefined;
197
- // Rasterise at 2× the displayed height so the glyphs stay sharp.
198
- const png = engine.rasterise(math.svg, rows * cell.heightPx * 2);
197
+ // Rasterise at the displayed size: cell pixels are the terminal's own resolution,
198
+ // and renderImageRows would shrink anything larger back down.
199
+ const png = engine.rasterise(math.svg, rows * cell.heightPx);
199
200
  const image = renderImageRows(Buffer.from(png).toString("base64"), "image/png", width, {
200
201
  maxWidthCells: cols,
201
202
  maxHeightCells: rows,
package/src/mermaid.ts CHANGED
@@ -280,10 +280,11 @@ export function renderMermaidImage(source: string, width: number): string[] | un
280
280
  const shrink = Math.min(1, (width - FRAME_COLS) / cols, MAX_ROWS / rows);
281
281
  cols = Math.max(1, Math.floor(cols * shrink));
282
282
  rows = Math.max(1, Math.floor(rows * shrink));
283
- // Rasterise at 2× the displayed height so text and lines stay sharp.
283
+ // Rasterise at the displayed size: cell pixels are the terminal's own resolution,
284
+ // and renderImageRows would shrink anything larger back down.
284
285
  let png: Uint8Array;
285
286
  try {
286
- png = engine.rasterise(svg, rows * cell.heightPx * 2);
287
+ png = engine.rasterise(svg, rows * cell.heightPx);
287
288
  } catch {
288
289
  return undefined;
289
290
  }