Method: Asciidoctor::PDF::FormattedText::Transform#initialize

Defined in:
lib/asciidoctor/pdf/formatted_text/transform.rb

#initialize(options = {}) ⇒ Transform

Returns a new instance of Transform.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/asciidoctor/pdf/formatted_text/transform.rb', line 29

def initialize options = {}
  @merge_adjacent_text_nodes = options[:merge_adjacent_text_nodes]
  # TODO: add support for character spacing
  if (theme = options[:theme])
    @theme_settings = {
      button: {
        color: theme.button_font_color,
        font: theme.button_font_family,
        size: theme.button_font_size,
        styles: (to_styles theme.button_font_style),
        background_color: (button_bg_color = theme.button_background_color),
        border_width: (button_border_width = theme.button_border_width),
        border_color: button_border_width && (theme.button_border_color || theme.base_border_color),
        border_offset: (button_border_offset = (button_bg_or_border = button_bg_color || button_border_width) && theme.button_border_offset),
        border_radius: button_bg_or_border && theme.button_border_radius,
        align: button_border_offset && :center,
        callback: button_bg_or_border && [TextBackgroundAndBorderRenderer],
      }.compact,
      code: {
        color: theme.codespan_font_color,
        font: theme.codespan_font_family,
        size: theme.codespan_font_size,
        styles: (to_styles theme.codespan_font_style),
        background_color: (mono_bg_color = theme.codespan_background_color),
        border_width: (mono_border_width = theme.codespan_border_width),
        border_color: mono_border_width && (theme.codespan_border_color || theme.base_border_color),
        border_offset: (mono_border_offset = (mono_bg_or_border = mono_bg_color || mono_border_width) && theme.codespan_border_offset),
        border_radius: mono_bg_or_border && theme.codespan_border_radius,
        align: mono_border_offset && :center,
        callback: mono_bg_or_border && [TextBackgroundAndBorderRenderer],
      }.compact,
      kbd: {
        color: theme.kbd_font_color,
        font: theme.kbd_font_family || theme.codespan_font_family,
        size: theme.kbd_font_size,
        styles: (to_styles theme.kbd_font_style),
        background_color: (kbd_bg_color = theme.kbd_background_color),
        border_width: (kbd_border_width = theme.kbd_border_width),
        border_color: kbd_border_width && (theme.kbd_border_color || theme.base_border_color),
        border_offset: (kbd_border_offset = (kbd_bg_or_border = kbd_bg_color || kbd_border_width) && theme.kbd_border_offset),
        border_radius: kbd_bg_or_border && theme.kbd_border_radius,
        align: kbd_border_offset && :center,
        callback: kbd_bg_or_border && [TextBackgroundAndBorderRenderer],
      }.compact,
      link: {
        color: theme.link_font_color,
        font: theme.link_font_family,
        size: theme.link_font_size,
        styles: (to_styles theme.link_font_style, theme.link_text_decoration),
        text_decoration_color: theme.link_text_decoration_color,
        text_decoration_width: theme.link_text_decoration_width,
        background_color: (link_bg_color = theme.link_background_color),
        border_offset: (link_border_offset = link_bg_color && theme.link_border_offset),
        align: link_border_offset && :center,
        callback: link_bg_color && [TextBackgroundAndBorderRenderer],
      }.compact,
      mark: {
        color: theme.mark_font_color,
        styles: (to_styles theme.mark_font_style),
        background_color: (mark_bg_color = theme.mark_background_color),
        border_offset: (mark_border_offset = mark_bg_color && theme.mark_border_offset),
        align: mark_border_offset && :center,
        callback: mark_bg_color && [TextBackgroundAndBorderRenderer],
      }.compact,
      menu: {
        color: theme.menu_font_color,
        font: theme.menu_font_family,
        size: theme.menu_font_size,
        styles: (to_styles theme.menu_font_style),
      }.compact,
    }
    @theme_settings.tap do |accum|
      roles_with_styles = [].to_set
      theme.each_pair do |key, val|
        next unless (key = key.to_s).start_with? 'role_'
        role, key = (key.slice 5, key.length).split '_', 2
        if (prop = ThemeKeyToFragmentProperty[key])
          (accum[role] ||= {})[prop] = val
          if key == 'border_width' && val && !(theme[%(role_#{role}_border_color)])
            accum[role][:border_color] = theme.base_border_color
          end
        #elsif key == 'font_kerning'
        #  unless (resolved_val = val == 'none' ? false : (val == 'normal' ? true : nil)).nil?
        #    (accum[role] ||= {})[:kerning] = resolved_val
        #  end
        elsif key == 'font_style' || key == 'text_decoration'
          roles_with_styles << role
        end
      end
      roles_with_styles.each do |role|
        (accum[role] ||= {})[:styles] = to_styles theme[%(role_#{role}_font_style)], theme[%(role_#{role}_text_decoration)]
      end
    end
    @theme_settings['line-through'] = { styles: [:strikethrough].to_set } unless @theme_settings.key? 'line-through'
    @theme_settings['underline'] = { styles: [:underline].to_set } unless @theme_settings.key? 'underline'
    unless @theme_settings.key? 'big'
      if (base_font_size_large = theme.base_font_size_large)
        @theme_settings['big'] = { size: %(#{(base_font_size_large / theme.base_font_size.to_f).round 5}em) }
      else
        @theme_settings['big'] = { size: '1.1667em' }
      end
    end
    unless @theme_settings.key? 'small'
      if (base_font_size_small = theme.base_font_size_small)
        @theme_settings['small'] = { size: %(#{(base_font_size_small / theme.base_font_size.to_f).round 5}em) }
      else
        @theme_settings['small'] = { size: '0.8333em' }
      end
    end
  else
    @theme_settings = {
      button: { font: 'Courier', styles: [:bold].to_set },
      code: { font: 'Courier' },
      kbd: { font: 'Courier', styles: [:italic].to_set },
      link: { color: '0000FF' },
      mark: { background_color: 'FFFF00', callback: [TextBackgroundAndBorderRenderer] },
      menu: { styles: [:bold].to_set },
      'line-through' => { styles: [:strikethrough].to_set },
      'underline' => { styles: [:underline].to_set },
      'big' => { size: '1.667em' },
      'small' => { size: '0.8333em' },
    }
  end
end