Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/pptx/generate_sample.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ def main
slide.add_textbox date_dimensions, '18 Feb 2015'
slide.add_textbox text_dimensions,
"Text box text with long text that should be broken " +
"down into multiple lines.\n:)\nwith<stuff>&to<be>escaped\nYay!"
"down into multiple lines.\n:)\nwith<stuff>&to<be>escaped\nYay!",
bg: '4d4d4d'

image = PPTX::OPC::FilePart.new(pkg, 'spec/fixtures/test_picture.png')
slide.add_picture image_dimensions, 'photo.jpg', image
slide.add_filled_rectangle(PPTX::cm(24.9, 0, 0.5, 19.05), '558ed5')
slide.add_filled_rectangle(PPTX::cm(24.9, 0, 0.5, 19.05), bg: '558ed5')
slide.add_slide_number(PPTX::cm(23.4, 17.5, 1, 0.8), 1, sz: 12*PPTX::POINT,
color: '4d4d4d',
align: 'r')
Expand Down
9 changes: 3 additions & 6 deletions lib/pptx/shapes/filled_rectangle.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
module PPTX
module Shapes
class FilledRectangle < Shape
def initialize(transform, color)
super(transform)
@color = color
def initialize(transform, formatting={})
super(transform, extract_shape_properties(formatting))
end

def base_xml
Expand All @@ -23,9 +22,7 @@ def base_xml
end

def build_node
base_node.tap do |node|
node.xpath('.//p:spPr', p: Presentation::NS).first.add_child build_solid_fill @color
end
base_node
end
end
end
Expand Down
27 changes: 24 additions & 3 deletions lib/pptx/shapes/shape.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
module PPTX
module Shapes
class Shape
def initialize(transform)
def initialize(transform, shape_formatting={})
@transform = transform
@shape_formatting = shape_formatting
end

def base_node
@base_node ||= Nokogiri::XML::DocumentFragment.parse(base_xml).tap do |node|
set_shape_properties(node, *@transform)
build_shape_properties(node, *@transform)
set_shape_properties(node, @shape_formatting)
end
end

def set_shape_properties(node, x, y, width, height)
def build_shape_properties(node, x, y, width, height)
xml = """
<p:spPr xmlns:a='http://schemas.openxmlformats.org/drawingml/2006/main'
xmlns:p='http://schemas.openxmlformats.org/presentationml/2006/main'>
Expand All @@ -30,6 +32,25 @@ def set_shape_properties(node, x, y, width, height)
.replace(Nokogiri::XML::DocumentFragment.parse(xml))
end


def set_shape_properties(node, formatting)
bg = formatting.delete(:bg)
node.xpath('.//p:spPr', p: Presentation::NS).first.add_child build_solid_fill(bg) if(bg)
end

def extract_shape_properties(formatting)
shape_formats = [:bg]

shape_formatting = {}
formatting.each do |type, value|
if(shape_formats.include?(type))
shape_formatting[type] = value
formatting.delete(type)
end
end
shape_formatting
end

def build_solid_fill(rgb_color)
fill_xml = """
<a:solidFill xmlns:a='http://schemas.openxmlformats.org/drawingml/2006/main'>
Expand Down
2 changes: 1 addition & 1 deletion lib/pptx/shapes/textbox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module PPTX
module Shapes
class Textbox < Shape
def initialize(transform, content, formatting={})
super(transform)
super(transform, extract_shape_properties(formatting))
@content = content
@formatting = formatting
end
Expand Down
3 changes: 2 additions & 1 deletion lib/pptx/slide.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ def add_picture(transform, name, image)
# * sz: font size (use PPTX::POINT)
# * b: 1 for bold
# * i: 1 for italic
# There are two custom properties:
# There are three custom properties:
# * color: aabbcc (hex RGB color)
# * align: ctr|dist|just|justLow|l|r|thaiDist (ST_TextAlignType (Text Alignment Types))
# * bg: aabbcc (hex RGB color)
def add_textbox(transform, content, formatting={})
shape = Shapes::Textbox.new(transform, content, formatting)
shape_tree_xml.add_child(shape.build_node)
Expand Down