Class: Idml::Render::StyleResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/idml/render/style_resolver.rb

Overview

Extracts styled text from an IDML Story in two granularities:

  1. extract_runs — flat list of StyledRun (character-level attributes only). Useful when paragraph boundaries don't matter (e.g., simple-render fallback).
  2. extract_paragraphs — list of Paragraph structs, each carrying paragraph-level attributes (spacing, indents, leading) plus its runs. Used by the layout engine so the renderer can apply SpaceBefore / FirstLineIndent / etc.

Both methods read the same source (PSR → CSR → Content) so the text content is identical; only the grouping and attribute propagation differ.

Defined Under Namespace

Classes: Paragraph, StyledRun

Constant Summary collapse

DEFAULT_POINT_SIZE =
12.0
ALIGNMENT_MAP =

IDML Justification enum → Justifier symbol. Full justify and binding-side variants defer to :left for now (TODO 80).

{
  "Left" => :left,
  "Center" => :center,
  "Right" => :right,
  "LeftJustified" => :left,
  "RightJustified" => :right,
  "CenterJustified" => :center,
  "FullyJustified" => :justified,
  "ToBinding" => :left,
}.freeze
HANGING_INDENT_WIDTH =
18.0

Class Method Summary collapse

Class Method Details

.concatenate(runs) ⇒ Object

Concatenate runs into a single block. Used when the renderer can't handle per-run styling (e.g., no font metrics available).



495
496
497
# File 'lib/idml/render/style_resolver.rb', line 495

def self.concatenate(runs)
  runs.map(&:text).join
end

.csr_runs(psr, condition_filter: nil, style_lookup: nil, footnote_counter: nil, footnote_option: nil, endnote_counter: nil) ⇒ Object



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/idml/render/style_resolver.rb', line 342

def self.csr_runs(psr, condition_filter: nil, style_lookup: nil,
                  footnote_counter: nil, footnote_option: nil,
                  endnote_counter: nil)
  counter = footnote_counter || Footnote.counter_for(footnote_option)
  endnotes = endnote_counter || Footnote.counter_for(nil)
  psr.character_style_range.flat_map do |csr|
    next [] unless condition_visible?(csr, condition_filter)

    csr_content_runs(csr, condition_filter: condition_filter,
                          style_lookup: style_lookup,
                          footnote_counter: counter,
                          footnote_option: footnote_option,
                          endnote_counter: endnotes)
  end
end

.extract_container_paragraphs(container, condition_filter: nil, style_lookup: nil, footnote_option: nil) ⇒ Object

Extracts paragraphs from any container exposing paragraph_style_range (StoryInner, Elements::Footnote). Footnote-counter state is shared across the whole walk so footnotes number in document order.



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/idml/render/style_resolver.rb', line 185

def self.extract_container_paragraphs(container, condition_filter: nil,
                                      style_lookup: nil,
                                      footnote_option: nil)
  counter = Footnote.counter_for(footnote_option)
  endnotes = Footnote.counter_for(nil)
  container.paragraph_style_range.filter_map do |psr|
    next unless condition_visible?(psr, condition_filter)

    runs = csr_runs(psr, condition_filter: condition_filter,
                         style_lookup: style_lookup,
                         footnote_counter: counter,
                         footnote_option: footnote_option,
                         endnote_counter: endnotes)
    next if runs.empty?

    paragraph = Paragraph.new(
      runs: runs,
      alignment: runs.first.alignment,
      paragraph_direction: resolve_attr(
        style_lookup, psr, :paragraph_direction
      ),
      space_before: resolve_attr(style_lookup, psr, :space_before),
      space_after: resolve_attr(style_lookup, psr, :space_after),
      first_line_indent: resolve_attr(style_lookup, psr,
                                      :first_line_indent),
      left_indent: resolve_attr(style_lookup, psr, :left_indent),
      right_indent: resolve_attr(style_lookup, psr, :right_indent),
      auto_leading: resolve_attr(style_lookup, psr, :auto_leading),
      maximum_word_spacing: resolve_attr(
        style_lookup, psr, :maximum_word_spacing
      ),
      maximum_letter_spacing: resolve_attr(
        style_lookup, psr, :maximum_letter_spacing
      ),
      maximum_glyph_scaling: resolve_attr(
        style_lookup, psr, :maximum_glyph_scaling
      ),
      minimum_glyph_scaling: resolve_attr(
        style_lookup, psr, :minimum_glyph_scaling
      ),
      start_paragraph: resolve_attr(style_lookup, psr,
                                    :start_paragraph),
      keep_all_lines_together: resolve_attr(
        style_lookup, psr, :keep_all_lines_together
      ),
      keep_with_next: resolve_attr(style_lookup, psr,
                                   :keep_with_next),
      keep_first_lines: resolve_attr(style_lookup, psr,
                                     :keep_first_lines),
      keep_last_lines: resolve_attr(style_lookup, psr,
                                    :keep_last_lines),
      drop_cap_lines: resolve_attr(style_lookup, psr, :drop_cap_lines),
      drop_cap_characters: resolve_attr(style_lookup, psr,
                                        :drop_cap_characters),
      bullets_and_numbering_list_type: resolve_attr(
        style_lookup, psr, :bullets_and_numbering_list_type
      ),
      bullet_character_value: resolve_attr(style_lookup, psr,
                                           :bullet_character_value),
      bullets_text_after: resolve_attr(style_lookup, psr,
                                       :bullets_text_after),
      numbering_expression: resolve_attr(style_lookup, psr,
                                         :numbering_expression),
      rule_above: resolve_attr(style_lookup, psr, :rule_above),
      rule_above_line_weight: resolve_attr(style_lookup, psr,
                                           :rule_above_line_weight),
      rule_above_color: properties_color(psr, :rule_above_color) ||
                       resolve_attr(style_lookup, psr, :stroke_color),
      rule_above_tint: resolve_attr(style_lookup, psr,
                                    :rule_above_tint),
      rule_above_offset: resolve_attr(style_lookup, psr,
                                      :rule_above_offset),
      rule_above_left_indent: resolve_attr(style_lookup, psr,
                                           :rule_above_left_indent),
      rule_above_right_indent: resolve_attr(style_lookup, psr,
                                            :rule_above_right_indent),
      rule_above_width: resolve_attr(style_lookup, psr,
                                     :rule_above_width),
      rule_below: resolve_attr(style_lookup, psr, :rule_below),
      rule_below_line_weight: resolve_attr(style_lookup, psr,
                                           :rule_below_line_weight),
      rule_below_color: properties_color(psr, :rule_below_color) ||
                       resolve_attr(style_lookup, psr, :stroke_color),
      rule_below_tint: resolve_attr(style_lookup, psr,
                                    :rule_below_tint),
      rule_below_offset: resolve_attr(style_lookup, psr,
                                      :rule_below_offset),
      rule_below_left_indent: resolve_attr(style_lookup, psr,
                                           :rule_below_left_indent),
      rule_below_right_indent: resolve_attr(style_lookup, psr,
                                            :rule_below_right_indent),
      rule_below_width: resolve_attr(style_lookup, psr,
                                     :rule_below_width),
      paragraph_shading_on: resolve_attr(style_lookup, psr,
                                         :paragraph_shading_on),
      paragraph_shading_color: properties_color(psr,
                                                :paragraph_shading_color),
      paragraph_shading_tint: resolve_attr(style_lookup, psr,
                                           :paragraph_shading_tint),
      paragraph_shading_width: resolve_attr(style_lookup, psr,
                                            :paragraph_shading_width),
      paragraph_shading_left_offset: resolve_attr(
        style_lookup, psr, :paragraph_shading_left_offset
      ),
      paragraph_shading_right_offset: resolve_attr(
        style_lookup, psr, :paragraph_shading_right_offset
      ),
      paragraph_shading_top_offset: resolve_attr(
        style_lookup, psr, :paragraph_shading_top_offset
      ),
      paragraph_shading_bottom_offset: resolve_attr(
        style_lookup, psr, :paragraph_shading_bottom_offset
      ),
      paragraph_border_on: resolve_attr(style_lookup, psr,
                                        :paragraph_border_on),
      paragraph_border_color: properties_color(psr,
                                               :paragraph_border_color),
      paragraph_border_tint: resolve_attr(style_lookup, psr,
                                          :paragraph_border_tint),
      paragraph_border_top_line_weight: resolve_attr(
        style_lookup, psr, :paragraph_border_top_line_weight
      ),
      paragraph_border_left_line_weight: resolve_attr(
        style_lookup, psr, :paragraph_border_left_line_weight
      ),
      paragraph_border_right_line_weight: resolve_attr(
        style_lookup, psr, :paragraph_border_right_line_weight
      ),
      paragraph_border_bottom_line_weight: resolve_attr(
        style_lookup, psr, :paragraph_border_bottom_line_weight
      ),
      paragraph_border_top_offset: resolve_attr(
        style_lookup, psr, :paragraph_border_top_offset
      ),
      paragraph_border_left_offset: resolve_attr(
        style_lookup, psr, :paragraph_border_left_offset
      ),
      paragraph_border_right_offset: resolve_attr(
        style_lookup, psr, :paragraph_border_right_offset
      ),
      paragraph_border_bottom_offset: resolve_attr(
        style_lookup, psr, :paragraph_border_bottom_offset
      ),
    )
    prepend_list_marker(paragraph)
    paragraph
  end
end

.extract_paragraphs(story, condition_filter: nil, style_lookup: nil, footnote_option: nil) ⇒ Object

Returns the paragraph list for a story. Each paragraph groups its runs and carries the paragraph-level attributes from the owning PSR. Empty paragraphs (no runs after filtering) are skipped. Footnotes anchored in the story emit superscript marker runs, numbered sequentially from the FootnoteOption's StartAt (default 1).



170
171
172
173
174
175
176
177
178
179
# File 'lib/idml/render/style_resolver.rb', line 170

def self.extract_paragraphs(story, condition_filter: nil,
                            style_lookup: nil, footnote_option: nil)
  return [] unless story&.inner

  extract_container_paragraphs(
    story.inner,
    condition_filter: condition_filter, style_lookup: style_lookup,
    footnote_option: footnote_option
  )
end

.extract_runs(story) ⇒ Object

Returns the flat run list for a story. Each run carries only character-level attributes. Paragraph boundaries are lost. Footnote markers number sequentially across the story.



154
155
156
157
158
159
160
161
162
# File 'lib/idml/render/style_resolver.rb', line 154

def self.extract_runs(story)
  return [] unless story&.inner

  counter = Footnote.counter_for(nil)
  endnotes = Footnote.counter_for(nil)
  story.inner.paragraph_style_range.flat_map do |psr|
    csr_runs(psr, footnote_counter: counter, endnote_counter: endnotes)
  end
end