Class: Carve::Hexapdf::StyleMap
- Inherits:
-
Object
- Object
- Carve::Hexapdf::StyleMap
- Defined in:
- lib/carve/hexapdf/style_map.rb
Overview
Hierarchical style resolver for renderer properties.
Constant Summary collapse
- DEFAULTS =
deep_freeze({ "base" => { font: "Times" }, "heading" => { margin: [10, 0, 6] }, "heading.1" => { font_size: 22 }, "heading.2" => { font_size: 18 }, "heading.3" => { font_size: 15 }, "heading.4" => { font_size: 13 }, "heading.5" => { font_size: 12 }, "heading.6" => { font_size: 11 }, "paragraph" => { margin: [0, 0, 8] }, "code" => { font: "Courier" }, "code.block" => { font_size: 9, margin: [2, 0, 8], box: { background_color: "f2f2f2", padding: 6 }, }, "code.inline" => {}, "quote" => { box: { margin: [2, 0, 8], padding: [4, 10], background_color: "f7f7f7" }, }, "admonition" => { box: { margin: [2, 0, 8], padding: [6, 10], background_color: "eef3fb" }, title_margin: [0, 0, 4], }, "list" => { item_spacing: 3, content_indentation: 18 }, "definition_list" => { box: { margin: [0, 0, 8] }, definition_indent: 16 }, "table" => { font_size: 10, cell_padding: 4, margin: [2, 0, 8] }, "table.header" => {}, "table.caption" => { font_size: 9, margin: [0, 0, 8] }, "figure.caption" => { font_size: 9, margin: [2, 0, 8], text_align: :center }, # A composite figure (PART 9 section 4c). :column_gap and # :min_column_width are read by the renderer rather than by HexaPDF: # they decide whether a `.columns-N` hint is honored or the panels # stack. "figure.group" => { box: { margin: [2, 0, 8] }, column_gap: 18, min_column_width: 90, }, "figure.group.caption" => { font_size: 9, margin: [4, 0, 0], text_align: :center }, "footnote" => { font_size: 9, margin: [0, 0, 3] }, "link" => { fill_color: "hp-blue" }, "highlight" => { background_color: "fff3a3" }, "image" => { margin: [2, 0, 8] }, "math" => { font_size: 11, margin: [4, 0, 8], box: { padding: 4 } }, "thematic_break" => { height: 2, margin: [8, 0, 8], background_color: "cccccc" }, })
Instance Method Summary collapse
-
#initialize(styles = nil) ⇒ StyleMap
constructor
A new instance of StyleMap.
- #resolve(key) ⇒ Object
- #user_set?(key, property = nil) ⇒ Boolean
- #user_set_in_chain?(key, property) ⇒ Boolean
Constructor Details
#initialize(styles = nil) ⇒ StyleMap
Returns a new instance of StyleMap.
66 67 68 69 |
# File 'lib/carve/hexapdf/style_map.rb', line 66 def initialize(styles = nil) @user = normalize(styles || {}) @memo = {} end |
Instance Method Details
#resolve(key) ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/carve/hexapdf/style_map.rb', line 71 def resolve(key) normalized = normalize_key(key) validate_key!(normalized) @memo[normalized] ||= chain_for(normalized).each_with_object({}) do |part, resolved| merge_entry!(resolved, DEFAULTS[part]) if DEFAULTS.key?(part) merge_entry!(resolved, @user[part]) if @user.key?(part) end.freeze end |
#user_set?(key, property = nil) ⇒ Boolean
80 81 82 83 84 85 86 |
# File 'lib/carve/hexapdf/style_map.rb', line 80 def user_set?(key, property = nil) normalized = normalize_key(key) return false unless @user.key?(normalized) return true if property.nil? @user[normalized].key?(property) end |
#user_set_in_chain?(key, property) ⇒ Boolean
88 89 90 |
# File 'lib/carve/hexapdf/style_map.rb', line 88 def user_set_in_chain?(key, property) chain_for(normalize_key(key)).any? { |part| user_set?(part, property) } end |