Class: HexaPDF::Extras::GraphicObject::Zint
- Inherits:
-
Object
- Object
- HexaPDF::Extras::GraphicObject::Zint
- Defined in:
- lib/hexapdf/extras/graphic_object/zint.rb
Overview
Generates a barcode using the ruby-zint library that uses the libzint barcode generation library.
It implements the HexaPDF graphic object interface and can therefore easily be used via the :barcode
name:
canvas.draw(:barcode, width: 50, at: [10, 40], value: 'Hello!', symbology: :code128)
Except for a few keyword arguments all are passed through to ruby-zint, so everything that is supported by Zint::Barcode can be used. To make specifying symbologies easier, it is possible to use symbolic names instead of the constants, see #configure.
Examples
-
Linear barcode
#>pdf-canvas100 canvas.draw(:barcode, width: 60, at: [20, 45], value: '1123456', symbology: :upce) canvas.draw(:barcode, width: 60, at: [20, 5], value: 'Hello!', symbology: :code128)
-
Stacked barcode
#>pdf-canvas100 canvas.draw(:barcode, width: 80, at: [10, 40], symbology: :codablockf, value: 'Hello HexaPDF!', option_1: 3)
-
Composite barcode
#>pdf-canvas100 canvas.draw(:barcode, width: 80, at: [10, 20], symbology: :gs1_128_cc, value: '[99]1234-abcd', primary: "[01]03312345678903", option_1: 3)
-
2D barcode
#>pdf-canvas100 canvas.draw(:barcode, width: 80, at: [10, 10], symbology: :datamatrix, value: 'Hello HexaPDF!', option_3: 100, output_options: 0x0100)
Constant Summary collapse
- COLOR_CODES =
Maps the Zint color codes to HexaPDF color names.
{ 1 => "cyan", 2 => "blue", 3 => "magenta", 4 => "red", 5 => "yellow", 6 => "green", 7 => "black", 8 => "white", }
Instance Attribute Summary collapse
-
#at ⇒ Object
The position of the bottom-left corner of the barcode.
-
#font ⇒ Object
The font used when outputting strings.
-
#height ⇒ Object
The height of the barcode.
-
#width ⇒ Object
The width of resulting barcode.
-
#zint_kws ⇒ Object
The keyword arguments that are passed on to Zint::Barcode.new.
Class Method Summary collapse
-
.configure(**kwargs) ⇒ Object
Creates and configures a new Zint drawing support object.
Instance Method Summary collapse
-
#configure(at: nil, width: nil, height: nil, font: nil, symbology: nil, **zint_kws) ⇒ Object
Configures the Zint graphic object and returns self.
-
#draw(canvas) ⇒ Object
Draws the Zint::Barcode object onto the given canvas, with the bottom-left corner at the position specified by #at and the size specified by #width and #height.
-
#form_xobject(document) ⇒ Object
Creates a Form XObject for the given HexaPDF::Document that contains the visual representation of the barcode.
-
#initialize ⇒ Zint
constructor
Creates a Zint graphic object.
Constructor Details
#initialize ⇒ Zint
Creates a Zint graphic object.
124 125 126 127 128 129 130 |
# File 'lib/hexapdf/extras/graphic_object/zint.rb', line 124 def initialize @at = [0, 0] @width = nil @height = nil @font = 'Helvetica' @zint_kws = {} end |
Instance Attribute Details
#at ⇒ Object
The position of the bottom-left corner of the barcode.
Default: [0, 0].
Examples:
#>pdf-canvas100
canvas.draw(:barcode, height: 50, value: 'test', symbology: :code128)
canvas.draw(:barcode, height: 50, value: 'test', symbology: :code128, at: [20, 50])
65 66 67 |
# File 'lib/hexapdf/extras/graphic_object/zint.rb', line 65 def at @at end |
#font ⇒ Object
The font used when outputting strings.
Any font that is supported by the HexaPDF::Document::Layout module is supported.
Default: ‘Helvetica’.
Examples:
#>pdf-canvas100
canvas.draw(:barcode, height: 50, font: 'Courier', value: 'test', symbology: :code128)
116 117 118 |
# File 'lib/hexapdf/extras/graphic_object/zint.rb', line 116 def font @font end |
#height ⇒ Object
The height of the barcode.
For details and examples see #width.
Default: nil.
104 105 106 |
# File 'lib/hexapdf/extras/graphic_object/zint.rb', line 104 def height @height end |
#width ⇒ Object
The width of resulting barcode.
The resulting size of the barcode depends on whether width and #height are set:
-
If neither width nor height are set, the barcode uses the size returned by ruby-zint.
-
If both are set, the barcode is fit exactly into the given rectangle.
-
If either width or height is set, the other dimension is based on the set dimension so that the original aspect ratio is maintained.
Default: nil.
Examples:
-
No dimension set
#>pdf-canvas100 canvas.draw(:barcode, value: 'test', symbology: :code128)
-
One dimension set
#>pdf-canvas100 canvas.draw(:barcode, width: 60, value: 'test', symbology: :code128) canvas.draw(:barcode, height: 50, value: 'test', symbology: :code128, at: [0, 50])
-
Both dimensions set
#>pdf-canvas100 canvas.draw(:barcode, width: 60, height: 60, value: 'test', symbology: :code128)
97 98 99 |
# File 'lib/hexapdf/extras/graphic_object/zint.rb', line 97 def width @width end |
#zint_kws ⇒ Object
The keyword arguments that are passed on to Zint::Barcode.new.
Default: {}.
121 122 123 |
# File 'lib/hexapdf/extras/graphic_object/zint.rb', line 121 def zint_kws @zint_kws end |
Class Method Details
.configure(**kwargs) ⇒ Object
Creates and configures a new Zint drawing support object.
See #configure for the allowed keyword arguments.
52 53 54 |
# File 'lib/hexapdf/extras/graphic_object/zint.rb', line 52 def self.configure(**kwargs) new.configure(**kwargs) end |
Instance Method Details
#configure(at: nil, width: nil, height: nil, font: nil, symbology: nil, **zint_kws) ⇒ Object
Configures the Zint graphic object and returns self.
The following arguments are allowed:
- :at
-
The position of the bottom-left corner (see #at).
- :width
-
The width of the barcode (see #width).
- :height
-
The height of the barcode (see #height).
- :font
-
The font to use when outputting strings (see #font).
- :symbology
-
The type of barcode. Supports using symbols instead of constants, e.g.
:code128
instead of Zint::BARCODE_CODE128. - :zint_kws
-
Keyword arguments that are passed on to ruby-zint.
Any arguments not specified are not modified and retain their old value, see the attribute methods for the inital default values.
157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/hexapdf/extras/graphic_object/zint.rb', line 157 def configure(at: nil, width: nil, height: nil, font: nil, symbology: nil, **zint_kws) @at = at if at @width = width if width @height = height if height @font = font if font @zint_kws = zint_kws unless zint_kws.empty? @zint_kws[:symbology] = if symbology && symbology.kind_of?(Symbol) ::Zint.const_get("BARCODE_#{symbology.upcase}") elsif symbology symbology end self end |
#draw(canvas) ⇒ Object
Draws the Zint::Barcode object onto the given canvas, with the bottom-left corner at the position specified by #at and the size specified by #width and #height.
185 186 187 188 |
# File 'lib/hexapdf/extras/graphic_object/zint.rb', line 185 def draw(canvas) form = form_xobject(canvas.context.document) canvas.xobject(form, at: @at, width: @width, height: @height) end |
#form_xobject(document) ⇒ Object
Creates a Form XObject for the given HexaPDF::Document that contains the visual representation of the barcode.
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/hexapdf/extras/graphic_object/zint.rb', line 192 def form_xobject(document) = ::Zint::Barcode.new(**@zint_kws) vector = .to_vector height = vector.height form = document.add({Type: :XObject, Subtype: :Form, BBox: [0, 0, vector.width, height]}) canvas = form.canvas canvas.fill_color(.bgcolour).rectangle(0, 0, vector.width, height).fill vector.each_rectangle.group_by {|rect| rect.colour }.each do |color, rects| canvas.fill_color(COLOR_CODES.fetch(color, .fgcolour)) rects.each {|rect| canvas.rectangle(rect.x, height - rect.y, rect.width, -rect.height) } canvas.fill end vector.each_circle.group_by {|circle| circle.colour }.each do |color, circles| canvas.fill_color(COLOR_CODES.fetch(color, .fgcolour)) circles.each {|circle| canvas.circle(circle.x, height - circle.y, circle.diameter / 2.0) } canvas.fill end layout = canvas.context.document.layout vector.each_string do |string| fragment = layout.text_fragments(string.text, font: @font, font_size: string.fsize)[0] x = string.x + case string.halign when 0 then -fragment.width / 2.0 when 1 then 0 when 2 then -fragment.width end fragment.draw(canvas, x, height - string.y) end form end |