From d09a5daa5b5814d72a9ffc3e8f3989b2dfd01d74 Mon Sep 17 00:00:00 2001 From: Martin Kaiblinger Date: Thu, 16 Feb 2017 16:07:20 +0100 Subject: [PATCH] Shapes: Support setting background color --- lib/pptx/generate_sample.rb | 5 +++-- lib/pptx/shapes/filled_rectangle.rb | 9 +++------ lib/pptx/shapes/shape.rb | 27 ++++++++++++++++++++++++--- lib/pptx/shapes/textbox.rb | 2 +- lib/pptx/slide.rb | 3 ++- 5 files changed, 33 insertions(+), 13 deletions(-) diff --git a/lib/pptx/generate_sample.rb b/lib/pptx/generate_sample.rb index efe0d4b..c3bebff 100644 --- a/lib/pptx/generate_sample.rb +++ b/lib/pptx/generate_sample.rb @@ -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&toescaped\nYay!" + "down into multiple lines.\n:)\nwith&toescaped\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') diff --git a/lib/pptx/shapes/filled_rectangle.rb b/lib/pptx/shapes/filled_rectangle.rb index 8559c15..00c0646 100644 --- a/lib/pptx/shapes/filled_rectangle.rb +++ b/lib/pptx/shapes/filled_rectangle.rb @@ -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 @@ -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 diff --git a/lib/pptx/shapes/shape.rb b/lib/pptx/shapes/shape.rb index 02dc218..0ab636d 100644 --- a/lib/pptx/shapes/shape.rb +++ b/lib/pptx/shapes/shape.rb @@ -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 = """ @@ -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 = """ diff --git a/lib/pptx/shapes/textbox.rb b/lib/pptx/shapes/textbox.rb index a2129b6..6a2e31f 100644 --- a/lib/pptx/shapes/textbox.rb +++ b/lib/pptx/shapes/textbox.rb @@ -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 diff --git a/lib/pptx/slide.rb b/lib/pptx/slide.rb index cb37393..2160714 100644 --- a/lib/pptx/slide.rb +++ b/lib/pptx/slide.rb @@ -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)