lib/write_xlsx/drawing.rb in write_xlsx-0.72.2 vs lib/write_xlsx/drawing.rb in write_xlsx-0.72.3.beta1

- old
+ new

@@ -25,34 +25,26 @@ # # Assemble and write the XML file. # def assemble_xml_file - @writer.xml_decl - - # Write the xdr:wsDr element. - write_drawing_workspace - - if @embedded - index = 0 - @drawings.each do |dimensions| - # Write the xdr:twoCellAnchor element. - index += 1 - - write_two_cell_anchor(index, *(dimensions.flatten)) + write_xml_declaration do + # Write the xdr:wsDr element. + write_drawing_workspace do + if @embedded + index = 0 + @drawings.each do |dimensions| + # Write the xdr:twoCellAnchor element. + index += 1 + write_two_cell_anchor(index, *(dimensions.flatten)) + end + else + # Write the xdr:absoluteAnchor element. + write_absolute_anchor(1) + end end - else - index = 0 - - # Write the xdr:absoluteAnchor element. - index += 1 - write_absolute_anchor(index) end - - @writer.end_tag('xdr:wsDr') - @writer.crlf - @writer.close end # # Add a chart, image or shape sub object to the drawing. # @@ -66,15 +58,15 @@ # Write the <xdr:wsDr> element. # def write_drawing_workspace schema = 'http://schemas.openxmlformats.org/drawingml/' attributes = [ - 'xmlns:xdr', "#{schema}2006/spreadsheetDrawing", - 'xmlns:a', "#{schema}2006/main" + ['xmlns:xdr', "#{schema}2006/spreadsheetDrawing"], + ['xmlns:a', "#{schema}2006/main"] ] - @writer.start_tag('xdr:wsDr', attributes) + @writer.tag_elements('xdr:wsDr', attributes) { yield } end # # Write the <xdr:twoCellAnchor> element. # @@ -84,14 +76,14 @@ width, height, description, shape = args attributes = [] # Add attribute for images. - attributes << :editAs << 'oneCell' if type == 2 + attributes << [:editAs, 'oneCell'] if type == 2 # Add attribute for shapes. - attributes << :editAs << shape.edit_as if shape && !shape.edit_as.nil? + attributes << [:editAs, shape.edit_as] if shape && !shape.edit_as.nil? @writer.tag_elements('xdr:twoCellAnchor', attributes) do # Write the xdr:from element. write_from(col_from, row_from, col_from_offset, row_from_offset) # Write the xdr:from element. @@ -211,24 +203,24 @@ # # Write the <xdr:pos> element. # def write_pos(x, y) attributes = [ - 'x', x, - 'y', y + ['x', x], + ['y', y] ] @writer.empty_tag('xdr:pos', attributes) end # # Write the <xdr:ext> element. # def write_ext(cx, cy) attributes = [ - 'cx', cx, - 'cy', cy + ['cx', cx], + ['cy', cy] ] @writer.empty_tag('xdr:ext', attributes) end @@ -236,11 +228,11 @@ # Write the <xdr:graphicFrame> element. # def write_graphic_frame(index, name = nil) macro = '' - attributes = ['macro', macro] + attributes = [ ['macro', macro] ] @writer.tag_elements('xdr:graphicFrame', attributes) do # Write the xdr:nvGraphicFramePr element. write_nv_graphic_frame_pr(index, name) # Write the xdr:xfrm element. @@ -267,16 +259,16 @@ # # Write the <xdr:cNvPr> element. # def write_c_nv_pr(id, name, descr = nil) attributes = [ - 'id', id, - 'name', name + ['id', id], + ['name', name] ] # Add description attribute for images. - attributes << 'descr' << descr if descr + attributes << ['descr', descr] if descr @writer.empty_tag('xdr:cNvPr', attributes) end @@ -298,11 +290,11 @@ # Write the <a:graphicFrameLocks> element. # def write_a_graphic_frame_locks no_grp = 1 - attributes = ['noGrp', no_grp ] + attributes = [ ['noGrp', no_grp ] ] @writer.empty_tag('a:graphicFrameLocks', attributes) end # @@ -323,12 +315,12 @@ def write_xfrm_offset x = 0 y = 0 attributes = [ - 'x', x, - 'y', y + ['x', x], + ['y', y] ] @writer.empty_tag('a:off', attributes) end @@ -338,12 +330,12 @@ def write_xfrm_extension x = 0 y = 0 attributes = [ - 'cx', x, - 'cy', y + ['cx', x], + ['cy', y] ] @writer.empty_tag('a:ext', attributes) end @@ -361,11 +353,11 @@ # Write the <a:graphicData> element. # def write_atag_graphic_data(index) uri = 'http://schemas.openxmlformats.org/drawingml/2006/chart' - attributes = ['uri', uri] + attributes = [ ['uri', uri] ] @writer.tag_elements('a:graphicData', attributes) do # Write the c:chart element. write_c_chart(index) end @@ -378,12 +370,15 @@ schema = 'http://schemas.openxmlformats.org/' xmlns_c = "#{schema}drawingml/2006/chart" xmlns_r = "#{schema}officeDocument/2006/relationships" - attributes = ['xmlns:c', xmlns_c, 'xmlns:r', xmlns_r] - attributes += r_id_attributes(id) + attributes = [ + ['xmlns:c', xmlns_c], + ['xmlns:r', xmlns_r] + ] + attributes << r_id_attributes(id) @writer.empty_tag('c:chart', attributes) end # @@ -396,22 +391,25 @@ # # Write the <xdr:sp> element. # def write_sp(index, col_absolute, row_absolute, width, height, shape) if shape.connect != 0 - attributes = [:macro, ''] + attributes = [ [:macro, ''] ] @writer.tag_elements('xdr:cxnSp', attributes) do # Write the xdr:nvCxnSpPr element. write_nv_cxn_sp_pr(index, shape) # Write the xdr:spPr element. write_xdr_sp_pr(index, col_absolute, row_absolute, width, height, shape) end else # Add attribute for shapes. - attributes = [:macro, '', :textlink, ''] + attributes = [ + [:macro, ''], + [:textlink, ''] + ] @writer.tag_elements('xdr:sp', attributes) do # Write the xdr:nvSpPr element. write_nv_sp_pr(index, shape) @@ -435,20 +433,26 @@ shape.name = [shape.type, index].join(' ') unless shape.name write_c_nv_pr(shape.id, shape.name) @writer.tag_elements('xdr:cNvCxnSpPr') do - attributes = [:noChangeShapeType, '1'] + attributes = [ [:noChangeShapeType, '1'] ] @writer.empty_tag('a:cxnSpLocks', attributes) if shape.start - attributes = ['id', shape.start, 'idx', shape.start_index] + attributes = [ + ['id', shape.start], + ['idx', shape.start_index] + ] @writer.empty_tag('a:stCxn', attributes) end if shape.end - attributes = ['id', shape.end, 'idx', shape.end_index] + attributes = [ + ['id', shape.end], + ['idx', shape.end_index] + ] @writer.empty_tag('a:endCxn', attributes) end end end end @@ -456,17 +460,17 @@ # # Write the <xdr:NvSpPr> element. # def write_nv_sp_pr(index, shape) attributes = [] - attributes << 'txBox' << 1 if shape.tx_box + attributes << ['txBox', 1] if shape.tx_box @writer.tag_elements('xdr:nvSpPr') do write_c_nv_pr(shape.id, "#{shape.type} #{index}") @writer.tag_elements('xdr:cNvSpPr', attributes) do - @writer.empty_tag('a:spLocks', [:noChangeArrowheads, '1']) + @writer.empty_tag('a:spLocks', [ [:noChangeArrowheads, '1'] ]) end end end # @@ -514,11 +518,11 @@ # Write the <a:picLocks> element. # def write_a_pic_locks no_change_aspect = 1 - attributes = ['noChangeAspect', no_change_aspect] + attributes = [ ['noChangeAspect', no_change_aspect] ] @writer.empty_tag('a:picLocks', attributes) end # @@ -540,12 +544,12 @@ schema = 'http://schemas.openxmlformats.org/officeDocument/' xmlns_r = "#{schema}2006/relationships" r_embed = "rId#{index}" attributes = [ - 'xmlns:r', xmlns_r, - 'r:embed', r_embed + ['xmlns:r', xmlns_r], + ['r:embed', r_embed] ] @writer.empty_tag('a:blip', attributes) end @@ -580,11 +584,11 @@ # # Write the <xdr:spPr> element for shapes. # def write_xdr_sp_pr(index, col_absolute, row_absolute, width, height, shape) - attributes = ['bwMode', 'auto'] + attributes = [ ['bwMode', 'auto'] ] @writer.tag_elements('xdr:spPr', attributes) do # Write the a:xfrm element. write_a_xfrm(col_absolute, row_absolute, width, height, shape) @@ -611,13 +615,13 @@ attributes = [] rotation = shape ? shape.rotation : 0 rotation *= 60000 - attributes << 'rot' << rotation if rotation != 0 - attributes << 'flipH' << 1 if shape && ptrue?(shape.flip_h) - attributes << 'flipV' << 1 if shape && ptrue?(shape.flip_v) + attributes << ['rot', rotation] if rotation != 0 + attributes << ['flipH', 1] if shape && ptrue?(shape.flip_h) + attributes << ['flipV', 1] if shape && ptrue?(shape.flip_v) @writer.tag_elements('a:xfrm', attributes) do # Write the a:off element. write_a_off( col_absolute, row_absolute ) # Write the a:ext element. @@ -628,12 +632,12 @@ # # Write the <a:off> element. # def write_a_off(x, y) attributes = [ - 'x', x, - 'y', y + ['x', x], + ['y', y] ] @writer.empty_tag('a:off', attributes) end @@ -641,23 +645,23 @@ # # Write the <a:ext> element. # def write_a_ext(cx, cy) attributes = [ - 'cx', cx, - 'cy', cy + ['cx', cx], + ['cy', cy] ] @writer.empty_tag('a:ext', attributes) end # # Write the <a:prstGeom> element. # def write_a_prst_geom(shape = {}) attributes = [] - attributes << 'prst' << shape.type if shape.type + attributes << ['prst', shape.type] if shape.type @writer.tag_elements('a:prstGeom', attributes) do # Write the a:avLst element. write_a_av_lst(shape) end @@ -684,11 +688,14 @@ suffix = shape.connect != 0 ? i : '' # Scale Adjustments: 100,000 = 100%. adj_int = (adj * 1000).to_i - attributes = [:name, "adj#{suffix}", :fmla, "val #{adj_int}"] + attributes = [ + [:name, "adj#{suffix}"], + [:fmla, "val #{adj_int}"] + ] @writer.empty_tag('a:gd', attributes) end end else @writer.empty_tag('a:avLst') @@ -697,11 +704,11 @@ # # Write the <a:solidFill> element. # def write_a_solid_fill(rgb = '000000') - attributes = ['val', rgb] + attributes = [ ['val', rgb] ] @writer.tag_elements('a:solidFill') do @writer.empty_tag('a:srgbClr', attributes) end end @@ -709,29 +716,29 @@ # # Write the <a:ln> elements. # def write_a_ln(shape = {}) weight = shape.line_weight || 0 - attributes = ['w', weight * 9525] + attributes = [ ['w', weight * 9525] ] @writer.tag_elements('a:ln', attributes) do line = shape.line || 0 if line.to_s.bytesize > 1 # Write the a:solidFill element. write_a_solid_fill(line) else @writer.empty_tag('a:noFill') end if shape.line_type != '' - attributes = ['val', shape.line_type] + attributes = [ ['val', shape.line_type] ] @writer.empty_tag('a:prstDash', attributes) end if shape.connect != 0 @writer.empty_tag('a:round') else - attributes = ['lim', 800000] + attributes = [ ['lim', 800000] ] @writer.empty_tag('a:miter', attributes) end @writer.empty_tag('a:headEnd') @writer.empty_tag('a:tailEnd') @@ -741,30 +748,33 @@ # # Write the <xdr:txBody> element. # def write_txBody(col_absolute, row_absolute, width, height, shape) attributes = [ - :vertOverflow, "clip", - :wrap, "square", - :lIns, "27432", - :tIns, "22860", - :rIns, "27432", - :bIns, "22860", - :anchor, shape.valign, - :upright, "1" + [:vertOverflow, "clip"], + [:wrap, "square"], + [:lIns, "27432"], + [:tIns, "22860"], + [:rIns, "27432"], + [:bIns, "22860"], + [:anchor, shape.valign], + [:upright, "1"] ] @writer.tag_elements('xdr:txBody') do @writer.empty_tag('a:bodyPr', attributes) @writer.empty_tag('a:lstStyle') @writer.tag_elements('a:p') do rotation = shape.format[:rotation] || 0 rotation *= 60000 - attributes = [:algn, shape.align, :rtl, rotation] + attributes = [ + [:algn, shape.align], + [:rtl, rotation] + ] @writer.tag_elements('a:pPr', attributes) do - attributes = [:sz, "1000"] + attributes = [ [:sz, "1000"] ] @writer.empty_tag('a:defRPr', attributes) end @writer.tag_elements('a:r') do size = shape.format[:size] || 8 @@ -774,17 +784,17 @@ italic = shape.format[:italic] || 0 underline = ptrue?(shape.format[:underline]) ? 'sng' : 'none' strike = ptrue?(shape.format[:font_strikeout]) ? 'Strike' : 'noStrike' attributes = [ - :lang, "en-US", - :sz, size, - :b, bold, - :i, italic, - :u, underline, - :strike, strike, - :baseline, 0 + [:lang, "en-US"], + [:sz, size], + [:b, bold], + [:i, italic], + [:u, underline], + [:strike, strike], + [:baseline, 0] ] @writer.tag_elements('a:rPr', attributes) do color = shape.format[:color] if color color = shape.get_palette_color(color) @@ -794,10 +804,10 @@ end write_a_solid_fill(color) font = shape.format[:font] || 'Calibri' - attributes = [:typeface, font] + attributes = [ [:typeface, font] ] @writer.empty_tag('a:latin', attributes) @writer.empty_tag('a:cs', attributes) end @writer.tag_elements('a:t') do @writer.characters(shape.text)