Browse Source

Refactoring to use case more, fixed build

dev
Andy Meneely 11 years ago
parent
commit
c6e85b0e46
  1. 55
      lib/squib/graphics/text.rb
  2. 10
      spec/graphics/graphics_text_spec.rb

55
lib/squib/graphics/text.rb

@ -23,39 +23,35 @@ module Squib
# :nodoc: # :nodoc:
# @api private # @api private
def ellipsize(layout, ellipsize) def set_ellipsize!(layout, ellipsize)
unless ellipsize.nil? case ellipsize.to_s.downcase
h = { :none => Pango::Layout::ELLIPSIZE_NONE, when 'none', 'false'
:start => Pango::Layout::ELLIPSIZE_START, layout.ellipsize = Pango::Layout::ELLIPSIZE_NONE
:middle => Pango::Layout::ELLIPSIZE_MIDDLE, when 'start'
:end => Pango::Layout::ELLIPSIZE_END, layout.ellipsize = Pango::Layout::ELLIPSIZE_START
true => Pango::Layout::ELLIPSIZE_END, when 'middle'
false => Pango::Layout::ELLIPSIZE_NONE layout.ellipsize = Pango::Layout::ELLIPSIZE_MIDDLE
} when 'end', 'true'
layout.ellipsize = h[ellipsize] layout.ellipsize = Pango::Layout::ELLIPSIZE_END
end end
layout
end end
# :nodoc: # :nodoc:
# @api private # @api private
def wrap(layout, wrap) def set_wrap!(layout, wrap)
layout.wrap = case wrap.to_s case wrap.to_s.downcase
when 'word' when 'word'
Pango::Layout::WRAP_WORD layout.wrap = Pango::Layout::WRAP_WORD
when 'char' when 'char'
Pango::Layout::WRAP_CHAR layout.wrap = Pango::Layout::WRAP_CHAR
when 'word_char', true when 'word_char', 'true'
Pango::Layout::WRAP_WORD_CHAR layout.wrap = Pango::Layout::WRAP_WORD_CHAR
else end
nil
end
layout
end end
# :nodoc: # :nodoc:
# @api private # @api private
def align(layout, align) def set_align!(layout, align)
case align.to_s case align.to_s
when 'left' when 'left'
layout.alignment = Pango::ALIGN_LEFT layout.alignment = Pango::ALIGN_LEFT
@ -64,7 +60,6 @@ module Squib
when 'center' when 'center'
layout.alignment = Pango::ALIGN_CENTER layout.alignment = Pango::ALIGN_CENTER
end end
layout
end end
# :nodoc: # :nodoc:
@ -83,7 +78,7 @@ module Squib
# :nodoc: # :nodoc:
# @api private # @api private
def setwh(layout, width, height) def set_wh!(layout, width, height)
layout.width = width * Pango::SCALE unless width.nil? || width == :native layout.width = width * Pango::SCALE unless width.nil? || width == :native
layout.height = height * Pango::SCALE unless height.nil? || height == :native layout.height = height * Pango::SCALE unless height.nil? || height == :native
layout layout
@ -107,10 +102,10 @@ module Squib
layout.font_description = font_desc layout.font_description = font_desc
layout.text = str.to_s layout.text = str.to_s
layout.markup = str.to_s if markup layout.markup = str.to_s if markup
layout = setwh(layout, width, height) set_wh!(layout, width, height)
layout = wrap(layout, wrap) set_wrap!(layout, wrap)
layout = ellipsize(layout, ellipsize) set_ellipsize!(layout, ellipsize)
layout = align(layout, align) set_align!(layout, align)
layout.justify = justify unless justify.nil? layout.justify = justify unless justify.nil?
layout.spacing = spacing * Pango::SCALE unless spacing.nil? layout.spacing = spacing * Pango::SCALE unless spacing.nil?
cc.update_pango_layout(layout) cc.update_pango_layout(layout)

10
spec/graphics/graphics_text_spec.rb

@ -23,7 +23,6 @@ describe Squib::Card, '#text' do
expect(@layout).to receive(:text=).with('foo').once expect(@layout).to receive(:text=).with('foo').once
expect(@layout).to receive(:width=).with(20 * Pango::SCALE).once expect(@layout).to receive(:width=).with(20 * Pango::SCALE).once
expect(@layout).to receive(:height=).with(25 * Pango::SCALE).once expect(@layout).to receive(:height=).with(25 * Pango::SCALE).once
expect(@layout).to receive(:wrap=).with(nil).once
expect(@layout).to receive(:ellipsize=).with(Pango::Layout::ELLIPSIZE_NONE).once expect(@layout).to receive(:ellipsize=).with(Pango::Layout::ELLIPSIZE_NONE).once
expect(@layout).to receive(:alignment=).with(Pango::Layout::ALIGN_LEFT).once expect(@layout).to receive(:alignment=).with(Pango::Layout::ALIGN_LEFT).once
expect(@layout).to receive(:justify=).with(false).once expect(@layout).to receive(:justify=).with(false).once
@ -94,5 +93,14 @@ describe Squib::Card, '#text' do
1.0, :left, :top, nil, 0.0) 1.0, :left, :top, nil, 0.0)
end end
it 'sets wrap to word_char with true' do
card = Squib::Card.new(@deck, 100, 150)
expect(@layout).to receive(:wrap=).with(Pango::Layout::WRAP_WORD_CHAR).once
card.text('foo', 'Sans 12', nil, '#abc',
10, 15, 20, 50,
nil, false, true, false,
1.0, :left, :top, nil, 0.0)
end
end end
end end

Loading…
Cancel
Save