Class: ODDB::FiPDF::ParagraphWrapper

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
ext/fipdf/src/paragraph_wrapper.rb,
ext/fipdf/test/paragraph_wrapper_test.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(paragraph) ⇒ ParagraphWrapper

Returns a new instance of ParagraphWrapper.



11
12
13
14
# File 'ext/fipdf/src/paragraph_wrapper.rb', line 11

def initialize(paragraph)
	@paragraph = paragraph
	super
end

Instance Attribute Details

#hyphenatorObject

Returns the value of attribute hyphenator.



10
11
12
# File 'ext/fipdf/src/paragraph_wrapper.rb', line 10

def hyphenator
  @hyphenator
end

#wrapper_classObject

Returns the value of attribute wrapper_class.



14
15
16
# File 'ext/fipdf/test/paragraph_wrapper_test.rb', line 14

def wrapper_class
  @wrapper_class
end

#writerObject

Returns the value of attribute writer.



14
15
16
# File 'ext/fipdf/test/paragraph_wrapper_test.rb', line 14

def writer
  @writer
end

Instance Method Details

#enforce_page_break?(first_height, column_height, width, format) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
# File 'ext/fipdf/src/paragraph_wrapper.rb', line 44

def enforce_page_break?(first_height, column_height, width, format)
	total_lines = format.line_count(text(), width)
	first_lines = lines_per_height(first_height, format)
	column_lines = lines_per_height(column_height, format)
	if((total_lines - first_lines) % column_lines == 1)
		total_lines - 2
	else
		false
	end
end

#format_textObject



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
# File 'ext/fipdf/src/paragraph_wrapper.rb', line 54

def format_text
	return '' unless(@paragraph.respond_to?(:text) \
		&& @paragraph.respond_to?(:formats))
	txt = @paragraph.text
	text = ""
	@paragraph.formats.each { |format|
		str = txt[format.range].to_s
		if(format.italic?)
			str = "<i>" + str + "</i>"
		end
		if(format.bold?)
			str = "<b>" + str + "</b>"
		end
		if(format.symbol?)
         body = str.dup
         head = body.slice!(/^\s*/u)
         tail = body.slice!(/\s*$/u)
         str = head << '<f:Symbol>' << body << '</f>' << tail
		end
		text << str
	}
	unless(@paragraph.preformatted?)
		text  = text.sub(/^-/u, "")
	end
	text
end

#image?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'ext/fipdf/src/paragraph_wrapper.rb', line 41

def image?
  @paragraph.is_a?(Text::ImageLink)
end

#lines_per_height(height, format) ⇒ Object



80
81
82
83
# File 'ext/fipdf/src/paragraph_wrapper.rb', line 80

def lines_per_height(height, format)
	font_size = format.size
	(height / format.font_height(font_size)).floor
end

#need_new_page?(height, width, formats) ⇒ Boolean

Returns:

  • (Boolean)


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
# File 'ext/fipdf/src/paragraph_wrapper.rb', line 15

def need_new_page?(height, width, formats)
     if image?
       path = File.join(PROJECT_ROOT, 'doc', @paragraph.src)
       if File.exist?(path)
         info = PDF::Writer::Graphics::ImageInfo.new(File.read(path))
         width * info.height / info.width > height
       end
     else
       format = if(preformatted?)
         formats[:preformatted]
       else
         formats[:paragraph]
       end
       paragraph_height = format.get_height(text(), width)
       num_lines = format.line_count(text(), width)
       available_height = height + format.spacing_before(text())
       lines_on_page = lines_per_height(available_height, format)
       if(num_lines <= 3 && paragraph_height > height)
         true
       elsif(num_lines > 3 && lines_on_page < 2)
         true
       else
         false
       end
     end
end

#preformatted?Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
90
# File 'ext/fipdf/src/paragraph_wrapper.rb', line 84

def preformatted?
	if(@paragraph.respond_to? :preformatted?)
		@paragraph.preformatted?
	else
		false
	end
end

#textObject



91
92
93
94
95
96
97
98
99
100
101
102
# File 'ext/fipdf/src/paragraph_wrapper.rb', line 91

def text
  @text ||= begin
    case @paragraph
    when Text::ImageLink
      @paragraph.to_s
    when Text::Table
      @paragraph.to_s :width => 50, :hyphenator => @hyphenator
    else
      format_text
    end
  end
end