Class: ODDB::FiParse::FachinfoTextHandler

Inherits:
Rwv2::TextHandler
  • Object
show all
Defined in:
ext/fiparse/src/fachinfo_doc.rb,
ext/fiparse/test/test_fachinfo_doc_parser.rb

Constant Summary

CHARS_PER_INCH =
14
TWIPS_PER_CHAR =
(72*20 / CHARS_PER_INCH ).to_i
DEFAULT_TAB_WIDTH =
720

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (FachinfoTextHandler) initialize

A new instance of FachinfoTextHandler



200
201
202
203
204
205
206
# File 'ext/fiparse/src/fachinfo_doc.rb', line 200

def initialize
  @writers = []
  @table_handler = FachinfoTableHandler.new
  @cutoff_fontsize = 40
  @max_fontsize = 0
  @iconv = Iconv.new('UTF-8', 'UTF-16')
end

Instance Attribute Details

- (Object) cutoff_fontsize

Returns the value of attribute cutoff_fontsize



199
200
201
# File 'ext/fiparse/src/fachinfo_doc.rb', line 199

def cutoff_fontsize
  @cutoff_fontsize
end

- (Object) max_fontsize (readonly)

Returns the value of attribute max_fontsize



198
199
200
# File 'ext/fiparse/src/fachinfo_doc.rb', line 198

def max_fontsize
  @max_fontsize
end

- (Object) table_handler (readonly)

Returns the value of attribute table_handler



198
199
200
# File 'ext/fiparse/src/fachinfo_doc.rb', line 198

def table_handler
  @table_handler
end

- (Object) tabs

Returns the value of attribute tabs



24
25
26
# File 'ext/fiparse/test/test_fachinfo_doc_parser.rb', line 24

def tabs
  @tabs
end

- (Object) writers (readonly)

Returns the value of attribute writers



198
199
200
# File 'ext/fiparse/src/fachinfo_doc.rb', line 198

def writers
  @writers
end

Instance Method Details

- (Object) _run_of_text(text, char_props)



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
# File 'ext/fiparse/src/fachinfo_doc.rb', line 235

def _run_of_text(text, char_props)
  #puts sprintf("%2i %s -> %s",char_props.fontsize, same_font?(@current_char_props, char_props), text[0,10])
  if(!same_font?(@current_char_props, char_props))
    if(char_props.fontsize >= @cutoff_fontsize \
       && (@writer.nil? || @writer.complete?))
      @writer = FachinfoDocWriter.new
      @table_handler.writer = @writer
      @writer.cutoff_fontsize = @cutoff_fontsize
      @writers.push(@writer)
    end
    @max_fontsize = [@max_fontsize, char_props.fontsize].max
  end
  if(/^-{5,}$/u.match(text))
    @preformatted = text.length
  elsif(@preformatted && (text.length > @preformatted))
    @preformatted = nil
    @current_char_props = nil
  elsif(@preformatted \
    && !same_font?(@current_char_props, char_props))
    @preformatted = nil
  end
  unless(@writer.nil?)
    unless(same_font?(@current_char_props, char_props))
      @writer.new_font(char_props, text)
    end
    if(@preformatted)
      @writer.send_literal_data(expand_tabs(text))
    #elsif(@table_handler.in_table?)
    #  @table_handler.send_flowing_data(text)
    else
      @writer.send_flowing_data(text)
    end
  end
  @current_char_props = char_props
end

- (Object) expand_tabs(text)



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
# File 'ext/fiparse/src/fachinfo_doc.rb', line 271

def expand_tabs(text)
    while(pos = text.index("\t")) do
      twips = pos * TWIPS_PER_CHAR
      next_tab = @tabs.select { |tab|
        tab.position > twips
      }.sort_by { |tab|
        tab.position
      }.first
      next_pos, align = if(next_tab.nil?)
        ((twips / DEFAULT_TAB_WIDTH) + 1) * DEFAULT_TAB_WIDTH
      else
        [next_tab.position, next_tab.align]
      end
      distance = ((next_pos - twips) / TWIPS_PER_CHAR) + 1
      if(align == Rwv2::TabDescriptor::ALIGN_CENTER)
        end_pos = text.index("\t", pos.next) || (text.length - 1)
        text_length = end_pos - pos
        distance = [distance - (text_length / 2), 1].max
      end
      text[pos,1] = " " * distance
    end
    text
  end
  def same_font?(font1, font2)
    font1.class == font2.class \
    && font1.fontsize == font2.fontsize \
      && font1.bold? == font2.bold? \
      && font1.italic? == font2.italic?
  end
end

- (Object) paragraph_end



207
208
209
210
211
212
213
214
215
216
217
# File 'ext/fiparse/src/fachinfo_doc.rb', line 207

def paragraph_end
  unless(@writer.nil?)
    if(@preformatted)
      @writer.send_literal_data("\n")
    else
      @current_char_props = nil
      @writer.send_flowing_data("\n")
      @writer.send_line_break
    end
  end
end

- (Object) paragraph_start(paragraph_props)



218
219
220
221
222
223
224
225
# File 'ext/fiparse/src/fachinfo_doc.rb', line 218

def paragraph_start(paragraph_props)
  tabs = paragraph_props.tab_descriptors
  unless(tabs.any? { |tab| tab.position > 9000})
    @tabs = tabs
  else
    @tabs = []
  end
end

- (Object) run_of_text(text, char_props)



226
227
228
229
230
231
232
233
234
# File 'ext/fiparse/src/fachinfo_doc.rb', line 226

def run_of_text(text, char_props)
  text = @iconv.iconv(text)
  text.split(/\v/u).each_with_index { |run, idx|
    if(idx > 0 && @writer)
      @writer.send_line_break
    end
    _run_of_text(run, char_props)
  }
end

- (Boolean) same_font?(font1, font2)

Returns:

  • (Boolean)


294
295
296
297
298
299
# File 'ext/fiparse/src/fachinfo_doc.rb', line 294

def same_font?(font1, font2)
  font1.class == font2.class \
  && font1.fontsize == font2.fontsize \
    && font1.bold? == font2.bold? \
    && font1.italic? == font2.italic?
end