Class: Fontisan::SvgToGlyf::Geometry::Normalizer

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

Overview

Computes the affine transform that maps SVG coordinate space (Y-down, arbitrary origin) into font coordinate space (Y-up, origin at the bottom-left of the em-square, scaled to UPM).

The normalization accepts a Ufo::Bounds describing the SVG coordinate extent to map into the em-square. That bounds may be the SVG viewBox, the actual content extents, or the union of both — Assembler decides which.

The transform is:

1. Translate the source origin (min_x, min_y) to (0, 0).
2. Scale uniformly to font units: (upm/w, upm/h).
3. Flip Y across the half-height of the scaled space.
4. Translate up by upm so the top of the source maps to the
 top of the em-square.

The resulting matrix is composed with the SVG document's accumulated group transform to produce the final per-point transform.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bounds:, upm:) ⇒ Normalizer

Returns a new instance of Normalizer.

Parameters:

  • bounds (Ufo::Bounds)

    source coordinate extents

  • upm (Integer, Float)

    font units-per-em



31
32
33
34
# File 'lib/fontisan/svg_to_glyf/geometry/normalizer.rb', line 31

def initialize(bounds:, upm:)
  @bounds = bounds
  @upm = upm.to_f
end

Instance Attribute Details

#boundsObject (readonly)

Returns the value of attribute bounds.



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

def bounds
  @bounds
end

#upmObject (readonly)

Returns the value of attribute upm.



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

def upm
  @upm
end

Instance Method Details

#final_transform(group_transform = AffineTransform.identity) ⇒ AffineTransform

Compose the normalization with an SVG group transform, producing the final per-point transform.

Parameters:

  • group_transform (AffineTransform) (defaults to: AffineTransform.identity)

    accumulated transforms

Returns:



56
57
58
# File 'lib/fontisan/svg_to_glyf/geometry/normalizer.rb', line 56

def final_transform(group_transform = AffineTransform.identity)
  matrix.compose(group_transform)
end

#matrixAffineTransform

Returns the source→font normalization.

Returns:



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fontisan/svg_to_glyf/geometry/normalizer.rb', line 37

def matrix
  w = @bounds.width
  h = @bounds.height
  return AffineTransform.identity if w.zero? || h.zero?

  sx = @upm / w
  sy = @upm / h
  # x' = sx * (x - min_x)
  # y' = -sy * (y - min_y) + upm
  AffineTransform.new(sx, 0, 0, -sy,
                      -sx * @bounds.min_x,
                      sy * @bounds.min_y + @upm)
end