OpenXLSX 1.9.1
Loading...
Searching...
No Matches
XLDrawing.hpp
Go to the documentation of this file.
1#ifndef OPENXLSX_XLDRAWING_HPP
2#define OPENXLSX_XLDRAWING_HPP
3
4// ===== External Includes ===== //
5#include <cstdint> // uint8_t, uint16_t, uint32_t
6#include <gsl/pointers>
7#include <ostream> // std::basic_ostream
8#include <string_view>
9
10// ===== OpenXLSX Includes ===== //
11#include "OpenXLSX-Exports.hpp"
12// #include "XLDocument.hpp"
13#include "XLConstants.hpp"
14#include "XLException.hpp"
15#include "XLImageOptions.hpp"
16#include "XLXmlData.hpp"
17#include "XLXmlFile.hpp"
18
19#include <memory>
20#include "XLRichText.hpp"
21
22namespace OpenXLSX
23{
24 class XLRelationships;
25
26 extern const std::string_view ShapeNodeName; // = "v:shape"
27 extern const std::string_view ShapeTypeNodeName; // = "v:shapetype"
28
29 // NOTE: numerical values of XLShapeTextVAlign and XLShapeTextHAlign are shared with the same alignments from XLAlignmentStyle
30 // (XLStyles.hpp)
31 enum class XLShapeTextVAlign : uint8_t {
32 Center = 3, // value="center", both
33 Top = 4, // value="top", vertical only
34 Bottom = 5, // value="bottom", vertical only
35 Invalid = 255 // all other values
36 };
38
39 enum class XLShapeTextHAlign : uint8_t {
40 Left = 1, // value="left", horizontal only
41 Right = 2, // value="right", horizontal only
42 Center = 3, // value="center", both
43 Invalid = 255 // all other values
44 };
46
50 class OPENXLSX_EXPORT XLShapeClientData
51 {
52 public:
54
59 explicit XLShapeClientData(const XMLNode& node);
60
65 XLShapeClientData(const XLShapeClientData& other) = default;
66
71 XLShapeClientData(XLShapeClientData&& other) noexcept = default;
72
73 ~XLShapeClientData() = default;
74
81
87 XLShapeClientData& operator=(XLShapeClientData&& other) noexcept = default;
88
92 std::string objectType() const; // attribute ObjectType, value "Note"
93 bool moveWithCells() const; // element x:MoveWithCells - true = present or lowercase node_pcdata "true", false = not present or
94 // lowercase node_pcdata "false"
95 bool sizeWithCells() const; // element x:SizeWithCells - logic as in MoveWithCells
96 std::string anchor() const; // element x:Anchor - e.g. "3, 23, 0, 0, 4, 25, 3, 5"
97 bool autoFill() const; // element x:AutoFill - logic as in MoveWithCells
98 XLShapeTextVAlign textVAlign() const; // element x:TextVAlign - Top, ???
99 XLShapeTextHAlign textHAlign() const; // element x:TextHAlign - Left, ???
100 uint32_t row() const; // element x:Row, 0-indexed row of cell to which this shape is linked
101 uint16_t column() const; // element x:Column, 0-indexed column of cell to which this shape is linked
102
106 bool setObjectType(std::string_view newObjectType);
107 bool setMoveWithCells(bool set = true);
108 bool setSizeWithCells(bool set = true);
109 bool setAnchor(std::string_view newAnchor);
110 bool setAutoFill(bool set = true);
111 bool setTextVAlign(XLShapeTextVAlign newTextVAlign);
112 bool setTextHAlign(XLShapeTextHAlign newTextHAlign);
113 bool setRow(uint32_t newRow);
114 bool setColumn(uint16_t newColumn);
115
116 private:
117 mutable XMLNode m_clientDataNode;
118 inline static const std::vector<std::string_view> m_nodeOrder =
119 {"x:MoveWithCells", "x:SizeWithCells", "x:Anchor", "x:AutoFill", "x:TextVAlign", "x:TextHAlign", "x:Row", "x:Column"};
120 };
121
123 {
124 std::string name;
125 std::string value;
126 };
127
128 class OPENXLSX_EXPORT XLShapeStyle
129 {
130 public:
131 XLShapeStyle();
132
137 explicit XLShapeStyle(const std::string& styleAttribute);
138
144 explicit XLShapeStyle(const XMLAttribute& styleAttribute);
145
146 private:
152 int16_t attributeOrderIndex(std::string_view attributeName) const;
156 XLShapeStyleAttribute getAttribute(std::string_view attributeName, std::string_view valIfNotFound = "") const;
157 bool setAttribute(std::string_view attributeName, std::string_view attributeValue);
158
159 public:
163 std::string position() const;
164 uint16_t marginLeft() const;
165 uint16_t marginTop() const;
166 uint16_t width() const;
167 uint16_t height() const;
168 std::string msoWrapStyle() const;
169 std::string vTextAnchor() const;
170 bool hidden() const;
171 bool visible() const;
172
173 std::string raw() const { return m_style; }
174
178 bool setPosition(std::string_view newPosition);
179 bool setMarginLeft(uint16_t newMarginLeft);
180 bool setMarginTop(uint16_t newMarginTop);
181 bool setWidth(uint16_t newWidth);
182 bool setHeight(uint16_t newHeight);
183 bool setMsoWrapStyle(std::string_view newMsoWrapStyle);
184 bool setVTextAnchor(std::string_view newVTextAnchor);
185 bool hide(); // set visibility:hidden
186 bool show(); // set visibility:visible
187
188 bool setRaw(std::string_view newStyle)
189 {
190 m_style = newStyle;
191 return true;
192 }
193
194 private:
195 mutable std::string m_style; // mutable so getter functions can update it from m_styleAttribute if the latter is not empty
196 mutable XMLAttribute m_styleAttribute;
197 inline static const std::vector<std::string_view> m_nodeOrder = {"position",
198 "margin-left",
199 "margin-top",
200 "width",
201 "height",
202 "mso-wrap-style",
203 "mso-fit-shape-to-text", // Added for auto-size support
204 "v-text-anchor",
205 "visibility"};
206 };
207
208 class OPENXLSX_EXPORT XLShape
209 {
210 friend class XLVmlDrawing; // for access to m_shapeNode in XLVmlDrawing::addShape
211 friend class XLComments; // added for access to m_shapeNode in XLComments::set
212 public:
213 XLShape();
214
219 explicit XLShape(const XMLNode& node);
220
225 XLShape(const XLShape& other) = default;
226
231 XLShape(XLShape&& other) noexcept = default;
232
233 ~XLShape() = default;
234
240 XLShape& operator=(const XLShape& other);
241
247 XLShape& operator=(XLShape&& other) noexcept = default;
248
252 std::string shapeId() const; // v:shape attribute id - shape_# - can't be set by the user
253 std::string fillColor() const; // v:shape attribute fillcolor, #<3 byte hex code>, e.g. #ffffc0
254 bool stroked() const; // v:shape attribute stroked "t" ("f"?)
255 std::string type() const; // v:shape attribute type, link to v:shapetype attribute id
256 bool allowInCell() const; // v:shape attribute o:allowincell "f"
257 XLShapeStyle style(); // v:shape attribute style, but constructed from the XMLAttribute
258
259 XLShapeClientData clientData(); // v:shape subnode x:ClientData
260
266 // NOTE: setShapeId is not available because shape id is managed by the parent class in createShape
267 bool setFillColor(std::string_view newFillColor);
268 bool setStroked(bool set);
269 bool setType(std::string_view newType);
270 bool setAllowInCell(bool set);
271 bool setStyle(std::string_view newStyle);
272 bool setStyle(XLShapeStyle const& newStyle);
273
274 private:
275 mutable XMLNode m_shapeNode;
276 inline static const std::vector<std::string_view> m_nodeOrder =
277 {"v:shadow", "v:fill", "v:stroke", "v:path", "v:textbox", "x:ClientData"};
278 };
279
284 enum class XLVectorShapeType {
285 Rectangle,
286 Ellipse,
287 Line,
288 Triangle,
290 Arrow,
291 Diamond,
293 Hexagon,
294 Star4,
295 Star5,
296 Star16,
297 Star24,
298 Heart,
300 Cloud,
301 Donut,
302 Ribbon,
303 Sun,
304 Moon,
310 };
311
313 {
315 std::string name = "Shape 1";
316 std::string text = "";
317 std::string fillColor = "4286F4"; // ARGB, no #
318 std::string lineColor = "000000";
319 double lineWidth = 1.0;
320
321 uint32_t width = 100;
322 uint32_t height = 100;
323 int32_t offsetX = 0;
324 int32_t offsetY = 0;
325
326 // If endRow and endCol are specified, the shape uses a twoCellAnchor (resizes with cells)
327 std::optional<uint32_t> endRow;
328 std::optional<uint32_t> endCol;
329 int32_t endOffsetX = 0;
330 int32_t endOffsetY = 0;
331
332 // Shape transformations
333 uint32_t rotation = 0; // rotation in degrees
334 bool flipH = false;
335 bool flipV = false;
336
337 // Advanced outline properties
338 std::string lineDash = ""; // e.g. "dash", "sysDash", "dot", "dashDot"
339 std::string arrowStart = ""; // e.g. "triangle", "stealth", "diamond"
340 std::string arrowEnd = "";
341
342 // Text properties
343 std::optional<XLRichText> richText = std::nullopt;
344 std::string horzAlign = ""; // e.g. "ctr", "l", "r"
345 std::string vertAlign = ""; // e.g. "ctr", "t", "b"
346
347 std::string macro = "";
348 };
349 class OPENXLSX_EXPORT XLDrawingItem
350 {
351 public:
353 explicit XLDrawingItem(const XMLNode& node, XLDocument* parentDoc = nullptr);
354 XLDrawingItem(const XLDrawingItem& other) = default;
355 XLDrawingItem(XLDrawingItem&& other) noexcept = default;
356 ~XLDrawingItem() = default;
357 XLDrawingItem& operator=(const XLDrawingItem& other) = default;
358 XLDrawingItem& operator=(XLDrawingItem&& other) noexcept = default;
359
360 std::string name() const;
361 std::string description() const;
362 uint32_t row() const;
363 uint32_t col() const;
364 uint32_t width() const; // in pixels (converted from EMUs)
365 uint32_t height() const; // in pixels (converted from EMUs)
366 std::string relationshipId() const;
367
372 std::vector<uint8_t> imageBinary() const;
373
374 private:
375 mutable XMLNode m_anchorNode;
376 XLDocument* m_parentDoc{nullptr};
377 };
378
382 class OPENXLSX_EXPORT XLDrawing : public XLXmlFile
383 {
384 friend class XLWorksheet;
385 friend class XLDocument;
386
387 public:
391 XLDrawing() : XLXmlFile(nullptr) {};
392
397 explicit XLDrawing(gsl::not_null<XLXmlData*> xmlData);
398
403 XLDrawing(const XLDrawing& other);
404
408 XLDrawing(XLDrawing&& other) noexcept;
409
413 ~XLDrawing() = default;
414
418 XLDrawing& operator=(const XLDrawing& other);
419
423 XLDrawing& operator=(XLDrawing&& other) noexcept;
424
436 void addImage(std::string_view rId,
437 std::string_view name,
438 std::string_view description,
439 uint32_t row,
440 uint32_t col,
441 uint32_t width,
442 uint32_t height,
443 const XLImageOptions& options = XLImageOptions());
444
455 void addShape(uint32_t row, uint32_t col, const XLVectorShapeOptions& options = XLVectorShapeOptions());
456 void addChartAnchor(std::string_view rId, std::string_view name, uint32_t row, uint32_t col, uint32_t width, uint32_t height);
457
458 void addChartAnchor(std::string_view rId, std::string_view name, uint32_t row, uint32_t col, XLDistance width, XLDistance height);
459
470 void addScaledImage(std::string_view rId,
471 std::string_view name,
472 std::string_view description,
473 std::string_view data,
474 uint32_t row,
475 uint32_t col,
476 double scalingFactor = 1.0);
477
482 XLRelationships& relationships();
483
488 uint32_t imageCount() const;
489
495 XLDrawingItem image(uint32_t index) const;
496
497 private:
498 // Helper to add namespaces if missing
499 void initXml();
500
501 std::unique_ptr<XLRelationships> m_relationships;
502 };
503
507 class OPENXLSX_EXPORT XLVmlDrawing : public XLXmlFile
508 {
509 friend class XLWorksheet; // for access to XLXmlFile::getXmlPath
510 friend class XLComments; // for access to firstShapeNode
511 public:
515 XLVmlDrawing() : XLXmlFile(nullptr) {};
516
521 explicit XLVmlDrawing(gsl::not_null<XLXmlData*> xmlData);
522
528 XLVmlDrawing(const XLVmlDrawing& other) = default;
529
530 XLVmlDrawing(XLVmlDrawing&& other) noexcept = default;
531
536 ~XLVmlDrawing() = default;
537
544
547 XLVmlDrawing& operator=(XLVmlDrawing&& other) noexcept = default;
548
549 private: // helper functions with repeating code
550 XMLNode firstShapeNode() const;
551 XMLNode lastShapeNode() const;
552 XMLNode shapeNode(uint32_t index) const;
553
554 public:
560 XMLNode shapeNode(std::string_view cellRef) const;
561
562 uint32_t shapeCount() const;
563
564 XLShape shape(uint32_t index) const;
565
566 bool deleteShape(uint32_t index);
567 bool deleteShape(std::string_view cellRef);
568
569 XLShape createShape(const XLShape& shapeTemplate = XLShape());
570
574 void print(std::basic_ostream<char>& ostr) const;
575
576 private:
577 uint32_t m_shapeCount{0};
578 uint32_t m_lastAssignedShapeId{0};
579 std::string m_defaultShapeTypeId{};
580 };
581} // namespace OpenXLSX
582
583#endif // OPENXLSX_XLDRAWING_HPP
XLXmlData * xmlData
Definition XLDocument.cpp:1422
return XLRelationships(xmlData, relsFilename)
Definition XLXmlParser.hpp:84
The XLComments class is the base class for worksheet comments.
Definition XLComments.hpp:88
Definition XLConstants.hpp:32
This class encapsulates the concept of an excel file. It is different from the XLWorkbook,...
Definition XLDocument.hpp:82
Definition XLDrawing.hpp:350
XLDrawingItem & operator=(const XLDrawingItem &other)=default
XLDrawingItem(const XLDrawingItem &other)=default
XLDrawingItem & operator=(XLDrawingItem &&other) noexcept=default
XLDrawingItem(XLDrawingItem &&other) noexcept=default
The XLDrawing class is the base class for worksheet drawings (images, charts, etc....
Definition XLDrawing.hpp:383
~XLDrawing()=default
The destructor.
XLDrawing()
Constructor.
Definition XLDrawing.hpp:391
XLDrawing(XLDrawing &&other) noexcept
Move constructor.
XLDrawing & operator=(XLDrawing &&other) noexcept
Move assignment operator.
Definition XLRelationships.hpp:140
An encapsulation of a shape client data element x:ClientData.
Definition XLDrawing.hpp:51
XLShapeClientData(const XLShapeClientData &other)=default
Copy Constructor.
XLShapeClientData(XLShapeClientData &&other) noexcept=default
Move Constructor.
XLShapeClientData & operator=(XLShapeClientData &&other) noexcept=default
Move assignment operator.
XLShapeClientData & operator=(const XLShapeClientData &other)=default
Copy assignment operator.
Definition XLDrawing.hpp:129
std::string raw() const
Definition XLDrawing.hpp:173
bool setRaw(std::string_view newStyle)
Definition XLDrawing.hpp:188
Definition XLDrawing.hpp:209
XLShape(const XLShape &other)=default
Copy Constructor.
XLShape & operator=(XLShape &&other) noexcept=default
Move assignment operator.
XLShape & operator=(const XLShape &other)
Copy assignment operator.
~XLShape()=default
XLShape(XLShape &&other) noexcept=default
Move Constructor.
The XLVmlDrawing class is the base class for worksheet comments.
Definition XLDrawing.hpp:508
XLVmlDrawing(const XLVmlDrawing &other)=default
The copy constructor.
XLVmlDrawing()
Constructor.
Definition XLDrawing.hpp:515
XLVmlDrawing & operator=(XLVmlDrawing &&other) noexcept=default
~XLVmlDrawing()=default
The destructor.
XLVmlDrawing & operator=(const XLVmlDrawing &)=default
Assignment operator.
XLVmlDrawing(XLVmlDrawing &&other) noexcept=default
A class encapsulating an Excel worksheet. Access to XLWorksheet objects should be via the workbook ob...
Definition XLWorksheet.hpp:118
The XLXmlFile class provides an interface for derived classes to use. It functions as an ancestor to ...
Definition XLXmlFile.hpp:42
Definition IZipArchive.hpp:18
XLVectorShapeType
An encapsulation of a drawing item (e.g. an image)
Definition XLDrawing.hpp:284
const std::string_view ShapeNodeName
Definition XLDrawing.cpp:19
pugi::xml_attribute XMLAttribute
Definition XLXmlParser.hpp:64
constexpr const XLShapeTextHAlign XLDefaultShapeTextHAlign
Definition XLDrawing.hpp:45
XLShapeTextVAlign
Definition XLDrawing.hpp:31
const std::string_view ShapeTypeNodeName
Definition XLDrawing.cpp:20
constexpr const XLShapeTextVAlign XLDefaultShapeTextVAlign
Definition XLDrawing.hpp:37
XLShapeTextHAlign
Definition XLDrawing.hpp:39
Options for inserting an image into a worksheet.
Definition XLImageOptions.hpp:23
Definition XLDrawing.hpp:123
std::string value
Definition XLDrawing.hpp:125
std::string name
Definition XLDrawing.hpp:124
Definition XLDrawing.hpp:313
std::optional< uint32_t > endCol
Definition XLDrawing.hpp:328
bool flipV
Definition XLDrawing.hpp:335
int32_t endOffsetY
Definition XLDrawing.hpp:330
XLVectorShapeType type
Definition XLDrawing.hpp:314
std::string arrowStart
Definition XLDrawing.hpp:339
double lineWidth
Definition XLDrawing.hpp:319
bool flipH
Definition XLDrawing.hpp:334
int32_t offsetX
Definition XLDrawing.hpp:323
uint32_t width
Definition XLDrawing.hpp:321
std::string vertAlign
Definition XLDrawing.hpp:345
std::string lineDash
Definition XLDrawing.hpp:338
std::string macro
Definition XLDrawing.hpp:347
std::string fillColor
Definition XLDrawing.hpp:317
int32_t endOffsetX
Definition XLDrawing.hpp:329
std::string lineColor
Definition XLDrawing.hpp:318
std::string name
Definition XLDrawing.hpp:315
uint32_t height
Definition XLDrawing.hpp:322
uint32_t rotation
Definition XLDrawing.hpp:333
std::string arrowEnd
Definition XLDrawing.hpp:340
std::optional< XLRichText > richText
Definition XLDrawing.hpp:343
int32_t offsetY
Definition XLDrawing.hpp:324
std::string horzAlign
Definition XLDrawing.hpp:344
std::optional< uint32_t > endRow
Definition XLDrawing.hpp:327
std::string text
Definition XLDrawing.hpp:316