spec/font_spec.rb in prawn-1.0.0.rc1 vs spec/font_spec.rb in prawn-1.0.0.rc2

- old
+ new

@@ -1,11 +1,11 @@ # encoding: utf-8 -require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper") -require 'iconv' +require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper") +require 'pathname' -describe "Font behavior" do +describe "Font behavior" do it "should default to Helvetica if no font is specified" do @pdf = Prawn::Document.new @pdf.font.name.should == "Helvetica" end @@ -22,11 +22,11 @@ end it "should exclude newlines" do create_pdf # Use a TTF font that has a non-zero width for \n - @pdf.font("#{Prawn::BASEDIR}/data/fonts/gkai00mp.ttf") + @pdf.font("#{Prawn::DATADIR}/fonts/gkai00mp.ttf") @pdf.width_of("\nhello world\n").should == @pdf.width_of("hello world") end @@ -37,11 +37,11 @@ inline_bold_hello = @pdf.width_of("<b>hello</b>", :inline_format => true) @pdf.font("Helvetica", :style => :bold) { @bold_hello = @pdf.width_of("hello") } - inline_bold_hello.should.be > normal_hello + inline_bold_hello.should be > normal_hello inline_bold_hello.should == @bold_hello end it "should accept :style as an argument" do create_pdf @@ -68,11 +68,11 @@ it "should complain if there is no @current_page" do pdf_without_page = Prawn::Document.new(:skip_page_creation => true) lambda{ pdf_without_page.font "Helvetica" }. - should.raise(Prawn::Errors::NotOnPage) + should raise_error(Prawn::Errors::NotOnPage) end it "should allow specifying font style by style name and font family" do @pdf.font "Courier", :style => :bold @pdf.text "In Courier bold" @@ -94,11 +94,11 @@ [:"Courier-Bold", :"Courier-BoldOblique", :"Courier-Oblique", :Courier, :Helvetica] end it "should allow font familes to be defined in a single dfont" do - file = "#{Prawn::BASEDIR}/data/fonts/Action Man.dfont" + file = "#{Prawn::DATADIR}/fonts/Action Man.dfont" @pdf.font_families["Action Man"] = { :normal => { :file => file, :font => "ActionMan" }, :italic => { :file => file, :font => "ActionMan-Italic" }, :bold => { :file => file, :font => "ActionMan-Bold" }, :bold_italic => { :file => file, :font => "ActionMan-BoldItalic" } @@ -110,10 +110,25 @@ text = PDF::Inspector::Text.analyze(@pdf.render) name = text.font_settings.map { |e| e[:name] }.first.to_s name = name.sub(/\w+\+/, "subset+") name.should == "subset+ActionMan-Italic" end + + it "should accept Pathname objects for font files" do + file = Pathname.new( "#{Prawn::DATADIR}/fonts/Chalkboard.ttf" ) + @pdf.font_families["Chalkboard"] = { + :normal => file + } + + @pdf.font "Chalkboard" + @pdf.text "In Chalkboard" + + text = PDF::Inspector::Text.analyze(@pdf.render) + name = text.font_settings.map { |e| e[:name] }.first.to_s + name = name.sub(/\w+\+/, "subset+") + name.should == "subset+Chalkboard" + end end describe "Transactional font handling" do before(:each) { create_pdf } @@ -168,67 +183,68 @@ def page_includes_font?(font) @pdf.page.fonts.values.map { |e| e.data[:BaseFont] }.include?(font.to_sym) end def page_should_include_font(font) - assert_block("Expected page to include font: #{font}") do - page_includes_font?(font) - end + page_includes_font?(font).should be_true end def page_should_not_include_font(font) - assert_block("Did not expect page to include font: #{font}") do - not page_includes_font?(font) - end + page_includes_font?(font).should be_false end end describe "AFM fonts" do - setup do + before do create_pdf @times = @pdf.find_font "Times-Roman" - @iconv = ::Iconv.new('Windows-1252', 'utf-8') end it "should calculate string width taking into account accented characters" do - @times.compute_width_of(@iconv.iconv("é"), :size => 12).should == @times.compute_width_of("e", :size => 12) + input = win1252_string("\xE9")# é in win-1252 + @times.compute_width_of(input, :size => 12).should == @times.compute_width_of("e", :size => 12) end it "should calculate string width taking into account kerning pairs" do - @times.compute_width_of(@iconv.iconv("To"), :size => 12).should == 13.332 - @times.compute_width_of(@iconv.iconv("To"), :size => 12, :kerning => true).should == 12.372 - @times.compute_width_of(@iconv.iconv("Tö"), :size => 12, :kerning => true).should == 12.372 + @times.compute_width_of(win1252_string("To"), :size => 12).should == 13.332 + @times.compute_width_of(win1252_string("To"), :size => 12, :kerning => true).should == 12.372 + + input = win1252_string("T\xF6") # Tö in win-1252 + @times.compute_width_of(input, :size => 12, :kerning => true).should == 12.372 end it "should encode text without kerning by default" do - @times.encode_text(@iconv.iconv("To")).should == [[0, "To"]] - @times.encode_text(@iconv.iconv("Télé")).should == [[0, @iconv.iconv("Télé")]] - @times.encode_text(@iconv.iconv("Technology")).should == [[0, "Technology"]] - @times.encode_text(@iconv.iconv("Technology...")).should == [[0, "Technology..."]] + @times.encode_text(win1252_string("To")).should == [[0, "To"]] + input = win1252_string("T\xE9l\xE9") # Télé in win-1252 + @times.encode_text(input).should == [[0, input]] + @times.encode_text(win1252_string("Technology")).should == [[0, "Technology"]] + @times.encode_text(win1252_string("Technology...")).should == [[0, "Technology..."]] end it "should encode text with kerning if requested" do - @times.encode_text(@iconv.iconv("To"), :kerning => true).should == [[0, ["T", 80, "o"]]] - @times.encode_text(@iconv.iconv("Télé"), :kerning => true).should == [[0, ["T", 70, @iconv.iconv("élé")]]] - @times.encode_text(@iconv.iconv("Technology"), :kerning => true).should == [[0, ["T", 70, "echnology"]]] - @times.encode_text(@iconv.iconv("Technology..."), :kerning => true).should == [[0, ["T", 70, "echnology", 65, "..."]]] + @times.encode_text(win1252_string("To"), :kerning => true).should == [[0, ["T", 80, "o"]]] + input = win1252_string("T\xE9l\xE9") # Télé in win-1252 + output = win1252_string("\xE9l\xE9") # élé in win-1252 + @times.encode_text(input, :kerning => true).should == [[0, ["T", 70, output]]] + @times.encode_text(win1252_string("Technology"), :kerning => true).should == [[0, ["T", 70, "echnology"]]] + @times.encode_text(win1252_string("Technology..."), :kerning => true).should == [[0, ["T", 70, "echnology", 65, "..."]]] end describe "when normalizing encoding" do it "should not modify the original string when normalize_encoding() is used" do original = "Foo" normalized = @times.normalize_encoding(original) - assert ! original.equal?(normalized) + original.equal?(normalized).should be_false end it "should modify the original string when normalize_encoding!() is used" do original = "Foo" normalized = @times.normalize_encoding!(original) - assert original.equal?(normalized) + original.equal?(normalized).should be_true end end it "should omit /Encoding for symbolic fonts" do @@ -242,37 +258,37 @@ describe "#glyph_present" do before(:each) { create_pdf } it "should return true when present in an AFM font" do font = @pdf.find_font("Helvetica") - font.glyph_present?("H").should.be true + font.glyph_present?("H").should be_true end it "should return false when absent in an AFM font" do font = @pdf.find_font("Helvetica") - font.glyph_present?("再").should.be false + font.glyph_present?("再").should be_false end it "should return true when present in a TTF font" do - font = @pdf.find_font("#{Prawn::BASEDIR}/data/fonts/Activa.ttf") - font.glyph_present?("H").should.be true + font = @pdf.find_font("#{Prawn::DATADIR}/fonts/Activa.ttf") + font.glyph_present?("H").should be_true end it "should return false when absent in a TTF font" do - font = @pdf.find_font("#{Prawn::BASEDIR}/data/fonts/Activa.ttf") - font.glyph_present?("再").should.be false + font = @pdf.find_font("#{Prawn::DATADIR}/fonts/Activa.ttf") + font.glyph_present?("再").should be_false - font = @pdf.find_font("#{Prawn::BASEDIR}/data/fonts/gkai00mp.ttf") - font.glyph_present?("€").should.be false + font = @pdf.find_font("#{Prawn::DATADIR}/fonts/gkai00mp.ttf") + font.glyph_present?("€").should be_false end end describe "TTF fonts" do - setup do + before do create_pdf - @activa = @pdf.find_font "#{Prawn::BASEDIR}/data/fonts/Activa.ttf" + @activa = @pdf.find_font "#{Prawn::DATADIR}/fonts/Activa.ttf" end it "should calculate string width taking into account accented characters" do @activa.compute_width_of("é", :size => 12).should == @activa.compute_width_of("e", :size => 12) end @@ -283,13 +299,15 @@ end it "should encode text without kerning by default" do @activa.encode_text("To").should == [[0, "To"]] - tele = (RUBY_VERSION < '1.9') ? "T\216l\216" : - "T\216l\216".force_encoding("US-ASCII") - @activa.encode_text("Télé").should == [[0, tele]] + tele = "T\216l\216" + result = @activa.encode_text("Télé") + result.length.should == 1 + result[0][0].should == 0 + result[0][1].bytes.to_a.should == tele.bytes.to_a @activa.encode_text("Technology").should == [[0, "Technology"]] @activa.encode_text("Technology...").should == [[0, "Technology..."]] @activa.encode_text("Teχnology...").should == [[0, "Te"], [1, "!"], [0, "nology..."]] end @@ -318,37 +336,37 @@ describe "when normalizing encoding" do it "should not modify the original string when normalize_encoding() is used" do original = "Foo" normalized = @activa.normalize_encoding(original) - assert ! original.equal?(normalized) + original.equal?(normalized).should be_false end it "should modify the original string when normalize_encoding!() is used" do original = "Foo" normalized = @activa.normalize_encoding!(original) - assert original.equal?(normalized) + original.equal?(normalized).should be_true end end describe "when used with snapshots or transactions" do it "should allow TTF fonts to be used alongside document transactions" do lambda { Prawn::Document.new do - font "#{Prawn::BASEDIR}/data/fonts/DejaVuSans.ttf" + font "#{Prawn::DATADIR}/fonts/DejaVuSans.ttf" text "Hi there" transaction { text "Nice, thank you" } end - }.should.not.raise + }.should_not raise_error end it "should allow TTF fonts to be used inside transactions" do pdf = Prawn::Document.new do transaction do - font "#{Prawn::BASEDIR}/data/fonts/DejaVuSans.ttf" + font "#{Prawn::DATADIR}/fonts/DejaVuSans.ttf" text "Hi there" end end text = PDF::Inspector::Text.analyze(pdf.render) @@ -360,13 +378,13 @@ end end describe "DFont fonts" do - setup do + before do create_pdf - @file = "#{Prawn::BASEDIR}/data/fonts/Action Man.dfont" + @file = "#{Prawn::DATADIR}/fonts/Action Man.dfont" end it "should list all named fonts" do list = Prawn::Font::DFont.named_fonts(@file) list.sort.should == %w(ActionMan ActionMan-Italic ActionMan-Bold ActionMan-BoldItalic).sort @@ -392,19 +410,19 @@ end it "should cache font object based on selected font" do f1 = @pdf.find_font(@file, :font => "ActionMan") f2 = @pdf.find_font(@file, :font => "ActionMan-Bold") - assert_not_equal f1.object_id, f2.object_id - assert_equal f1.object_id, @pdf.find_font(@file, :font => "ActionMan").object_id - assert_equal f2.object_id, @pdf.find_font(@file, :font => "ActionMan-Bold").object_id + f2.object_id.should_not == f1.object_id + @pdf.find_font(@file, :font => "ActionMan").object_id.should == f1.object_id + @pdf.find_font(@file, :font => "ActionMan-Bold").object_id.should == f2.object_id end end describe "#character_count(text)" do it "should work on TTF fonts" do create_pdf - @pdf.font("#{Prawn::BASEDIR}/data/fonts/gkai00mp.ttf") + @pdf.font("#{Prawn::DATADIR}/fonts/gkai00mp.ttf") @pdf.font.character_count("こんにちは世界").should == 7 @pdf.font.character_count("Hello, world!").should == 13 end it "should work on AFM fonts" do