Keeping receipt prices aligned when item names wrap
Put Coffee below Almond croissant with apricot jam on a narrow receipt. The croissant name wraps onto a second line. If the price column advances once per item, the coffee price ends up next to apricot jam . Repceipt's generator draws names and prices in separate text blocks. The two-column format is a familiar pattern on paper receipts, as shown in Repceipt's receipt design notes . Reserve space…
When item names on a receipt wrap onto multiple lines, keeping the corresponding prices aligned becomes a challenge. Repceipt's receipt generator handles this by treating names and prices as separate text blocks. The generator works with a preset width, which determines the number of columns available for each item's label and price.
For instance, a 58 mm preset offers 30 columns, while an 80 mm preset provides 42 columns. To calculate the space for each character, the generator divides the available width by the number of columns, then sizes the monospaced font accordingly.
The generator formats the price and label together before they wrap onto different lines. For example, "$6.50" consumes five columns, leaving 24 columns for the label at a 30-column width. After formatting the price as "6,50 EUR," which takes up eight columns, 21 columns remain for the label. The layout function pairs each label-label pair and processes them line by line, distributing the text across lines based on the available space.
This process creates a two-column receipt, with the price column shifting down when the label wraps onto multiple lines.
Both the preview and export use the same layout, ensuring that labels and prices align properly. The generator outputs two text blocks, one for labels and one for prices, both aligned left and right, respectively. When changing the paper setting, the layout recalculates, and the sections below the items adjust to fit the new height.
A regression test using a lengthy item name confirms that both blocks start at the same y-coordinate and that the price column correctly includes blank entries for continued lines. A simple visual test involves adding a short item after a long one; a missing blank line becomes apparent upon comparison.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to