File: text_spacing_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 (95 lines) | stat: -rw-r--r-- 2,943 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
# frozen_string_literal: true

require 'spec_helper'

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

  describe '#character_spacing' do
    it 'draws the character spacing to the document' do
      pdf.character_spacing(10.555555) do
        pdf.text('hello world')
      end
      contents = PDF::Inspector::Text.analyze(pdf.render)
      expect(contents.character_spacing.first).to eq(10.55556)
    end

    it 'does not draw the character spacing to the document' \
      ' when the new character spacing matches the old' do
      pdf.character_spacing(0) do
        pdf.text('hello world')
      end
      contents = PDF::Inspector::Text.analyze(pdf.render)
      expect(contents.character_spacing).to be_empty
    end

    it 'restores character spacing to 0' do
      pdf.character_spacing(10.555555) do
        pdf.text('hello world')
      end
      contents = PDF::Inspector::Text.analyze(pdf.render)
      expect(contents.character_spacing.last).to eq(0)
    end

    it 'functions as an accessor when no parameter given' do
      pdf.character_spacing(10.555555) do
        pdf.text('hello world')
        expect(pdf.character_spacing).to eq(10.555555)
      end
      expect(pdf.character_spacing).to eq(0)
    end

    # ensure that we properly internationalize by using the number of characters
    # in a string, not the number of bytes, to insert character spaces
    #
    it 'calculates character spacing widths by characters, not bytes' do
      pdf.font("/usr/share/fonts/truetype/arphic-gkai00mp/gkai00mp.ttf")

      str = 'こんにちは世界'
      raw_width = nil
      pdf.character_spacing(0) do
        raw_width = pdf.width_of(str)
      end

      pdf.character_spacing(10) do
        # the new width should include six 10-pt character spaces.
        expect(pdf.width_of(str)).to be_within(0.001).of(raw_width + (10 * 6))
      end
    end
  end

  describe '#word_spacing' do
    it 'draws the word spacing to the document' do
      pdf.word_spacing(10.555555) do
        pdf.text('hello world')
      end
      contents = PDF::Inspector::Text.analyze(pdf.render)
      expect(contents.word_spacing.first).to eq(10.55556)
    end

    it 'draws the word spacing to the document' \
      ' when the new word spacing matches the old' do
      pdf.word_spacing(0) do
        pdf.text('hello world')
      end
      contents = PDF::Inspector::Text.analyze(pdf.render)
      expect(contents.word_spacing).to be_empty
    end

    it 'restores word spacing to 0' do
      pdf.word_spacing(10.555555) do
        pdf.text('hello world')
      end
      contents = PDF::Inspector::Text.analyze(pdf.render)
      expect(contents.word_spacing.last).to eq(0)
    end

    it 'functions as an accessor when no parameter given' do
      pdf.word_spacing(10.555555) do
        pdf.text('hello world')
        expect(pdf.word_spacing).to eq(10.555555)
      end
      expect(pdf.word_spacing).to eq(0)
    end
  end
end