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]
 apply final transform (normalization · 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.

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



22
23
24
# File 'lib/fontisan/svg_to_glyf/assembler.rb', line 22

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

Instance Attribute Details

#upmObject (readonly)

Returns the value of attribute upm.



19
20
21
# File 'lib/fontisan/svg_to_glyf/assembler.rb', line 19

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:



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/fontisan/svg_to_glyf/assembler.rb', line 62

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:



47
48
49
50
51
52
53
54
55
56
# File 'lib/fontisan/svg_to_glyf/assembler.rb', line 47

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 build_from_doc_path(data, transform, doc, codepoint)
  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 (Hash{Symbol=>Float}, nil) (defaults to: nil)

    :width, :height

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

    group transform

Returns:



34
35
36
37
38
39
40
# File 'lib/fontisan/svg_to_glyf/assembler.rb', line 34

def build_from_path_data(path_data, codepoint: nil, name: nil,
                         viewbox: nil, transform: nil)
  viewbox ||= { width: @upm, height: @upm }
  final = normalizer_for(**viewbox).final_transform(transform || Geometry::AffineTransform.identity)
  contours = build_contours(path_data, final)
  assemble_glyph(name || glyph_name_for(codepoint), contours, codepoint)
end