zpl_render 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ZplRender
4
+ # A 1-bit monochrome dot canvas at printer resolution.
5
+ # Pixel value 1 = black dot (ink), 0 = white. Stored one byte per pixel
6
+ # for fast random access via String#getbyte/#setbyte.
7
+ class Canvas
8
+ attr_reader :width, :height
9
+
10
+ ROTATIONS = %i[n r i b].freeze
11
+
12
+ def initialize(width, height)
13
+ raise ArgumentError, "canvas size must be positive (#{width}x#{height})" if width < 1 || height < 1
14
+
15
+ @width = width
16
+ @height = height
17
+ @bits = ("\x00" * (width * height)).b
18
+ end
19
+
20
+ def set(x, y, on = true)
21
+ return if x.negative? || y.negative? || x >= @width || y >= @height
22
+
23
+ @bits.setbyte(y * @width + x, on ? 1 : 0)
24
+ end
25
+
26
+ def toggle(x, y)
27
+ return if x.negative? || y.negative? || x >= @width || y >= @height
28
+
29
+ idx = y * @width + x
30
+ @bits.setbyte(idx, @bits.getbyte(idx) ^ 1)
31
+ end
32
+
33
+ def get(x, y)
34
+ return false if x.negative? || y.negative? || x >= @width || y >= @height
35
+
36
+ @bits.getbyte(y * @width + x) == 1
37
+ end
38
+
39
+ def fill_rect(x, y, w, h, color: :black, xor: false)
40
+ x0 = [x, 0].max
41
+ y0 = [y, 0].max
42
+ x1 = [x + w, @width].min
43
+ y1 = [y + h, @height].min
44
+ return if x0 >= x1 || y0 >= y1
45
+
46
+ value = color == :black ? 1 : 0
47
+ (y0...y1).each do |yy|
48
+ row = yy * @width
49
+ (x0...x1).each do |xx|
50
+ if xor
51
+ @bits.setbyte(row + xx, @bits.getbyte(row + xx) ^ 1)
52
+ else
53
+ @bits.setbyte(row + xx, value)
54
+ end
55
+ end
56
+ end
57
+ end
58
+
59
+ # Blit +src+ onto self with its (rotated) top-left corner at (x, y).
60
+ # Rotation is one of :n (0), :r (90 CW), :i (180), :b (270 CW).
61
+ # mode :paint -> src ink paints black, src background is transparent
62
+ # mode :erase -> src ink paints white (used for ^GB/^GC color W)
63
+ # mode :xor -> src ink inverts the canvas (ZPL ^FR field reverse)
64
+ def blit(src, x, y, rotation: :n, mode: :paint)
65
+ sw = src.width
66
+ sh = src.height
67
+ (0...sh).each do |sy|
68
+ (0...sw).each do |sx|
69
+ next unless src.get(sx, sy)
70
+
71
+ tx, ty = case rotation
72
+ when :n then [x + sx, y + sy]
73
+ when :r then [x + (sh - 1 - sy), y + sx]
74
+ when :i then [x + (sw - 1 - sx), y + (sh - 1 - sy)]
75
+ when :b then [x + sy, y + (sw - 1 - sx)]
76
+ else [x + sx, y + sy]
77
+ end
78
+ case mode
79
+ when :xor then toggle(tx, ty)
80
+ when :erase then set(tx, ty, false)
81
+ else set(tx, ty, true)
82
+ end
83
+ end
84
+ end
85
+ end
86
+
87
+ # Dimensions of +src+ after rotation, as [w, h].
88
+ def self.rotated_size(w, h, rotation)
89
+ %i[r b].include?(rotation) ? [h, w] : [w, h]
90
+ end
91
+
92
+ # Packed rows, 8 pixels per byte, MSB first (for PNG/PDF encoders).
93
+ def packed_rows
94
+ Array.new(@height) do |y|
95
+ row_offset = y * @width
96
+ bytes = ("\x00" * ((@width + 7) / 8)).b
97
+ (0...@width).each do |x|
98
+ next unless @bits.getbyte(row_offset + x) == 1
99
+
100
+ bytes.setbyte(x >> 3, bytes.getbyte(x >> 3) | (0x80 >> (x & 7)))
101
+ end
102
+ bytes
103
+ end
104
+ end
105
+
106
+ def to_ascii(on: "#", off: ".")
107
+ Array.new(@height) do |y|
108
+ Array.new(@width) { |x| get(x, y) ? on : off }.join
109
+ end.join("\n")
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,210 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "font_data"
4
+ require_relative "helvetica_metrics"
5
+ require_relative "truetype"
6
+
7
+ module ZplRender
8
+ # Text rendering with metrics and shapes matching Zebra output.
9
+ #
10
+ # Glyphs come from the bundled Liberation Sans Bold (SIL OFL), whose
11
+ # metrics match Arial/Helvetica Bold, rasterized by the pure-Ruby
12
+ # TrueType engine. Zebra's scalable font 0 is CG Triumvirate Bold
13
+ # Condensed; the calibration constants below (measured against Labelary
14
+ # reference renders) reproduce its geometry:
15
+ #
16
+ # - :scalable (font 0 and other scalable designators): proportional
17
+ # advances = hmtx/upm * ADVANCE_SCALE * w; cap height = CAP_SCALE * h.
18
+ # - :bitmap (fonts A-H): fixed-pitch cells, advance = cell width, glyph
19
+ # condensed to fit the cell. Font B is uppercase-only, like the
20
+ # hardware. (Real bitmap fonts are chunky dot-matrix faces; we render
21
+ # them with the vector face at cell metrics, like Labelary does.)
22
+ #
23
+ # If the bundled TTF cannot be loaded, an embedded public-domain 8x8
24
+ # bitmap font (font8x8, IBM VGA derived) is used as a fallback.
25
+ module Font
26
+ # designator => [base height, base width, inter-character gap] in dots
27
+ # (per the ZPL manual's bitmapped-fonts matrix table)
28
+ BASE_CELLS = {
29
+ "A" => [9, 5, 1], "B" => [11, 7, 2], "C" => [18, 10, 2],
30
+ "D" => [18, 10, 2], "E" => [28, 15, 5], "F" => [26, 13, 3],
31
+ "G" => [60, 40, 8], "H" => [21, 13, 6]
32
+ }.freeze
33
+ SCALABLE_BASE = [15, 12, 0].freeze
34
+
35
+ ADVANCE_SCALE = 0.86 # em width per ^A w unit, measured against Labelary
36
+ CAP_SCALE = 0.76 # cap height per ^A h unit, measured against Labelary
37
+ BITMAP_CAP_SCALE = 0.72 # matrix fonts reserve more cell for descenders
38
+
39
+ Face = Struct.new(:style, :char_h, :char_w, :gap, :upcase, keyword_init: true)
40
+
41
+ module_function
42
+
43
+ def vector
44
+ return @vector if defined?(@vector)
45
+
46
+ @vector = begin
47
+ TrueType.default
48
+ rescue StandardError
49
+ nil
50
+ end
51
+ end
52
+
53
+ def mono_vector
54
+ return @mono_vector if defined?(@mono_vector)
55
+
56
+ @mono_vector = begin
57
+ TrueType.mono
58
+ rescue StandardError
59
+ vector
60
+ end
61
+ end
62
+
63
+ def face_for(designator, height, width)
64
+ name = designator.to_s.upcase
65
+ name = "0" if name.empty?
66
+ style = BASE_CELLS.key?(name) ? :bitmap : :scalable
67
+ base_h, base_w, base_gap = BASE_CELLS[name] || SCALABLE_BASE
68
+
69
+ h = height && height.positive? ? height : nil
70
+ w = width && width.positive? ? width : nil
71
+ if style == :scalable
72
+ h ||= w || base_h
73
+ w ||= h
74
+ gap = 0
75
+ else
76
+ h ||= w ? (w * base_h / base_w) : base_h
77
+ w ||= (h * base_w.to_f / base_h).round
78
+ gap = [(base_gap.to_f * w / base_w).round, 1].max
79
+ end
80
+ Face.new(style: style, char_h: h.clamp(2, 32_000), char_w: w.clamp(2, 32_000),
81
+ gap: gap, upcase: name == "B")
82
+ end
83
+
84
+ # Pen advance for one character, in dots.
85
+ def advance(ch, face)
86
+ return face.char_w + face.gap if face.style == :bitmap
87
+
88
+ code = printable_code(ch)
89
+ if (tt = vector)
90
+ [(tt.advance(code) / tt.units_per_em.to_f * ADVANCE_SCALE * face.char_w).round, 1].max
91
+ else
92
+ afm = HELVETICA_WIDTHS[code - 32] || 556
93
+ [(afm / 1000.0 * ADVANCE_SCALE * face.char_w).round, 1].max
94
+ end
95
+ end
96
+
97
+ # Rendered width of a line of text, in dots.
98
+ def measure(text, face)
99
+ prepare(text, face).each_char.sum { |ch| advance(ch, face) }
100
+ end
101
+
102
+ # Height of one text line (canvas height and stacking step), in dots.
103
+ def line_height(face)
104
+ face.char_h
105
+ end
106
+
107
+ # Baseline offset from the top of the field, in dots.
108
+ def baseline(face)
109
+ scale = face.style == :bitmap ? BITMAP_CAP_SCALE : CAP_SCALE
110
+ (scale * face.char_h).round
111
+ end
112
+
113
+ def prepare(text, face)
114
+ face.upcase ? text.to_s.upcase : text.to_s
115
+ end
116
+
117
+ def printable_code(ch)
118
+ code = ch.ord
119
+ code < 32 || code == 127 ? "?".ord : code
120
+ end
121
+
122
+ # Render a single line of text into a fresh Canvas.
123
+ def render_line(text, face)
124
+ text = prepare(text, face)
125
+ width = [measure(text, face), 1].max
126
+ canvas = Canvas.new(width, [line_height(face), 1].max)
127
+ draw_line(canvas, 0, 0, text, face)
128
+ canvas
129
+ end
130
+
131
+ def draw_line(canvas, x, y, text, face)
132
+ pen = x
133
+ prepare(text, face).each_char do |ch|
134
+ adv = advance(ch, face)
135
+ draw_char(canvas, pen, y, ch, face, adv)
136
+ pen += adv
137
+ end
138
+ pen - x
139
+ end
140
+
141
+ def draw_char(canvas, x, y, ch, face, adv)
142
+ code = printable_code(ch)
143
+ tt = face.style == :bitmap ? mono_vector : vector
144
+ if tt
145
+ draw_vector_char(canvas, x, y, code, face, adv, tt)
146
+ else
147
+ draw_fallback_char(canvas, x, y, code, face, adv)
148
+ end
149
+ end
150
+
151
+ def draw_vector_char(canvas, x, y, code, face, adv, tt)
152
+ upm = tt.units_per_em.to_f
153
+ scale_y = baseline(face) / tt.cap_height.to_f
154
+ if face.style == :scalable
155
+ scale_x = ADVANCE_SCALE * face.char_w / upm
156
+ pen_x = x
157
+ else
158
+ # scale the mono advance to the matrix cell width, glyph centered
159
+ natural = tt.advance(code) / upm # em units
160
+ scale_x = face.char_w / natural / upm
161
+ glyph_adv = (tt.advance(code) * scale_x).round
162
+ pen_x = x + (adv - face.gap - glyph_adv) / 2
163
+ end
164
+ glyph = tt.glyph_bitmap(code, scale_x, scale_y)
165
+ return unless glyph
166
+
167
+ base_y = y + baseline(face)
168
+ canvas.blit(glyph.canvas, pen_x + glyph.left, base_y - glyph.top)
169
+ end
170
+
171
+ # ---- 8x8 bitmap fallback -------------------------------------------------
172
+
173
+ # Per-glyph [leftmost ink column, ink width in columns], nil for blank.
174
+ GLYPH_EXTENTS = FONT8X8.map do |glyph|
175
+ min = 8
176
+ max = -1
177
+ glyph.each do |row|
178
+ next if row.zero?
179
+
180
+ 8.times do |bit|
181
+ next unless (row >> bit) & 1 == 1
182
+
183
+ min = bit if bit < min
184
+ max = bit if bit > max
185
+ end
186
+ end
187
+ max >= 0 ? [min, max - min + 1].freeze : nil
188
+ end.freeze
189
+
190
+ def draw_fallback_char(canvas, x, y, code, face, adv)
191
+ glyph = FONT8X8[code]
192
+ extents = GLYPH_EXTENTS[code]
193
+ return unless glyph && extents
194
+
195
+ cell_h = (face.char_h * 0.99).round
196
+ bearing = [(adv * 0.1).round, 1].max
197
+ target_w = [adv - 2 * bearing, 1].max
198
+ left, src_w = face.style == :bitmap ? [0, 8] : extents
199
+ (0...cell_h).each do |py|
200
+ row = glyph[py * 8 / cell_h]
201
+ next if row.zero?
202
+
203
+ (0...target_w).each do |px|
204
+ bit = left + px * src_w / target_w
205
+ canvas.set(x + bearing + px, y + py) if (row >> bit) & 1 == 1
206
+ end
207
+ end
208
+ end
209
+ end
210
+ end
@@ -0,0 +1,153 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ZplRender
4
+
5
+ # 8x8 monochrome bitmap font covering ASCII 0x00-0x7F.
6
+
7
+ # Derived from font8x8 by Daniel Hepper (public domain),
8
+
9
+ # based on public-domain IBM VGA fonts. Each glyph is 8 bytes,
10
+
11
+ # one per row, LSB = leftmost pixel.
12
+
13
+ FONT8X8 = [
14
+
15
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
16
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
17
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
18
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
19
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
20
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
21
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
22
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
23
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
24
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
25
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
26
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
27
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
28
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
29
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
30
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
31
+ # ^ 0x00-0x0f
32
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
33
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
34
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
35
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
36
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
37
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
38
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
39
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
40
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
41
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
42
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
43
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
44
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
45
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
46
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
47
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
48
+ # ^ 0x10-0x1f
49
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
50
+ [0x18, 0x3C, 0x3C, 0x18, 0x18, 0x00, 0x18, 0x00],
51
+ [0x36, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
52
+ [0x36, 0x36, 0x7F, 0x36, 0x7F, 0x36, 0x36, 0x00],
53
+ [0x0C, 0x3E, 0x03, 0x1E, 0x30, 0x1F, 0x0C, 0x00],
54
+ [0x00, 0x63, 0x33, 0x18, 0x0C, 0x66, 0x63, 0x00],
55
+ [0x1C, 0x36, 0x1C, 0x6E, 0x3B, 0x33, 0x6E, 0x00],
56
+ [0x06, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00],
57
+ [0x18, 0x0C, 0x06, 0x06, 0x06, 0x0C, 0x18, 0x00],
58
+ [0x06, 0x0C, 0x18, 0x18, 0x18, 0x0C, 0x06, 0x00],
59
+ [0x00, 0x66, 0x3C, 0xFF, 0x3C, 0x66, 0x00, 0x00],
60
+ [0x00, 0x0C, 0x0C, 0x3F, 0x0C, 0x0C, 0x00, 0x00],
61
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x0C, 0x06],
62
+ [0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00],
63
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x0C, 0x00],
64
+ [0x60, 0x30, 0x18, 0x0C, 0x06, 0x03, 0x01, 0x00],
65
+ # ^ 0x20-0x2f
66
+ [0x3E, 0x63, 0x73, 0x7B, 0x6F, 0x67, 0x3E, 0x00],
67
+ [0x0C, 0x0E, 0x0C, 0x0C, 0x0C, 0x0C, 0x3F, 0x00],
68
+ [0x1E, 0x33, 0x30, 0x1C, 0x06, 0x33, 0x3F, 0x00],
69
+ [0x1E, 0x33, 0x30, 0x1C, 0x30, 0x33, 0x1E, 0x00],
70
+ [0x38, 0x3C, 0x36, 0x33, 0x7F, 0x30, 0x78, 0x00],
71
+ [0x3F, 0x03, 0x1F, 0x30, 0x30, 0x33, 0x1E, 0x00],
72
+ [0x1C, 0x06, 0x03, 0x1F, 0x33, 0x33, 0x1E, 0x00],
73
+ [0x3F, 0x33, 0x30, 0x18, 0x0C, 0x0C, 0x0C, 0x00],
74
+ [0x1E, 0x33, 0x33, 0x1E, 0x33, 0x33, 0x1E, 0x00],
75
+ [0x1E, 0x33, 0x33, 0x3E, 0x30, 0x18, 0x0E, 0x00],
76
+ [0x00, 0x0C, 0x0C, 0x00, 0x00, 0x0C, 0x0C, 0x00],
77
+ [0x00, 0x0C, 0x0C, 0x00, 0x00, 0x0C, 0x0C, 0x06],
78
+ [0x18, 0x0C, 0x06, 0x03, 0x06, 0x0C, 0x18, 0x00],
79
+ [0x00, 0x00, 0x3F, 0x00, 0x00, 0x3F, 0x00, 0x00],
80
+ [0x06, 0x0C, 0x18, 0x30, 0x18, 0x0C, 0x06, 0x00],
81
+ [0x1E, 0x33, 0x30, 0x18, 0x0C, 0x00, 0x0C, 0x00],
82
+ # ^ 0x30-0x3f
83
+ [0x3E, 0x63, 0x7B, 0x7B, 0x7B, 0x03, 0x1E, 0x00],
84
+ [0x0C, 0x1E, 0x33, 0x33, 0x3F, 0x33, 0x33, 0x00],
85
+ [0x3F, 0x66, 0x66, 0x3E, 0x66, 0x66, 0x3F, 0x00],
86
+ [0x3C, 0x66, 0x03, 0x03, 0x03, 0x66, 0x3C, 0x00],
87
+ [0x1F, 0x36, 0x66, 0x66, 0x66, 0x36, 0x1F, 0x00],
88
+ [0x7F, 0x46, 0x16, 0x1E, 0x16, 0x46, 0x7F, 0x00],
89
+ [0x7F, 0x46, 0x16, 0x1E, 0x16, 0x06, 0x0F, 0x00],
90
+ [0x3C, 0x66, 0x03, 0x03, 0x73, 0x66, 0x7C, 0x00],
91
+ [0x33, 0x33, 0x33, 0x3F, 0x33, 0x33, 0x33, 0x00],
92
+ [0x1E, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00],
93
+ [0x78, 0x30, 0x30, 0x30, 0x33, 0x33, 0x1E, 0x00],
94
+ [0x67, 0x66, 0x36, 0x1E, 0x36, 0x66, 0x67, 0x00],
95
+ [0x0F, 0x06, 0x06, 0x06, 0x46, 0x66, 0x7F, 0x00],
96
+ [0x63, 0x77, 0x7F, 0x7F, 0x6B, 0x63, 0x63, 0x00],
97
+ [0x63, 0x67, 0x6F, 0x7B, 0x73, 0x63, 0x63, 0x00],
98
+ [0x1C, 0x36, 0x63, 0x63, 0x63, 0x36, 0x1C, 0x00],
99
+ # ^ 0x40-0x4f
100
+ [0x3F, 0x66, 0x66, 0x3E, 0x06, 0x06, 0x0F, 0x00],
101
+ [0x1E, 0x33, 0x33, 0x33, 0x3B, 0x1E, 0x38, 0x00],
102
+ [0x3F, 0x66, 0x66, 0x3E, 0x36, 0x66, 0x67, 0x00],
103
+ [0x1E, 0x33, 0x07, 0x0E, 0x38, 0x33, 0x1E, 0x00],
104
+ [0x3F, 0x2D, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00],
105
+ [0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x3F, 0x00],
106
+ [0x33, 0x33, 0x33, 0x33, 0x33, 0x1E, 0x0C, 0x00],
107
+ [0x63, 0x63, 0x63, 0x6B, 0x7F, 0x77, 0x63, 0x00],
108
+ [0x63, 0x63, 0x36, 0x1C, 0x1C, 0x36, 0x63, 0x00],
109
+ [0x33, 0x33, 0x33, 0x1E, 0x0C, 0x0C, 0x1E, 0x00],
110
+ [0x7F, 0x63, 0x31, 0x18, 0x4C, 0x66, 0x7F, 0x00],
111
+ [0x1E, 0x06, 0x06, 0x06, 0x06, 0x06, 0x1E, 0x00],
112
+ [0x03, 0x06, 0x0C, 0x18, 0x30, 0x60, 0x40, 0x00],
113
+ [0x1E, 0x18, 0x18, 0x18, 0x18, 0x18, 0x1E, 0x00],
114
+ [0x08, 0x1C, 0x36, 0x63, 0x00, 0x00, 0x00, 0x00],
115
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF],
116
+ # ^ 0x50-0x5f
117
+ [0x0C, 0x0C, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00],
118
+ [0x00, 0x00, 0x1E, 0x30, 0x3E, 0x33, 0x6E, 0x00],
119
+ [0x07, 0x06, 0x06, 0x3E, 0x66, 0x66, 0x3B, 0x00],
120
+ [0x00, 0x00, 0x1E, 0x33, 0x03, 0x33, 0x1E, 0x00],
121
+ [0x38, 0x30, 0x30, 0x3e, 0x33, 0x33, 0x6E, 0x00],
122
+ [0x00, 0x00, 0x1E, 0x33, 0x3f, 0x03, 0x1E, 0x00],
123
+ [0x1C, 0x36, 0x06, 0x0f, 0x06, 0x06, 0x0F, 0x00],
124
+ [0x00, 0x00, 0x6E, 0x33, 0x33, 0x3E, 0x30, 0x1F],
125
+ [0x07, 0x06, 0x36, 0x6E, 0x66, 0x66, 0x67, 0x00],
126
+ [0x0C, 0x00, 0x0E, 0x0C, 0x0C, 0x0C, 0x1E, 0x00],
127
+ [0x30, 0x00, 0x30, 0x30, 0x30, 0x33, 0x33, 0x1E],
128
+ [0x07, 0x06, 0x66, 0x36, 0x1E, 0x36, 0x67, 0x00],
129
+ [0x0E, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00],
130
+ [0x00, 0x00, 0x33, 0x7F, 0x7F, 0x6B, 0x63, 0x00],
131
+ [0x00, 0x00, 0x1F, 0x33, 0x33, 0x33, 0x33, 0x00],
132
+ [0x00, 0x00, 0x1E, 0x33, 0x33, 0x33, 0x1E, 0x00],
133
+ # ^ 0x60-0x6f
134
+ [0x00, 0x00, 0x3B, 0x66, 0x66, 0x3E, 0x06, 0x0F],
135
+ [0x00, 0x00, 0x6E, 0x33, 0x33, 0x3E, 0x30, 0x78],
136
+ [0x00, 0x00, 0x3B, 0x6E, 0x66, 0x06, 0x0F, 0x00],
137
+ [0x00, 0x00, 0x3E, 0x03, 0x1E, 0x30, 0x1F, 0x00],
138
+ [0x08, 0x0C, 0x3E, 0x0C, 0x0C, 0x2C, 0x18, 0x00],
139
+ [0x00, 0x00, 0x33, 0x33, 0x33, 0x33, 0x6E, 0x00],
140
+ [0x00, 0x00, 0x33, 0x33, 0x33, 0x1E, 0x0C, 0x00],
141
+ [0x00, 0x00, 0x63, 0x6B, 0x7F, 0x7F, 0x36, 0x00],
142
+ [0x00, 0x00, 0x63, 0x36, 0x1C, 0x36, 0x63, 0x00],
143
+ [0x00, 0x00, 0x33, 0x33, 0x33, 0x3E, 0x30, 0x1F],
144
+ [0x00, 0x00, 0x3F, 0x19, 0x0C, 0x26, 0x3F, 0x00],
145
+ [0x38, 0x0C, 0x0C, 0x07, 0x0C, 0x0C, 0x38, 0x00],
146
+ [0x18, 0x18, 0x18, 0x00, 0x18, 0x18, 0x18, 0x00],
147
+ [0x07, 0x0C, 0x0C, 0x38, 0x0C, 0x0C, 0x07, 0x00],
148
+ [0x6E, 0x3B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
149
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
150
+ # ^ 0x70-0x7f
151
+ ].each(&:freeze).freeze
152
+
153
+ end
@@ -0,0 +1,102 @@
1
+ Digitized data copyright (c) 2010 Google Corporation
2
+ with Reserved Font Arimo, Tinos and Cousine.
3
+ Copyright (c) 2012 Red Hat, Inc.
4
+ with Reserved Font Name Liberation.
5
+
6
+ This Font Software is licensed under the SIL Open Font License,
7
+ Version 1.1.
8
+
9
+ This license is copied below, and is also available with a FAQ at:
10
+ http://scripts.sil.org/OFL
11
+
12
+ SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
13
+
14
+ PREAMBLE The goals of the Open Font License (OFL) are to stimulate
15
+ worldwide development of collaborative font projects, to support the font
16
+ creation efforts of academic and linguistic communities, and to provide
17
+ a free and open framework in which fonts may be shared and improved in
18
+ partnership with others.
19
+
20
+ The OFL allows the licensed fonts to be used, studied, modified and
21
+ redistributed freely as long as they are not sold by themselves.
22
+ The fonts, including any derivative works, can be bundled, embedded,
23
+ redistributed and/or sold with any software provided that any reserved
24
+ names are not used by derivative works. The fonts and derivatives,
25
+ however, cannot be released under any other type of license. The
26
+ requirement for fonts to remain under this license does not apply to
27
+ any document created using the fonts or their derivatives.
28
+
29
+
30
+
31
+ DEFINITIONS
32
+ "Font Software" refers to the set of files released by the Copyright
33
+ Holder(s) under this license and clearly marked as such.
34
+ This may include source files, build scripts and documentation.
35
+
36
+ "Reserved Font Name" refers to any names specified as such after the
37
+ copyright statement(s).
38
+
39
+ "Original Version" refers to the collection of Font Software components
40
+ as distributed by the Copyright Holder(s).
41
+
42
+ "Modified Version" refers to any derivative made by adding to, deleting,
43
+ or substituting ? in part or in whole ?
44
+ any of the components of the Original Version, by changing formats or
45
+ by porting the Font Software to a new environment.
46
+
47
+ "Author" refers to any designer, engineer, programmer, technical writer
48
+ or other person who contributed to the Font Software.
49
+
50
+
51
+ PERMISSION & CONDITIONS
52
+
53
+ Permission is hereby granted, free of charge, to any person obtaining a
54
+ copy of the Font Software, to use, study, copy, merge, embed, modify,
55
+ redistribute, and sell modified and unmodified copies of the Font
56
+ Software, subject to the following conditions:
57
+
58
+ 1) Neither the Font Software nor any of its individual components,in
59
+ Original or Modified Versions, may be sold by itself.
60
+
61
+ 2) Original or Modified Versions of the Font Software may be bundled,
62
+ redistributed and/or sold with any software, provided that each copy
63
+ contains the above copyright notice and this license. These can be
64
+ included either as stand-alone text files, human-readable headers or
65
+ in the appropriate machine-readable metadata fields within text or
66
+ binary files as long as those fields can be easily viewed by the user.
67
+
68
+ 3) No Modified Version of the Font Software may use the Reserved Font
69
+ Name(s) unless explicit written permission is granted by the
70
+ corresponding Copyright Holder. This restriction only applies to the
71
+ primary font name as presented to the users.
72
+
73
+ 4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
74
+ Software shall not be used to promote, endorse or advertise any
75
+ Modified Version, except to acknowledge the contribution(s) of the
76
+ Copyright Holder(s) and the Author(s) or with their explicit written
77
+ permission.
78
+
79
+ 5) The Font Software, modified or unmodified, in part or in whole, must
80
+ be distributed entirely under this license, and must not be distributed
81
+ under any other license. The requirement for fonts to remain under
82
+ this license does not apply to any document created using the Font
83
+ Software.
84
+
85
+
86
+
87
+ TERMINATION
88
+ This license becomes null and void if any of the above conditions are not met.
89
+
90
+
91
+
92
+ DISCLAIMER
93
+ THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
94
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
95
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
96
+ OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
97
+ COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
98
+ INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
99
+ DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
100
+ FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER
101
+ DEALINGS IN THE FONT SOFTWARE.
102
+
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "zlib"
4
+
5
+ module ZplRender
6
+ # Decodes ZPL graphic data (^GF and ~DG) into a Canvas.
7
+ #
8
+ # Supports:
9
+ # - ASCII hex data, including Zebra's run-length compression scheme
10
+ # (G-Y repeat counts, g-z counts in multiples of 20, ',' zero-fill
11
+ # to end of row, '!' one-fill to end of row, ':' repeat previous row)
12
+ # - :B64: (base64) and :Z64: (base64 + zlib deflate) wrapped data
13
+ # - raw binary data (^GFB)
14
+ module GraphicField
15
+ module_function
16
+
17
+ # bytes_per_row determines the pixel width (8 px per byte).
18
+ def decode(data, bytes_per_row, total_bytes)
19
+ return Canvas.new(1, 1) if bytes_per_row.to_i < 1 || total_bytes.to_i < 1
20
+
21
+ rows = (total_bytes.to_f / bytes_per_row).ceil
22
+ raw = if data =~ /\A:?([BZ])64:/
23
+ decode_b64_z64(data)
24
+ elsif data.encoding == Encoding::BINARY && data =~ /[^0-9A-Fa-f,:!\sG-Yg-z]/n
25
+ data # raw binary (^GFB)
26
+ else
27
+ decode_ascii_hex(data, bytes_per_row)
28
+ end
29
+ to_canvas(raw, bytes_per_row, rows)
30
+ end
31
+
32
+ def decode_b64_z64(data)
33
+ m = data.match(/\A:?([BZ])64:([A-Za-z0-9+\/=\s]*):?[0-9A-Fa-f]{0,4}\z/m)
34
+ return "" unless m
35
+
36
+ decoded = m[2].unpack1("m") # lenient base64, same as Base64.decode64
37
+ m[1] == "Z" ? Zlib::Inflate.inflate(decoded) : decoded
38
+ rescue Zlib::Error
39
+ ""
40
+ end
41
+
42
+ # Expand Zebra RLE ASCII hex into raw bytes.
43
+ def decode_ascii_hex(data, bytes_per_row)
44
+ nibbles_per_row = bytes_per_row * 2
45
+ out = []
46
+ row = []
47
+ prev_row = nil
48
+ count = 0
49
+ i = 0
50
+ flush = lambda do
51
+ row << 0 while row.length < nibbles_per_row
52
+ out.concat(row[0, nibbles_per_row])
53
+ prev_row = row[0, nibbles_per_row]
54
+ row = []
55
+ end
56
+
57
+ while i < data.length
58
+ ch = data[i]
59
+ i += 1
60
+ case ch
61
+ when /[0-9A-Fa-f]/
62
+ nib = ch.to_i(16)
63
+ (count.zero? ? 1 : count).times { row << nib }
64
+ count = 0
65
+ flush.call if row.length >= nibbles_per_row
66
+ when "G".."Y"
67
+ count += ch.ord - "G".ord + 1
68
+ when "g".."z"
69
+ count += (ch.ord - "g".ord + 1) * 20
70
+ when ","
71
+ row << 0 while row.length < nibbles_per_row
72
+ flush.call
73
+ count = 0
74
+ when "!"
75
+ row << 0xF while row.length < nibbles_per_row
76
+ flush.call
77
+ count = 0
78
+ when ":"
79
+ flush.call unless row.empty?
80
+ if prev_row
81
+ out.concat(prev_row)
82
+ else
83
+ out.concat(Array.new(nibbles_per_row, 0))
84
+ end
85
+ count = 0
86
+ end
87
+ # whitespace and anything else is ignored
88
+ end
89
+ flush.call unless row.empty?
90
+
91
+ bytes = +""
92
+ out.each_slice(2) { |hi, lo| bytes << ((hi << 4) | (lo || 0)).chr }
93
+ bytes.b
94
+ end
95
+
96
+ def to_canvas(raw, bytes_per_row, rows)
97
+ width = bytes_per_row * 8
98
+ canvas = Canvas.new(width, [rows, 1].max)
99
+ rows.times do |y|
100
+ bytes_per_row.times do |bx|
101
+ byte = raw.getbyte(y * bytes_per_row + bx)
102
+ next if byte.nil? || byte.zero?
103
+
104
+ 8.times do |bit|
105
+ canvas.set(bx * 8 + bit, y) if (byte >> (7 - bit)) & 1 == 1
106
+ end
107
+ end
108
+ end
109
+ canvas
110
+ end
111
+ end
112
+ end