Skip to content

Commit

Permalink
feat: implements ability to alter dot size #32
Browse files Browse the repository at this point in the history
Implements dotSize to change the size of the dot in dotty mode.
  • Loading branch information
jshor committed Mar 29, 2021
1 parent 11b69e6 commit 32b9b42
Show file tree
Hide file tree
Showing 12 changed files with 1,545 additions and 1,396 deletions.
3 changes: 2 additions & 1 deletion docs/docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ The following symbology options are available:
| `showHumanReadableText` | `Boolean` | Show or hide the symbology data as human-readable text (if applicable). | No | `true` |
| `encoding` | `Encoding` | [The enumerated encoding type of input data.](encoding.md#encoding-modes) | No | `DATA_MODE` |
| `eci` | `Number` | [ECI encoding mode.](encoding.md#extended-channel-interpolation-eci) | No | `0` |
| `primary` | `String` | Primary message data for more complex symbols. | No | `null` |v
| `primary` | `String` | Primary message data for more complex symbols. | No | `null` |
| `rotation` | `Number` | Clockwise rotation of the barcode. Valid values are `0`, `90`, `180`, or `270`. | No | `0` |
| `dotSize` | `Number` | Size of dot used in [`BARCODE_DOTTY_MODE`](#output-options). | No | `0.8` |
| `text` | `String` | Human-readable text to display. Defaults to the input data value. | No | (Data value) |

\* required only if using [`createFile()`](api.md#createfile).
Expand Down
6 changes: 5 additions & 1 deletion src/binding/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ namespace symbology {

// options (-1 indicates not set)
int option_1, option_2, option_3, output_options;
float scale;
float scale, dot_size;

scale = (float)args[9]->NumberValue(context).FromJust();
dot_size = (float)args[19]->NumberValue(context).FromJust();
option_1 = (int)args[10]->NumberValue(context).FromJust();
option_2 = (int)args[11]->NumberValue(context).FromJust();
option_3 = (int)args[12]->NumberValue(context).FromJust();
Expand All @@ -119,6 +120,9 @@ namespace symbology {
if(scale > 0) {
symbol->scale = scale;
}
if(dot_size > -1) {
symbol->dot_size = dot_size;
}

Nan::Utf8String bgcolor(args[6]);
Nan::Utf8String fgcolor(args[7]);
Expand Down
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ const defaultConfig = {
encoding: exp.Encoding.DATA_MODE,
eci: 0,
primary: '',
rotation: 0
rotation: 0,
dotSize: 0.8
}

/**
Expand Down Expand Up @@ -91,7 +92,7 @@ async function createFile (config, barcodeData) {
await png.writeFile(image, symbol.fileName)
} else {
// write SVG or EPS to a file
fs.writeFileSync(symbol.fileName, res.data)
fs.writeFileSync(symbol.fileName, res.data.trim())
}

return omit(res, 'data')
Expand Down
12 changes: 8 additions & 4 deletions src/lib/__tests__/binary.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ describe('Binary methods', () => {
eci: 0,
primary: '',
text: 'example text',
rotation: 0
rotation: 0,
dotSize: 0.8
}
const barcodeData = 'primary data'

Expand All @@ -62,7 +63,8 @@ describe('Binary methods', () => {
symbol.encoding,
symbol.eci,
symbol.primary,
symbol.rotation
symbol.rotation,
symbol.dotSize
)
})

Expand All @@ -89,7 +91,8 @@ describe('Binary methods', () => {
eci: 0,
primary: '',
text: '',
rotation: 0
rotation: 0,
dotSize: 0.8
}
const barcodeData = 'primary data'

Expand All @@ -114,7 +117,8 @@ describe('Binary methods', () => {
symbol.encoding,
symbol.eci,
symbol.primary,
0
symbol.rotation,
symbol.dotSize
)
})
})
Expand Down
3 changes: 2 additions & 1 deletion src/lib/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ function createBuffer (symbol, barcodeData) {
symbol.encoding,
symbol.eci,
symbol.primary,
symbol.rotation
symbol.rotation,
symbol.dotSize
)
}

Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
115 changes: 115 additions & 0 deletions test/e2e/__snapshots__/transformations.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Symbology Transformations PostScript should render an eps 1`] = `
"%!PS-Adobe-3.0 EPSF-3.0
%%Creator: Zint 2.9.1.9
%%Title: Zint Generated Symbol
%%Pages: 0
%%BoundingBox: 0 0 158 119
%%EndComments
/TL { setlinewidth moveto lineto stroke } bind def
/TD { newpath 0 360 arc fill } bind def
/TH { 0 setlinewidth moveto lineto lineto lineto lineto lineto closepath fill } bind def
/TB { 2 copy } bind def
/TR { newpath 4 1 roll exch moveto 1 index 0 rlineto 0 exch rlineto neg 0 rlineto closepath fill } bind def
/TE { pop pop } bind def
newpath
1.00 1.00 1.00 setrgbcolor
118.90 0.00 TB 0.00 158.00 TR
TE
0.00 0.00 0.00 setrgbcolor
100.00 18.90 TB 0.00 4.00 TR
TE
100.00 18.90 TB 6.00 2.00 TR
TE
100.00 18.90 TB 12.00 6.00 TR
TE
100.00 18.90 TB 22.00 2.00 TR
TE
100.00 18.90 TB 26.00 4.00 TR
TE
100.00 18.90 TB 34.00 6.00 TR
TE
100.00 18.90 TB 44.00 2.00 TR
TE
100.00 18.90 TB 52.00 2.00 TR
TE
100.00 18.90 TB 56.00 4.00 TR
TE
100.00 18.90 TB 66.00 2.00 TR
TE
100.00 18.90 TB 70.00 8.00 TR
TE
100.00 18.90 TB 80.00 6.00 TR
TE
100.00 18.90 TB 88.00 4.00 TR
TE
100.00 18.90 TB 94.00 6.00 TR
TE
100.00 18.90 TB 104.00 2.00 TR
TE
100.00 18.90 TB 110.00 6.00 TR
TE
100.00 18.90 TB 118.00 2.00 TR
TE
100.00 18.90 TB 122.00 4.00 TR
TE
100.00 18.90 TB 132.00 4.00 TR
TE
100.00 18.90 TB 142.00 6.00 TR
TE
100.00 18.90 TB 150.00 2.00 TR
TE
100.00 18.90 TB 154.00 4.00 TR
TE
matrix currentmatrix
/Helvetica findfont
14.00 scalefont setfont
0 0 moveto 79.00 3.50 translate 0.00 rotate 0 0 moveto
(12345) stringwidth
pop
-2 div 0 rmoveto
(12345) show
setmatrix"
`;

exports[`Symbology Transformations Scalable Vector Graphics should render an SVG 1`] = `
"<?xml version=\\"1.0\\" standalone=\\"no\\"?>
<!DOCTYPE svg PUBLIC \\"-//W3C//DTD SVG 1.1//EN\\"
\\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\\">
<svg width=\\"158\\" height=\\"119\\" version=\\"1.1\\"
xmlns=\\"http://www.w3.org/2000/svg\\">
<desc>Zint Generated Symbol
</desc>
<g id=\\"barcode\\" fill=\\"#000000\\">
<rect x=\\"0\\" y=\\"0\\" width=\\"158\\" height=\\"119\\" fill=\\"#FFFFFF\\" />
<rect x=\\"0.00\\" y=\\"0.00\\" width=\\"4.00\\" height=\\"100.00\\" />
<rect x=\\"6.00\\" y=\\"0.00\\" width=\\"2.00\\" height=\\"100.00\\" />
<rect x=\\"12.00\\" y=\\"0.00\\" width=\\"6.00\\" height=\\"100.00\\" />
<rect x=\\"22.00\\" y=\\"0.00\\" width=\\"2.00\\" height=\\"100.00\\" />
<rect x=\\"26.00\\" y=\\"0.00\\" width=\\"4.00\\" height=\\"100.00\\" />
<rect x=\\"34.00\\" y=\\"0.00\\" width=\\"6.00\\" height=\\"100.00\\" />
<rect x=\\"44.00\\" y=\\"0.00\\" width=\\"2.00\\" height=\\"100.00\\" />
<rect x=\\"52.00\\" y=\\"0.00\\" width=\\"2.00\\" height=\\"100.00\\" />
<rect x=\\"56.00\\" y=\\"0.00\\" width=\\"4.00\\" height=\\"100.00\\" />
<rect x=\\"66.00\\" y=\\"0.00\\" width=\\"2.00\\" height=\\"100.00\\" />
<rect x=\\"70.00\\" y=\\"0.00\\" width=\\"8.00\\" height=\\"100.00\\" />
<rect x=\\"80.00\\" y=\\"0.00\\" width=\\"6.00\\" height=\\"100.00\\" />
<rect x=\\"88.00\\" y=\\"0.00\\" width=\\"4.00\\" height=\\"100.00\\" />
<rect x=\\"94.00\\" y=\\"0.00\\" width=\\"6.00\\" height=\\"100.00\\" />
<rect x=\\"104.00\\" y=\\"0.00\\" width=\\"2.00\\" height=\\"100.00\\" />
<rect x=\\"110.00\\" y=\\"0.00\\" width=\\"6.00\\" height=\\"100.00\\" />
<rect x=\\"118.00\\" y=\\"0.00\\" width=\\"2.00\\" height=\\"100.00\\" />
<rect x=\\"122.00\\" y=\\"0.00\\" width=\\"4.00\\" height=\\"100.00\\" />
<rect x=\\"132.00\\" y=\\"0.00\\" width=\\"4.00\\" height=\\"100.00\\" />
<rect x=\\"142.00\\" y=\\"0.00\\" width=\\"6.00\\" height=\\"100.00\\" />
<rect x=\\"150.00\\" y=\\"0.00\\" width=\\"2.00\\" height=\\"100.00\\" />
<rect x=\\"154.00\\" y=\\"0.00\\" width=\\"4.00\\" height=\\"100.00\\" />
<text x=\\"79.00\\" y=\\"115.40\\" text-anchor=\\"middle\\"
font-family=\\"Helvetica, sans-serif\\" font-size=\\"14.0\\" >
12345
</text>
</g>
</svg>"
`;
41 changes: 41 additions & 0 deletions test/e2e/transformations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,45 @@ describe('Symbology Transformations', () => {
expect(image).toMatchImageSnapshot()
})
})

describe('dot size', () => {
it('should set the default dot size to 0.8px in dotty mode', async () => {
const image = await createImageFile({
symbology: symbology.Barcode.DOTCODE,
outputOptions: symbology.Options.BARCODE_DOTTY_MODE
}, 'png', '12345')

expect(image).toMatchImageSnapshot()
})

it('should render the dot size as 1px in dotty mode', async () => {
const image = await createImageFile({
symbology: symbology.Barcode.DOTCODE,
outputOptions: symbology.Options.BARCODE_DOTTY_MODE,
dotSize: 1
}, 'png', '12345')

expect(image).toMatchImageSnapshot()
})
})

describe('Scalable Vector Graphics', () => {
it('should render an SVG', async () => {
const image = await createImageFile({
symbology: symbology.Barcode.CODE128,
}, 'svg', '12345')

expect(image).toMatchSnapshot()
})
})

describe('PostScript', () => {
it('should render an eps', async () => {
const image = await createImageFile({
symbology: symbology.Barcode.CODE128,
}, 'eps', '12345')

expect(image).toMatchSnapshot()
})
})
})

0 comments on commit 32b9b42

Please sign in to comment.