Class: Ucode::Glyphs::CellExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/ucode/glyphs/cell_extractor.rb

Overview

Extracts a single character cell from a Code Charts SVG page and returns a normalized standalone SVG containing only that cell’s vector paths.

The cell is identified by codepoint. The extractor asks the Grid for the cell’s anchor position, finds the ‘<use>` element placed at that position, resolves its glyph definition from `<defs>`, and emits a fresh `<svg>` whose viewBox is `0 0 1000 1000` and whose body is the glyph’s ‘<path>` data translated and scaled to fit that viewBox with a small margin.

Vector-only. Never rasterizes, never OCRs. If the cell is empty (no character glyph placed there, e.g. unassigned codepoint or control character), the extractor returns nil.

Instance Method Summary collapse

Constructor Details

#initialize(doc) ⇒ CellExtractor

Returns a new instance of CellExtractor.

Parameters:

  • doc (Nokogiri::XML::Document)

    the rendered Code Charts page



29
30
31
32
# File 'lib/ucode/glyphs/cell_extractor.rb', line 29

def initialize(doc)
  @doc = doc
  @glyph_cache = {}
end

Instance Method Details

#extract(grid, codepoint) ⇒ Nokogiri::XML::Document?

Returns a standalone ‘<svg>` doc with viewBox `0 0 1000 1000`, or nil if the cell is empty.

Parameters:

Returns:

  • (Nokogiri::XML::Document, nil)

    a standalone ‘<svg>` doc with viewBox `0 0 1000 1000`, or nil if the cell is empty.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ucode/glyphs/cell_extractor.rb', line 38

def extract(grid, codepoint)
  anchor = grid.cell_position(codepoint)
  return nil unless anchor

  use_node = find_use_at(anchor, grid)
  return nil unless use_node

  path_data = collect_paths(use_node["xlink:href"] || use_node["href"])
  return nil if path_data.empty?

  bbox = PathBbox.estimate(path_data.join(" "))
  return nil if bbox.empty?

  build_svg(path_data, bbox, use_node["x"].to_f, use_node["y"].to_f)
end