Class: PPTX::Shapes::Textbox

Inherits:
Shape
  • Object
show all
Defined in:
lib/pptx/shapes/textbox.rb

Direct Known Subclasses

SlideNumber

Instance Method Summary collapse

Methods inherited from Shape

#base_node, #build_solid_fill, #set_shape_properties

Constructor Details

#initialize(transform, content, formatting = {}) ⇒ Textbox

Returns a new instance of Textbox.



4
5
6
7
8
# File 'lib/pptx/shapes/textbox.rb', line 4

def initialize(transform, content, formatting={})
  super(transform)
  @content = content
  @formatting = formatting
end

Instance Method Details

#base_xmlObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pptx/shapes/textbox.rb', line 10

def base_xml
  # TODO replace cNvPr descr, id and name
  """
    <p:sp xmlns:a='http://schemas.openxmlformats.org/drawingml/2006/main'
          xmlns:p='http://schemas.openxmlformats.org/presentationml/2006/main'>
        <p:nvSpPr>
            <p:cNvPr id='2' name='TextBox'/>
            <p:cNvSpPr txBox='1'/>
            <p:nvPr/>
        </p:nvSpPr>
        <p:spPr>
        </p:spPr>
        <p:txBody>
            <a:bodyPr rtlCol='0' wrap='square' vertOverflow='clip'>
                <a:spAutoFit/>
            </a:bodyPr>
            <a:lstStyle/>
        </p:txBody>
    </p:sp>
  """
end

#build_nodeObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pptx/shapes/textbox.rb', line 32

def build_node
  txbody = base_node.xpath('./p:sp/p:txBody', p:Presentation::NS).first

  @content.split("\n").each do |line|
    paragraph = build_paragraph line
    set_formatting(paragraph, @formatting)
    txbody.add_child paragraph
  end

  # txBody always needs a paragraph
  unless txbody.xpath('./a:p', a: DRAWING_NS).size >= 1
    txbody.add_child empty_paragraph
  end

  base_node
end

#build_paragraph(line) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/pptx/shapes/textbox.rb', line 49

def build_paragraph(line)
  paragraph_xml = """
    <a:p xmlns:a='http://schemas.openxmlformats.org/drawingml/2006/main'>
        <a:pPr/>
        <a:r>
            <a:rPr lang='en-US' smtClean='0'/>
            <a:t></a:t>
        </a:r>
        <a:endParaRPr lang='en-US'/>
    </a:p>
  """

  Nokogiri::XML::DocumentFragment.parse(paragraph_xml).tap do |node|
    tn = node.xpath('./a:p/a:r/a:t', a: DRAWING_NS).first
    tn.content = line
  end
end

#empty_paragraphObject



67
68
69
70
71
72
# File 'lib/pptx/shapes/textbox.rb', line 67

def empty_paragraph
  paragraph_xml = """
    <a:p xmlns:a='http://schemas.openxmlformats.org/drawingml/2006/main'></a:p>
  """
  Nokogiri::XML::DocumentFragment.parse(paragraph_xml)
end

#set_formatting(paragraph, formatting) ⇒ Object

Set paragraph and text run properties on a paragraph. Node must already have pPr and rPr elements.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/pptx/shapes/textbox.rb', line 76

def set_formatting(paragraph, formatting)
  formatting = formatting.dup
  color = formatting.delete(:color)
  align = formatting.delete(:align)

  if align
    p_properties = paragraph.xpath('./a:p/a:pPr', a: DRAWING_NS).first
    p_properties['algn'] = align
  end

  run_properties = paragraph.xpath('.//a:rPr', a: DRAWING_NS).first
  formatting.each do |key, val|
    run_properties[key] = val
  end

  run_properties.add_child build_solid_fill(color) if color
end