@@ -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+
4273export 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
0 commit comments