hexapdf 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +46 -0
  3. data/CONTRIBUTERS +1 -1
  4. data/README.md +5 -5
  5. data/VERSION +1 -1
  6. data/examples/emoji-smile.png +0 -0
  7. data/examples/emoji-wink.png +0 -0
  8. data/examples/graphics.rb +9 -8
  9. data/examples/standard_pdf_fonts.rb +2 -1
  10. data/examples/text_box_alignment.rb +47 -0
  11. data/examples/text_box_inline_boxes.rb +56 -0
  12. data/examples/text_box_line_wrapping.rb +57 -0
  13. data/examples/text_box_shapes.rb +166 -0
  14. data/examples/text_box_styling.rb +72 -0
  15. data/examples/truetype.rb +3 -4
  16. data/lib/hexapdf/cli/optimize.rb +2 -2
  17. data/lib/hexapdf/configuration.rb +8 -6
  18. data/lib/hexapdf/content/canvas.rb +8 -5
  19. data/lib/hexapdf/content/parser.rb +3 -2
  20. data/lib/hexapdf/content/processor.rb +14 -3
  21. data/lib/hexapdf/document.rb +1 -0
  22. data/lib/hexapdf/document/fonts.rb +2 -1
  23. data/lib/hexapdf/document/pages.rb +23 -0
  24. data/lib/hexapdf/font/invalid_glyph.rb +78 -0
  25. data/lib/hexapdf/font/true_type/font.rb +14 -3
  26. data/lib/hexapdf/font/true_type/table.rb +1 -0
  27. data/lib/hexapdf/font/true_type/table/cmap.rb +1 -1
  28. data/lib/hexapdf/font/true_type/table/cmap_subtable.rb +1 -0
  29. data/lib/hexapdf/font/true_type/table/glyf.rb +4 -0
  30. data/lib/hexapdf/font/true_type/table/kern.rb +170 -0
  31. data/lib/hexapdf/font/true_type/table/post.rb +5 -1
  32. data/lib/hexapdf/font/true_type_wrapper.rb +71 -24
  33. data/lib/hexapdf/font/type1/afm_parser.rb +3 -2
  34. data/lib/hexapdf/font/type1/character_metrics.rb +0 -9
  35. data/lib/hexapdf/font/type1/font.rb +11 -0
  36. data/lib/hexapdf/font/type1/font_metrics.rb +6 -1
  37. data/lib/hexapdf/font/type1_wrapper.rb +51 -7
  38. data/lib/hexapdf/font_loader/standard14.rb +1 -1
  39. data/lib/hexapdf/layout.rb +51 -0
  40. data/lib/hexapdf/layout/inline_box.rb +95 -0
  41. data/lib/hexapdf/layout/line_fragment.rb +333 -0
  42. data/lib/hexapdf/layout/numeric_refinements.rb +56 -0
  43. data/lib/hexapdf/layout/style.rb +365 -0
  44. data/lib/hexapdf/layout/text_box.rb +727 -0
  45. data/lib/hexapdf/layout/text_fragment.rb +206 -0
  46. data/lib/hexapdf/layout/text_shaper.rb +155 -0
  47. data/lib/hexapdf/task.rb +0 -1
  48. data/lib/hexapdf/task/dereference.rb +1 -1
  49. data/lib/hexapdf/tokenizer.rb +3 -2
  50. data/lib/hexapdf/type/font_descriptor.rb +2 -1
  51. data/lib/hexapdf/type/font_type0.rb +3 -1
  52. data/lib/hexapdf/type/form.rb +12 -4
  53. data/lib/hexapdf/version.rb +1 -1
  54. data/test/hexapdf/common_tokenizer_tests.rb +7 -0
  55. data/test/hexapdf/content/common.rb +8 -0
  56. data/test/hexapdf/content/test_canvas.rb +10 -22
  57. data/test/hexapdf/content/test_processor.rb +4 -1
  58. data/test/hexapdf/document/test_pages.rb +16 -0
  59. data/test/hexapdf/font/test_invalid_glyph.rb +34 -0
  60. data/test/hexapdf/font/test_true_type_wrapper.rb +25 -11
  61. data/test/hexapdf/font/test_type1_wrapper.rb +26 -10
  62. data/test/hexapdf/font/true_type/table/common.rb +27 -0
  63. data/test/hexapdf/font/true_type/table/test_cmap.rb +14 -20
  64. data/test/hexapdf/font/true_type/table/test_cmap_subtable.rb +7 -0
  65. data/test/hexapdf/font/true_type/table/test_glyf.rb +8 -6
  66. data/test/hexapdf/font/true_type/table/test_head.rb +9 -13
  67. data/test/hexapdf/font/true_type/table/test_hhea.rb +16 -23
  68. data/test/hexapdf/font/true_type/table/test_hmtx.rb +4 -7
  69. data/test/hexapdf/font/true_type/table/test_kern.rb +61 -0
  70. data/test/hexapdf/font/true_type/table/test_loca.rb +7 -13
  71. data/test/hexapdf/font/true_type/table/test_maxp.rb +4 -9
  72. data/test/hexapdf/font/true_type/table/test_name.rb +14 -17
  73. data/test/hexapdf/font/true_type/table/test_os2.rb +3 -5
  74. data/test/hexapdf/font/true_type/table/test_post.rb +21 -19
  75. data/test/hexapdf/font/true_type/test_font.rb +4 -0
  76. data/test/hexapdf/font/type1/common.rb +6 -0
  77. data/test/hexapdf/font/type1/test_afm_parser.rb +9 -0
  78. data/test/hexapdf/font/type1/test_font.rb +6 -0
  79. data/test/hexapdf/layout/test_inline_box.rb +40 -0
  80. data/test/hexapdf/layout/test_line_fragment.rb +206 -0
  81. data/test/hexapdf/layout/test_style.rb +143 -0
  82. data/test/hexapdf/layout/test_text_box.rb +640 -0
  83. data/test/hexapdf/layout/test_text_fragment.rb +208 -0
  84. data/test/hexapdf/layout/test_text_shaper.rb +64 -0
  85. data/test/hexapdf/task/test_dereference.rb +1 -0
  86. data/test/hexapdf/test_writer.rb +2 -2
  87. data/test/hexapdf/type/test_font_descriptor.rb +4 -2
  88. data/test/hexapdf/type/test_font_type0.rb +7 -0
  89. data/test/hexapdf/type/test_form.rb +12 -0
  90. metadata +29 -2
@@ -1,7 +1,7 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
3
  require 'test_helper'
4
- require 'stringio'
4
+ require_relative 'common'
5
5
  require 'hexapdf/font/true_type/table/head'
6
6
 
7
7
  describe HexaPDF::Font::TrueType::Table::Head do
@@ -10,15 +10,12 @@ describe HexaPDF::Font::TrueType::Table::Head do
10
10
  @time = Time.new(2016, 05, 01)
11
11
  data << ([(@time - HexaPDF::Font::TrueType::Table::TIME_EPOCH).to_i] * 2).pack('Q>*')
12
12
  data << [-132, -152, 3423, 4231, 3, 9, -2, 0, 0].pack('s>4n2s>3')
13
- io = StringIO.new(data)
14
- @file = Object.new
15
- @file.define_singleton_method(:io) { io }
16
- @entry = HexaPDF::Font::TrueType::Table::Directory::Entry.new('head', 0, 0, io.length)
13
+ set_up_stub_true_type_font(data)
17
14
  end
18
15
 
19
16
  describe "initialize" do
20
17
  it "reads the data from the associated file" do
21
- table = HexaPDF::Font::TrueType::Table::Head.new(@file, @entry)
18
+ table = create_table(:Head)
22
19
  assert_equal('1.0', '%1.1f' % table.version)
23
20
  assert_equal('2.1', '%1.1f' % table.font_revision)
24
21
  assert_equal(42, table.checksum_adjustment)
@@ -34,22 +31,21 @@ describe HexaPDF::Font::TrueType::Table::Head do
34
31
  end
35
32
 
36
33
  it "raises an error if the magic number is false when reading from a file" do
37
- @file.io.string[12, 1] = '\x5e'
38
- assert_raises(HexaPDF::Error) { HexaPDF::Font::TrueType::Table::Head.new(@file, @entry) }
34
+ @font.io.string[12, 1] = '\x5e'
35
+ assert_raises(HexaPDF::Error) { create_table(:Head) }
39
36
  end
40
37
 
41
38
  it "raises an error if an invalid glyph format is specified when reading from a file" do
42
- @file.io.string[-1] = '\x5e'
43
- assert_raises(HexaPDF::Error) { HexaPDF::Font::TrueType::Table::Head.new(@file, @entry) }
39
+ @font.io.string[-1] = '\x5e'
40
+ assert_raises(HexaPDF::Error) { create_table(:Head) }
44
41
  end
45
42
  end
46
43
 
47
44
  describe "checksum_valid?" do
48
45
  it "checks whether an entry's checksum is valid" do
49
- @file.io.string = 254.chr * 12 + [0x5F0F3CF5].pack('N') + 254.chr * 36 + 0.chr * 4
46
+ data = 254.chr * 12 + [0x5F0F3CF5].pack('N') + 254.chr * 36 + 0.chr * 4
50
47
  @entry.checksum = (0xfefefefe * 11 + 0x5F0F3CF5) % 2**32
51
- @entry.length = @file.io.string.length
52
- table = HexaPDF::Font::TrueType::Table::Head.new(@file, @entry)
48
+ table = create_table(:Head, data)
53
49
  assert(table.checksum_valid?)
54
50
  end
55
51
  end
@@ -1,33 +1,26 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
3
  require 'test_helper'
4
- require 'stringio'
4
+ require_relative 'common'
5
5
  require 'hexapdf/font/true_type/table/hhea'
6
6
 
7
7
  describe HexaPDF::Font::TrueType::Table::Hhea do
8
- before do
8
+ it "reads the data from the associated file" do
9
9
  data = [1, 0, 10, 11, 12, 100, 101, 102, 115, 1, 0, 0, 0, 0, 0, 0, 0, 10].pack('n2s>3ns>11n')
10
- io = StringIO.new(data)
11
- @file = Object.new
12
- @file.define_singleton_method(:io) { io }
13
- @entry = HexaPDF::Font::TrueType::Table::Directory::Entry.new('hhea', 0, 0, io.length)
14
- end
10
+ set_up_stub_true_type_font(data)
11
+ table = create_table(:Hhea)
15
12
 
16
- describe "initialize" do
17
- it "reads the data from the associated file" do
18
- table = HexaPDF::Font::TrueType::Table::Hhea.new(@file, @entry)
19
- assert_equal(1, table.version)
20
- assert_equal(10, table.ascent)
21
- assert_equal(11, table.descent)
22
- assert_equal(12, table.line_gap)
23
- assert_equal(100, table.advance_width_max)
24
- assert_equal(101, table.min_left_side_bearing)
25
- assert_equal(102, table.min_right_side_bearing)
26
- assert_equal(115, table.x_max_extent)
27
- assert_equal(1, table.caret_slope_rise)
28
- assert_equal(0, table.caret_slope_run)
29
- assert_equal(0, table.caret_offset)
30
- assert_equal(10, table.num_of_long_hor_metrics)
31
- end
13
+ assert_equal(1, table.version)
14
+ assert_equal(10, table.ascent)
15
+ assert_equal(11, table.descent)
16
+ assert_equal(12, table.line_gap)
17
+ assert_equal(100, table.advance_width_max)
18
+ assert_equal(101, table.min_left_side_bearing)
19
+ assert_equal(102, table.min_right_side_bearing)
20
+ assert_equal(115, table.x_max_extent)
21
+ assert_equal(1, table.caret_slope_rise)
22
+ assert_equal(0, table.caret_slope_run)
23
+ assert_equal(0, table.caret_offset)
24
+ assert_equal(10, table.num_of_long_hor_metrics)
32
25
  end
33
26
  end
@@ -1,25 +1,22 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
3
  require 'test_helper'
4
- require 'stringio'
4
+ require_relative 'common'
5
5
  require 'hexapdf/font/true_type/table/hhea'
6
6
  require 'hexapdf/font/true_type/table/hmtx'
7
7
 
8
8
  describe HexaPDF::Font::TrueType::Table::Hhea do
9
9
  before do
10
10
  data = [1, -2, 3, -4, 5, -6].pack('ns>ns>s>2')
11
- io = StringIO.new(data)
12
- @file = Object.new
13
- @file.define_singleton_method(:io) { io }
11
+ set_up_stub_true_type_font(data)
14
12
  hhea = Object.new
15
13
  hhea.define_singleton_method(:num_of_long_hor_metrics) { 2 }
16
- @file.define_singleton_method(:[]) {|_arg| hhea }
17
- @entry = HexaPDF::Font::TrueType::Table::Directory::Entry.new('hmtx', 0, 0, io.length)
14
+ @font.define_singleton_method(:[]) {|_arg| hhea }
18
15
  end
19
16
 
20
17
  describe "initialize" do
21
18
  it "reads the data from the associated file" do
22
- table = HexaPDF::Font::TrueType::Table::Hmtx.new(@file, @entry)
19
+ table = create_table(:Hmtx)
23
20
  assert_equal(1, table[0].advance_width)
24
21
  assert_equal(-2, table[0].left_side_bearing)
25
22
  assert_equal(3, table[1].advance_width)
@@ -0,0 +1,61 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'test_helper'
4
+ require_relative 'common'
5
+ require 'hexapdf/font/true_type/table/kern'
6
+
7
+ describe HexaPDF::Font::TrueType::Table::Kern do
8
+ describe "table format 0" do
9
+ before do
10
+ data = [0, 2].pack('n2')
11
+ data << [0, 6, 0x101].pack('n3')
12
+ data << [0, 6 + 8 + 24, 0x1].pack('n3')
13
+ data << [4, 0, 0, 0, 1, 2, 10, 2, 3, -30, 3, 4, 32767, 4, 5, -32768].pack('n4n2s>n2s>n2s>n2s>')
14
+ set_up_stub_true_type_font(data)
15
+ end
16
+
17
+ it "reads the data from the associated file" do
18
+ table = create_table(:Kern)
19
+ assert_equal(0, table.version)
20
+ assert_equal(1, table.subtables.length)
21
+
22
+ subtable = table.horizontal_kerning_subtable
23
+ assert(subtable.horizontal?)
24
+ refute(subtable.minimum_values?)
25
+ refute(subtable.cross_stream?)
26
+ assert_equal(10, subtable.kern(1, 2))
27
+ assert_equal(-30, subtable.kern(2, 3))
28
+ assert_equal(32767, subtable.kern(3, 4))
29
+ assert_equal(-32768, subtable.kern(4, 5))
30
+ assert_nil(subtable.kern(1, 3))
31
+ assert_nil(subtable.kern(6, 3))
32
+ end
33
+
34
+ it "raises an error for unknown formats if configured" do
35
+ @font.config['font.true_type.unknown_format'] = :raise
36
+ assert_raises(HexaPDF::Error) { create_table(:Kern) }
37
+ end
38
+ end
39
+
40
+ describe "table format 1" do
41
+ before do
42
+ data = [1, 0, 2].pack('n2N')
43
+ data << [8 + 8, 0xC001, 0, 0, 0, 0, 0].pack('Nn6')
44
+ data << [8 + 8 + 6, 0xC000, 0].pack('Nnn')
45
+ data << [1, 0, 0, 0, 1, 2, 10].pack('n4n2s>')
46
+ set_up_stub_true_type_font(data)
47
+ end
48
+
49
+ it "reads the data from the associated file" do
50
+ table = create_table(:Kern)
51
+ assert_equal(1, table.version)
52
+ assert_equal(1, table.subtables.length)
53
+
54
+ subtable = table.subtables.first
55
+ refute(subtable.horizontal?)
56
+ refute(subtable.minimum_values?)
57
+ assert(subtable.cross_stream?)
58
+ assert_equal(10, subtable.kern(1, 2))
59
+ end
60
+ end
61
+ end
@@ -1,25 +1,21 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
3
  require 'test_helper'
4
- require 'stringio'
4
+ require_relative 'common'
5
5
  require 'hexapdf/font/true_type/table/head'
6
6
  require 'hexapdf/font/true_type/table/loca'
7
7
 
8
8
  describe HexaPDF::Font::TrueType::Table::Loca do
9
9
  before do
10
- @file = Object.new
11
- @file.define_singleton_method(:io) { @io ||= StringIO.new('') }
10
+ set_up_stub_true_type_font
12
11
  head = Object.new
13
12
  head.define_singleton_method(:index_to_loc_format) { 0 }
14
- @file.define_singleton_method(:[]) {|_arg| head }
15
- @entry = HexaPDF::Font::TrueType::Table::Directory::Entry.new('loca', 0, 0, @file.io.length)
13
+ @font.define_singleton_method(:[]) {|_arg| head }
16
14
  end
17
15
 
18
16
  describe "initialize" do
19
17
  it "reads the data in short format from the associated file" do
20
- @file.io.string = [0, 10, 30, 50, 90].pack('n*')
21
- @entry.length = @file.io.length
22
- table = HexaPDF::Font::TrueType::Table::Loca.new(@file, @entry)
18
+ table = create_table(:Loca, [0, 10, 30, 50, 90].pack('n*'))
23
19
  assert_equal([0, 20, 60, 100, 180], table.offsets)
24
20
  assert_equal(0, table.offset(0))
25
21
  assert_equal(100, table.offset(3))
@@ -28,11 +24,9 @@ describe HexaPDF::Font::TrueType::Table::Loca do
28
24
  end
29
25
 
30
26
  it "reads the data in long format from the associated file" do
31
- @file.io.string = [0, 10, 30, 50, 90].pack('N*')
32
- @file[:head].singleton_class.send(:remove_method, :index_to_loc_format)
33
- @file[:head].define_singleton_method(:index_to_loc_format) { 1 }
34
- @entry.length = @file.io.length
35
- table = HexaPDF::Font::TrueType::Table::Loca.new(@file, @entry)
27
+ @font[:head].singleton_class.send(:remove_method, :index_to_loc_format)
28
+ @font[:head].define_singleton_method(:index_to_loc_format) { 1 }
29
+ table = create_table(:Loca, [0, 10, 30, 50, 90].pack('N*'))
36
30
  assert_equal([0, 10, 30, 50, 90], table.offsets)
37
31
  end
38
32
  end
@@ -1,21 +1,18 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
3
  require 'test_helper'
4
- require 'stringio'
4
+ require_relative 'common'
5
5
  require 'hexapdf/font/true_type/table/maxp'
6
6
 
7
7
  describe HexaPDF::Font::TrueType::Table::Maxp do
8
8
  before do
9
9
  data = [1, 0, 10, 11, 12, 13, 14, 2, 15, 16, 17, 18, 19, 20, 21, 22].pack('n*')
10
- io = StringIO.new(data)
11
- @file = Object.new
12
- @file.define_singleton_method(:io) { io }
13
- @entry = HexaPDF::Font::TrueType::Table::Directory::Entry.new('hhea', 0, 0, io.length)
10
+ set_up_stub_true_type_font(data)
14
11
  end
15
12
 
16
13
  describe "initialize" do
17
14
  it "reads the version 1.0 data from the associated file" do
18
- table = HexaPDF::Font::TrueType::Table::Maxp.new(@file, @entry)
15
+ table = create_table(:Maxp)
19
16
  assert_equal(1, table.version)
20
17
  assert_equal(10, table.num_glyphs)
21
18
  assert_equal(11, table.max_points)
@@ -33,9 +30,7 @@ describe HexaPDF::Font::TrueType::Table::Maxp do
33
30
  end
34
31
 
35
32
  it "reads the version 0.5 data from the associated file" do
36
- @file.io.string = [0, 0x5000, 10].pack('n*')
37
- @entry.length = @file.io.length
38
- table = HexaPDF::Font::TrueType::Table::Maxp.new(@file, @entry)
33
+ table = create_table(:Maxp, [0, 0x5000, 10].pack('n*'))
39
34
  assert_equal(0.3125, table.version)
40
35
  assert_equal(10, table.num_glyphs)
41
36
  assert_nil(table.max_points)
@@ -1,27 +1,24 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
3
  require 'test_helper'
4
- require 'stringio'
4
+ require_relative 'common'
5
5
  require 'hexapdf/font/true_type/table/name'
6
6
 
7
7
  describe HexaPDF::Font::TrueType::Table::Name do
8
8
  before do
9
- @file = Object.new
10
- @file.define_singleton_method(:io) { @io ||= StringIO.new(''.b) }
11
-
12
- @file.io.string = [0, 3, 42].pack('n3')
13
- @file.io.string << [1, 0, 0, 0, 4, 0].pack('n6')
14
- @file.io.string << [0, 3, 1, 0, 8, 4].pack('n6')
15
- @file.io.string << [0, 3, 2, 1, 14, 12].pack('n6')
16
- @file.io.string << 'hexa'.encode('MACROMAN').b << 'hexa'.encode('UTF-16BE').b <<
9
+ data = [0, 3, 42].pack('n3') << \
10
+ [1, 0, 0, 0, 4, 0].pack('n6') << \
11
+ [0, 3, 1, 0, 8, 4].pack('n6') << \
12
+ [0, 3, 2, 1, 14, 12].pack('n6') << \
13
+ 'hexa'.encode('MACROMAN').b << \
14
+ 'hexa'.encode('UTF-16BE').b <<
17
15
  'hexapdf'.encode('UTF-16BE').b
18
-
19
- @entry = HexaPDF::Font::TrueType::Table::Directory::Entry.new('name', 0, 0, @file.io.length)
16
+ set_up_stub_true_type_font(data)
20
17
  end
21
18
 
22
19
  describe "initialize" do
23
20
  it "reads the data in format 0 from the associated file" do
24
- table = HexaPDF::Font::TrueType::Table::Name.new(@file, @entry)
21
+ table = create_table(:Name)
25
22
  assert_equal(0, table.format)
26
23
  assert_equal({}, table.language_tags)
27
24
  assert_equal('hexa', table[:copyright][0])
@@ -40,10 +37,10 @@ describe HexaPDF::Font::TrueType::Table::Name do
40
37
  end
41
38
 
42
39
  it "reads the data in format 1 from the associated file" do
43
- @file.io.string[0, 6] = [1, 3, 52].pack('n3')
44
- @file.io.string[42, 0] = [2, 4, 26, 4, 30].pack('n*')
45
- @file.io.string << 'ende'.encode('UTF-16BE').b
46
- table = HexaPDF::Font::TrueType::Table::Name.new(@file, @entry)
40
+ @font.io.string[0, 6] = [1, 3, 52].pack('n3')
41
+ @font.io.string[42, 0] = [2, 4, 26, 4, 30].pack('n*')
42
+ @font.io.string << 'ende'.encode('UTF-16BE').b
43
+ table = create_table(:Name)
47
44
  assert_equal(1, table.format)
48
45
  assert_equal({0x8000 => 'en', 0x8001 => 'de'}, table.language_tags)
49
46
  end
@@ -51,7 +48,7 @@ describe HexaPDF::Font::TrueType::Table::Name do
51
48
 
52
49
  describe "NameRecord" do
53
50
  before do
54
- @table = HexaPDF::Font::TrueType::Table::Name.new(@file, @entry)
51
+ @table = create_table(:Name)
55
52
  end
56
53
 
57
54
  describe "platform?" do
@@ -1,7 +1,7 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
3
  require 'test_helper'
4
- require 'stringio'
4
+ require_relative 'common'
5
5
  require 'hexapdf/font/true_type/table/os2'
6
6
 
7
7
  describe HexaPDF::Font::TrueType::Table::OS2 do
@@ -9,14 +9,12 @@ describe HexaPDF::Font::TrueType::Table::OS2 do
9
9
  data = [5, -1, 2, 3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15,
10
10
  'PANOSPANOS', 16, 17, 'VEND', 16, 17, 18, -19, -20, -21, 22, 23, 24, -25, -26,
11
11
  27, 28, 29, 30, 31].pack('ns>n2s>12a10Q>2a4n3s>3n2Q>s>2n5')
12
- @file = Object.new
13
- @file.define_singleton_method(:io) { @io ||= StringIO.new(data) }
14
- @entry = HexaPDF::Font::TrueType::Table::Directory::Entry.new('OS/2', 0, 0, @file.io.length)
12
+ set_up_stub_true_type_font(data)
15
13
  end
16
14
 
17
15
  describe "initialize" do
18
16
  it "reads the data from the associated file" do
19
- table = HexaPDF::Font::TrueType::Table::OS2.new(@file, @entry)
17
+ table = create_table(:OS2)
20
18
  assert_equal(5, table.version)
21
19
  assert_equal(-1, table.x_avg_char_width)
22
20
  assert_equal(200, table.weight_class)
@@ -1,20 +1,18 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
3
  require 'test_helper'
4
- require 'stringio'
4
+ require_relative 'common'
5
5
  require 'hexapdf/font/true_type/table/post'
6
6
 
7
7
  describe HexaPDF::Font::TrueType::Table::Post do
8
8
  before do
9
9
  data = [1, 0, 1, 0, -142, 15, 0, 0, 0, 0, 0].pack('n4s>2N5')
10
- @file = Object.new
11
- @file.define_singleton_method(:io) { @io ||= StringIO.new(data) }
12
- @entry = HexaPDF::Font::TrueType::Table::Directory::Entry.new('post', 0, 0, @file.io.length)
10
+ set_up_stub_true_type_font(data)
13
11
  end
14
12
 
15
13
  describe "initialize" do
16
14
  it "reads the format 1 data from the associated file" do
17
- table = HexaPDF::Font::TrueType::Table::Post.new(@file, @entry)
15
+ table = create_table(:Post)
18
16
  assert_equal(1, table.format)
19
17
  assert_equal(1, table.italic_angle)
20
18
  assert_equal(-142, table.underline_position)
@@ -32,11 +30,10 @@ describe HexaPDF::Font::TrueType::Table::Post do
32
30
  end
33
31
 
34
32
  it "reads the format 2 data from the associated file" do
35
- @file.io.string[0, 2] = [2].pack('n')
36
- @file.io.string << ([260, 0] + (1..257).to_a.reverse + [258, 259]).pack('n*')
37
- @file.io.string << [4, "hexa", 3, "pdf"].pack('CA4CA3')
38
- @entry.length = @file.io.length
39
- table = HexaPDF::Font::TrueType::Table::Post.new(@file, @entry)
33
+ @font.io.string[0, 2] = [2].pack('n')
34
+ @font.io.string << ([260, 0] + (1..257).to_a.reverse + [258, 259]).pack('n*')
35
+ @font.io.string << [4, "hexa", 3, "pdf"].pack('CA4CA3')
36
+ table = create_table(:Post, @font.io.string)
40
37
  assert_equal(2, table.format)
41
38
  assert_equal('.notdef', table[0])
42
39
  assert_equal('A', table[258 - 36])
@@ -47,8 +44,8 @@ describe HexaPDF::Font::TrueType::Table::Post do
47
44
  end
48
45
 
49
46
  it "reads the format 3 data from the associated file" do
50
- @file.io.string[0, 2] = [3].pack('n')
51
- table = HexaPDF::Font::TrueType::Table::Post.new(@file, @entry)
47
+ @font.io.string[0, 2] = [3].pack('n')
48
+ table = create_table(:Post, @font.io.string)
52
49
  assert_equal(3, table.format)
53
50
  assert_equal('.notdef', table[0])
54
51
  assert_equal('.notdef', table[36])
@@ -56,10 +53,9 @@ describe HexaPDF::Font::TrueType::Table::Post do
56
53
  end
57
54
 
58
55
  it "reads the format 4 data from the associated file" do
59
- @file.io.string[0, 2] = [4].pack('n')
60
- @file.io.string << [0x1234, 0x5678].pack('n*')
61
- @entry.length = @file.io.length
62
- table = HexaPDF::Font::TrueType::Table::Post.new(@file, @entry)
56
+ @font.io.string[0, 2] = [4].pack('n')
57
+ @font.io.string << [0x1234, 0x5678].pack('n*')
58
+ table = create_table(:Post, @font.io.string)
63
59
  assert_equal(4, table.format)
64
60
  assert_equal(0x1234, table[0])
65
61
  assert_equal(0x5678, table[1])
@@ -68,9 +64,15 @@ describe HexaPDF::Font::TrueType::Table::Post do
68
64
  assert_equal(0xFFFF, table[1_000_000])
69
65
  end
70
66
 
71
- it "raises an error if an unsupported format is given" do
72
- @file.io.string[0, 2] = [5].pack('n')
73
- assert_raises(HexaPDF::Error) { HexaPDF::Font::TrueType::Table::Post.new(@file, @entry) }
67
+ it "handles unsupported formats" do
68
+ @font.io.string[0, 2] = [5].pack('n')
69
+
70
+ @font.config['font.true_type.unknown_format'] = :ignore
71
+ table = create_table(:Post, @font.io.string)
72
+ assert_equal('.notdef', table[0])
73
+
74
+ @font.config['font.true_type.unknown_format'] = :raise
75
+ assert_raises(HexaPDF::Error) { create_table(:Post) }
74
76
  end
75
77
  end
76
78
  end