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_registry: nil) ⇒ Font

Returns a new instance of Font.



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

def initialize(name, weight, style, font_registry: nil)
  @font_registry = font_registry
  unless font_registry.installed_fonts.key?(name)
    # map generic font name to one of the built-in PDF fonts if not already mapped
    name = GENERIC_CSS_FONT_MAPPING[name] || name
  end
  @name = font_registry.correctly_cased_font_name(name) || name
  @weight = weight
  @style = style
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#styleObject (readonly)

Returns the value of attribute style.



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

def style
  @style
end

#weightObject (readonly)

Returns the value of attribute weight.



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

def weight
  @weight
end

Class Method Details

.weight_for_css_font_weight(weight) ⇒ Object



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

def self.weight_for_css_font_weight(weight)
  case weight
  when '100', '200', '300'    then :light
  when '400', '500', 'normal' 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

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/prawn/svg/font.rb', line 34

def installed?
  subfamilies = @font_registry.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.



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

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