class TTFunk::Table::Cff::Path

Constants

CLOSE_PATH_CMD

Attributes

commands[R]
number_of_contours[R]

Public Class Methods

new() click to toggle source
# File lib/ttfunk/table/cff/path.rb, line 11
def initialize
  @commands = []
  @number_of_contours = 0
end

Public Instance Methods

close_path() click to toggle source
# File lib/ttfunk/table/cff/path.rb, line 28
def close_path
  @commands << CLOSE_PATH_CMD
  @number_of_contours += 1
end
curve_to(x1, y1, x2, y2, x, y) click to toggle source
# File lib/ttfunk/table/cff/path.rb, line 24
def curve_to(x1, y1, x2, y2, x, y) # rubocop: disable Metrics/ParameterLists,Style/CommentedKeyword
  @commands << [:curve, x1, y1, x2, y2, x, y]
end
line_to(x, y) click to toggle source
# File lib/ttfunk/table/cff/path.rb, line 20
def line_to(x, y)
  @commands << [:line, x, y]
end
move_to(x, y) click to toggle source
# File lib/ttfunk/table/cff/path.rb, line 16
def move_to(x, y)
  @commands << [:move, x, y]
end
render(x: 0, y: 0, font_size: 72, units_per_em: 1000) click to toggle source
# File lib/ttfunk/table/cff/path.rb, line 33
def render(x: 0, y: 0, font_size: 72, units_per_em: 1000)
  new_path = self.class.new
  scale = 1.0 / units_per_em * font_size

  commands.each do |cmd|
    case cmd[:type]
    when :move
      new_path.move_to(x + (cmd[1] * scale), y + (-cmd[2] * scale))
    when :line
      new_path.line_to(x + (cmd[1] * scale), y + (-cmd[2] * scale))
    when :curve
      new_path.curve_to(
        x + (cmd[1] * scale),
        y + (-cmd[2] * scale),
        x + (cmd[3] * scale),
        y + (-cmd[4] * scale),
        x + (cmd[5] * scale),
        y + (-cmd[6] * scale)
      )
    when :close
      new_path.close_path
    end
  end

  new_path
end

Private Instance Methods

format_values(command) click to toggle source
# File lib/ttfunk/table/cff/path.rb, line 62
def format_values(command)
  command[1..-1].map { |k| format('%.2f', k) }.join(' ')
end