Skip to content

Commit

Permalink
Use module namespace for fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
pointlessone committed Jul 25, 2020
1 parent 4b02b01 commit 41403fe
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 19 deletions.
23 changes: 15 additions & 8 deletions lib/prawn/font.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
#
# This is free software. Please see the LICENSE and COPYING files for details.
#
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 @@ -37,7 +33,7 @@ class Document
#
# The :name parameter must be a string. It can be one of the 14 built-in
# fonts supported by PDF, or the location of a TTF file. The
# Font::AFM::BUILT_INS array specifies the valid built in font values.
# Fonts::AFM::BUILT_INS array specifies the valid built in font values.
#
# If a ttf font is specified, the glyphs necessary to render your document
# will be embedded in the rendered PDF. This should be your preferred option
Expand Down Expand Up @@ -283,6 +279,17 @@ def width_of_string(string, options = {})
# Provides font information and helper functions.
#
class Font
require_relative 'fonts/afm'
require_relative 'fonts/ttf'
require_relative 'fonts/dfont'
require_relative 'fonts/ttc'

# @deprecated
AFM = Fonts::AFM
TTF = Fonts::TTF
DFont = Fonts::DFont
TTC = Fonts::TTC

# The current font name
attr_reader :name

Expand All @@ -293,9 +300,9 @@ 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, *.ttc goes to
# Font::TTC.new, and anything else will be passed through to
# Font::AFM.new()
# *.ttf will call Fonts::TTF.new, *.dfont Fonts::DFont.new, *.ttc goes to
# Fonts::TTC.new, and anything else will be passed through to
# Fonts::AFM.new()
def self.load(document, src, options = {})
case font_format(src, options)
when 'ttf' then TTF.new(document, src, options)
Expand Down
6 changes: 3 additions & 3 deletions lib/prawn/font/afm.rb → lib/prawn/fonts/afm.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

# prawn/font/afm.rb : Implements AFM font support for Prawn
# Implements AFM font support for Prawn
#
# Copyright May 2008, Gregory Brown / James Healy. All Rights Reserved.
#
Expand All @@ -9,7 +9,7 @@
require_relative '../encoding'

module Prawn
class Font
module Fonts
# @private

class AFM < Font
Expand Down Expand Up @@ -113,7 +113,7 @@ def normalize_encoding(text)
raise Prawn::Errors::IncompatibleStringEncoding,
"Your document includes text that's not compatible with the " \
"Windows-1252 character set.\n" \
"If you need full UTF-8 support, use TTF fonts instead of PDF's " \
"If you need full UTF-8 support, use external fonts instead of PDF's " \
"built-in fonts.\n"
end

Expand Down
2 changes: 1 addition & 1 deletion lib/prawn/font/dfont.rb → lib/prawn/fonts/dfont.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require_relative 'ttf'

module Prawn
class Font
module Fonts
# @private
class DFont < TTF
# Returns a list of the names of all named fonts in the given dfont file.
Expand Down
2 changes: 1 addition & 1 deletion lib/prawn/font/ttc.rb → lib/prawn/fonts/ttc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require_relative 'ttf'

module Prawn
class Font
module Fonts
# @private
class TTC < TTF
# Returns a list of the names of all named fonts in the given ttc file.
Expand Down
2 changes: 1 addition & 1 deletion lib/prawn/font/ttf.rb → lib/prawn/fonts/ttf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
require 'ttfunk/subset_collection'

module Prawn
class Font
module Fonts
# @private
class TTF < Font
attr_reader :ttf, :subsets
Expand Down
2 changes: 1 addition & 1 deletion lib/prawn/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def draw_text!(text, options)
"internationalized text.\nIf you need full UTF-8 support, " \
"consider using a TTF font instead.\n\nTo disable this " \
"warning, add the following line to your code:\n" \
"Prawn::Font::AFM.hide_m17n_warning = true\n"
"Prawn::Fonts::AFM.hide_m17n_warning = true\n"

font.class.hide_m17n_warning = true
end
Expand Down
2 changes: 1 addition & 1 deletion manual/example_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
require 'prawn/manual_builder'

Prawn::ManualBuilder.manual_dir = File.dirname(__FILE__)
Prawn::Font::AFM.hide_m17n_warning = true
Prawn::Fonts::AFM.hide_m17n_warning = true
4 changes: 2 additions & 2 deletions spec/prawn/font_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -461,14 +461,14 @@ def page_should_not_include_font(font)
let(:file) { "#{Prawn::DATADIR}/fonts/Panic+Sans.dfont" }

it 'lists all named fonts' do
list = Prawn::Font::DFont.named_fonts(file)
list = Prawn::Fonts::DFont.named_fonts(file)
expect(list).to match_array(%w[
PanicSans PanicSans-Bold PanicSans-BoldItalic PanicSans-Italic
])
end

it 'counts the number of fonts in the file' do
expect(Prawn::Font::DFont.font_count(file)).to eq(4)
expect(Prawn::Fonts::DFont.font_count(file)).to eq(4)
end

it 'defaults selected font to the first one if not specified' do
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
require_relative '../lib/prawn'

Prawn.debug = true
Prawn::Font::AFM.hide_m17n_warning = true
Prawn::Fonts::AFM.hide_m17n_warning = true

require 'rspec'
require 'pdf/reader'
Expand Down

0 comments on commit 41403fe

Please sign in to comment.