File: icon_spec.rb

package info (click to toggle)
ruby-prawn-icon 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,596 kB
  • sloc: ruby: 1,398; makefile: 5
file content (286 lines) | stat: -rw-r--r-- 8,580 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
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
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
222
223
224
225
226
227
228
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# encoding: utf-8
#
# Copyright October 2014, Jesse Doyle. All rights reserved.
#
# This is free software. Please see the LICENSE and COPYING files for details.

require 'spec_helper'

describe Prawn::Icon::Interface do
  let(:pdf) { create_pdf }

  describe '::icon' do
    context 'valid icon key' do
      context 'with options' do
        it 'should handle text options (size)' do
          pdf.icon 'far-address-book', size: 60
          text = PDF::Inspector::Text.analyze(pdf.render)

          expect(text.font_settings.first[:size]).to eq(60)
        end

        it 'should handle text options (icon height)' do
          pdf.icon 'far-address-book', size: 60, size_mode: :icon_height
          text = PDF::Inspector::Text.analyze(pdf.render)

          expect(text.font_settings.first[:size]).to be_within(0.0001).of(58.25243)
        end
      end

      context 'inline_format: true' do
        it 'should handle text options (size)' do
          pdf.icon '<icon size="60">far-address-book</icon>', inline_format: true
          text = PDF::Inspector::Text.analyze(pdf.render)

          expect(text.strings.first).to eq('')
          expect(text.font_settings.first[:size]).to eq(60.0)
        end

        it 'should be able to render on multiple documents' do
          pdf1 = create_pdf
          pdf2 = create_pdf
          pdf1.icon '<icon>far-address-book</icon>', inline_format: true
          pdf2.icon '<icon>far-address-book</icon>', inline_format: true
          text1 = PDF::Inspector::Text.analyze(pdf1.render)
          text2 = PDF::Inspector::Text.analyze(pdf2.render)

          expect(text1.strings.first).to eq('')
          expect(text2.strings.first).to eq('')
        end

        it 'renders the icon at the proper cursor position (#24)' do
          icon_text = '<icon>fas-info-circle</icon> icon here!'
          pdf.text 'Start'
          pdf.move_down 10
          pdf.text 'More'
          pdf.move_down 20
          pdf.icon icon_text, inline_format: true
          pdf.move_down 30
          pdf.text 'End'
          inspector = PDF::Inspector::Text.analyze(pdf.render)
          x, y = inspector.positions[2]

          expect(x).to eq(0)
          expect(y.round).to eq(724)
        end

        context 'with final_gap: false' do
          it 'renders the icon without a final gap' do
            pdf.icon(
              '<icon size="60">far-address-book</icon>',
              inline_format: true,
              final_gap: false
            )
            pdf.text('Hello')
            inspector = PDF::Inspector::Text.analyze(pdf.render)
            y = inspector.positions[1].last.round

            expect(y).to eq(722)
          end
        end
      end

      context 'without options' do
        it 'should render an icon to document' do
          pdf.icon 'far-address-book'
          text = PDF::Inspector::Text.analyze(pdf.render)

          expect(text.strings.first).to eq('')
        end
      end
    end

    context 'invalid icon key' do
      it 'should raise IconNotFound' do
        expect { pdf.icon('far-__INVALID') }.to raise_error(
          Prawn::Icon::Errors::IconNotFound
        )
      end
    end

    context 'invalid specifier' do
      it 'should raise UnknownFont' do
        expect { pdf.icon('__INVALID__') }.to raise_error(
          Prawn::Errors::UnknownFont
        )
      end
    end
  end

  describe '::make_icon' do
    context ':inline_format => false (default)' do
      it 'should return a Prawn::Icon instance' do
        icon = pdf.make_icon 'far-address-book'

        expect(icon).to be_a(Prawn::Icon)
      end
    end

    context ':inline_format => true' do
      it 'returns nil' do
        icon = pdf.make_icon '<icon>far-address-book</icon>', inline_format: true

        expect(icon).to be_nil
      end
    end
  end

  describe '::inline_icon' do
    it 'returns nil' do
      icon = pdf.inline_icon '<icon>far-address-book</icon>'

      expect(icon).to be_nil
    end

    it 'starts a new page if necessary', github_issue: '49' do
      text = 209.times.map { 'Hello, World!' }.join(' ')
      pdf.text(text, size: 18)
      pdf.icon('Hello, <icon>fas-globe</icon>', inline_format: true, size: 18)
      inspector = PDF::Inspector::Page.analyze(pdf.render)

      expect(inspector.pages.size).to eq(2)
    end
  end

  describe '::formatted_icon_box' do
    it 'returns a Prawn::Text::Formatted::Box instance' do
      icon_text = <<~CONTENT
        <icon size="20">fas-broom</icon>
        <strikethrough>cancel that</strikethrough>
        <icon>fas-check</icon>
      CONTENT
      box = pdf.formatted_icon_box(icon_text, inline_format: true)

      expect(box).to be_a(Prawn::Text::Formatted::Box)
    end

    it 'accepts an absolute position parameter' do
      icon_text = 'Hello, <icon>fas-globe</icon>!'
      pdf.formatted_icon_box(icon_text, inline_format: true, x: 200, y: 100).render
      inspector = PDF::Inspector::Text.analyze(pdf.render)
      x, y = inspector.positions[0]

      expect(x).to eq(200)
      expect(y.round).to eq(89)
    end

    it 'handles final_gap: false correctly' do
      icon_text = <<~CONTENT
        Hello, <icon size="60">fas-globe</icon>
        Next line.
      CONTENT
      pdf.formatted_icon_box(icon_text, inline_format: true, final_gap: false).render
      inspector = PDF::Inspector::Text.analyze(pdf.render)
      x = inspector.positions[1].first

      expect(x.round).to eq(34)
    end
  end

  describe '::table_icon' do
    context 'inline_format: false (default)' do
      it 'should return a hash with font and content keys' do
        icon = pdf.table_icon 'far-address-book'

        expect(icon).to be_a(Hash)
        expect(icon[:font]).to eq('far')
        expect(icon[:content]).to eq('')
      end
    end

    context 'inline_format: true' do
      it 'should convert <icon> to <font> tags' do
        icon = pdf.table_icon '<icon>fas-user</icon>', inline_format: true

        expect(icon).to be_a(Hash)
        expect(icon[:content]).to eq('<font name="fas"></font>')
        expect(icon[:inline_format]).to be true
      end

      it 'should ignore all other tags' do
        a = ['<b>BOLD</b> <color rgb="0099FF">BLUE</color>', inline_format: true]
        icon = pdf.table_icon(*a)

        expect(icon).to be_a(Hash)
        expect(icon[:content]).to eq(a[0])
        expect(icon[:inline_format]).to be true
      end

      context 'multiple icons' do
        it 'should ignore any text not in an icon tag' do
          a = ['<icon>fas-user</icon> Some Text <icon>fi-laptop</icon>', inline_format: true]
          out = '<font name="fas"></font> Some Text <font name="fi"></font>'
          icon = pdf.table_icon(*a)

          expect(icon).to be_a(Hash)
          expect(icon[:content]).to eq(out)
          expect(icon[:inline_format]).to be true
        end
      end
    end
  end
end

describe Prawn::Icon do
  let(:pdf) { create_pdf }

  context 'FontAwesome | Regular' do
    it 'should render regular glyphs' do
      pdf.icon 'far-user'
      text = PDF::Inspector::Text.analyze(pdf.render)

      expect(text.strings.first).to eq('')
    end
  end

  context 'FontAwesome | Solid' do
    it 'should render solid glyphs' do
      pdf.icon 'fas-user'
      text = PDF::Inspector::Text.analyze(pdf.render)

      expect(text.strings.first).to eq('')
    end
  end

  context 'FontAwesome | Brands' do
    it 'should render FontAwesome glyphs' do
      pdf.icon 'fab-amazon'
      text = PDF::Inspector::Text.analyze(pdf.render)

      expect(text.strings.first).to eq('')
    end
  end

  context 'Foundation Icons' do
    it 'should render Foundation glyphs' do
      pdf.icon 'fi-laptop'
      text = PDF::Inspector::Text.analyze(pdf.render)

      expect(text.strings.first).to eq('')
    end
  end

  context 'PaymentFont' do
    it 'should render PaymentFont glyphs' do
      pdf.icon 'pf-amazon'
      text = PDF::Inspector::Text.analyze(pdf.render)

      expect(text.strings.first).to eq('')
    end
  end

  context 'Material Design Icons' do
    it 'renders Material Design Icon glyphs' do
      pdf.icon 'mdi-beer'
      text = PDF::Inspector::Text.analyze(pdf.render)

      # NOTE: Prawn <= 2.4.0 incorrectly handled unicode codepoints
      # see: https://github.com/prawnpdf/prawn/pull/1327#issuecomment-1905817491
      if Gem::Version.new(Prawn::VERSION) <= Gem::Version.new('2.4.0')
        expect(text.strings.first).to eq("\u{F009}")
      else
        expect(text.strings.first).to eq("\u{F0098}")
      end
    end
  end
end