Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
173 lines (150 loc) · 4.1 KB
/
Copy pathindex.js
File metadata and controls
173 lines (150 loc) · 4.1 KB
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
import React, { PureComponent } from 'react';
import { View, StyleSheet, Text } from 'react-native';
import PropTypes from 'prop-types';
import Svg, {
Path,
} from 'react-native-svg';
import barcodes from 'jsbarcode/src/barcodes';
export default class Barcode extends PureComponent {
static propTypes = {
/* what the barCode stands for */
value: PropTypes.string,
/* Select which barcode type to use */
format: PropTypes.oneOf(Object.keys(barcodes)),
/* Set the color of the bars */
lineColor: PropTypes.string,
/* Handle error for invalid barcode of selected format */
onError: PropTypes.func
};
static defaultProps = {
value: undefined,
format: 'CODE128',
text: undefined,
lineColor: '#000000',
onError: undefined
};
constructor(props) {
super(props);
this.state = {
bars: [],
barCodeWidth: 0
};
}
componentWillUpdate(nextProps) {
if (nextProps.value !== this.props.value) {
this.update(nextProps);
}
}
componentDidMount() {
this.update();
}
componentDidUpdate() {
this.update();
}
update() {
const encoder = barcodes[this.props.format];
const encoded = this.encode(this.props.value, encoder, this.props);
if (encoded) {
this.state.bars = this.drawSvgBarCode(encoded, this.props);
this.state.barCodeWidth = encoded.data.length;
}
}
drawSvgBarCode(encoding) {
const rects = [];
// binary data of barcode
const binary = encoding.data;
let barWidth = 0;
let x = 0;
let yFrom = 0;
// alert(JSON.stringify(options));
for (let b = 0; b < binary.length; b++) {
x = b;
if (binary[b] === '1') {
barWidth++;
} else if (barWidth > 0) {
rects[rects.length] = this.drawRect(
x - barWidth,
yFrom,
barWidth,
100
);
barWidth = 0;
}
}
// Last draw is needed since the barcode ends with 1
if (barWidth > 0) {
rects[rects.length] = this.drawRect(
x - barWidth + 1,
yFrom,
barWidth,
100
);
}
return rects;
}
drawRect(x, y, width, height) {
return `M${x},${y}h${width}v${height}h-${width}z`;
}
getTotalWidthOfEncodings(encodings) {
let totalWidth = 0;
for (let i = 0; i < encodings.length; i++) {
totalWidth += encodings[i].width;
}
return totalWidth;
}
// encode() handles the Encoder call and builds the binary string to be rendered
encode(text, Encoder, options) {
// If text is not a non-empty string, throw error.
if (typeof text !== "string" || text.length === 0) {
if (this.props.onError) {
this.props.onError(new Error('Barcode value must be a non-empty string'));
return;
}
throw new Error('Barcode value must be a non-empty string');
}
var encoder;
try {
encoder = new Encoder(text, options);
} catch (error) {
// If the encoder could not be instantiated, throw error.
if (this.props.onError) {
this.props.onError(new Error('Invalid barcode format.'));
return;
}
throw new Error('Invalid barcode format.');
}
// If the input is not valid for the encoder, throw error.
if (!encoder.valid()) {
if (this.props.onError) {
this.props.onError(new Error('Invalid barcode for selected format.'));
return;
}
throw new Error('Invalid barcode for selected format.');
}
// Make a request for the binary data (and other infromation) that should be rendered
// encoded stucture is {
// text: 'xxxxx',
// data: '110100100001....'
// }
var encoded = encoder.encode();
return encoded;
}
render() {
this.update();
return (
<View style={{width: "100%", height: "100%"}}>
<Svg
height="100%"
width="100%"
viewBox={`0 0 ${this.state.barCodeWidth} 100`}
// width={200}
preserveAspectRatio="xMinYMin slice"
fill={this.props.lineColor}>
<Path
d={this.state.bars.join(' ')}
/>
</Svg>
</View>
);
}
}