File: font_spec.rb

package info (click to toggle)
ruby-pdf-reader 1.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 12,908 kB
  • ctags: 569
  • sloc: ruby: 8,330; makefile: 10
file content (152 lines) | stat: -rw-r--r-- 4,388 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
# coding: utf-8

require File.dirname(__FILE__) + "/spec_helper"

describe PDF::Reader::Font do

  let(:object_hash) { PDF::Reader::ObjectHash.allocate }

  describe "basefont=()" do

    let(:font) { PDF::Reader::Font.new(object_hash, {}) }

    it "should select a sensible encoding when set to a symbol font" do
      font.basefont = "Arial"
      font.encoding.should be_a_kind_of(PDF::Reader::Encoding)

      font.basefont = "Symbol"
      font.encoding.should be_a_kind_of(PDF::Reader::Encoding)

      font.basefont = "ZapfDingbats"
      font.encoding.should be_a_kind_of(PDF::Reader::Encoding)
    end

    it "should correctly store the font BaseFont" do
      font.basefont = :Helvetica
      font.basefont.should eql(:Helvetica)
    end

  end

  describe "to_utf8()" do
    context "with no ToUnicode CMap" do
      let(:font) { PDF::Reader::Font.new(object_hash, {}) }

      it "should delegate to an Encoding object to convert strings to utf-8" do
        encoding = stub
        font.encoding = encoding
        encoding.should_receive(:to_utf8).with("hello")
        font.to_utf8("hello")
      end

      it "should delegate to an Encoding object to convert arrays of strings to utf-8" do
        encoding = stub
        font.encoding = encoding
        encoding.should_receive(:to_utf8).with("hello")
        encoding.should_receive(:to_utf8).with("howdy")
        font.to_utf8(["hello", "howdy"])
      end

      it "should return the same type when to_utf8 is called with a string or array" do
        font.to_utf8("abc").should be_a_kind_of(String)
        font.to_utf8(["abc"]).should be_a_kind_of(Array)
      end

      it "should convert integers to a utf-8 string" do
        font.to_utf8(123).should be_a_kind_of(String)
      end

      it "should use an encoding of StandardEncoding if none has been specified" do
        str = "abc\xA8"
        font.to_utf8(str).should eql("abc\xC2\xA4")
      end
    end

    context "with a ToUnicode CMap" do
      let(:font) { PDF::Reader::Font.new(object_hash, {}) }

      it "should delegate to a CMap object to convert strings to utf-8" do
        cmap = stub
        cmap.should_receive(:decode).with(104).and_return(104)
        cmap.should_receive(:decode).with(101).and_return(104)
        cmap.should_receive(:decode).with(108).and_return(104)
        cmap.should_receive(:decode).with(108).and_return(104)
        cmap.should_receive(:decode).with(111).and_return(104)
        font.tounicode = cmap

        font.to_utf8("hello")
      end

      it "should not delegate to an Encoding object to convert strings to utf-8" do
        encoding = stub
        encoding.should_not_receive(:to_utf8)
        encoding.should_receive(:unpack).and_return("C*")
        font.encoding = encoding
        cmap = stub
        cmap.should_receive(:decode).exactly(5).times.and_return(104)
        font.tounicode = cmap

        font.to_utf8("hello")
      end

    end
  end
  describe "unpack()" do
    context "with a WinAnsi encoded font" do
      let(:raw)  do
        {
          :Encoding  => :WinAnsiEncoding,
          :Type      => :Font
        }
      end
      let(:font) { PDF::Reader::Font.new(object_hash, raw) }

      it "should unpack a binary string into ints" do
        font.unpack("\x41\x42").should == [65,66]
      end
    end
  end

  describe "glyph_width()" do
    context "with a FirstChar of 1" do
      let(:raw)  do
        {
          :Encoding  => :WinAnsiEncoding,
          :Type      => :Font,
          :FirstChar => 1,
          :Widths    => [100, 200, 300, 400]
        }
      end
      let(:font) { PDF::Reader::Font.new(object_hash, raw) }

      it "should return the width for a glyph" do
        font.glyph_width(2).should == 200
      end

      it "should return 0 for an unknown glyph" do
        font.glyph_width(10).should == 0
      end
    end

    context "with a FirstChar of 5" do
      let(:raw)  do
        {
          :Encoding  => :WinAnsiEncoding,
          :Type      => :Font,
          :FirstChar => 5,
          :Widths    => [100, 200, 300, 400]
        }
      end
      let(:font) { PDF::Reader::Font.new(object_hash, raw) }

      it "should return the width for a glyph" do
        font.glyph_width(7).should == 300
      end

      it "should return 0 for an unknown glyph" do
        font.glyph_width(20).should == 0
      end
    end
  end

end