@angadie/chittie-text 0.1.0 → 0.2.0

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/dist/index.d.mts CHANGED
@@ -10,6 +10,8 @@ type Codepage = Parameters<typeof CodepageEncoder.encode>[1];
10
10
  * must be printed as a raster image (see VENDOR research: non-latin-printing).
11
11
  */
12
12
  declare function needsRaster(text: string, codepage?: Codepage): boolean;
13
+ /** Fold common typographic punctuation to ASCII so receipts don't choke on a code page (× → x, … → ...). */
14
+ declare function foldTypographic(text: string): string;
13
15
  /** Options handed to the injected rasterizer. */
14
16
  interface RasterOptions {
15
17
  fontSize?: number;
@@ -48,7 +50,7 @@ interface SmartTextOptions {
48
50
  * rasterized image when it isn't (Sinhala/Tamil/…). If a raster is needed but no
49
51
  * rasterizer was supplied, it throws a clear error instead of silently printing "?".
50
52
  */
51
- declare function smartText(encoder: EncoderLike, text: string, options?: SmartTextOptions): void;
53
+ declare function smartText(encoder: EncoderLike, raw: string, options?: SmartTextOptions): void;
52
54
  /**
53
55
  * Render a two-column row (left flush-left, right flush-right) to one
54
56
  * printer-width image — the correct way to print non-Latin text inside a
@@ -59,4 +61,4 @@ declare function rasterizeRow(rasterizer: TextRasterizer, left: string, right: s
59
61
  dotWidth: number;
60
62
  } & RasterOptions): ImageData;
61
63
  //#endregion
62
- export { Codepage, RasterOptions, SmartTextOptions, TextRasterizer, needsRaster, padTo8, rasterizeRow, smartText };
64
+ export { Codepage, RasterOptions, SmartTextOptions, TextRasterizer, foldTypographic, needsRaster, padTo8, rasterizeRow, smartText };
package/dist/index.mjs CHANGED
@@ -14,6 +14,28 @@ function needsRaster(text, codepage = "cp437") {
14
14
  }
15
15
  return false;
16
16
  }
17
+ const TYPOGRAPHIC = {
18
+ "×": "x",
19
+ "–": "-",
20
+ "—": "-",
21
+ "−": "-",
22
+ "‘": "'",
23
+ "’": "'",
24
+ "′": "'",
25
+ "“": "\"",
26
+ "”": "\"",
27
+ "″": "\"",
28
+ "…": "...",
29
+ "•": "*",
30
+ "→": "->",
31
+ "\xA0": " "
32
+ };
33
+ /** Fold common typographic punctuation to ASCII so receipts don't choke on a code page (× → x, … → ...). */
34
+ function foldTypographic(text) {
35
+ let out = "";
36
+ for (const ch of text) out += TYPOGRAPHIC[ch] ?? ch;
37
+ return out;
38
+ }
17
39
  /**
18
40
  * ESC/POS raster requires dimensions that are multiples of 8. Pad right/bottom
19
41
  * with white so any image "just works". Platform-neutral: reuses the input's own
@@ -35,8 +57,10 @@ function padTo8(img) {
35
57
  * rasterized image when it isn't (Sinhala/Tamil/…). If a raster is needed but no
36
58
  * rasterizer was supplied, it throws a clear error instead of silently printing "?".
37
59
  */
38
- function smartText(encoder, text, options = {}) {
39
- if (!needsRaster(text, options.codepage ?? "cp437")) {
60
+ function smartText(encoder, raw, options = {}) {
61
+ const codepage = options.codepage ?? "cp437";
62
+ const text = foldTypographic(raw);
63
+ if (!needsRaster(text, codepage)) {
40
64
  encoder.text(text);
41
65
  return;
42
66
  }
@@ -79,4 +103,4 @@ function blankImage(w, h) {
79
103
  return new Ctor(new Uint8ClampedArray(w * h * 4).fill(255), w, h);
80
104
  }
81
105
  //#endregion
82
- export { needsRaster, padTo8, rasterizeRow, smartText };
106
+ export { foldTypographic, needsRaster, padTo8, rasterizeRow, smartText };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@angadie/chittie-text",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Smart text for chittie: print code-page text when representable, auto-rasterize complex scripts (Sinhala/Tamil/…) via an injected rasterizer, never silently print '?'. Platform-neutral.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -15,11 +15,11 @@
15
15
  "dist"
16
16
  ],
17
17
  "dependencies": {
18
- "@angadie/chittie-codepage": "0.1.0"
18
+ "@angadie/chittie-codepage": "0.2.0"
19
19
  },
20
20
  "devDependencies": {
21
21
  "@canvas/image-data": "^1.1.0",
22
- "@angadie/chittie-core": "0.1.0"
22
+ "@angadie/chittie-core": "0.2.0"
23
23
  },
24
24
  "publishConfig": {
25
25
  "access": "public"