Class: Prawn::SVG::Properties

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

Defined Under Namespace

Classes: Config

Constant Summary collapse

EM =
16
FONT_SIZES =
{
  'xx-small' => EM / 4,
  'x-small'  => EM / 4 * 2,
  'small'    => EM / 4 * 3,
  'medium'   => EM / 4 * 4,
  'large'    => EM / 4 * 5,
  'x-large'  => EM / 4 * 6,
  'xx-large' => EM / 4 * 7
}
PROPERTIES =
{
  "clip-path"        => Config.new("none", false, %w(inherit none)),
  "color"            => Config.new('', true),
  "display"          => Config.new("inline", false, %w(inherit inline none), true),
  "fill"             => Config.new("black", true, %w(inherit none currentColor)),
  "fill-opacity"     => Config.new("1", true),
  "fill-rule"        => Config.new("nonzero", true, %w(inherit nonzero evenodd)),
  "font-family"      => Config.new("sans-serif", true),
  "font-size"        => Config.new("medium", true, %w(inherit xx-small x-small small medium large x-large xx-large larger smaller)),
  "font-style"       => Config.new("normal", true, %w(inherit normal italic oblique), true),
  "font-variant"     => Config.new("normal", true, %w(inherit normal small-caps), true),
  "font-weight"      => Config.new("normal", true, %w(inherit normal bold 100 200 300 400 500 600 700 800 900), true), # bolder/lighter not supported
  "letter-spacing"   => Config.new("normal", true, %w(inherit normal)),
  "marker-end"       => Config.new("none", true, %w(inherit none)),
  "marker-mid"       => Config.new("none", true, %w(inherit none)),
  "marker-start"     => Config.new("none", true, %w(inherit none)),
  "opacity"          => Config.new("1", false),
  "overflow"         => Config.new('visible', false, %w(inherit visible hidden scroll auto), true),
  "stop-color"       => Config.new("black", false, %w(inherit none currentColor)),
  "stroke"           => Config.new("none", true, %w(inherit none currentColor)),
  "stroke-dasharray" => Config.new("none", true, %w(inherit none)),
  "stroke-linecap"   => Config.new("butt", true, %w(inherit butt round square), true),
  "stroke-linejoin"  => Config.new("miter", true, %w(inherit miter round bevel), true),
  "stroke-opacity"   => Config.new("1", true),
  "stroke-width"     => Config.new("1", true),
  "text-anchor"      => Config.new("start", true, %w(inherit start middle end), true),
  'text-decoration'  => Config.new('none', true, %w(inherit none underline), true),
  "dominant-baseline" => Config.new("auto", true, %w(inherit auto middle), true),
}.freeze
PROPERTY_CONFIGS =
PROPERTIES.values
NAMES =
PROPERTIES.keys
ATTR_NAMES =
PROPERTIES.keys.map { |name| name.gsub('-', '_') }

Instance Method Summary collapse

Instance Method Details

#compute_properties(other) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/prawn/svg/properties.rb', line 90

def compute_properties(other)
  PROPERTY_CONFIGS.each do |config|
    value = other.send(config.attr)

    if value && value != 'inherit'
      value = compute_font_size_property(value).to_s if config.attr == "font_size"
      instance_variable_set(config.ivar, value)

    elsif value.nil? && !config.inheritable?
      instance_variable_set(config.ivar, config.default)
    end
  end
end

#load_default_stylesheetObject



56
57
58
59
60
61
62
# File 'lib/prawn/svg/properties.rb', line 56

def load_default_stylesheet
  PROPERTY_CONFIGS.each do |config|
    instance_variable_set(config.ivar, config.default)
  end

  self
end

#load_hash(hash) ⇒ Object



86
87
88
# File 'lib/prawn/svg/properties.rb', line 86

def load_hash(hash)
  hash.each { |name, value| set(name, value) if value }
end

#numerical_font_sizeObject



104
105
106
107
# File 'lib/prawn/svg/properties.rb', line 104

def numerical_font_size
  # px = pt for PDFs
  FONT_SIZES[font_size] || font_size.to_f
end

#set(name, value) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/prawn/svg/properties.rb', line 64

def set(name, value)
  if config = PROPERTIES[name.to_s.downcase]
    value = value.strip
    keyword = value.downcase
    keywords = config.keywords || ['inherit']

    if keywords.include?(keyword)
      value = keyword
    elsif config.keyword_restricted?
      value = config.default
    end

    instance_variable_set(config.ivar, value)
  end
end

#to_hObject



80
81
82
83
84
# File 'lib/prawn/svg/properties.rb', line 80

def to_h
  PROPERTIES.each.with_object({}) do |(name, config), result|
    result[name] = instance_variable_get(config.ivar)
  end
end