| 15 | import { SymbolShapeHint } from './encoder/constants'; |
| 16 | |
| 17 | export default class DataMatrixWriter implements Writer { |
| 18 | public encode( |
| 19 | contents: string, |
| 20 | format: BarcodeFormat, |
| 21 | width: number, |
| 22 | height: number, |
| 23 | hints: Map<EncodeHintType, unknown> = null |
| 24 | ): BitMatrix { |
| 25 | if (contents.trim() === '') { |
| 26 | throw new Error('Found empty contents'); |
| 27 | } |
| 28 | |
| 29 | if (format !== BarcodeFormat.DATA_MATRIX) { |
| 30 | throw new Error('Can only encode DATA_MATRIX, but got ' + format); |
| 31 | } |
| 32 | |
| 33 | if (width < 0 || height < 0) { |
| 34 | throw new Error( |
| 35 | 'Requested dimensions can\'t be negative: ' + width + 'x' + height |
| 36 | ); |
| 37 | } |
| 38 | |
| 39 | // Try to get force shape & min / max size |
| 40 | let shape = SymbolShapeHint.FORCE_NONE; |
| 41 | let minSize: Dimension = null; |
| 42 | let maxSize: Dimension = null; |
| 43 | if (hints != null) { |
| 44 | const requestedShape = hints.get( |
| 45 | EncodeHintType.DATA_MATRIX_SHAPE |
| 46 | ) as SymbolShapeHint; |
| 47 | if (requestedShape != null) { |
| 48 | shape = requestedShape; |
| 49 | } |
| 50 | |
| 51 | const requestedMinSize = hints.get(EncodeHintType.MIN_SIZE) as Dimension; |
| 52 | if (requestedMinSize != null) { |
| 53 | minSize = requestedMinSize; |
| 54 | } |
| 55 | |
| 56 | const requestedMaxSize = hints.get(EncodeHintType.MAX_SIZE) as Dimension; |
| 57 | if (requestedMaxSize != null) { |
| 58 | maxSize = requestedMaxSize; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // 1. step: Data encodation |
| 63 | let encoded: string; |
| 64 | |
| 65 | const hasCompactionHint = |
| 66 | hints != null && |
| 67 | hints.has(EncodeHintType.DATA_MATRIX_COMPACT) && |
| 68 | Boolean(hints.get(EncodeHintType.DATA_MATRIX_COMPACT).toString()); |
| 69 | if (hasCompactionHint) { |
| 70 | const hasGS1FormatHint = |
| 71 | hints.has(EncodeHintType.GS1_FORMAT) && |
| 72 | Boolean(hints.get(EncodeHintType.GS1_FORMAT).toString()); |
| 73 | |
| 74 | let charset: Charset = null; |
nothing calls this directly
no outgoing calls
no test coverage detected