|
| 1 | +import { Line, Rect, Text } from "react-konva"; |
| 2 | +import type { ReactNode } from "react"; |
| 3 | +import { VERA_MONO_HRI_CAP_TOP_PAD } from "./bwipConstants"; |
| 4 | + |
| 5 | +// Monospace advance (cell width / em), used to estimate the centered HRI |
| 6 | +// text's width so the glyphs flank it (assumes a monospace fontFamily, which |
| 7 | +// the Vera/Font-A HRI is); the text itself is rendered unchanged. |
| 8 | +const MONO_ADVANCE = 0.6; |
| 9 | +// Cap band within the em box; matches the HRI text's glyphTopPad. |
| 10 | +const CAP_HEIGHT = 1 - 2 * VERA_MONO_HRI_CAP_TOP_PAD; |
| 11 | +// Triangle apex slightly obtuse (Labelary Code 11): width ~= height (~51deg). |
| 12 | +const TRI_W_RATIO = 0.95; |
| 13 | +const TRI_Y_SHIFT = 0.12; |
| 14 | +// Triangle height per cap, asymmetric: small start, larger stop. |
| 15 | +const TRI_H_START = 0.5; |
| 16 | +const TRI_H_STOP = 0.75; |
| 17 | +// Extra horizontal gap (per cell) so the triangle does not crowd the text. |
| 18 | +const TRI_GAP = 0.2; |
| 19 | +// Code 93 oblong: 0.5:1 aspect scaled to ~0.8, bottom-anchored to the baseline. |
| 20 | +const SQ_W = 0.4; |
| 21 | +const SQ_H = 0.8; |
| 22 | +// The font's asterisk sits high; push it down to center it on the digit row. |
| 23 | +const ASTERISK_Y_SHIFT = 0.2; |
| 24 | +// Stroke width per em for the drawn triangle/square outlines. |
| 25 | +const STROKE_RATIO = 0.06; |
| 26 | + |
| 27 | +type Glyph = "triangle" | "square" | "asterisk"; |
| 28 | + |
| 29 | +interface BuildArgs { |
| 30 | + /** The HRI text already rendered (centered) by BarcodeObject. */ |
| 31 | + text: string; |
| 32 | + fontFamily: string; |
| 33 | + fontSize: number; |
| 34 | + /** Upright bar left edge and width in display px. */ |
| 35 | + barLeftPx: number; |
| 36 | + barW: number; |
| 37 | + /** Top y of the HRI text box. */ |
| 38 | + textY: number; |
| 39 | + glyph: Glyph; |
| 40 | +} |
| 41 | + |
| 42 | +/** Start/stop markers flanking the centered HRI text: Code 11 triangle |
| 43 | + * (asymmetric, small start / large stop), Code 93 oblong square, Code 39 |
| 44 | + * asterisk (a real glyph, but lowered to sit centered like Labelary). The |
| 45 | + * text stays rendered as-is; markers sit one cell-width past its edges. */ |
| 46 | +export function buildCode1dStartStopGlyphs(a: BuildArgs): ReactNode[] { |
| 47 | + const { text, fontFamily, fontSize, barLeftPx, barW, textY, glyph } = a; |
| 48 | + const cellW = fontSize * MONO_ADVANCE; |
| 49 | + const capTop = textY + fontSize * VERA_MONO_HRI_CAP_TOP_PAD; |
| 50 | + const capH = fontSize * CAP_HEIGHT; |
| 51 | + const stroke = Math.max(fontSize * STROKE_RATIO, 1); |
| 52 | + const center = barLeftPx + barW / 2; |
| 53 | + const textHalf = (text.length * cellW) / 2; |
| 54 | + const triGap = glyph === "triangle" ? cellW * TRI_GAP : 0; |
| 55 | + const startCx = center - textHalf - cellW / 2 - triGap; |
| 56 | + const stopCx = center + textHalf + cellW / 2 + triGap; |
| 57 | + |
| 58 | + const make = (key: string, cx: number, isStop: boolean): ReactNode => { |
| 59 | + if (glyph === "asterisk") { |
| 60 | + // Box must exceed the glyph's measured width or Konva drops the line |
| 61 | + // (empty textArr -> 0x0, invisible); cellW alone is too tight. |
| 62 | + return ( |
| 63 | + <Text key={key} x={cx - fontSize / 2} width={fontSize} y={textY + capH * ASTERISK_Y_SHIFT} |
| 64 | + text="*" fontSize={fontSize} fontFamily={fontFamily} |
| 65 | + align="center" wrap="none" fill="#000000" listening={false} /> |
| 66 | + ); |
| 67 | + } |
| 68 | + if (glyph === "square") { |
| 69 | + const w = capH * SQ_W, h = capH * SQ_H; |
| 70 | + const cy = capTop + capH - h / 2; // bottom-anchored to the baseline |
| 71 | + return ( |
| 72 | + <Rect key={key} x={cx - w / 2} y={cy - h / 2} width={w} height={h} |
| 73 | + stroke="#000000" strokeWidth={stroke} listening={false} /> |
| 74 | + ); |
| 75 | + } |
| 76 | + const h = capH * (isStop ? TRI_H_STOP : TRI_H_START), w = h * TRI_W_RATIO; |
| 77 | + const cy = capTop + capH / 2 + capH * TRI_Y_SHIFT; |
| 78 | + return ( |
| 79 | + <Line key={key} closed listening={false} stroke="#000000" strokeWidth={stroke} |
| 80 | + points={[cx, cy - h / 2, cx - w / 2, cy + h / 2, cx + w / 2, cy + h / 2]} /> |
| 81 | + ); |
| 82 | + }; |
| 83 | + |
| 84 | + return [make("start", startCx, false), make("stop", stopCx, true)]; |
| 85 | +} |
0 commit comments