Skip to content

Commit

Permalink
feat(rotation): implements rotations #31
Browse files Browse the repository at this point in the history
  • Loading branch information
jshor committed Mar 29, 2021
1 parent 993699b commit 11b69e6
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 11 deletions.
3 changes: 2 additions & 1 deletion docs/docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ 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` |
| `primary` | `String` | Primary message data for more complex symbols. | No | `null` |v
| `rotation` | `Number` | Clockwise rotation of the barcode. Valid values are `0`, `90`, `180`, or `270`. | No | `0` |
| `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
12 changes: 8 additions & 4 deletions src/binding/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ namespace symbology {
/**
* Renders symbology and returns an object with PNG bitmap data, EPS, or SVG XML.
*/
Local<Object> createStreamHandle(Isolate* isolate, zint_symbol *symbol, uint8_t *data, char *str) {
Local<Object> createStreamHandle(Isolate* isolate, zint_symbol *symbol, uint8_t *data, char *str, int rotate_angle) {
int status_code;

if ((symbol->output_options & BARCODE_STDOUT) != 0) {
status_code = ZBarcode_Encode_and_Print(symbol, data, 0, 0);
status_code = ZBarcode_Encode_and_Print(symbol, data, 0, rotate_angle);
} else {
status_code = ZBarcode_Encode_and_Buffer(symbol, data, 0, 0);
status_code = ZBarcode_Encode_and_Buffer(symbol, data, 0, rotate_angle);
}

v8::Local<v8::Object> obj = Object::New(isolate);
Expand Down Expand Up @@ -165,9 +165,13 @@ namespace symbology {

struct zint_symbol *symbol = getSymbolFromArgs(context, args);

// parse `rotation` angle argument
int rotate_angle;
rotate_angle = (int)args[18]->NumberValue(context).FromJust();

Nan::Utf8String data(args[0]);

obj = createStreamHandle(isolate, symbol, (unsigned char*)*data, (char*)*data);
obj = createStreamHandle(isolate, symbol, (unsigned char*)*data, (char*)*data, rotate_angle);

ZBarcode_Delete(symbol);
args.GetReturnValue().Set(obj);
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ const defaultConfig = {
showHumanReadableText: true,
encoding: exp.Encoding.DATA_MODE,
eci: 0,
primary: ''
primary: '',
rotation: 0
}

/**
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 @@ -36,7 +36,8 @@ describe('Binary methods', () => {
encoding: Encoding.DATA_MODE,
eci: 0,
primary: '',
text: 'example text'
text: 'example text',
rotation: 0
}
const barcodeData = 'primary data'

Expand All @@ -60,7 +61,8 @@ describe('Binary methods', () => {
'example text',
symbol.encoding,
symbol.eci,
symbol.primary
symbol.primary,
symbol.rotation
)
})

Expand All @@ -86,7 +88,8 @@ describe('Binary methods', () => {
encoding: Encoding.DATA_MODE,
eci: 0,
primary: '',
text: ''
text: '',
rotation: 0
}
const barcodeData = 'primary data'

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

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.
32 changes: 32 additions & 0 deletions test/e2e/transformations.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const symbology = require('../../src')

describe('Symbology Transformations', () => {
describe('rotations', () => {
it('should rotate the symbol 90 degrees clockwise', async () => {
const image = await createImageFile({
symbology: symbology.Barcode.CODE128,
rotation: 90
}, 'png', '12345')

expect(image).toMatchImageSnapshot()
})

it('should rotate the symbol 180 degrees clockwise', async () => {
const image = await createImageFile({
symbology: symbology.Barcode.CODE128,
rotation: 180
}, 'png', '12345')

expect(image).toMatchImageSnapshot()
})

it('should rotate the symbol 270 degrees clockwise', async () => {
const image = await createImageFile({
symbology: symbology.Barcode.CODE128,
rotation: 270
}, 'png', '12345')

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

0 comments on commit 11b69e6

Please sign in to comment.