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
}.freeze
PROPERTIES =
{
  'clip-path'         => Config.new('none', false, ['none', :funciri]),
  'color'             => Config.new(Color.black, true, [:color]),
  'display'           => Config.new('inline', false, %w[inline none]),
  'dominant-baseline' => Config.new('auto', true, %w[auto middle]),
  'fill'              => Config.new(Paint.black, true, [:paint]),
  'fill-opacity'      => Config.new(1.0, true, [:number]),
  'fill-rule'         => Config.new('nonzero', true, %w[nonzero evenodd]),
  'font-family'       => Config.new('sans-serif', true, [:any]),
  # Only the computed (numeric) value of font-size is inherited, not the value itself
  'font-size'         => Config.new(nil, false,
    [:positive_length, :positive_percentage, 'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large', 'larger', 'smaller']),
  'font-style'        => Config.new('normal', true, %w[normal italic oblique]),
  'font-variant'      => Config.new('normal', true, %w[normal small-caps]),
  'font-weight'       => Config.new('normal', true, %w[normal bold 100 200 300 400 500 600 700 800 900]), # bolder/lighter not supported
  'letter-spacing'    => Config.new('normal', true, [:length, 'normal']),
  'marker-end'        => Config.new('none', true, [:funciri, 'none']),
  'marker-mid'        => Config.new('none', true, [:funciri, 'none']),
  'marker-start'      => Config.new('none', true, [:funciri, 'none']),
  'opacity'           => Config.new(1.0, false, [:number]),
  'overflow'          => Config.new('visible', false, %w[visible hidden scroll auto]),
  'stop-color'        => Config.new(Color.black, false, [:color_with_icc, 'currentcolor']),
  'stop-opacity'      => Config.new(1.0, false, [:number]),
  'stroke'            => Config.new(Paint.none, true, [:paint]),
  'stroke-dasharray'  => Config.new('none', true, [:dasharray, 'none']),
  'stroke-linecap'    => Config.new('butt', true, %w[butt round square]),
  'stroke-linejoin'   => Config.new('miter', true, %w[miter round bevel]),
  'stroke-opacity'    => Config.new(1.0, true, [:number]),
  'stroke-width'      => Config.new(1.0, true, [:positive_length, :positive_percentage]),
  'text-anchor'       => Config.new('start', true, %w[start middle end]),
  'text-decoration'   => Config.new('none', true, %w[none underline]),
  'visibility'        => Config.new('visible', true, %w[visible hidden collapse])
}.freeze
PROPERTY_CONFIGS =
PROPERTIES.values
NAMES =
PROPERTIES.keys
ATTR_NAMES =
PROPERTIES.keys.map { |name| name.gsub('-', '_') }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProperties

Returns a new instance of Properties.



63
64
65
66
# File 'lib/prawn/svg/properties.rb', line 63

def initialize
  @numeric_font_size = EM
  @important_ids = []
end

Instance Attribute Details

#important_idsObject (readonly)

Returns the value of attribute important_ids.



61
62
63
# File 'lib/prawn/svg/properties.rb', line 61

def important_ids
  @important_ids
end

Instance Method Details

#compute_properties(other) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/prawn/svg/properties.rb', line 102

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

    if value && value != 'inherit' && (!@important_ids.include?(config.id) || other.important_ids.include?(config.id))
      instance_variable_set(config.ivar, value)

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

  @important_ids += other.important_ids
  @numeric_font_size = calculate_numeric_font_size
  nil
end

#load_default_stylesheetObject



68
69
70
71
72
73
74
# File 'lib/prawn/svg/properties.rb', line 68

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

  self
end

#load_hash(hash) ⇒ Object



98
99
100
# File 'lib/prawn/svg/properties.rb', line 98

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

#numeric_font_sizeObject



88
89
90
# File 'lib/prawn/svg/properties.rb', line 88

def numeric_font_size
  @numeric_font_size or raise 'numeric_font_size not set; this is only present in computed properties'
end

#set(name, value, important: false) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/prawn/svg/properties.rb', line 76

def set(name, value, important: false)
  name = name.to_s.downcase
  if (config = PROPERTIES[name])
    if (value = parse_value(config, value.strip)) && (important || !@important_ids.include?(config.id))
      @important_ids << config.id if important
      instance_variable_set(config.ivar, value)
    end
  elsif name == 'font'
    apply_font_shorthand(value)
  end
end

#to_hObject



92
93
94
95
96
# File 'lib/prawn/svg/properties.rb', line 92

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