Module: Plurimath::Formatter::Numbers::OmmlRenderer

Defined in:
lib/plurimath/formatter/numbers/omml_renderer.rb

Overview

Renders a Formatter result (FormattedNotation, FormattedNumber, or String) into an OMML element tree. Number delegates to this so the model does not carry format-specific XML construction.

Class Method Summary collapse

Class Method Details

.plain_element(result) ⇒ Object



63
64
65
# File 'lib/plurimath/formatter/numbers/omml_renderer.rb', line 63

def plain_element(result)
  Utility.ox_element("t", namespace: "m") << result.to_s
end

.render(result) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/plurimath/formatter/numbers/omml_renderer.rb', line 12

def render(result)
  case result
  when FormattedNotation then render_notation(result)
  when FormattedNumber
    result.base_notation.semantic? ? render_semantic_base(result) : plain_element(result)
  else
    plain_element(result)
  end
end

.render_notation(notation) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/plurimath/formatter/numbers/omml_renderer.rb', line 22

def render_notation(notation)
  return plain_element(notation) if notation.notation_style == :e

  sup_struct = Utility.ox_element("sSup", namespace: "m")
  subpr = Utility.ox_element("sSupPr", namespace: "m")
  subpr << Utility.pr_element("ctrl", true, namespace: "m")

  coeff_run = text_run(notation.coefficient.to_s)
  times_run = text_run(" #{notation.times_symbol} ")
  base_run = text_run("10")
  exp_run = text_run(notation.formatted_exponent)

  e_el = Utility.ox_element("e", namespace: "m")
  Utility.update_nodes(e_el, [coeff_run, times_run, base_run])

  sup_el = Utility.ox_element("sup", namespace: "m")
  Utility.update_nodes(sup_el, [exp_run])

  Utility.update_nodes(sup_struct, [subpr, e_el, sup_el])
  sup_struct
end

.render_semantic_base(formatted) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/plurimath/formatter/numbers/omml_renderer.rb', line 44

def render_semantic_base(formatted)
  sub_struct = Utility.ox_element("sSub", namespace: "m")
  subpr = Utility.ox_element("sSubPr", namespace: "m")
  subpr << Utility.pr_element("ctrl", true, namespace: "m")

  digits_with_sign = "#{formatted.sign_text}#{formatted.digits_string}"
  base_run = text_run(digits_with_sign)
  sub_run = text_run(formatted.base_notation.base.to_s)

  e_el = Utility.ox_element("e", namespace: "m")
  Utility.update_nodes(e_el, [base_run])

  sub_el = Utility.ox_element("sub", namespace: "m")
  Utility.update_nodes(sub_el, [sub_run])

  Utility.update_nodes(sub_struct, [subpr, e_el, sub_el])
  sub_struct
end

.text_run(text) ⇒ Object



67
68
69
70
# File 'lib/plurimath/formatter/numbers/omml_renderer.rb', line 67

def text_run(text)
  run = Utility.ox_element("r", namespace: "m")
  run << (Utility.ox_element("t", namespace: "m") << text)
end