{"id":21494283,"url":"https://github.com/bew/zpl-scaler.rb","last_synced_at":"2026-02-11T09:07:59.608Z","repository":{"id":56899425,"uuid":"159319933","full_name":"bew/zpl-scaler.rb","owner":"bew","description":"Simple gem to scale ZPL label","archived":false,"fork":false,"pushed_at":"2019-02-04T11:50:18.000Z","size":71,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-12-07T02:33:11.056Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bew.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-11-27T10:46:27.000Z","updated_at":"2025-06-26T15:03:01.000Z","dependencies_parsed_at":"2022-08-21T01:50:28.033Z","dependency_job_id":null,"html_url":"https://github.com/bew/zpl-scaler.rb","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/bew/zpl-scaler.rb","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bew%2Fzpl-scaler.rb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bew%2Fzpl-scaler.rb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bew%2Fzpl-scaler.rb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bew%2Fzpl-scaler.rb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bew","download_url":"https://codeload.github.com/bew/zpl-scaler.rb/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bew%2Fzpl-scaler.rb/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29330858,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-11T06:13:03.264Z","status":"ssl_error","status_checked_at":"2026-02-11T06:12:55.843Z","response_time":97,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-23T15:49:41.112Z","updated_at":"2026-02-11T09:07:59.590Z","avatar_url":"https://github.com/bew.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ZplScaler - (new name: ZplTransformer)\n\n## Why\n\nOur Zebra printer has a 300dpi resolution, and can only handle ZPL that is to be printed by a 300dpi printer.\n\nSome shipping API are not configurable enough, and can return ZPL code that is to be printed by a 203dpi printer.\n\nWe needed a way to scale a ZPL code from 203dpi to 300dpi.\n\n\n## How it works\n\nIt works by parsing the ZPL commands and transform them through one or more transformers.\n\nThe ZPL command parser reads commands one by one, ignoring newlines and comments between each command.\nNOTE: the parser assumes the control char to be `^`, it doesn't support the command `^CC` to change the control char.\n\nA transformer is a class inheriting from `ZplScaler::Transformer::Base` and implementing the method `map_cmd(cmd)`\n\nTODO explain how a transformer is applied? \u0026 how to implem' a Transformer (takes a ZplCommand, and should return a ZplCommand, can return nil to delete a cmd)\n\n\n## Available transformers\n\n### `ZplScaler::Transformer::GenericScaler`\n\nThis transformer can scale to a given ratio, specific arguments of a set of commands.\n\nExample:\n\n```rb\ncode = '^XA^GB12,30,2,B^XZ',\nscaler = ZplScaler::Transformer::GenericScaler.new(1.2) # ratio: 1.2\nputs scaler.apply(code) # =\u003e \"^XA^GB14,36,2,B^XZ\"\n```\n\nList of commands that can be scaled currently:\n\n- `^MN` - Media Tracking\n- `^BY` - Bar Code Field Default\n- `^FO` - Field Origin\n- `^B2` - Interleaved 2 of 5 Bar Code\n- `^GB` - Graphic Box\n- `^BC` - Code 128 Bar Code (Subsets A, B, and C)\n\nThese commands have at least one argument that is a coordinate (in dots) and need to be recalculated (scaled).\nThe non-coordinate arguments and all other commands not listed above are not touched and left as is.\n\nThis transformer only scales a few ZPL commands we needed, feel free to send a PR to add support for a ZPL command you need to be scaled.\n\nNote: images that are embedded in the ZPL code are not supported.\n\n\n### `ZplScaler::Transformer::FontScaler`\n\nThis transformer can scale font commands (`^A` and `^CF`) to a given ratio.\n\nThe ZPL2 specification has two kind of fonts available: scalable and bitmap.\n- The scalable fonts don't have a fixed size, they can be easily scaled.\n- The bitmap fonts have a fixed base size (e.g: font `A` base `height`/`width` is `9`/`5` dots). They can only be scaled to a multiple of their font's base height/width.\n  This transformer implements a special algorithm to try to scale bitmap fonts more precisely, but keep in mind that it is inaccurate compared to a scalable font.\n\n#### What's so special about bitmap fonts\n\nGiven the font command `^ACR,9,9`, this will set the font `C` only for the next `^FD` field, with a field orientation `R` (rotated 90°) and a size of `9x9` dots for height and width.\nHowever the resulting font size will not be `9x9` dots as the base size of font `C` is `18x10`. The resulting size will be `18x10` since it must be a multiple of the base size of the font.\nSimilarly, if the input size is `20x15` the resulting size would be `36x20`.\n\n#### Scaling algorithm for bitmap fonts\n\n1. Normalize the given font height to the font's base height (e.g: size `20` becomes `36`).\n2. Find smallest font that can be the same size as the given font (e.g: font `A`'s base size is half of font `C`'s base size: `9x5` vs `18x10`). A smaller font means more result granularity when scaling.\n3. Scale the normalized given height to the given ratio.\n4. Compute the scaled width from the scaled height based on proportion of new font's base size.\n5. Normalize again the scaled sizes to the new font's base sizes.\n\n\n### `ZplScaler::Transformer::Pipeline`\n\nThis transformer allows composition of other transformers. It doesn't do anything on its own and allows you to apply multiple transformers in a row. Each transformer's output cmd is given as the input cmd of the next transformer unless the output is `nil`, in which case the cmd is skipped and following transformers are not called.\n\nExample: This will double the scale of `code`, by first applying the **generic scaler** then the **font scaler**.\n```rb\ncode = '^XA^GB12,30,2,B^AA,9,5^XZ',\npipeline = ZplScaler::Transformer::Pipeline.new([\n  ZplScaler::Transformer::GenericScaler.new(2.0),\n  ZplScaler::Transformer::FontScaler.new(2.0),\n])\npipeline.apply(code) # =\u003e \"^XA^GB24,60,2,B^AA,18,10^XZ\"\n```\n\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'zpl-scaler'\n```\n\nAnd then execute:\n\n```sh\n$ bundle\n```\n\nOr install it yourself as:\n\n```sh\n$ gem install zpl-scaler\n```\n\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/bew/zpl-scaler.\n\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbew%2Fzpl-scaler.rb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbew%2Fzpl-scaler.rb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbew%2Fzpl-scaler.rb/lists"}