Class: ZplRenderer::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/zpl_renderer/options.rb

Overview

Normalized render options with validation for DPI, dimensions, and format.

Constant Summary collapse

SUPPORTED_DPI =
{
  203 => 8,
  300 => 12,
  600 => 24
}.freeze
SUPPORTED_DPMM =
SUPPORTED_DPI.invert.freeze
SUPPORTED_FORMATS =
i[png pdf].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format: :png, width: 4, height: 6, dpi: 203, dpmm: nil, index: 0, timeout: 30, max_output_bytes: 50 * 1024 * 1024, strict: true, unit: :inches, clip_to_print_width: false) ⇒ Options



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/zpl_renderer/options.rb', line 18

def initialize(
  format: :png,
  width: 4,
  height: 6,
  dpi: 203,
  dpmm: nil,
  index: 0,
  timeout: 30,
  max_output_bytes: 50 * 1024 * 1024,
  strict: true,
  unit: :inches,
  clip_to_print_width: false
)
  @format = format.to_sym
  @unit = unit.to_sym
  @index = Integer(index)
  @timeout = Float(timeout)
  @max_output_bytes = Integer(max_output_bytes)
  @strict = !!strict
  @clip_to_print_width = !!clip_to_print_width

  validate_format!
  validate_timeout!
  validate_index!

  if dpmm
    @dpmm = Integer(dpmm)
    raise ConfigurationError, "unsupported dpmm=#{@dpmm}; use 8, 12, or 24" unless SUPPORTED_DPMM.key?(@dpmm)

    @dpi = SUPPORTED_DPMM[@dpmm]
  else
    @dpi = Integer(dpi)
    raise ConfigurationError, "unsupported dpi=#{@dpi}; use 203, 300, or 600" unless SUPPORTED_DPI.key?(@dpi)

    @dpmm = SUPPORTED_DPI[@dpi]
  end

  @width_in, @height_in = normalize_dimensions(width, height)
  validate_dimensions!
end

Instance Attribute Details

#clip_to_print_width ⇒ Object (readonly)

Returns the value of attribute clip_to_print_width.



15
16
17
# File 'lib/zpl_renderer/options.rb', line 15

def clip_to_print_width
  @clip_to_print_width
end

#dpi ⇒ Object (readonly)

Returns the value of attribute dpi.



15
16
17
# File 'lib/zpl_renderer/options.rb', line 15

def dpi
  @dpi
end

#dpmm ⇒ Object (readonly)

Returns the value of attribute dpmm.



15
16
17
# File 'lib/zpl_renderer/options.rb', line 15

def dpmm
  @dpmm
end

#format ⇒ Object (readonly)

Returns the value of attribute format.



15
16
17
# File 'lib/zpl_renderer/options.rb', line 15

def format
  @format
end

#height_in ⇒ Object (readonly)

Returns the value of attribute height_in.



15
16
17
# File 'lib/zpl_renderer/options.rb', line 15

def height_in
  @height_in
end

#index ⇒ Object (readonly)

Returns the value of attribute index.



15
16
17
# File 'lib/zpl_renderer/options.rb', line 15

def index
  @index
end

#max_output_bytes ⇒ Object (readonly)

Returns the value of attribute max_output_bytes.



15
16
17
# File 'lib/zpl_renderer/options.rb', line 15

def max_output_bytes
  @max_output_bytes
end

#strict ⇒ Object (readonly)

Returns the value of attribute strict.



15
16
17
# File 'lib/zpl_renderer/options.rb', line 15

def strict
  @strict
end

#timeout ⇒ Object (readonly)

Returns the value of attribute timeout.



15
16
17
# File 'lib/zpl_renderer/options.rb', line 15

def timeout
  @timeout
end

#unit ⇒ Object (readonly)

Returns the value of attribute unit.



15
16
17
# File 'lib/zpl_renderer/options.rb', line 15

def unit
  @unit
end

#width_in ⇒ Object (readonly)

Returns the value of attribute width_in.



15
16
17
# File 'lib/zpl_renderer/options.rb', line 15

def width_in
  @width_in
end

Instance Method Details

#content_type ⇒ Object



82
83
84
# File 'lib/zpl_renderer/options.rb', line 82

def content_type
  format == :pdf ? "application/pdf" : "image/png"
end

#height_mm ⇒ Object



63
64
65
# File 'lib/zpl_renderer/options.rb', line 63

def height_mm
  height_in * 25.4
end

#pixel_height ⇒ Object



72
73
74
# File 'lib/zpl_renderer/options.rb', line 72

def pixel_height
  (height_mm * dpmm).ceil
end

#pixel_width ⇒ Object

Printer-dot canvas size used by the offline renderer (mm * dpmm, ceiled).



68
69
70
# File 'lib/zpl_renderer/options.rb', line 68

def pixel_width
  (width_mm * dpmm).ceil
end

#sanitize_canvas_dots ⇒ Object

Canvas width in dots used when widening narrow ^PW values, or nil when printer-style clipping is requested.



78
79
80
# File 'lib/zpl_renderer/options.rb', line 78

def sanitize_canvas_dots
  clip_to_print_width ? nil : pixel_width
end

#to_h ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/zpl_renderer/options.rb', line 86

def to_h
  {
    format: format,
    width_in: width_in,
    height_in: height_in,
    width_mm: width_mm,
    height_mm: height_mm,
    dpi: dpi,
    dpmm: dpmm,
    index: index,
    timeout: timeout,
    max_output_bytes: max_output_bytes,
    strict: strict,
    unit: unit,
    clip_to_print_width: clip_to_print_width
  }
end

#width_mm ⇒ Object



59
60
61
# File 'lib/zpl_renderer/options.rb', line 59

def width_mm
  width_in * 25.4
end