diff --git a/data/fonts/DejaVuSans.ttc b/data/fonts/DejaVuSans.ttc new file mode 100644 index 000000000..5657e1a4a Binary files /dev/null and b/data/fonts/DejaVuSans.ttc differ diff --git a/lib/prawn/font.rb b/lib/prawn/font.rb index adf1b57a7..84c0c67a1 100644 --- a/lib/prawn/font.rb +++ b/lib/prawn/font.rb @@ -7,6 +7,7 @@ require_relative 'font/afm' require_relative 'font/ttf' require_relative 'font/dfont' +require_relative 'font/ttc' require_relative 'font_metric_cache' module Prawn @@ -289,12 +290,14 @@ class Font attr_reader :options # Shortcut interface for constructing a font object. Filenames of the form - # *.ttf will call Font::TTF.new, *.dfont Font::DFont.new, and anything else - # will be passed through to Font::AFM.new() + # *.ttf will call Font::TTF.new, *.dfont Font::DFont.new, *.ttc goes to + # Font::TTC.new, and anything else will be passed through to + # Font::AFM.new() def self.load(document, src, options = {}) case font_format(src, options) when 'ttf' then TTF.new(document, src, options) when 'dfont' then DFont.new(document, src, options) + when 'ttc' then TTC.new(document, src, options) else AFM.new(document, src, options) end end @@ -305,6 +308,7 @@ def self.font_format(src, options) case src.to_s when /\.ttf$/i then return 'ttf' when /\.dfont$/i then return 'dfont' + when /\.ttc$/i then return 'ttc' else return 'afm' end end diff --git a/lib/prawn/font/dfont.rb b/lib/prawn/font/dfont.rb index 9212edf8a..429cb3c15 100644 --- a/lib/prawn/font/dfont.rb +++ b/lib/prawn/font/dfont.rb @@ -1,9 +1,3 @@ -# font.rb : The Prawn font class -# -# Copyright November 2008, Jamis Buck. All Rights Reserved. -# -# This is free software. Please see the LICENSE and COPYING files for details. -# require_relative 'ttf' module Prawn diff --git a/lib/prawn/font/ttc.rb b/lib/prawn/font/ttc.rb new file mode 100644 index 000000000..10001c92c --- /dev/null +++ b/lib/prawn/font/ttc.rb @@ -0,0 +1,34 @@ +require_relative 'ttf' + +module Prawn + class Font + # @private + class TTC < TTF + # Returns a list of the names of all named fonts in the given ttc file. + # They are returned in order of their appearance in the file. + # + def self.font_names(file) + TTFunk::Collection.open(file) do |ttc| + ttc.map { |font| font.name.font_name.first } + end + end + + private + + def read_ttf_file + TTFunk::File.from_ttc( + @name, + font_option_to_index(@name, @options[:font]) + ) + end + + def font_option_to_index(file, option) + if option.is_a?(Numeric) + option + else + self.class.font_names(file).index { |n| n == option } || 0 + end + end + end + end +end diff --git a/spec/prawn/font_spec.rb b/spec/prawn/font_spec.rb index 4cd21c7d4..922479f4f 100644 --- a/spec/prawn/font_spec.rb +++ b/spec/prawn/font_spec.rb @@ -159,6 +159,38 @@ expect(name).to eq('subset+PanicSans-Italic') end + it 'allows font familes to be defined in a single ttc' do + file = "#{Prawn::DATADIR}/fonts/DejaVuSans.ttc" + pdf.font_families['DejaVu Sans'] = { + normal: { file: file, font: 0 }, + bold: { file: file, font: 1 } + } + + pdf.font 'DejaVu Sans', style: :bold + pdf.text 'In PanicSans-Bold' + + text = PDF::Inspector::Text.analyze(pdf.render) + name = text.font_settings.map { |e| e[:name] }.first.to_s + name = name.sub(/\w+\+/, 'subset+') + expect(name).to eq('subset+DejaVuSans-Bold') + end + + it 'allows fonts to be indexed by name in a ttc file' do + file = "#{Prawn::DATADIR}/fonts/DejaVuSans.ttc" + pdf.font_families['DejaVu Sans'] = { + normal: { file: file, font: 'DejaVu Sans' }, + bold: { file: file, font: 'DejaVu Sans Bold' } + } + + pdf.font 'DejaVu Sans', style: :bold + pdf.text 'In PanicSans-Bold' + + text = PDF::Inspector::Text.analyze(pdf.render) + name = text.font_settings.map { |e| e[:name] }.first.to_s + name = name.sub(/\w+\+/, 'subset+') + expect(name).to eq('subset+DejaVuSans-Bold') + end + it 'accepts Pathname objects for font files' do file = Pathname.new("#{Prawn::DATADIR}/fonts/DejaVuSans.ttf") pdf.font_families['DejaVu Sans'] = {