class Prawn::SVG::Properties
Constants
- ATTR_NAMES
- Config
- EM
- FONT_KEYWORD_MAPPING
- FONT_SIZES
- NAMES
- PROPERTIES
- PROPERTY_CONFIGS
Attributes
Public Class Methods
Source
# File lib/prawn/svg/properties.rb, line 63 def initialize @numeric_font_size = EM @important_ids = [] end
Public Instance Methods
Source
# 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
Source
# 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
Source
# File lib/prawn/svg/properties.rb, line 98 def load_hash(hash) hash.each { |name, value| set(name, value) if value } end
Source
# 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
Source
# 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
Source
# 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
Private Instance Methods
Source
# File lib/prawn/svg/properties.rb, line 200 def apply_font_shorthand(value) if value.strip.match(/\s/).nil? case value.strip.downcase when 'inherit' load_hash('font-style' => 'inherit', 'font-variant' => 'inherit', 'font-weight' => 'inherit', 'font-size' => 'inherit', 'font-family' => 'inherit') when 'caption', 'icon', 'menu', 'message-box', 'small-caption', 'status-bar' load_hash('font-style' => 'normal', 'font-variant' => 'normal', 'font-weight' => 'normal', 'font-size' => 'medium', 'font-family' => 'sans-serif') end return end properties = ['font-style', 'font-variant', 'font-weight'].each.with_object({}) do |property, result| result[property] = PROPERTIES[property].default end values = CSS::FontParser.parse(value) return if values.length < 2 properties['font-family'] = values.pop font_size = values.pop.sub(%r{/.*}, '') values.each do |keyword| keyword = keyword.downcase if (property = FONT_KEYWORD_MAPPING[keyword]) properties[property] = keyword else break end end or return set('font-size', font_size) or return load_hash(properties) value end
Source
# File lib/prawn/svg/properties.rb, line 121 def calculate_numeric_font_size case font_size when Length font_size.to_pixels(nil, numeric_font_size) when Percentage font_size.to_factor * numeric_font_size when Numeric font_size when 'larger' numeric_font_size + 4 when 'smaller' numeric_font_size - 4 when nil, 'inherit' numeric_font_size when String FONT_SIZES[font_size] or raise "Unknown font size keyword: #{font_size}" else raise "Unknown font size property value: #{font_size.inspect}" end end
Source
# File lib/prawn/svg/properties.rb, line 142 def parse_value(config, value) keyword = value.downcase return 'inherit' if keyword == 'inherit' config.valid_values.detect do |type| result = parse_value_with_type(type, value, keyword) break result if result end end
Source
# File lib/prawn/svg/properties.rb, line 153 def parse_value_with_type(type, value, keyword) case type when String keyword if type == keyword when :color values = Prawn::SVG::CSS::ValuesParser.parse(value) Prawn::SVG::Color.parse(values[0]) if values.length == 1 when :color_with_icc case Prawn::SVG::CSS::ValuesParser.parse(value) in [other, ['icc-color', _args]] Prawn::SVG::Color.parse(other) in [other] # rubocop:disable Lint/DuplicateBranch Prawn::SVG::Color.parse(other) else nil end when :paint Paint.parse(value) when :funciri FuncIRI.parse(value) when :dasharray value.split(Prawn::SVG::Elements::COMMA_WSP_REGEXP).map do |value| Length.parse(value) || Percentage.parse(value) || break end when :number Float(value, exception: false) when :length Length.parse(value) when :percentage Percentage.parse(value) when :positive_length Length.parse(value, positive_only: true) when :positive_percentage Percentage.parse(value, positive_only: true) when :any value else raise "Unknown valid value type: #{type}" end end