Module: PdfExtract::Characters

Defined in:
lib/model/characters.rb

Class Method Summary collapse

Class Method Details

.build_fonts(page) ⇒ Object



128
129
130
131
132
133
134
135
136
137
# File 'lib/model/characters.rb', line 128

def self.build_fonts page
  fonts = {}
  font_metrics = {}
  page.fonts.each do |label, font_obj|
    font = PDF::Reader::Font.new(page.objects, font_obj)
    fonts[label] = font
    font_metrics[label] = FontMetrics.new font
  end
  [fonts, font_metrics]
end

.find_media_box(page, objects) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/model/characters.rb', line 52

def self.find_media_box page, objects
  if page[:MediaBox]
    page[:MediaBox]
  elsif page[:Parent]
    find_media_box objects[page[:Parent]], objects
  else
    [0, 0, 0, 0]
  end
end

.glyph_ascent(c, state) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/model/characters.rb', line 18

def self.glyph_ascent c, state
  if state.last[:font_metrics].nil? || state.last[:font_metrics].ascent.nil?
    0
  else
    state.last[:font_metrics].ascent / 1000.0
  end
end

.glyph_descent(c, state) ⇒ Object

TODO Implement writing mode and :FontMatrix.



10
11
12
13
14
15
16
# File 'lib/model/characters.rb', line 10

def self.glyph_descent c, state
  if state.last[:font_metrics].nil? || state.last[:font_metrics].descent.nil?
    0
  else
    state.last[:font_metrics].descent / 1000.0
  end
end

.glyph_displacement(c, state) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/model/characters.rb', line 38

def self.glyph_displacement c, state
  # For non-Type3 fonts, vertical displacement is the glyph width,
  # horizontal displacement is always 0. Note glyph width is given
  # in 1000ths of text units.
  if state.last[:font_metrics].nil?
    # XXX Why are some font resources not reported via resource_font?
    # Bug in pdf-reader? Possibly because of :Font entry in graphics
    # state set.
    [ 0, 0 ]
  else
    [ state.last[:font_metrics].glyph_width(c) / 1000.0, 0 ]
  end
end

.glyph_height(c, state) ⇒ Object



32
33
34
35
36
# File 'lib/model/characters.rb', line 32

def self.glyph_height c, state
  # :Ascent and :Descent from the :FontDescriptor can be used to determine
  # maximum glyph height.
  glyph_ascent(c, state) - glyph_descent(c, state)
end

.glyph_width(c, state) ⇒ Object



26
27
28
29
30
# File 'lib/model/characters.rb', line 26

def self.glyph_width c, state
  # :Widths may be used to determine glyph width. This is the same as
  # horizontal displacemnt.
  glyph_displacement(c, state)[0]
end

.include_in(pdf) ⇒ Object



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
# File 'lib/model/characters.rb', line 139

def self.include_in pdf

  pdf.spatials :characters do |parser|
    state = []
    page = nil
    fonts = {}
    font_metrics = {}
    page_n = 0
    render_state = {
      :tm => Matrix.identity(3),
      :tlm => Matrix.identity(3)
    }

    # parser.for :resource_font do |data|
    #   puts data
    #   fonts[data[0]] = data[1]
    #   font_metrics[data[0]] = FontMetrics.new data[1]
    #   nil
    # end

    parser.for :begin_page do |data|
      page = data[0]
      page_n = page_n.next

      fonts, font_metrics = build_fonts page

      state << {
        :h_scale => 100,
        :char_spacing => 0,
        :word_spacing => 0,
        :leading => 0,
        :rise => 0,
        :font => nil,
        :font_metrics => nil,
        :font_size => 0,
        :ctm => Matrix.identity(3)
      }
      nil
    end

    parser.for :end_page do |data|
      state.pop
      nil
    end

    parser.for :begin_text_object do |data|
      render_state = {
        :tm => Matrix.identity(3),
        :tlm => Matrix.identity(3)
      }
      nil
    end

    # Graphics state operators.

    parser.for :set_graphics_state_parameters do |data|
      # TODO Handle gs graphics state dictionary set operation for
      # :Font dictionary entries. Probably why font is sometimes nil.
      # puts data
      nil
    end

    parser.for :save_graphics_state do |data|
      state.push state.last.dup
      nil
    end

    parser.for :restore_graphics_state do |data|
      state.pop
      nil
    end

    parser.for :concatenate_matrix do |data|
      a, b, c, d, e, f = data
      ctm = state.last[:ctm]
      state.last[:ctm] = Matrix[ [a, b, 0], [c, d, 0], [e, f, 1] ] * ctm
      nil
    end

    # State change operators.

    parser.for :set_text_leading do |data|
      state.last[:leading] = data.first
      nil
    end

    parser.for :set_text_rise do |data|
      state.last[:rise] = data.first
      nil
    end

    parser.for :set_character_spacing do |data|
      state.last[:char_spacing] = data.first
      nil
    end

    parser.for :set_word_spacing do |data|
      state.last[:word_spacing] = data.first
      nil
    end

    parser.for :set_horizontal_text_scaling do |data|
      state.last[:h_scale] = data.first
      nil
    end

    # Position change operators.

    parser.for :move_text_position do |data|
      render_state[:tm] = Matrix[
        [1, 0, 0], [0, 1, 0], [data[0], data[1], 1]
      ] * render_state[:tlm]
      render_state[:tlm] = render_state[:tm]
      nil
    end

    parser.for :move_text_position_and_set_leading do |data|
      state.last[:leading] = -data[1]
      render_state[:tm] = Matrix[
        [1, 0, 0], [0, 1, 0], [data[0], data[1], 1]
      ] * render_state[:tlm]
      render_state[:tlm] = render_state[:tm]
      nil
    end

    # Font change operators.

    parser.for :set_text_font_and_size do |data|
      state.last[:font] = fonts[data[0]]
      state.last[:font_metrics] = font_metrics[data[0]]
      state.last[:font_size] = data[1]
      nil
    end

    # Text matrix change operators.

    parser.for :set_text_matrix_and_text_line_matrix do |data|
      # --     --
      # | a b 0 |
      # | c d 0 |
      # | e f 1 |
      # --     --
      a, b, c, d, e, f = data
      render_state[:tm] = Matrix[ [a, b, 0], [c, d, 0], [e, f, 1] ]
      render_state[:tlm] = Matrix[ [a, b, 0], [c, d, 0], [e, f, 1] ]
      nil
    end

    # New line operators.

    parser.for :move_to_start_of_next_line do |data|
      render_state[:tm] = Matrix[
        [1, 0, 0], [0, 1, 0], [0, -state.last[:leading], 1]
      ] * render_state[:tlm]
      render_state[:tlm] = render_state[:tm]
      nil
    end

    # Show text operators.

    parser.for :set_spacing_next_line_show_text do |data|
      state.last[:word_spacing] = data[0]
      state.last[:char_spacing] = data[1]
      
      render_state[:tm] = Matrix[
        [1, 0, 0], [0, 1, 0], [0, -state.last[:leading], 1]
      ] * render_state[:tlm]
      render_state[:tlm] = render_state[:tm]

      make_text_runs data[2], 0, state, render_state, page, page_n
    end

    parser.for :move_to_next_line_and_show_text do |data|
      render_state[:tm] = Matrix[
        [1, 0, 0], [0, 1, 0], [0, -state.last[:leading], 1]
      ] * render_state[:tlm]
      render_state[:tlm] = render_state[:tm]
      
      make_text_runs data.first, 0, state, render_state, page, page_n
    end

    parser.for :show_text do |data|
      make_text_runs data.first, 0, state, render_state, page, page_n
    end
    
    parser.for :show_text_with_positioning do |data|
      data = data.first
      runs = []
      tj = 0
      
      data.each do |item|
        case item.class.to_s
        when "Fixnum", "Float"
          tj = item
        when "String"
          runs << make_text_runs(item, tj, state, render_state, page, page_n)
          tj = 0
        end
      end
      
      runs.flatten
    end
    
  end
end

.make_text_runs(text, tj, state, render_state, page, page_number) ⇒ Object



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
# File 'lib/model/characters.rb', line 62

def self.make_text_runs text, tj, state, render_state, page, page_number
  # TODO Mul UserUnit if specified by page.
  # TODO Include writing mode, so that runs can be joined either
  #      virtically or horizontally in the join stage.
  
  objs = []
  h_scale_mod = (state.last[:h_scale] / 100.0)
  s = state.last
  
  disp_x, disp_y = [0, 0]
  spacing = 0
  tx = ((disp_x - (tj / 1000.0)) * s[:font_size] + spacing) * h_scale_mod
  ty = (disp_y - (tj / 1000.0)) * s[:font_size] + spacing

  # TODO Should use either tx or ty depending on writing mode.
  render_state[:tm] = Matrix[ [1, 0, 0], [0, 1, 0], [tx, 0, 1] ] * render_state[:tm]

  # tj applies only to the first char of the Tj op.
  tj = 0
  
  text.each_char do |c|
    trm = Matrix[ [s[:font_size] * h_scale_mod, 0, 0],
                  [0, s[:font_size], 0],
                  [0, s[:rise], 1] ]
    trm = trm * render_state[:tm] * state.last[:ctm]

    bl_pos = Matrix.rows( [ [0, glyph_descent(c, state), 1] ])
    bl_pos = bl_pos * trm

    width = glyph_width(c, state)
    height = glyph_descent(c, state) + glyph_height(c, state)

    tr_pos = Matrix.rows([ [width, height, 1] ])
    tr_pos = tr_pos * trm

    px = bl_pos.row(0)[0]
    py = bl_pos.row(0)[1]

    media_box = find_media_box(page.page_object, page.objects)
    
    objs << {
      :x => px,
      :y => py,
      :width => tr_pos.row(0)[0] - px,
      :height => tr_pos.row(0)[1] - py,
      :line_height => tr_pos.row(0)[1] - py,
      :content => state.last[:font].to_utf8(c),
      :page => page_number,
      :font => state.last[:font].basefont,
      :page_width => media_box[2] - media_box[0],
      :page_height => media_box[3] - media_box[1]
    }
    
    disp_x, disp_y = glyph_displacement(c, state)
    spacing = s[:char_spacing] if c != ' '
    spacing = s[:word_spacing] if c == ' '
    tx = ((disp_x - (tj / 1000.0)) * s[:font_size] + spacing) * h_scale_mod
    ty = (disp_y - (tj / 1000.0)) * s[:font_size] + spacing

    # TODO Should use either tx or ty depending on writing mode.
    render_state[:tm] = Matrix[ [1, 0, 0], [0, 1, 0], [tx, 0, 1] ] * render_state[:tm]
  end
  
  objs
end