Class: Paperback::Document
- Inherits:
-
Object
- Object
- Paperback::Document
- Extended by:
- T::Sig
- Defined in:
- lib/paperback/document.rb
Instance Attribute Summary collapse
-
#debug ⇒ Object
readonly
Returns the value of attribute debug.
-
#pdf ⇒ Object
readonly
Returns the value of attribute pdf.
Instance Method Summary collapse
- #add_newline ⇒ Object
- #debug_draw_axes ⇒ Object
- #draw_base64(b64_content:, b64_bytes:, font_size: nil, is_encrypted: true) ⇒ Object
- #draw_header(labels:, passphrase_sha:, passphrase_len:, repo_url: 'https://github.com/ab/paperback') ⇒ Object
- #draw_paperback(qr_code:, sixword_lines:, sixword_bytes:, labels:, passphrase_sha: nil, passphrase_len: nil, sixword_font_size: nil, base64_content: nil, base64_bytes: nil) ⇒ Object
- #draw_qr_code(qr_modules:) ⇒ Object
- #draw_sixword(lines:, sixword_bytes:, columns: 3, hunks_per_row: 1, font_size: nil, is_encrypted: true) ⇒ Object
-
#initialize(debug: false) ⇒ Document
constructor
A new instance of Document.
- #log ⇒ Object
- #render(output_file:, draw_opts:) ⇒ Object
Constructor Details
#initialize(debug: false) ⇒ Document
Returns a new instance of Document.
17 18 19 20 21 22 |
# File 'lib/paperback/document.rb', line 17 def initialize(debug: false) log.debug('Document#initialize') @debug = T.let(debug, T::Boolean) @pdf = T.let(Prawn::Document.new, Prawn::Document) @log = T.let(nil, T.nilable(Logger)) end |
Instance Attribute Details
#debug ⇒ Object (readonly)
Returns the value of attribute debug.
14 15 16 |
# File 'lib/paperback/document.rb', line 14 def debug @debug end |
#pdf ⇒ Object (readonly)
Returns the value of attribute pdf.
11 12 13 |
# File 'lib/paperback/document.rb', line 11 def pdf @pdf end |
Instance Method Details
#add_newline ⇒ Object
123 124 125 |
# File 'lib/paperback/document.rb', line 123 def add_newline pdf.move_down(pdf.font_size) end |
#debug_draw_axes ⇒ Object
116 117 118 119 |
# File 'lib/paperback/document.rb', line 116 def debug_draw_axes return unless debug pdf.float { pdf.stroke_axis } end |
#draw_base64(b64_content:, b64_bytes:, font_size: nil, is_encrypted: true) ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/paperback/document.rb', line 269 def draw_base64(b64_content:, b64_bytes:, font_size: nil, is_encrypted: true) font_size ||= 11 debug_draw_axes if is_encrypted header = [ "This PGP text encodes #{b64_bytes} bytes in #{b64_content.length}", " characters. Decode with `gpg -d`." ].join else header = [ "This base64 text encodes #{b64_bytes} bytes in #{b64_content.length}", " characters. Decode with `base64 --decode`." ].join end add_newline add_newline pdf.text(header) add_newline pdf.font('Courier') do pdf.text(b64_content) end end |
#draw_header(labels:, passphrase_sha:, passphrase_len:, repo_url: 'https://github.com/ab/paperback') ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/paperback/document.rb', line 136 def draw_header(labels:, passphrase_sha:, passphrase_len:, repo_url: 'https://github.com/ab/paperback') intro = [ "This is a paper backup produced by `paperback`. ", "<u><link href='#{repo_url}'>#{repo_url}</link></u>", ].join pdf.text(intro, inline_format: true) add_newline label_pad = T.must(labels.keys.map(&:length).max) + 1 unless passphrase_sha && passphrase_len labels['Encrypted'] = 'no' end pdf.font('Courier') do labels.each_pair do |k, v| pdf.text("#{(k + ':').ljust(label_pad)} #{v}") end if passphrase_sha pdf.text("SHA256(passphrase)[0...16]: #{passphrase_sha}") end end add_newline if passphrase_len pdf.font('Helvetica') do pdf.font_size(12.8) do pdf.text('Passphrase: ' + '_ ' * passphrase_len) end end pdf.move_down(8) pdf.indent(72) do pdf.text('Be sure to cover the passphrase when scanning the QR code!' + ' Decrypt with `gpg -d`.') end end end |
#draw_paperback(qr_code:, sixword_lines:, sixword_bytes:, labels:, passphrase_sha: nil, passphrase_len: nil, sixword_font_size: nil, base64_content: nil, base64_bytes: nil) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/paperback/document.rb', line 76 def draw_paperback(qr_code:, sixword_lines:, sixword_bytes:, labels:, passphrase_sha: nil, passphrase_len: nil, sixword_font_size: nil, base64_content: nil, base64_bytes: nil) T.assert_type!(qr_code, RQRCode::QRCode) # Header & QR code page pdf.font('Times-Roman') debug_draw_axes draw_header(labels: labels, passphrase_sha: passphrase_sha, passphrase_len: passphrase_len) add_newline draw_qr_code(qr_modules: qr_code.modules) pdf.stroke_color '000000' pdf.fill_color '000000' # Sixword page pdf.start_new_page draw_sixword(lines: sixword_lines, sixword_bytes: sixword_bytes, font_size: sixword_font_size, is_encrypted: !!passphrase_len) if base64_content draw_base64(b64_content: base64_content, b64_bytes: T.must(base64_bytes), is_encrypted: !!passphrase_len) end pdf.number_pages('<page> of <total>', align: :right, at: [pdf.bounds.right - 100, -2]) end |
#draw_qr_code(qr_modules:) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/paperback/document.rb', line 229 def draw_qr_code(qr_modules:) qr_height = pdf.cursor # entire rest of page qr_width = pdf.bounds.width # entire page width # number of modules plus 2 for quiet area qr_code_size = qr_modules.length + 2 pixel_height = qr_height / qr_code_size pixel_width = qr_width / qr_code_size pdf.bounding_box([0, pdf.cursor], width: qr_width, height: qr_height) do if debug pdf.stroke_color('888888') pdf.stroke_bounds end qr_modules.each do |row| pdf.move_down(pixel_height) row.each_with_index do |pixel_val, col_i| pdf.stroke do pdf.stroke_color(pixel_val ? '000000' : 'ffffff') pdf.fill_color(pixel_val ? '000000' : 'ffffff') xy = [(col_i + 1) * pixel_width, pdf.cursor] pdf.fill_rectangle(xy, pixel_width, pixel_height) end end end end end |
#draw_sixword(lines:, sixword_bytes:, columns: 3, hunks_per_row: 1, font_size: nil, is_encrypted: true) ⇒ Object
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 |
# File 'lib/paperback/document.rb', line 193 def draw_sixword(lines:, sixword_bytes:, columns: 3, hunks_per_row: 1, font_size: nil, is_encrypted: true) font_size ||= 11 debug_draw_axes numbered = lines.each_slice(hunks_per_row).each_with_index.map { |row, i| "#{i * hunks_per_row + 1}: #{row.map(&:strip).join('. ')}" } header = [ "This sixword text encodes #{sixword_bytes} bytes in #{lines.length}", ' six-word sentences.', ' Decode with `sixword -d`', (is_encrypted ? ', then `gpg -d`.' : '.') ].join pdf.font('Times-Roman') do pdf.text(header) add_newline end pdf.column_box([0, pdf.cursor], columns: columns, width: pdf.bounds.width) do pdf.font('Times-Roman') do pdf.font_size(font_size) do pdf.text(numbered.join("\n")) end end end end |
#log ⇒ Object
25 26 27 |
# File 'lib/paperback/document.rb', line 25 def log @log ||= Paperback.class_log(self.class) end |
#render(output_file:, draw_opts:) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/paperback/document.rb', line 36 def render(output_file:, draw_opts:) log.info('Rendering PDF') # Create all the PDF content draw_paperback(**T.unsafe(draw_opts)) # Render to output file log.info("Writing PDF to #{output_file.inspect}") pdf.render_file(output_file) end |