diff --git a/lib/squib/api/text.rb b/lib/squib/api/text.rb index 9a97c81..cf9e2fe 100644 --- a/lib/squib/api/text.rb +++ b/lib/squib/api/text.rb @@ -34,7 +34,7 @@ module Squib # @option opts justify [Boolean] (false) toggles whether or not the text is justified or not. # @option opts valign [:top, :middle, :bottom] (:top) When width and height are set, align text vertically according to the ink extents of the text. # @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. + # @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 # @api public diff --git a/lib/squib/graphics/text.rb b/lib/squib/graphics/text.rb index c99f1e5..a3e7961 100644 --- a/lib/squib/graphics/text.rb +++ b/lib/squib/graphics/text.rb @@ -13,8 +13,6 @@ module Squib w = layout.extents[1].width / Pango::SCALE if w < 0 h = layout.height / Pango::SCALE h = layout.extents[1].height / Pango::SCALE if h < 0 - - Squib.logger.warn 'Text hints are broken on non-zero angles' if angle > 0 cc.rounded_rectangle(x,y,w,h,0,0) cc.set_source_color(color) cc.set_line_width(2.0) @@ -40,19 +38,19 @@ module Squib # @api private def set_wrap!(layout, wrap) case wrap.to_s.downcase - when 'word' - layout.wrap = Pango::Layout::WRAP_WORD - when 'char' - layout.wrap = Pango::Layout::WRAP_CHAR - when 'word_char', 'true' - layout.wrap = Pango::Layout::WRAP_WORD_CHAR - end + when 'word' + layout.wrap = Pango::Layout::WRAP_WORD + when 'char' + layout.wrap = Pango::Layout::WRAP_CHAR + when 'word_char', 'true' + layout.wrap = Pango::Layout::WRAP_WORD_CHAR + end end # :nodoc: # @api private def set_align!(layout, align) - case align.to_s + case align.to_s.downcase when 'left' layout.alignment = Pango::ALIGN_LEFT when 'right' @@ -93,8 +91,10 @@ module Squib Squib.logger.debug {"Placing '#{str}'' with font '#{font}' @ #{x}, #{y}, color: #{color}, angle: #{angle} etc."} use_cairo do |cc| cc.set_source_color(color) - cc.move_to(x,y) + cc.translate(x,y) cc.rotate(angle) + cc.translate(-1*x,-1*y) + cc.move_to(x,y) layout = cc.create_pango_layout font_desc = Pango::FontDescription.new(font) diff --git a/samples/text_options.rb b/samples/text_options.rb index 922a6f8..569f3ce 100644 --- a/samples/text_options.rb +++ b/samples/text_options.rb @@ -29,7 +29,7 @@ Squib::Deck.new(width: 825, height: 1125, cards: 3) do hint: :red, x: 65, y: 400, width: 300, height: 200, - align: :center, valign: :middle, + align: :center, valign: 'MIDDLE', #case-insenstive strings allowed too. font: 'Arial 18' text str: 'Ellipsization!\nThe ultimate question of life, the universe, and everything to life and everything is 42', @@ -48,8 +48,8 @@ Squib::Deck.new(width: 825, height: 1125, cards: 3) do font: 'Arial 22' text str: 'Rotated', - x: 565, y: 675, angle: 0.1, - font: 'Arial 18' + x: 565, y: 675, angle: 0.2, + font: 'Arial 18', hint: :red text str: longtext, font: 'Arial 16', x: 65, y: 700, diff --git a/spec/graphics/graphics_text_spec.rb b/spec/graphics/graphics_text_spec.rb index 67c8eae..f7af337 100644 --- a/spec/graphics/graphics_text_spec.rb +++ b/spec/graphics/graphics_text_spec.rb @@ -17,7 +17,9 @@ describe Squib::Card, '#text' do expect(@context).to receive(:save).once expect(@context).to receive(:set_source_color).once expect(@context).to receive(:move_to).with(10, 15).once + expect(@context).to receive(:translate).with(-10, -15).once expect(@context).to receive(:rotate).with(0.0).once + expect(@context).to receive(:translate).with(10, 15).once expect(@context).to receive(:create_pango_layout).once.and_return(@layout) expect(@layout).to receive(:font_description=).with(Pango::FontDescription.new('Sans 12')).once expect(@layout).to receive(:text=).with('foo').once