class CssCompare::CSS::Component::FontFace

Represents the @font-face directive.

Multiple @font-face rules can be used to construct font families with a variety of faces. Each @font-face rule specifies a value for every font descriptor, either implicitly or explicitly. Those not given explicit values in the rule take the initial value listed with each descriptor in the w3.org specification.

If multiple declarations of @font-face rules, that share the same ‘font-family` and `src` values are present, the last declaration overrides the other.

‘Font-family` property is saved downcased, since the user agents, when matching font-family names, do it in a case-insensitive manner.

@see www.w3.org/TR/css-fonts-3/#font-face-rule

Constants

INITIAL_VALUES

Attributes

declarations[R]

Public Class Methods

new(children) click to toggle source

@param [Array<Sass::Tree::PropNode>] children font properties

# File lib/css_compare/css/component/font_face.rb, line 26
def initialize(children)
  @declarations = {}
  init_declarations
  process_declarations(children)
end

Public Instance Methods

==(other) click to toggle source

Checks, whether two @font-face declarations are equal.

No need to check, whether both font-faces have the same keys, since they are also initialized with the default values.

@param [FontFace] other the @font-face to compare this

with.
# File lib/css_compare/css/component/font_face.rb, line 40
def ==(other)
  @declarations.all? { |k, _| @declarations[k] == other.declarations[k] }
end
family() click to toggle source

@return [String, nil] font-family name, if set

# File lib/css_compare/css/component/font_face.rb, line 56
def family
  @declarations['font-family']
end
src() click to toggle source

@return [String, nil] the source of the font if set

# File lib/css_compare/css/component/font_face.rb, line 61
def src
  @declarations['src']
end
to_json() click to toggle source

Creates the JSON representation of this object.

@return [Hash]

# File lib/css_compare/css/component/font_face.rb, line 68
def to_json
  @declarations
end
valid?() click to toggle source

Tells, whether this rule is valid or not.

@font-face rules require a font-family and src descriptor; if either of these are missing, the @font-face rule is invalid and must be ignored entirely.

@return [Boolean]

# File lib/css_compare/css/component/font_face.rb, line 51
def valid?
  family && src
end

Private Instance Methods

get_synonym(property, key) click to toggle source

Returns the synonym for a property’s value.

@example

get_synonym(INITIAL_VALUES[:font-weight], [:bold]) #=> '600'

@return [String, nil] a string, if a synonym exists

nil otherwise
# File lib/css_compare/css/component/font_face.rb, line 183
def get_synonym(property, key)
  return unless property[:synonyms] && property[:synonyms].include?(key)
  property[:synonyms][key]
end
init_declarations() click to toggle source

Initializes the font-face with values from the official specifications.

@return [Void]

# File lib/css_compare/css/component/font_face.rb, line 141
def init_declarations
  INITIAL_VALUES.each { |k, v| @declarations[k.to_s.tr('_', '-')] = v[:default] }
end
process_declarations(children) click to toggle source

Processes the @font-face declarations and set the values if:

1. the property processed is a valid font-face property
2. the property has a valid value

If the property’s value is not valid, it falls back to the default one.

@param [Array<Sass::Tree::PropNode>] children @return [Void]

# File lib/css_compare/css/component/font_face.rb, line 154
def process_declarations(children)
  children.each do |child|
    next unless child.is_a?(Sass::Tree::PropNode)
    name = child.resolved_name
    value = child.resolved_value
    key = name.tr('-', '_').to_sym
    property = INITIAL_VALUES[key]
    next unless property
    @declarations[name] = value.downcase if name == 'font-family'
    @declarations[name] = value.gsub(/'|"/, '') if name == 'src'
    next if name == 'font-family' || name == 'src'
    @declarations[name] = value
    allowed_values = property[:allowed]
    next unless allowed_values
    if allowed_values.include?(value)
      @declarations[name] = get_synonym(property, value.to_sym) || value
    else
      @declaration[name] = property[:default]
    end
  end
end