Class: Prawn::Svg::Font

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/svg/font.rb

Constant Summary collapse

GENERIC_CSS_FONT_MAPPING =
{
"serif"      => "Times-Roman",
"sans-serif" => "Helvetica",
"cursive"    => "Times-Roman",
"fantasy"    => "Times-Roman",
"monospace"  => "Courier"}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, weight, style) ⇒ Font


61
62
63
64
65
# File 'lib/prawn/svg/font.rb', line 61

def initialize(name, weight, style)
  @name = self.class.correctly_cased_font_name(name) || name
  @weight = weight
  @style = style
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name


9
10
11
# File 'lib/prawn/svg/font.rb', line 9

def name
  @name
end

#styleObject (readonly)

Returns the value of attribute style


9
10
11
# File 'lib/prawn/svg/font.rb', line 9

def style
  @style
end

#weightObject (readonly)

Returns the value of attribute weight


9
10
11
# File 'lib/prawn/svg/font.rb', line 9

def weight
  @weight
end

Class Method Details

.correctly_cased_font_name(name) ⇒ Object


56
57
58
# File 'lib/prawn/svg/font.rb', line 56

def self.correctly_cased_font_name(name)
  @font_case_mapping[name.downcase]
end

.installed_fontsObject


52
53
54
# File 'lib/prawn/svg/font.rb', line 52

def self.installed_fonts
  @installed_fonts
end

.load(family, weight = nil, style = nil) ⇒ Object


11
12
13
14
15
16
17
18
19
20
21
# File 'lib/prawn/svg/font.rb', line 11

def self.load(family, weight = nil, style = nil)
  family.split(",").detect do |name|
    name = name.gsub(/['"]/, '').gsub(/\s{2,}/, ' ').strip.downcase

    # If it's a standard CSS font name, map it to one of the standard PDF fonts.
    name = GENERIC_CSS_FONT_MAPPING[name] || name

    font = new(name, weight, style)
    break font if font.installed?
  end
end

.load_external_fonts(fonts) ⇒ Object

This method is passed prawn's font_families hash. It'll be pre-populated with the fonts that prawn natively supports. We'll add fonts we find in the font path to this hash.


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/prawn/svg/font.rb', line 36

def self.load_external_fonts(fonts)
  Prawn::Svg::Interface.font_path.uniq.collect {|path| Dir["#{path}/**/*"]}.flatten.each do |filename|
    information = font_information(filename) rescue nil
    if information && font_name = (information[16] || information[1])
      subfamily = (information[17] || information[2] || "normal").gsub(/\s+/, "_").downcase.to_sym
      subfamily = :normal if subfamily == :regular
      (fonts[font_name] ||= {})[subfamily] = filename
    end
  end

  @font_case_mapping = {}
  fonts.each {|key, _| @font_case_mapping[key.downcase] = key}

  @installed_fonts = fonts
end

.weight_for_css_font_weight(weight) ⇒ Object


23
24
25
26
27
28
29
30
31
32
# File 'lib/prawn/svg/font.rb', line 23

def self.weight_for_css_font_weight(weight)
  case weight
  when '100', '200', '300' then :light
  when '400', '500'        then :normal
  when '600'               then :semibold
  when '700', 'bold'       then :bold
  when '800'               then :extrabold
  when '900'               then :black
  end
end

Instance Method Details

#installed?Boolean


67
68
69
70
# File 'lib/prawn/svg/font.rb', line 67

def installed?
  subfamilies = self.class.installed_fonts[name]
  !subfamilies.nil? && subfamilies.key?(subfamily)
end

#subfamilyObject

Construct a subfamily name, ensuring that the subfamily is a valid one for the font.


73
74
75
76
77
78
79
80
81
82
83
# File 'lib/prawn/svg/font.rb', line 73

def subfamily
  if subfamilies = self.class.installed_fonts[name]
    if subfamilies.key?(subfamily_name)
      subfamily_name
    elsif subfamilies.key?(:normal)
      :normal
    else
      subfamilies.keys.first
    end
  end
end