File: encoding_spec.rb

package info (click to toggle)
ruby-pdf-reader 1.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 10,608 kB
  • sloc: ruby: 5,887; makefile: 10
file content (419 lines) | stat: -rw-r--r-- 12,729 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# coding: utf-8

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

describe PDF::Reader::Encoding do

  it "should return a new encoding object on request, or raise an error if unrecognised" do
    PDF::Reader::Encoding.new("FakeEncoding").should be_a_kind_of(PDF::Reader::Encoding)
    PDF::Reader::Encoding.new(nil).should be_a_kind_of(PDF::Reader::Encoding)
  end

  it "should return a new encoding object on request, or raise an error if unrecognised" do
    win =  {:Encoding => :WinAnsiEncoding}
    fake = {:Encoding => :FakeEncoding}
    PDF::Reader::Encoding.new(win).should  be_a_kind_of(PDF::Reader::Encoding)
    PDF::Reader::Encoding.new(fake).should be_a_kind_of(PDF::Reader::Encoding)
  end

  it "should return a new encoding object with a differences table on request" do
    win =  {
             :Encoding    => :WinAnsiEncoding,
             :Differences => [25, :A, 26, :B]
           }
    enc = PDF::Reader::Encoding.new(win)
    enc.should be_a_kind_of(PDF::Reader::Encoding)
    enc.differences.should be_a_kind_of(Hash)
    enc.differences[25].should eql(:A)
    enc.differences[26].should eql(:B)
  end

  it "should return a new encoding object with a differences table on request" do
    win =  {
             :Encoding    => :WinAnsiEncoding,
             :Differences => [25, :A, :B]
           }
    enc = PDF::Reader::Encoding.new(win)
    enc.should be_a_kind_of(PDF::Reader::Encoding)
    enc.differences.should be_a_kind_of(Hash)
    enc.differences[25].should eql(:A)
    enc.differences[26].should eql(:B)
  end

  it "should correctly replace control characters with 'unknown char' when there's no applicable difference table entry" do
    win =  {
             :Encoding    => :WinAnsiEncoding,
             :Differences => [1, :A,]
           }
    enc = PDF::Reader::Encoding.new(win)
    enc.to_utf8("\002").should eql("▯")
  end
end

describe "The PDF::Reader::Encoding::IdentityH class" do

  it "should return utf-8 squares if to_utf8 is called for an Identity-H encoding" do
    e = PDF::Reader::Encoding.new("Identity-H")
    [
      {:expert => "\x22",             :utf8 => ""},
      {:expert => "\x22\xF7",         :utf8 => [0x25AF].pack("U*")},
      {:expert => "\x22\xF7\x22\xF7", :utf8 => [0x25AF,0x25AF].pack("U*")}
    ].each do |vals|
      result = e.to_utf8(vals[:expert])
      if RUBY_VERSION >= "1.9"
        result.encoding.to_s.should eql("UTF-8")
        vals[:utf8].force_encoding("UTF-8")
      end

      result.should eql(vals[:utf8])
    end
  end

end

describe "The PDF::Reader::Encoding::IdentityV class" do
  it "should return utf-8 squares if to_utf8 is called without a cmap" do
    e = PDF::Reader::Encoding.new("Identity-V")
    [
      {:expert => "\x22",             :utf8 => ""},
      {:expert => "\x22\xF7",         :utf8 => [0x25AF].pack("U*")},
      {:expert => "\x22\xF7\x22\xF7", :utf8 => [0x25AF,0x25AF].pack("U*")}
    ].each do |vals|
      result = e.to_utf8(vals[:expert])

      if RUBY_VERSION >= "1.9"
        result.encoding.to_s.should eql("UTF-8")
        vals[:utf8].force_encoding("UTF-8")
      end

      result.should eql(vals[:utf8])
    end
  end

end

describe "The PDF::Reader::Encoding::MacExpertEncoding class" do

  it "should correctly convert various expert strings to utf-8" do
    e = PDF::Reader::Encoding.new(:MacExpertEncoding)
    [
      {:expert => "\x22", :utf8 => [0xF6F8].pack("U*")},
      {:expert => "\x62", :utf8 => [0xF762].pack("U*")},
      {:expert => "\xBE", :utf8 => [0xF7E6].pack("U*")},
      {:expert => "\xF7", :utf8 => [0xF6EF].pack("U*")}
    ].each do |vals|
      result = e.to_utf8(vals[:expert])

      if RUBY_VERSION >= "1.9"
        result.encoding.to_s.should eql("UTF-8")
        vals[:utf8].force_encoding("UTF-8")
      end

      result.should eql(vals[:utf8])
    end
  end

  it "should correctly convert various mac expert strings when a differences table is specified" do
    e = PDF::Reader::Encoding.new(:MacExpertEncoding)
    e.differences = [0xEE, :A]
    [
      {:mac => "\x22\xEE", :utf8 => [0xF6F8, 0x41].pack("U*")}
    ].each do |vals|

      result = e.to_utf8(vals[:mac])

      if RUBY_VERSION >= "1.9"
        result.encoding.to_s.should eql("UTF-8")
        vals[:utf8].force_encoding("UTF-8")
      end

      result.should eql(vals[:utf8])

    end
  end

end

describe "The PDF::Reader::Encoding::MacRomanEncoding class" do

  it "should correctly convert various mac roman strings to utf-8" do
    e = PDF::Reader::Encoding.new(:MacRomanEncoding)
    [
      {:mac => "abc", :utf8 => "abc"},
      {:mac => "ABC", :utf8 => "ABC"},
      {:mac => "123", :utf8 => "123"},
      {:mac => "\x24", :utf8 => "\x24"},         # dollar sign
      {:mac => "\xDB", :utf8 => "\xE2\x82\xAC"}, # € sign
      {:mac => "\xD8", :utf8 => "\xC3\xBF"},     # ÿ sign
      {:mac => "\xE4", :utf8 => "\xE2\x80\xB0"}, # ‰  sign
      {:mac => "\xFD", :utf8 => "\xCB\x9D"}      # ˝ sign
    ].each do |vals|
      result = e.to_utf8(vals[:mac])
      result.should eql(vals[:utf8])

      if RUBY_VERSION >= "1.9"
        result.encoding.to_s.should eql("UTF-8")
      end
    end
  end

  it "should correctly convert various mac roman strings when a differences table is specified" do
    e = PDF::Reader::Encoding.new(:MacRomanEncoding)
    e.differences = [0xEE, :A]
    [
      {:mac => "\x24\xEE", :utf8 => [0x24, 0x41].pack("U*")}, # dollar sign, A
    ].each do |vals|

      result = e.to_utf8(vals[:mac])

      if RUBY_VERSION >= "1.9"
        result.encoding.to_s.should eql("UTF-8")
        vals[:utf8].force_encoding("UTF-8")
      end

      result.should eql(vals[:utf8])

    end
  end

end

describe "The PDF::Reader::Encoding::PDFDocEncoding class" do

  it "should correctly convert various PDFDoc strings to utf-8" do
    e = PDF::Reader::Encoding.new(:PDFDocEncoding)
    [
      {:pdf => "\x22", :utf8 => [0x22].pack("U*")},
      {:pdf => "\x62", :utf8 => [0x62].pack("U*")},
      {:pdf => "\xA0", :utf8 => [0x20AC].pack("U*")},
      {:pdf => "\x94", :utf8 => [0xFB02].pack("U*")}
    ].each do |vals|
      result = e.to_utf8(vals[:pdf])

      if RUBY_VERSION >= "1.9"
        result.encoding.to_s.should eql("UTF-8")
        vals[:utf8].force_encoding("UTF-8")
      end

      result.should eql(vals[:utf8])
    end
  end

  it "should correctly convert various pdf doc strings when a differences table is specified" do
    e = PDF::Reader::Encoding.new(:PDFDocEncoding)
    e.differences = [0xEE, :A]
    [
      {:pdf => "\x22\xEE", :utf8 => [0x22, 0x41].pack("U*")}
    ].each do |vals|

      result = e.to_utf8(vals[:pdf])

      if RUBY_VERSION >= "1.9"
        result.encoding.to_s.should eql("UTF-8")
        vals[:utf8].force_encoding("UTF-8")
      end

      result.should eql(vals[:utf8])

    end
  end

end

describe "The PDF::Reader::Encoding::StandardEncoding class" do

  it "should correctly convert various standard strings to utf-8" do
    e = PDF::Reader::Encoding.new(:StandardEncoding)
    [
      {:standard => "abc",  :utf8 => "abc"},
      {:standard => "ABC",  :utf8 => "ABC"},
      {:standard => "123",  :utf8 => "123"},
      {:standard => "\x60", :utf8 => [0x2018].pack("U*")}, # "
      {:standard => "\xA4", :utf8 => [0x2044].pack("U*")}, # fraction sign
      {:standard => "\xBD", :utf8 => [0x2030].pack("U*")}, # per mile sign
      {:standard => "\xFA", :utf8 => [0x0153].pack("U*")}
    ].each do |vals|
      result = e.to_utf8(vals[:standard])

      if RUBY_VERSION >= "1.9"
        result.encoding.to_s.should eql("UTF-8")
        vals[:utf8].force_encoding("UTF-8")
      end

      result.should eql(vals[:utf8])

    end
  end

  it "should correctly convert various standard strings when a differences table is specified" do
    e = PDF::Reader::Encoding.new(:StandardEncoding)
    e.differences = [0xEE, :A]
    [
      {:std => "\x60\xEE", :utf8 => [0x2018, 0x41].pack("U*")}, # ", A
    ].each do |vals|

      result = e.to_utf8(vals[:std])

      if RUBY_VERSION >= "1.9"
        result.encoding.to_s.should eql("UTF-8")
        vals[:utf8].force_encoding("UTF-8")
      end

      result.should eql(vals[:utf8])

    end
  end

end

describe "The PDF::Reader::Encoding::SymbolEncoding class" do

  it "should correctly convert various symbol strings to utf-8" do
    e = PDF::Reader::Encoding.new(:SymbolEncoding)
    [
      {:symbol => "\x41", :utf8 => [0x0391].pack("U*")}, # alpha
      {:symbol => "\x42", :utf8 => [0x0392].pack("U*")}, # beta
      {:symbol => "\x47", :utf8 => [0x0393].pack("U*")}, # gamma
      {:symbol => "123",  :utf8 => "123"},
      {:symbol => "\xA0", :utf8 => [0x20AC].pack("U*")}, # € sign
    ].each do |vals|
      result = e.to_utf8(vals[:symbol])

      if RUBY_VERSION >= "1.9"
        result.encoding.to_s.should eql("UTF-8")
        vals[:utf8].force_encoding("UTF-8")
      end

      result.should eql(vals[:utf8])

    end
  end

  it "should correctly convert various symbol strings when a differences table is specified" do
    e = PDF::Reader::Encoding.new(:SymbolEncoding)
    e.differences = [0xEE, :A]
    [
      {:symbol => "\x41\xEE", :utf8 => [0x0391, 0x41].pack("U*")}, # alpha, A
    ].each do |vals|

      result = e.to_utf8(vals[:symbol])

      if RUBY_VERSION >= "1.9"
        result.encoding.to_s.should eql("UTF-8")
        vals[:utf8].force_encoding("UTF-8")
      end

      result.should eql(vals[:utf8])

    end
  end

end

describe "The PDF::Reader::Encoding::WinAnsiEncoding class" do

  it "should correctly convert various win-1252 strings to utf-8" do
    e = PDF::Reader::Encoding.new(:WinAnsiEncoding)
    [
      {:win => "abc", :utf8 => "abc"},
      {:win => "ABC", :utf8 => "ABC"},
      {:win => "123", :utf8 => "123"},
      {:win => "\x24", :utf8 => "\x24"},         # dollar sign
      {:win => "\x80", :utf8 => "\xE2\x82\xAC"}, # € sign
      {:win => "\x82", :utf8 => "\xE2\x80\x9A"}, # ‚ sign
      {:win => "\x83", :utf8 => "\xC6\x92"},     # ƒ sign
      {:win => "\x9F", :utf8 => "\xC5\xB8"}      # Ÿ sign
    ].each do |vals|
      result = e.to_utf8(vals[:win])
      result.should eql(vals[:utf8])

      if RUBY_VERSION >= "1.9"
        result.encoding.to_s.should eql("UTF-8")
      end
    end
  end

  it "should correctly convert various win-1252 strings when a differences table is specified" do
    e = PDF::Reader::Encoding.new(:WinAnsiEncoding)
    e.differences = [0xEE, :A]
    [
      {:win => "abc", :utf8 => "abc"},
      {:win => "ABC", :utf8 => "ABC"},
      {:win => "123", :utf8 => "123"},
      {:win => "ABC\xEE", :utf8 => "ABCA"}
    ].each do |vals|
      result = e.to_utf8(vals[:win])
      result.should eql(vals[:utf8])

      if RUBY_VERSION >= "1.9"
        result.encoding.to_s.should eql("UTF-8")
      end
    end
  end

end

describe "The PDF::Reader::Encoding::ZapfDingbatsEncoding class" do

  it "should correctly convert various dingbats strings to utf-8" do
    e = PDF::Reader::Encoding.new(:ZapfDingbatsEncoding)
    [
      {:dingbats => "\x22", :utf8 => [0x2702].pack("U*")}, # scissors
      {:dingbats => "\x25", :utf8 => [0x260E].pack("U*")}, # telephone
      {:dingbats => "\xAB", :utf8 => [0x2660].pack("U*")}, # spades
      {:dingbats => "\xDE", :utf8 => [0x279E].pack("U*")}, # ->
    ].each do |vals|
      result = e.to_utf8(vals[:dingbats])

      if RUBY_VERSION >= "1.9"
        result.encoding.to_s.should eql("UTF-8")
        vals[:utf8].force_encoding("UTF-8")
      end

      result.should eql(vals[:utf8])

    end
  end

  it "should correctly convert various dingbats strings when a differences table is specified" do
    e = PDF::Reader::Encoding.new(:ZapfDingbatsEncoding)
    e.differences = [0xEE, :A]
    [
      {:dingbats => "\x22\xEE", :utf8 => [0x2702, 0x41].pack("U*")}, # scissors
    ].each do |vals|

      result = e.to_utf8(vals[:dingbats])

      if RUBY_VERSION >= "1.9"
        result.encoding.to_s.should eql("UTF-8")
        vals[:utf8].force_encoding("UTF-8")
      end

      result.should eql(vals[:utf8])

    end
  end

end

describe "The PDF::Reader::Encoding::UTF16Encoding class" do

  it "should correctly convert various PDFDoc strings to utf-8" do
    e = PDF::Reader::Encoding.new(:UTF16Encoding)
    [
      {:utf16 => "\x00\x41", :utf8 => [0x41].pack("U*")},
      {:utf16 => "\x20\x22", :utf8 => [0x2022].pack("U*")},
      {:utf16 => "\x00\x41", :utf8 => [0x41].pack("U*")},
      {:utf16 => "\x20\x22", :utf8 => [0x2022].pack("U*")}
    ].each do |vals|
      result = e.to_utf8(vals[:utf16])

      if RUBY_VERSION >= "1.9"
        result.encoding.to_s.should eql("UTF-8")
        vals[:utf8].force_encoding("UTF-8")
      end

      result.should eql(vals[:utf8])
    end
  end
end