xlsxrb 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +43 -0
  3. data/lib/xlsxrb/elements/cell.rb +1 -1
  4. data/lib/xlsxrb/elements/workbook.rb +115 -4
  5. data/lib/xlsxrb/number_formatter.rb +5 -5
  6. data/lib/xlsxrb/ooxml/cfb.rb +61 -25
  7. data/lib/xlsxrb/ooxml/crypto/agile.rb +41 -12
  8. data/lib/xlsxrb/ooxml/reader/listeners/core_listeners.rb +4 -0
  9. data/lib/xlsxrb/ooxml/reader/listeners/feature_listeners.rb +2 -0
  10. data/lib/xlsxrb/ooxml/shared_strings_parser.rb +4 -2
  11. data/lib/xlsxrb/ooxml/utils.rb +4 -3
  12. data/lib/xlsxrb/ooxml/workbook_parser.rb +88 -40
  13. data/lib/xlsxrb/ooxml/workbook_writer.rb +48 -24
  14. data/lib/xlsxrb/ooxml/worksheet_parser.rb +186 -78
  15. data/lib/xlsxrb/ooxml/worksheet_writer.rb +81 -18
  16. data/lib/xlsxrb/ooxml/writer/drawing_xml.rb +1511 -1458
  17. data/lib/xlsxrb/ooxml/writer/features_xml.rb +558 -525
  18. data/lib/xlsxrb/ooxml/writer/styles_xml.rb +272 -280
  19. data/lib/xlsxrb/ooxml/writer.rb +1017 -941
  20. data/lib/xlsxrb/ooxml/xml_builder.rb +48 -16
  21. data/lib/xlsxrb/ooxml/xml_parser.rb +37 -2
  22. data/lib/xlsxrb/ooxml/zip_generator.rb +10 -10
  23. data/lib/xlsxrb/ooxml/zip_reader.rb +19 -6
  24. data/lib/xlsxrb/ooxml/zip_writer.rb +15 -7
  25. data/lib/xlsxrb/stream_row.rb +8 -5
  26. data/lib/xlsxrb/stream_sheet.rb +3 -0
  27. data/lib/xlsxrb/stream_writer.rb +48 -18
  28. data/lib/xlsxrb/style_builder.rb +9 -4
  29. data/lib/xlsxrb/version.rb +1 -1
  30. data/lib/xlsxrb/workbook_builder.rb +14 -8
  31. data/lib/xlsxrb/worksheet_builder.rb +10 -4
  32. data/lib/xlsxrb.rb +415 -418
  33. data/sig/generated/xlsxrb/elements/workbook.rbs +23 -1
  34. data/sig/generated/xlsxrb/ooxml/reader/listeners/core_listeners.rbs +2 -0
  35. data/sig/generated/xlsxrb/ooxml/workbook_parser.rbs +21 -1
  36. data/sig/generated/xlsxrb/ooxml/worksheet_parser.rbs +20 -2
  37. data/sig/generated/xlsxrb/ooxml/writer/drawing_xml.rbs +162 -47
  38. data/sig/generated/xlsxrb/ooxml/writer/features_xml.rbs +20 -24
  39. data/sig/generated/xlsxrb/ooxml/writer/styles_xml.rbs +20 -20
  40. data/sig/generated/xlsxrb/ooxml/writer.rbs +61 -10
  41. data/sig/generated/xlsxrb/ooxml/xml_builder.rbs +19 -1
  42. data/sig/generated/xlsxrb/ooxml/xml_parser.rbs +21 -0
  43. data/sig/generated/xlsxrb/ooxml/zip_writer.rbs +5 -0
  44. data/sig/generated/xlsxrb/stream_row.rbs +4 -3
  45. data/sig/generated/xlsxrb/style_builder.rbs +5 -5
  46. data/sig/generated/xlsxrb.rbs +41 -20
  47. metadata +1 -1
@@ -7,571 +7,838 @@ module Xlsxrb
7
7
  class Writer
8
8
  # Mixin containing DrawingML, Chart, Shape, Comment, and VML XML generation logic.
9
9
  module DrawingXml
10
- # : (untyped drawing_parts) -> untyped
10
+ # : (untyped drawing_parts) -> ::String
11
11
  def generate_drawing_xml(drawing_parts)
12
- parts = [
13
- XML_HEADER,
14
- %(<xdr:wsDr xmlns:xdr="#{XDR_NS}" xmlns:a="#{A_NS}" xmlns:r="#{DOC_REL_NS}">)
15
- ]
16
-
17
- drawing_parts.each do |dp|
18
- case dp[:kind]
19
- when :pic
20
- img = dp[:img]
21
- rid = "rId#{dp[:rid_index]}"
22
- ea = img[:edit_as] || "oneCell"
23
- img_pub_attr = img[:published] ? ' fPublished="1"' : ""
24
- parts << %(<xdr:twoCellAnchor editAs="#{xml_escape(ea)}"#{img_pub_attr}>)
25
- parts << anchor_xml("from", img[:from_column] || img[:from_col], img[:from_row], col_off: img[:from_col_off] || 0, row_off: img[:from_row_off] || 0)
26
- parts << anchor_xml("to", img[:to_column] || img[:to_col], img[:to_row], col_off: img[:to_col_off] || 0, row_off: img[:to_row_off] || 0)
27
- macro_attr = img[:macro] ? %( macro="#{xml_escape(img[:macro])}") : ""
28
- parts << "<xdr:pic#{macro_attr}>"
29
- descr_attr = img[:description] ? %( descr="#{xml_escape(img[:description])}") : ""
30
- title_attr = img[:title] ? %( title="#{xml_escape(img[:title])}") : ""
31
- hidden_attr = img[:hidden] ? ' hidden="1"' : ""
32
- pic_lock_attrs = +""
33
- pic_lock_attrs << ' noChangeAspect="1"' if img[:no_change_aspect]
34
- pic_lock_attrs << ' noCrop="1"' if img[:no_crop]
35
- pic_locks = pic_lock_attrs.empty? ? "<a:picLocks/>" : "<a:picLocks#{pic_lock_attrs}/>"
36
- parts << %(<xdr:nvPicPr><xdr:cNvPr id="#{xml_escape(dp[:rid_index] + 1)}" name="#{xml_escape(img[:name])}"#{descr_attr}#{title_attr}#{hidden_attr}/><xdr:cNvPicPr>#{pic_locks}</xdr:cNvPicPr></xdr:nvPicPr>)
37
- src_rect_xml = if img[:src_rect]
38
- sr = img[:src_rect]
39
- sr_attrs = +""
40
- sr_attrs << %( t="#{xml_escape(sr[:top])}") if sr[:top]
41
- sr_attrs << %( b="#{xml_escape(sr[:bottom])}") if sr[:bottom]
42
- sr_attrs << %( l="#{xml_escape(sr[:left])}") if sr[:left]
43
- sr_attrs << %( r="#{xml_escape(sr[:right])}") if sr[:right]
44
- "<a:srcRect#{sr_attrs}/>"
45
- else
46
- ""
47
- end
48
- blip_xml = if img[:alpha_mod_fix]
49
- %(<a:blip r:embed="#{xml_escape(rid)}"><a:alphaModFix amt="#{xml_escape(img[:alpha_mod_fix])}"/></a:blip>)
12
+ XmlBuilder.build(declaration: :plain) do |xml|
13
+ xml.tag("xdr:wsDr", { "xmlns:xdr": XDR_NS, "xmlns:a": A_NS, "xmlns:r": DOC_REL_NS }) do
14
+ drawing_parts.each do |dp|
15
+ case dp[:kind]
16
+ when :pic
17
+ write_pic_anchor_xml(xml, dp)
18
+ when :chart
19
+ write_chart_anchor_xml(xml, dp)
20
+ when :sp
21
+ write_shape_anchor_xml(xml, dp)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ # : (untyped rels_data) -> ::String
29
+ def generate_drawing_rels(rels_data)
30
+ XmlBuilder.build(declaration: :plain) do |xml|
31
+ xml.tag("Relationships", { xmlns: REL_NS }) do
32
+ rels_data.each_with_index do |rel, i|
33
+ rel_type = case rel[:type]
34
+ when :image then "#{DOC_REL_NS}/image"
35
+ when :chart then "#{DOC_REL_NS}/chart"
36
+ end
37
+ xml.empty_tag("Relationship", { Id: "rId#{i + 1}", Type: rel_type || "", Target: rel[:target] || "" })
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ # : (untyped chart) -> ::String
44
+ def generate_chart_xml(chart)
45
+ # Charts read from files carry the element name ("lineChart") as their type.
46
+ chart_type = CHART_TYPE_MAP[chart[:type]] || CHART_TYPE_MAP.values.find { |element| element == chart[:type].to_s } || "barChart"
47
+ XmlBuilder.build(declaration: :plain) do |xml|
48
+ xml.tag("c:chartSpace", { "xmlns:c": C_NS, "xmlns:a": A_NS, "xmlns:r": DOC_REL_NS }) do
49
+ xml.empty_tag("c:roundedCorners", { val: chart[:rounded_corners] ? 1 : 0 }) unless chart[:rounded_corners].nil?
50
+ xml.empty_tag("c:style", { val: chart[:style] }) if chart[:style]
51
+ if (prot = chart[:protection])
52
+ xml.tag("c:protection") do
53
+ xml.empty_tag("c:chartObject", { val: prot[:chart_object] ? 1 : 0 }) unless prot[:chart_object].nil?
54
+ xml.empty_tag("c:data", { val: prot[:data] ? 1 : 0 }) unless prot[:data].nil?
55
+ xml.empty_tag("c:formatting", { val: prot[:formatting] ? 1 : 0 }) unless prot[:formatting].nil?
56
+ xml.empty_tag("c:selection", { val: prot[:selection] ? 1 : 0 }) unless prot[:selection].nil?
57
+ xml.empty_tag("c:userInterface", { val: prot[:user_interface] ? 1 : 0 }) unless prot[:user_interface].nil?
58
+ end
59
+ end
60
+
61
+ xml.tag("c:chart") do
62
+ write_chart_top_title_xml(xml, chart)
63
+ xml.empty_tag("c:autoTitleDeleted", { val: chart[:auto_title_deleted] ? 1 : 0 }) unless chart[:auto_title_deleted].nil?
64
+ write_view_3d_xml(xml, chart[:view_3d]) if chart[:view_3d]
65
+ write_chart_walls_xml(xml, chart)
66
+
67
+ write_plot_area_xml(xml, chart, chart_type)
68
+
69
+ write_chart_legend_xml(xml, chart[:legend])
70
+ xml.empty_tag("c:plotVisOnly", { val: chart[:plot_vis_only] ? 1 : 0 }) unless chart[:plot_vis_only].nil?
71
+ xml.empty_tag("c:dispBlanksAs", { val: chart[:disp_blanks_as] }) if chart[:disp_blanks_as]
72
+ xml.empty_tag("c:showDLblsOverMax", { val: chart[:show_d_lbls_over_max] ? 1 : 0 }) unless chart[:show_d_lbls_over_max].nil?
73
+ end
74
+
75
+ if chart[:chart_fill] || chart[:chart_no_fill] || chart[:chart_line_color] || chart[:chart_line_dash]
76
+ write_simple_sp_pr(
77
+ xml,
78
+ fill_color: chart[:chart_fill],
79
+ no_fill: chart[:chart_no_fill],
80
+ line_color: chart[:chart_line_color],
81
+ line_width: chart[:chart_line_width],
82
+ line_dash: chart[:chart_line_dash]
83
+ )
84
+ end
85
+ write_axis_txpr(xml, nil, chart[:chart_font]) if chart[:chart_font]
86
+ write_chart_print_settings_xml(xml, chart[:print_settings]) if chart[:print_settings]
87
+ end
88
+ end
89
+ end
90
+
91
+ # : (untyped sheet_comments) -> ::String
92
+ def generate_comments_xml(sheet_comments)
93
+ authors = sheet_comments.map { |c| c[:author] }.uniq
94
+ XmlBuilder.build(declaration: :plain) do |xml|
95
+ xml.tag("comments", { xmlns: SSML_NS }) do
96
+ xml.tag("authors") do
97
+ authors.each { |a| xml.text_tag("author", a || "") }
98
+ end
99
+ xml.tag("commentList") do
100
+ sheet_comments.each do |c|
101
+ aid = authors.index(c[:author]) || 0
102
+ xml.tag("comment", { ref: c[:ref] || "", authorId: aid, guid: c[:guid], shapeId: c[:shape_id] }) do
103
+ xml.tag("text") do
104
+ if c[:text].is_a?(Xlsxrb::Elements::RichText)
105
+ write_rich_text_xml(xml, c[:text])
106
+ else
107
+ xml.tag("r") { xml.text_tag("t", c[:text] || "") }
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
116
+
117
+ # : (untyped sheet_comments) -> ::String
118
+ def generate_vml_drawing_xml(sheet_comments)
119
+ XmlBuilder.build do |xml|
120
+ xml.tag(
121
+ "xml",
122
+ {
123
+ "xmlns:v": "urn:schemas-microsoft-com:vml",
124
+ "xmlns:o": "urn:schemas-microsoft-com:office:office",
125
+ "xmlns:x": "urn:schemas-microsoft-com:office:excel"
126
+ }
127
+ ) do
128
+ xml.tag("o:shapelayout", { "v:ext": "edit" }) do
129
+ xml.empty_tag("o:idmap", { "v:ext": "edit", data: 1 })
130
+ end
131
+ xml.tag(
132
+ "v:shapetype",
133
+ {
134
+ id: "_x0000_t202",
135
+ coordsize: "21600,21600",
136
+ "o:spt": 202,
137
+ path: "m,l,21600r21600,l21600,xe"
138
+ }
139
+ ) do
140
+ xml.empty_tag("v:stroke", { joinstyle: "miter" })
141
+ xml.empty_tag("v:path", { gradientshapeok: "t", "o:connecttype": "rect" })
142
+ end
143
+
144
+ sheet_comments.each_with_index do |c, idx|
145
+ col, row = cell_to_col_row(c[:ref])
146
+ shape_id = 1025 + idx
147
+ style = "position:absolute;margin-left:59.25pt;margin-top:1.5pt;width:108pt;height:59.25pt;z-index:#{idx + 1};visibility:#{c[:visible] ? "visible" : "hidden"}"
148
+ xml.tag(
149
+ "v:shape",
150
+ {
151
+ id: "_x0000_s#{shape_id}",
152
+ type: "#_x0000_t202",
153
+ style: style,
154
+ fillcolor: "#ffffe1",
155
+ "o:insetmode": "auto"
156
+ }
157
+ ) do
158
+ xml.empty_tag("v:fill", { color2: "#ffffe1" })
159
+ xml.empty_tag("v:shadow", { on: "t", color: "black", obscured: "t" })
160
+ xml.empty_tag("v:path", { "o:connecttype": "none" })
161
+ xml.tag("v:textbox", { style: "mso-direction-alt:auto" }) do
162
+ xml.open_tag("div", { style: "text-align:left" })
163
+ xml.close_tag("div")
164
+ end
165
+ xml.tag("x:ClientData", { ObjectType: "Note" }) do
166
+ xml.empty_tag("x:MoveWithCells")
167
+ xml.empty_tag("x:SizeWithCells")
168
+ anchor = c[:anchor] || Xlsxrb::Elements::Comment.default_anchor(col, row)
169
+ xml.text_tag("x:Anchor", anchor.join(", "))
170
+ xml.text_tag("x:AutoFill", "False")
171
+ xml.text_tag("x:Row", row)
172
+ xml.text_tag("x:Column", col)
173
+ xml.empty_tag("x:Visible") if c[:visible]
174
+ end
175
+ end
176
+ end
177
+ end
178
+ end
179
+ end
180
+
181
+ # : (untyped cell_ref) -> (::Array[0] | ::Array[untyped])
182
+ def cell_to_col_row(cell_ref)
183
+ m = cell_ref.match(/\A([A-Z]+)(\d+)\z/)
184
+ return [0, 0] unless m
185
+
186
+ col = column_letter_to_index(m[1]) - 1
187
+ row = m[2].to_i - 1
188
+ [col, row]
189
+ end
190
+
191
+ THREADED_COMMENTS_NS = "http://schemas.microsoft.com/office/spreadsheetml/2018/threadedcomments"
192
+
193
+ # Formats or generates a GUID in standard uppercase braced format {UUID}.
194
+ # : (?String? val) -> String
195
+ def format_guid(val = nil)
196
+ return "{#{SecureRandom.uuid.upcase}}" if val.nil? || val.to_s.empty?
197
+
198
+ str = val.to_s.strip
199
+ str = "{#{str}}" unless str.start_with?("{") && str.end_with?("}")
200
+ str.upcase
201
+ end
202
+
203
+ # Generates XML for xl/threadedComments/threadedComment{N}.xml.
204
+ # : (Array[Hash[Symbol, untyped]] sheet_threaded_comments) -> String
205
+ def generate_threaded_comments_xml(sheet_threaded_comments)
206
+ XmlBuilder.build(declaration: :plain) do |xml|
207
+ xml.tag("ThreadedComments", { xmlns: THREADED_COMMENTS_NS }) do
208
+ sheet_threaded_comments.each do |c|
209
+ ref = c[:ref] || c[:cell]
210
+ dt = c[:timestamp] || c[:dt]
211
+ dt_str = case dt
212
+ when Time, DateTime
213
+ dt.to_time.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
214
+ when String
215
+ dt
50
216
  else
51
- %(<a:blip r:embed="#{xml_escape(rid)}"/>)
217
+ Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
52
218
  end
53
- parts << %(<xdr:blipFill>#{blip_xml}#{src_rect_xml}<a:stretch><a:fillRect/></a:stretch></xdr:blipFill>)
54
- img_line_xml = if img[:line_color]
55
- ln_w_attr = img[:line_width] ? %( w="#{img[:line_width].to_i}") : ""
56
- %(<a:ln#{ln_w_attr}><a:solidFill>#{color_xml(img[:line_color])}</a:solidFill></a:ln>)
57
- else
58
- ""
59
- end
60
- img_xfrm_xml = img[:rotation] ? %(<a:xfrm rot="#{img[:rotation].to_i}"><a:off x="0" y="0"/><a:ext cx="0" cy="0"/></a:xfrm>) : ""
61
- parts << %(<xdr:spPr>#{img_xfrm_xml}<a:prstGeom prst="rect"><a:avLst/></a:prstGeom>#{img_line_xml}</xdr:spPr>)
62
- parts << "</xdr:pic>"
63
- parts << client_data_xml(img)
64
- parts << "</xdr:twoCellAnchor>"
65
- when :chart
66
- chart = dp[:chart]
67
- rid = "rId#{dp[:rid_index]}"
68
- chart_ea_attr = chart[:edit_as] ? %( editAs="#{xml_escape(chart[:edit_as])}") : ""
69
- chart_pub_attr = chart[:published] ? ' fPublished="1"' : ""
70
- parts << "<xdr:twoCellAnchor#{chart_ea_attr}#{chart_pub_attr}>"
71
- parts << anchor_xml("from", chart[:from_column] || chart[:from_col], chart[:from_row], col_off: chart[:from_col_off] || 0, row_off: chart[:from_row_off] || 0)
72
- parts << anchor_xml("to", chart[:to_column] || chart[:to_col], chart[:to_row], col_off: chart[:to_col_off] || 0, row_off: chart[:to_row_off] || 0)
73
- gf_macro = chart[:frame_macro] ? xml_escape(chart[:frame_macro]) : ""
74
- parts << %(<xdr:graphicFrame macro="#{xml_escape(gf_macro)}">)
219
+ person_id = c[:person_id] || c[:author_id] || "{00000000-0000-0000-0000-000000000000}"
220
+ comment_id = c[:id] || format_guid(nil)
221
+ attrs = {
222
+ ref: ref || "",
223
+ dT: dt_str,
224
+ personId: person_id,
225
+ id: comment_id,
226
+ parentId: c[:parent_id],
227
+ done: ([true, "true", "1"].include?(c[:done]) ? "true" : nil)
228
+ }
229
+ xml.tag("threadedComment", attrs) do
230
+ xml.text_tag("text", c[:text].to_s)
231
+ end
232
+ end
233
+ end
234
+ end
235
+ end
236
+
237
+ # Generates XML for xl/persons/person.xml.
238
+ # : (Array[Hash[Symbol, untyped]] persons) -> String
239
+ def generate_persons_xml(persons)
240
+ XmlBuilder.build(declaration: :plain) do |xml|
241
+ xml.tag("personList", { xmlns: THREADED_COMMENTS_NS }) do
242
+ persons.each do |p|
243
+ p_id = p[:id] || format_guid(nil)
244
+ name = p[:display_name] || p[:name] || "Author"
245
+ user_id = p[:user_id] || "AD:#{name}"
246
+ provider_id = p[:provider_id] || "AD"
247
+ xml.empty_tag("person", { displayName: name, id: p_id, userId: user_id, providerId: provider_id })
248
+ end
249
+ end
250
+ end
251
+ end
252
+
253
+ private
254
+
255
+ # : (XmlBuilder xml, untyped part) -> void
256
+ def write_pic_anchor_xml(xml, part)
257
+ img = part[:img]
258
+ rid = "rId#{part[:rid_index]}"
259
+ ea = img[:edit_as] || "oneCell"
260
+ xml.tag("xdr:twoCellAnchor", { editAs: ea, fPublished: (1 if img[:published]) }) do
261
+ write_anchor_xml(xml, "from", img[:from_column] || img[:from_col], img[:from_row], col_off: img[:from_col_off] || 0, row_off: img[:from_row_off] || 0)
262
+ write_anchor_xml(xml, "to", img[:to_column] || img[:to_col], img[:to_row], col_off: img[:to_col_off] || 0, row_off: img[:to_row_off] || 0)
263
+ xml.tag("xdr:pic", { macro: img[:macro] }) do
264
+ xml.tag("xdr:nvPicPr") do
265
+ xml.empty_tag(
266
+ "xdr:cNvPr",
267
+ {
268
+ id: part[:rid_index] + 1,
269
+ name: img[:name] || "",
270
+ descr: img[:description],
271
+ title: img[:title],
272
+ hidden: (1 if img[:hidden])
273
+ }
274
+ )
275
+ xml.tag("xdr:cNvPicPr") do
276
+ xml.empty_tag(
277
+ "a:picLocks",
278
+ {
279
+ noChangeAspect: (1 if img[:no_change_aspect]),
280
+ noCrop: (1 if img[:no_crop])
281
+ }
282
+ )
283
+ end
284
+ end
285
+ xml.tag("xdr:blipFill") do
286
+ if img[:alpha_mod_fix]
287
+ xml.tag("a:blip", { "r:embed": rid }) do
288
+ xml.empty_tag("a:alphaModFix", { amt: img[:alpha_mod_fix] })
289
+ end
290
+ else
291
+ xml.empty_tag("a:blip", { "r:embed": rid })
292
+ end
293
+ if (sr = img[:src_rect])
294
+ xml.empty_tag("a:srcRect", { t: sr[:top], b: sr[:bottom], l: sr[:left], r: sr[:right] })
295
+ end
296
+ xml.tag("a:stretch") { xml.empty_tag("a:fillRect") }
297
+ end
298
+ xml.tag("xdr:spPr") do
299
+ if img[:rotation]
300
+ xml.tag("a:xfrm", { rot: img[:rotation].to_i }) do
301
+ xml.empty_tag("a:off", { x: 0, y: 0 })
302
+ xml.empty_tag("a:ext", { cx: 0, cy: 0 })
303
+ end
304
+ end
305
+ xml.tag("a:prstGeom", { prst: "rect" }) { xml.empty_tag("a:avLst") }
306
+ if img[:line_color]
307
+ xml.tag("a:ln", { w: img[:line_width]&.to_i }) do
308
+ xml.tag("a:solidFill") { write_drawing_color_xml(xml, img[:line_color]) }
309
+ end
310
+ end
311
+ end
312
+ end
313
+ write_client_data_xml(xml, img)
314
+ end
315
+ end
316
+
317
+ # : (XmlBuilder xml, untyped part) -> void
318
+ def write_chart_anchor_xml(xml, part)
319
+ chart = part[:chart]
320
+ rid = "rId#{part[:rid_index]}"
321
+ xml.tag("xdr:twoCellAnchor", { editAs: chart[:edit_as], fPublished: (1 if chart[:published]) }) do
322
+ write_anchor_xml(xml, "from", chart[:from_column] || chart[:from_col], chart[:from_row], col_off: chart[:from_col_off] || 0, row_off: chart[:from_row_off] || 0)
323
+ write_anchor_xml(xml, "to", chart[:to_column] || chart[:to_col], chart[:to_row], col_off: chart[:to_col_off] || 0, row_off: chart[:to_row_off] || 0)
324
+ xml.tag("xdr:graphicFrame", { macro: chart[:frame_macro] || "" }) do
75
325
  chart_frame_name = chart[:name] || chart[:title] || "Chart"
76
- chart_descr_attr = chart[:description] ? %( descr="#{xml_escape(chart[:description])}") : ""
77
- chart_title_attr = chart[:frame_title] ? %( title="#{xml_escape(chart[:frame_title])}") : ""
78
- chart_hidden_attr = chart[:frame_hidden] ? ' hidden="1"' : ""
79
- cnv_gf_pr = if chart[:frame_no_grp]
80
- '<xdr:cNvGraphicFramePr><a:graphicFrameLocks noGrp="1"/></xdr:cNvGraphicFramePr>'
81
- else
82
- "<xdr:cNvGraphicFramePr/>"
83
- end
84
- parts << %(<xdr:nvGraphicFramePr><xdr:cNvPr id="#{xml_escape(dp[:rid_index] + 1)}" name="#{xml_escape(chart_frame_name)}"#{chart_descr_attr}#{chart_title_attr}#{chart_hidden_attr}/>#{cnv_gf_pr}</xdr:nvGraphicFramePr>)
85
- parts << '<xdr:xfrm><a:off x="0" y="0"/><a:ext cx="5000000" cy="3000000"/></xdr:xfrm>'
86
- parts << %(<a:graphic><a:graphicData uri="#{C_NS}"><c:chart xmlns:c="#{C_NS}" xmlns:r="#{DOC_REL_NS}" r:id="#{xml_escape(rid)}"/></a:graphicData></a:graphic>)
87
- parts << "</xdr:graphicFrame>"
88
- parts << client_data_xml(chart)
89
- parts << "</xdr:twoCellAnchor>"
90
- when :sp
91
- shape = dp[:shape]
92
- shape = shape.to_h if shape.is_a?(Elements::MetadataRecord)
93
- shape_ea_attr = shape[:edit_as] ? %( editAs="#{xml_escape(shape[:edit_as])}") : ""
94
- shape_pub_attr = shape[:published] ? ' fPublished="1"' : ""
95
- parts << "<xdr:twoCellAnchor#{shape_ea_attr}#{shape_pub_attr}>"
96
- parts << anchor_xml("from", shape[:from_column] || shape[:from_col], shape[:from_row], col_off: shape[:from_col_off] || 0, row_off: shape[:from_row_off] || 0)
97
- parts << anchor_xml("to", shape[:to_column] || shape[:to_col], shape[:to_row], col_off: shape[:to_col_off] || 0, row_off: shape[:to_row_off] || 0)
98
- sp_macro_attr = shape[:macro] ? %( macro="#{xml_escape(shape[:macro])}") : ""
99
- sp_textlink_attr = shape[:textlink] ? %( textlink="#{xml_escape(shape[:textlink])}") : ""
100
- parts << "<xdr:sp#{sp_macro_attr}#{sp_textlink_attr}>"
101
- shape_descr_attr = shape[:description] ? %( descr="#{xml_escape(shape[:description])}") : ""
102
- shape_title_attr = shape[:title] ? %( title="#{xml_escape(shape[:title])}") : ""
103
- shape_hidden_attr = shape[:hidden] ? ' hidden="1"' : ""
104
- sp_lock_attrs = +""
105
- sp_lock_attrs << ' noGrp="1"' if shape[:no_grp]
106
- sp_lock_attrs << ' noRot="1"' if shape[:no_rot]
107
- sp_lock_attrs << ' fLocksText="1"' if shape[:f_locks_text]
108
- cnv_sp_pr = if sp_lock_attrs.empty?
109
- "<xdr:cNvSpPr/>"
110
- else
111
- "<xdr:cNvSpPr><a:spLocks#{sp_lock_attrs}/></xdr:cNvSpPr>"
112
- end
113
- parts << %(<xdr:nvSpPr><xdr:cNvPr id="#{xml_escape(dp[:id])}" name="#{xml_escape(shape[:name])}"#{shape_descr_attr}#{shape_title_attr}#{shape_hidden_attr}/>#{cnv_sp_pr}</xdr:nvSpPr>)
114
- shape_fill_xml = if shape[:no_fill]
115
- "<a:noFill/>"
116
- elsif shape[:fill_color]
117
- "<a:solidFill>#{srgb_clr_xml(shape[:fill_color], alpha: shape[:fill_alpha], transforms: shape[:fill_color_transforms])}</a:solidFill>"
118
- elsif shape[:gradient_fill]
119
- gf = shape[:gradient_fill]
120
- gf_attrs = +""
121
- gf_attrs << %( rotWithShape="#{gf[:rot_with_shape] ? 1 : 0}") unless gf[:rot_with_shape].nil?
122
- gf_attrs << %( flip="#{xml_escape(gf[:flip])}") if gf[:flip]
123
- gs_xml = (gf[:stops] || []).map { |gs| %(<a:gs pos="#{xml_escape(gs[:pos])}">#{color_xml(gs[:color])}</a:gs>) }.join
124
- type_xml = if gf[:path]
125
- %(<a:path path="#{xml_escape(gf[:path])}"/>)
126
- elsif gf[:angle]
127
- scaled_attr = gf[:scaled] ? %( scaled="1") : %( scaled="0")
128
- %(<a:lin ang="#{xml_escape(gf[:angle])}"#{scaled_attr}/>)
129
- else
130
- ""
131
- end
132
- tile_xml = gf[:tile_rect] ? %(<a:tileRect#{gf[:tile_rect].map { |k, v| %( #{k}="#{xml_escape(v)}") }.join}/>) : ""
133
- "<a:gradFill#{gf_attrs}><a:gsLst>#{gs_xml}</a:gsLst>#{type_xml}#{tile_xml}</a:gradFill>"
134
- elsif shape[:pattern_fill]
135
- pf = shape[:pattern_fill]
136
- pf_children = +""
137
- pf_children << %(<a:fgClr>#{color_xml(pf[:fg_color])}</a:fgClr>) if pf[:fg_color]
138
- pf_children << %(<a:bgClr>#{color_xml(pf[:bg_color])}</a:bgClr>) if pf[:bg_color]
139
- %(<a:pattFill prst="#{xml_escape(pf[:preset])}">#{pf_children}</a:pattFill>)
140
- else
141
- ""
142
- end
143
- shape_line_xml = if shape[:no_line]
144
- "<a:ln><a:noFill/></a:ln>"
145
- elsif shape[:line_color] || shape[:line_dash] || shape[:line_custom_dash] || shape[:head_end] || shape[:tail_end] || shape[:line_cap] || shape[:line_align] || shape[:line_compound] || shape[:line_join]
146
- ln_attrs = +(shape[:line_width] ? %( w="#{shape[:line_width].to_i}") : "")
147
- ln_attrs << %( cap="#{xml_escape(shape[:line_cap])}") if shape[:line_cap]
148
- ln_attrs << %( algn="#{xml_escape(shape[:line_align])}") if shape[:line_align]
149
- ln_attrs << %( cmpd="#{xml_escape(shape[:line_compound])}") if shape[:line_compound]
150
- fill_part = if shape[:line_color]
151
- "<a:solidFill>#{srgb_clr_xml(shape[:line_color], alpha: shape[:line_alpha], transforms: shape[:line_color_transforms])}</a:solidFill>"
152
- else
153
- ""
154
- end
155
- dash_part = if shape[:line_custom_dash]
156
- ds_xml = shape[:line_custom_dash].map { |ds| %(<a:ds d="#{xml_escape(ds[:d])}" sp="#{xml_escape(ds[:sp])}"/>) }.join
157
- "<a:custDash>#{ds_xml}</a:custDash>"
158
- elsif shape[:line_dash]
159
- %(<a:prstDash val="#{xml_escape(shape[:line_dash])}"/>)
160
- else
161
- ""
162
- end
163
- join_part = case shape[:line_join]
164
- when "round" then "<a:round/>"
165
- when "bevel" then "<a:bevel/>"
166
- when "miter"
167
- shape[:line_miter_limit] ? "<a:miter lim=\"#{shape[:line_miter_limit].to_i}\"/>" : "<a:miter/>"
168
- else ""
169
- end
170
- head_part = build_line_end_xml("headEnd", shape[:head_end])
171
- tail_part = build_line_end_xml("tailEnd", shape[:tail_end])
172
- %(<a:ln#{ln_attrs}>#{fill_part}#{dash_part}#{join_part}#{head_part}#{tail_part}</a:ln>)
173
- else
174
- ""
175
- end
176
- shape_xfrm_xml = shape[:rotation] ? %(<a:xfrm rot="#{shape[:rotation].to_i}"><a:off x="0" y="0"/><a:ext cx="0" cy="0"/></a:xfrm>) : ""
177
- av_lst_xml = if shape[:adjust_values]&.any?
178
- gds = shape[:adjust_values].map { |gd| %(<a:gd name="#{xml_escape(gd[:name])}" fmla="#{xml_escape(gd[:fmla])}"/>) }
179
- "<a:avLst>#{gds.join}</a:avLst>"
180
- else
181
- "<a:avLst/>"
182
- end
183
- effect_lst_xml = if shape[:outer_shadow] || shape[:inner_shadow] || shape[:glow] || shape[:soft_edge] || shape[:reflection] || shape[:blur]
184
- effect_children = +""
185
- if shape[:outer_shadow]
186
- os = shape[:outer_shadow]
187
- os_attrs = +""
188
- os_attrs << %( blurRad="#{xml_escape(os[:blur_rad])}") if os[:blur_rad]
189
- os_attrs << %( dist="#{xml_escape(os[:dist])}") if os[:dist]
190
- os_attrs << %( dir="#{xml_escape(os[:dir])}") if os[:dir]
191
- os_attrs << %( algn="#{xml_escape(os[:algn])}") if os[:algn]
192
- os_attrs << %( rotWithShape="#{os[:rot_with_shape] ? 1 : 0}") unless os[:rot_with_shape].nil?
193
- os_color = os[:color] ? srgb_clr_xml(os[:color], alpha: os[:alpha]) : ""
194
- effect_children << "<a:outerShdw#{os_attrs}>#{os_color}</a:outerShdw>"
195
- end
196
- if shape[:glow]
197
- gl = shape[:glow]
198
- gl_attrs = +""
199
- gl_attrs << %( rad="#{xml_escape(gl[:rad])}") if gl[:rad]
200
- gl_color = gl[:color] ? srgb_clr_xml(gl[:color], alpha: gl[:alpha]) : ""
201
- effect_children << "<a:glow#{gl_attrs}>#{gl_color}</a:glow>"
202
- end
203
- if shape[:inner_shadow]
204
- is = shape[:inner_shadow]
205
- is_attrs = +""
206
- is_attrs << %( blurRad="#{xml_escape(is[:blur_rad])}") if is[:blur_rad]
207
- is_attrs << %( dist="#{xml_escape(is[:dist])}") if is[:dist]
208
- is_attrs << %( dir="#{xml_escape(is[:dir])}") if is[:dir]
209
- is_color = is[:color] ? srgb_clr_xml(is[:color], alpha: is[:alpha]) : ""
210
- effect_children << "<a:innerShdw#{is_attrs}>#{is_color}</a:innerShdw>"
211
- end
212
- if shape[:reflection]
213
- rf = shape[:reflection]
214
- rf_attrs = +""
215
- rf_attrs << %( blurRad="#{xml_escape(rf[:blur_rad])}") if rf[:blur_rad]
216
- rf_attrs << %( stA="#{xml_escape(rf[:st_a])}") if rf[:st_a]
217
- rf_attrs << %( endA="#{xml_escape(rf[:end_a])}") if rf[:end_a]
218
- rf_attrs << %( dist="#{xml_escape(rf[:dist])}") unless rf[:dist].nil?
219
- rf_attrs << %( dir="#{xml_escape(rf[:dir])}") if rf[:dir]
220
- rf_attrs << %( fadeDir="#{xml_escape(rf[:fade_dir])}") if rf[:fade_dir]
221
- rf_attrs << %( sx="#{xml_escape(rf[:sx])}") if rf[:sx]
222
- rf_attrs << %( sy="#{xml_escape(rf[:sy])}") if rf[:sy]
223
- rf_attrs << %( kx="#{xml_escape(rf[:kx])}") if rf[:kx]
224
- rf_attrs << %( ky="#{xml_escape(rf[:ky])}") if rf[:ky]
225
- rf_attrs << %( algn="#{xml_escape(rf[:algn])}") if rf[:algn]
226
- rf_attrs << %( rotWithShape="#{rf[:rot_with_shape] ? 1 : 0}") unless rf[:rot_with_shape].nil?
227
- effect_children << "<a:reflection#{rf_attrs}/>"
228
- end
229
- if shape[:soft_edge]
230
- se = shape[:soft_edge]
231
- effect_children << %(<a:softEdge rad="#{xml_escape(se[:rad])}"/>)
232
- end
233
- if shape[:blur]
234
- bl = shape[:blur]
235
- bl_attrs = +""
236
- bl_attrs << %( rad="#{xml_escape(bl[:rad])}") if bl[:rad]
237
- bl_attrs << %( grow="#{bl[:grow] ? 1 : 0}") unless bl[:grow].nil?
238
- effect_children << "<a:blur#{bl_attrs}/>"
239
- end
240
- "<a:effectLst>#{effect_children}</a:effectLst>"
241
- else
242
- ""
243
- end
244
- parts << %(<xdr:spPr>#{shape_xfrm_xml}<a:prstGeom prst="#{xml_escape(shape[:preset])}">#{av_lst_xml}</a:prstGeom>#{shape_fill_xml}#{shape_line_xml}#{effect_lst_xml}</xdr:spPr>)
245
- if shape[:text] || shape[:text_paragraphs]
246
- body_pr_attrs = +""
247
- body_pr_attrs << %( rot="#{xml_escape(shape[:text_rot])}") if shape[:text_rot]
248
- body_pr_attrs << %( spcFirstLastPara="1") if shape[:text_spc_first_last_para]
249
- body_pr_attrs << %( wrap="#{xml_escape(shape[:text_wrap])}") if shape[:text_wrap]
250
- body_pr_attrs << %( anchor="#{xml_escape(shape[:text_anchor])}") if shape[:text_anchor]
251
- body_pr_attrs << %( anchorCtr="1") if shape[:text_anchor_ctr]
252
- body_pr_attrs << %( vertOverflow="#{xml_escape(shape[:text_vert_overflow])}") if shape[:text_vert_overflow]
253
- body_pr_attrs << %( horzOverflow="#{xml_escape(shape[:text_horz_overflow])}") if shape[:text_horz_overflow]
254
- body_pr_attrs << %( numCol="#{xml_escape(shape[:text_num_col])}") if shape[:text_num_col]
255
- body_pr_attrs << %( spcCol="#{xml_escape(shape[:text_spc_col])}") if shape[:text_spc_col]
256
- body_pr_attrs << %( rtlCol="1") if shape[:text_rtl_col]
257
- body_pr_attrs << %( fromWordArt="1") if shape[:text_from_word_art]
258
- body_pr_attrs << %( upright="1") if shape[:text_upright]
259
- body_pr_attrs << %( compatLnSpc="1") if shape[:text_compat_ln_spc]
260
- body_pr_attrs << %( forceAA="1") if shape[:text_force_aa]
261
- body_pr_attrs << %( vert="#{xml_escape(shape[:text_vertical])}") if shape[:text_vertical]
262
- if shape[:text_insets]
263
- ins = shape[:text_insets]
264
- body_pr_attrs << %( lIns="#{xml_escape(ins[:left])}") if ins[:left]
265
- body_pr_attrs << %( tIns="#{xml_escape(ins[:top])}") if ins[:top]
266
- body_pr_attrs << %( rIns="#{xml_escape(ins[:right])}") if ins[:right]
267
- body_pr_attrs << %( bIns="#{xml_escape(ins[:bottom])}") if ins[:bottom]
326
+ xml.tag("xdr:nvGraphicFramePr") do
327
+ xml.empty_tag(
328
+ "xdr:cNvPr",
329
+ {
330
+ id: part[:rid_index] + 1,
331
+ name: chart_frame_name,
332
+ descr: chart[:description],
333
+ title: chart[:frame_title],
334
+ hidden: (1 if chart[:frame_hidden])
335
+ }
336
+ )
337
+ if chart[:frame_no_grp]
338
+ xml.tag("xdr:cNvGraphicFramePr") { xml.empty_tag("a:graphicFrameLocks", { noGrp: 1 }) }
339
+ else
340
+ xml.empty_tag("xdr:cNvGraphicFramePr")
268
341
  end
269
- body_pr_children = +""
270
- if shape[:text_warp]
271
- tw = shape[:text_warp]
272
- body_pr_children << %(<a:prstTxWarp prst="#{xml_escape(tw[:preset])}"><a:avLst/></a:prstTxWarp>)
342
+ end
343
+ xml.tag("xdr:xfrm") do
344
+ xml.empty_tag("a:off", { x: 0, y: 0 })
345
+ xml.empty_tag("a:ext", { cx: 5_000_000, cy: 3_000_000 })
346
+ end
347
+ xml.tag("a:graphic") do
348
+ xml.tag("a:graphicData", { uri: C_NS }) do
349
+ xml.empty_tag("c:chart", { "xmlns:c": C_NS, "xmlns:r": DOC_REL_NS, "r:id": rid })
273
350
  end
274
- autofit_xml = case shape[:autofit]
275
- when "none" then "<a:noAutofit/>"
276
- when "shape" then "<a:spAutoFit/>"
277
- when "normal" then "<a:normAutofit/>"
278
- when Hash
279
- af = shape[:autofit]
280
- case af[:type]
281
- when "normal"
282
- na_attrs = +""
283
- na_attrs << %( fontScale="#{xml_escape(af[:font_scale])}") if af[:font_scale]
284
- na_attrs << %( lnSpcReduction="#{xml_escape(af[:ln_spc_reduction])}") if af[:ln_spc_reduction]
285
- "<a:normAutofit#{na_attrs}/>"
286
- when "none" then "<a:noAutofit/>"
287
- when "shape" then "<a:spAutoFit/>"
288
- else ""
289
- end
290
- else ""
291
- end
292
- body_pr_children << autofit_xml
293
- body_pr_xml = if body_pr_children.empty?
294
- "<a:bodyPr#{body_pr_attrs}/>"
295
- else
296
- "<a:bodyPr#{body_pr_attrs}>#{body_pr_children}</a:bodyPr>"
297
- end
298
- parts << "<xdr:txBody>#{body_pr_xml}<a:lstStyle/>"
299
- if shape[:text_paragraphs]
300
- shape[:text_paragraphs].each { |para| parts << paragraph_xml(para) }
301
- elsif shape[:text]
302
- parts << paragraph_xml(
303
- text: shape[:text], font: shape[:text_font],
304
- align: shape[:text_align], font_align: shape[:text_font_align],
305
- def_tab_sz: shape[:text_def_tab_sz], rtl: shape[:text_rtl],
306
- ea_ln_brk: shape[:text_ea_ln_brk], latin_ln_brk: shape[:text_latin_ln_brk],
307
- hanging_punct: shape[:text_hanging_punct], level: shape[:text_level],
308
- indent: shape[:text_indent], spacing: shape[:text_spacing],
309
- tab_stops: shape[:text_tab_stops], bullet: shape[:text_bullet],
310
- def_rpr: shape[:text_def_rpr], end_para_rpr: shape[:text_end_para_rpr]
311
- )
351
+ end
352
+ end
353
+ write_client_data_xml(xml, chart)
354
+ end
355
+ end
356
+
357
+ # : (XmlBuilder xml, untyped part) -> void
358
+ def write_shape_anchor_xml(xml, part)
359
+ shape = part[:shape]
360
+ shape = shape.to_h if shape.is_a?(Elements::MetadataRecord)
361
+ xml.tag("xdr:twoCellAnchor", { editAs: shape[:edit_as], fPublished: (1 if shape[:published]) }) do
362
+ write_anchor_xml(xml, "from", shape[:from_column] || shape[:from_col], shape[:from_row], col_off: shape[:from_col_off] || 0, row_off: shape[:from_row_off] || 0)
363
+ write_anchor_xml(xml, "to", shape[:to_column] || shape[:to_col], shape[:to_row], col_off: shape[:to_col_off] || 0, row_off: shape[:to_row_off] || 0)
364
+ xml.tag("xdr:sp", { macro: shape[:macro], textlink: shape[:textlink] }) do
365
+ xml.tag("xdr:nvSpPr") do
366
+ xml.empty_tag(
367
+ "xdr:cNvPr",
368
+ {
369
+ id: part[:id] || "",
370
+ name: shape[:name] || "",
371
+ descr: shape[:description],
372
+ title: shape[:title],
373
+ hidden: (1 if shape[:hidden])
374
+ }
375
+ )
376
+ if shape[:no_grp] || shape[:no_rot] || shape[:f_locks_text]
377
+ xml.tag("xdr:cNvSpPr") do
378
+ xml.empty_tag(
379
+ "a:spLocks",
380
+ {
381
+ noGrp: (1 if shape[:no_grp]),
382
+ noRot: (1 if shape[:no_rot]),
383
+ fLocksText: (1 if shape[:f_locks_text])
384
+ }
385
+ )
386
+ end
387
+ else
388
+ xml.empty_tag("xdr:cNvSpPr")
389
+ end
390
+ end
391
+ xml.tag("xdr:spPr") do
392
+ if shape[:rotation]
393
+ xml.tag("a:xfrm", { rot: shape[:rotation].to_i }) do
394
+ xml.empty_tag("a:off", { x: 0, y: 0 })
395
+ xml.empty_tag("a:ext", { cx: 0, cy: 0 })
396
+ end
397
+ end
398
+ xml.tag("a:prstGeom", { prst: shape[:preset] || "" }) do
399
+ if shape[:adjust_values]&.any?
400
+ xml.tag("a:avLst") do
401
+ shape[:adjust_values].each do |gd|
402
+ xml.empty_tag("a:gd", { name: gd[:name] || "", fmla: gd[:fmla] || "" })
403
+ end
404
+ end
405
+ else
406
+ xml.empty_tag("a:avLst")
407
+ end
408
+ end
409
+ write_shape_fill_xml(xml, shape)
410
+ write_shape_line_xml(xml, shape)
411
+ write_effect_lst_xml(xml, shape)
412
+ end
413
+ write_shape_text_body_xml(xml, shape) if shape[:text] || shape[:text_paragraphs]
414
+ end
415
+ write_client_data_xml(xml, shape)
416
+ end
417
+ end
418
+
419
+ # : (XmlBuilder xml, untyped shape) -> void
420
+ def write_shape_fill_xml(xml, shape)
421
+ if shape[:no_fill]
422
+ xml.empty_tag("a:noFill")
423
+ elsif shape[:fill_color]
424
+ xml.tag("a:solidFill") do
425
+ write_drawing_color_xml(xml, shape[:fill_color], alpha: shape[:fill_alpha], transforms: shape[:fill_color_transforms])
426
+ end
427
+ elsif (gf = shape[:gradient_fill])
428
+ rot = xml_bool_attr(gf[:rot_with_shape])
429
+ xml.tag("a:gradFill", { rotWithShape: rot, flip: gf[:flip] }) do
430
+ xml.tag("a:gsLst") do
431
+ (gf[:stops] || []).each do |gs|
432
+ xml.tag("a:gs", { pos: gs[:pos] || "" }) { write_drawing_color_xml(xml, gs[:color]) }
433
+ end
434
+ end
435
+ if gf[:path]
436
+ xml.empty_tag("a:path", { path: gf[:path] })
437
+ elsif gf[:angle]
438
+ xml.empty_tag("a:lin", { ang: gf[:angle], scaled: gf[:scaled] ? 1 : 0 })
439
+ end
440
+ xml.empty_tag("a:tileRect", gf[:tile_rect]) if gf[:tile_rect]
441
+ end
442
+ elsif (pf = shape[:pattern_fill])
443
+ xml.tag("a:pattFill", { prst: pf[:preset] || "" }) do
444
+ xml.tag("a:fgClr") { write_drawing_color_xml(xml, pf[:fg_color]) } if pf[:fg_color]
445
+ xml.tag("a:bgClr") { write_drawing_color_xml(xml, pf[:bg_color]) } if pf[:bg_color]
446
+ end
447
+ end
448
+ end
449
+
450
+ # : (XmlBuilder xml, untyped shape) -> void
451
+ def write_shape_line_xml(xml, shape)
452
+ if shape[:no_line]
453
+ xml.tag("a:ln") { xml.empty_tag("a:noFill") }
454
+ elsif shape[:line_color] || shape[:line_dash] || shape[:line_custom_dash] || shape[:head_end] || shape[:tail_end] || shape[:line_cap] || shape[:line_align] || shape[:line_compound] || shape[:line_join]
455
+ xml.tag(
456
+ "a:ln",
457
+ {
458
+ w: shape[:line_width]&.to_i,
459
+ cap: shape[:line_cap],
460
+ algn: shape[:line_align],
461
+ cmpd: shape[:line_compound]
462
+ }
463
+ ) do
464
+ if shape[:line_color]
465
+ xml.tag("a:solidFill") do
466
+ write_drawing_color_xml(xml, shape[:line_color], alpha: shape[:line_alpha], transforms: shape[:line_color_transforms])
467
+ end
468
+ end
469
+ if shape[:line_custom_dash]
470
+ xml.tag("a:custDash") do
471
+ shape[:line_custom_dash].each do |ds|
472
+ xml.empty_tag("a:ds", { d: ds[:d] || "", sp: ds[:sp] || "" })
473
+ end
474
+ end
475
+ elsif shape[:line_dash]
476
+ xml.empty_tag("a:prstDash", { val: shape[:line_dash] })
477
+ end
478
+ write_line_join_xml(xml, shape[:line_join], shape[:line_miter_limit])
479
+ write_line_end_xml(xml, "headEnd", shape[:head_end])
480
+ write_line_end_xml(xml, "tailEnd", shape[:tail_end])
481
+ end
482
+ end
483
+ end
484
+
485
+ # : (XmlBuilder xml, untyped line_join, untyped miter_limit) -> void
486
+ def write_line_join_xml(xml, line_join, miter_limit)
487
+ case line_join
488
+ when "round" then xml.empty_tag("a:round")
489
+ when "bevel" then xml.empty_tag("a:bevel")
490
+ when "miter" then xml.empty_tag("a:miter", { lim: miter_limit&.to_i })
491
+ end
492
+ end
493
+
494
+ # : (XmlBuilder xml, untyped source) -> void
495
+ def write_effect_lst_xml(xml, source)
496
+ return unless source[:outer_shadow] || source[:inner_shadow] || source[:glow] || source[:soft_edge] || source[:reflection] || source[:blur]
497
+
498
+ xml.tag("a:effectLst") do
499
+ write_shadow_xml(xml, "a:outerShdw", source[:outer_shadow], include_algn: true) if source[:outer_shadow]
500
+ if (gl = source[:glow])
501
+ xml.tag("a:glow", { rad: gl[:rad] }) do
502
+ write_drawing_color_xml(xml, gl[:color], alpha: gl[:alpha]) if gl[:color]
503
+ end
504
+ end
505
+ write_shadow_xml(xml, "a:innerShdw", source[:inner_shadow], include_algn: false) if source[:inner_shadow]
506
+ if (rf = source[:reflection])
507
+ xml.empty_tag(
508
+ "a:reflection",
509
+ {
510
+ blurRad: rf[:blur_rad],
511
+ stA: rf[:st_a],
512
+ endA: rf[:end_a],
513
+ dist: rf[:dist],
514
+ dir: rf[:dir],
515
+ fadeDir: rf[:fade_dir],
516
+ sx: rf[:sx],
517
+ sy: rf[:sy],
518
+ kx: rf[:kx],
519
+ ky: rf[:ky],
520
+ algn: rf[:algn],
521
+ rotWithShape: xml_bool_attr(rf[:rot_with_shape])
522
+ }
523
+ )
524
+ end
525
+ xml.empty_tag("a:softEdge", { rad: source[:soft_edge][:rad] || "" }) if source[:soft_edge]
526
+ if (bl = source[:blur])
527
+ xml.empty_tag(
528
+ "a:blur",
529
+ {
530
+ rad: bl[:rad],
531
+ grow: xml_bool_attr(bl[:grow])
532
+ }
533
+ )
534
+ end
535
+ end
536
+ end
537
+
538
+ # : (XmlBuilder xml, ::String tag, untyped spec, include_algn: bool) -> void
539
+ def write_shadow_xml(xml, tag, spec, include_algn:)
540
+ attrs = { blurRad: spec[:blur_rad], dist: spec[:dist], dir: spec[:dir] }
541
+ if include_algn
542
+ attrs[:algn] = spec[:algn]
543
+ attrs[:rotWithShape] = (spec[:rot_with_shape] ? 1 : 0) unless spec[:rot_with_shape].nil?
544
+ end
545
+ xml.tag(tag, attrs) do
546
+ write_drawing_color_xml(xml, spec[:color], alpha: spec[:alpha]) if spec[:color]
547
+ end
548
+ end
549
+
550
+ # : (XmlBuilder xml, untyped shape) -> void
551
+ def write_shape_text_body_xml(xml, shape)
552
+ ins = shape[:text_insets]
553
+ body_pr_attrs = {
554
+ rot: shape[:text_rot],
555
+ spcFirstLastPara: (1 if shape[:text_spc_first_last_para]),
556
+ wrap: shape[:text_wrap],
557
+ anchor: shape[:text_anchor],
558
+ anchorCtr: (1 if shape[:text_anchor_ctr]),
559
+ vertOverflow: shape[:text_vert_overflow],
560
+ horzOverflow: shape[:text_horz_overflow],
561
+ numCol: shape[:text_num_col],
562
+ spcCol: shape[:text_spc_col],
563
+ rtlCol: (1 if shape[:text_rtl_col]),
564
+ fromWordArt: (1 if shape[:text_from_word_art]),
565
+ upright: (1 if shape[:text_upright]),
566
+ compatLnSpc: (1 if shape[:text_compat_ln_spc]),
567
+ forceAA: (1 if shape[:text_force_aa]),
568
+ vert: shape[:text_vertical],
569
+ lIns: ins&.[](:left),
570
+ tIns: ins&.[](:top),
571
+ rIns: ins&.[](:right),
572
+ bIns: ins&.[](:bottom)
573
+ }
574
+ autofit = shape[:autofit]
575
+ has_autofit = %w[none shape normal].include?(autofit) || (autofit.is_a?(Hash) && %w[normal none shape].include?(autofit[:type]))
576
+ xml.tag("xdr:txBody") do
577
+ if shape[:text_warp] || has_autofit
578
+ xml.tag("a:bodyPr", body_pr_attrs) do
579
+ if (tw = shape[:text_warp])
580
+ xml.tag("a:prstTxWarp", { prst: tw[:preset] || "" }) { xml.empty_tag("a:avLst") }
312
581
  end
313
- parts << "</xdr:txBody>"
582
+ write_autofit_xml(xml, autofit) if has_autofit
314
583
  end
315
- parts << "</xdr:sp>"
316
- parts << client_data_xml(shape)
317
- parts << "</xdr:twoCellAnchor>"
584
+ else
585
+ xml.empty_tag("a:bodyPr", body_pr_attrs)
586
+ end
587
+ xml.empty_tag("a:lstStyle")
588
+ if shape[:text_paragraphs]
589
+ shape[:text_paragraphs].each { |para| write_paragraph_xml(xml, para) }
590
+ elsif shape[:text]
591
+ write_paragraph_xml(
592
+ xml,
593
+ text: shape[:text], font: shape[:text_font],
594
+ align: shape[:text_align], font_align: shape[:text_font_align],
595
+ def_tab_sz: shape[:text_def_tab_sz], rtl: shape[:text_rtl],
596
+ ea_ln_brk: shape[:text_ea_ln_brk], latin_ln_brk: shape[:text_latin_ln_brk],
597
+ hanging_punct: shape[:text_hanging_punct], level: shape[:text_level],
598
+ indent: shape[:text_indent], spacing: shape[:text_spacing],
599
+ tab_stops: shape[:text_tab_stops], bullet: shape[:text_bullet],
600
+ def_rpr: shape[:text_def_rpr], end_para_rpr: shape[:text_end_para_rpr]
601
+ )
602
+ end
603
+ end
604
+ end
605
+
606
+ # : (XmlBuilder xml, untyped autofit) -> void
607
+ def write_autofit_xml(xml, autofit)
608
+ case autofit
609
+ when "none" then xml.empty_tag("a:noAutofit")
610
+ when "shape" then xml.empty_tag("a:spAutoFit")
611
+ when "normal" then xml.empty_tag("a:normAutofit")
612
+ when Hash
613
+ case autofit[:type]
614
+ when "normal"
615
+ xml.empty_tag("a:normAutofit", { fontScale: autofit[:font_scale], lnSpcReduction: autofit[:ln_spc_reduction] })
616
+ when "none"
617
+ xml.empty_tag("a:noAutofit")
618
+ when "shape"
619
+ xml.empty_tag("a:spAutoFit")
318
620
  end
319
621
  end
622
+ end
320
623
 
321
- parts << "</xdr:wsDr>"
322
- parts.join
323
- end
324
-
325
- # : (untyped para) -> ::String
326
- def paragraph_xml(para)
327
- ppr_attrs = +""
328
- ppr_attrs << %( algn="#{xml_escape(para[:align])}") if para[:align]
329
- ppr_attrs << %( fontAlgn="#{xml_escape(para[:font_align])}") if para[:font_align]
330
- ppr_attrs << %( defTabSz="#{xml_escape(para[:def_tab_sz])}") if para[:def_tab_sz]
331
- ppr_attrs << %( rtl="1") if para[:rtl]
332
- ppr_attrs << %( eaLnBrk="1") if para[:ea_ln_brk]
333
- ppr_attrs << %( latinLnBrk="1") if para[:latin_ln_brk]
334
- ppr_attrs << %( hangingPunct="1") if para[:hanging_punct]
335
- ppr_attrs << %( lvl="#{xml_escape(para[:level])}") if para[:level]
336
- if para[:indent]
337
- ti = para[:indent]
338
- ppr_attrs << %( marL="#{xml_escape(ti[:left])}") if ti[:left]
339
- ppr_attrs << %( marR="#{xml_escape(ti[:right])}") if ti[:right]
340
- ppr_attrs << %( indent="#{xml_escape(ti[:indent])}") if ti[:indent]
624
+ # : (XmlBuilder xml, untyped para) -> void
625
+ def write_paragraph_xml(xml, para)
626
+ ti = para[:indent]
627
+ ppr_attrs = {
628
+ algn: para[:align],
629
+ fontAlgn: para[:font_align],
630
+ defTabSz: para[:def_tab_sz],
631
+ rtl: (1 if para[:rtl]),
632
+ eaLnBrk: (1 if para[:ea_ln_brk]),
633
+ latinLnBrk: (1 if para[:latin_ln_brk]),
634
+ hangingPunct: (1 if para[:hanging_punct]),
635
+ lvl: para[:level],
636
+ marL: ti&.[](:left),
637
+ marR: ti&.[](:right),
638
+ indent: ti&.[](:indent)
639
+ }.compact
640
+ has_children = paragraph_ppr_has_children?(para)
641
+
642
+ xml.tag("a:p") do
643
+ if has_children
644
+ xml.tag("a:pPr", ppr_attrs) { write_paragraph_ppr_children_xml(xml, para) }
645
+ elsif !ppr_attrs.empty?
646
+ xml.empty_tag("a:pPr", ppr_attrs)
647
+ end
648
+ if para[:runs]
649
+ para[:runs].each do |run|
650
+ r_font = run.respond_to?(:font) ? run.font : run[:font]
651
+ r_text = run.respond_to?(:text) ? run.text : run[:text]
652
+ xml.tag("a:r") do
653
+ write_text_char_props_xml(xml, "a:rPr", r_font) if r_font
654
+ xml.text_tag("a:t", r_text || "")
655
+ end
656
+ end
657
+ else
658
+ xml.tag("a:r") do
659
+ write_text_char_props_xml(xml, "a:rPr", para[:font]) if para[:font]
660
+ xml.text_tag("a:t", para[:text] || "")
661
+ end
662
+ end
663
+ write_text_char_props_xml(xml, "a:endParaRPr", para[:end_para_rpr]) if para[:end_para_rpr]
341
664
  end
342
- ppr_children = +""
343
- if para[:spacing]
344
- ts = para[:spacing]
665
+ end
666
+
667
+ # : (untyped para) -> bool
668
+ def paragraph_ppr_has_children?(para)
669
+ return true if para[:tab_stops]&.any? || para[:def_rpr]
670
+
671
+ ts = para[:spacing]
672
+ return true if ts && (ts[:line] || ts[:line_pct] || ts[:before] || ts[:before_pct] || ts[:after] || ts[:after_pct])
673
+
674
+ bu = para[:bullet]
675
+ return true if bu && (bu[:color] || bu[:size_pts] || bu[:size_pct] || bu[:font] || %w[none char auto].include?(bu[:type]))
676
+
677
+ false
678
+ end
679
+
680
+ # : (XmlBuilder xml, untyped para) -> void
681
+ def write_paragraph_ppr_children_xml(xml, para)
682
+ if (ts = para[:spacing])
345
683
  if ts[:line]
346
- ppr_children << %(<a:lnSpc><a:spcPts val="#{xml_escape(ts[:line])}"/></a:lnSpc>)
684
+ xml.tag("a:lnSpc") { xml.empty_tag("a:spcPts", { val: ts[:line] }) }
347
685
  elsif ts[:line_pct]
348
- ppr_children << %(<a:lnSpc><a:spcPct val="#{xml_escape(ts[:line_pct])}"/></a:lnSpc>)
686
+ xml.tag("a:lnSpc") { xml.empty_tag("a:spcPct", { val: ts[:line_pct] }) }
349
687
  end
350
688
  if ts[:before]
351
- ppr_children << %(<a:spcBef><a:spcPts val="#{xml_escape(ts[:before])}"/></a:spcBef>)
689
+ xml.tag("a:spcBef") { xml.empty_tag("a:spcPts", { val: ts[:before] }) }
352
690
  elsif ts[:before_pct]
353
- ppr_children << %(<a:spcBef><a:spcPct val="#{xml_escape(ts[:before_pct])}"/></a:spcBef>)
691
+ xml.tag("a:spcBef") { xml.empty_tag("a:spcPct", { val: ts[:before_pct] }) }
354
692
  end
355
693
  if ts[:after]
356
- ppr_children << %(<a:spcAft><a:spcPts val="#{xml_escape(ts[:after])}"/></a:spcAft>)
694
+ xml.tag("a:spcAft") { xml.empty_tag("a:spcPts", { val: ts[:after] }) }
357
695
  elsif ts[:after_pct]
358
- ppr_children << %(<a:spcAft><a:spcPct val="#{xml_escape(ts[:after_pct])}"/></a:spcAft>)
696
+ xml.tag("a:spcAft") { xml.empty_tag("a:spcPct", { val: ts[:after_pct] }) }
359
697
  end
360
698
  end
361
699
  if para[:tab_stops]&.any?
362
- tabs = para[:tab_stops].map do |tab|
363
- tab_attrs = %( pos="#{xml_escape(tab[:pos])}")
364
- tab_attrs << %( algn="#{xml_escape(tab[:align])}") if tab[:align]
365
- "<a:tab#{tab_attrs}/>"
700
+ xml.tag("a:tabLst") do
701
+ para[:tab_stops].each do |tab|
702
+ xml.empty_tag("a:tab", { pos: tab[:pos] || "", algn: tab[:align] })
703
+ end
366
704
  end
367
- ppr_children << "<a:tabLst>#{tabs.join}</a:tabLst>"
368
705
  end
369
- if para[:bullet]
370
- bu = para[:bullet]
371
- ppr_children << %(<a:buClr>#{color_xml(bu[:color])}</a:buClr>) if bu[:color]
372
- ppr_children << %(<a:buSzPts val="#{xml_escape(bu[:size_pts])}"/>) if bu[:size_pts]
373
- ppr_children << %(<a:buSzPct val="#{xml_escape(bu[:size_pct])}"/>) if bu[:size_pct]
374
- ppr_children << %(<a:buFont typeface="#{xml_escape(bu[:font])}"/>) if bu[:font]
706
+ if (bu = para[:bullet])
707
+ xml.tag("a:buClr") { write_drawing_color_xml(xml, bu[:color]) } if bu[:color]
708
+ xml.empty_tag("a:buSzPts", { val: bu[:size_pts] }) if bu[:size_pts]
709
+ xml.empty_tag("a:buSzPct", { val: bu[:size_pct] }) if bu[:size_pct]
710
+ xml.empty_tag("a:buFont", { typeface: bu[:font] }) if bu[:font]
375
711
  case bu[:type]
376
712
  when "none"
377
- ppr_children << "<a:buNone/>"
713
+ xml.empty_tag("a:buNone")
378
714
  when "char"
379
- ppr_children << %(<a:buChar char="#{xml_escape(bu[:char])}"/>)
715
+ xml.empty_tag("a:buChar", { char: bu[:char] || "" })
380
716
  when "auto"
381
- auto_attrs = %( type="#{xml_escape(bu[:auto_type])}")
382
- auto_attrs << %( startAt="#{xml_escape(bu[:start_at])}") if bu[:start_at]
383
- ppr_children << "<a:buAutoNum#{auto_attrs}/>"
717
+ xml.empty_tag("a:buAutoNum", { type: bu[:auto_type] || "", startAt: bu[:start_at] })
384
718
  end
385
719
  end
386
- ppr_children << text_char_props_xml("a:defRPr", para[:def_rpr]) if para[:def_rpr]
387
- ppr_xml = if ppr_attrs.empty? && ppr_children.empty?
388
- ""
389
- elsif ppr_children.empty?
390
- "<a:pPr#{ppr_attrs}/>"
391
- else
392
- "<a:pPr#{ppr_attrs}>#{ppr_children}</a:pPr>"
393
- end
394
- end_para_rpr_xml = para[:end_para_rpr] ? text_char_props_xml("a:endParaRPr", para[:end_para_rpr]) : ""
395
- runs_xml = if para[:runs]
396
- para[:runs].map do |run|
397
- r_font = run.respond_to?(:font) ? run.font : run[:font]
398
- r_text = run.respond_to?(:text) ? run.text : run[:text]
399
- run_rpr = r_font ? text_char_props_xml("a:rPr", r_font) : ""
400
- "<a:r>#{run_rpr}<a:t>#{xml_escape(r_text || "")}</a:t></a:r>"
401
- end.join
402
- else
403
- rpr_xml = para[:font] ? text_char_props_xml("a:rPr", para[:font]) : ""
404
- "<a:r>#{rpr_xml}<a:t>#{xml_escape(para[:text] || "")}</a:t></a:r>"
405
- end
406
- "<a:p>#{ppr_xml}#{runs_xml}#{end_para_rpr_xml}</a:p>"
407
- end
408
-
409
- # : (untyped tag, untyped font) -> untyped
410
- def text_char_props_xml(tag, font)
411
- attrs = +""
412
- attrs << %( b="1") if font[:bold]
413
- attrs << %( i="1") if font[:italic]
414
- attrs << %( noProof="1") if font[:no_proof]
415
- attrs << %( normalizeH="1") if font[:normalize_h]
416
- attrs << %( kumimoji="1") if font[:kumimoji]
417
- attrs << %( sz="#{xml_escape(font[:size])}") if font[:size]
418
- attrs << %( strike="#{xml_escape(font[:strike])}") if font[:strike]
419
- attrs << %( u="#{xml_escape(font[:underline])}") if font[:underline]
420
- attrs << %( baseline="#{xml_escape(font[:baseline])}") if font[:baseline]
421
- attrs << %( spc="#{xml_escape(font[:spacing])}") if font[:spacing]
422
- attrs << %( kern="#{xml_escape(font[:kern])}") if font[:kern]
423
- attrs << %( cap="#{xml_escape(font[:cap])}") if font[:cap]
424
- attrs << %( lang="#{xml_escape(font[:lang])}") if font[:lang]
425
- attrs << %( altLang="#{xml_escape(font[:alt_lang])}") if font[:alt_lang]
426
- attrs << %( dirty="1") if font[:dirty]
427
- attrs << %( smtClean="1") if font[:smt_clean]
428
- attrs << %( err="1") if font[:err]
429
- attrs << %( bmk="#{xml_escape(font[:bmk])}") if font[:bmk]
430
- children = +""
431
- if font[:line_color] || font[:line_width] || font[:line_dash] || font[:line_cap] || font[:line_join]
432
- ln_attrs = +(font[:line_width] ? %( w="#{font[:line_width].to_i}") : "")
433
- ln_attrs << %( cap="#{xml_escape(font[:line_cap])}") if font[:line_cap]
434
- ln_fill = font[:line_color] ? %(<a:solidFill>#{color_xml(font[:line_color])}</a:solidFill>) : ""
435
- ln_dash = font[:line_dash] ? %(<a:prstDash val="#{xml_escape(font[:line_dash])}"/>) : ""
436
- ln_join = case font[:line_join]
437
- when "round" then "<a:round/>"
438
- when "bevel" then "<a:bevel/>"
439
- when "miter"
440
- font[:line_miter_limit] ? "<a:miter lim=\"#{font[:line_miter_limit].to_i}\"/>" : "<a:miter/>"
441
- else ""
442
- end
443
- children << "<a:ln#{ln_attrs}>#{ln_fill}#{ln_dash}#{ln_join}</a:ln>"
444
- end
445
- children << %(<a:solidFill>#{color_xml(font[:color])}</a:solidFill>) if font[:color]
446
- if font[:outer_shadow] || font[:inner_shadow] || font[:glow] || font[:soft_edge] || font[:reflection] || font[:blur]
447
- eff = +""
448
- if font[:outer_shadow]
449
- os = font[:outer_shadow]
450
- oa = +""
451
- oa << %( blurRad="#{xml_escape(os[:blur_rad])}") if os[:blur_rad]
452
- oa << %( dist="#{xml_escape(os[:dist])}") if os[:dist]
453
- oa << %( dir="#{xml_escape(os[:dir])}") if os[:dir]
454
- oa << %( algn="#{xml_escape(os[:algn])}") if os[:algn]
455
- oa << %( rotWithShape="#{os[:rot_with_shape] ? 1 : 0}") unless os[:rot_with_shape].nil?
456
- oc = os[:color] ? srgb_clr_xml(os[:color], alpha: os[:alpha]) : ""
457
- eff << "<a:outerShdw#{oa}>#{oc}</a:outerShdw>"
458
- end
459
- if font[:glow]
460
- gl = font[:glow]
461
- ga = +(gl[:rad] ? %( rad="#{xml_escape(gl[:rad])}") : "")
462
- gc = gl[:color] ? srgb_clr_xml(gl[:color], alpha: gl[:alpha]) : ""
463
- eff << "<a:glow#{ga}>#{gc}</a:glow>"
464
- end
465
- if font[:inner_shadow]
466
- is = font[:inner_shadow]
467
- ia = +""
468
- ia << %( blurRad="#{xml_escape(is[:blur_rad])}") if is[:blur_rad]
469
- ia << %( dist="#{xml_escape(is[:dist])}") if is[:dist]
470
- ia << %( dir="#{xml_escape(is[:dir])}") if is[:dir]
471
- ic = is[:color] ? srgb_clr_xml(is[:color], alpha: is[:alpha]) : ""
472
- eff << "<a:innerShdw#{ia}>#{ic}</a:innerShdw>"
473
- end
474
- if font[:reflection]
475
- rf = font[:reflection]
476
- ra = +""
477
- ra << %( blurRad="#{xml_escape(rf[:blur_rad])}") if rf[:blur_rad]
478
- ra << %( stA="#{xml_escape(rf[:st_a])}") if rf[:st_a]
479
- ra << %( endA="#{xml_escape(rf[:end_a])}") if rf[:end_a]
480
- ra << %( dist="#{xml_escape(rf[:dist])}") unless rf[:dist].nil?
481
- ra << %( dir="#{xml_escape(rf[:dir])}") if rf[:dir]
482
- ra << %( fadeDir="#{xml_escape(rf[:fade_dir])}") if rf[:fade_dir]
483
- ra << %( sx="#{xml_escape(rf[:sx])}") if rf[:sx]
484
- ra << %( sy="#{xml_escape(rf[:sy])}") if rf[:sy]
485
- ra << %( kx="#{xml_escape(rf[:kx])}") if rf[:kx]
486
- ra << %( ky="#{xml_escape(rf[:ky])}") if rf[:ky]
487
- ra << %( algn="#{xml_escape(rf[:algn])}") if rf[:algn]
488
- ra << %( rotWithShape="#{rf[:rot_with_shape] ? 1 : 0}") unless rf[:rot_with_shape].nil?
489
- eff << "<a:reflection#{ra}/>"
490
- end
491
- if font[:soft_edge]
492
- se = font[:soft_edge]
493
- eff << %(<a:softEdge rad="#{xml_escape(se[:rad])}"/>)
494
- end
495
- if font[:blur]
496
- bl = font[:blur]
497
- ba = +(bl[:rad] ? %( rad="#{xml_escape(bl[:rad])}") : "")
498
- ba << %( grow="#{bl[:grow] ? 1 : 0}") unless bl[:grow].nil?
499
- eff << "<a:blur#{ba}/>"
500
- end
501
- children << "<a:effectLst>#{eff}</a:effectLst>"
720
+ write_text_char_props_xml(xml, "a:defRPr", para[:def_rpr]) if para[:def_rpr]
721
+ end
722
+
723
+ # : (XmlBuilder xml, ::String tag, untyped font) -> void
724
+ def write_text_char_props_xml(xml, tag, font)
725
+ attrs = {
726
+ b: (1 if font[:bold]),
727
+ i: (1 if font[:italic]),
728
+ noProof: (1 if font[:no_proof]),
729
+ normalizeH: (1 if font[:normalize_h]),
730
+ kumimoji: (1 if font[:kumimoji]),
731
+ sz: font[:size],
732
+ strike: font[:strike],
733
+ u: font[:underline],
734
+ baseline: font[:baseline],
735
+ spc: font[:spacing],
736
+ kern: font[:kern],
737
+ cap: font[:cap],
738
+ lang: font[:lang],
739
+ altLang: font[:alt_lang],
740
+ dirty: (1 if font[:dirty]),
741
+ smtClean: (1 if font[:smt_clean]),
742
+ err: (1 if font[:err]),
743
+ bmk: font[:bmk]
744
+ }
745
+ has_ln = font[:line_color] || font[:line_width] || font[:line_dash] || font[:line_cap] || font[:line_join]
746
+ has_eff = font[:outer_shadow] || font[:inner_shadow] || font[:glow] || font[:soft_edge] || font[:reflection] || font[:blur]
747
+ has_children = has_ln || font[:color] || has_eff || font[:highlight] || font[:u_ln_tx] || font[:u_fill_tx] || font[:name] || font[:ea_font] || font[:cs_font] || font[:sym_font]
748
+
749
+ unless has_children
750
+ xml.empty_tag(tag, attrs)
751
+ return
502
752
  end
503
- children << %(<a:highlight>#{color_xml(font[:highlight])}</a:highlight>) if font[:highlight]
504
- children << "<a:uLnTx/>" if font[:u_ln_tx]
505
- children << "<a:uFillTx/>" if font[:u_fill_tx]
506
- children << %(<a:latin typeface="#{xml_escape(font[:name])}"/>) if font[:name]
507
- children << %(<a:ea typeface="#{xml_escape(font[:ea_font])}"/>) if font[:ea_font]
508
- children << %(<a:cs typeface="#{xml_escape(font[:cs_font])}"/>) if font[:cs_font]
509
- children << %(<a:sym typeface="#{xml_escape(font[:sym_font])}"/>) if font[:sym_font]
510
- if children.empty?
511
- "<#{tag}#{attrs}/>"
512
- else
513
- "<#{tag}#{attrs}>#{children}</#{tag}>"
753
+
754
+ xml.tag(tag, attrs) do
755
+ if has_ln
756
+ xml.tag("a:ln", { w: font[:line_width]&.to_i, cap: font[:line_cap] }) do
757
+ xml.tag("a:solidFill") { write_drawing_color_xml(xml, font[:line_color]) } if font[:line_color]
758
+ xml.empty_tag("a:prstDash", { val: font[:line_dash] }) if font[:line_dash]
759
+ write_line_join_xml(xml, font[:line_join], font[:line_miter_limit])
760
+ end
761
+ end
762
+ xml.tag("a:solidFill") { write_drawing_color_xml(xml, font[:color]) } if font[:color]
763
+ write_effect_lst_xml(xml, font)
764
+ xml.tag("a:highlight") { write_drawing_color_xml(xml, font[:highlight]) } if font[:highlight]
765
+ xml.empty_tag("a:uLnTx") if font[:u_ln_tx]
766
+ xml.empty_tag("a:uFillTx") if font[:u_fill_tx]
767
+ xml.empty_tag("a:latin", { typeface: font[:name] }) if font[:name]
768
+ xml.empty_tag("a:ea", { typeface: font[:ea_font] }) if font[:ea_font]
769
+ xml.empty_tag("a:cs", { typeface: font[:cs_font] }) if font[:cs_font]
770
+ xml.empty_tag("a:sym", { typeface: font[:sym_font] }) if font[:sym_font]
514
771
  end
515
772
  end
516
773
 
517
- # : (untyped tag, untyped col, untyped row, ?col_off: ::Integer, ?row_off: ::Integer) -> ::String
518
- def anchor_xml(tag, col, row, col_off: 0, row_off: 0)
774
+ # : (XmlBuilder xml, ::String tag, untyped col, untyped row, ?col_off: ::Integer, ?row_off: ::Integer) -> void
775
+ def write_anchor_xml(xml, tag, col, row, col_off: 0, row_off: 0)
519
776
  c = col.to_s.empty? ? 0 : col.to_i
520
777
  r = row.to_s.empty? ? 0 : row.to_i
521
778
  co = col_off.to_s.empty? ? 0 : col_off.to_i
522
779
  ro = row_off.to_s.empty? ? 0 : row_off.to_i
523
- "<xdr:#{tag}><xdr:col>#{c}</xdr:col><xdr:colOff>#{co}</xdr:colOff><xdr:row>#{r}</xdr:row><xdr:rowOff>#{ro}</xdr:rowOff></xdr:#{tag}>"
524
- end
525
-
526
- # : (untyped obj) -> ::String
527
- def client_data_xml(obj)
528
- cd_attrs = +""
529
- cd_attrs << ' fLocksWithSheet="0"' if obj[:locks_with_sheet] == false
530
- cd_attrs << ' fLocksWithSheet="1"' if obj[:locks_with_sheet] == true
531
- cd_attrs << ' fPrintsWithSheet="0"' if obj[:prints_with_sheet] == false
532
- cd_attrs << ' fPrintsWithSheet="1"' if obj[:prints_with_sheet] == true
533
- "<xdr:clientData#{cd_attrs}/>"
780
+ xml.tag("xdr:#{tag}") do
781
+ xml.text_tag("xdr:col", c)
782
+ xml.text_tag("xdr:colOff", co)
783
+ xml.text_tag("xdr:row", r)
784
+ xml.text_tag("xdr:rowOff", ro)
785
+ end
534
786
  end
535
787
 
536
- # : (untyped rels_data) -> untyped
537
- def generate_drawing_rels(rels_data)
538
- parts = [XML_HEADER, %(<Relationships xmlns="#{REL_NS}">)]
539
- rels_data.each_with_index do |rel, i|
540
- rel_type = case rel[:type]
541
- when :image then "#{DOC_REL_NS}/image"
542
- when :chart then "#{DOC_REL_NS}/chart"
543
- end
544
- parts << %(<Relationship Id="rId#{i + 1}" Type="#{xml_escape(rel_type)}" Target="#{xml_escape(rel[:target])}"/>)
545
- end
546
- parts << "</Relationships>"
547
- parts.join
788
+ # : (XmlBuilder xml, untyped obj) -> void
789
+ def write_client_data_xml(xml, obj)
790
+ locks = case obj[:locks_with_sheet]
791
+ when false then 0
792
+ when true then 1
793
+ end
794
+ prints = case obj[:prints_with_sheet]
795
+ when false then 0
796
+ when true then 1
797
+ end
798
+ xml.empty_tag("xdr:clientData", { fLocksWithSheet: locks, fPrintsWithSheet: prints })
548
799
  end
549
800
 
550
- # : (untyped title_spec, ?overlay: bool) -> untyped
551
- def build_chart_title_xml(title_spec, overlay: false)
801
+ # : (XmlBuilder xml, untyped title_spec, ?overlay: bool) -> void
802
+ def write_chart_title_xml(xml, title_spec, overlay: false)
552
803
  overlay_val = overlay ? 1 : 0
553
- if title_spec.is_a?(Hash)
554
- text = xml_escape(title_spec[:text].to_s)
555
- rotation = title_spec[:rotation]
556
- body_pr = rotation ? %(<a:bodyPr rot="#{xml_escape(rotation)}"/>) : "<a:bodyPr/>"
557
- font = title_spec[:font] || {}
558
- rpr_attrs = +""
559
- rpr_attrs << %( b="1") if font[:bold]
560
- rpr_attrs << %( i="1") if font[:italic]
561
- rpr_attrs << %( sz="#{xml_escape(font[:size])}") if font[:size]
562
- rpr_children = +""
563
- rpr_children << %(<a:solidFill>#{color_xml(font[:color])}</a:solidFill>) if font[:color]
564
- rpr_children << %(<a:latin typeface="#{xml_escape(font[:name])}"/>) if font[:name]
565
- rpr_xml = if rpr_children.empty?
566
- "<a:rPr#{rpr_attrs}/>"
804
+ xml.tag("c:title") do
805
+ if title_spec.is_a?(Hash)
806
+ text = title_spec[:text].to_s
807
+ rotation = title_spec[:rotation]
808
+ font = title_spec[:font] || {}
809
+ xml.tag("c:tx") do
810
+ xml.tag("c:rich") do
811
+ xml.empty_tag("a:bodyPr", { rot: rotation })
812
+ xml.empty_tag("a:lstStyle")
813
+ xml.tag("a:p") do
814
+ xml.tag("a:r") do
815
+ rpr_attrs = { b: (1 if font[:bold]), i: (1 if font[:italic]), sz: font[:size] }
816
+ if font[:color] || font[:name]
817
+ xml.tag("a:rPr", rpr_attrs) do
818
+ xml.tag("a:solidFill") { write_drawing_color_xml(xml, font[:color]) } if font[:color]
819
+ xml.empty_tag("a:latin", { typeface: font[:name] }) if font[:name]
820
+ end
567
821
  else
568
- "<a:rPr#{rpr_attrs}>#{rpr_children}</a:rPr>"
822
+ xml.empty_tag("a:rPr", rpr_attrs)
569
823
  end
570
- layout_xml = build_title_layout_xml(title_spec[:layout])
571
- sp_xml = build_title_sp_pr(title_spec)
572
- "<c:title><c:tx><c:rich>#{body_pr}<a:lstStyle/><a:p><a:r>#{rpr_xml}<a:t>#{text}</a:t></a:r></a:p></c:rich></c:tx>#{layout_xml}<c:overlay val=\"#{xml_escape(overlay_val)}\"/>#{sp_xml}</c:title>"
573
- else
574
- "<c:title><c:tx><c:rich><a:bodyPr/><a:lstStyle/><a:p><a:r><a:t>#{xml_escape(title_spec)}</a:t></a:r></a:p></c:rich></c:tx><c:overlay val=\"#{xml_escape(overlay_val)}\"/></c:title>"
824
+ xml.text_tag("a:t", text)
825
+ end
826
+ end
827
+ end
828
+ end
829
+ write_title_layout_xml(xml, title_spec[:layout])
830
+ xml.empty_tag("c:overlay", { val: overlay_val })
831
+ write_title_sp_pr(xml, title_spec)
832
+ else
833
+ xml.tag("c:tx") do
834
+ xml.tag("c:rich") do
835
+ xml.empty_tag("a:bodyPr")
836
+ xml.empty_tag("a:lstStyle")
837
+ xml.tag("a:p") { xml.tag("a:r") { xml.text_tag("a:t", title_spec || "") } }
838
+ end
839
+ end
840
+ xml.empty_tag("c:overlay", { val: overlay_val })
841
+ end
575
842
  end
576
843
  end
577
844
 
@@ -598,50 +865,30 @@ module Xlsxrb
598
865
  spec
599
866
  end
600
867
 
601
- # : (untyped spec) -> untyped
602
- def build_title_sp_pr(spec)
603
- children = +""
604
- children << %(<a:solidFill>#{color_xml(spec[:fill_color])}</a:solidFill>) if spec[:fill_color]
605
- children << "<a:noFill/>" if spec[:no_fill]
606
- if spec[:line_color] || spec[:line_width] || spec[:line_dash]
607
- lw = spec[:line_width] ? %( w="#{(spec[:line_width] * 12_700).to_i}") : ""
608
- lf = spec[:line_color] ? %(<a:solidFill>#{color_xml(spec[:line_color])}</a:solidFill>) : ""
609
- ld = spec[:line_dash] ? %(<a:prstDash val="#{xml_escape(spec[:line_dash])}"/>) : ""
610
- children << "<a:ln#{lw}>#{lf}#{ld}</a:ln>"
611
- end
612
- children.empty? ? "" : "<c:spPr>#{children}</c:spPr>"
868
+ # : (XmlBuilder xml, untyped spec) -> void
869
+ def write_title_sp_pr(xml, spec)
870
+ write_simple_sp_pr_from_hash(xml, spec)
613
871
  end
614
872
 
615
- # : (untyped layout) -> ("" | untyped)
616
- def build_title_layout_xml(layout)
617
- return "" unless layout.is_a?(Hash)
618
-
619
- ml = +""
620
- ml << %(<c:x val="#{xml_escape(layout[:x])}"/>) if layout[:x]
621
- ml << %(<c:y val="#{xml_escape(layout[:y])}"/>) if layout[:y]
622
- ml << %(<c:w val="#{xml_escape(layout[:w])}"/>) if layout[:w]
623
- ml << %(<c:h val="#{xml_escape(layout[:h])}"/>) if layout[:h]
624
- ml.empty? ? "" : "<c:layout><c:manualLayout>#{ml}</c:manualLayout></c:layout>"
625
- end
873
+ # : (XmlBuilder xml, untyped layout) -> void
874
+ def write_title_layout_xml(xml, layout)
875
+ return unless layout.is_a?(Hash)
626
876
 
627
- # : (untyped tag, untyped spec) -> ("" | untyped)
628
- def gridlines_xml(tag, spec)
629
- return "" unless spec
877
+ attrs = { x: layout[:x], y: layout[:y], w: layout[:w], h: layout[:h] }.compact
878
+ return if attrs.empty?
630
879
 
631
- if spec.is_a?(Hash)
632
- sp_children = +""
633
- if spec[:line_color] || spec[:line_width] || spec[:line_dash]
634
- lw = spec[:line_width] ? %( w="#{(spec[:line_width] * 12_700).to_i}") : ""
635
- lf = spec[:line_color] ? %(<a:solidFill>#{color_xml(spec[:line_color])}</a:solidFill>) : ""
636
- ld = spec[:line_dash] ? %(<a:prstDash val="#{xml_escape(spec[:line_dash])}"/>) : ""
637
- sp_children << "<a:ln#{lw}>#{lf}#{ld}</a:ln>"
880
+ xml.tag("c:layout") do
881
+ xml.tag("c:manualLayout") do
882
+ attrs.each { |k, v| xml.empty_tag("c:#{k}", { val: v }) }
638
883
  end
639
- sp_children.empty? ? "<c:#{tag}/>" : "<c:#{tag}><c:spPr>#{sp_children}</c:spPr></c:#{tag}>"
640
- else
641
- "<c:#{tag}/>"
642
884
  end
643
885
  end
644
886
 
887
+ # : (XmlBuilder xml, ::String tag, untyped spec) -> void
888
+ def write_gridlines_xml(xml, tag, spec)
889
+ write_line_sp_tag(xml, "c:#{tag}", spec)
890
+ end
891
+
645
892
  # Resolves a cell reference like "Sheet1!$A$1:$A$5" to an array of values from @sheets.
646
893
  # : (untyped ref) -> (nil | untyped)
647
894
  def resolve_sheet_ref(ref)
@@ -649,10 +896,8 @@ module Xlsxrb
649
896
 
650
897
  m = if ref.start_with?("'")
651
898
  ref.match(/\A'([^']+)'!(.+)\z/)
652
-
653
899
  else
654
900
  ref.match(/\A([^!]+)!(.+)\z/)
655
-
656
901
  end
657
902
  return nil unless m
658
903
 
@@ -689,54 +934,61 @@ module Xlsxrb
689
934
  cells
690
935
  end
691
936
 
692
- # : (untyped ref) -> ("" | ::String)
693
- def num_cache_xml(ref)
937
+ # : (XmlBuilder xml, untyped ref) -> void
938
+ def write_num_cache_xml(xml, ref)
694
939
  values = resolve_sheet_ref(ref)
695
- return "" unless values
940
+ return unless values
696
941
 
697
- pts = values.each_with_index.filter_map do |v, i|
698
- next unless v
942
+ xml.tag("c:numCache") do
943
+ xml.text_tag("c:formatCode", "General")
944
+ xml.empty_tag("c:ptCount", { val: values.size })
945
+ values.each_with_index do |v, i|
946
+ next unless v
699
947
 
700
- %(<c:pt idx="#{i}"><c:v>#{xml_escape(v.to_s)}</c:v></c:pt>)
948
+ xml.tag("c:pt", { idx: i }) { xml.text_tag("c:v", v.to_s) }
949
+ end
701
950
  end
702
- %(<c:numCache><c:formatCode>General</c:formatCode><c:ptCount val="#{values.size}"/>#{pts.join}</c:numCache>)
703
951
  end
704
952
 
705
- # : (untyped ref) -> ("" | ::String)
706
- def str_cache_xml(ref)
953
+ # : (XmlBuilder xml, untyped ref) -> void
954
+ def write_str_cache_xml(xml, ref)
707
955
  values = resolve_sheet_ref(ref)
708
- return "" unless values
956
+ return unless values
709
957
 
710
- pts = values.each_with_index.filter_map do |v, i|
711
- next unless v
958
+ xml.tag("c:strCache") do
959
+ xml.empty_tag("c:ptCount", { val: values.size })
960
+ values.each_with_index do |v, i|
961
+ next unless v
712
962
 
713
- %(<c:pt idx="#{i}"><c:v>#{xml_escape(v.to_s)}</c:v></c:pt>)
963
+ xml.tag("c:pt", { idx: i }) { xml.text_tag("c:v", v.to_s) }
964
+ end
714
965
  end
715
- %(<c:strCache><c:ptCount val="#{values.size}"/>#{pts.join}</c:strCache>)
716
966
  end
717
967
 
718
- # : (untyped color, ?alpha: untyped?, ?transforms: untyped?) -> untyped
719
- def color_xml(color, alpha: nil, transforms: nil)
968
+ # : (XmlBuilder xml, untyped color, ?alpha: untyped?, ?transforms: untyped?) -> void
969
+ def write_drawing_color_xml(xml, color, alpha: nil, transforms: nil)
720
970
  if color.is_a?(Hash) && color[:scheme]
721
- tag = "schemeClr"
722
- val = xml_escape(color[:scheme])
971
+ tag = "a:schemeClr"
972
+ val = color[:scheme]
723
973
  transforms = color[:transforms] || transforms
724
974
  else
725
- tag = "srgbClr"
726
- val = xml_escape(normalize_drawing_rgb(color))
975
+ tag = "a:srgbClr"
976
+ val = normalize_drawing_rgb(color)
727
977
  end
728
- children = +""
729
- children << %(<a:alpha val="#{xml_escape(alpha)}"/>) if alpha
730
- (transforms || []).each do |t|
731
- type = t[:type].to_s
732
- raise ArgumentError, "Unknown color transform: #{type.inspect}. Use one of: #{COLOR_TRANSFORMS.join(", ")}." unless COLOR_TRANSFORMS.include?(type)
733
-
734
- children << %(<a:#{type} val="#{xml_escape(t[:val])}"/>)
978
+ has_children = alpha || (transforms && !transforms.empty?)
979
+ unless has_children
980
+ xml.empty_tag(tag, { val: val || "" })
981
+ return
735
982
  end
736
- if children.empty?
737
- %(<a:#{tag} val="#{xml_escape(val)}"/>)
738
- else
739
- %(<a:#{tag} val="#{xml_escape(val)}">#{children}</a:#{tag}>)
983
+
984
+ xml.tag(tag, { val: val || "" }) do
985
+ xml.empty_tag("a:alpha", { val: alpha }) if alpha
986
+ (transforms || []).each do |t|
987
+ type = t[:type].to_s
988
+ raise ArgumentError, "Unknown color transform: #{type.inspect}. Use one of: #{COLOR_TRANSFORMS.join(", ")}." unless COLOR_TRANSFORMS.include?(type)
989
+
990
+ xml.empty_tag("a:#{type}", { val: t[:val] || "" })
991
+ end
740
992
  end
741
993
  end
742
994
 
@@ -756,31 +1008,40 @@ module Xlsxrb
756
1008
  str
757
1009
  end
758
1010
 
759
- alias srgb_clr_xml color_xml
760
-
761
- # : (untyped rotation, untyped font) -> ("" | ::String)
762
- def build_axis_txpr(rotation, font)
763
- return "" unless rotation || font
764
-
765
- body_attrs = rotation ? %( rot="#{xml_escape(rotation)}") : ""
766
- rpr_attrs = +""
767
- rpr_children = +""
768
- if font
769
- rpr_attrs << %( sz="#{(font[:size] * 100).to_i}") if font[:size]
770
- rpr_attrs << %( b="1") if font[:bold]
771
- rpr_attrs << %( i="1") if font[:italic]
772
- rpr_children << %(<a:solidFill>#{color_xml(font[:color])}</a:solidFill>) if font[:color]
773
- rpr_children << %(<a:latin typeface="#{xml_escape(font[:name])}"/>) if font[:name]
1011
+ # : (XmlBuilder xml, untyped rotation, untyped font) -> void
1012
+ def write_axis_txpr(xml, rotation, font)
1013
+ return unless rotation || font
1014
+
1015
+ xml.tag("c:txPr") do
1016
+ xml.empty_tag("a:bodyPr", { rot: rotation })
1017
+ xml.empty_tag("a:lstStyle")
1018
+ xml.tag("a:p") do
1019
+ xml.tag("a:pPr") do
1020
+ rpr_attrs = if font
1021
+ {
1022
+ sz: (font[:size] ? (font[:size] * 100).to_i : nil),
1023
+ b: (1 if font[:bold]),
1024
+ i: (1 if font[:italic])
1025
+ }
1026
+ else
1027
+ XmlBuilder::EMPTY_HASH
1028
+ end
1029
+ if font && (font[:color] || font[:name])
1030
+ xml.tag("a:defRPr", rpr_attrs) do
1031
+ xml.tag("a:solidFill") { write_drawing_color_xml(xml, font[:color]) } if font[:color]
1032
+ xml.empty_tag("a:latin", { typeface: font[:name] }) if font[:name]
1033
+ end
1034
+ else
1035
+ xml.empty_tag("a:defRPr", rpr_attrs)
1036
+ end
1037
+ end
1038
+ xml.empty_tag("a:endParaRPr")
1039
+ end
774
1040
  end
775
- def_rpr = if rpr_children.empty?
776
- "<a:defRPr#{rpr_attrs}/>"
777
- else
778
- "<a:defRPr#{rpr_attrs}>#{rpr_children}</a:defRPr>"
779
- end
780
- %(<c:txPr><a:bodyPr#{body_attrs}/><a:lstStyle/><a:p><a:pPr>#{def_rpr}</a:pPr><a:endParaRPr/></a:p></c:txPr>)
781
1041
  end
782
1042
 
783
- def build_val_ax_xml(ax_id:, prefix:, chart:, default_pos: "l", default_crosses: nil)
1043
+ # : (XmlBuilder xml, ax_id: ::Integer, prefix: ::Symbol, chart: untyped, ?default_pos: ::String, ?default_crosses: ::String?) -> void
1044
+ def write_val_ax_xml(xml, ax_id:, prefix:, chart:, default_pos: "l", default_crosses: nil)
784
1045
  del_key = :"#{prefix}_delete"
785
1046
  orient_key = :"#{prefix}_orientation"
786
1047
  log_key = :"#{prefix}_log_base"
@@ -808,196 +1069,200 @@ module Xlsxrb
808
1069
  minor_unit_key = :"#{prefix}_minor_unit"
809
1070
  disp_units_key = :"#{prefix}_disp_units"
810
1071
 
811
- parts = +""
812
- val_del = chart[del_key] ? 1 : 0
813
- val_orient = chart[orient_key] || "minMax"
814
- parts << %(<c:valAx><c:axId val="#{xml_escape(ax_id)}"/><c:scaling>)
815
- parts << %(<c:logBase val="#{xml_escape(chart[log_key])}"/>) if chart[log_key]
816
- parts << %(<c:orientation val="#{xml_escape(val_orient)}"/>)
817
- parts << %(<c:max val="#{xml_escape(chart[max_key])}"/>) if chart[max_key]
818
- parts << %(<c:min val="#{xml_escape(chart[min_key])}"/>) if chart[min_key]
819
- parts << %(</c:scaling><c:delete val="#{xml_escape(val_del)}"/><c:axPos val="#{xml_escape(chart[pos_key] || default_pos)}"/>)
820
- parts << gridlines_xml("majorGridlines", chart[major_grid_key])
821
- parts << gridlines_xml("minorGridlines", chart[minor_grid_key])
822
- if chart[title_key]
823
- val_title_spec = merge_flat_title_styling(chart[title_key], chart, title_key)
824
- parts << build_chart_title_xml(val_title_spec)
825
- end
826
- if (vnf = chart[num_fmt_key])
827
- vnf_hash = vnf.is_a?(Hash) ? vnf : { format_code: vnf }
828
- sl = vnf_hash[:source_linked] ? 1 : 0
829
- parts << %(<c:numFmt formatCode="#{xml_escape(vnf_hash[:format_code])}" sourceLinked="#{xml_escape(sl)}"/>)
830
- end
831
- parts << %(<c:majorTickMark val="#{xml_escape(chart[major_tick_key])}"/>) if chart[major_tick_key]
832
- parts << %(<c:minorTickMark val="#{xml_escape(chart[minor_tick_key])}"/>) if chart[minor_tick_key]
833
- parts << %(<c:tickLblPos val="#{xml_escape(chart[tick_lbl_pos_key])}"/>) if chart[tick_lbl_pos_key]
834
- if chart[line_color_key] || chart[fill_key] || chart[no_fill_key] || chart[line_dash_key]
835
- parts << "<c:spPr>"
836
- parts << "<a:noFill/>" if chart[no_fill_key]
837
- parts << %(<a:solidFill>#{color_xml(chart[fill_key])}</a:solidFill>) if chart[fill_key]
838
- if chart[line_color_key] || chart[line_dash_key]
839
- w_attr = chart[line_width_key] ? %( w="#{xml_escape(chart[line_width_key])}") : ""
840
- va_ln_f = chart[line_color_key] ? %(<a:solidFill>#{srgb_clr_xml(chart[line_color_key])}</a:solidFill>) : ""
841
- va_ln_d = chart[line_dash_key] ? %(<a:prstDash val="#{xml_escape(chart[line_dash_key])}"/>) : ""
842
- parts << "<a:ln#{w_attr}>#{va_ln_f}#{va_ln_d}</a:ln>"
843
- end
844
- parts << "</c:spPr>"
845
- end
846
- parts << build_axis_txpr(chart[rotation_key], chart[font_key])
847
- parts << '<c:crossAx val="1"/>'
848
- crosses_val = chart[crosses_key] || default_crosses
849
- parts << %(<c:crosses val="#{xml_escape(crosses_val)}"/>) if crosses_val
850
- parts << %(<c:crossesAt val="#{xml_escape(chart[crosses_at_key])}"/>) if !crosses_val && chart[crosses_at_key]
851
- parts << %(<c:crossBetween val="#{xml_escape(chart[cross_between_key])}"/>) if chart[cross_between_key]
852
- parts << %(<c:majorUnit val="#{xml_escape(chart[major_unit_key])}"/>) if chart[major_unit_key]
853
- parts << %(<c:minorUnit val="#{xml_escape(chart[minor_unit_key])}"/>) if chart[minor_unit_key]
854
- if chart[disp_units_key]
855
- du = chart[disp_units_key]
856
- if du.is_a?(Hash)
857
- parts << "<c:dispUnits>"
858
- parts << %(<c:builtInUnit val="#{xml_escape(du[:built_in_unit])}"/>) if du[:built_in_unit]
859
- parts << %(<c:custUnit val="#{xml_escape(du[:cust_unit])}"/>) if du[:cust_unit]
860
- if du[:label]
861
- dul = du[:label]
862
- parts << "<c:dispUnitsLbl>"
863
- if dul[:num_fmt]
864
- nf = dul[:num_fmt]
865
- nf_src = nf.is_a?(Hash) ? nf : { format_code: nf }
866
- nf_linked = if nf_src.key?(:source_linked)
867
- nf_src[:source_linked] ? 1 : 0
868
- else
869
- 0
870
- end
871
- parts << %(<c:numFmt formatCode="#{xml_escape(nf_src[:format_code])}" sourceLinked="#{xml_escape(nf_linked)}"/>)
1072
+ xml.tag("c:valAx") do
1073
+ xml.empty_tag("c:axId", { val: ax_id })
1074
+ xml.tag("c:scaling") do
1075
+ xml.empty_tag("c:logBase", { val: chart[log_key] }) if chart[log_key]
1076
+ xml.empty_tag("c:orientation", { val: chart[orient_key] || "minMax" })
1077
+ xml.empty_tag("c:max", { val: chart[max_key] }) if chart[max_key]
1078
+ xml.empty_tag("c:min", { val: chart[min_key] }) if chart[min_key]
1079
+ end
1080
+ xml.empty_tag("c:delete", { val: chart[del_key] ? 1 : 0 })
1081
+ xml.empty_tag("c:axPos", { val: chart[pos_key] || default_pos })
1082
+ write_gridlines_xml(xml, "majorGridlines", chart[major_grid_key]) if chart[major_grid_key]
1083
+ write_gridlines_xml(xml, "minorGridlines", chart[minor_grid_key]) if chart[minor_grid_key]
1084
+ if chart[title_key]
1085
+ val_title_spec = merge_flat_title_styling(chart[title_key], chart, title_key)
1086
+ write_chart_title_xml(xml, val_title_spec)
1087
+ end
1088
+ if (vnf = chart[num_fmt_key])
1089
+ vnf_hash = vnf.is_a?(Hash) ? vnf : { format_code: vnf }
1090
+ xml.empty_tag("c:numFmt", { formatCode: vnf_hash[:format_code] || "", sourceLinked: vnf_hash[:source_linked] ? 1 : 0 })
1091
+ end
1092
+ xml.empty_tag("c:majorTickMark", { val: chart[major_tick_key] }) if chart[major_tick_key]
1093
+ xml.empty_tag("c:minorTickMark", { val: chart[minor_tick_key] }) if chart[minor_tick_key]
1094
+ xml.empty_tag("c:tickLblPos", { val: chart[tick_lbl_pos_key] }) if chart[tick_lbl_pos_key]
1095
+ if chart[line_color_key] || chart[fill_key] || chart[no_fill_key] || chart[line_dash_key]
1096
+ xml.tag("c:spPr") do
1097
+ xml.empty_tag("a:noFill") if chart[no_fill_key]
1098
+ xml.tag("a:solidFill") { write_drawing_color_xml(xml, chart[fill_key]) } if chart[fill_key]
1099
+ if chart[line_color_key] || chart[line_dash_key]
1100
+ xml.tag("a:ln", { w: chart[line_width_key] }) do
1101
+ xml.tag("a:solidFill") { write_drawing_color_xml(xml, chart[line_color_key]) } if chart[line_color_key]
1102
+ xml.empty_tag("a:prstDash", { val: chart[line_dash_key] }) if chart[line_dash_key]
1103
+ end
872
1104
  end
873
- dul_sp = +""
874
- dul_sp << %(<a:solidFill>#{color_xml(dul[:fill_color])}</a:solidFill>) if dul[:fill_color]
875
- dul_sp << "<a:noFill/>" if dul[:no_fill]
876
- if dul[:line_color] || dul[:line_width] || dul[:line_dash]
877
- dul_lw = dul[:line_width] ? %( w="#{(dul[:line_width] * 12_700).to_i}") : ""
878
- dul_lf = dul[:line_color] ? %(<a:solidFill>#{color_xml(dul[:line_color])}</a:solidFill>) : ""
879
- dul_ld = dul[:line_dash] ? %(<a:prstDash val="#{xml_escape(dul[:line_dash])}"/>) : ""
880
- dul_sp << "<a:ln#{dul_lw}>#{dul_lf}#{dul_ld}</a:ln>"
1105
+ end
1106
+ end
1107
+ write_axis_txpr(xml, chart[rotation_key], chart[font_key])
1108
+ xml.empty_tag("c:crossAx", { val: 1 })
1109
+ crosses_val = chart[crosses_key] || default_crosses
1110
+ xml.empty_tag("c:crosses", { val: crosses_val }) if crosses_val
1111
+ xml.empty_tag("c:crossesAt", { val: chart[crosses_at_key] }) if !crosses_val && chart[crosses_at_key]
1112
+ xml.empty_tag("c:crossBetween", { val: chart[cross_between_key] }) if chart[cross_between_key]
1113
+ xml.empty_tag("c:majorUnit", { val: chart[major_unit_key] }) if chart[major_unit_key]
1114
+ xml.empty_tag("c:minorUnit", { val: chart[minor_unit_key] }) if chart[minor_unit_key]
1115
+ write_disp_units_xml(xml, chart[disp_units_key]) if chart[disp_units_key]
1116
+ end
1117
+ end
1118
+
1119
+ # : (XmlBuilder xml, untyped disp_units) -> void
1120
+ def write_disp_units_xml(xml, disp_units)
1121
+ xml.tag("c:dispUnits") do
1122
+ if disp_units.is_a?(Hash)
1123
+ xml.empty_tag("c:builtInUnit", { val: disp_units[:built_in_unit] }) if disp_units[:built_in_unit]
1124
+ xml.empty_tag("c:custUnit", { val: disp_units[:cust_unit] }) if disp_units[:cust_unit]
1125
+ if (dul = disp_units[:label])
1126
+ xml.tag("c:dispUnitsLbl") do
1127
+ if dul[:num_fmt]
1128
+ nf = dul[:num_fmt]
1129
+ nf_src = nf.is_a?(Hash) ? nf : { format_code: nf }
1130
+ nf_linked = nf_src[:source_linked] ? 1 : 0
1131
+ xml.empty_tag("c:numFmt", { formatCode: nf_src[:format_code] || "", sourceLinked: nf_linked })
1132
+ end
1133
+ write_simple_sp_pr_from_hash(xml, dul)
1134
+ write_axis_txpr(xml, nil, dul[:font]) if dul[:font]
881
1135
  end
882
- parts << "<c:spPr>#{dul_sp}</c:spPr>" unless dul_sp.empty?
883
- parts << build_axis_txpr(nil, dul[:font]) if dul[:font]
884
- parts << "</c:dispUnitsLbl>"
885
1136
  end
886
- parts << "</c:dispUnits>"
887
1137
  else
888
- parts << %(<c:dispUnits><c:builtInUnit val="#{xml_escape(du.to_s)}"/></c:dispUnits>)
1138
+ xml.empty_tag("c:builtInUnit", { val: disp_units.to_s })
889
1139
  end
890
1140
  end
891
- parts << "</c:valAx>"
892
- parts
893
1141
  end
894
1142
 
895
- # : (untyped chart) -> untyped
896
- def generate_chart_xml(chart)
897
- # Charts read from files carry the element name ("lineChart") as their type.
898
- chart_type = CHART_TYPE_MAP[chart[:type]] || CHART_TYPE_MAP.values.find { |element| element == chart[:type].to_s } || "barChart"
899
- parts = [
900
- XML_HEADER,
901
- %(<c:chartSpace xmlns:c="#{C_NS}" xmlns:a="#{A_NS}" xmlns:r="#{DOC_REL_NS}">)
902
- ]
903
- rc = chart[:rounded_corners]
904
- parts << %(<c:roundedCorners val="#{rc ? 1 : 0}"/>) unless rc.nil?
905
- parts << %(<c:style val="#{xml_escape(chart[:style])}"/>) if chart[:style]
906
- if (prot = chart[:protection])
907
- parts << "<c:protection>"
908
- parts << %(<c:chartObject val="#{prot[:chart_object] ? 1 : 0}"/>) unless prot[:chart_object].nil?
909
- parts << %(<c:data val="#{prot[:data] ? 1 : 0}"/>) unless prot[:data].nil?
910
- parts << %(<c:formatting val="#{prot[:formatting] ? 1 : 0}"/>) unless prot[:formatting].nil?
911
- parts << %(<c:selection val="#{prot[:selection] ? 1 : 0}"/>) unless prot[:selection].nil?
912
- parts << %(<c:userInterface val="#{prot[:user_interface] ? 1 : 0}"/>) unless prot[:user_interface].nil?
913
- parts << "</c:protection>"
1143
+ # : (XmlBuilder xml, untyped chart) -> void
1144
+ def write_chart_top_title_xml(xml, chart)
1145
+ return unless chart[:title]
1146
+
1147
+ title_spec = chart[:title]
1148
+ if chart[:title_font] || chart[:title_fill_color] || chart[:title_no_fill] ||
1149
+ chart[:title_line_color] || chart[:title_line_width] || chart[:title_line_dash] ||
1150
+ chart[:title_rotation]
1151
+ title_spec = { text: title_spec } unless title_spec.is_a?(Hash)
1152
+ title_spec[:font] ||= chart[:title_font] if chart[:title_font]
1153
+ title_spec[:fill_color] ||= chart[:title_fill_color] if chart[:title_fill_color]
1154
+ title_spec[:no_fill] = chart[:title_no_fill] if title_spec[:no_fill].nil? && !chart[:title_no_fill].nil?
1155
+ title_spec[:line_color] ||= chart[:title_line_color] if chart[:title_line_color]
1156
+ title_spec[:line_width] ||= chart[:title_line_width] if chart[:title_line_width]
1157
+ title_spec[:line_dash] ||= chart[:title_line_dash] if chart[:title_line_dash]
1158
+ title_spec[:rotation] ||= chart[:title_rotation] if chart[:title_rotation]
914
1159
  end
915
- parts << "<c:chart>"
916
-
917
- if chart[:title]
918
- title_spec = chart[:title]
919
- if chart[:title_font] || chart[:title_fill_color] || chart[:title_no_fill] ||
920
- chart[:title_line_color] || chart[:title_line_width] || chart[:title_line_dash] ||
921
- chart[:title_rotation]
922
- title_spec = { text: title_spec } unless title_spec.is_a?(Hash)
923
- title_spec[:font] ||= chart[:title_font] if chart[:title_font]
924
- title_spec[:fill_color] ||= chart[:title_fill_color] if chart[:title_fill_color]
925
- title_spec[:no_fill] = chart[:title_no_fill] if title_spec[:no_fill].nil? && !chart[:title_no_fill].nil?
926
- title_spec[:line_color] ||= chart[:title_line_color] if chart[:title_line_color]
927
- title_spec[:line_width] ||= chart[:title_line_width] if chart[:title_line_width]
928
- title_spec[:line_dash] ||= chart[:title_line_dash] if chart[:title_line_dash]
929
- title_spec[:rotation] ||= chart[:title_rotation] if chart[:title_rotation]
930
- end
931
- parts << build_chart_title_xml(title_spec, overlay: chart[:title_overlay])
932
- end
933
- atd = chart[:auto_title_deleted]
934
- parts << %(<c:autoTitleDeleted val="#{atd ? 1 : 0}"/>) unless atd.nil?
935
-
936
- if (v3d = chart[:view_3d])
937
- parts << "<c:view3D>"
938
- parts << %(<c:rotX val="#{xml_escape(v3d[:rot_x])}"/>) if v3d[:rot_x]
939
- parts << %(<c:hPercent val="#{xml_escape(v3d[:h_percent])}"/>) if v3d[:h_percent]
940
- parts << %(<c:rotY val="#{xml_escape(v3d[:rot_y])}"/>) if v3d[:rot_y]
941
- parts << %(<c:depthPercent val="#{xml_escape(v3d[:depth_percent])}"/>) if v3d[:depth_percent]
942
- r_ang = v3d[:r_ang_ax]
943
- parts << %(<c:rAngAx val="#{r_ang ? 1 : 0}"/>) unless r_ang.nil?
944
- parts << %(<c:perspective val="#{xml_escape(v3d[:perspective])}"/>) if v3d[:perspective]
945
- parts << "</c:view3D>"
1160
+ write_chart_title_xml(xml, title_spec, overlay: chart[:title_overlay])
1161
+ end
1162
+
1163
+ # : (XmlBuilder xml, untyped v3d) -> void
1164
+ def write_view_3d_xml(xml, v3d)
1165
+ xml.tag("c:view3D") do
1166
+ xml.empty_tag("c:rotX", { val: v3d[:rot_x] }) if v3d[:rot_x]
1167
+ xml.empty_tag("c:hPercent", { val: v3d[:h_percent] }) if v3d[:h_percent]
1168
+ xml.empty_tag("c:rotY", { val: v3d[:rot_y] }) if v3d[:rot_y]
1169
+ xml.empty_tag("c:depthPercent", { val: v3d[:depth_percent] }) if v3d[:depth_percent]
1170
+ xml.empty_tag("c:rAngAx", { val: v3d[:r_ang_ax] ? 1 : 0 }) unless v3d[:r_ang_ax].nil?
1171
+ xml.empty_tag("c:perspective", { val: v3d[:perspective] }) if v3d[:perspective]
946
1172
  end
1173
+ end
947
1174
 
948
- %i[floor side_wall back_wall].each do |wall_key|
1175
+ # : (XmlBuilder xml, untyped chart) -> void
1176
+ def write_chart_walls_xml(xml, chart)
1177
+ { floor: "floor", side_wall: "sideWall", back_wall: "backWall" }.each do |wall_key, tag|
949
1178
  next unless (wall = chart[wall_key])
950
1179
 
951
- tag = { floor: "floor", side_wall: "sideWall", back_wall: "backWall" }[wall_key]
952
- parts << "<c:#{tag}><c:spPr>"
953
- parts << "<a:solidFill>#{srgb_clr_xml(wall[:fill_color])}</a:solidFill>" if wall[:fill_color]
954
- parts << "<a:noFill/>" if wall[:no_fill]
955
- if wall[:line_color] || wall[:line_dash]
956
- w_attr = wall[:line_width] ? %( w="#{xml_escape(wall[:line_width])}") : ""
957
- w_fill = wall[:line_color] ? %(<a:solidFill>#{srgb_clr_xml(wall[:line_color])}</a:solidFill>) : ""
958
- w_dash = wall[:line_dash] ? %(<a:prstDash val="#{xml_escape(wall[:line_dash])}"/>) : ""
959
- parts << "<a:ln#{w_attr}>#{w_fill}#{w_dash}</a:ln>"
960
- end
961
- parts << "</c:spPr></c:#{tag}>"
1180
+ xml.tag("c:#{tag}") do
1181
+ xml.tag("c:spPr") do
1182
+ xml.tag("a:solidFill") { write_drawing_color_xml(xml, wall[:fill_color]) } if wall[:fill_color]
1183
+ xml.empty_tag("a:noFill") if wall[:no_fill]
1184
+ if wall[:line_color] || wall[:line_dash]
1185
+ xml.tag("a:ln", { w: wall[:line_width] }) do
1186
+ xml.tag("a:solidFill") { write_drawing_color_xml(xml, wall[:line_color]) } if wall[:line_color]
1187
+ xml.empty_tag("a:prstDash", { val: wall[:line_dash] }) if wall[:line_dash]
1188
+ end
1189
+ end
1190
+ end
1191
+ end
962
1192
  end
1193
+ end
963
1194
 
964
- parts << "<c:plotArea>"
965
- pa_layout = chart[:plot_area_layout]
966
- if pa_layout.is_a?(Hash)
967
- ml_parts = +""
968
- ml_parts << %(<c:layoutTarget val="#{xml_escape(pa_layout[:target])}"/>) if pa_layout[:target]
969
- ml_parts << %(<c:xMode val="#{xml_escape(pa_layout[:x_mode])}"/>) if pa_layout[:x_mode]
970
- ml_parts << %(<c:yMode val="#{xml_escape(pa_layout[:y_mode])}"/>) if pa_layout[:y_mode]
971
- ml_parts << %(<c:wMode val="#{xml_escape(pa_layout[:w_mode])}"/>) if pa_layout[:w_mode]
972
- ml_parts << %(<c:hMode val="#{xml_escape(pa_layout[:h_mode])}"/>) if pa_layout[:h_mode]
973
- ml_parts << %(<c:x val="#{xml_escape(pa_layout[:x])}"/>) if pa_layout[:x]
974
- ml_parts << %(<c:y val="#{xml_escape(pa_layout[:y])}"/>) if pa_layout[:y]
975
- ml_parts << %(<c:w val="#{xml_escape(pa_layout[:w])}"/>) if pa_layout[:w]
976
- ml_parts << %(<c:h val="#{xml_escape(pa_layout[:h])}"/>) if pa_layout[:h]
977
- parts << if ml_parts.empty?
978
- "<c:layout/>"
979
- else
980
- "<c:layout><c:manualLayout>#{ml_parts}</c:manualLayout></c:layout>"
981
- end
982
- else
983
- parts << "<c:layout/>"
984
- end
985
- all_series = chart[:series] || []
986
- all_series.each do |s|
987
- s[:cat_ref] ||= s[:categories] if s[:categories]
988
- s[:val_ref] ||= s[:values] if s[:values]
1195
+ # : (XmlBuilder xml, untyped chart, ::String chart_type) -> void
1196
+ def write_plot_area_xml(xml, chart, chart_type)
1197
+ xml.tag("c:plotArea") do
1198
+ write_plot_area_layout_xml(xml, chart[:plot_area_layout])
1199
+ all_series = chart[:series] || []
1200
+ all_series.each do |s|
1201
+ s[:cat_ref] ||= s[:categories] if s[:categories]
1202
+ s[:val_ref] ||= s[:values] if s[:values]
1203
+ end
1204
+
1205
+ has_secondary = !chart[:sec_val_axis_title].nil? ||
1206
+ !chart[:secondary_value_axis].nil? ||
1207
+ all_series.any? { |s| s[:secondary_axis] || s[:axis] == :secondary || s[:axis] == "secondary" }
1208
+
1209
+ groups = group_chart_series(all_series, chart_type)
1210
+ groups.each { |grp| write_chart_group_xml(xml, grp, chart) }
1211
+
1212
+ write_chart_axes_xml(xml, chart, groups, has_secondary) if groups.any? { |g| !NO_AXIS_CHARTS.include?(g[:type]) }
1213
+ write_chart_data_table_xml(xml, chart[:data_table]) if chart[:data_table]
1214
+
1215
+ if chart[:plot_area_fill] || chart[:plot_area_no_fill] || chart[:plot_area_line_color] || chart[:plot_area_line_dash]
1216
+ write_simple_sp_pr(
1217
+ xml,
1218
+ fill_color: chart[:plot_area_fill],
1219
+ no_fill: chart[:plot_area_no_fill],
1220
+ line_color: chart[:plot_area_line_color],
1221
+ line_width: chart[:plot_area_line_width],
1222
+ line_dash: chart[:plot_area_line_dash],
1223
+ emu: false
1224
+ )
1225
+ end
989
1226
  end
1227
+ end
990
1228
 
991
- has_secondary = !chart[:sec_val_axis_title].nil? ||
992
- !chart[:secondary_value_axis].nil? ||
993
- all_series.any? { |s| s[:secondary_axis] || s[:axis] == :secondary || s[:axis] == "secondary" }
1229
+ # : (XmlBuilder xml, untyped layout, ?emit_empty: bool) -> void
1230
+ def write_plot_area_layout_xml(xml, layout, emit_empty: true)
1231
+ ml_entries = if layout.is_a?(Hash)
1232
+ [
1233
+ ["c:layoutTarget", layout[:target]],
1234
+ ["c:xMode", layout[:x_mode]],
1235
+ ["c:yMode", layout[:y_mode]],
1236
+ ["c:wMode", layout[:w_mode]],
1237
+ ["c:hMode", layout[:h_mode]],
1238
+ ["c:x", layout[:x]],
1239
+ ["c:y", layout[:y]],
1240
+ ["c:w", layout[:w]],
1241
+ ["c:h", layout[:h]]
1242
+ ].select { |_, val| val }
1243
+ else
1244
+ []
1245
+ end
1246
+ if ml_entries.empty?
1247
+ xml.empty_tag("c:layout") if emit_empty
1248
+ else
1249
+ xml.tag("c:layout") do
1250
+ xml.tag("c:manualLayout") do
1251
+ ml_entries.each { |tag_name, val| xml.empty_tag(tag_name, { val: val }) }
1252
+ end
1253
+ end
1254
+ end
1255
+ end
994
1256
 
1257
+ # : (untyped all_series, ::String chart_type) -> ::Array[untyped]
1258
+ def group_chart_series(all_series, chart_type)
995
1259
  groups = []
996
1260
  all_series.each_with_index do |ser, idx|
997
- s_type = if ser[:type]
1261
+ raw_type = ser[:type] || (chart_type == "comboChart" ? ser[:chart_type] : nil)
1262
+ s_type = if raw_type
998
1263
  # The type is written as an element name, so only known types are accepted.
999
- CHART_TYPE_MAP[ser[:type]] || CHART_TYPE_MAP.values.find { |element| element == ser[:type].to_s } ||
1000
- raise(ArgumentError, "Unknown chart series type: #{ser[:type].inspect}. Use one of: #{CHART_TYPE_MAP.keys.map(&:inspect).join(", ")}.")
1264
+ CHART_TYPE_MAP[raw_type] || CHART_TYPE_MAP.values.find { |element| element == raw_type.to_s } ||
1265
+ raise(ArgumentError, "Unknown chart series type: #{raw_type.inspect}. Use one of: #{CHART_TYPE_MAP.keys.map(&:inspect).join(", ")}.")
1001
1266
  elsif chart_type == "comboChart"
1002
1267
  "barChart"
1003
1268
  else
@@ -1017,751 +1282,539 @@ module Xlsxrb
1017
1282
  if groups.empty?
1018
1283
  groups << { type: (chart_type == "comboChart" ? "barChart" : chart_type), val_ax_id: 2, series: [] }
1019
1284
  end
1285
+ groups
1286
+ end
1020
1287
 
1021
- groups.each do |grp|
1022
- g_type = grp[:type]
1023
- g_val_ax = grp[:val_ax_id]
1024
- g_no_axes = NO_AXIS_CHARTS.include?(g_type)
1288
+ # : (XmlBuilder xml, untyped grp, untyped chart) -> void
1289
+ def write_chart_group_xml(xml, grp, chart)
1290
+ g_type = grp[:type]
1291
+ g_val_ax = grp[:val_ax_id]
1292
+ g_no_axes = NO_AXIS_CHARTS.include?(g_type)
1025
1293
 
1026
- parts << "<c:#{g_type}>"
1027
- parts << %(<c:ofPieType val="#{xml_escape(chart[:of_pie_type] || "pie")}"/>) if g_type == "ofPieChart"
1294
+ xml.tag("c:#{g_type}") do
1295
+ xml.empty_tag("c:ofPieType", { val: chart[:of_pie_type] || "pie" }) if g_type == "ofPieChart"
1028
1296
  if %w[barChart bar3DChart].include?(g_type)
1029
- bd = chart[:bar_dir] || "col"
1030
- gr = chart[:grouping] || "clustered"
1031
- parts << %(<c:barDir val="#{xml_escape(bd)}"/><c:grouping val="#{xml_escape(gr)}"/>)
1297
+ xml.empty_tag("c:barDir", { val: chart[:bar_dir] || "col" })
1298
+ xml.empty_tag("c:grouping", { val: chart[:grouping] || "clustered" })
1032
1299
  elsif GROUPING_CHARTS.include?(g_type)
1033
- parts << %(<c:grouping val="#{xml_escape(chart[:grouping] || "standard")}"/>)
1034
- end
1035
- parts << %(<c:scatterStyle val="#{xml_escape(chart[:scatter_style] || "lineMarker")}"/>) if g_type == "scatterChart"
1036
- parts << %(<c:radarStyle val="#{xml_escape(chart[:radar_style] || "standard")}"/>) if g_type == "radarChart"
1037
- vc = chart[:vary_colors]
1038
- parts << %(<c:varyColors val="#{vc ? 1 : 0}"/>) unless vc.nil?
1039
- wf = chart[:wireframe]
1040
- parts << %(<c:wireframe val="#{wf ? 1 : 0}"/>) unless wf.nil?
1041
-
1042
- grp[:series].each do |ser, idx|
1043
- ser_order = ser[:order] || idx
1044
- parts << "<c:ser><c:idx val=\"#{xml_escape(idx)}\"/><c:order val=\"#{xml_escape(ser_order)}\"/>"
1045
- parts << "<c:tx><c:strRef><c:f>#{xml_escape(ser[:name])}</c:f>#{str_cache_xml(ser[:name])}</c:strRef></c:tx>" if ser[:name]
1046
- head_end = parts.size
1047
- iin = ser[:invert_if_negative]
1048
- parts << %(<c:invertIfNegative val="#{iin ? 1 : 0}"/>) unless iin.nil?
1049
- parts << %(<c:explosion val="#{xml_escape(ser[:explosion])}"/>) if ser[:explosion]
1050
- options_end = parts.size
1051
- ser[:data_points]&.each do |dp|
1052
- parts << "<c:dPt><c:idx val=\"#{xml_escape(dp[:idx])}\"/>"
1053
- dp_iin = dp[:invert_if_negative]
1054
- parts << %(<c:invertIfNegative val="#{dp_iin ? 1 : 0}"/>) unless dp_iin.nil?
1055
- if dp[:marker_symbol] || dp[:marker_size] || dp[:marker_fill] || dp[:marker_no_fill] || dp[:marker_line_color] || dp[:marker_line_dash] || dp[:marker_no_line]
1056
- parts << "<c:marker>"
1057
- parts << %(<c:symbol val="#{xml_escape(dp[:marker_symbol])}"/>) if dp[:marker_symbol]
1058
- parts << %(<c:size val="#{xml_escape(dp[:marker_size])}"/>) if dp[:marker_size]
1059
- if dp[:marker_fill] || dp[:marker_no_fill] || dp[:marker_line_color] || dp[:marker_line_dash] || dp[:marker_no_line]
1060
- mk_sp = +""
1061
- mk_sp << "<a:noFill/>" if dp[:marker_no_fill]
1062
- mk_sp << %(<a:solidFill>#{color_xml(dp[:marker_fill])}</a:solidFill>) if dp[:marker_fill]
1063
- if dp[:marker_no_line]
1064
- mk_sp << "<a:ln><a:noFill/></a:ln>"
1065
- elsif dp[:marker_line_color] || dp[:marker_line_dash]
1066
- mk_ln_w = dp[:marker_line_width] ? %( w="#{(dp[:marker_line_width] * 12_700).to_i}") : ""
1067
- mk_ln_f = dp[:marker_line_color] ? %(<a:solidFill>#{color_xml(dp[:marker_line_color])}</a:solidFill>) : ""
1068
- mk_ln_d = dp[:marker_line_dash] ? %(<a:prstDash val="#{xml_escape(dp[:marker_line_dash])}"/>) : ""
1069
- mk_sp << "<a:ln#{mk_ln_w}>#{mk_ln_f}#{mk_ln_d}</a:ln>"
1070
- end
1071
- parts << "<c:spPr>#{mk_sp}</c:spPr>"
1072
- end
1073
- parts << "</c:marker>"
1074
- end
1075
- dp_b3d = dp[:bubble_3d]
1076
- parts << %(<c:bubble3D val="#{dp_b3d ? 1 : 0}"/>) unless dp_b3d.nil?
1077
- parts << %(<c:explosion val="#{xml_escape(dp[:explosion])}"/>) if dp[:explosion]
1078
- dp_sp_children = +""
1079
- dp_sp_children << %(<a:solidFill>#{color_xml(dp[:fill_color])}</a:solidFill>) if dp[:fill_color]
1080
- dp_sp_children << "<a:noFill/>" if dp[:no_fill]
1081
- if dp[:no_line]
1082
- dp_sp_children << "<a:ln><a:noFill/></a:ln>"
1083
- elsif dp[:line_color] || dp[:line_width] || dp[:line_dash]
1084
- dp_ln_attrs = dp[:line_width] ? %( w="#{(dp[:line_width] * 12_700).to_i}") : ""
1085
- dp_ln_fill = dp[:line_color] ? %(<a:solidFill>#{color_xml(dp[:line_color])}</a:solidFill>) : ""
1086
- dp_ln_dash = dp[:line_dash] ? %(<a:prstDash val="#{xml_escape(dp[:line_dash])}"/>) : ""
1087
- dp_sp_children << "<a:ln#{dp_ln_attrs}>#{dp_ln_fill}#{dp_ln_dash}</a:ln>"
1088
- end
1089
- parts << "<c:spPr>#{dp_sp_children}</c:spPr>" unless dp_sp_children.empty?
1090
- parts << "</c:dPt>"
1091
- end
1092
- data_points_end = parts.size
1093
- if ser[:fill_color] || ser[:no_fill] || ser[:line_color] || ser[:no_line] || ser[:line_width] || ser[:line_cap] || ser[:line_join] || ser[:line_dash]
1094
- parts << "<c:spPr>"
1095
- parts << %(<a:solidFill>#{color_xml(ser[:fill_color])}</a:solidFill>) if ser[:fill_color]
1096
- parts << "<a:noFill/>" if ser[:no_fill]
1097
- if ser[:line_color] || ser[:no_line] || ser[:line_width] || ser[:line_cap] || ser[:line_join] || ser[:line_dash]
1098
- lw = ser[:line_width] ? %( w="#{(ser[:line_width] * 12_700).to_i}") : ""
1099
- lc = ser[:line_cap] ? %( cap="#{xml_escape(ser[:line_cap])}") : ""
1100
- parts << "<a:ln#{lw}#{lc}>"
1101
- parts << "<a:noFill/>" if ser[:no_line]
1102
- parts << %(<a:solidFill>#{color_xml(ser[:line_color])}</a:solidFill>) if ser[:line_color]
1103
- parts << %(<a:prstDash val="#{xml_escape(ser[:line_dash])}"/>) if ser[:line_dash]
1104
- case ser[:line_join]
1105
- when "round" then parts << "<a:round/>"
1106
- when "bevel" then parts << "<a:bevel/>"
1107
- when "miter"
1108
- parts << (ser[:line_miter_limit] ? "<a:miter lim=\"#{ser[:line_miter_limit].to_i}\"/>" : "<a:miter/>")
1109
- end
1110
- parts << "</a:ln>"
1111
- end
1112
- parts << "</c:spPr>"
1300
+ xml.empty_tag("c:grouping", { val: chart[:grouping] || "standard" })
1301
+ end
1302
+ xml.empty_tag("c:scatterStyle", { val: chart[:scatter_style] || "lineMarker" }) if g_type == "scatterChart"
1303
+ xml.empty_tag("c:radarStyle", { val: chart[:radar_style] || "standard" }) if g_type == "radarChart"
1304
+ xml.empty_tag("c:varyColors", { val: chart[:vary_colors] ? 1 : 0 }) unless chart[:vary_colors].nil?
1305
+ xml.empty_tag("c:wireframe", { val: chart[:wireframe] ? 1 : 0 }) unless chart[:wireframe].nil?
1306
+
1307
+ grp[:series].each { |ser, idx| write_chart_series_xml(xml, ser, idx, g_type, chart) }
1308
+
1309
+ write_band_fmts_xml(xml, chart[:band_fmts]) if chart[:band_fmts]
1310
+ xml.empty_tag("c:gapWidth", { val: chart[:gap_width] }) if chart[:gap_width]
1311
+ xml.empty_tag("c:splitType", { val: chart[:split_type] }) if chart[:split_type]
1312
+ xml.empty_tag("c:splitPos", { val: chart[:split_pos] }) if chart[:split_pos]
1313
+ if chart[:cust_split]&.any?
1314
+ xml.tag("c:custSplit") do
1315
+ chart[:cust_split].each { |idx| xml.empty_tag("c:secondPiePt", { val: idx || "" }) }
1113
1316
  end
1114
- shape_end = parts.size
1115
- if ser[:marker_symbol] || ser[:marker_size] || ser[:marker_fill] || ser[:marker_no_fill] || ser[:marker_line_color] || ser[:marker_line_dash] || ser[:marker_no_line]
1116
- parts << "<c:marker>"
1117
- parts << %(<c:symbol val="#{xml_escape(ser[:marker_symbol])}"/>) if ser[:marker_symbol]
1118
- parts << %(<c:size val="#{xml_escape(ser[:marker_size])}"/>) if ser[:marker_size]
1119
- if ser[:marker_fill] || ser[:marker_no_fill] || ser[:marker_line_color] || ser[:marker_line_dash] || ser[:marker_no_line]
1120
- parts << "<c:spPr>"
1121
- parts << "<a:noFill/>" if ser[:marker_no_fill]
1122
- parts << %(<a:solidFill>#{color_xml(ser[:marker_fill])}</a:solidFill>) if ser[:marker_fill]
1123
- if ser[:marker_no_line]
1124
- parts << "<a:ln><a:noFill/></a:ln>"
1125
- elsif ser[:marker_line_color] || ser[:marker_line_dash]
1126
- mk_ln_w = ser[:marker_line_width] ? %( w="#{(ser[:marker_line_width] * 12_700).to_i}") : ""
1127
- mk_ln_f = ser[:marker_line_color] ? %(<a:solidFill>#{color_xml(ser[:marker_line_color])}</a:solidFill>) : ""
1128
- mk_ln_d = ser[:marker_line_dash] ? %(<a:prstDash val="#{xml_escape(ser[:marker_line_dash])}"/>) : ""
1129
- parts << "<a:ln#{mk_ln_w}>#{mk_ln_f}#{mk_ln_d}</a:ln>"
1130
- end
1131
- parts << "</c:spPr>"
1317
+ end
1318
+ xml.empty_tag("c:secondPieSize", { val: chart[:second_pie_size] }) if chart[:second_pie_size]
1319
+ xml.empty_tag("c:gapDepth", { val: chart[:gap_depth] }) if chart[:gap_depth]
1320
+ xml.empty_tag("c:overlap", { val: chart[:overlap] }) if chart[:overlap]
1321
+ xml.empty_tag("c:shape", { val: chart[:bar_shape] }) if chart[:bar_shape]
1322
+ xml.empty_tag("c:bubble3D", { val: chart[:bubble_3d] ? 1 : 0 }) unless chart[:bubble_3d].nil?
1323
+ xml.empty_tag("c:bubbleScale", { val: chart[:bubble_scale] }) if chart[:bubble_scale]
1324
+ xml.empty_tag("c:showNegBubbles", { val: chart[:show_neg_bubbles] ? 1 : 0 }) unless chart[:show_neg_bubbles].nil?
1325
+ xml.empty_tag("c:sizeRepresents", { val: chart[:size_represents] }) if chart[:size_represents]
1326
+ # Doughnut charts carry both; Excel's defaults are 0 and 50.
1327
+ first_slice_ang = chart[:first_slice_ang] || (0 if g_type == "doughnutChart")
1328
+ hole_size = chart[:hole_size] || (50 if g_type == "doughnutChart")
1329
+ xml.empty_tag("c:firstSliceAng", { val: first_slice_ang }) if first_slice_ang
1330
+ xml.empty_tag("c:holeSize", { val: hole_size }) if hole_size
1331
+ write_line_sp_tag(xml, "c:serLines", chart[:ser_lines]) if chart[:ser_lines]
1332
+ write_line_sp_tag(xml, "c:dropLines", chart[:drop_lines]) if chart[:drop_lines]
1333
+ write_line_sp_tag(xml, "c:hiLowLines", chart[:hi_low_lines]) if chart[:hi_low_lines]
1334
+ write_up_down_bars_xml(xml, chart[:up_down_bars]) if chart[:up_down_bars]
1335
+ xml.empty_tag("c:marker", { val: chart[:marker] ? 1 : 0 }) unless chart[:marker].nil?
1336
+ xml.empty_tag("c:smooth", { val: chart[:smooth] ? 1 : 0 }) unless chart[:smooth].nil?
1337
+ unless g_no_axes
1338
+ xml.empty_tag("c:axId", { val: 1 })
1339
+ xml.empty_tag("c:axId", { val: g_val_ax })
1340
+ xml.empty_tag("c:axId", { val: 3 }) if THREE_AXIS_CHARTS.include?(g_type)
1341
+ end
1342
+ end
1343
+ end
1344
+
1345
+ # : (XmlBuilder xml, untyped ser, ::Integer idx, ::String g_type, untyped chart) -> void
1346
+ def write_chart_series_xml(xml, ser, idx, g_type, chart)
1347
+ ser_order = ser[:order] || idx
1348
+ xml.tag("c:ser") do
1349
+ xml.empty_tag("c:idx", { val: idx })
1350
+ xml.empty_tag("c:order", { val: ser_order })
1351
+ if ser[:name]
1352
+ xml.tag("c:tx") do
1353
+ xml.tag("c:strRef") do
1354
+ xml.text_tag("c:f", ser[:name])
1355
+ write_str_cache_xml(xml, ser[:name])
1132
1356
  end
1133
- parts << "</c:marker>"
1134
1357
  end
1135
- # The series schemas order these children spPr, then invertIfNegative, marker, or
1136
- # explosion (by chart type), then dPt.
1137
- options = parts[head_end...options_end]
1138
- data_points = parts[options_end...data_points_end]
1139
- shape = parts[data_points_end...shape_end]
1140
- marker = parts[shape_end..]
1141
- parts[head_end..] = shape + options + marker + data_points
1142
- if (dl = ser[:data_labels] || chart[:data_labels])
1143
- parts << "<c:dLbls>"
1144
- dl[:labels]&.each do |lbl|
1145
- parts << "<c:dLbl>"
1146
- parts << %(<c:idx val="#{xml_escape(lbl[:idx])}"/>)
1147
- if lbl[:delete]
1148
- parts << '<c:delete val="1"/>'
1149
- else
1150
- if (lbl_layout = lbl[:layout])
1151
- ml = +""
1152
- ml << %(<c:x val="#{xml_escape(lbl_layout[:x])}"/>) if lbl_layout[:x]
1153
- ml << %(<c:y val="#{xml_escape(lbl_layout[:y])}"/>) if lbl_layout[:y]
1154
- parts << (ml.empty? ? "<c:layout/>" : "<c:layout><c:manualLayout>#{ml}</c:manualLayout></c:layout>")
1155
- end
1156
- parts << "<c:tx><c:rich><a:bodyPr/><a:lstStyle/><a:p><a:r><a:t>#{xml_escape(lbl[:text])}</a:t></a:r></a:p></c:rich></c:tx>" if lbl[:text]
1157
- if lbl[:num_fmt]
1158
- lnf = lbl[:num_fmt]
1159
- lnf_src = lnf[:source_linked] ? ' sourceLinked="1"' : ""
1160
- parts << %(<c:numFmt formatCode="#{xml_escape(lnf[:format_code])}"#{lnf_src}/>)
1161
- end
1162
- lbl_sp = +""
1163
- lbl_sp << %(<a:solidFill>#{color_xml(lbl[:fill_color])}</a:solidFill>) if lbl[:fill_color]
1164
- lbl_sp << "<a:noFill/>" if lbl[:no_fill]
1165
- if lbl[:line_color] || lbl[:line_width] || lbl[:line_dash]
1166
- lbl_lw = lbl[:line_width] ? %( w="#{(lbl[:line_width] * 12_700).to_i}") : ""
1167
- lbl_lf = lbl[:line_color] ? %(<a:solidFill>#{color_xml(lbl[:line_color])}</a:solidFill>) : ""
1168
- lbl_ld = lbl[:line_dash] ? %(<a:prstDash val="#{xml_escape(lbl[:line_dash])}"/>) : ""
1169
- lbl_sp << "<a:ln#{lbl_lw}>#{lbl_lf}#{lbl_ld}</a:ln>"
1170
- end
1171
- parts << "<c:spPr>#{lbl_sp}</c:spPr>" unless lbl_sp.empty?
1172
- parts << build_axis_txpr(nil, lbl[:font]) if lbl[:font]
1173
- parts << %(<c:dLblPos val="#{xml_escape(lbl[:position])}"/>) if lbl[:position]
1174
- parts << "<c:showLegendKey val=\"#{lbl[:show_legend_key] ? 1 : 0}\"/>" unless lbl[:show_legend_key].nil?
1175
- parts << "<c:showVal val=\"#{lbl[:show_val] ? 1 : 0}\"/>" unless lbl[:show_val].nil?
1176
- parts << "<c:showCatName val=\"#{lbl[:show_cat_name] ? 1 : 0}\"/>" unless lbl[:show_cat_name].nil?
1177
- parts << "<c:showSerName val=\"#{lbl[:show_ser_name] ? 1 : 0}\"/>" unless lbl[:show_ser_name].nil?
1178
- parts << "<c:showPercent val=\"#{lbl[:show_percent] ? 1 : 0}\"/>" unless lbl[:show_percent].nil?
1179
- parts << "<c:showBubbleSize val=\"#{lbl[:show_bubble_size] ? 1 : 0}\"/>" unless lbl[:show_bubble_size].nil?
1180
- parts << "<c:separator>#{xml_escape(lbl[:separator])}</c:separator>" if lbl[:separator]
1358
+ end
1359
+
1360
+ # The series schemas order these children spPr, then invertIfNegative, marker, or
1361
+ # explosion (by chart type), then dPt.
1362
+ write_series_sp_pr_xml(xml, ser)
1363
+ xml.empty_tag("c:invertIfNegative", { val: ser[:invert_if_negative] ? 1 : 0 }) unless ser[:invert_if_negative].nil?
1364
+ xml.empty_tag("c:explosion", { val: ser[:explosion] }) if ser[:explosion]
1365
+ write_marker_xml(xml, ser)
1366
+ ser[:data_points]&.each { |dp| write_data_point_xml(xml, dp) }
1367
+
1368
+ if (dl = ser[:data_labels] || chart[:data_labels])
1369
+ write_data_labels_xml(xml, dl)
1370
+ end
1371
+ trendline_list = ser[:trendlines] || (ser[:trendline] ? [ser[:trendline]] : [])
1372
+ trendline_list.each { |tl| write_trendline_xml(xml, tl) }
1373
+ err_bars_list = ser[:error_bars_list] || (ser[:error_bars] ? [ser[:error_bars]] : [])
1374
+ err_bars_list.each { |eb| write_error_bars_xml(xml, eb) }
1375
+
1376
+ uses_xy = %w[scatterChart bubbleChart].include?(g_type)
1377
+ cat_tag = uses_xy ? "xVal" : "cat"
1378
+ val_tag = uses_xy ? "yVal" : "val"
1379
+ if ser[:cat_ref]
1380
+ xml.tag("c:#{cat_tag}") do
1381
+ if uses_xy || ser[:cat_ref_type] == :num
1382
+ xml.tag("c:numRef") do
1383
+ xml.text_tag("c:f", ser[:cat_ref])
1384
+ write_num_cache_xml(xml, ser[:cat_ref])
1181
1385
  end
1182
- parts << "</c:dLbl>"
1183
- end
1184
- if dl[:num_fmt]
1185
- nf = dl[:num_fmt]
1186
- nf_src = nf[:source_linked] ? ' sourceLinked="1"' : ""
1187
- parts << %(<c:numFmt formatCode="#{xml_escape(nf[:format_code])}"#{nf_src}/>)
1188
- end
1189
- dl_sp_children = +""
1190
- dl_sp_children << %(<a:solidFill>#{color_xml(dl[:fill_color])}</a:solidFill>) if dl[:fill_color]
1191
- dl_sp_children << "<a:noFill/>" if dl[:no_fill]
1192
- if dl[:line_color] || dl[:line_width] || dl[:line_dash]
1193
- dl_lw = dl[:line_width] ? %( w="#{(dl[:line_width] * 12_700).to_i}") : ""
1194
- dl_lf = dl[:line_color] ? %(<a:solidFill>#{color_xml(dl[:line_color])}</a:solidFill>) : ""
1195
- dl_ld = dl[:line_dash] ? %(<a:prstDash val="#{xml_escape(dl[:line_dash])}"/>) : ""
1196
- dl_sp_children << "<a:ln#{dl_lw}>#{dl_lf}#{dl_ld}</a:ln>"
1197
- end
1198
- parts << "<c:spPr>#{dl_sp_children}</c:spPr>" unless dl_sp_children.empty?
1199
- parts << build_axis_txpr(nil, dl[:font]) if dl[:font]
1200
- parts << %(<c:dLblPos val="#{xml_escape(dl[:position])}"/>) if dl[:position]
1201
- parts << "<c:showLegendKey val=\"#{dl[:show_legend_key] ? 1 : 0}\"/>" unless dl[:show_legend_key].nil?
1202
- parts << "<c:showVal val=\"#{dl[:show_val] ? 1 : 0}\"/>" unless dl[:show_val].nil?
1203
- parts << "<c:showCatName val=\"#{dl[:show_cat_name] ? 1 : 0}\"/>" unless dl[:show_cat_name].nil?
1204
- parts << "<c:showSerName val=\"#{dl[:show_ser_name] ? 1 : 0}\"/>" unless dl[:show_ser_name].nil?
1205
- parts << "<c:showPercent val=\"#{dl[:show_percent] ? 1 : 0}\"/>" unless dl[:show_percent].nil?
1206
- parts << "<c:showBubbleSize val=\"#{dl[:show_bubble_size] ? 1 : 0}\"/>" unless dl[:show_bubble_size].nil?
1207
- parts << "<c:separator>#{xml_escape(dl[:separator])}</c:separator>" if dl[:separator]
1208
- sl = dl[:show_leader_lines]
1209
- parts << "<c:showLeaderLines val=\"#{sl ? 1 : 0}\"/>" unless sl.nil?
1210
- if dl[:leader_lines]
1211
- ll_spec = dl[:leader_lines]
1212
- if ll_spec.is_a?(Hash)
1213
- ll_sp = +""
1214
- if ll_spec[:line_color] || ll_spec[:line_width] || ll_spec[:line_dash]
1215
- ll_lw = ll_spec[:line_width] ? %( w="#{(ll_spec[:line_width] * 12_700).to_i}") : ""
1216
- ll_lf = ll_spec[:line_color] ? %(<a:solidFill>#{color_xml(ll_spec[:line_color])}</a:solidFill>) : ""
1217
- ll_ld = ll_spec[:line_dash] ? %(<a:prstDash val="#{xml_escape(ll_spec[:line_dash])}"/>) : ""
1218
- ll_sp << "<a:ln#{ll_lw}>#{ll_lf}#{ll_ld}</a:ln>"
1219
- end
1220
- parts << (ll_sp.empty? ? "<c:leaderLines/>" : "<c:leaderLines><c:spPr>#{ll_sp}</c:spPr></c:leaderLines>")
1221
- else
1222
- parts << "<c:leaderLines/>"
1386
+ else
1387
+ xml.tag("c:strRef") do
1388
+ xml.text_tag("c:f", ser[:cat_ref])
1389
+ write_str_cache_xml(xml, ser[:cat_ref])
1223
1390
  end
1224
1391
  end
1225
- parts << "</c:dLbls>"
1226
1392
  end
1227
- trendline_list = ser[:trendlines] || (ser[:trendline] ? [ser[:trendline]] : [])
1228
- trendline_list.each do |tl|
1229
- parts << "<c:trendline>"
1230
- parts << "<c:name>#{xml_escape(tl[:name])}</c:name>" if tl[:name]
1231
- tl_sp_children = +""
1232
- if tl[:line_color] || tl[:line_width] || tl[:line_dash]
1233
- tl_ln_w = tl[:line_width] ? %( w="#{(tl[:line_width] * 12_700).to_i}") : ""
1234
- tl_ln_f = tl[:line_color] ? %(<a:solidFill>#{color_xml(tl[:line_color])}</a:solidFill>) : ""
1235
- tl_ln_d = tl[:line_dash] ? %(<a:prstDash val="#{xml_escape(tl[:line_dash])}"/>) : ""
1236
- tl_sp_children << "<a:ln#{tl_ln_w}>#{tl_ln_f}#{tl_ln_d}</a:ln>"
1237
- end
1238
- parts << "<c:spPr>#{tl_sp_children}</c:spPr>" unless tl_sp_children.empty?
1239
- parts << %(<c:trendlineType val="#{xml_escape(tl[:type] || "linear")}"/>)
1240
- parts << %(<c:order val="#{xml_escape(tl[:order])}"/>) if tl[:order]
1241
- parts << %(<c:period val="#{xml_escape(tl[:period])}"/>) if tl[:period]
1242
- parts << %(<c:forward val="#{xml_escape(tl[:forward])}"/>) if tl[:forward]
1243
- parts << %(<c:backward val="#{xml_escape(tl[:backward])}"/>) if tl[:backward]
1244
- parts << %(<c:intercept val="#{xml_escape(tl[:intercept])}"/>) if tl[:intercept]
1245
- parts << %(<c:dispRSqr val="#{tl[:disp_r_sqr] ? 1 : 0}"/>) unless tl[:disp_r_sqr].nil?
1246
- parts << %(<c:dispEq val="#{tl[:disp_eq] ? 1 : 0}"/>) unless tl[:disp_eq].nil?
1247
- if tl[:label]
1248
- lbl = tl[:label]
1249
- parts << "<c:trendlineLbl>"
1250
- parts << build_title_layout_xml(lbl[:layout])
1251
- parts << "<c:tx><c:rich><a:bodyPr/><a:lstStyle/><a:p><a:r><a:t>#{xml_escape(lbl[:text])}</a:t></a:r></a:p></c:rich></c:tx>" if lbl[:text]
1252
- if lbl[:num_fmt]
1253
- nf = lbl[:num_fmt]
1254
- nf_src = nf.is_a?(Hash) ? nf : { format_code: nf }
1255
- nf_linked = if nf_src.key?(:source_linked)
1256
- nf_src[:source_linked] ? 1 : 0
1257
- else
1258
- 0
1259
- end
1260
- parts << %(<c:numFmt formatCode="#{xml_escape(nf_src[:format_code])}" sourceLinked="#{xml_escape(nf_linked)}"/>)
1261
- end
1262
- tll_sp = +""
1263
- tll_sp << %(<a:solidFill>#{color_xml(lbl[:fill_color])}</a:solidFill>) if lbl[:fill_color]
1264
- tll_sp << "<a:noFill/>" if lbl[:no_fill]
1265
- if lbl[:line_color] || lbl[:line_width] || lbl[:line_dash]
1266
- tll_lw = lbl[:line_width] ? %( w="#{(lbl[:line_width] * 12_700).to_i}") : ""
1267
- tll_lf = lbl[:line_color] ? %(<a:solidFill>#{color_xml(lbl[:line_color])}</a:solidFill>) : ""
1268
- tll_ld = lbl[:line_dash] ? %(<a:prstDash val="#{xml_escape(lbl[:line_dash])}"/>) : ""
1269
- tll_sp << "<a:ln#{tll_lw}>#{tll_lf}#{tll_ld}</a:ln>"
1270
- end
1271
- parts << "<c:spPr>#{tll_sp}</c:spPr>" unless tll_sp.empty?
1272
- parts << build_axis_txpr(nil, lbl[:font]) if lbl[:font]
1273
- parts << "</c:trendlineLbl>"
1393
+ end
1394
+ if ser[:val_ref]
1395
+ xml.tag("c:#{val_tag}") do
1396
+ xml.tag("c:numRef") do
1397
+ xml.text_tag("c:f", ser[:val_ref])
1398
+ write_num_cache_xml(xml, ser[:val_ref])
1274
1399
  end
1275
- parts << "</c:trendline>"
1276
1400
  end
1277
- err_bars_list = ser[:error_bars_list] || (ser[:error_bars] ? [ser[:error_bars]] : [])
1278
- err_bars_list.each do |eb|
1279
- parts << "<c:errBars>"
1280
- parts << %(<c:errDir val="#{xml_escape(eb[:direction])}"/>) if eb[:direction]
1281
- parts << %(<c:errBarType val="#{xml_escape(eb[:bar_type] || "both")}"/>)
1282
- parts << %(<c:errValType val="#{xml_escape(eb[:val_type] || "fixedVal")}"/>)
1283
- parts << %(<c:noEndCap val="#{eb[:no_end_cap] ? 1 : 0}"/>) unless eb[:no_end_cap].nil?
1284
- parts << "<c:plus><c:numRef><c:f>#{xml_escape(eb[:plus])}</c:f></c:numRef></c:plus>" if eb[:plus]
1285
- parts << "<c:minus><c:numRef><c:f>#{xml_escape(eb[:minus])}</c:f></c:numRef></c:minus>" if eb[:minus]
1286
- parts << %(<c:val val="#{xml_escape(eb[:val])}"/>) if eb[:val]
1287
- eb_sp_children = +""
1288
- eb_sp_children << %(<a:solidFill>#{color_xml(eb[:fill_color])}</a:solidFill>) if eb[:fill_color]
1289
- eb_sp_children << "<a:noFill/>" if eb[:no_fill]
1290
- if eb[:line_color] || eb[:line_width] || eb[:line_dash]
1291
- eb_ln_w = eb[:line_width] ? %( w="#{(eb[:line_width] * 12_700).to_i}") : ""
1292
- eb_ln_f = eb[:line_color] ? %(<a:solidFill>#{color_xml(eb[:line_color])}</a:solidFill>) : ""
1293
- eb_ln_d = eb[:line_dash] ? %(<a:prstDash val="#{xml_escape(eb[:line_dash])}"/>) : ""
1294
- eb_sp_children << "<a:ln#{eb_ln_w}>#{eb_ln_f}#{eb_ln_d}</a:ln>"
1401
+ end
1402
+ if ser[:bubble_size_ref]
1403
+ xml.tag("c:bubbleSize") do
1404
+ xml.tag("c:numRef") do
1405
+ xml.text_tag("c:f", ser[:bubble_size_ref])
1406
+ write_num_cache_xml(xml, ser[:bubble_size_ref])
1295
1407
  end
1296
- parts << "<c:spPr>#{eb_sp_children}</c:spPr>" unless eb_sp_children.empty?
1297
- parts << "</c:errBars>"
1298
1408
  end
1299
- uses_xy = %w[scatterChart bubbleChart].include?(g_type)
1300
- cat_tag = uses_xy ? "xVal" : "cat"
1301
- val_tag = uses_xy ? "yVal" : "val"
1302
- if ser[:cat_ref]
1303
- parts << if uses_xy || ser[:cat_ref_type] == :num
1304
- "<c:#{cat_tag}><c:numRef><c:f>#{xml_escape(ser[:cat_ref])}</c:f>#{num_cache_xml(ser[:cat_ref])}</c:numRef></c:#{cat_tag}>"
1305
- else
1306
- "<c:#{cat_tag}><c:strRef><c:f>#{xml_escape(ser[:cat_ref])}</c:f>#{str_cache_xml(ser[:cat_ref])}</c:strRef></c:#{cat_tag}>"
1307
- end
1409
+ end
1410
+ xml.empty_tag("c:smooth", { val: ser[:smooth] ? 1 : 0 }) unless ser[:smooth].nil?
1411
+ xml.empty_tag("c:shape", { val: ser[:shape] }) if ser[:shape]
1412
+ end
1413
+ end
1414
+
1415
+ # : (XmlBuilder xml, untyped ser) -> void
1416
+ def write_series_sp_pr_xml(xml, ser)
1417
+ has_ln = ser[:line_color] || ser[:no_line] || ser[:line_width] || ser[:line_cap] || ser[:line_join] || ser[:line_dash]
1418
+ return unless ser[:fill_color] || ser[:no_fill] || has_ln
1419
+
1420
+ xml.tag("c:spPr") do
1421
+ xml.tag("a:solidFill") { write_drawing_color_xml(xml, ser[:fill_color]) } if ser[:fill_color]
1422
+ xml.empty_tag("a:noFill") if ser[:no_fill]
1423
+ if has_ln
1424
+ lw = ser[:line_width] ? (ser[:line_width] * 12_700).to_i : nil
1425
+ xml.tag("a:ln", { w: lw, cap: ser[:line_cap] }) do
1426
+ xml.empty_tag("a:noFill") if ser[:no_line]
1427
+ xml.tag("a:solidFill") { write_drawing_color_xml(xml, ser[:line_color]) } if ser[:line_color]
1428
+ xml.empty_tag("a:prstDash", { val: ser[:line_dash] }) if ser[:line_dash]
1429
+ write_line_join_xml(xml, ser[:line_join], ser[:line_miter_limit])
1308
1430
  end
1309
- parts << "<c:#{val_tag}><c:numRef><c:f>#{xml_escape(ser[:val_ref])}</c:f>#{num_cache_xml(ser[:val_ref])}</c:numRef></c:#{val_tag}>" if ser[:val_ref]
1310
- parts << "<c:bubbleSize><c:numRef><c:f>#{xml_escape(ser[:bubble_size_ref])}</c:f>#{num_cache_xml(ser[:bubble_size_ref])}</c:numRef></c:bubbleSize>" if ser[:bubble_size_ref]
1311
- parts << %(<c:smooth val="#{ser[:smooth] ? 1 : 0}"/>) unless ser[:smooth].nil?
1312
- parts << %(<c:shape val="#{xml_escape(ser[:shape])}"/>) if ser[:shape]
1313
- parts << "</c:ser>"
1314
- end
1315
-
1316
- if chart[:band_fmts]
1317
- parts << "<c:bandFmts>"
1318
- chart[:band_fmts].each do |bf|
1319
- parts << "<c:bandFmt><c:idx val=\"#{xml_escape(bf[:idx])}\"/>"
1320
- bf_sp = +""
1321
- bf_sp << %(<a:solidFill>#{color_xml(bf[:fill_color])}</a:solidFill>) if bf[:fill_color]
1322
- bf_sp << "<a:noFill/>" if bf[:no_fill]
1323
- if bf[:line_color] || bf[:line_width] || bf[:line_dash]
1324
- bf_lw = bf[:line_width] ? %( w="#{(bf[:line_width] * 12_700).to_i}") : ""
1325
- bf_lf = bf[:line_color] ? %(<a:solidFill>#{color_xml(bf[:line_color])}</a:solidFill>) : ""
1326
- bf_ld = bf[:line_dash] ? %(<a:prstDash val="#{xml_escape(bf[:line_dash])}"/>) : ""
1327
- bf_sp << "<a:ln#{bf_lw}>#{bf_lf}#{bf_ld}</a:ln>"
1431
+ end
1432
+ end
1433
+ end
1434
+
1435
+ # : (XmlBuilder xml, untyped source) -> void
1436
+ def write_marker_xml(xml, source)
1437
+ has_sp = source[:marker_fill] || source[:marker_no_fill] || source[:marker_line_color] || source[:marker_line_dash] || source[:marker_no_line]
1438
+ return unless source[:marker_symbol] || source[:marker_size] || has_sp
1439
+
1440
+ xml.tag("c:marker") do
1441
+ xml.empty_tag("c:symbol", { val: source[:marker_symbol] }) if source[:marker_symbol]
1442
+ xml.empty_tag("c:size", { val: source[:marker_size] }) if source[:marker_size]
1443
+ if has_sp
1444
+ xml.tag("c:spPr") do
1445
+ xml.empty_tag("a:noFill") if source[:marker_no_fill]
1446
+ xml.tag("a:solidFill") { write_drawing_color_xml(xml, source[:marker_fill]) } if source[:marker_fill]
1447
+ if source[:marker_no_line]
1448
+ xml.tag("a:ln") { xml.empty_tag("a:noFill") }
1449
+ elsif source[:marker_line_color] || source[:marker_line_dash]
1450
+ lw = source[:marker_line_width] ? (source[:marker_line_width] * 12_700).to_i : nil
1451
+ xml.tag("a:ln", { w: lw }) do
1452
+ xml.tag("a:solidFill") { write_drawing_color_xml(xml, source[:marker_line_color]) } if source[:marker_line_color]
1453
+ xml.empty_tag("a:prstDash", { val: source[:marker_line_dash] }) if source[:marker_line_dash]
1454
+ end
1328
1455
  end
1329
- parts << "<c:spPr>#{bf_sp}</c:spPr>" unless bf_sp.empty?
1330
- parts << "</c:bandFmt>"
1331
1456
  end
1332
- parts << "</c:bandFmts>"
1333
1457
  end
1458
+ end
1459
+ end
1334
1460
 
1335
- parts << %(<c:gapWidth val="#{xml_escape(chart[:gap_width])}"/>) if chart[:gap_width]
1336
- parts << %(<c:splitType val="#{xml_escape(chart[:split_type])}"/>) if chart[:split_type]
1337
- parts << %(<c:splitPos val="#{xml_escape(chart[:split_pos])}"/>) if chart[:split_pos]
1338
- if chart[:cust_split]&.any?
1339
- parts << "<c:custSplit>"
1340
- chart[:cust_split].each { |idx| parts << %(<c:secondPiePt val="#{xml_escape(idx)}"/>) }
1341
- parts << "</c:custSplit>"
1342
- end
1343
- parts << %(<c:secondPieSize val="#{xml_escape(chart[:second_pie_size])}"/>) if chart[:second_pie_size]
1344
- parts << %(<c:gapDepth val="#{xml_escape(chart[:gap_depth])}"/>) if chart[:gap_depth]
1345
- parts << %(<c:overlap val="#{xml_escape(chart[:overlap])}"/>) if chart[:overlap]
1346
- parts << %(<c:shape val="#{xml_escape(chart[:bar_shape])}"/>) if chart[:bar_shape]
1347
- b3d = chart[:bubble_3d]
1348
- parts << %(<c:bubble3D val="#{b3d ? 1 : 0}"/>) unless b3d.nil?
1349
- parts << %(<c:bubbleScale val="#{xml_escape(chart[:bubble_scale])}"/>) if chart[:bubble_scale]
1350
- snb = chart[:show_neg_bubbles]
1351
- parts << %(<c:showNegBubbles val="#{snb ? 1 : 0}"/>) unless snb.nil?
1352
- parts << %(<c:sizeRepresents val="#{xml_escape(chart[:size_represents])}"/>) if chart[:size_represents]
1353
- # Doughnut charts carry both; Excel's defaults are 0 and 50.
1354
- first_slice_ang = chart[:first_slice_ang] || (0 if g_type == "doughnutChart")
1355
- hole_size = chart[:hole_size] || (50 if g_type == "doughnutChart")
1356
- parts << %(<c:firstSliceAng val="#{xml_escape(first_slice_ang)}"/>) if first_slice_ang
1357
- parts << %(<c:holeSize val="#{xml_escape(hole_size)}"/>) if hole_size
1358
- if chart[:ser_lines]
1359
- sl_spec = chart[:ser_lines]
1360
- if sl_spec.is_a?(Hash)
1361
- sl_sp = +""
1362
- if sl_spec[:line_color] || sl_spec[:line_width] || sl_spec[:line_dash]
1363
- sl_lw = sl_spec[:line_width] ? %( w="#{(sl_spec[:line_width] * 12_700).to_i}") : ""
1364
- sl_lf = sl_spec[:line_color] ? %(<a:solidFill>#{color_xml(sl_spec[:line_color])}</a:solidFill>) : ""
1365
- sl_ld = sl_spec[:line_dash] ? %(<a:prstDash val="#{xml_escape(sl_spec[:line_dash])}"/>) : ""
1366
- sl_sp << "<a:ln#{sl_lw}>#{sl_lf}#{sl_ld}</a:ln>"
1461
+ # : (XmlBuilder xml, untyped data_point) -> void
1462
+ def write_data_point_xml(xml, data_point)
1463
+ xml.tag("c:dPt") do
1464
+ xml.empty_tag("c:idx", { val: data_point[:idx] || "" })
1465
+ xml.empty_tag("c:invertIfNegative", { val: data_point[:invert_if_negative] ? 1 : 0 }) unless data_point[:invert_if_negative].nil?
1466
+ write_marker_xml(xml, data_point)
1467
+ xml.empty_tag("c:bubble3D", { val: data_point[:bubble_3d] ? 1 : 0 }) unless data_point[:bubble_3d].nil?
1468
+ xml.empty_tag("c:explosion", { val: data_point[:explosion] }) if data_point[:explosion]
1469
+ has_ln = data_point[:no_line] || data_point[:line_color] || data_point[:line_width] || data_point[:line_dash]
1470
+ if data_point[:fill_color] || data_point[:no_fill] || has_ln
1471
+ xml.tag("c:spPr") do
1472
+ xml.tag("a:solidFill") { write_drawing_color_xml(xml, data_point[:fill_color]) } if data_point[:fill_color]
1473
+ xml.empty_tag("a:noFill") if data_point[:no_fill]
1474
+ if data_point[:no_line]
1475
+ xml.tag("a:ln") { xml.empty_tag("a:noFill") }
1476
+ elsif data_point[:line_color] || data_point[:line_width] || data_point[:line_dash]
1477
+ write_ln_xml(xml, line_color: data_point[:line_color], line_width: data_point[:line_width], line_dash: data_point[:line_dash])
1367
1478
  end
1368
- parts << (sl_sp.empty? ? "<c:serLines/>" : "<c:serLines><c:spPr>#{sl_sp}</c:spPr></c:serLines>")
1369
- else
1370
- parts << "<c:serLines/>"
1371
1479
  end
1372
1480
  end
1373
- if chart[:drop_lines]
1374
- dl_spec = chart[:drop_lines]
1375
- if dl_spec.is_a?(Hash)
1376
- dl_sp = +""
1377
- if dl_spec[:line_color] || dl_spec[:line_width] || dl_spec[:line_dash]
1378
- dl_lw = dl_spec[:line_width] ? %( w="#{(dl_spec[:line_width] * 12_700).to_i}") : ""
1379
- dl_lf = dl_spec[:line_color] ? %(<a:solidFill>#{color_xml(dl_spec[:line_color])}</a:solidFill>) : ""
1380
- dl_ld = dl_spec[:line_dash] ? %(<a:prstDash val="#{xml_escape(dl_spec[:line_dash])}"/>) : ""
1381
- dl_sp << "<a:ln#{dl_lw}>#{dl_lf}#{dl_ld}</a:ln>"
1481
+ end
1482
+ end
1483
+
1484
+ # : (XmlBuilder xml, untyped data_labels) -> void
1485
+ def write_data_labels_xml(xml, data_labels)
1486
+ xml.tag("c:dLbls") do
1487
+ data_labels[:labels]&.each do |lbl|
1488
+ xml.tag("c:dLbl") do
1489
+ xml.empty_tag("c:idx", { val: lbl[:idx] || "" })
1490
+ if lbl[:delete]
1491
+ xml.empty_tag("c:delete", { val: 1 })
1492
+ else
1493
+ if (lbl_layout = lbl[:layout])
1494
+ ml_entries = [["c:x", lbl_layout[:x]], ["c:y", lbl_layout[:y]]].select { |_, val| val }
1495
+ if ml_entries.empty?
1496
+ xml.empty_tag("c:layout")
1497
+ else
1498
+ xml.tag("c:layout") do
1499
+ xml.tag("c:manualLayout") do
1500
+ ml_entries.each { |tag_name, val| xml.empty_tag(tag_name, { val: val }) }
1501
+ end
1502
+ end
1503
+ end
1504
+ end
1505
+ if lbl[:text]
1506
+ xml.tag("c:tx") do
1507
+ xml.tag("c:rich") do
1508
+ xml.empty_tag("a:bodyPr")
1509
+ xml.empty_tag("a:lstStyle")
1510
+ xml.tag("a:p") { xml.tag("a:r") { xml.text_tag("a:t", lbl[:text]) } }
1511
+ end
1512
+ end
1513
+ end
1514
+ if (lnf = lbl[:num_fmt])
1515
+ xml.empty_tag("c:numFmt", { formatCode: lnf[:format_code] || "", sourceLinked: (1 if lnf[:source_linked]) })
1516
+ end
1517
+ write_simple_sp_pr_from_hash(xml, lbl)
1518
+ write_axis_txpr(xml, nil, lbl[:font]) if lbl[:font]
1519
+ write_data_label_flags_xml(xml, lbl)
1382
1520
  end
1383
- parts << (dl_sp.empty? ? "<c:dropLines/>" : "<c:dropLines><c:spPr>#{dl_sp}</c:spPr></c:dropLines>")
1384
- else
1385
- parts << "<c:dropLines/>"
1386
1521
  end
1387
1522
  end
1388
- if chart[:hi_low_lines]
1389
- hl_spec = chart[:hi_low_lines]
1390
- if hl_spec.is_a?(Hash)
1391
- hl_sp = +""
1392
- if hl_spec[:line_color] || hl_spec[:line_width] || hl_spec[:line_dash]
1393
- hl_lw = hl_spec[:line_width] ? %( w="#{(hl_spec[:line_width] * 12_700).to_i}") : ""
1394
- hl_lf = hl_spec[:line_color] ? %(<a:solidFill>#{color_xml(hl_spec[:line_color])}</a:solidFill>) : ""
1395
- hl_ld = hl_spec[:line_dash] ? %(<a:prstDash val="#{xml_escape(hl_spec[:line_dash])}"/>) : ""
1396
- hl_sp << "<a:ln#{hl_lw}>#{hl_lf}#{hl_ld}</a:ln>"
1397
- end
1398
- parts << (hl_sp.empty? ? "<c:hiLowLines/>" : "<c:hiLowLines><c:spPr>#{hl_sp}</c:spPr></c:hiLowLines>")
1399
- else
1400
- parts << "<c:hiLowLines/>"
1523
+ if (nf = data_labels[:num_fmt])
1524
+ xml.empty_tag("c:numFmt", { formatCode: nf[:format_code] || "", sourceLinked: (1 if nf[:source_linked]) })
1525
+ end
1526
+ write_simple_sp_pr_from_hash(xml, data_labels)
1527
+ write_axis_txpr(xml, nil, data_labels[:font]) if data_labels[:font]
1528
+ write_data_label_flags_xml(xml, data_labels)
1529
+ xml.empty_tag("c:showLeaderLines", { val: data_labels[:show_leader_lines] ? 1 : 0 }) unless data_labels[:show_leader_lines].nil?
1530
+ write_line_sp_tag(xml, "c:leaderLines", data_labels[:leader_lines]) if data_labels[:leader_lines]
1531
+ end
1532
+ end
1533
+
1534
+ # : (XmlBuilder xml, untyped lbl) -> void
1535
+ def write_data_label_flags_xml(xml, lbl)
1536
+ xml.empty_tag("c:dLblPos", { val: lbl[:position] }) if lbl[:position]
1537
+ xml.empty_tag("c:showLegendKey", { val: lbl[:show_legend_key] ? 1 : 0 }) unless lbl[:show_legend_key].nil?
1538
+ xml.empty_tag("c:showVal", { val: lbl[:show_val] ? 1 : 0 }) unless lbl[:show_val].nil?
1539
+ xml.empty_tag("c:showCatName", { val: lbl[:show_cat_name] ? 1 : 0 }) unless lbl[:show_cat_name].nil?
1540
+ xml.empty_tag("c:showSerName", { val: lbl[:show_ser_name] ? 1 : 0 }) unless lbl[:show_ser_name].nil?
1541
+ xml.empty_tag("c:showPercent", { val: lbl[:show_percent] ? 1 : 0 }) unless lbl[:show_percent].nil?
1542
+ xml.empty_tag("c:showBubbleSize", { val: lbl[:show_bubble_size] ? 1 : 0 }) unless lbl[:show_bubble_size].nil?
1543
+ xml.text_tag("c:separator", lbl[:separator]) if lbl[:separator]
1544
+ end
1545
+
1546
+ # : (XmlBuilder xml, untyped trendline) -> void
1547
+ def write_trendline_xml(xml, trendline)
1548
+ xml.tag("c:trendline") do
1549
+ xml.text_tag("c:name", trendline[:name]) if trendline[:name]
1550
+ if trendline[:line_color] || trendline[:line_width] || trendline[:line_dash]
1551
+ xml.tag("c:spPr") do
1552
+ write_ln_xml(xml, line_color: trendline[:line_color], line_width: trendline[:line_width], line_dash: trendline[:line_dash])
1401
1553
  end
1402
1554
  end
1403
- if chart[:up_down_bars]
1404
- udb = chart[:up_down_bars]
1405
- parts << "<c:upDownBars>"
1406
- parts << %(<c:gapWidth val="#{xml_escape(udb[:gap_width])}"/>) if udb.is_a?(Hash) && udb[:gap_width]
1407
- %i[up_bars down_bars].each do |bar_key|
1408
- tag = bar_key == :up_bars ? "upBars" : "downBars"
1409
- bar = udb.is_a?(Hash) ? udb[bar_key] : nil
1410
- if bar
1411
- bar_sp = +""
1412
- bar_sp << %(<a:solidFill>#{color_xml(bar[:fill_color])}</a:solidFill>) if bar[:fill_color]
1413
- bar_sp << "<a:noFill/>" if bar[:no_fill]
1414
- if bar[:line_color] || bar[:line_width] || bar[:line_dash]
1415
- b_lw = bar[:line_width] ? %( w="#{(bar[:line_width] * 12_700).to_i}") : ""
1416
- b_lf = bar[:line_color] ? %(<a:solidFill>#{color_xml(bar[:line_color])}</a:solidFill>) : ""
1417
- b_ld = bar[:line_dash] ? %(<a:prstDash val="#{xml_escape(bar[:line_dash])}"/>) : ""
1418
- bar_sp << "<a:ln#{b_lw}>#{b_lf}#{b_ld}</a:ln>"
1555
+ xml.empty_tag("c:trendlineType", { val: trendline[:type] || "linear" })
1556
+ xml.empty_tag("c:order", { val: trendline[:order] }) if trendline[:order]
1557
+ xml.empty_tag("c:period", { val: trendline[:period] }) if trendline[:period]
1558
+ xml.empty_tag("c:forward", { val: trendline[:forward] }) if trendline[:forward]
1559
+ xml.empty_tag("c:backward", { val: trendline[:backward] }) if trendline[:backward]
1560
+ xml.empty_tag("c:intercept", { val: trendline[:intercept] }) if trendline[:intercept]
1561
+ xml.empty_tag("c:dispRSqr", { val: trendline[:disp_r_sqr] ? 1 : 0 }) unless trendline[:disp_r_sqr].nil?
1562
+ xml.empty_tag("c:dispEq", { val: trendline[:disp_eq] ? 1 : 0 }) unless trendline[:disp_eq].nil?
1563
+ if (lbl = trendline[:label])
1564
+ xml.tag("c:trendlineLbl") do
1565
+ write_title_layout_xml(xml, lbl[:layout])
1566
+ if lbl[:text]
1567
+ xml.tag("c:tx") do
1568
+ xml.tag("c:rich") do
1569
+ xml.empty_tag("a:bodyPr")
1570
+ xml.empty_tag("a:lstStyle")
1571
+ xml.tag("a:p") { xml.tag("a:r") { xml.text_tag("a:t", lbl[:text]) } }
1572
+ end
1419
1573
  end
1420
- parts << (bar_sp.empty? ? "<c:#{tag}/>" : "<c:#{tag}><c:spPr>#{bar_sp}</c:spPr></c:#{tag}>")
1421
- else
1422
- parts << "<c:#{tag}/>"
1423
1574
  end
1575
+ if (nf = lbl[:num_fmt])
1576
+ nf_src = nf.is_a?(Hash) ? nf : { format_code: nf }
1577
+ nf_linked = nf_src[:source_linked] ? 1 : 0
1578
+ xml.empty_tag("c:numFmt", { formatCode: nf_src[:format_code] || "", sourceLinked: nf_linked })
1579
+ end
1580
+ write_simple_sp_pr_from_hash(xml, lbl)
1581
+ write_axis_txpr(xml, nil, lbl[:font]) if lbl[:font]
1424
1582
  end
1425
- parts << "</c:upDownBars>"
1426
1583
  end
1427
- mk = chart[:marker]
1428
- parts << %(<c:marker val="#{mk ? 1 : 0}"/>) unless mk.nil?
1429
- sm = chart[:smooth]
1430
- parts << %(<c:smooth val="#{sm ? 1 : 0}"/>) unless sm.nil?
1431
- three_axes = THREE_AXIS_CHARTS.include?(g_type)
1432
- unless g_no_axes
1433
- parts << %(<c:axId val="1"/><c:axId val="#{xml_escape(g_val_ax)}"/>)
1434
- parts << '<c:axId val="3"/>' if three_axes
1584
+ end
1585
+ end
1586
+
1587
+ # : (XmlBuilder xml, untyped err_bars) -> void
1588
+ def write_error_bars_xml(xml, err_bars)
1589
+ xml.tag("c:errBars") do
1590
+ xml.empty_tag("c:errDir", { val: err_bars[:direction] }) if err_bars[:direction]
1591
+ xml.empty_tag("c:errBarType", { val: err_bars[:bar_type] || "both" })
1592
+ xml.empty_tag("c:errValType", { val: err_bars[:val_type] || "fixedVal" })
1593
+ xml.empty_tag("c:noEndCap", { val: err_bars[:no_end_cap] ? 1 : 0 }) unless err_bars[:no_end_cap].nil?
1594
+ xml.tag("c:plus") { xml.tag("c:numRef") { xml.text_tag("c:f", err_bars[:plus]) } } if err_bars[:plus]
1595
+ xml.tag("c:minus") { xml.tag("c:numRef") { xml.text_tag("c:f", err_bars[:minus]) } } if err_bars[:minus]
1596
+ xml.empty_tag("c:val", { val: err_bars[:val] }) if err_bars[:val]
1597
+ write_simple_sp_pr_from_hash(xml, err_bars)
1598
+ end
1599
+ end
1600
+
1601
+ # : (XmlBuilder xml, untyped band_fmts) -> void
1602
+ def write_band_fmts_xml(xml, band_fmts)
1603
+ xml.tag("c:bandFmts") do
1604
+ band_fmts.each do |bf|
1605
+ xml.tag("c:bandFmt") do
1606
+ xml.empty_tag("c:idx", { val: bf[:idx] || "" })
1607
+ write_simple_sp_pr_from_hash(xml, bf)
1608
+ end
1435
1609
  end
1436
- parts << "</c:#{g_type}>"
1437
1610
  end
1611
+ end
1438
1612
 
1439
- has_axes = groups.any? { |g| !NO_AXIS_CHARTS.include?(g[:type]) }
1440
- if has_axes
1441
- cat_del = chart[:cat_axis_delete] ? 1 : 0
1442
- cat_orient = chart[:cat_axis_orientation] || "minMax"
1443
- cat_ax_tag = chart[:cat_axis_type] == :date ? "dateAx" : "catAx"
1444
- parts << %(<c:#{cat_ax_tag}><c:axId val="1"/><c:scaling>)
1445
- parts << %(<c:logBase val="#{xml_escape(chart[:cat_axis_log_base])}"/>) if chart[:cat_axis_log_base]
1446
- parts << %(<c:orientation val="#{xml_escape(cat_orient)}"/>)
1447
- parts << %(<c:max val="#{xml_escape(chart[:cat_axis_scaling_max])}"/>) if chart[:cat_axis_scaling_max]
1448
- parts << %(<c:min val="#{xml_escape(chart[:cat_axis_scaling_min])}"/>) if chart[:cat_axis_scaling_min]
1449
- parts << %(</c:scaling><c:delete val="#{xml_escape(cat_del)}"/><c:axPos val="#{xml_escape(chart[:cat_axis_pos] || "b")}"/>)
1450
- parts << gridlines_xml("majorGridlines", chart[:cat_axis_major_gridlines])
1451
- parts << gridlines_xml("minorGridlines", chart[:cat_axis_minor_gridlines])
1613
+ # : (XmlBuilder xml, untyped udb) -> void
1614
+ def write_up_down_bars_xml(xml, udb)
1615
+ xml.tag("c:upDownBars") do
1616
+ xml.empty_tag("c:gapWidth", { val: udb[:gap_width] }) if udb.is_a?(Hash) && udb[:gap_width]
1617
+ %i[up_bars down_bars].each do |bar_key|
1618
+ tag = bar_key == :up_bars ? "c:upBars" : "c:downBars"
1619
+ bar = udb.is_a?(Hash) ? udb[bar_key] : nil
1620
+ if bar && (bar[:fill_color] || bar[:no_fill] || bar[:line_color] || bar[:line_width] || bar[:line_dash])
1621
+ xml.tag(tag) { write_simple_sp_pr_from_hash(xml, bar) }
1622
+ else
1623
+ xml.empty_tag(tag)
1624
+ end
1625
+ end
1626
+ end
1627
+ end
1628
+
1629
+ # : (XmlBuilder xml, untyped chart, ::Array[untyped] groups, bool has_secondary) -> void
1630
+ def write_chart_axes_xml(xml, chart, groups, has_secondary)
1631
+ cat_ax_tag = chart[:cat_axis_type] == :date ? "cat_date" : "cat_ax"
1632
+ el_name = cat_ax_tag == "cat_date" ? "c:dateAx" : "c:catAx"
1633
+ xml.tag(el_name) do
1634
+ xml.empty_tag("c:axId", { val: 1 })
1635
+ xml.tag("c:scaling") do
1636
+ xml.empty_tag("c:logBase", { val: chart[:cat_axis_log_base] }) if chart[:cat_axis_log_base]
1637
+ xml.empty_tag("c:orientation", { val: chart[:cat_axis_orientation] || "minMax" })
1638
+ xml.empty_tag("c:max", { val: chart[:cat_axis_scaling_max] }) if chart[:cat_axis_scaling_max]
1639
+ xml.empty_tag("c:min", { val: chart[:cat_axis_scaling_min] }) if chart[:cat_axis_scaling_min]
1640
+ end
1641
+ xml.empty_tag("c:delete", { val: chart[:cat_axis_delete] ? 1 : 0 })
1642
+ xml.empty_tag("c:axPos", { val: chart[:cat_axis_pos] || "b" })
1643
+ write_gridlines_xml(xml, "majorGridlines", chart[:cat_axis_major_gridlines]) if chart[:cat_axis_major_gridlines]
1644
+ write_gridlines_xml(xml, "minorGridlines", chart[:cat_axis_minor_gridlines]) if chart[:cat_axis_minor_gridlines]
1452
1645
  if chart[:cat_axis_title]
1453
1646
  cat_title_spec = merge_flat_title_styling(chart[:cat_axis_title], chart, :cat_axis_title)
1454
- parts << build_chart_title_xml(cat_title_spec)
1647
+ write_chart_title_xml(xml, cat_title_spec)
1455
1648
  end
1456
1649
  if (cnf = chart[:cat_axis_num_fmt])
1457
- sl = cnf[:source_linked] ? 1 : 0
1458
- parts << %(<c:numFmt formatCode="#{xml_escape(cnf[:format_code])}" sourceLinked="#{xml_escape(sl)}"/>)
1650
+ xml.empty_tag("c:numFmt", { formatCode: cnf[:format_code] || "", sourceLinked: cnf[:source_linked] ? 1 : 0 })
1459
1651
  end
1460
- parts << %(<c:majorTickMark val="#{xml_escape(chart[:cat_axis_major_tick_mark])}"/>) if chart[:cat_axis_major_tick_mark]
1461
- parts << %(<c:minorTickMark val="#{xml_escape(chart[:cat_axis_minor_tick_mark])}"/>) if chart[:cat_axis_minor_tick_mark]
1462
- parts << %(<c:tickLblPos val="#{xml_escape(chart[:cat_axis_tick_lbl_pos])}"/>) if chart[:cat_axis_tick_lbl_pos]
1652
+ xml.empty_tag("c:majorTickMark", { val: chart[:cat_axis_major_tick_mark] }) if chart[:cat_axis_major_tick_mark]
1653
+ xml.empty_tag("c:minorTickMark", { val: chart[:cat_axis_minor_tick_mark] }) if chart[:cat_axis_minor_tick_mark]
1654
+ xml.empty_tag("c:tickLblPos", { val: chart[:cat_axis_tick_lbl_pos] }) if chart[:cat_axis_tick_lbl_pos]
1463
1655
  if chart[:cat_axis_line_color] || chart[:cat_axis_fill] || chart[:cat_axis_no_fill] || chart[:cat_axis_line_dash]
1464
- parts << "<c:spPr>"
1465
- parts << "<a:noFill/>" if chart[:cat_axis_no_fill]
1466
- parts << %(<a:solidFill>#{color_xml(chart[:cat_axis_fill])}</a:solidFill>) if chart[:cat_axis_fill]
1467
- if chart[:cat_axis_line_color] || chart[:cat_axis_line_dash]
1468
- w_attr = chart[:cat_axis_line_width] ? %( w="#{xml_escape(chart[:cat_axis_line_width])}") : ""
1469
- ca_ln_f = chart[:cat_axis_line_color] ? %(<a:solidFill>#{srgb_clr_xml(chart[:cat_axis_line_color])}</a:solidFill>) : ""
1470
- ca_ln_d = chart[:cat_axis_line_dash] ? %(<a:prstDash val="#{xml_escape(chart[:cat_axis_line_dash])}"/>) : ""
1471
- parts << "<a:ln#{w_attr}>#{ca_ln_f}#{ca_ln_d}</a:ln>"
1656
+ xml.tag("c:spPr") do
1657
+ xml.empty_tag("a:noFill") if chart[:cat_axis_no_fill]
1658
+ xml.tag("a:solidFill") { write_drawing_color_xml(xml, chart[:cat_axis_fill]) } if chart[:cat_axis_fill]
1659
+ if chart[:cat_axis_line_color] || chart[:cat_axis_line_dash]
1660
+ xml.tag("a:ln", { w: chart[:cat_axis_line_width] }) do
1661
+ xml.tag("a:solidFill") { write_drawing_color_xml(xml, chart[:cat_axis_line_color]) } if chart[:cat_axis_line_color]
1662
+ xml.empty_tag("a:prstDash", { val: chart[:cat_axis_line_dash] }) if chart[:cat_axis_line_dash]
1663
+ end
1664
+ end
1472
1665
  end
1473
- parts << "</c:spPr>"
1474
- end
1475
- parts << build_axis_txpr(chart[:cat_axis_label_rotation], chart[:cat_axis_font])
1476
- parts << '<c:crossAx val="2"/>'
1477
- parts << %(<c:crosses val="#{xml_escape(chart[:cat_axis_crosses])}"/>) if chart[:cat_axis_crosses]
1478
- parts << %(<c:crossesAt val="#{xml_escape(chart[:cat_axis_crosses_at])}"/>) if !chart[:cat_axis_crosses] && chart[:cat_axis_crosses_at]
1479
- ca_auto = chart[:cat_axis_auto]
1480
- parts << %(<c:auto val="#{ca_auto ? 1 : 0}"/>) unless ca_auto.nil?
1481
- parts << %(<c:lblAlgn val="#{xml_escape(chart[:cat_axis_lbl_algn])}"/>) if chart[:cat_axis_lbl_algn]
1482
- parts << %(<c:lblOffset val="#{xml_escape(chart[:cat_axis_lbl_offset])}"/>) if chart[:cat_axis_lbl_offset]
1483
- parts << %(<c:tickLblSkip val="#{xml_escape(chart[:cat_axis_tick_lbl_skip])}"/>) if chart[:cat_axis_tick_lbl_skip]
1484
- parts << %(<c:tickMarkSkip val="#{xml_escape(chart[:cat_axis_tick_mark_skip])}"/>) if chart[:cat_axis_tick_mark_skip]
1485
- nml = chart[:cat_axis_no_multi_lvl_lbl]
1486
- parts << %(<c:noMultiLvlLbl val="#{nml ? 1 : 0}"/>) unless nml.nil?
1487
- if cat_ax_tag == "dateAx"
1488
- parts << %(<c:baseTimeUnit val="#{xml_escape(chart[:cat_axis_base_time_unit])}"/>) if chart[:cat_axis_base_time_unit]
1489
- parts << %(<c:majorUnit val="#{xml_escape(chart[:cat_axis_major_unit])}"/>) if chart[:cat_axis_major_unit]
1490
- parts << %(<c:majorTimeUnit val="#{xml_escape(chart[:cat_axis_major_time_unit])}"/>) if chart[:cat_axis_major_time_unit]
1491
- parts << %(<c:minorUnit val="#{xml_escape(chart[:cat_axis_minor_unit])}"/>) if chart[:cat_axis_minor_unit]
1492
- parts << %(<c:minorTimeUnit val="#{xml_escape(chart[:cat_axis_minor_time_unit])}"/>) if chart[:cat_axis_minor_time_unit]
1493
- end
1494
- parts << "</c:#{cat_ax_tag}>"
1495
-
1496
- parts << build_val_ax_xml(ax_id: 2, prefix: :val_axis, chart: chart, default_pos: "l")
1497
- parts << build_val_ax_xml(ax_id: 3, prefix: :sec_val_axis, chart: chart, default_pos: "r", default_crosses: "max") if has_secondary
1498
-
1499
- if groups.any? { |g| THREE_AXIS_CHARTS.include?(g[:type]) }
1500
- parts << '<c:serAx><c:axId val="3"/><c:scaling><c:orientation val="minMax"/></c:scaling>'
1501
- parts << '<c:delete val="0"/><c:axPos val="b"/><c:crossAx val="2"/></c:serAx>'
1666
+ end
1667
+ write_axis_txpr(xml, chart[:cat_axis_label_rotation], chart[:cat_axis_font])
1668
+ xml.empty_tag("c:crossAx", { val: 2 })
1669
+ xml.empty_tag("c:crosses", { val: chart[:cat_axis_crosses] }) if chart[:cat_axis_crosses]
1670
+ xml.empty_tag("c:crossesAt", { val: chart[:cat_axis_crosses_at] }) if !chart[:cat_axis_crosses] && chart[:cat_axis_crosses_at]
1671
+ xml.empty_tag("c:auto", { val: chart[:cat_axis_auto] ? 1 : 0 }) unless chart[:cat_axis_auto].nil?
1672
+ xml.empty_tag("c:lblAlgn", { val: chart[:cat_axis_lbl_algn] }) if chart[:cat_axis_lbl_algn]
1673
+ xml.empty_tag("c:lblOffset", { val: chart[:cat_axis_lbl_offset] }) if chart[:cat_axis_lbl_offset]
1674
+ xml.empty_tag("c:tickLblSkip", { val: chart[:cat_axis_tick_lbl_skip] }) if chart[:cat_axis_tick_lbl_skip]
1675
+ xml.empty_tag("c:tickMarkSkip", { val: chart[:cat_axis_tick_mark_skip] }) if chart[:cat_axis_tick_mark_skip]
1676
+ xml.empty_tag("c:noMultiLvlLbl", { val: chart[:cat_axis_no_multi_lvl_lbl] ? 1 : 0 }) unless chart[:cat_axis_no_multi_lvl_lbl].nil?
1677
+ if el_name == "c:dateAx"
1678
+ xml.empty_tag("c:baseTimeUnit", { val: chart[:cat_axis_base_time_unit] }) if chart[:cat_axis_base_time_unit]
1679
+ xml.empty_tag("c:majorUnit", { val: chart[:cat_axis_major_unit] }) if chart[:cat_axis_major_unit]
1680
+ xml.empty_tag("c:majorTimeUnit", { val: chart[:cat_axis_major_time_unit] }) if chart[:cat_axis_major_time_unit]
1681
+ xml.empty_tag("c:minorUnit", { val: chart[:cat_axis_minor_unit] }) if chart[:cat_axis_minor_unit]
1682
+ xml.empty_tag("c:minorTimeUnit", { val: chart[:cat_axis_minor_time_unit] }) if chart[:cat_axis_minor_time_unit]
1502
1683
  end
1503
1684
  end
1504
1685
 
1505
- if chart[:data_table]
1506
- dt = chart[:data_table]
1507
- parts << "<c:dTable>"
1508
- parts << %(<c:showHorzBorder val="#{dt[:show_horz_border] ? 1 : 0}"/>) unless dt[:show_horz_border].nil?
1509
- parts << %(<c:showVertBorder val="#{dt[:show_vert_border] ? 1 : 0}"/>) unless dt[:show_vert_border].nil?
1510
- parts << %(<c:showOutline val="#{dt[:show_outline] ? 1 : 0}"/>) unless dt[:show_outline].nil?
1511
- parts << %(<c:showKeys val="#{dt[:show_keys] ? 1 : 0}"/>) unless dt[:show_keys].nil?
1512
- dt_sp_children = +""
1513
- dt_sp_children << %(<a:solidFill>#{color_xml(dt[:fill_color])}</a:solidFill>) if dt[:fill_color]
1514
- dt_sp_children << "<a:noFill/>" if dt[:no_fill]
1515
- if dt[:line_color] || dt[:line_width] || dt[:line_dash]
1516
- dt_ln_w = dt[:line_width] ? %( w="#{(dt[:line_width] * 12_700).to_i}") : ""
1517
- dt_ln_f = dt[:line_color] ? %(<a:solidFill>#{color_xml(dt[:line_color])}</a:solidFill>) : ""
1518
- dt_ln_d = dt[:line_dash] ? %(<a:prstDash val="#{xml_escape(dt[:line_dash])}"/>) : ""
1519
- dt_sp_children << "<a:ln#{dt_ln_w}>#{dt_ln_f}#{dt_ln_d}</a:ln>"
1520
- end
1521
- parts << "<c:spPr>#{dt_sp_children}</c:spPr>" unless dt_sp_children.empty?
1522
- parts << build_axis_txpr(nil, dt[:font]) if dt[:font]
1523
- parts << "</c:dTable>"
1524
- end
1686
+ write_val_ax_xml(xml, ax_id: 2, prefix: :val_axis, chart: chart, default_pos: "l")
1687
+ write_val_ax_xml(xml, ax_id: 3, prefix: :sec_val_axis, chart: chart, default_pos: "r", default_crosses: "max") if has_secondary
1525
1688
 
1526
- if chart[:plot_area_fill] || chart[:plot_area_no_fill] || chart[:plot_area_line_color] || chart[:plot_area_line_dash]
1527
- pa_sp = +""
1528
- pa_sp << %(<a:solidFill>#{color_xml(chart[:plot_area_fill])}</a:solidFill>) if chart[:plot_area_fill]
1529
- pa_sp << "<a:noFill/>" if chart[:plot_area_no_fill]
1530
- if chart[:plot_area_line_color] || chart[:plot_area_line_dash]
1531
- pa_ln_w = chart[:plot_area_line_width] ? %( w="#{chart[:plot_area_line_width].to_i}") : ""
1532
- pa_ln_f = chart[:plot_area_line_color] ? %(<a:solidFill>#{color_xml(chart[:plot_area_line_color])}</a:solidFill>) : ""
1533
- pa_ln_d = chart[:plot_area_line_dash] ? %(<a:prstDash val="#{xml_escape(chart[:plot_area_line_dash])}"/>) : ""
1534
- pa_sp << "<a:ln#{pa_ln_w}>#{pa_ln_f}#{pa_ln_d}</a:ln>"
1535
- end
1536
- parts << "<c:spPr>#{pa_sp}</c:spPr>"
1537
- end
1689
+ return unless groups.any? { |g| THREE_AXIS_CHARTS.include?(g[:type]) }
1538
1690
 
1539
- parts << "</c:plotArea>"
1540
- legend_pos = chart.dig(:legend, :position) || "r"
1541
- legend_overlay = chart.dig(:legend, :overlay)
1542
- parts << %(<c:legend><c:legendPos val="#{xml_escape(legend_pos)}"/>)
1543
- legend_entries = chart.dig(:legend, :entries)
1544
- legend_entries&.each do |entry|
1545
- parts << %(<c:legendEntry><c:idx val="#{xml_escape(entry[:idx])}"/>)
1546
- if !entry[:delete].nil?
1547
- parts << %(<c:delete val="#{entry[:delete] ? 1 : 0}"/>)
1548
- elsif entry[:font]
1549
- parts << build_axis_txpr(nil, entry[:font])
1550
- end
1551
- parts << "</c:legendEntry>"
1552
- end
1553
- legend_layout = chart.dig(:legend, :layout)
1554
- if legend_layout.is_a?(Hash)
1555
- ml_parts = +""
1556
- ml_parts << %(<c:layoutTarget val="#{xml_escape(legend_layout[:target])}"/>) if legend_layout[:target]
1557
- ml_parts << %(<c:xMode val="#{xml_escape(legend_layout[:x_mode])}"/>) if legend_layout[:x_mode]
1558
- ml_parts << %(<c:yMode val="#{xml_escape(legend_layout[:y_mode])}"/>) if legend_layout[:y_mode]
1559
- ml_parts << %(<c:wMode val="#{xml_escape(legend_layout[:w_mode])}"/>) if legend_layout[:w_mode]
1560
- ml_parts << %(<c:hMode val="#{xml_escape(legend_layout[:h_mode])}"/>) if legend_layout[:h_mode]
1561
- ml_parts << %(<c:x val="#{xml_escape(legend_layout[:x])}"/>) if legend_layout[:x]
1562
- ml_parts << %(<c:y val="#{xml_escape(legend_layout[:y])}"/>) if legend_layout[:y]
1563
- ml_parts << %(<c:w val="#{xml_escape(legend_layout[:w])}"/>) if legend_layout[:w]
1564
- ml_parts << %(<c:h val="#{xml_escape(legend_layout[:h])}"/>) if legend_layout[:h]
1565
- parts << "<c:layout><c:manualLayout>#{ml_parts}</c:manualLayout></c:layout>" unless ml_parts.empty?
1566
- end
1567
- parts << %(<c:overlay val="#{legend_overlay ? 1 : 0}"/>) unless legend_overlay.nil?
1568
- leg_sp_children = +""
1569
- leg_fill = chart.dig(:legend, :fill_color)
1570
- leg_sp_children << %(<a:solidFill>#{color_xml(leg_fill)}</a:solidFill>) if leg_fill
1571
- leg_no_fill = chart.dig(:legend, :no_fill)
1572
- leg_sp_children << "<a:noFill/>" if leg_no_fill
1573
- leg_lc = chart.dig(:legend, :line_color)
1574
- leg_lw = chart.dig(:legend, :line_width)
1575
- leg_ld = chart.dig(:legend, :line_dash)
1576
- if leg_lc || leg_lw || leg_ld
1577
- lw_attr = leg_lw ? %( w="#{(leg_lw * 12_700).to_i}") : ""
1578
- lf = leg_lc ? %(<a:solidFill>#{color_xml(leg_lc)}</a:solidFill>) : ""
1579
- ld = leg_ld ? %(<a:prstDash val="#{xml_escape(leg_ld)}"/>) : ""
1580
- leg_sp_children << "<a:ln#{lw_attr}>#{lf}#{ld}</a:ln>"
1581
- end
1582
- parts << "<c:spPr>#{leg_sp_children}</c:spPr>" unless leg_sp_children.empty?
1583
- if (lfont = chart.dig(:legend, :font))
1584
- parts << build_axis_txpr(nil, lfont)
1691
+ xml.tag("c:serAx") do
1692
+ xml.empty_tag("c:axId", { val: 3 })
1693
+ xml.tag("c:scaling") { xml.empty_tag("c:orientation", { val: "minMax" }) }
1694
+ xml.empty_tag("c:delete", { val: 0 })
1695
+ xml.empty_tag("c:axPos", { val: "b" })
1696
+ xml.empty_tag("c:crossAx", { val: 2 })
1585
1697
  end
1586
- parts << %(</c:legend>)
1587
- pvo = chart[:plot_vis_only]
1588
- parts << %(<c:plotVisOnly val="#{pvo ? 1 : 0}"/>) unless pvo.nil?
1589
- parts << %(<c:dispBlanksAs val="#{xml_escape(chart[:disp_blanks_as])}"/>) if chart[:disp_blanks_as]
1590
- sdom = chart[:show_d_lbls_over_max]
1591
- parts << %(<c:showDLblsOverMax val="#{sdom ? 1 : 0}"/>) unless sdom.nil?
1592
- parts << "</c:chart>"
1593
- if chart[:chart_fill] || chart[:chart_no_fill] || chart[:chart_line_color] || chart[:chart_line_dash]
1594
- cs_sp = +""
1595
- cs_sp << %(<a:solidFill>#{color_xml(chart[:chart_fill])}</a:solidFill>) if chart[:chart_fill]
1596
- cs_sp << "<a:noFill/>" if chart[:chart_no_fill]
1597
- if chart[:chart_line_color] || chart[:chart_line_dash]
1598
- cs_lw = chart[:chart_line_width] ? %( w="#{(chart[:chart_line_width] * 12_700).to_i}") : ""
1599
- cs_ln_f = chart[:chart_line_color] ? %(<a:solidFill>#{color_xml(chart[:chart_line_color])}</a:solidFill>) : ""
1600
- cs_ln_d = chart[:chart_line_dash] ? %(<a:prstDash val="#{xml_escape(chart[:chart_line_dash])}"/>) : ""
1601
- cs_sp << "<a:ln#{cs_lw}>#{cs_ln_f}#{cs_ln_d}</a:ln>"
1602
- end
1603
- parts << "<c:spPr>#{cs_sp}</c:spPr>"
1604
- end
1605
- parts << build_axis_txpr(nil, chart[:chart_font]) if chart[:chart_font]
1606
- if (ps = chart[:print_settings])
1607
- parts << "<c:printSettings>"
1608
- if (hf = ps[:header_footer])
1609
- parts << "<c:headerFooter>"
1610
- parts << "<c:oddHeader>#{xml_escape(hf[:odd_header])}</c:oddHeader>" if hf[:odd_header]
1611
- parts << "<c:oddFooter>#{xml_escape(hf[:odd_footer])}</c:oddFooter>" if hf[:odd_footer]
1612
- parts << "<c:evenHeader>#{xml_escape(hf[:even_header])}</c:evenHeader>" if hf[:even_header]
1613
- parts << "<c:evenFooter>#{xml_escape(hf[:even_footer])}</c:evenFooter>" if hf[:even_footer]
1614
- parts << "<c:firstHeader>#{xml_escape(hf[:first_header])}</c:firstHeader>" if hf[:first_header]
1615
- parts << "<c:firstFooter>#{xml_escape(hf[:first_footer])}</c:firstFooter>" if hf[:first_footer]
1616
- parts << "</c:headerFooter>"
1617
- end
1618
- if (pm = ps[:page_margins])
1619
- pm_attrs = %w[b l r t header footer].filter_map { |a| pm[a.to_sym] ? %( #{a}="#{xml_escape(pm[a.to_sym])}") : nil }.join
1620
- parts << "<c:pageMargins#{pm_attrs}/>"
1621
- end
1622
- if (psu = ps[:page_setup])
1623
- psu_parts = +""
1624
- psu_parts << %( paperSize="#{xml_escape(psu[:paper_size])}") if psu[:paper_size]
1625
- psu_parts << %( firstPageNumber="#{xml_escape(psu[:first_page_number])}") if psu[:first_page_number]
1626
- psu_parts << %( orientation="#{xml_escape(psu[:orientation])}") if psu[:orientation]
1627
- psu_parts << %( horizontalDpi="#{xml_escape(psu[:horizontal_dpi])}") if psu[:horizontal_dpi]
1628
- psu_parts << %( verticalDpi="#{xml_escape(psu[:vertical_dpi])}") if psu[:vertical_dpi]
1629
- psu_parts << %( copies="#{xml_escape(psu[:copies])}") if psu[:copies]
1630
- parts << "<c:pageSetup#{psu_parts}/>"
1631
- end
1632
- parts << "</c:printSettings>"
1633
- end
1634
- parts << "</c:chartSpace>"
1635
- parts.join
1636
1698
  end
1637
1699
 
1638
- # : (untyped sheet_comments) -> untyped
1639
- def generate_comments_xml(sheet_comments)
1640
- authors = sheet_comments.map { |c| c[:author] }.uniq
1641
- parts = [
1642
- XML_HEADER,
1643
- %(<comments xmlns="#{SSML_NS}">),
1644
- "<authors>"
1645
- ]
1646
- authors.each { |a| parts << "<author>#{xml_escape(a)}</author>" }
1647
- parts << "</authors><commentList>"
1648
- sheet_comments.each do |c|
1649
- aid = authors.index(c[:author]) || 0
1650
- text_xml = if c[:text].is_a?(Xlsxrb::Elements::RichText)
1651
- rich_text_xml(c[:text])
1652
- else
1653
- "<r><t>#{xml_escape(c[:text])}</t></r>"
1654
- end
1655
- comment_attrs = %(ref="#{xml_escape(c[:ref])}" authorId="#{xml_escape(aid)}")
1656
- comment_attrs << %( guid="#{xml_escape(c[:guid])}") if c[:guid]
1657
- comment_attrs << %( shapeId="#{xml_escape(c[:shape_id])}") if c[:shape_id]
1658
- parts << "<comment #{comment_attrs}><text>#{text_xml}</text></comment>"
1700
+ # : (XmlBuilder xml, untyped data_table) -> void
1701
+ def write_chart_data_table_xml(xml, data_table)
1702
+ xml.tag("c:dTable") do
1703
+ xml.empty_tag("c:showHorzBorder", { val: data_table[:show_horz_border] ? 1 : 0 }) unless data_table[:show_horz_border].nil?
1704
+ xml.empty_tag("c:showVertBorder", { val: data_table[:show_vert_border] ? 1 : 0 }) unless data_table[:show_vert_border].nil?
1705
+ xml.empty_tag("c:showOutline", { val: data_table[:show_outline] ? 1 : 0 }) unless data_table[:show_outline].nil?
1706
+ xml.empty_tag("c:showKeys", { val: data_table[:show_keys] ? 1 : 0 }) unless data_table[:show_keys].nil?
1707
+ write_simple_sp_pr_from_hash(xml, data_table)
1708
+ write_axis_txpr(xml, nil, data_table[:font]) if data_table[:font]
1659
1709
  end
1660
- parts << "</commentList></comments>"
1661
- parts.join
1662
1710
  end
1663
1711
 
1664
- # : (untyped sheet_comments) -> untyped
1665
- def generate_vml_drawing_xml(sheet_comments)
1666
- parts = [
1667
- '<xml xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel">',
1668
- '<o:shapelayout v:ext="edit"><o:idmap v:ext="edit" data="1"/></o:shapelayout>',
1669
- '<v:shapetype id="_x0000_t202" coordsize="21600,21600" o:spt="202" path="m,l,21600r21600,l21600,xe">',
1670
- '<v:stroke joinstyle="miter"/><v:path gradientshapeok="t" o:connecttype="rect"/>',
1671
- "</v:shapetype>"
1672
- ]
1673
- sheet_comments.each_with_index do |c, idx|
1674
- col, row = cell_to_col_row(c[:ref])
1675
- shape_id = 1025 + idx
1676
- parts << %(<v:shape id="_x0000_s#{xml_escape(shape_id)}" type="#_x0000_t202" style="position:absolute;margin-left:59.25pt;margin-top:1.5pt;width:108pt;height:59.25pt;z-index:#{xml_escape(idx + 1)};visibility:#{c[:visible] ? "visible" : "hidden"}" fillcolor="#ffffe1" o:insetmode="auto">)
1677
- parts << '<v:fill color2="#ffffe1"/>'
1678
- parts << '<v:shadow on="t" color="black" obscured="t"/>'
1679
- parts << '<v:path o:connecttype="none"/>'
1680
- parts << '<v:textbox style="mso-direction-alt:auto"><div style="text-align:left"></div></v:textbox>'
1681
- parts << '<x:ClientData ObjectType="Note">'
1682
- parts << "<x:MoveWithCells/><x:SizeWithCells/>"
1683
- anchor = c[:anchor] || Xlsxrb::Elements::Comment.default_anchor(col, row)
1684
- parts << "<x:Anchor>#{anchor.join(", ")}</x:Anchor>"
1685
- parts << "<x:AutoFill>False</x:AutoFill>"
1686
- parts << "<x:Row>#{row}</x:Row>"
1687
- parts << "<x:Column>#{col}</x:Column>"
1688
- parts << "<x:Visible/>" if c[:visible]
1689
- parts << "</x:ClientData></v:shape>"
1712
+ # : (XmlBuilder xml, untyped legend) -> void
1713
+ def write_chart_legend_xml(xml, legend)
1714
+ legend_pos = legend&.[](:position) || "r"
1715
+ legend_overlay = legend&.[](:overlay)
1716
+ xml.tag("c:legend") do
1717
+ xml.empty_tag("c:legendPos", { val: legend_pos })
1718
+ legend&.[](:entries)&.each do |entry|
1719
+ xml.tag("c:legendEntry") do
1720
+ xml.empty_tag("c:idx", { val: entry[:idx] || "" })
1721
+ if !entry[:delete].nil?
1722
+ xml.empty_tag("c:delete", { val: entry[:delete] ? 1 : 0 })
1723
+ elsif entry[:font]
1724
+ write_axis_txpr(xml, nil, entry[:font])
1725
+ end
1726
+ end
1727
+ end
1728
+ write_plot_area_layout_xml(xml, legend&.[](:layout), emit_empty: false)
1729
+ xml.empty_tag("c:overlay", { val: legend_overlay ? 1 : 0 }) unless legend_overlay.nil?
1730
+ write_simple_sp_pr_from_hash(xml, legend) if legend
1731
+ write_axis_txpr(xml, nil, legend[:font]) if legend&.[](:font)
1690
1732
  end
1691
- parts << "</xml>"
1692
- parts.join
1693
1733
  end
1694
1734
 
1695
- # : (untyped cell_ref) -> (::Array[0] | ::Array[untyped])
1696
- def cell_to_col_row(cell_ref)
1697
- m = cell_ref.match(/\A([A-Z]+)(\d+)\z/)
1698
- return [0, 0] unless m
1699
-
1700
- col = column_letter_to_index(m[1]) - 1
1701
- row = m[2].to_i - 1
1702
- [col, row]
1735
+ # : (XmlBuilder xml, untyped print_settings) -> void
1736
+ def write_chart_print_settings_xml(xml, print_settings)
1737
+ xml.tag("c:printSettings") do
1738
+ if (hf = print_settings[:header_footer])
1739
+ xml.tag("c:headerFooter") do
1740
+ xml.text_tag("c:oddHeader", hf[:odd_header]) if hf[:odd_header]
1741
+ xml.text_tag("c:oddFooter", hf[:odd_footer]) if hf[:odd_footer]
1742
+ xml.text_tag("c:evenHeader", hf[:even_header]) if hf[:even_header]
1743
+ xml.text_tag("c:evenFooter", hf[:even_footer]) if hf[:even_footer]
1744
+ xml.text_tag("c:firstHeader", hf[:first_header]) if hf[:first_header]
1745
+ xml.text_tag("c:firstFooter", hf[:first_footer]) if hf[:first_footer]
1746
+ end
1747
+ end
1748
+ if (pm = print_settings[:page_margins])
1749
+ pm_attrs = %w[b l r t header footer].each_with_object({}) do |a, h|
1750
+ val = pm[a.to_sym]
1751
+ h[a] = val if val
1752
+ end
1753
+ xml.empty_tag("c:pageMargins", pm_attrs)
1754
+ end
1755
+ if (psu = print_settings[:page_setup])
1756
+ xml.empty_tag(
1757
+ "c:pageSetup",
1758
+ {
1759
+ paperSize: psu[:paper_size],
1760
+ firstPageNumber: psu[:first_page_number],
1761
+ orientation: psu[:orientation],
1762
+ horizontalDpi: psu[:horizontal_dpi],
1763
+ verticalDpi: psu[:vertical_dpi],
1764
+ copies: psu[:copies]
1765
+ }
1766
+ )
1767
+ end
1768
+ end
1703
1769
  end
1704
1770
 
1705
- THREADED_COMMENTS_NS = "http://schemas.microsoft.com/office/spreadsheetml/2018/threadedcomments"
1706
-
1707
- # Formats or generates a GUID in standard uppercase braced format {UUID}.
1708
- # : (?String? val) -> String
1709
- def format_guid(val = nil)
1710
- return "{#{SecureRandom.uuid.upcase}}" if val.nil? || val.to_s.empty?
1711
-
1712
- str = val.to_s.strip
1713
- str = "{#{str}}" unless str.start_with?("{") && str.end_with?("}")
1714
- str.upcase
1771
+ # : (XmlBuilder xml, untyped hash) -> void
1772
+ def write_simple_sp_pr_from_hash(xml, hash)
1773
+ return unless hash[:fill_color] || hash[:no_fill] || hash[:line_color] || hash[:line_width] || hash[:line_dash]
1774
+
1775
+ write_simple_sp_pr(
1776
+ xml,
1777
+ fill_color: hash[:fill_color],
1778
+ no_fill: hash[:no_fill],
1779
+ line_color: hash[:line_color],
1780
+ line_width: hash[:line_width],
1781
+ line_dash: hash[:line_dash]
1782
+ )
1715
1783
  end
1716
1784
 
1717
- # Generates XML for xl/threadedComments/threadedComment{N}.xml.
1718
- # : (Array[Hash[Symbol, untyped]] sheet_threaded_comments) -> String
1719
- def generate_threaded_comments_xml(sheet_threaded_comments)
1720
- parts = [
1721
- XML_HEADER,
1722
- %(<ThreadedComments xmlns="#{THREADED_COMMENTS_NS}">)
1723
- ]
1724
- sheet_threaded_comments.each do |c|
1725
- ref = c[:ref] || c[:cell]
1726
- dt = c[:timestamp] || c[:dt]
1727
- dt_str = case dt
1728
- when Time, DateTime
1729
- dt.to_time.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
1730
- when String
1731
- dt
1732
- else
1733
- Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
1734
- end
1735
- person_id = c[:person_id] || c[:author_id] || "{00000000-0000-0000-0000-000000000000}"
1736
- comment_id = c[:id] || format_guid(nil)
1785
+ # : (XmlBuilder xml, ?fill_color: untyped?, ?no_fill: untyped?, ?line_color: untyped?, ?line_width: untyped?, ?line_dash: untyped?, ?emu: bool) -> void
1786
+ def write_simple_sp_pr(xml, fill_color: nil, no_fill: false, line_color: nil, line_width: nil, line_dash: nil, emu: true)
1787
+ xml.tag("c:spPr") do
1788
+ xml.tag("a:solidFill") { write_drawing_color_xml(xml, fill_color) } if fill_color
1789
+ xml.empty_tag("a:noFill") if no_fill
1790
+ write_ln_xml(xml, line_color: line_color, line_width: line_width, line_dash: line_dash, emu: emu)
1791
+ end
1792
+ end
1737
1793
 
1738
- attrs = %(ref="#{xml_escape(ref)}" dT="#{xml_escape(dt_str)}" personId="#{xml_escape(person_id)}" id="#{xml_escape(comment_id)}")
1739
- attrs << %( parentId="#{xml_escape(c[:parent_id])}") if c[:parent_id]
1740
- attrs << %( done="true") if [true, "true", "1"].include?(c[:done])
1794
+ # : (XmlBuilder xml, ?line_color: untyped?, ?line_width: untyped?, ?line_dash: untyped?, ?emu: bool) -> void
1795
+ def write_ln_xml(xml, line_color: nil, line_width: nil, line_dash: nil, emu: true)
1796
+ return unless line_color || line_width || line_dash
1741
1797
 
1742
- text_val = c[:text].to_s
1743
- parts << "<threadedComment #{attrs}><text>#{xml_escape(text_val)}</text></threadedComment>"
1798
+ w = if line_width
1799
+ emu ? (line_width * 12_700).to_i : line_width.to_i
1800
+ end
1801
+ xml.tag("a:ln", { w: w }) do
1802
+ xml.tag("a:solidFill") { write_drawing_color_xml(xml, line_color) } if line_color
1803
+ xml.empty_tag("a:prstDash", { val: line_dash }) if line_dash
1744
1804
  end
1745
- parts << "</ThreadedComments>"
1746
- parts.join
1747
1805
  end
1748
1806
 
1749
- # Generates XML for xl/persons/person.xml.
1750
- # : (Array[Hash[Symbol, untyped]] persons) -> String
1751
- def generate_persons_xml(persons)
1752
- parts = [
1753
- XML_HEADER,
1754
- %(<personList xmlns="#{THREADED_COMMENTS_NS}">)
1755
- ]
1756
- persons.each do |p|
1757
- p_id = p[:id] || format_guid(nil)
1758
- name = xml_escape(p[:display_name] || p[:name] || "Author")
1759
- user_id = xml_escape(p[:user_id] || "AD:#{p[:display_name] || p[:name] || "Author"}")
1760
- provider_id = xml_escape(p[:provider_id] || "AD")
1761
- parts << %(<person displayName="#{xml_escape(name)}" id="#{xml_escape(p_id)}" userId="#{xml_escape(user_id)}" providerId="#{xml_escape(provider_id)}"/>)
1807
+ # : (XmlBuilder xml, ::String tag, untyped spec) -> void
1808
+ def write_line_sp_tag(xml, tag, spec)
1809
+ if spec.is_a?(Hash) && (spec[:line_color] || spec[:line_width] || spec[:line_dash])
1810
+ xml.tag(tag) do
1811
+ xml.tag("c:spPr") do
1812
+ write_ln_xml(xml, line_color: spec[:line_color], line_width: spec[:line_width], line_dash: spec[:line_dash])
1813
+ end
1814
+ end
1815
+ else
1816
+ xml.empty_tag(tag)
1762
1817
  end
1763
- parts << "</personList>"
1764
- parts.join
1765
1818
  end
1766
1819
  end
1767
1820
  end