Class: TTFunk::Table::Cff::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/ttfunk/table/cff/path.rb

Overview

Path. Mostly used for CFF glyph outlines.

Constant Summary collapse

CLOSE_PATH_CMD =

Close path command.

[:close].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePath

Returns a new instance of Path.

Source Code
lib/ttfunk/table/cff/path.rb, line 19
19
def initialize
20
  @commands = []
21
  @number_of_contours = 0
22
end

Instance Attribute Details

#commandsArray (readonly)

Commands in this path.

Returns:

  • (Array)
Source Code
lib/ttfunk/table/cff/path.rb, line 13
13
def commands
14
  @commands
15
end

#number_of_contoursInteger (readonly)

Number of contours in this path.

Returns:

  • (Integer)
Source Code
lib/ttfunk/table/cff/path.rb, line 17
17
def number_of_contours
18
  @number_of_contours
19
end

Instance Method Details

#close_pathvoid

This method returns an undefined value.

Close current contour.

Source Code
lib/ttfunk/table/cff/path.rb, line 60
60
def close_path
61
  @commands << CLOSE_PATH_CMD
62
  @number_of_contours += 1
63
end

#curve_to(x1, y1, x2, y2, x, y) ⇒ void

This method returns an undefined value.

Add a Bézier curve. Current position is the first control point, (x1, y1) is the second, (x2, y2) is the third, and (x, y) is the last control point.

Parameters:

  • x1 (Integer, Float)
  • y1 (Integer, Float)
  • x2 (Integer, Float)
  • y2 (Integer, Float)
  • x (Integer, Float)
  • y (Integer, Float)
Source Code
lib/ttfunk/table/cff/path.rb, line 53
53
def curve_to(x1, y1, x2, y2, x, y) # rubocop: disable Metrics/ParameterLists,Style/CommentedKeyword
54
  @commands << [:curve, x1, y1, x2, y2, x, y]
55
end

#line_to(x, y) ⇒ void

This method returns an undefined value.

Add a line to coordinates.

Parameters:

  • x (Integer, Float)
  • y (Integer, Float)
Source Code
lib/ttfunk/table/cff/path.rb, line 38
38
def line_to(x, y)
39
  @commands << [:line, x, y]
40
end

#move_to(x, y) ⇒ void

This method returns an undefined value.

Move implicit cursor to coordinates.

Parameters:

  • x (Integer, Float)
  • y (Integer, Float)
Source Code
lib/ttfunk/table/cff/path.rb, line 29
29
def move_to(x, y)
30
  @commands << [:move, x, y]
31
end

#render(x: 0, y: 0, font_size: 72, units_per_em: 1000) ⇒ TTFunk::Table::Cff::Path

Reposition and scale path.

Parameters:

  • x (Integer, Float) (defaults to: 0)

    new horizontal position.

  • y (Integer, Float) (defaults to: 0)

    new vertical position.

  • font_size (Integer, Float) (defaults to: 72)

    font size.

  • units_per_em (Integer) (defaults to: 1000)

    units per Em as defined in the font.

Returns:

Source Code
lib/ttfunk/table/cff/path.rb, line 72
72
def render(x: 0, y: 0, font_size: 72, units_per_em: 1000)
73
  new_path = self.class.new
74
  scale = 1.0 / units_per_em * font_size
75
76
  commands.each do |cmd|
77
    case cmd[:type]
78
    when :move
79
      new_path.move_to(x + (cmd[1] * scale), y + (-cmd[2] * scale))
80
    when :line
81
      new_path.line_to(x + (cmd[1] * scale), y + (-cmd[2] * scale))
82
    when :curve
83
      new_path.curve_to(
84
        x + (cmd[1] * scale),
85
        y + (-cmd[2] * scale),
86
        x + (cmd[3] * scale),
87
        y + (-cmd[4] * scale),
88
        x + (cmd[5] * scale),
89
        y + (-cmd[6] * scale),
90
      )
91
    when :close
92
      new_path.close_path
93
    end
94
  end
95
96
  new_path
97
end