Browse Source

text: fix valign when we only have an embed key

Fixes #134
dev
Andy Meneely 10 years ago
parent
commit
9cdd3c4d2f
  1. 15
      lib/squib/graphics/text.rb
  2. 14
      samples/bug134.rb

15
lib/squib/graphics/text.rb

@ -22,9 +22,10 @@ module Squib
# :nodoc: # :nodoc:
# @api private # @api private
def compute_valign(layout, valign) def compute_valign(layout, valign, embed_h)
return 0 unless layout.height > 0 return 0 unless layout.height > 0
ink_extents = layout.extents[1] ink_extents = layout.extents[1]
ink_extents.height = embed_h * Pango::SCALE if ink_extents.height == 0 #JUST embed, bug #134
case valign.to_s.downcase case valign.to_s.downcase
when 'middle' when 'middle'
Pango.pixels( (layout.height - ink_extents.height) / 2) Pango.pixels( (layout.height - ink_extents.height) / 2)
@ -50,6 +51,12 @@ module Squib
layout.height = height * Pango::SCALE unless height.nil? || height == :auto layout.height = height * Pango::SCALE unless height.nil? || height == :auto
end end
def max_embed_height(embed_draws)
embed_draws.inject(0) do |max, ed|
ed[:h] > max ? ed[:h] : max
end
end
# :nodoc: # :nodoc:
# @api private # @api private
def next_embed(keys, str) def next_embed(keys, str)
@ -97,7 +104,9 @@ module Squib
rect = layout.index_to_pos(search[:index]) rect = layout.index_to_pos(search[:index])
x = Pango.pixels(rect.x) + search[:rule][:adjust].dx[@index] x = Pango.pixels(rect.x) + search[:rule][:adjust].dx[@index]
y = Pango.pixels(rect.y) + search[:rule][:adjust].dy[@index] y = Pango.pixels(rect.y) + search[:rule][:adjust].dy[@index]
draw_calls << {x: x, y: y, draw: search[:rule][:draw]} # defer drawing until we've valigned h = rule[:box].height[@index]
draw_calls << {x: x, y: y, h: h, # defer drawing until we've valigned
draw: search[:rule][:draw]}
end end
return draw_calls return draw_calls
end end
@ -148,7 +157,7 @@ module Squib
embed_draws = process_embeds(embed, para.str, layout) embed_draws = process_embeds(embed, para.str, layout)
vertical_start = compute_valign(layout, para.valign) vertical_start = compute_valign(layout, para.valign, max_embed_height(embed_draws))
cc.move_to(0, vertical_start) #TODO clean this up a bit cc.move_to(0, vertical_start) #TODO clean this up a bit
stroke_outline!(cc, layout, draw) if draw.stroke_strategy == :stroke_first stroke_outline!(cc, layout, draw) if draw.stroke_strategy == :stroke_first

14
samples/bug134.rb

@ -0,0 +1,14 @@
require 'squib'
require 'game_icons'
Squib::Deck.new(cards: 1) do
rect
text str: 'foo', x: 275, y: 75, width: 500, height: 500, valign: :middle, hint: :blue
text(str: '(heart)(heart2)',
x: 75, y: 75, width: 500, height: 500,
valign: :middle, hint: :red) do |embed|
embed.svg key: '(heart)', width: 50, height: 50, data: GameIcons.get('glass-heart').string
embed.svg key: '(heart2)', width: 50, height: 50, data: GameIcons.get('glass-heart').string
end
save_png prefix: 'bug_134_'
end
Loading…
Cancel
Save