Implementing ink extent return values

This commit is contained in:
Andy Meneely
2014-11-25 00:30:28 -05:00
parent ba54d5c2fa
commit d5181302ad
5 changed files with 31 additions and 12 deletions
+4 -2
View File
@@ -36,18 +36,20 @@ module Squib
# @option opts ellipsize [:none, :start, :middle, :end, true, false] (:end) When width and height are set, determines the behavior of overflowing text. Also: `true` maps to `:end` and `false` maps to `:none`. Default `:end`
# @option opts angle [FixNum] (0) Rotation of the text in radians. Note that this rotates around the upper-left corner of the text box, making the placement of x-y coordinates slightly tricky.
# @option opts hint [String] (:nil) draw a rectangle around the text with the given color. Overrides global hints (see {Deck#hint}).
# @return [nil] Returns nothing
# @return [Array] Returns an Array of hashes keyed by :width and :height that mark the ink extents of the text rendered.
# @api public
def text(opts = {})
opts = needs(opts, [:range, :str, :font, :font_size, :x, :y, :width, :height, :color, :wrap,
:align, :justify, :spacing, :valign, :markup, :ellipsize, :hint, :layout, :angle])
extents = Array.new(@cards.size)
opts[:range].each do |i|
@cards[i].text(opts[:str][i], opts[:font][i], opts[:font_size][i], opts[:color][i],
extents[i] = @cards[i].text(opts[:str][i], opts[:font][i], opts[:font_size][i], opts[:color][i],
opts[:x][i], opts[:y][i], opts[:width][i], opts[:height][i],
opts[:markup][i], opts[:justify][i], opts[:wrap][i],
opts[:ellipsize][i], opts[:spacing][i], opts[:align][i],
opts[:valign][i], opts[:hint][i], opts[:angle][i])
end
return extents
end
end
+4
View File
@@ -89,6 +89,7 @@ module Squib
markup, justify, wrap, ellipsize,
spacing, align, valign, hint, angle)
Squib.logger.debug {"Placing '#{str}'' with font '#{font}' @ #{x}, #{y}, color: #{color}, angle: #{angle} etc."}
extents = nil
use_cairo do |cc|
cc.set_source_color(color)
cc.translate(x,y)
@@ -112,7 +113,10 @@ module Squib
valign!(cc, layout, x, y, valign)
cc.update_pango_layout(layout) ; cc.show_pango_layout(layout)
draw_text_hint(cc,x,y,layout,hint,angle)
extents = { width: layout.extents[1].width / Pango::SCALE,
height: layout.extents[1].height / Pango::SCALE }
end
return extents
end
end