Class: HPDFPage
- Inherits:
-
Object
- Object
- HPDFPage
- Defined in:
- lib/eleanor/hpdfpaper.rb
Overview
Some handy, high-level methods for libHaru’s HPDFPage class.
Instance Method Summary collapse
-
#begin_text ⇒ Object
Overridden to implement underlining.
- #begin_text_ ⇒ Object
-
#end_text ⇒ Object
Overridden to implement underlining.
- #end_text_ ⇒ Object
-
#line(str, line_height_pts) ⇒ Object
Writes a line,
str
, to the page and moves the text pointer down the page byline_height_pts
points. -
#line_center(str, line_height_pts) ⇒ Object
Writes a centered line at the current vertical position.
-
#line_flush_right(str, line_height_pts, margin_right) ⇒ Object
Writes a line flushed to the right margin, which is specified by
margin_right
, a Length, at the current vertical position. -
#margin_left=(length) ⇒ Object
Moves the text pointer horizontally to the given margin
length
. -
#move_down(length) ⇒ Object
Moves the text pointer down the page by
length
. -
#move_to_top ⇒ Object
Moves the text pointer to the very first line on the page.
-
#text_width(str) ⇒ Object
Overridden to implement underlining.
- #text_width_ ⇒ Object
Instance Method Details
#begin_text ⇒ Object
Overridden to implement underlining.
76 77 78 79 |
# File 'lib/eleanor/hpdfpaper.rb', line 76 def begin_text self.begin_text_ @underline_coords= [] end |
#begin_text_ ⇒ Object
71 |
# File 'lib/eleanor/hpdfpaper.rb', line 71 alias :begin_text_ :begin_text |
#end_text ⇒ Object
Overridden to implement underlining.
82 83 84 85 86 87 88 89 |
# File 'lib/eleanor/hpdfpaper.rb', line 82 def end_text self.end_text_ @underline_coords.each do |pair| self.move_to(pair[0][0], pair[0][1]) self.line_to(pair[1][0], pair[1][1]) self.stroke end end |
#end_text_ ⇒ Object
72 |
# File 'lib/eleanor/hpdfpaper.rb', line 72 alias :end_text_ :end_text |
#line(str, line_height_pts) ⇒ Object
Writes a line, str
, to the page and moves the text pointer down the page by line_height_pts
points. Any text surrounded by underscores is underlined.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/eleanor/hpdfpaper.rb', line 94 def line str, line_height_pts unless str.nil? || str.empty? pos= self.get_current_text_pos # implement underlining: split the line on "_" => every other segment is a # string of text that should be underlined. segs= str.split(/_/) segs.each_with_index do |seg, si| if si % 2 == 1 y= pos[1] - 1 # draw underline one point below line coord1= [pos[0] + self.text_width(segs[0..(si - 1)].join), y] coord2= [coord1[0] + self.text_width(seg), y] @underline_coords << [coord1, coord2] end end # finally, print the line self.show_text(segs.join) end self.move_text_pos(0, -line_height_pts) end |
#line_center(str, line_height_pts) ⇒ Object
Writes a centered line at the current vertical position. See line.
115 116 117 118 119 120 121 |
# File 'lib/eleanor/hpdfpaper.rb', line 115 def line_center str, line_height_pts self.move_text_pos(-self.get_current_text_pos[0] + (self.get_width / 2.0) - (self.text_width(str) / 2.0), 0) self.line(str, line_height_pts) end |
#line_flush_right(str, line_height_pts, margin_right) ⇒ Object
Writes a line flushed to the right margin, which is specified by margin_right
, a Length, at the current vertical position. See line.
125 126 127 128 129 130 131 132 |
# File 'lib/eleanor/hpdfpaper.rb', line 125 def line_flush_right str, line_height_pts, margin_right self.move_text_pos(-self.get_current_text_pos[0] + self.get_width - (margin_right.to_points.to_f) - self.text_width(str), 0) self.line(str, line_height_pts) end |
#margin_left=(length) ⇒ Object
Moves the text pointer horizontally to the given margin length
.
135 136 137 |
# File 'lib/eleanor/hpdfpaper.rb', line 135 def margin_left= length self.move_text_pos(-self.get_current_text_pos[0] + length.to_points.to_f, 0) end |
#move_down(length) ⇒ Object
Moves the text pointer down the page by length
.
140 141 142 |
# File 'lib/eleanor/hpdfpaper.rb', line 140 def move_down length self.move_text_pos(0, -length.to_points.to_f) end |
#move_to_top ⇒ Object
Moves the text pointer to the very first line on the page.
145 146 147 148 149 150 151 |
# File 'lib/eleanor/hpdfpaper.rb', line 145 def move_to_top # + 3 because without it, there's a little gap at the top self.move_text_pos(0, -self.get_current_text_pos[1] + self.get_height - self.get_current_font_size + 3) end |
#text_width(str) ⇒ Object
Overridden to implement underlining.
154 155 156 |
# File 'lib/eleanor/hpdfpaper.rb', line 154 def text_width str self.text_width_(str.gsub(/_/, '')) end |
#text_width_ ⇒ Object
73 |
# File 'lib/eleanor/hpdfpaper.rb', line 73 alias :text_width_ :text_width |