-
Notifications
You must be signed in to change notification settings - Fork 6
/
Barcode.tsx
201 lines (186 loc) · 5.85 KB
/
Barcode.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Original source:
// https://github.com/Kichiyaki/react-native-barcode-generator/blob/22f020eacf47fe23f0e311732ec801068db14fbc/index.js
// Original license:
// The MIT License (MIT)
//
// Copyright (c) 2021 Dawid Wysokiński
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import CODE128 from 'jsbarcode/src/barcodes/CODE128/CODE128';
import CODE128AUTO from 'jsbarcode/src/barcodes/CODE128/CODE128_AUTO';
import React, {useCallback, useMemo} from 'react';
import {StyleSheet, Text, TextProps, View, ViewProps} from 'react-native';
import {Path, Svg} from 'react-native-svg';
export type Format = 'CODE128' | 'CODE128AUTO';
export const START_CHARACTERS = {
'CODE-A': String.fromCharCode(105 + 103),
'CODE-B': String.fromCharCode(105 + 104),
'CODE-C': String.fromCharCode(105 + 105),
};
export const CODE_SET_CHARACTERS = {
'CODE-A': String.fromCharCode(105 + 101),
'CODE-B': String.fromCharCode(105 + 100),
'CODE-C': String.fromCharCode(105 + 99),
};
const ENCODERS = {
CODE128,
CODE128AUTO,
};
export type BarcodeProps = {
/**
* バーコードに設定するデータ
*/
value: string;
/**
* バーの幅
*/
lineWidth?: number;
/**
* バーコードの最大幅(textで表示する領域は含まない)
*/
maxWidth?: number;
/**
* バーコードの高さ(textで表示する領域は含まない)
*/
height?: number;
/**
* バーコードの左右の余白
*/
quietZone?: number;
/**
* バーコードのフォーマット
* 以下のフォーマットを設定可能
* - CODE128: スタートキャラクタやコードセットキャラクタを自身で設定するフォーマット
* - CODE128AUTO: 指定されたデータに応じて自動でスタートキャラクタやコードセットキャラクタを設定するフォーマット
*/
format?: Format;
/**
* バーの色
*/
lineColor?: string;
/**
* 背景色
*/
background?: string;
/**
* バーコード下部に表示するテキスト
*/
text?: string;
/**
* バーコード下部に表示するテキストのProps
*/
textProps?: TextProps;
/**
* ViewProps
*/
viewProps?: ViewProps;
/**
* エラー発生時に呼び出されるコールバック関数
*/
onError?: (err: unknown) => void;
};
const initialBarcode = {
bars: '',
barCodeWidth: 0,
};
const drawRect = (x: number, y: number, width: number, height: number) => {
return `M${x},${y}h${width}v${height}h-${width}z`;
};
const drawSvgBarCode = (binary: string, height: number, barCodeWidth: number) => {
const rects = [];
const singleBarWidth = barCodeWidth / binary.length;
let barWidth = 0;
let x = 0;
const yFrom = 0;
for (let b = 0; b < binary.length; b++) {
x = b * singleBarWidth;
if (binary[b] === '1') {
barWidth++;
} else if (barWidth > 0) {
rects[rects.length] = drawRect(x - singleBarWidth * barWidth, yFrom, singleBarWidth * barWidth, height);
barWidth = 0;
}
}
if (barWidth > 0) {
rects[rects.length] = drawRect(x - singleBarWidth * (barWidth - 1), yFrom, singleBarWidth * barWidth, height);
}
return rects;
};
/**
* バーコードを生成し、表示するコンポーネント
* CODE128のバーコードのみをサポートしている
*/
export const Barcode: React.FC<BarcodeProps> = ({
value = '',
lineWidth = 2,
maxWidth = Infinity,
height = 100,
quietZone = 0,
format = 'CODE128',
lineColor = '#000000',
background = '#FFFFFF',
text,
textProps: {style: textStyle, ...textProps} = {},
viewProps: {style, ...viewProps} = {},
onError,
}) => {
const getEncoder = useCallback(() => {
const encoder = new ENCODERS[format](value, {});
if (!encoder.valid()) {
throw new Error('Invalid barcode for selected format.');
}
return encoder;
}, [format, value]);
const {bars, barCodeWidth} = useMemo(() => {
try {
const encoder = getEncoder();
const encoded = encoder.encode();
const barCodeWidth = Math.min(maxWidth, encoded.data.length * lineWidth);
return {
bars: drawSvgBarCode(encoded.data, height, barCodeWidth).join(' '),
barCodeWidth,
};
} catch (error) {
onError?.(error);
}
return initialBarcode;
}, [getEncoder, lineWidth, height, maxWidth, onError]);
return (
<View
style={StyleSheet.flatten([styles.container, {backgroundColor: background, paddingHorizontal: quietZone}, style])}
{...viewProps}>
<Svg height={height} width={barCodeWidth} fill={lineColor}>
<Path d={bars} />
</Svg>
{text && (
<Text style={StyleSheet.flatten([styles.textStyle, textStyle])} {...textProps}>
{text}
</Text>
)}
</View>
);
};
const styles = StyleSheet.create({
container: {
alignItems: 'center',
},
textStyle: {
textAlign: 'center',
},
});