label-printer 0.4.10 → 0.5.2

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.mjs CHANGED
@@ -418,7 +418,6 @@ var ImageProcessor = class {
418
418
  */
419
419
  static getImageDataBrowser(image2) {
420
420
  return __async(this, null, function* () {
421
- console.log("Processing image in browser environment");
422
421
  return new Promise((resolve, reject) => {
423
422
  const img = new Image();
424
423
  img.crossOrigin = "anonymous";
@@ -1414,6 +1413,9 @@ var UsbDevice = class {
1414
1413
  writeData(data) {
1415
1414
  return __async(this, null, function* () {
1416
1415
  const endpointNumber = this.outEndpoint;
1416
+ if (endpointNumber == null) {
1417
+ throw new Error("usb-no-out-endpoint");
1418
+ }
1417
1419
  yield this.device.transferOut(endpointNumber, data);
1418
1420
  });
1419
1421
  }
@@ -1435,6 +1437,9 @@ var UsbDevice = class {
1435
1437
  readData(length) {
1436
1438
  return __async(this, null, function* () {
1437
1439
  const endpointNumber = this.inEndpoint;
1440
+ if (endpointNumber == null) {
1441
+ throw new Error("usb-no-in-endpoint");
1442
+ }
1438
1443
  const result = yield this.device.transferIn(endpointNumber, length);
1439
1444
  if (result.status == "ok" && result.data) {
1440
1445
  return result.data;
@@ -1490,8 +1495,12 @@ var PrinterService = class _PrinterService {
1490
1495
  return __async(this, null, function* () {
1491
1496
  const classes = [TSPLPrinter];
1492
1497
  for (const key in classes) {
1493
- if (yield classes[key].try(device)) {
1494
- return new classes[key](device);
1498
+ try {
1499
+ if (yield classes[key].try(device)) {
1500
+ return new classes[key](device);
1501
+ }
1502
+ } catch (_e) {
1503
+ return void 0;
1495
1504
  }
1496
1505
  }
1497
1506
  return void 0;
@@ -1533,6 +1542,7 @@ __export(labels_exports, {
1533
1542
  LabelField: () => LabelField,
1534
1543
  Line: () => Line,
1535
1544
  QRCode: () => QRCode,
1545
+ Table: () => Table,
1536
1546
  Text: () => Text
1537
1547
  });
1538
1548
 
@@ -1586,11 +1596,16 @@ var Label = class extends Printable {
1586
1596
  return {
1587
1597
  dpi: this.dpi,
1588
1598
  textWidth: (text, font) => {
1589
- const size = dotToPoint(font.size, this.dpi);
1590
- const fontObject = this.getIndexedFont(font).font;
1591
- const run = fontObject.layout(text);
1592
- const scaledWidth = size * run.advanceWidth / fontObject.unitsPerEm;
1593
- return pointsToDots(scaledWidth, this.dpi);
1599
+ const indexedFont = this.getIndexedFont(font);
1600
+ if (indexedFont == null) {
1601
+ return text.length * font.size;
1602
+ } else {
1603
+ const size = dotToPoint(font.size, this.dpi);
1604
+ const fontObject = indexedFont.font;
1605
+ const run = fontObject.layout(text);
1606
+ const scaledWidth = size * run.advanceWidth / fontObject.unitsPerEm;
1607
+ return pointsToDots(scaledWidth, this.dpi);
1608
+ }
1594
1609
  },
1595
1610
  getFontName: this.getFontName.bind(this)
1596
1611
  };
@@ -1664,8 +1679,7 @@ var Label = class extends Printable {
1664
1679
  const generator = this.commandGeneratorFor(language);
1665
1680
  const commands = yield this.fullCommand(language, 0, direction, mirror, 0, generator);
1666
1681
  commands.push(generator.display());
1667
- const group = generator.commandGroup(commands);
1668
- return group;
1682
+ return generator.commandGroup(commands);
1669
1683
  });
1670
1684
  }
1671
1685
  /**
@@ -1697,6 +1711,7 @@ var Label = class extends Printable {
1697
1711
  getIndexedFont(font) {
1698
1712
  var _a, _b;
1699
1713
  const family = this.fonts[font.name];
1714
+ if (!family) return null;
1700
1715
  const style = (_a = font.style) != null ? _a : DEFAULT_FONT_STYLE;
1701
1716
  const weigth = (_b = font.weight) != null ? _b : DEFAULT_FONT_WEIGHT;
1702
1717
  const fontKeys = Object.keys(family.fonts);
@@ -1725,6 +1740,9 @@ var Label = class extends Printable {
1725
1740
  }
1726
1741
  getFontName(font) {
1727
1742
  const indexedFont = this.getIndexedFont(font);
1743
+ if (indexedFont == null) {
1744
+ return font.name;
1745
+ }
1728
1746
  return indexedFont.alias;
1729
1747
  }
1730
1748
  /// This can be extended when we want support multiple fonts
@@ -1773,7 +1791,7 @@ var Text = class extends LabelField {
1773
1791
  this.type = "singleline";
1774
1792
  this.context = void 0;
1775
1793
  this.lineSpacing = 1;
1776
- this.content = content.replace("\n", "");
1794
+ this.content = content.replace("\n", "").replace('"', '\\"');
1777
1795
  this.x = x;
1778
1796
  this.y = y;
1779
1797
  this.formatted = formatted;
@@ -1884,7 +1902,7 @@ var Text = class extends LabelField {
1884
1902
  */
1885
1903
  generatePlainTextCore(content, initialX, initialY, font, features = []) {
1886
1904
  if (!this.context) throw "context-not-set";
1887
- const textWidhtFunction = this.textWithFunction;
1905
+ const textWidhtFunction = this.textWidthFunction;
1888
1906
  let fullWidth = textWidhtFunction(content, font);
1889
1907
  if (this.width) {
1890
1908
  const initialPadding = initialX - this.x;
@@ -1986,7 +2004,7 @@ var Text = class extends LabelField {
1986
2004
  return textCommand;
1987
2005
  } else {
1988
2006
  let lineHeight = font.size * 0.1;
1989
- let textWidth = this.textWithFunction(text, font);
2007
+ let textWidth = this.textWidthFunction(text, font);
1990
2008
  if (features.includes("strike")) {
1991
2009
  commands.push(this.textLineCommand(textWidth, x, y, lineHeight, 0.5, font.size));
1992
2010
  }
@@ -2015,7 +2033,7 @@ var Text = class extends LabelField {
2015
2033
  return (_a = this.context.config) == null ? void 0 : _a.getFontName(font);
2016
2034
  }
2017
2035
  }
2018
- get textWithFunction() {
2036
+ get textWidthFunction() {
2019
2037
  var _a, _b, _c;
2020
2038
  if (this.font.name == "default") {
2021
2039
  return this.defaultTextWidth;
@@ -2103,6 +2121,210 @@ var QRCode = class extends LabelField {
2103
2121
  });
2104
2122
  }
2105
2123
  };
2124
+
2125
+ // src/labels/fields/Table.ts
2126
+ var Table = class extends LabelField {
2127
+ constructor(x, y, rows, options = {}) {
2128
+ var _a, _b, _c, _d;
2129
+ super();
2130
+ this.x = x;
2131
+ this.y = y;
2132
+ this.rows = rows;
2133
+ this.size = options.size;
2134
+ this.columnWidths = options.columnWidths;
2135
+ this.rowHeights = options.rowHeights;
2136
+ this.lineThickness = (_a = options.lineThickness) != null ? _a : 2;
2137
+ this.cellPadding = (_b = options.cellPadding) != null ? _b : 4;
2138
+ this.formatted = (_c = options.formatted) != null ? _c : true;
2139
+ this.font = (_d = options.font) != null ? _d : { name: "default", size: 10 };
2140
+ }
2141
+ setSize(size) {
2142
+ this.size = size;
2143
+ }
2144
+ setColumnWidths(widths) {
2145
+ this.columnWidths = widths;
2146
+ }
2147
+ setRowHeights(heights) {
2148
+ this.rowHeights = heights;
2149
+ }
2150
+ setLineThickness(thickness) {
2151
+ this.lineThickness = thickness;
2152
+ }
2153
+ setCellPadding(padding) {
2154
+ this.cellPadding = padding;
2155
+ }
2156
+ setFormatted(formatted) {
2157
+ this.formatted = formatted;
2158
+ }
2159
+ setFont(font) {
2160
+ this.font = font;
2161
+ }
2162
+ commandForLanguage(language, config) {
2163
+ return __async(this, null, function* () {
2164
+ var _a, _b, _c, _d;
2165
+ const generator = this.commandGeneratorFor(language);
2166
+ const rowCount = this.rows.length;
2167
+ const colCount = Math.max(0, ...this.rows.map((r) => r.length));
2168
+ const resolvedColumnWidths = this.resolveTrackSizes({
2169
+ trackCount: colCount,
2170
+ explicitSizes: this.columnWidths,
2171
+ total: (_a = this.size) == null ? void 0 : _a.width,
2172
+ measure: (i) => this.measureAutoColumnWidth(i, colCount, config)
2173
+ });
2174
+ const resolvedRowHeights = this.resolveTrackSizes({
2175
+ trackCount: rowCount,
2176
+ explicitSizes: this.rowHeights,
2177
+ total: (_b = this.size) == null ? void 0 : _b.height,
2178
+ measure: (i) => this.measureAutoRowHeight(i, resolvedColumnWidths, config)
2179
+ });
2180
+ const xPositions = this.accumulatePositions(this.x, resolvedColumnWidths);
2181
+ const yPositions = this.accumulatePositions(this.y, resolvedRowHeights);
2182
+ const fields = [];
2183
+ for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
2184
+ for (let colIndex = 0; colIndex < colCount; colIndex++) {
2185
+ const cellContent = (_d = (_c = this.rows[rowIndex]) == null ? void 0 : _c[colIndex]) != null ? _d : "";
2186
+ const cellX = xPositions[colIndex];
2187
+ const cellY = yPositions[rowIndex];
2188
+ const cellWidth = resolvedColumnWidths[colIndex];
2189
+ const cellHeight = resolvedRowHeights[rowIndex];
2190
+ const textX = cellX + this.cellPadding;
2191
+ const textY = cellY + this.cellPadding;
2192
+ const textWidth = Math.max(1, cellWidth - this.cellPadding * 2);
2193
+ const textHeight = Math.max(1, cellHeight - this.cellPadding * 2);
2194
+ const text = new Text(cellContent, textX, textY, this.formatted);
2195
+ text.setFont(this.font);
2196
+ text.setMultiLine(textWidth, textHeight);
2197
+ fields.push(text);
2198
+ }
2199
+ }
2200
+ const totalWidth = resolvedColumnWidths.reduce((a, b) => a + b, 0);
2201
+ const totalHeight = resolvedRowHeights.reduce((a, b) => a + b, 0);
2202
+ for (let colIndex = 0; colIndex <= colCount; colIndex++) {
2203
+ const sx = colIndex == colCount ? this.x + totalWidth : xPositions[colIndex];
2204
+ fields.push(new Line(
2205
+ { x: sx, y: this.y },
2206
+ { x: sx, y: this.y + totalHeight },
2207
+ this.lineThickness
2208
+ ));
2209
+ }
2210
+ for (let rowIndex = 0; rowIndex <= rowCount; rowIndex++) {
2211
+ const sy = rowIndex == rowCount ? this.y + totalHeight : yPositions[rowIndex];
2212
+ fields.push(new Line(
2213
+ { x: this.x, y: sy },
2214
+ { x: this.x + totalWidth, y: sy },
2215
+ this.lineThickness
2216
+ ));
2217
+ }
2218
+ const commandList = yield Promise.all(fields.map((field) => field.commandForLanguage(language, config)));
2219
+ return generator.commandGroup(commandList);
2220
+ });
2221
+ }
2222
+ accumulatePositions(start, sizes) {
2223
+ const positions = [];
2224
+ let current = start;
2225
+ for (const s of sizes) {
2226
+ positions.push(current);
2227
+ current += s;
2228
+ }
2229
+ return positions;
2230
+ }
2231
+ resolveTrackSizes(params) {
2232
+ if (params.trackCount == 0) return [];
2233
+ const sizes = new Array(params.trackCount).fill(void 0);
2234
+ if (params.explicitSizes) {
2235
+ for (let i = 0; i < Math.min(params.trackCount, params.explicitSizes.length); i++) {
2236
+ sizes[i] = params.explicitSizes[i];
2237
+ }
2238
+ }
2239
+ if (params.total != void 0) {
2240
+ const fixedTotal = sizes.reduce((sum, s) => sum + (s != null ? s : 0), 0);
2241
+ const flexibleCount = sizes.filter((s) => s == void 0).length;
2242
+ const remaining = Math.max(0, params.total - fixedTotal);
2243
+ const flexSize = flexibleCount == 0 ? 0 : remaining / flexibleCount;
2244
+ return sizes.map((s, i) => {
2245
+ var _a;
2246
+ return Math.max(1, (_a = s != null ? s : flexSize) != null ? _a : params.measure(i));
2247
+ });
2248
+ }
2249
+ return sizes.map((s, i) => Math.max(1, s != null ? s : params.measure(i)));
2250
+ }
2251
+ measureAutoColumnWidth(columnIndex, colCount, config) {
2252
+ var _a;
2253
+ const contentWidths = [];
2254
+ for (const row of this.rows) {
2255
+ const content = (_a = row == null ? void 0 : row[columnIndex]) != null ? _a : "";
2256
+ const plain = this.toPlainText(content);
2257
+ const w = this.textWidth(plain, config);
2258
+ contentWidths.push(w);
2259
+ }
2260
+ const maxContentWidth = contentWidths.length == 0 ? 0 : Math.max(...contentWidths);
2261
+ const minWidth = this.font.size;
2262
+ const measured = Math.max(minWidth, maxContentWidth) + this.cellPadding * 2;
2263
+ if (colCount == 0) return measured;
2264
+ return measured;
2265
+ }
2266
+ measureAutoRowHeight(rowIndex, columnWidths, config) {
2267
+ var _a, _b;
2268
+ const row = (_a = this.rows[rowIndex]) != null ? _a : [];
2269
+ const contentHeights = [];
2270
+ for (let colIndex = 0; colIndex < columnWidths.length; colIndex++) {
2271
+ const content = (_b = row[colIndex]) != null ? _b : "";
2272
+ const plain = this.toPlainText(content);
2273
+ const availableWidth = Math.max(1, columnWidths[colIndex] - this.cellPadding * 2);
2274
+ const lineCount = this.estimateLineCount(plain, availableWidth, config);
2275
+ const lineHeight = this.font.size + 1;
2276
+ contentHeights.push(lineCount * lineHeight);
2277
+ }
2278
+ const maxContentHeight = contentHeights.length == 0 ? this.font.size : Math.max(...contentHeights);
2279
+ return maxContentHeight + this.cellPadding * 2;
2280
+ }
2281
+ estimateLineCount(content, width, config) {
2282
+ const normalized = content.replace("\n", " ");
2283
+ if (normalized.trim() == "") return 1;
2284
+ const words = [];
2285
+ let buffer2 = "";
2286
+ for (let i = 0; i < normalized.length; i++) {
2287
+ const ch = normalized.charAt(i);
2288
+ if (isWhitespace(ch)) {
2289
+ if (buffer2 != "") {
2290
+ words.push(buffer2);
2291
+ buffer2 = "";
2292
+ }
2293
+ continue;
2294
+ }
2295
+ buffer2 += ch;
2296
+ }
2297
+ if (buffer2 != "") words.push(buffer2);
2298
+ let lines = 1;
2299
+ let currentLine = "";
2300
+ for (const word of words) {
2301
+ const candidate = currentLine == "" ? word : `${currentLine} ${word}`;
2302
+ if (this.textWidth(candidate, config) <= width) {
2303
+ currentLine = candidate;
2304
+ } else {
2305
+ if (currentLine == "") {
2306
+ const charsPerLine = Math.max(1, Math.floor(width / Math.max(1, this.font.size)));
2307
+ const neededLines = Math.ceil(word.length / charsPerLine);
2308
+ lines += neededLines - 1;
2309
+ currentLine = word.substring((neededLines - 1) * charsPerLine);
2310
+ } else {
2311
+ lines += 1;
2312
+ currentLine = word;
2313
+ }
2314
+ }
2315
+ }
2316
+ return Math.max(1, lines);
2317
+ }
2318
+ textWidth(text, config) {
2319
+ if (this.font.name == "default" || !config) {
2320
+ return text.length * this.font.size;
2321
+ }
2322
+ return config.textWidth(text, this.font);
2323
+ }
2324
+ toPlainText(content) {
2325
+ return content.replace(/<[^>]*>/g, "");
2326
+ }
2327
+ };
2106
2328
  export {
2107
2329
  commands_exports as commands,
2108
2330
  labels_exports as labels,