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
STRETCH_ORDER =
%w[
  ultra-condensed extra-condensed condensed semi-condensed normal
  semi-expanded expanded extra-expanded ultra-expanded
].freeze
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 =
{
  'alignment-baseline' => Config.new('auto', false,
    %w[auto baseline before-edge text-before-edge middle central after-edge text-after-edge ideographic alphabetic hanging mathematical]),
  'baseline-shift'     => Config.new('baseline', false, ['baseline', 'sub', 'super', :length, :percentage]),
  'clip-path'          => Config.new('none', false, ['none', :funciri]),
  'clip-rule'          => Config.new('nonzero', true, %w[nonzero evenodd]),
  'color'              => Config.new(Color.black, true, [:color]),
  'display'            => Config.new('inline', false, %w[inline none]),
  'dominant-baseline'  => Config.new('auto', true, %w[auto middle central hanging alphabetic text-before-edge text-after-edge]),
  '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-stretch'       => Config.new('normal', true, STRETCH_ORDER + %w[wider narrower]),
  '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 bolder lighter 100 200 300 400 500 600 700 800 900]),
  # font-kerning is SVG 2 / CSS Fonts Level 3, supported here for forwards compatibility
  # since modern browsers have deprecated the SVG 1.1 kerning property.
  'font-kerning'       => Config.new('auto', true, %w[auto normal none]),
  'kerning'            => Config.new('auto', true, [:length, 'auto']),
  'letter-spacing'     => Config.new('normal', true, [:length, 'normal']),
  'word-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']),
  'mask'               => Config.new('none', false, [: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-dashoffset'  => Config.new(0, true, [:length, :percentage]),
  'stroke-linecap'     => Config.new('butt', true, %w[butt round square]),
  'stroke-linejoin'    => Config.new('miter', true, %w[miter round bevel]),
  'stroke-miterlimit'  => Config.new(4.0, true, [:positive_number]),
  '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, [:text_decoration]),
  'visibility'         => Config.new('visible', true, %w[visible hidden collapse]),
  'writing-mode'       => Config.new('horizontal-tb', true, %w[horizontal-tb vertical-rl vertical-lr])
}.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.



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

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

Instance Attribute Details

#important_idsObject (readonly)

Returns the value of attribute important_ids.



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

def important_ids
  @important_ids
end

Instance Method Details

#compute_properties(other) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/prawn/svg/properties.rb', line 123

def compute_properties(other)
  parent_font_stretch = @font_stretch_before_relative || font_stretch
  parent_font_weight = font_weight

  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

  if ['wider', 'narrower'].include?(font_stretch)
    @font_stretch_before_relative ||= parent_font_stretch
  else
    @font_stretch_before_relative = nil
  end

  @font_stretch = resolve_font_stretch(parent_font_stretch)
  @font_weight = resolve_font_weight(parent_font_weight)
  @important_ids += other.important_ids
  @numeric_font_size = calculate_numeric_font_size
  nil
end

#load_default_stylesheetObject



87
88
89
90
91
92
93
# File 'lib/prawn/svg/properties.rb', line 87

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

  self
end

#load_hash(hash) ⇒ Object



119
120
121
# File 'lib/prawn/svg/properties.rb', line 119

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

#numeric_font_sizeObject



109
110
111
# File 'lib/prawn/svg/properties.rb', line 109

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



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/prawn/svg/properties.rb', line 95

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)
  elsif name == 'marker'
    apply_marker_shorthand(value, important: important)
  end
end

#to_hObject



113
114
115
116
117
# File 'lib/prawn/svg/properties.rb', line 113

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