Skip to content

Commit 01ef568

Browse files
committed
refactor(canvas): name the 1.3 fontHeight ratio and dedupe renderer Props
Three small cleanups on top of the KonvaObject split: - The 1.3 ZPL-fontHeight to CSS-fontSize ratio is now a named export (ZPL_FONT_HEIGHT_TO_CSS_RATIO) used by textPositionTransforms and the text/serial render paths in KonvaObjectInner. Same number, no longer a magic literal sprinkled across files. - BarcodeObject was redeclaring the renderer Props interface structurally identical to KonvaObjectProps. Drop the duplicate and import the shared one. - konvaObjectProps.ts docstring updated: LineObject/ImageObject now receive the narrowed obj from the dispatcher (no internal cast) while BarcodeObject and KonvaObjectInner still narrow internally.
1 parent 61cc1ad commit 01ef568

4 files changed

Lines changed: 30 additions & 23 deletions

File tree

‎src/components/Canvas/BarcodeObject.tsx‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ import React, { useCallback, useRef } from "react";
22
import bwipjs from "bwip-js/browser";
33
import { Image as KImage, Group, Rect, Text } from "react-konva";
44
import type Konva from "konva";
5-
import type { LabelObject } from "../../registry";
65
import { BARCODE_1D_TYPES, ObjectRegistry } from "../../registry";
7-
import type { ObjectChanges } from "../../store/labelStore";
86
import { dotsToPx, pxToDots } from "../../lib/coordinates";
7+
import type { KonvaObjectProps } from "./konvaObjectProps";
98
import {
109
buildBwipOptions,
1110
getDisplaySize,
@@ -23,17 +22,6 @@ import {
2322
EAN_UPC_TYPES,
2423
} from "./bwipConstants";
2524

26-
interface Props {
27-
obj: LabelObject;
28-
scale: number;
29-
dpmm: number;
30-
offsetX: number;
31-
offsetY: number;
32-
isSelected: boolean;
33-
onSelect: (addToSelection: boolean) => void;
34-
onChange: (changes: ObjectChanges) => void;
35-
snap: (dots: number) => number;
36-
}
3725
export function BarcodeObject({
3826
obj,
3927
scale,
@@ -44,7 +32,7 @@ export function BarcodeObject({
4432
onSelect,
4533
onChange,
4634
snap,
47-
}: Props) {
35+
}: KonvaObjectProps) {
4836
const groupRef = useRef<Konva.Group>(null);
4937
const textRef = useRef<Konva.Text>(null);
5038

‎src/components/Canvas/KonvaObject.tsx‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import { LineObject } from "./LineObject";
66
import { ImageObject } from "./ImageObject";
77
import type Konva from "konva";
88
import { dotsToPx, pxToDots } from "../../lib/coordinates";
9-
import { objectToDisplay, displayToObject } from "./textPositionTransforms";
9+
import {
10+
objectToDisplay,
11+
displayToObject,
12+
ZPL_FONT_HEIGHT_TO_CSS_RATIO,
13+
} from "./textPositionTransforms";
1014
import type { KonvaObjectProps } from "./konvaObjectProps";
1115

1216
type Props = KonvaObjectProps;
@@ -106,7 +110,10 @@ function KonvaObjectInner({
106110

107111
if (obj.type === "text") {
108112
const p = obj.props;
109-
const fontSize = Math.max(dotsToPx(p.fontHeight, scale, dpmm) / 1.3, 6);
113+
const fontSize = Math.max(
114+
dotsToPx(p.fontHeight, scale, dpmm) / ZPL_FONT_HEIGHT_TO_CSS_RATIO,
115+
6,
116+
);
110117
const fontFamily = p.printerFontName
111118
? (getFontFamily(p.printerFontName) ?? "'Roboto Condensed', sans-serif")
112119
: "'Roboto Condensed', sans-serif";
@@ -179,7 +186,10 @@ function KonvaObjectInner({
179186

180187
if (obj.type === "serial") {
181188
const p = obj.props;
182-
const fontSize = Math.max(dotsToPx(p.fontHeight, scale, dpmm) / 1.3, 6);
189+
const fontSize = Math.max(
190+
dotsToPx(p.fontHeight, scale, dpmm) / ZPL_FONT_HEIGHT_TO_CSS_RATIO,
191+
6,
192+
);
183193
const zplRotationDeg: Record<typeof p.rotation, number> = {
184194
N: 0,
185195
R: 90,

‎src/components/Canvas/konvaObjectProps.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ import type { LabelObject } from "../../registry";
22
import type { ObjectChanges } from "../../store/labelStore";
33

44
/** Shared props for the per-type renderers under KonvaObject (LineObject,
5-
* ImageObject, BarcodeObject, KonvaObjectInner). The dispatcher hands
6-
* the wide LabelObject down; each renderer narrows internally. */
5+
* ImageObject, BarcodeObject, KonvaObjectInner). LineObject and
6+
* ImageObject re-narrow `obj` at the type level via `Omit & { obj: ... }`
7+
* and the dispatcher passes the narrowed value explicitly; BarcodeObject
8+
* and KonvaObjectInner currently take the wide LabelObject and narrow
9+
* internally. */
710
export interface KonvaObjectProps {
811
obj: LabelObject;
912
scale: number;

‎src/components/Canvas/textPositionTransforms.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,18 @@ interface TextLikeProps {
2424
/** 15 dots empirical canvas/ZPL alignment offset for rotated text. */
2525
const ROTATION_OFFSET_DOTS = 15;
2626

27+
/** Ratio between ZPL fontHeight (cap-height) and CSS/Konva fontSize
28+
* (em-height) for Roboto Condensed Bold. Empirical: divide ZPL
29+
* fontHeight by this to get the Konva-rendered height in dots, or to
30+
* derive the Konva fontSize. Lives here because it appears in both
31+
* the FT baseline math and the text/serial render paths. */
32+
export const ZPL_FONT_HEIGHT_TO_CSS_RATIO = 1.3;
33+
2734
function ftBaselineDelta(props: TextLikeProps): { dx: number; dy: number } {
2835
// For R/I/B the Konva anchor sits at the far end of the rendered
29-
// glyph, so we use the actual rendered height (fontHeight / 1.3).
30-
// For N the anchor is at the top, so we shift up by the full ZPL
31-
// fontHeight.
32-
const renderedH = props.fontHeight / 1.3;
36+
// glyph, so we use the actual rendered height. For N the anchor is
37+
// at the top, so we shift up by the full ZPL fontHeight.
38+
const renderedH = props.fontHeight / ZPL_FONT_HEIGHT_TO_CSS_RATIO;
3339
switch (props.rotation) {
3440
case 'N': return { dx: 0, dy: -props.fontHeight };
3541
case 'R': return { dx: renderedH, dy: 0 };

0 commit comments

Comments
 (0)