Class: Ucode::Glyphs::PdfFetcher

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

Overview

Resolves a Unicode block to its source PDF on disk.

Primary source: the per-block PDF cached at ‘<cache>/<version>/pdfs/U<XXXX>.pdf` (downloaded from `unicode.org/charts/PDF/` by `Ucode::Fetch::CodeCharts`).

Fallback: slice the page range from the monolith ‘CodeCharts.pdf`. The page range is resolved by `MonolithPageMap` from the PDF’s bookmark outline, cached under ‘data/codecharts_page_map.json`.

Instance Method Summary collapse

Constructor Details

#initialize(version, monolith_path: nil, blocks: [], page_map_cache: nil) ⇒ PdfFetcher

Returns a new instance of PdfFetcher.

Parameters:

  • version (String)

    UCD version, used as the cache namespace.

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

    path to the full ‘CodeCharts.pdf`. Pass nil to disable monolith fallback.

  • blocks (Array<Ucode::Models::Block>) (defaults to: [])

    required for monolith fallback — used to match bookmark titles to block first-cps.

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

    where to read/write the monolith page-map JSON cache.



29
30
31
32
33
34
# File 'lib/ucode/glyphs/pdf_fetcher.rb', line 29

def initialize(version, monolith_path: nil, blocks: [], page_map_cache: nil)
  @version = version
  @monolith_path = monolith_path && Pathname.new(monolith_path)
  @blocks = blocks
  @page_map_cache = page_map_cache
end

Instance Method Details

#fetch(block_first_cp:, force: false) ⇒ Pathname?

Resolve the per-block PDF for ‘block_first_cp`, fetching from the network if missing. Returns the local PDF path, or nil if the block’s PDF is unavailable (network failure + no monolith, or monolith lacks the requested block).

Parameters:

  • block_first_cp (Integer)

    first codepoint of the block; also the PDF’s URL slug per unicode.org’s naming convention.

  • force (Boolean) (defaults to: false)

    re-download even if cached.

Returns:

  • (Pathname, nil)


45
46
47
48
49
50
51
52
53
# File 'lib/ucode/glyphs/pdf_fetcher.rb', line 45

def fetch(block_first_cp:, force: false)
  path = per_block_path(block_first_cp)
  return path if path.exist? && !force

  download(block_first_cp)
  return path if path.exist?

  slice_from_monolith(block_first_cp)
end