File: test-page.rb

package info (click to toggle)
ruby-gnome 4.3.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,320 kB
  • sloc: ruby: 55,206; ansic: 29,012; xml: 333; sh: 229; cpp: 45; makefile: 42
file content (134 lines) | stat: -rw-r--r-- 4,288 bytes parent folder | download | duplicates (3)
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
# Copyright (C) 2008-2021  Ruby-GNOME Project Team
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

class TestPage < Test::Unit::TestCase
  include PopplerTestUtils

  def test_get_image
    document = Poppler::Document.new(image_pdf)
    page, mapping = find_first_image_mapping(document)
    assert_kind_of(Cairo::ImageSurface, page.get_image(mapping.image_id))
    assert_kind_of(Cairo::ImageSurface, mapping.image)
  end

  def test_selection_region
    document = Poppler::Document.new(form_pdf)
    page = document[0]
    rectangle = Poppler::Rectangle.new(0, 0, *page.size)
    region = page.get_selection_region(0.5, :word, rectangle)
    assert_kind_of(Poppler::Rectangle, region[0])
  end

  def test_annot_mapping
    document = Poppler::Document.new(form_pdf)
    page = document[0]
    assert_equal([Poppler::AnnotMapping],
                 page.annot_mapping.collect {|mapping| mapping.class}.uniq)
    mapping = page.annot_mapping[0]
    assert_kind_of(Poppler::Rectangle, mapping.area)
    assert_kind_of(Poppler::Annot, mapping.annot)
  end

  def test_text_layout
    only_poppler_version(0, 16, 0)
    document = Poppler::Document.new(form_pdf)
    page = document[0]
    layout = page.text_layout
    assert_equal([75, 67, 81, 80],
                 layout[0].to_a.collect(&:round))
  end

  sub_test_case("#find_text") do
    def normalize_rectangles(rectangles)
      rectangles.collect do |rectangle|
        rectangle.to_a.collect(&:round)
      end
    end

    test "no options" do
      document = Poppler::Document.new(multiple_pages_pdf)
      page = document[0]
      text_areas = page.find_text("firs")
      assert_equal([[78, 771, 94, 785]],
                   normalize_rectangles(text_areas))
    end

    test "with default option" do
      document = Poppler::Document.new(multiple_pages_pdf)
      page = document[0]
      text_areas = page.find_text("firs", :default)
      assert_equal([[78, 771, 94, 785]],
                   normalize_rectangles(text_areas))
    end

    test "with options and bad text" do
      document = Poppler::Document.new(multiple_pages_pdf)
      page = document[0]
      text_areas = page.find_text("fIrs", [:whole_words_only, :case_sensitive])
      assert_equal([],
                   normalize_rectangles(text_areas))
    end

    test "with options and good text" do
      document = Poppler::Document.new(multiple_pages_pdf)
      page = document[0]
      text_areas = page.find_text("first", [:whole_words_only, :case_sensitive])
      assert_equal([[78, 771, 98, 785]],
                   normalize_rectangles(text_areas))
    end
  end

  sub_test_case("#thumbnail_size") do
    def create_document
      pdf = StringIO.new
      surface = Cairo::PDFSurface.new(pdf, 10, 20)
      yield(surface) if block_given?
      surface.finish
      Poppler::Document.new(pdf.string)
    end

    def only_cairo_version(major, minor, micro=nil)
      unless Cairo.satisfied_version?(major, minor, micro)
        omit("Require cairo >= #{major}.#{minor}.#{micro}")
      end
    end

    def test_exist
      only_cairo_version(1, 15, 4)
      document = create_document do |surface|
        surface.set_thumbnail_size(5, 10)
      end
      page = document[0]
      assert_equal([5, 10], page.thumbnail_size)
    end

    def test_not_exist
      document = create_document
      page = document[0]
      assert_nil(page.thumbnail_size)
    end
  end

  private
  def find_first_image_mapping(document)
    document.each do |page|
      page.image_mapping.each do |mapping|
        return [page, mapping]
      end
    end
    nil
  end
end