Module: Ucode::Glyphs::PathBbox

Defined in:
lib/ucode/glyphs/path_bbox.rb

Overview

Estimates the axis-aligned bounding box of an SVG ‘<path>` `d` attribute by scanning every numeric coordinate pair in the path data. This is a conservative over-estimate: control points and implicit vertices are included, so the true curve bbox is always contained within the estimate. For grid detection and cell membership tests, the over-estimate is sufficient and avoids the cost of a Bezier solver.

Only absolute coordinates are returned. Relative commands (lowercase ‘m`, `l`, `c`, …) are NOT supported — Code Charts SVGs from every supported renderer (pdftocairo, pdf2svg, dvisvgm, mutool) emit absolute commands. If relative commands appear, parse them via a proper SVG path parser before calling this.

Defined Under Namespace

Classes: Result

Constant Summary collapse

NUMBER =
/-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?/.freeze

Class Method Summary collapse

Class Method Details

.estimate(path_d) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ucode/glyphs/path_bbox.rb', line 40

def estimate(path_d)
  return Result.new if path_d.nil? || path_d.empty?

  numbers = path_d.scan(NUMBER).map(&:to_f)
  return Result.new if numbers.empty?

  xs = []
  ys = []
  numbers.each_slice(2) do |x, y|
    xs << x
    ys << y
  end
  Result.new(
    min_x: xs.min,
    min_y: ys.min,
    max_x: xs.max,
    max_y: ys.max,
  )
end