From 7cc75b24a22d8225c39a2cccbd44ca2dd95a153e Mon Sep 17 00:00:00 2001 From: pickfifteen Date: Mon, 30 Mar 2015 16:19:19 -0400 Subject: [PATCH] Embed wrapping fix Draw call locations now calculated after all string replacements. Does this fix the center and right align embed? Quick testing could not find any errors, but more thorough testing needed. --- lib/squib/graphics/text.rb | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/squib/graphics/text.rb b/lib/squib/graphics/text.rb index cd7b968..c632952 100644 --- a/lib/squib/graphics/text.rb +++ b/lib/squib/graphics/text.rb @@ -105,6 +105,7 @@ module Squib layout.markup = str clean_str = layout.text draw_calls = [] + searches = [] while (key = next_embed(embed.rules.keys, clean_str)) != nil rule = embed.rules[key] spacing = rule[:width] * Pango::SCALE @@ -112,17 +113,20 @@ module Squib str.sub!(key, "aaa") layout.markup = str clean_str = layout.text - rect = layout.index_to_pos(index) + searches << { index: index, rule: rule } + end + searches.each do |search| + rect = layout.index_to_pos(search[:index]) iter = layout.iter - while iter.next_char! && iter.index < index; end + while iter.next_char! && iter.index < search[:index]; end case layout.alignment when Pango::Layout::Alignment::CENTER, Pango::Layout::Alignment::RIGHT Squib.logger.warn "Center- or right-aligned text do not always embed properly. This is a known issue with a workaround. See https://github.com/andymeneely/squib/issues/46" end - x = Pango.pixels(rect.x) + rule[:dx] - y = Pango.pixels(rect.y) + rule[:dy] - draw_calls << {x: x, y: y, draw: rule[:draw]} # defer drawing until we've valigned + x = Pango.pixels(rect.x) + search[:rule][:dx] + y = Pango.pixels(rect.y) + search[:rule][:dy] + draw_calls << {x: x, y: y, draw: search[:rule][:draw]} # defer drawing until we've valigned end return draw_calls end