Sha256: 75096eab7d24a95f44f8fc7154b2984cd47d44aff6f7d09c96b80a6429e2e96f

Contents?: true

Size: 1.89 KB

Versions: 73

Compression:

Stored size: 1.89 KB

Contents

# -*- encoding: utf-8 -*-

require 'test_helper'
require 'hexapdf/document'

describe HexaPDF::Document::Fonts do
  before do
    @doc = HexaPDF::Document.new
    @doc.config['font_loader'] = []
  end

  describe "add" do
    before do
      @doc.config['font_loader'] << lambda do |doc, name, **options|
        assert_equal(@doc, doc)
        if name == :TestFont
          x = Object.new
          x.define_singleton_method(:name) do
            options[:variant] == :bold ? :BoldFont : :NormalFont
          end
          x
        else
          nil
        end
      end
    end

    it "loads the specified font" do
      assert_equal(:NormalFont, @doc.fonts.add(:TestFont).name)
      assert_equal(:BoldFont, @doc.fonts.add(:TestFont, variant: :bold).name)
    end

    it "caches loaded fonts" do
      font = @doc.fonts.add(:TestFont)
      assert_same(font, @doc.fonts.add(:TestFont))
      assert_same(font, @doc.fonts.add(:TestFont, variant: :none))
    end

    it "fails if the requested font is not found" do
      @doc.config['font_loader'] << 'HexaPDF::FontLoader::Standard14'
      error = assert_raises(HexaPDF::Error) { @doc.fonts.add("Unknown") }
      assert_match(/Times \(none/, error.message)
    end

    it "raises an error if a font loader cannot be correctly retrieved" do
      @doc.config['font_loader'][0] = 'UnknownFontLoader'
      assert_raises(HexaPDF::Error) { @doc.fonts.add(:Other) }
    end
  end

  it "returns the configured fonts" do
    @doc.config['font_loader'] << 'HexaPDF::FontLoader::Standard14'
    @doc.config['font_loader'] << 'HexaPDF::FontLoader::FromConfiguration'
    @doc.config['font.map'] = {'Times' => {heavy: 'none', none: 'none'}, 'Other' => {none: 'none'}}
    fonts = @doc.fonts.configured_fonts
    assert_equal([:none], fonts['Symbol'])
    assert_equal([:none, :bold, :italic, :bold_italic, :heavy], fonts['Times'])
    assert_equal([:none], fonts['Other'])
  end
end

Version data entries

73 entries across 73 versions & 1 rubygems

Version Path
hexapdf-1.0.2 test/hexapdf/document/test_fonts.rb
hexapdf-1.0.1 test/hexapdf/document/test_fonts.rb
hexapdf-1.0.0 test/hexapdf/document/test_fonts.rb
hexapdf-0.47.0 test/hexapdf/document/test_fonts.rb
hexapdf-0.46.0 test/hexapdf/document/test_fonts.rb
hexapdf-0.45.0 test/hexapdf/document/test_fonts.rb
hexapdf-0.44.0 test/hexapdf/document/test_fonts.rb
hexapdf-0.41.0 test/hexapdf/document/test_fonts.rb
hexapdf-0.40.0 test/hexapdf/document/test_fonts.rb
hexapdf-0.39.1 test/hexapdf/document/test_fonts.rb
hexapdf-0.39.0 test/hexapdf/document/test_fonts.rb
hexapdf-0.38.0 test/hexapdf/document/test_fonts.rb
hexapdf-0.37.2 test/hexapdf/document/test_fonts.rb
hexapdf-0.37.1 test/hexapdf/document/test_fonts.rb
hexapdf-0.37.0 test/hexapdf/document/test_fonts.rb
hexapdf-0.36.0 test/hexapdf/document/test_fonts.rb
hexapdf-0.35.1 test/hexapdf/document/test_fonts.rb
hexapdf-0.35.0 test/hexapdf/document/test_fonts.rb
hexapdf-0.34.1 test/hexapdf/document/test_fonts.rb
hexapdf-0.34.0 test/hexapdf/document/test_fonts.rb