Skip to content

Commit

Permalink
Add support for TTC files
Browse files Browse the repository at this point in the history
  • Loading branch information
jamis authored and pointlessone committed Feb 13, 2017
1 parent 1444f4d commit 631331f
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 8 deletions.
Binary file added data/fonts/DejaVuSans.ttc
Binary file not shown.
8 changes: 6 additions & 2 deletions lib/prawn/font.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 0 additions & 6 deletions lib/prawn/font/dfont.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
34 changes: 34 additions & 0 deletions lib/prawn/font/ttc.rb
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions spec/prawn/font_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'] = {
Expand Down

0 comments on commit 631331f

Please sign in to comment.