Module: Thinreports::BasicReport::Generator::PDF::Font

Included in:
Document
Defined in:
lib/thinreports/basic_report/generator/pdf/document/font.rb

Constant Summary collapse

FONT_STORE =
Thinreports.root.join('fonts')
BUILTIN_FONTS =
{
  'IPAMincho'  => FONT_STORE.join('ipam.ttf').to_s,
  'IPAPMincho' => FONT_STORE.join('ipamp.ttf').to_s,
  'IPAGothic'  => FONT_STORE.join('ipag.ttf').to_s,
  'IPAPGothic' => FONT_STORE.join('ipagp.ttf').to_s
}.freeze
DEFAULT_FALLBACK_FONTS =
%w[IPAMincho].freeze
PRAWN_BUINTIN_FONT_ALIASES =
{
  'Courier New' => 'Courier',
  'Times New Roman' => 'Times-Roman'
}.freeze

Instance Method Summary collapse

Instance Method Details

#default_familyString

Returns:

  • (String)


65
66
67
# File 'lib/thinreports/basic_report/generator/pdf/document/font.rb', line 65

def default_family
  'Helvetica'
end

#default_family_if_missing(family) ⇒ String

Parameters:

  • family (String)

Returns:

  • (String)


71
72
73
# File 'lib/thinreports/basic_report/generator/pdf/document/font.rb', line 71

def default_family_if_missing(family)
  pdf.font_families.key?(family) ? family : default_family
end

#font_has_style?(font_name, font_style) ⇒ Boolean

Parameters:

  • font_name (String)
  • font_style (:bold, :italic)

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
# File 'lib/thinreports/basic_report/generator/pdf/document/font.rb', line 78

def font_has_style?(font_name, font_style)
  font = pdf.font_families[font_name]

  return false unless font
  return false unless font.key?(font_style)

  font[font_style] != font[:normal]
end

#install_font(name, file) ⇒ String

Returns installed font name.

Parameters:

  • name (String)
  • file (String)

Returns:

  • (String)

    installed font name

Raises:



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/thinreports/basic_report/generator/pdf/document/font.rb', line 52

def install_font(name, file)
  raise Errors::FontFileNotFound unless File.exist?(file)

  pdf.font_families[name] = {
    normal: file,
    bold: file,
    italic: file,
    bold_italic: file
  }
  name
end

#setup_fontsObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/thinreports/basic_report/generator/pdf/document/font.rb', line 24

def setup_fonts
  # Install built-in fonts.
  BUILTIN_FONTS.each do |font_name, font_path|
    install_font(font_name, font_path)
  end

  # Create aliases from the font list provided by Prawn.
  PRAWN_BUINTIN_FONT_ALIASES.each do |alias_name, name|
    pdf.font_families[alias_name] = pdf.font_families[name]
  end

  # Setup custom fallback fonts
  fallback_fonts = Thinreports.config.fallback_fonts.uniq
  fallback_fonts.map!.with_index do |font, i|
    if pdf.font_families.key?(font)
      font
    else
      install_font "Custom-fallback-font#{i}", font
    end
  end

  # Set fallback fonts
  pdf.fallback_fonts(fallback_fonts + DEFAULT_FALLBACK_FONTS)
end