Skip to content

Instantly share code, notes, and snippets.

@LeeU1911
Forked from tresf/scale-zpl.js
Last active April 27, 2021 13:40
Show Gist options
  • Save LeeU1911/a2b61a26f852d9ee4798ef93ded9dba0 to your computer and use it in GitHub Desktop.
Save LeeU1911/a2b61a26f852d9ee4798ef93ded9dba0 to your computer and use it in GitHub Desktop.
(Experimental) Scales a Raw ZPL file from 203 DPI (dots per inch) to 300 DPI
/*
* author: Tres Finocchiaro
* date: 2015-05-22
* license: Public Domain; Use as you wish.
* source: http://qz.io
* updated: 2021-04-27 - Add 'BQN,2' to scale QR Code size (N is orientation and 2 is model so no need to scale that number)
*/
/*
* Scales text from a raw ZPL label from 203 DPI to 300 DPI
*/
function scaleZPL(rawCommands, scaleFactor) {
if (!scaleFactor) {
scaleFactor = 300/203;
}
var sections = rawCommands.split('^');
// ZPL commands to perform conversion on
var cmds = ['FO', 'A0', 'A@', 'LL', 'LH', 'GB', 'FB', 'BY', 'B3', 'FT', 'BQN,2'];
var output = '';
for (var i in cmds) {
for (var j in sections) {
if (sections[j].indexOf(cmds[i]) === 0) {
sections[j] = scaleSection(cmds[i], sections[j], scaleFactor);
}
}
}
return sections.join('^');
}
/*
* Scales all integers found in a designated section
*/
function scaleSection(cmd, section, scaleFactor) {
section = section.slice(cmd.length, section.length);
parts = section.split(',');
for (var p in parts) {
if (isInt(parts[p])) {
parts[p] = Math.round(scaleFactor * parts[p]);
}
}
return cmd + parts.join();
}
/*
* Checks if a string is an integer
*/
function isInt(value) {
return !isNaN(value) &&
parseInt(Number(value)) == value &&
!isNaN(parseInt(value, 10));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment