Class: Fontico::Preprocessor

Inherits:
Object
  • Object
show all
Defined in:
lib/fontico/preprocessor.rb

Overview

Normalises one SVG into a body fragment that is safe to merge with any other. Implements docs/icon-authoring.html section 01.

Vendor icons arrive uniform; first-party exports do not. Everything here exists because a real Inkscape or Illustrator export breaks it.

Defined Under Namespace

Classes: Result

Constant Summary collapse

DROP_ELEMENTS =

Editor state, document furniture, and anything executable.

%w[
  script metadata foreignObject sodipodi:namedview
  title desc inkscape:templateinfo
].freeze
DROP_ATTR_PREFIXES =

Attributes that never survive: editor namespaces and event handlers.

%w[sodipodi: inkscape: serif: xml:space].freeze
URL_ATTRS =

Attributes whose value may contain url(#id) and must be rewritten alongside the ids themselves.

%w[
  fill stroke clip-path mask filter style
  marker-start marker-mid marker-end
].freeze
COLOR_ATTRS =
%w[fill stroke stop-color].freeze

Instance Method Summary collapse

Constructor Details

#initialize(icon, size: 24) ⇒ Preprocessor

Returns a new instance of Preprocessor.



32
33
34
35
36
# File 'lib/fontico/preprocessor.rb', line 32

def initialize(icon, size: 24)
  @icon = icon
  @size = size
  @warnings = []
end

Instance Method Details

#call(source, width: nil, height: nil) ⇒ Object

source is a whole SVG document (local files) or a body fragment (Iconify, which returns inner markup only).



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fontico/preprocessor.rb', line 40

def call(source, width: nil, height: nil)
  doc = REXML::Document.new(wrap(source, width, height))
  root = doc.root

  vb_w, vb_h, min_x, min_y = viewbox_of(root, width, height)

  strip_comments!(root)
  strip_elements!(root)
  strip_attributes!(root)
  namespace_ids!(root)

  # The manifest stays one line per icon: multicolor is detected, not
  # declared, unless the author overrides it explicitly.
  multicolor = @icon.multicolor.nil? ? distinct_colors(root).size > 1 : @icon.multicolor?
  fold_colors!(root) unless multicolor
  flag_hard_failures!(root)

  Result.new(
    body: refit(inner_markup(root), vb_w, vb_h, min_x, min_y),
    width: @size, height: @size, warnings: @warnings, multicolor: multicolor
  )
end