Skip to content

Commit b2c5a70

Browse files
committed
Add support for codabar
1 parent 72169a4 commit b2c5a70

5 files changed

Lines changed: 56 additions & 5 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Supported codes:
1616
- [x] Code 39
1717
- [x] Code 93
1818
- [x] ITF-14
19+
- [x] Codabar
1920
- [x] Code 128
2021
- [x] UPC-A
2122

android/app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ android {
9393
minSdkVersion rootProject.ext.minSdkVersion
9494
targetSdkVersion rootProject.ext.targetSdkVersion
9595
versionCode 1
96-
versionName "2.1.4"
96+
versionName "2.1.5"
9797
}
9898
signingConfigs {
9999
debug {

app.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"expo": {
33
"name": "Passes",
44
"slug": "passes",
5-
"version": "2.1.4",
5+
"version": "2.1.5",
66
"orientation": "portrait",
77
"scheme": "passes",
88
"newArchEnabled": true,

app/add/qrDisplay.tsx

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,37 @@ const getBwipJsBcid = (expoType: string): string => {
3939
return BARCODE_TYPE_MAPPING[expoType.toLowerCase()] || expoType;
4040
};
4141

42+
type BwipRenderOptions = Parameters<typeof toDataURL>[0] & {
43+
includestartstop?: boolean;
44+
};
45+
46+
const CODABAR_SENTINELS = new Set(["A", "B", "C", "D"]);
47+
48+
const ensureCodabarSentinels = (value: string) => {
49+
const uppercaseValue = value.toUpperCase();
50+
51+
const startsWithSentinel =
52+
uppercaseValue.length > 0 && CODABAR_SENTINELS.has(uppercaseValue[0]);
53+
const endsWithSentinel =
54+
uppercaseValue.length > 0 && CODABAR_SENTINELS.has(uppercaseValue[uppercaseValue.length - 1]);
55+
56+
let sanitizedText = uppercaseValue;
57+
58+
if (!startsWithSentinel) {
59+
sanitizedText = `A${sanitizedText}`;
60+
}
61+
62+
if (!endsWithSentinel) {
63+
sanitizedText = `${sanitizedText}A`;
64+
}
65+
66+
return {
67+
sanitizedText,
68+
altText: uppercaseValue,
69+
includeStartStop: startsWithSentinel && endsWithSentinel,
70+
};
71+
};
72+
4273
export default function QRDisplayScreen() {
4374
const { invertColors } = useInvertColors();
4475
const router = useRouter();
@@ -51,6 +82,7 @@ export default function QRDisplayScreen() {
5182
}>();
5283
const { addPass, getPassById, deletePass } = usePasses();
5384
const [barcodeSource, setBarcodeSource] = useState<DataURL | null>(null);
85+
const [barcodeError, setBarcodeError] = useState<string | null>(null);
5486
const [scaledSize, setScaledSize] = useState({ width: 0, height: 0 });
5587
const [viewSize, setViewSize] = useState({ width: 0, height: 0 });
5688
const existingPass = passId ? getPassById(passId) : undefined;
@@ -87,16 +119,28 @@ export default function QRDisplayScreen() {
87119
if (currentData && currentType) {
88120
const bcidForBwipJs = getBwipJsBcid(currentType);
89121

90-
const bwipJsOptions = {
122+
const bwipJsOptions: BwipRenderOptions = {
91123
bcid: bcidForBwipJs,
92124
text: currentData,
93125
scale: PixelRatio.get() * 2,
94126
includetext: true,
95-
textxalign: "center" as "center",
127+
textxalign: "center",
96128
barcolor: "000000",
97129
backgroundcolor: "FFFFFF",
98130
};
99131

132+
if (bcidForBwipJs === "rationalizedCodabar") {
133+
const { sanitizedText, altText, includeStartStop } = ensureCodabarSentinels(
134+
currentData
135+
);
136+
137+
bwipJsOptions.text = sanitizedText;
138+
bwipJsOptions.alttext = altText || sanitizedText;
139+
bwipJsOptions.includestartstop = includeStartStop;
140+
}
141+
142+
setBarcodeError(null);
143+
100144
toDataURL(bwipJsOptions)
101145
.then(result => {
102146
if (!cancelled) {
@@ -109,11 +153,13 @@ export default function QRDisplayScreen() {
109153
"bwip-js toDataURL error:",
110154
err.message ? err.message : err
111155
);
156+
setBarcodeError("Unable to render barcode. Please try again.");
112157
setBarcodeSource(null);
113158
}
114159
});
115160
} else {
116161
setBarcodeSource(null);
162+
setBarcodeError(null);
117163
}
118164

119165
return () => {
@@ -186,6 +232,10 @@ export default function QRDisplayScreen() {
186232
style={imageStyle}
187233
source={{ uri: barcodeSource.uri }}
188234
/>
235+
) : barcodeError ? (
236+
<StyledText style={styles.loadingText}>
237+
{barcodeError}
238+
</StyledText>
189239
) : (
190240
<StyledText style={styles.loadingText}>
191241
{currentData

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "passes",
33
"main": "expo-router/entry",
4-
"version": "2.1.4",
4+
"version": "2.1.5",
55
"scripts": {
66
"start": "expo start",
77
"reset-project": "node ./scripts/reset-project.js",

0 commit comments

Comments
 (0)