Class: Ucode::Glyphs::EmbeddedFonts::Source

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

Overview

Locates the Code Charts PDF on disk and the directory where extracted font streams are cached.

PDF resolution order (first match wins):

1. Explicit `pdf:` argument.
2. `UCODE_CODE_CHARTS_PDF` environment variable.
3. Conventional `<gem_root>/CodeCharts.pdf`.

Per-block PDFs (preferred for incremental runs) can be supplied via the ‘pdf:` argument by the caller — typically the CLI.

Cache resolution order:

1. Explicit `cache_dir:` argument.
2. `UCODE_PDF_FONT_CACHE` environment variable.
3. Conventional `<gem_root>/data/pdf-fonts/`.

The cache holds one file per embedded font program, named after the BaseFont (e.g. ‘CIAIIP+Uni2000Generalpunctuation.ttf`). Re-runs skip extraction when the cached file is newer than the PDF.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pdf: nil, cache_dir: nil, env: ENV, gem_root: nil) ⇒ Source

Returns a new instance of Source.

Parameters:

  • pdf (String, Pathname, nil) (defaults to: nil)

    path to a Code Charts PDF

  • cache_dir (String, Pathname, nil) (defaults to: nil)

    directory for cached font files; created on demand

  • env (Hash{String=>String}) (defaults to: ENV)

    env var source (defaults to ENV)

  • gem_root (String, Pathname, nil) (defaults to: nil)

    gem root for the conventional fallback; injectable for tests

Raises:



42
43
44
45
46
47
48
49
# File 'lib/ucode/glyphs/embedded_fonts/source.rb', line 42

def initialize(pdf: nil, cache_dir: nil, env: ENV, gem_root: nil)
  @pdf_path = resolve_pdf(pdf, env, gem_root)
  raise Ucode::EmbeddedFontsMissingError,
        "Code Charts PDF not found at #{@pdf_path}" unless @pdf_path&.exist?

  @cache_dir = resolve_cache(cache_dir, env, gem_root)
  @cache_dir.mkpath unless @cache_dir.exist?
end

Instance Attribute Details

#cache_dirObject (readonly)

Returns the value of attribute cache_dir.



33
34
35
# File 'lib/ucode/glyphs/embedded_fonts/source.rb', line 33

def cache_dir
  @cache_dir
end

#pdf_pathObject (readonly)

Returns the value of attribute pdf_path.



33
34
35
# File 'lib/ucode/glyphs/embedded_fonts/source.rb', line 33

def pdf_path
  @pdf_path
end

Instance Method Details

#font_cache_path(base_font, extension) ⇒ Pathname

Returns cache path for the named font.

Parameters:

  • base_font (String)

    e.g. “CIAIIP+Uni2000Generalpunctuation”

  • extension (String)

    e.g. “.ttf” or “.cff”

Returns:

  • (Pathname)

    cache path for the named font



60
61
62
# File 'lib/ucode/glyphs/embedded_fonts/source.rb', line 60

def font_cache_path(base_font, extension)
  @cache_dir.join("#{base_font}#{extension}")
end

#pdf_to_sString

Returns absolute path to the PDF, suitable for shelling out to ‘mutool`.

Returns:

  • (String)

    absolute path to the PDF, suitable for shelling out to ‘mutool`



53
54
55
# File 'lib/ucode/glyphs/embedded_fonts/source.rb', line 53

def pdf_to_s
  @pdf_path.to_s
end