Class: Ucode::Glyphs::EmbeddedFonts::Writer

Inherits:
Object
  • Object
show all
Includes:
Repo::AtomicWrites
Defined in:
lib/ucode/glyphs/embedded_fonts/writer.rb

Overview

Writes one glyph.svg per codepoint in codepoints, sourcing the outline from the Code Charts PDF's embedded font program.

The Catalog and Renderer are shared across the loop so the expensive PDF walk + ToUnicode parse + fontisan load happen once per process. Each FontEntry memoizes its own fontisan accessor; in long CJK runs you may want to call entry.reset_accessor! periodically (the Writer doesn't).

Idempotent and atomic via Repo::AtomicWrites — same protocol as the LastResort and v0.1 cell-extractor writers.

Instance Method Summary collapse

Methods included from Repo::AtomicWrites

#same_content?, #to_pretty_json, #write_atomic

Constructor Details

#initialize(output_root:, catalog:) ⇒ Writer

Returns a new instance of Writer.

Parameters:

  • output_root (String, Pathname)
  • catalog (Catalog)


28
29
30
31
32
# File 'lib/ucode/glyphs/embedded_fonts/writer.rb', line 28

def initialize(output_root:, catalog:)
  @output_root = Pathname.new(output_root)
  @catalog = catalog
  @renderer = Renderer.new(catalog)
end

Instance Method Details

#write_many(codepoints = nil, block_lookup:) ⇒ Hash

Write glyph.svg for every codepoint covered by the PDF.

Parameters:

  • codepoints (Array<Integer>, Enumerable<Integer>) (defaults to: nil)

    which codepoints to render. Defaults to all codepoints the Catalog has fonts for.

  • block_lookup (Proc, #call)

    codepoint → block id string (e.g. "Basic_Latin"). Returns nil for codepoints without a block; those are skipped.

Returns:

  • (Hash)

    tally { written:, skipped:, missing:, total: }



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ucode/glyphs/embedded_fonts/writer.rb', line 43

def write_many(codepoints = nil, block_lookup:)
  cps = codepoints || @catalog.codepoints
  tally = { written: 0, skipped: 0, missing: 0, total: 0 }
  cps.each do |cp|
    tally[:total] += 1
    block_id = block_lookup.call(cp)
    if block_id.nil?
      tally[:missing] += 1
      next
    end

    result = @renderer.render(cp)
    if result.nil? || !result.ok?
      tally[:missing] += 1
      next
    end

    written = write_glyph(block_id, cp, result.svg)
    tally[written ? :written : :skipped] += 1
  end
  tally
end