Class: HexaPDF::Layout::Line
- Inherits:
-
Object
- Object
- HexaPDF::Layout::Line
- Defined in:
- lib/hexapdf/layout/line.rb
Overview
A Line describes a line of text and can contain TextFragment objects or InlineBox objects.
The items of a line fragment are aligned along the x-axis which coincides with the text baseline. The vertical alignment is determined by the value of the #valign method:
- :text_top
-
Align the top of the box with the top of the text of the Line.
- :text_bottom
-
Align the bottom of the box with the bottom of the text of the Line.
- :baseline
-
Align the bottom of the box with the baseline of the Line.
- :top
-
Align the top of the box with the top of the Line.
- :bottom
-
Align the bottom of the box with the bottom of the Line.
- :text
-
This is a special alignment value for text fragment objects. The text fragment is aligned on the baseline and its minimum and maximum y-coordinates are used when calculating the line’s #text_y_min and #text_y_max.
This value may be used by other objects if they should be handled similar to text fragments, e.g. graphical representation of characters (think: emoji fonts).
Item Requirements
Each item of a line fragment has to respond to the following methods:
- #x_min
-
The minimum x-coordinate of the item.
- #x_max
-
The maximum x-coordinate of the item.
- #width
-
The width of the item.
- #valign
-
The vertical alignment of the item (see above).
- #draw(canvas, x, y)
-
Should draw the item onto the canvas at the position (x, y).
If an item has a vertical alignment of :text, it additionally has to respond to the following methods:
- #y_min
-
The minimum y-coordinate of the item.
- #y_max
-
The maximum y-coordinate of the item.
Otherwise (i.e. a vertical alignment different from :text), the following method must be implemented:
- #height
-
The height of the item.
Defined Under Namespace
Classes: HeightCalculator
Instance Attribute Summary collapse
-
#items ⇒ Object
The items: TextFragment and InlineBox objects.
-
#x_offset ⇒ Object
An optional horizontal offset that should be taken into account when positioning the line.
-
#y_offset ⇒ Object
An optional vertical offset that should be taken into account when positioning the line.
Instance Method Summary collapse
-
#add(item) ⇒ Object
(also: #<<)
Adds the given item at the end of the item list.
-
#clear_cache ⇒ Object
:call-seq: line.clear_cache -> line.
-
#each ⇒ Object
:call-seq: line.each {|item, x, y| block }.
-
#height ⇒ Object
The height of the line fragment.
-
#ignore_justification! ⇒ Object
Specifies that this line should not be justified if line justification is used.
-
#ignore_justification? ⇒ Boolean
Returns
true
if justification should be ignored for this line. -
#initialize(items = []) ⇒ Line
constructor
Creates a new Line object, adding all given items to it.
-
#text_y_max ⇒ Object
The maximum y-coordinate of any TextFragment item of the line.
-
#text_y_min ⇒ Object
The minimum y-coordinate of any TextFragment item of the line.
-
#width ⇒ Object
The width of the line fragment.
-
#x_max ⇒ Object
The maximum x-coordinate of the whole line.
-
#x_min ⇒ Object
The minimum x-coordinate of the whole line.
-
#y_max ⇒ Object
The maximum y-coordinate of any item of the line.
-
#y_min ⇒ Object
The minimum y-coordinate of any item of the line.
Constructor Details
#initialize(items = []) ⇒ Line
Creates a new Line object, adding all given items to it.
190 191 192 193 194 195 |
# File 'lib/hexapdf/layout/line.rb', line 190 def initialize(items = []) @items = [] items.each {|i| add(i) } @x_offset = 0 @y_offset = 0 end |
Instance Attribute Details
#items ⇒ Object
The items: TextFragment and InlineBox objects
173 174 175 |
# File 'lib/hexapdf/layout/line.rb', line 173 def items @items end |
#x_offset ⇒ Object
An optional horizontal offset that should be taken into account when positioning the line.
This offset always describes the offset from the left side (and not, for example, the offset from the right side of another line even if those two lines are actually on the same horizontal level).
180 181 182 |
# File 'lib/hexapdf/layout/line.rb', line 180 def x_offset @x_offset end |
#y_offset ⇒ Object
An optional vertical offset that should be taken into account when positioning the line.
For the first line in a paragraph this describes the offset from the top of the box to the baseline of the line. For all other lines it describes the offset from the previous baseline to the baseline of this line.
187 188 189 |
# File 'lib/hexapdf/layout/line.rb', line 187 def y_offset @y_offset end |
Instance Method Details
#add(item) ⇒ Object Also known as: <<
Adds the given item at the end of the item list.
If both the item and the last item in the item list are TextFragment objects with the same attributes, they are combined.
Note: The cache is not cleared!
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/hexapdf/layout/line.rb', line 203 def add(item) last = @items.last if last.instance_of?(item.class) && item.kind_of?(TextFragment) && last.attributes_hash == item.attributes_hash if last.items.frozen? @items[-1] = last = last.dup last.items = last.items.dup end last.items[last.items.length, 0] = item.items last.clear_cache else @items << item end self end |
#clear_cache ⇒ Object
:call-seq:
line.clear_cache -> line
Clears all cached values.
This method needs to be called if the line’s items are changed!
302 303 304 305 |
# File 'lib/hexapdf/layout/line.rb', line 302 def clear_cache @x_max = @y_min = @y_max = @text_y_min = @text_y_max = @width = nil self end |
#each ⇒ Object
:call-seq:
line.each {|item, x, y| block }
Yields each item together with its horizontal offset from 0 and vertical offset from the baseline.
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/hexapdf/layout/line.rb', line 225 def each x = 0 @items.each do |item| y = case item.valign when :text, :baseline then 0 when :top then y_max - item.height when :text_top then text_y_max - item.height when :text_bottom then text_y_min when :bottom then y_min else raise HexaPDF::Error, "Unknown inline box alignment #{item.valign}" end yield(item, x, y) x += item.width end end |
#height ⇒ Object
The height of the line fragment.
282 283 284 |
# File 'lib/hexapdf/layout/line.rb', line 282 def height y_max - y_min end |
#ignore_justification! ⇒ Object
Specifies that this line should not be justified if line justification is used.
287 288 289 |
# File 'lib/hexapdf/layout/line.rb', line 287 def ignore_justification! @ignore_justification = true end |
#ignore_justification? ⇒ Boolean
Returns true
if justification should be ignored for this line.
292 293 294 |
# File 'lib/hexapdf/layout/line.rb', line 292 def ignore_justification? defined?(@ignore_justification) && @ignore_justification end |
#text_y_max ⇒ Object
The maximum y-coordinate of any TextFragment item of the line.
272 273 274 |
# File 'lib/hexapdf/layout/line.rb', line 272 def text_y_max @text_y_max ||= calculate_y_dimensions[3] end |
#text_y_min ⇒ Object
The minimum y-coordinate of any TextFragment item of the line.
260 261 262 |
# File 'lib/hexapdf/layout/line.rb', line 260 def text_y_min @text_y_min ||= calculate_y_dimensions[2] end |
#width ⇒ Object
The width of the line fragment.
277 278 279 |
# File 'lib/hexapdf/layout/line.rb', line 277 def width @width ||= @items.sum(&:width) end |
#x_max ⇒ Object
The maximum x-coordinate of the whole line.
248 249 250 |
# File 'lib/hexapdf/layout/line.rb', line 248 def x_max @x_max ||= width + (items[-1].x_max - items[-1].width) end |
#x_min ⇒ Object
The minimum x-coordinate of the whole line.
243 244 245 |
# File 'lib/hexapdf/layout/line.rb', line 243 def x_min @items[0].x_min end |
#y_max ⇒ Object
The maximum y-coordinate of any item of the line.
It is always greater than or equal to zero.
267 268 269 |
# File 'lib/hexapdf/layout/line.rb', line 267 def y_max @y_max ||= calculate_y_dimensions[1] end |
#y_min ⇒ Object
The minimum y-coordinate of any item of the line.
It is always lower than or equal to zero.
255 256 257 |
# File 'lib/hexapdf/layout/line.rb', line 255 def y_min @y_min ||= calculate_y_dimensions[0] end |