Module: DocPDF::FontFiles

Defined in:
lib/docpdf/font_files.rb

Overview

Prawn's built-in AFM fonts only cover Windows-1252, so any text outside it fails to render. Pointing an adapter at a TrueType file lifts that limit. Each backend wants the styles keyed differently, so normalize once here.

Constant Summary collapse

STYLES =
%i[normal bold italic bold_italic].freeze
HEXAPDF_STYLES =

HexaPDF calls the unstyled variant :none where Prawn calls it :normal.

{ normal: :none, bold: :bold, italic: :italic, bold_italic: :bold_italic }.freeze

Class Method Summary collapse

Class Method Details

.for_hexapdf(font_file) ⇒ Object



23
24
25
26
27
28
# File 'lib/docpdf/font_files.rb', line 23

def for_hexapdf(font_file)
  styles = normalize(font_file)
  return unless styles

  styles.each_with_object({}) { |(style, path), map| map[HEXAPDF_STYLES.fetch(style)] = path }
end

.for_prawn(font_file) ⇒ Object



30
31
32
# File 'lib/docpdf/font_files.rb', line 30

def for_prawn(font_file)
  normalize(font_file)
end

.normalize(font_file) ⇒ Object

Accepts a path for the regular weight, or a hash of styles:

"DejaVuSans.ttf"
{ normal: "...", bold: "...", italic: "...", bold_italic: "..." }


15
16
17
18
19
20
21
# File 'lib/docpdf/font_files.rb', line 15

def normalize(font_file)
  return if font_file.nil?
  return { normal: font_file.to_s } if font_file.is_a?(String) || font_file.is_a?(Pathname)

  styles = font_file.to_h.transform_keys(&:to_sym).slice(*STYLES)
  styles.empty? ? nil : styles.transform_values(&:to_s)
end