Class: Fontisan::SvgToGlyf::Assembler

Inherits:
Object
  • Object
show all
Defined in:
lib/fontisan/svg_to_glyf/assembler.rb

Overview

Orchestrates the full SVG → Ufo::Glyph pipeline:

path data + transforms
 Path::Parser.parse  [Command]
 Path::ContourBuilder.build  [Ufo::Contour]
 measure content bounds (Ufo::Bounds)
 union content bounds with the SVG viewBox bounds
 Normalizer maps the unioned space into the em-square
 compose with the accumulated group transform
 round to Integer
 Ufo::Glyph

For SVG files and directories, the Document class extracts the viewBox, accumulated transforms, and path data; the Assembler composes the normalizer with the group transform and runs the pipeline once per path.

Constant Summary collapse

DEFAULT_VIEWBOX =
Ufo::Bounds.new(min_x: 0, min_y: 0, max_x: DEFAULT_UPM, max_y: DEFAULT_UPM)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(upm: SvgToGlyf::DEFAULT_UPM) ⇒ Assembler

Returns a new instance of Assembler.

Parameters:

  • upm (Integer) (defaults to: SvgToGlyf::DEFAULT_UPM)

    font units-per-em



27
28
29
# File 'lib/fontisan/svg_to_glyf/assembler.rb', line 27

def initialize(upm: SvgToGlyf::DEFAULT_UPM)
  @upm = upm.to_i
end

Instance Attribute Details

#upmObject (readonly)

Returns the value of attribute upm.



24
25
26
# File 'lib/fontisan/svg_to_glyf/assembler.rb', line 24

def upm
  @upm
end

Instance Method Details

#build_from_directory(dir) ⇒ Fontisan::Ufo::Font

Build a font from a directory of SVG files.

Parameters:

  • dir (String)

Returns:



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fontisan/svg_to_glyf/assembler.rb', line 72

def build_from_directory(dir)
  font = Fontisan::Ufo::Font.new
  font.info.units_per_em = @upm

  Dir.glob(File.join(dir, "*.svg")).each do |path|
    glyph = build_from_file(path)
    font.glyphs[glyph.name] = glyph
  end

  font
end

#build_from_file(file_path, codepoint: nil) ⇒ Fontisan::Ufo::Glyph

Build a glyph from an SVG file.

Parameters:

  • file_path (String)
  • codepoint (Integer, nil) (defaults to: nil)

    override; otherwise derived from filename

Returns:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fontisan/svg_to_glyf/assembler.rb', line 53

def build_from_file(file_path, codepoint: nil)
  doc = Document.from_file(file_path)
  codepoint ||= codepoint_from_filename(File.basename(file_path))

  doc.each_path.with_object(nil) do |(data, transform), _|
    return assemble(path_data: data,
                    viewbox: doc.viewbox || default_viewbox,
                    group_transform: transform,
                    codepoint: codepoint,
                    name: nil)
  end

  empty_glyph(codepoint)
end

#build_from_path_data(path_data, codepoint: nil, name: nil, viewbox: nil, transform: nil) ⇒ Fontisan::Ufo::Glyph

Build a glyph directly from a path data string.

Parameters:

  • path_data (String)

    SVG path d= attribute

  • codepoint (Integer, nil) (defaults to: nil)

    Unicode codepoint

  • name (String, nil) (defaults to: nil)

    glyph name

  • viewbox (Ufo::Bounds, Hash{Symbol=>Float}, nil) (defaults to: nil)

    SVG viewbox

  • transform (Geometry::AffineTransform, nil) (defaults to: nil)

    group transform

Returns:



39
40
41
42
43
44
45
46
# File 'lib/fontisan/svg_to_glyf/assembler.rb', line 39

def build_from_path_data(path_data, codepoint: nil, name: nil,
                         viewbox: nil, transform: nil)
  assemble(path_data: path_data,
           viewbox: resolve_viewbox(viewbox),
           group_transform: transform || Geometry::AffineTransform.identity,
           codepoint: codepoint,
           name: name)
end