Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

spec all the WidthCalculator implementations

  • Loading branch information...
commit a788714d14b8068ad1273c3eca3088c3a006fdc0 1 parent 4bd8f6b
@yob authored
View
10 spec/support/duck_types/width_calculator.rb
@@ -0,0 +1,10 @@
+# coding: utf-8
+
+# All objects that implement the WidthCalculator duck type must conform to
+# the contract defined in this file
+
+shared_examples "a WidthCalculator duck type" do
+ it "should implement the glyph_width method" do
+ subject.should respond_to(:glyph_width)
+ end
+end
View
32 spec/width_calculator/built_in_spec.rb
@@ -0,0 +1,32 @@
+# coding: utf-8
+
+require "spec_helper"
+
+describe PDF::Reader::WidthCalculator::BuiltIn do
+ it_behaves_like "a WidthCalculator duck type" do
+ let!(:font) { double(:basefont => :Helvetica) }
+ subject { PDF::Reader::WidthCalculator::BuiltIn.new(font)}
+ end
+end
+
+describe PDF::Reader::WidthCalculator::BuiltIn, "#initialize" do
+ context "when the basefont is one of the 14 standard fonts" do
+ let!(:font) { double(:basefont => :Helvetica) }
+
+ it "should initialize with no errors" do
+ lambda {
+ PDF::Reader::WidthCalculator::BuiltIn.new(font)
+ }.should_not raise_error
+ end
+ end
+
+ context "when the basefont is not one of the 14 standard fonts" do
+ let!(:font) { double(:basefont => :Foo) }
+
+ it "should raise an error" do
+ lambda {
+ PDF::Reader::WidthCalculator::BuiltIn.new(font)
+ }.should raise_error(ArgumentError)
+ end
+ end
+end
View
32 spec/width_calculator/composite_spec.rb
@@ -0,0 +1,32 @@
+# coding: utf-8
+
+require "spec_helper"
+
+describe PDF::Reader::WidthCalculator::Composite do
+ it_behaves_like "a WidthCalculator duck type" do
+ let!(:font) { double(:cid_default_width => 50,
+ :cid_widths => [10,[30,40]])
+ }
+ subject { PDF::Reader::WidthCalculator::Composite.new(font)}
+ end
+end
+
+describe PDF::Reader::WidthCalculator::Composite, "#glyph_width" do
+ context "when font#cid_default_width is defined" do
+ let!(:font) { double(:cid_default_width => 50,
+ :cid_widths => [10,[30,40]])
+ }
+ subject { PDF::Reader::WidthCalculator::Composite.new(font)}
+
+ context "when the glyph code is provided in cid_widths" do
+ it "should return the correct width" do
+ subject.glyph_width(10).should == 30
+ end
+ end
+ context "when the glyph code is equal to greater than font#first_char" do
+ it "should return the default width" do
+ subject.glyph_width(9).should == 50
+ end
+ end
+ end
+end
View
47 spec/width_calculator/true_type_spec.rb
@@ -0,0 +1,47 @@
+# coding: utf-8
+
+require "spec_helper"
+
+describe PDF::Reader::WidthCalculator::TrueType do
+ it_behaves_like "a WidthCalculator duck type" do
+ let!(:descriptor) { double(:missing_width => 50) }
+ let!(:font) { double(:font_descriptor => descriptor,
+ :widths => [20,30,40],
+ :first_char => 10) }
+ subject { PDF::Reader::WidthCalculator::TrueType.new(font)}
+ end
+end
+
+describe PDF::Reader::WidthCalculator::TrueType, "#glyph_width" do
+ context "when font#widths is defined" do
+ let!(:descriptor) { double(:missing_width => 50) }
+ let!(:font) { double(:font_descriptor => descriptor,
+ :widths => [20,30,40],
+ :first_char => 10) }
+ subject { PDF::Reader::WidthCalculator::TrueType.new(font)}
+
+ context "when the glyph code is less than font#first_char" do
+ it "should return the missing width" do
+ subject.glyph_width(9).should == 50
+ end
+ end
+ context "when the glyph code is equal to greater than font#first_char" do
+ it "should return the correct width" do
+ subject.glyph_width(10).should == 20
+ end
+ end
+ end
+ context "when font#widths is undefined" do
+ let!(:descriptor) { double(:missing_width => 50,
+ :find_glyph_width => 60,
+ :glyph_to_pdf_scale_factor => 1) }
+ let!(:font) { double(:font_descriptor => descriptor,
+ :widths => nil,
+ :first_char => 10) }
+ subject { PDF::Reader::WidthCalculator::TrueType.new(font)}
+
+ it "should fetch the width from the descriptor" do
+ subject.glyph_width(10).should == 60
+ end
+ end
+end
View
45 spec/width_calculator/type_one_or_three_spec.rb
@@ -0,0 +1,45 @@
+# coding: utf-8
+
+require "spec_helper"
+
+describe PDF::Reader::WidthCalculator::TypeOneOrThree do
+ it_behaves_like "a WidthCalculator duck type" do
+ let!(:descriptor) { double(:missing_width => 50) }
+ let!(:font) { double(:font_descriptor => descriptor,
+ :widths => [20,30,40],
+ :first_char => 10) }
+ subject { PDF::Reader::WidthCalculator::TypeOneOrThree.new(font)}
+ end
+end
+
+describe PDF::Reader::WidthCalculator::TypeOneOrThree, "#glyph_width" do
+ context "when font#widths is defined" do
+ let!(:descriptor) { double(:missing_width => 50) }
+ let!(:font) { double(:font_descriptor => descriptor,
+ :widths => [20,30,40],
+ :first_char => 10) }
+ subject { PDF::Reader::WidthCalculator::TypeOneOrThree.new(font)}
+
+ context "when the glyph code is less than font#first_char" do
+ it "should return the missing width" do
+ subject.glyph_width(9).should == 50
+ end
+ end
+ context "when the glyph code is equal to greater than font#first_char" do
+ it "should return the correct width" do
+ subject.glyph_width(10).should == 20
+ end
+ end
+ end
+ context "when font#widths is undefined" do
+ let!(:descriptor) { double(:missing_width => 50) }
+ let!(:font) { double(:font_descriptor => descriptor,
+ :widths => nil,
+ :first_char => 10) }
+ subject { PDF::Reader::WidthCalculator::TypeOneOrThree.new(font)}
+
+ it "should return 0" do
+ subject.glyph_width(10).should == 0
+ end
+ end
+end
View
23 spec/width_calculator/type_zero_spec.rb
@@ -0,0 +1,23 @@
+# coding: utf-8
+
+require "spec_helper"
+
+describe PDF::Reader::WidthCalculator::TypeZero do
+ it_behaves_like "a WidthCalculator duck type" do
+ let!(:descendants) { [double(:glyph_width => 50)] }
+ let!(:font) { double(:descendantfonts => descendants) }
+ subject { PDF::Reader::WidthCalculator::TypeZero.new(font)}
+ end
+end
+
+describe PDF::Reader::WidthCalculator::TypeZero, "#glyph_width" do
+ context "when font#descendantfonts is defined" do
+ let!(:descendants) { [double(:glyph_width => 50)] }
+ let!(:font) { double(:descendantfonts => descendants) }
+ subject { PDF::Reader::WidthCalculator::TypeZero.new(font)}
+
+ it "should delegate the width calculation to the first descendant font" do
+ subject.glyph_width(10).should == 50
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.