Class: Typstify::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/typstify/config.rb

Overview

Everything tunable, set once in config/initializers/typstify.rb.

Typstify.configure do |c|
c.font_paths   = [Rails.root.join("app/assets/fonts")]
c.pdf_standard = :ua_1
end

Constant Summary collapse

PDF_STANDARDS =

Symbol names for the PDF standards Typst can emit, mapped to the strings the compiler wants. Verified against typst_pdf::PdfStandard.

{
  pdf_1_7: "1.7",
  pdf_2_0: "2.0",
  a_1b: "a-1b",
  a_2b: "a-2b",
  a_3b: "a-3b",
  a_4: "a-4",
  ua_1: "ua-1"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#font_pathsObject

Directories searched for fonts, in addition to system fonts. The gem's own bundled faces are always appended, so starter templates work with no configuration at all.



42
43
44
# File 'lib/typstify/config.rb', line 42

def font_paths
  Array(@font_paths).map { |p| Pathname.new(p) } + [Typstify.bundled_font_path]
end

#ignore_system_fontsObject

Skip the operating system's font directories entirely and use only font_paths plus Typst's embedded faces.

Worth knowing: scanning system fonts costs roughly 50 ms per render. On a styled A4 invoice on an M-series Mac, 58 ms becomes 4 ms with this on. Once you have vendored the fonts your templates name — which you should; see docs/fonts-and-docker.md — nothing on the system is being used anyway, and a slim container has no system fonts to find at all.

Off by default, because turning it on changes which face a template that names "Helvetica" ends up with.



63
64
65
# File 'lib/typstify/config.rb', line 63

def ignore_system_fonts
  @ignore_system_fonts.nil? ? false : @ignore_system_fonts
end

#on_warningObject

Called as on_warning.call(warnings, template) — warnings is an Array of strings. Defaults to Rails.logger.warn.



95
96
97
98
99
100
101
102
103
104
# File 'lib/typstify/config.rb', line 95

def on_warning
  @on_warning ||= lambda do |warnings, template|
    message = "[typstify] #{template}: #{warnings.join("; ")}"
    if defined?(::Rails) && ::Rails.logger
      ::Rails.logger.warn(message)
    else
      warn(message)
    end
  end
end

#package_cacheObject

Directory holding vendored Typst Universe packages, for network-free builds. See docs/fonts-and-docker.md for the platform caveat.



48
49
50
# File 'lib/typstify/config.rb', line 48

def package_cache
  @package_cache && Pathname.new(@package_cache)
end

#pdf_standardObject

nil (plain PDF), or one of PDF_STANDARDS' keys.



68
69
70
# File 'lib/typstify/config.rb', line 68

def pdf_standard
  @pdf_standard
end

#shared_dirObject

Subdirectory of template_root copied into every workspace, so #import "shared/branding.typ" resolves from any template.



35
36
37
# File 'lib/typstify/config.rb', line 35

def shared_dir
  @shared_dir ||= "shared"
end

#strict_fontsObject

Raise FontMissingError instead of warning when a template asks for a font nothing can supply. On in development and test, off in production, where a substituted glyph beats a 500.



87
88
89
90
91
# File 'lib/typstify/config.rb', line 87

def strict_fonts
  return @strict_fonts unless @strict_fonts.nil?

  @strict_fonts = !defined?(::Rails) || ::Rails.env.nil? || ::Rails.env.development? || ::Rails.env.test?
end

#template_rootObject

Where .typ templates live. Also the boundary a template path may not escape (see Resolver).



28
29
30
31
# File 'lib/typstify/config.rb', line 28

def template_root
  @template_root ||= defined?(::Rails) && ::Rails.root ? ::Rails.root.join("app/views") : Pathname.pwd
  Pathname.new(@template_root)
end

Instance Method Details

#pdf_standardsObject

The compiler flag string, or nil.



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/typstify/config.rb', line 71

def pdf_standards
  return [] if pdf_standard.nil?

  key = pdf_standard.to_sym
  value = PDF_STANDARDS[key]
  unless value
    raise ArgumentError,
          "Unknown pdf_standard #{pdf_standard.inspect}. " \
          "Expected one of: #{PDF_STANDARDS.keys.map(&:inspect).join(", ")}"
  end
  [value]
end