File: text_draw_text_spec.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 (150 lines) | stat: -rw-r--r-- 4,813 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# frozen_string_literal: true

require 'spec_helper'

describe Prawn::Text do
  describe '#draw_text' do
    let(:pdf) { create_pdf }

    it 'raise_errors ArgumentError if :at option omitted' do
      expect { pdf.draw_text('hai', {}) }.to raise_error(ArgumentError)
    end

    it 'raise_errors ArgumentError if :align option included' do
      expect do
        pdf.draw_text('hai', at: [0, 0], align: :center)
      end.to raise_error(ArgumentError)
    end

    it 'allows drawing empty strings to the page' do
      pdf.draw_text(' ', at: [100, 100])
      text = PDF::Inspector::Text.analyze(pdf.render)
      expect(text.strings.first).to eq(' ')
    end

    it 'defaults to 12 point helvetica' do
      pdf.draw_text('Blah', at: [100, 100])
      text = PDF::Inspector::Text.analyze(pdf.render)
      expect(text.font_settings[0][:name]).to eq(:Helvetica)
      expect(text.font_settings[0][:size]).to eq(12)
      expect(text.strings.first).to eq('Blah')
    end

    it 'allows setting font size' do
      pdf.draw_text('Blah', at: [100, 100], size: 16)
      text = PDF::Inspector::Text.analyze(pdf.render)
      expect(text.font_settings[0][:size]).to eq(16)
    end

    it 'allows setting a default font size' do
      pdf.font_size = 16
      pdf.draw_text('Blah', at: [0, 0])

      text = PDF::Inspector::Text.analyze(pdf.render)
      expect(text.font_settings[0][:size]).to eq(16)
    end

    # rubocop: disable Naming/AccessorMethodName
    rotated_text_inspector =
      Class.new(PDF::Inspector) do
        attr_reader :tm_operator_used

        def initialize
          @tm_operator_used = false
        end

        def set_text_matrix_and_text_line_matrix(*_arguments)
          @tm_operator_used = true
        end
      end
    # rubocop: enable Naming/AccessorMethodName

    it 'allows rotation' do
      pdf.draw_text('Test', at: [100, 100], rotate: 90)

      text = rotated_text_inspector.analyze(pdf.render)

      expect(text.tm_operator_used).to eq true
    end

    it 'does not use rotation matrix by default' do
      pdf.draw_text('Test', at: [100, 100])

      text = rotated_text_inspector.analyze(pdf.render)

      expect(text.tm_operator_used).to eq false
    end

    it 'allows overriding default font for a single instance' do
      pdf.font_size = 16

      pdf.draw_text('Blah', size: 11, at: [0, 0])
      pdf.draw_text('Blaz', at: [0, 0])
      text = PDF::Inspector::Text.analyze(pdf.render)
      expect(text.font_settings[0][:size]).to eq(11)
      expect(text.font_settings[1][:size]).to eq(16)
    end

    it 'allows setting a font size transaction with a block' do
      pdf.font_size 16 do
        pdf.draw_text('Blah', at: [0, 0])
      end

      pdf.draw_text('blah', at: [0, 0])

      text = PDF::Inspector::Text.analyze(pdf.render)
      expect(text.font_settings[0][:size]).to eq(16)
      expect(text.font_settings[1][:size]).to eq(12)
    end

    it 'allows manual setting the font size when in a font size block' do
      pdf.font_size(16) do
        pdf.draw_text('Foo', at: [0, 0])
        pdf.draw_text('Blah', size: 11, at: [0, 0])
        pdf.draw_text('Blaz', at: [0, 0])
      end
      text = PDF::Inspector::Text.analyze(pdf.render)
      expect(text.font_settings[0][:size]).to eq(16)
      expect(text.font_settings[1][:size]).to eq(11)
      expect(text.font_settings[2][:size]).to eq(16)
    end

    it 'allows registering of built-in font_settings on the fly' do
      pdf.font 'Times-Roman'
      pdf.draw_text('Blah', at: [100, 100])
      pdf.font 'Courier'
      pdf.draw_text('Blaz', at: [150, 150])
      text = PDF::Inspector::Text.analyze(pdf.render)
      expect(text.font_settings[0][:name]).to eq(:"Times-Roman")
      expect(text.font_settings[1][:name]).to eq(:Courier)
    end

    it 'raise_errors an exception when an unknown font is used' do
      expect { pdf.font 'Pao bu' }.to raise_error(Prawn::Errors::UnknownFont)
    end

    it 'correctlies render a utf-8 string when using a built-in font' do
      str = '©' # copyright symbol
      pdf.draw_text(str, at: [0, 0])

      # grab the text from the rendered PDF and ensure it matches
      text = PDF::Inspector::Text.analyze(pdf.render)
      expect(text.strings.first).to eq(str)
    end

    it 'raises an exception when a utf-8 incompatible string is rendered' do
      str = "Blah \xDD"
      expect { pdf.draw_text(str, at: [0, 0]) }.to raise_error(
        Prawn::Errors::IncompatibleStringEncoding
      )
    end

    it 'does not raise an exception when a shift-jis string is rendered' do
      datafile = "#{Prawn::DATADIR}/shift_jis_text.txt"
      sjis_str = File.open(datafile, 'r:shift_jis', &:gets)
      pdf.font("/usr/share/fonts/truetype/arphic-gkai00mp/gkai00mp.ttf")

      pdf.draw_text(sjis_str, at: [0, 0])
    end
  end
end