File: text_box_extensions.rb

package info (click to toggle)
ruby-prawn 2.4.0%2Bdfsg-1~
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 4,396 kB
  • sloc: ruby: 16,090; sh: 43; makefile: 20
file content (48 lines) | stat: -rw-r--r-- 1,369 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
# frozen_string_literal: true

# We've already seen one way of using text boxes with the <code>text_box</code>
# method. Turns out this method is just a convenience for using the
# <code>Prawn::Text::Box</code> class as it creates a new object and call
# <code>render</code> on it.
#
# Knowing that any extensions we add to <code>Prawn::Text::Box</code> will take
# effect when we use the <code>text_box</code> method. To add an extension all
# we need to do is append the <code>Prawn::Text::Box.extensions</code> array
# with a module.

require '../example_helper'

filename = File.basename(__FILE__).gsub('.rb', '.pdf')
Prawn::ManualBuilder::Example.generate(filename) do
  module TriangleBox
    def available_width
      height + 25
    end
  end

  y_position = cursor - 10
  width = 100
  height = 100

  Prawn::Text::Box.extensions << TriangleBox
  stroke_rectangle([0, y_position], width, height)
  text_box(
    'A' * 100,
    at: [0, y_position],
    width: width,
    height: height
  )

  Prawn::Text::Formatted::Box.extensions << TriangleBox
  stroke_rectangle([200, y_position], width, height)
  formatted_text_box(
    [{ text: 'A' * 100, color: '009900' }],
    at: [200, y_position],
    width: width,
    height: height
  )

  # Here we clear the extensions array
  Prawn::Text::Box.extensions.clear
  Prawn::Text::Formatted::Box.extensions.clear
end