label-printer 0.7.6 → 0.7.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.
package/dist/index.d.mts CHANGED
@@ -631,6 +631,7 @@ declare class Text extends LabelField {
631
631
  private type;
632
632
  private context;
633
633
  private readonly lineSpacing;
634
+ private endsWithBreak;
634
635
  /**
635
636
  * Width of the text.
636
637
  * If set, the text will be clipped to this size
package/dist/index.d.ts CHANGED
@@ -631,6 +631,7 @@ declare class Text extends LabelField {
631
631
  private type;
632
632
  private context;
633
633
  private readonly lineSpacing;
634
+ private endsWithBreak;
634
635
  /**
635
636
  * Width of the text.
636
637
  * If set, the text will be clipped to this size
package/dist/index.js CHANGED
@@ -2548,6 +2548,8 @@ var BOLD_TAG = "b";
2548
2548
  var ITALIC_TAG = "i";
2549
2549
  var UNDERLINE_TAG = "u";
2550
2550
  var STRIKE_TAG = ["s", "del", "strike"];
2551
+ var PARAGRAPH_TAG = "p";
2552
+ var BREAK_TAG = "br";
2551
2553
  var Text = class extends LabelField {
2552
2554
  constructor(content, x, y, formatted = true) {
2553
2555
  super();
@@ -2560,6 +2562,24 @@ var Text = class extends LabelField {
2560
2562
  this.y = y;
2561
2563
  this.formatted = formatted;
2562
2564
  }
2565
+ endsWithBreak(node) {
2566
+ if (node.nodeType == import_node_html_parser.NodeType.TEXT_NODE) {
2567
+ return node.innerText.trim() == "";
2568
+ }
2569
+ const elementNode = node;
2570
+ if (elementNode.rawTagName == BREAK_TAG) return true;
2571
+ const children = elementNode.childNodes;
2572
+ for (let i = children.length - 1; i >= 0; i--) {
2573
+ const child = children[i];
2574
+ if (child.nodeType == import_node_html_parser.NodeType.TEXT_NODE) {
2575
+ if (child.innerText.trim() == "") continue;
2576
+ return false;
2577
+ }
2578
+ if (this.endsWithBreak(child)) return true;
2579
+ return false;
2580
+ }
2581
+ return false;
2582
+ }
2563
2583
  /**
2564
2584
  * Sets the field to single line
2565
2585
  * @param width Max width of the text. Leave it undefined to allow the field to grow
@@ -2632,6 +2652,13 @@ var Text = class extends LabelField {
2632
2652
  } else {
2633
2653
  const elementNode = rootNode;
2634
2654
  const tag = elementNode.rawTagName;
2655
+ if (tag == BREAK_TAG) {
2656
+ return {
2657
+ x: this.x,
2658
+ y: initialY + font.size + this.lineSpacing,
2659
+ command: this.context.generator.commandGroup([])
2660
+ };
2661
+ }
2635
2662
  let commands = [];
2636
2663
  let currentX = initialX;
2637
2664
  let currentY = initialY;
@@ -2646,12 +2673,24 @@ var Text = class extends LabelField {
2646
2673
  } else if (tag == ITALIC_TAG) {
2647
2674
  baseFont.style = "italic";
2648
2675
  }
2676
+ if (tag == PARAGRAPH_TAG) {
2677
+ if (initialX != this.x) {
2678
+ currentX = this.x;
2679
+ currentY = initialY + baseFont.size + this.lineSpacing;
2680
+ }
2681
+ }
2649
2682
  elementNode.childNodes.forEach((node) => {
2650
2683
  const { x, y, command } = this.generateFormattedRecursive(currentX, currentY, node, baseFont, baseFeatures);
2651
2684
  currentX = x;
2652
2685
  currentY = y;
2653
2686
  commands.push(command);
2654
2687
  });
2688
+ if (tag == PARAGRAPH_TAG) {
2689
+ if (!this.endsWithBreak(elementNode)) {
2690
+ currentX = this.x;
2691
+ currentY += baseFont.size + this.lineSpacing;
2692
+ }
2693
+ }
2655
2694
  return {
2656
2695
  x: currentX,
2657
2696
  y: currentY,