Class: Prawn::QRCode::Renderer
- Inherits:
-
Object
- Object
- Prawn::QRCode::Renderer
- Defined in:
- lib/prawn/qrcode.rb
Overview
Renderer is responsible for actually rendering a QR code to pdf
Constant Summary collapse
- RENDER_OPTS =
%I[dot pos stroke foreground_color background_color stroke_color margin align debug extent].freeze
Instance Attribute Summary collapse
- #qr_code ⇒ Object
- #stroke ⇒ Object readonly
Instance Method Summary collapse
- #align(bounding_box) ⇒ Object
- #background_color ⇒ Object
- #dot ⇒ Object
- #extent ⇒ Object
- #foreground_color ⇒ Object
-
#initialize(qr_code, **options) ⇒ Renderer
constructor
creates a new renderer for the given QR code.
- #margin ⇒ Object
- #margin_size ⇒ Object
-
#render(pdf) ⇒ Object
rubocop:disable Metrics/AbcSize.
- #stroke_color ⇒ Object
Constructor Details
#initialize(qr_code, **options) ⇒ Renderer
creates a new renderer for the given QR code
Options :dot and :extent are mutually exclusive.
124 125 126 127 128 129 130 |
# File 'lib/prawn/qrcode.rb', line 124 def initialize(qr_code, **) raise QRCodeError, 'Specify either :dot or :extent, not both' if .key?(:dot) && .key?(:extent) @stroke = true @qr_code = qr_code .select { |k, _v| RENDER_OPTS.include?(k) }.each { |k, v| send("#{k}=", v) } end |
Instance Attribute Details
#qr_code ⇒ Object
100 101 102 |
# File 'lib/prawn/qrcode.rb', line 100 def qr_code @qr_code end |
#stroke ⇒ Object (readonly)
138 139 140 |
# File 'lib/prawn/qrcode.rb', line 138 def stroke @stroke end |
Instance Method Details
#align(bounding_box) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/prawn/qrcode.rb', line 165 def align(bounding_box) rlim = bounding_box.right case @align when :center @pos[0] = (rlim / 2) - (extent / 2) when :right @pos[0] = rlim - extent when :left @pos[0] = 0 end end |
#background_color ⇒ Object
144 145 146 |
# File 'lib/prawn/qrcode.rb', line 144 def background_color @background_color ||= 'FFFFFF' end |
#dot ⇒ Object
132 133 134 135 136 |
# File 'lib/prawn/qrcode.rb', line 132 def dot @dot ||= Prawn::QRCode.dotsize(qr_code, @extent, margin) if defined?(@extent) @dot ||= DEFAULT_DOTSIZE unless defined?(@extent) @dot end |
#extent ⇒ Object
156 157 158 159 |
# File 'lib/prawn/qrcode.rb', line 156 def extent @extent ||= (2 * margin + qr_code.modules.length) * dot @extent end |
#foreground_color ⇒ Object
140 141 142 |
# File 'lib/prawn/qrcode.rb', line 140 def foreground_color @foreground_color ||= '000000' end |
#margin ⇒ Object
152 153 154 |
# File 'lib/prawn/qrcode.rb', line 152 def margin @margin ||= 4 end |
#margin_size ⇒ Object
161 162 163 |
# File 'lib/prawn/qrcode.rb', line 161 def margin_size margin * dot end |
#render(pdf) ⇒ Object
rubocop:disable Metrics/AbcSize
178 179 180 181 182 183 184 185 186 187 188 189 190 191 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 |
# File 'lib/prawn/qrcode.rb', line 178 def render(pdf) pdf.fill_color background_color pos(pdf) # make sure the @pos attribute is set before calling align align(pdf.bounds) pdf.bounding_box(pos(pdf), width: extent, height: extent) do |_box| pdf.fill_color foreground_color margin_dist = margin * dot m = qr_code.modules pos_y = margin_dist + m.length * dot m.each_with_index do |row, index| pos_x = margin_dist dark_col = 0 row.each_index do |col| pdf.move_to [pos_x, pos_y] if qr_code.qrcode.checked?(index, col) dark_col += 1 else if dark_col > 0 dark_col_extent = dark_col * dot pdf.fill { pdf.rectangle([pos_x - dark_col_extent, pos_y], dark_col_extent, dot) } dark_col = 0 end end pos_x += dot end pdf.fill { pdf.rectangle([pos_x - dark_col * dot, pos_y], dot * dark_col, dot) } if dark_col > 0 pos_y -= dot end if stroke pdf.fill_color stroke_color pdf.stroke_bounds end pdf.stroke_axis(at: [-1, -1], negative_axes_length: 0, color: '0C0C0C', step_length: 50) if debug end end |
#stroke_color ⇒ Object
148 149 150 |
# File 'lib/prawn/qrcode.rb', line 148 def stroke_color @stroke_color ||= '000000' end |