Last active
March 5, 2024 14:37
-
-
Save timdw/391ecb62efd1dde9f6af017ead208ec9 to your computer and use it in GitHub Desktop.
(Experimental) Scales a Raw ZPL file from 203 DPI (dots per inch) to 300 DPI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
# author: Tim Walker | |
# date: 2016-11-11 | |
# original author: Tres Finocchiaro | |
# original date: 2015-05-22 | |
# license: Public Domain; Use as you wish. | |
# original source: https://gist.github.com/tresf/95f28644669a1e1970a7 | |
#> | |
<# | |
# Scales all integers found in a designated section | |
#> | |
function scaleSection([string]$cmd, [string]$section, [double]$scaleFactor) { | |
[bool]$isInt = $false | |
[int]$toInt = 0 | |
$parts = $section.Substring($cmd.length).Split(',') | |
for ($i = 0; $i -lt $parts.Count; $i++) { | |
$isInt = [int]::TryParse($parts[$i], [ref]$toInt) | |
if ($isInt) { | |
$eol = '' | |
if ($parts[$i].EndsWith("`r`n")) { | |
$eol = "`r`n" | |
} | |
elseif ($parts[$i].EndsWith("`n")) { | |
$eol = "`n" | |
} | |
elseif ($parts[$i].EndsWith("`r")) { | |
$eol = "`r" | |
} | |
$parts[$i] = ($scaleFactor * $toInt).ToString("000") + $eol | |
} | |
} | |
return $cmd + ($parts -join ',') | |
} | |
<# | |
# Scales text from a raw ZPL label file from 203 DPI to 300 DPI | |
#> | |
function scaleZPL([string]$inputFile, [double]$scaleFactor = 300/203) { | |
$sections = (Get-Content -path $inputFile -Delimiter `0).Split('^') | |
# ZPL commands to perform conversion on | |
$zplmatchcmds = @('FO', 'A[^,]*', 'LL', 'LH', 'GB', 'FB', 'B[^,]*') | |
$regexcmds = "^((" + ($zplmatchcmds -join ')|(') + ')).*' | |
# Can't do foreach here as it creates a copy of the array | |
for ($i = 0; $i -lt $sections.Count; $i++) { | |
$cmdmatch = $sections[$i] | Select-String -Pattern $regexcmds | |
if ($cmdmatch.Matches.Groups.Count -ne 0) { | |
$cmd = $cmdmatch.Matches.Groups[1].Value | |
$sections[$i] = scaleSection -cmd $cmd -section $sections[$i] -scaleFactor $scaleFactor | |
} | |
} | |
($sections -join '^') | |
} | |
# example usage | |
scaleZPL -inputFile 'c:\myfile.zpl' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment