File: text.rb

package info (click to toggle)
ruby-pdf-inspector 1.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 176 kB
  • sloc: ruby: 293; makefile: 2
file content (73 lines) | stat: -rw-r--r-- 1,989 bytes parent folder | download | duplicates (2)
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
module PDF
  class Inspector
    class Text < Inspector
      attr_accessor :font_settings, :size, :strings
      attr_accessor :character_spacing, :word_spacing
      attr_accessor :kerned, :text_rendering_mode, :positions
      attr_accessor :horizontal_text_scaling

      def initialize
        @font_settings = []
        @fonts = {}
        @font_objects = {}
        @strings = []
        @character_spacing = []
        @word_spacing = []
        @kerned = []
        @text_rendering_mode = []
        @positions = []
        @horizontal_text_scaling = []
      end

      def page=(page)
        @state = PDF::Reader::PageState.new(page)
        page.fonts.each do |label, stream|
          @fonts[label]        = stream[:BaseFont]
          @font_objects[label] = PDF::Reader::Font.new(page.objects, stream)
        end
      end

      def set_text_font_and_size(*params)
        @state.set_text_font_and_size(*params)
        @font_settings << { name: @fonts[params[0]], size: params[1] }
      end

      def move_text_position(tx, ty)
        @positions << [tx, ty]
      end

      def show_text(*params)
        @kerned << false
        @strings << @state.current_font.to_utf8(params[0])
      end

      def show_text_with_positioning(*params)
        @kerned << true
        # ignore kerning information
        show_text params[0].reject { |e|
          e.is_a? Numeric
        }.join
      end

      def set_text_rendering_mode(*params)
        @state.set_text_rendering_mode(*params)
        @text_rendering_mode << params[0]
      end

      def set_character_spacing(spacing)
        @state.set_character_spacing(spacing)
        @character_spacing << spacing
      end

      def set_word_spacing(*params)
        @state.set_word_spacing(*params)
        @word_spacing << params[0]
      end

      def set_horizontal_text_scaling(scaling)
        @state.set_horizontal_text_scaling(scaling)
        @horizontal_text_scaling << scaling
      end
    end
  end
end