class ZplScaler::Scaler

TODO: doc It works by parsing ZPL commands, then edit the parameters of specific commands to scale the coordinates to the new dpi

NOTE: Cannot work for embedded images

Constants

COMMANDS_PARAM_INDEXES_TO_SCALE

Public Class Methods

ratio_scale(zpl_content, scale_ratio) click to toggle source
# File lib/zpl-scaler.rb, line 143
def self.ratio_scale(zpl_content, scale_ratio)
  reader = ZplReader.new zpl_content
  scaled_zpl = StringIO.new

  reader.each_command do |cmd|
    scale_cmd!(cmd, scale_ratio)
    scaled_zpl << cmd.to_zpl_string
  end

  scaled_zpl.string
end

Protected Class Methods

cmd_need_scale?(cmd) click to toggle source
# File lib/zpl-scaler.rb, line 157
def self.cmd_need_scale?(cmd)
  !!COMMANDS_PARAM_INDEXES_TO_SCALE[cmd.name]
end
param_to_i?(param) click to toggle source

Returns an integer converted from the string param, or nil if it cannot be converted.

# File lib/zpl-scaler.rb, line 177
def self.param_to_i?(param)
  begin
    Integer(param)
  rescue ArgumentError # raised when *param* string cannot be converted to Integer
    nil
  end
end
scale_cmd!(cmd, scale_ratio) click to toggle source
# File lib/zpl-scaler.rb, line 161
def self.scale_cmd!(cmd, scale_ratio)
  return unless cmd_need_scale? cmd

  cmd_params = cmd.params

  param_indexes_to_scale = COMMANDS_PARAM_INDEXES_TO_SCALE[cmd.name]
  param_indexes_to_scale.each do |param_index|

    if (param_s = cmd_params[param_index]) && (param_i = param_to_i?(param_s))
      cmd_params[param_index] = (param_i * scale_ratio).to_i
    end
  end
end