Browse Source
* Involved a big rewrite of arg_loader to accomodate nil and false better. Sadly this meant dumping ||= for if-statements. Blech, but works. * Updates to regression tests are pretty trivial. * Removed graphics_text_spec because, even though it looks useful, with rake sanity it really isn't. * Exposed font on deck as well Cleaned up graphics/text.rb too!dev
26 changed files with 441 additions and 456 deletions
@ -1,37 +0,0 @@
|
||||
require 'spec_helper' |
||||
require 'squib' |
||||
|
||||
describe Squib::Deck, '#text' do |
||||
|
||||
context 'fonts' do |
||||
it "should use the default font when #text and #set_font don't specify" do |
||||
card = instance_double(Squib::Card) |
||||
expect(card).to receive(:text).with(anything, 'a', 'Arial 36', *([anything] * 17)).once |
||||
Squib::Deck.new do |
||||
@cards = [card] |
||||
text str: 'a' |
||||
end |
||||
end |
||||
|
||||
it "should use the #set_font when #text doesn't specify" do |
||||
card = instance_double(Squib::Card) |
||||
expect(card).to receive(:text).with(anything, 'a', 'Times New Roman 16', *([anything] * 17)).once |
||||
Squib::Deck.new do |
||||
@cards = [card] |
||||
set font: 'Times New Roman 16' |
||||
text str: 'a' |
||||
end |
||||
end |
||||
|
||||
it 'should use the specified font no matter what' do |
||||
card = instance_double(Squib::Card) |
||||
expect(card).to receive(:text).with(anything, 'a', 'Arial 18', *([anything] * 17)).once |
||||
Squib::Deck.new do |
||||
@cards = [card] |
||||
set font: 'Times New Roman 16' |
||||
text str: 'a', font: 'Arial 18' |
||||
end |
||||
end |
||||
end |
||||
|
||||
end |
||||
@ -0,0 +1,153 @@
|
||||
require 'spec_helper' |
||||
require 'squib/args/paragraph' |
||||
require 'squib/constants' |
||||
|
||||
describe Squib::Args::Paragraph do |
||||
subject(:para) { Squib::Args::Paragraph.new('FooFont 32') } |
||||
|
||||
context 'str validator' do |
||||
it 'converts everything to string' do |
||||
para.load!( {str: 5} ) |
||||
expect(para.str).to eq ['5'] |
||||
end |
||||
end |
||||
|
||||
context 'font validator' do |
||||
it 'uses deck font by default' do |
||||
para.load!( {} ) |
||||
expect(para.font).to eq ['FooFont 32'] |
||||
end |
||||
|
||||
it 'uses system default font when deck font is :default' do |
||||
para = Squib::Args::Paragraph.new(:default) |
||||
para.load!( {} ) |
||||
expect(para.font).to eq [Squib::SYSTEM_DEFAULTS[:default_font]] |
||||
end |
||||
|
||||
it 'uses specified font when given' do |
||||
para.load!( {font: 'MyFont 8'}) |
||||
expect(para.font).to eq ['MyFont 8'] |
||||
end |
||||
end |
||||
|
||||
context 'align validator' do |
||||
it 'converts to pango left' do |
||||
para.load!( { align: :left } ) |
||||
expect(para.align).to eq [Pango::ALIGN_LEFT] |
||||
end |
||||
|
||||
it 'converts to pango right' do |
||||
para.load!( { align: :RIGHT } ) |
||||
expect(para.align).to eq [Pango::ALIGN_RIGHT] |
||||
end |
||||
|
||||
it 'converts to pango center' do |
||||
para.load!( { align: 'center' } ) |
||||
expect(para.align).to eq [Pango::ALIGN_CENTER] |
||||
end |
||||
|
||||
it 'raises an exception on anything else' do |
||||
expect { para.load!( { align: 'foo' } ) }.to raise_error(ArgumentError, 'align must be one of: center, left, right') |
||||
end |
||||
end |
||||
|
||||
context 'wrap validator' do |
||||
it 'converts to pango wrap word' do |
||||
para.load!( { wrap: 'word'} ) |
||||
expect(para.wrap).to eq [Pango::WRAP_WORD] |
||||
end |
||||
|
||||
it 'converts to pango wrap char' do |
||||
para.load!( { wrap: 'WORD_ChAr'} ) |
||||
expect(para.wrap).to eq [Pango::WRAP_WORD_CHAR] |
||||
end |
||||
|
||||
it 'converts to pango wrap char on true' do |
||||
para.load!( { wrap: true} ) |
||||
expect(para.wrap).to eq [Pango::WRAP_WORD_CHAR] |
||||
end |
||||
|
||||
it 'converts to pango wrap char with false' do |
||||
para.load!( { wrap: false} ) |
||||
expect(para.wrap).to eq [Pango::WRAP_CHAR] |
||||
end |
||||
|
||||
it 'raises an exception on anything else' do |
||||
expect { para.load!( {wrap: 'foo' }) }.to raise_error(ArgumentError, 'wrap must be one of: word, char, word_char, true, or false') |
||||
end |
||||
end |
||||
|
||||
context 'ellipsize validator' do |
||||
it 'converts to pango on none and false' do |
||||
para.load!( { ellipsize: 'none'} ) |
||||
expect(para.ellipsize).to eq [Pango::Layout::ELLIPSIZE_NONE] |
||||
end |
||||
|
||||
it 'converts to pango with start' do |
||||
para.load!( { ellipsize: :StArt} ) |
||||
expect(para.ellipsize).to eq [Pango::Layout::ELLIPSIZE_START] |
||||
end |
||||
|
||||
it 'converts to pango middle' do |
||||
para.load!( { ellipsize: 'middle'} ) |
||||
expect(para.ellipsize).to eq [Pango::Layout::ELLIPSIZE_MIDDLE] |
||||
end |
||||
|
||||
it 'converts to pango end' do |
||||
para.load!( { ellipsize: 'END'} ) |
||||
expect(para.ellipsize).to eq [Pango::Layout::ELLIPSIZE_END] |
||||
end |
||||
|
||||
it 'raises an exception on anything else' do |
||||
expect { para.load!( {ellipsize: 'foo' }) }.to raise_error(ArgumentError, 'ellipsize must be one of: none, start, middle, end, true, or false') |
||||
end |
||||
end |
||||
|
||||
context 'justify validator' do |
||||
it 'allows nil' do |
||||
para.load!( { justify: nil} ) |
||||
expect(para.justify).to eq [nil] |
||||
end |
||||
|
||||
it 'can be true' do |
||||
para.load!( { justify: true} ) |
||||
expect(para.justify).to eq [true] |
||||
end |
||||
|
||||
it 'can be false' do |
||||
para.load!( { justify: false} ) |
||||
expect(para.justify).to eq [false] |
||||
end |
||||
|
||||
it 'raises an exception on anything else' do |
||||
expect { para.load!( {justify: 'false' }) }.to raise_error(ArgumentError, 'justify must be one of: nil, true, or false') |
||||
end |
||||
end |
||||
|
||||
context 'spacing validator' do |
||||
it 'allows nil' do |
||||
para.load!( { spacing: nil} ) |
||||
expect(para.spacing).to eq [nil] |
||||
end |
||||
it 'is converted to Pango space' do |
||||
para.load!( { spacing: 519} ) |
||||
expect(para.spacing).to eq [Pango::SCALE * 519.0] |
||||
end |
||||
|
||||
it 'raises an exception if not a float' do |
||||
expect { para.load!( {spacing: /foo/ }) }.to raise_error(ArgumentError, 'spacing must be a number or nil') |
||||
end |
||||
end |
||||
|
||||
context 'valign validator' do |
||||
it 'converts top' do |
||||
para.load!( { valign: :top} ) |
||||
expect(para.valign).to eq ['top'] |
||||
end |
||||
|
||||
it 'raises an exception if not one of the three' do |
||||
expect { para.load!( {valign: 'foo' }) }.to raise_error(ArgumentError, 'valign must be one of: top, middle, bottom') |
||||
end |
||||
end |
||||
|
||||
end |
||||
@ -1,164 +0,0 @@
|
||||
require 'spec_helper' |
||||
require 'squib' |
||||
|
||||
describe Squib::Card, '#text' do |
||||
|
||||
context 'typical inputs' do |
||||
let(:deck) { double(Squib::Deck) } |
||||
let(:context) { double(Cairo::Context) } |
||||
let(:layout) { double(Pango::Layout) } |
||||
let(:font_desc) { double(Pango::FontDescription) } |
||||
let(:pango_cxt) { double(Pango::Context) } |
||||
|
||||
before(:each) do |
||||
allow(Cairo::Context).to receive(:new).and_return(context) |
||||
allow(deck).to receive(:dir).and_return('_output') |
||||
allow(deck).to receive(:count_format).and_return('%02d') |
||||
allow(deck).to receive(:prefix).and_return('card_') |
||||
allow(deck).to receive(:antialias).and_return('best') |
||||
allow(deck).to receive(:antialias).and_return('subpixel') |
||||
allow(deck).to receive(:backend).and_return('memory') |
||||
allow(layout).to receive(:context).and_return(pango_cxt) |
||||
end |
||||
|
||||
it 'make all the expected calls on a smoke test' do |
||||
extent = Pango::Rectangle.new(50,60,100,200) |
||||
expect(Squib.logger).to receive(:debug).once |
||||
expect(context).to receive(:antialias=).with('subpixel').once |
||||
expect(context).to receive(:save).once |
||||
expect(context).to receive(:set_source_color).once |
||||
expect(context).to receive(:move_to).with(0, 0).twice |
||||
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(Pango::FontDescription).to receive(:new).with('Sans 12').and_return(font_desc) |
||||
expect(layout ).to receive(:font_description=).with(font_desc).once |
||||
expect(layout ).to receive(:text=).with('foo').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(:ellipsize=).with(Pango::Layout::ELLIPSIZE_NONE).once |
||||
expect(layout ).to receive(:alignment=).with(Pango::Layout::ALIGN_LEFT).once |
||||
expect(pango_cxt).to receive(:font_options=).once |
||||
expect(layout ).to receive(:justify=).with(false).once |
||||
expect(layout ).to receive(:spacing=).with(1.0 * Pango::SCALE).once |
||||
expect(context).to receive(:update_pango_layout).once |
||||
expect(layout ).to receive(:height).once.and_return(25) |
||||
expect(layout ).to receive(:extents).thrice.and_return([nil,extent]) |
||||
expect(context).to receive(:update_pango_layout).once |
||||
expect(context).to receive(:show_pango_layout).once |
||||
expect(context).to receive(:restore).once |
||||
|
||||
card = Squib::Card.new(deck, 100, 150) |
||||
# text(str, font, font_size, color, |
||||
# x, y, width, height, |
||||
# markup, justify, wrap, ellipsize, |
||||
# spacing, align, valign, hint, angle, |
||||
# stroke_width, stroke_color) |
||||
ret = card.text(Squib::TextEmbed.new,'foo', 'Sans 12', nil, '#abc', |
||||
10, 15, 20, 25, |
||||
nil, false, false, false, |
||||
1.0, :left, :top, nil, 0.0, '#fff', 0) |
||||
expect(ret).to eq({width: 0, height: 0}) |
||||
end |
||||
end |
||||
|
||||
context 'convenience lookups' do |
||||
let(:deck) { double(Squib::Deck) } |
||||
let(:context) { double(Cairo::Context).as_null_object } |
||||
let(:layout) { double(Pango::Layout).as_null_object } |
||||
let(:extents) { double("extents") } |
||||
|
||||
before(:each) do |
||||
allow(Cairo::Context).to receive(:new).and_return(context) |
||||
expect(context).to receive(:create_pango_layout).once.and_return(layout) |
||||
allow(deck).to receive(:dir).and_return('_output') |
||||
allow(deck).to receive(:count_format).and_return('%02d') |
||||
allow(deck).to receive(:prefix).and_return('card_') |
||||
allow(deck).to receive(:antialias).and_return('subpixel') |
||||
allow(deck).to receive(:backend).and_return('memory') |
||||
end |
||||
|
||||
it 'aligns right with strings' do |
||||
card = Squib::Card.new(deck, 100, 150) |
||||
expect(layout).to receive(:alignment=).with(Pango::Layout::ALIGN_RIGHT).once |
||||
card.text(Squib::TextEmbed.new, 'foo', 'Sans 12', nil, '#abc', |
||||
10, 15, 20, 50, |
||||
nil, false, false, false, |
||||
1.0, 'right', :top, nil, 0.0, '#fff', 0) |
||||
end |
||||
|
||||
it 'aligns center with strings' do |
||||
card = Squib::Card.new(deck, 100, 150) |
||||
expect(layout).to receive(:alignment=).with(Pango::Layout::ALIGN_CENTER).once |
||||
card.text(Squib::TextEmbed.new, 'foo', 'Sans 12', nil, '#abc', |
||||
10, 15, 20, 50, |
||||
nil, false, false, false, |
||||
1.0, 'center', :top, nil, 0.0, '#fff', 0) |
||||
end |
||||
|
||||
it 'sets wrap to char with string char' do |
||||
card = Squib::Card.new(deck, 100, 150) |
||||
expect(layout).to receive(:wrap=).with(Pango::Layout::WRAP_CHAR).once |
||||
card.text(Squib::TextEmbed.new, 'foo', 'Sans 12', nil, '#abc', |
||||
10, 15, 20, 50, |
||||
nil, false, 'char', false, |
||||
1.0, :left, :top, nil, 0.0, '#fff', 0) |
||||
end |
||||
|
||||
it 'sets wrap to word with word string' do |
||||
card = Squib::Card.new(deck, 100, 150) |
||||
expect(layout).to receive(:wrap=).with(Pango::Layout::WRAP_WORD).once |
||||
card.text(Squib::TextEmbed.new, 'foo', 'Sans 12', nil, '#abc', |
||||
10, 15, 20, 50, |
||||
nil, false, 'word', false, |
||||
1.0, :left, :top, nil, 0.0, '#fff', 0) |
||||
end |
||||
|
||||
it 'sets wrap to word_char with symbol word_char' do |
||||
card = Squib::Card.new(deck, 100, 150) |
||||
expect(layout).to receive(:wrap=).with(Pango::Layout::WRAP_WORD_CHAR).once |
||||
card.text(Squib::TextEmbed.new, 'foo', 'Sans 12', nil, '#abc', |
||||
10, 15, 20, 50, |
||||
nil, false, :word_char, false, |
||||
1.0, :left, :top, nil, 0.0, '#fff', 0) |
||||
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(Squib::TextEmbed.new, 'foo', 'Sans 12', nil, '#abc', |
||||
10, 15, 20, 50, |
||||
nil, false, true, false, |
||||
1.0, :left, :top, nil, 0.0, '#fff', 0) |
||||
end |
||||
|
||||
it 'sets ellipsize to start properly' do |
||||
card = Squib::Card.new(deck, 100, 150) |
||||
expect(layout).to receive(:ellipsize=).with(Pango::Layout::ELLIPSIZE_START).once |
||||
card.text(Squib::TextEmbed.new, 'foo', 'Sans 12', nil, '#abc', |
||||
10, 15, 20, 50, |
||||
nil, false, true, :start, |
||||
1.0, :left, :top, nil, 0.0, '#fff', 0) |
||||
end |
||||
|
||||
it 'sets ellipsize to middle properly' do |
||||
card = Squib::Card.new(deck, 100, 150) |
||||
expect(layout).to receive(:ellipsize=).with(Pango::Layout::ELLIPSIZE_MIDDLE).once |
||||
card.text(Squib::TextEmbed.new, 'foo', 'Sans 12', nil, '#abc', |
||||
10, 15, 20, 50, |
||||
nil, false, true, 'middle', |
||||
1.0, :left, :top, nil, 0.0, '#fff', 0) |
||||
end |
||||
|
||||
it 'implements stroke and fill when asked to' do |
||||
card = Squib::Card.new(deck, 100, 150) |
||||
expect(context).to receive(:set_line_width).with(3.0).once |
||||
expect(context).to receive(:pango_layout_path).once |
||||
card.text(Squib::TextEmbed.new, 'foo', 'Sans 12', nil, '#abc', |
||||
10, 15, 20, 50, |
||||
nil, false, true, 'middle', |
||||
1.0, :left, :top, nil, 0.0, '#f00', 3.0) |
||||
end |
||||
|
||||
end |
||||
end |
||||
Loading…
Reference in new issue