class DragonflyHarfbuzz::MarkupSvgService

Attributes

options[RW]
ox_doc[RW]
svg[RW]
text[RW]
unicodes[RW]

Public Class Methods

call(*args) click to toggle source
# File lib/dragonfly_harfbuzz/markup_svg_service.rb, line 12
def self.call(*args)
  new(*args).call
end
new(text, svg, options = {}) click to toggle source
# File lib/dragonfly_harfbuzz/markup_svg_service.rb, line 16
def initialize(text, svg, options = {})
  @text = text
  @svg = svg
  @ox_doc = Ox.parse(svg)
  @options = options
end

Public Instance Methods

call() click to toggle source
# File lib/dragonfly_harfbuzz/markup_svg_service.rb, line 23
def call
  split_paths if split_paths?

  lines.each_with_index do |line, index|
    line_group = ox_doc.locate("svg/g/g[#{index}]").first
    line_group[:line] = line
    line_group[:class] = 'line'

    word_groups = []
    words_in_line(line).each_with_index do |word, word_index|
      next if word =~ /\s+/
      word_groups << marked_up_word(word, word_index, line, line_group)
    end

    line_group.nodes.clear

    word_groups.each { |wg| line_group << wg }
  end

  Ox.dump(ox_doc)
end

Private Instance Methods

lines() click to toggle source
# File lib/dragonfly_harfbuzz/markup_svg_service.rb, line 47
def lines
  text.split(/\n+/)
end
marked_up_characters(characters, word) click to toggle source
# File lib/dragonfly_harfbuzz/markup_svg_service.rb, line 98
def marked_up_characters(characters, word)
  characters.each do |character|
    index = characters.index(character)
    character[:character] = word[index]
    character[:class] = 'character'
  end
end
marked_up_word(word, word_index, line, line_group) click to toggle source
# File lib/dragonfly_harfbuzz/markup_svg_service.rb, line 77
def marked_up_word(word, word_index, line, line_group)
  word_group = Ox::Element.new('g').tap do |prop|
    prop[:word] = word
    prop[:class] = 'word'
  end

  previous_words = words_in_line(line)[0...word_index]
  index_offset = previous_words.join.length

  index_of_first_character = index_offset + 0
  index_of_last_character = index_offset + word.length - 1

  characters = line_group.locate('use')[index_of_first_character..index_of_last_character]

  marked_up_characters(characters, word).each do |char|
    word_group << char
  end

  word_group
end
split_paths() click to toggle source

FIXME: fix issues with negative paths ('O', 'd', etc.)

# File lib/dragonfly_harfbuzz/markup_svg_service.rb, line 60
def split_paths
  symbols = ox_doc.locate('svg/defs/g/symbol')
  symbols.each do |symbol|
    path = symbol.nodes.first
    parsed_path = Savage::Parser.parse(path[:d])
    subpath_elements = []

    parsed_path.subpaths.each do |subpath|
      path_element = Ox::Element.new('path').tap { |prop| prop[:d] = subpath.to_command }
      subpath_elements.push path_element
    end

    symbol.nodes.clear
    subpath_elements.each { |pth| symbol << pth }
  end
end
split_paths?() click to toggle source
# File lib/dragonfly_harfbuzz/markup_svg_service.rb, line 55
def split_paths?
  options.fetch(:split_paths, true)
end
words_in_line(line) click to toggle source
# File lib/dragonfly_harfbuzz/markup_svg_service.rb, line 51
def words_in_line(line)
  line.split(/(\s+)/)
end