Repository navigation
Expand file tree
/
Copy pathshape.go
More file actions
859 lines (832 loc) · 27.7 KB
/
Copy pathshape.go
File metadata and controls
859 lines (832 loc) · 27.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
// Copyright 2016 - 2026 The excelize Authors. All rights reserved. Use of
// this source code is governed by a BSD-style license that can be found in
// the LICENSE file.
//
// Package excelize providing a set of functions that allow you to write to and
// read from XLAM / XLSM / XLSX / XLTM / XLTX files. Supports reading and
// writing spreadsheet documents generated by Microsoft Excel™ 2007 and later.
// Supports complex components by high compatibility, and provided streaming
// API for generating or reading data from a worksheet with huge amounts of
// data. This library needs Go version 1.26.0 or later.
package excelize
import (
"strconv"
"strings"
)
// parseShapeOptions provides a function to parse the format settings of the
// shape with default value.
func parseShapeOptions(opts *Shape) (*Shape, error) {
if opts == nil {
return nil, ErrParameterInvalid
}
if opts.Type == "" {
return nil, ErrParameterInvalid
}
if opts.Width == 0 {
opts.Width = defaultShapeSize
}
if opts.Height == 0 {
opts.Height = defaultShapeSize
}
if opts.Line.Width == 0 {
opts.Line.Width = defaultLineWidth
}
if opts.Fill.Transparency < 0 || 100 < opts.Fill.Transparency {
return opts, ErrTransparency
}
format := opts.Format
graphicOptions, err := format.parseGraphicOptions(nil)
if err != nil {
return opts, err
}
opts.Format = *graphicOptions
return opts, err
}
// AddShape provides the method to add shape in a sheet by given worksheet
// name and shape format set (such as offset, scale, aspect ratio setting and
// print settings). For example, add text box (rect shape) in Sheet1:
//
// lineWidth := 1.2
// err := f.AddShape("Sheet1",
// &excelize.Shape{
// Cell: "G6",
// Type: "rect",
// Line: excelize.LineOptions{
// Fill: excelize.Fill{
// Type: "pattern", Color: []string{"4286F4"}, Pattern: 1,
// },
// Width: lineWidth,
// },
// Fill: excelize.Fill{Color: []string{"8EB9FF"}, Pattern: 1},
// Paragraph: []excelize.RichTextRun{
// {
// Text: "Rectangle Shape",
// Font: &excelize.Font{
// Bold: true,
// Italic: true,
// Family: "Times New Roman",
// Size: 18,
// Color: "777777",
// Underline: "sng",
// },
// },
// },
// Width: 180,
// Height: 40,
// },
// )
//
// The following shows the type of shape supported by excelize:
//
// accentBorderCallout1 (Callout 1 with Border and Accent Shape)
// accentBorderCallout2 (Callout 2 with Border and Accent Shape)
// accentBorderCallout3 (Callout 3 with Border and Accent Shape)
// accentCallout1 (Callout 1 Shape)
// accentCallout2 (Callout 2 Shape)
// accentCallout3 (Callout 3 Shape)
// actionButtonBackPrevious (Back or Previous Button Shape)
// actionButtonBeginning (Beginning Button Shape)
// actionButtonBlank (Blank Button Shape)
// actionButtonDocument (Document Button Shape)
// actionButtonEnd (End Button Shape)
// actionButtonForwardNext (Forward or Next Button Shape)
// actionButtonHelp (Help Button Shape)
// actionButtonHome (Home Button Shape)
// actionButtonInformation (Information Button Shape)
// actionButtonMovie (Movie Button Shape)
// actionButtonReturn (Return Button Shape)
// actionButtonSound (Sound Button Shape)
// arc (Curved Arc Shape)
// bentArrow (Bent Arrow Shape)
// bentConnector2 (Bent Connector 2 Shape)
// bentConnector3 (Bent Connector 3 Shape)
// bentConnector4 (Bent Connector 4 Shape)
// bentConnector5 (Bent Connector 5 Shape)
// bentUpArrow (Bent Up Arrow Shape)
// bevel (Bevel Shape)
// blockArc (Block Arc Shape)
// borderCallout1 (Callout 1 with Border Shape)
// borderCallout2 (Callout 2 with Border Shape)
// borderCallout3 (Callout 3 with Border Shape)
// bracePair (Brace Pair Shape)
// bracketPair (Bracket Pair Shape)
// callout1 (Callout 1 Shape)
// callout2 (Callout 2 Shape)
// callout3 (Callout 3 Shape)
// can (Can Shape)
// chartPlus (Chart Plus Shape)
// chartStar (Chart Star Shape)
// chartX (Chart X Shape)
// chevron (Chevron Shape)
// chord (Chord Shape)
// circularArrow (Circular Arrow Shape)
// cloud (Cloud Shape)
// cloudCallout (Callout Cloud Shape)
// corner (Corner Shape)
// cornerTabs (Corner Tabs Shape)
// cube (Cube Shape)
// curvedConnector2 (Curved Connector 2 Shape)
// curvedConnector3 (Curved Connector 3 Shape)
// curvedConnector4 (Curved Connector 4 Shape)
// curvedConnector5 (Curved Connector 5 Shape)
// curvedDownArrow (Curved Down Arrow Shape)
// curvedLeftArrow (Curved Left Arrow Shape)
// curvedRightArrow (Curved Right Arrow Shape)
// curvedUpArrow (Curved Up Arrow Shape)
// decagon (Decagon Shape)
// diagStripe (Diagonal Stripe Shape)
// diamond (Diamond Shape)
// dodecagon (Dodecagon Shape)
// donut (Donut Shape)
// doubleWave (Double Wave Shape)
// downArrow (Down Arrow Shape)
// downArrowCallout (Callout Down Arrow Shape)
// ellipse (Ellipse Shape)
// ellipseRibbon (Ellipse Ribbon Shape)
// ellipseRibbon2 (Ellipse Ribbon 2 Shape)
// flowChartAlternateProcess (Alternate Process Flow Shape)
// flowChartCollate (Collate Flow Shape)
// flowChartConnector (Connector Flow Shape)
// flowChartDecision (Decision Flow Shape)
// flowChartDelay (Delay Flow Shape)
// flowChartDisplay (Display Flow Shape)
// flowChartDocument (Document Flow Shape)
// flowChartExtract (Extract Flow Shape)
// flowChartInputOutput (Input Output Flow Shape)
// flowChartInternalStorage (Internal Storage Flow Shape)
// flowChartMagneticDisk (Magnetic Disk Flow Shape)
// flowChartMagneticDrum (Magnetic Drum Flow Shape)
// flowChartMagneticTape (Magnetic Tape Flow Shape)
// flowChartManualInput (Manual Input Flow Shape)
// flowChartManualOperation (Manual Operation Flow Shape)
// flowChartMerge (Merge Flow Shape)
// flowChartMultidocument (Multi-Document Flow Shape)
// flowChartOfflineStorage (Offline Storage Flow Shape)
// flowChartOffpageConnector (Off-Page Connector Flow Shape)
// flowChartOnlineStorage (Online Storage Flow Shape)
// flowChartOr (Or Flow Shape)
// flowChartPredefinedProcess (Predefined Process Flow Shape)
// flowChartPreparation (Preparation Flow Shape)
// flowChartProcess (Process Flow Shape)
// flowChartPunchedCard (Punched Card Flow Shape)
// flowChartPunchedTape (Punched Tape Flow Shape)
// flowChartSort (Sort Flow Shape)
// flowChartSummingJunction (Summing Junction Flow Shape)
// flowChartTerminator (Terminator Flow Shape)
// foldedCorner (Folded Corner Shape)
// frame (Frame Shape)
// funnel (Funnel Shape)
// gear6 (Gear 6 Shape)
// gear9 (Gear 9 Shape)
// halfFrame (Half Frame Shape)
// heart (Heart Shape)
// heptagon (Heptagon Shape)
// hexagon (Hexagon Shape)
// homePlate (Home Plate Shape)
// horizontalScroll (Horizontal Scroll Shape)
// irregularSeal1 (Irregular Seal 1 Shape)
// irregularSeal2 (Irregular Seal 2 Shape)
// leftArrow (Left Arrow Shape)
// leftArrowCallout (Callout Left Arrow Shape)
// leftBrace (Left Brace Shape)
// leftBracket (Left Bracket Shape)
// leftCircularArrow (Left Circular Arrow Shape)
// leftRightArrow (Left Right Arrow Shape)
// leftRightArrowCallout (Callout Left Right Arrow Shape)
// leftRightCircularArrow (Left Right Circular Arrow Shape)
// leftRightRibbon (Left Right Ribbon Shape)
// leftRightUpArrow (Left Right Up Arrow Shape)
// leftUpArrow (Left Up Arrow Shape)
// lightningBolt (Lightning Bolt Shape)
// line (Line Shape)
// lineInv (Line Inverse Shape)
// mathDivide (Divide Math Shape)
// mathEqual (Equal Math Shape)
// mathMinus (Minus Math Shape)
// mathMultiply (Multiply Math Shape)
// mathNotEqual (Not Equal Math Shape)
// mathPlus (Plus Math Shape)
// moon (Moon Shape)
// nonIsoscelesTrapezoid (Non-Isosceles Trapezoid Shape)
// noSmoking (No Smoking Shape)
// notchedRightArrow (Notched Right Arrow Shape)
// octagon (Octagon Shape)
// parallelogram (Parallelogram Shape)
// pentagon (Pentagon Shape)
// pie (Pie Shape)
// pieWedge (Pie Wedge Shape)
// plaque (Plaque Shape)
// plaqueTabs (Plaque Tabs Shape)
// plus (Plus Shape)
// quadArrow (Quad-Arrow Shape)
// quadArrowCallout (Callout Quad-Arrow Shape)
// rect (Rectangle Shape)
// ribbon (Ribbon Shape)
// ribbon2 (Ribbon 2 Shape)
// rightArrow (Right Arrow Shape)
// rightArrowCallout (Callout Right Arrow Shape)
// rightBrace (Right Brace Shape)
// rightBracket (Right Bracket Shape)
// round1Rect (One Round Corner Rectangle Shape)
// round2DiagRect (Two Diagonal Round Corner Rectangle Shape)
// round2SameRect (Two Same-side Round Corner Rectangle Shape)
// roundRect (Round Corner Rectangle Shape)
// rtTriangle (Right Triangle Shape)
// smileyFace (Smiley Face Shape)
// snip1Rect (One Snip Corner Rectangle Shape)
// snip2DiagRect (Two Diagonal Snip Corner Rectangle Shape)
// snip2SameRect (Two Same-side Snip Corner Rectangle Shape)
// snipRoundRect (One Snip One Round Corner Rectangle Shape)
// squareTabs (Square Tabs Shape)
// star10 (Ten Pointed Star Shape)
// star12 (Twelve Pointed Star Shape)
// star16 (Sixteen Pointed Star Shape)
// star24 (Twenty Four Pointed Star Shape)
// star32 (Thirty Two Pointed Star Shape)
// star4 (Four Pointed Star Shape)
// star5 (Five Pointed Star Shape)
// star6 (Six Pointed Star Shape)
// star7 (Seven Pointed Star Shape)
// star8 (Eight Pointed Star Shape)
// straightConnector1 (Straight Connector 1 Shape)
// stripedRightArrow (Striped Right Arrow Shape)
// sun (Sun Shape)
// swooshArrow (Swoosh Arrow Shape)
// teardrop (Teardrop Shape)
// trapezoid (Trapezoid Shape)
// triangle (Triangle Shape)
// upArrow (Up Arrow Shape)
// upArrowCallout (Callout Up Arrow Shape)
// upDownArrow (Up Down Arrow Shape)
// upDownArrowCallout (Callout Up Down Arrow Shape)
// uturnArrow (U-Turn Arrow Shape)
// verticalScroll (Vertical Scroll Shape)
// wave (Wave Shape)
// wedgeEllipseCallout (Callout Wedge Ellipse Shape)
// wedgeRectCallout (Callout Wedge Rectangle Shape)
// wedgeRoundRectCallout (Callout Wedge Round Rectangle Shape)
//
// The following shows the type of text underline supported by excelize:
//
// none
// words
// sng
// dbl
// heavy
// dotted
// dottedHeavy
// dash
// dashHeavy
// dashLong
// dashLongHeavy
// dotDash
// dotDashHeavy
// dotDotDash
// dotDotDashHeavy
// wavy
// wavyHeavy
// wavyDbl
func (f *File) AddShape(sheet string, opts *Shape) error {
options, err := parseShapeOptions(opts)
if err != nil {
return err
}
// Read sheet data
ws, err := f.workSheetReader(sheet)
if err != nil {
return err
}
// Add first shape for given sheet, create xl/drawings/ and xl/drawings/_rels/ folder.
drawingID := f.countDrawings() + 1
drawingXML := "xl/drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
sheetRelationshipsDrawingXML := "../drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
if ws.Drawing != nil {
// The worksheet already has a shape or chart relationships, use the relationships drawing ../drawings/drawing%d.xml.
sheetRelationshipsDrawingXML = f.getSheetRelationshipsTargetByID(sheet, ws.Drawing.RID)
drawingID, _ = strconv.Atoi(strings.TrimSuffix(strings.TrimPrefix(sheetRelationshipsDrawingXML, "../drawings/drawing"), ".xml"))
drawingXML = strings.ReplaceAll(sheetRelationshipsDrawingXML, "..", "xl")
} else {
// Add first shape for given sheet.
sheetXMLPath, _ := f.getSheetXMLPath(sheet)
sheetRels := "xl/worksheets/_rels/" + strings.TrimPrefix(sheetXMLPath, "xl/worksheets/") + ".rels"
rID := f.addRels(sheetRels, SourceRelationshipDrawingML, sheetRelationshipsDrawingXML, "")
f.addSheetDrawing(sheet, rID)
f.addSheetNameSpace(sheet, SourceRelationship)
}
if err = f.addDrawingShape(sheet, drawingXML, opts.Cell, options); err != nil {
return err
}
return f.addContentTypePart(drawingID, "drawings")
}
// cellAnchorShape create a two cell anchor shape size placeholder for a
// group, a shape, or a drawing element.
func (f *File) cellAnchorShape(sheet, drawingXML, cell string, width, height uint, format GraphicOptions) (*xlsxWsDr, *xdrCellAnchor, int, error) {
fromCol, fromRow, err := CellNameToCoordinates(cell)
if err != nil {
return nil, nil, 0, err
}
w := int(float64(width) * format.ScaleX)
h := int(float64(height) * format.ScaleY)
colStart, rowStart, colEnd, rowEnd, x1, y1, x2, y2 := f.positionObjectPixels(sheet, fromCol, fromRow, w, h, &format)
content, cNvPrID, err := f.drawingParser(drawingXML)
if err != nil {
return content, nil, cNvPrID, err
}
cellAnchor := xdrCellAnchor{}
from := xlsxFrom{}
from.Col = colStart
from.ColOff = x1 * EMU
from.Row = rowStart
from.RowOff = y1 * EMU
cellAnchor.From = &from
if format.Positioning != "oneCell" {
to := xlsxTo{}
to.Col = colEnd
to.ColOff = x2 * EMU
to.Row = rowEnd
to.RowOff = y2 * EMU
cellAnchor.To = &to
cellAnchor.EditAs = format.Positioning
}
if format.Positioning == "oneCell" {
cellAnchor.Ext = &xlsxPositiveSize2D{
Cx: x2 * EMU,
Cy: y2 * EMU,
}
}
return content, &cellAnchor, cNvPrID, err
}
// addDrawingShape provides a function to add preset geometry by given sheet,
// drawingXML and format sets.
func (f *File) addDrawingShape(sheet, drawingXML, cell string, opts *Shape) error {
content, cellAnchor, cNvPrID, err := f.cellAnchorShape(
sheet, drawingXML, cell, opts.Width, opts.Height, opts.Format)
if err != nil {
return err
}
shapeColor := singleHexColor(opts.Fill.Color)
lineColor := singleHexColor(opts.Line.Fill.Color)
shape := newDrawingShape(opts, cNvPrID, shapeColor, lineColor)
applyShapeLineAndFill(&shape, opts, shapeColor)
if shape.TxBody.P, err = f.buildShapeParagraphs(opts); err != nil {
return err
}
cellAnchor.Sp = &shape
cellAnchor.ClientData = &xdrClientData{
FLocksWithSheet: *opts.Format.Locked,
FPrintsWithSheet: *opts.Format.PrintObject,
}
appendDrawingAnchor(content, cellAnchor, opts.Format.Positioning)
f.Drawings.Store(drawingXML, content)
return nil
}
// trimHexColor removes the "#" prefix from the color and converts it to upper
// case.
func trimHexColor(color string) string {
return strings.ReplaceAll(strings.ToUpper(color), "#", "")
}
// singleHexColor returns the normalized color when exactly one color is
// specified, otherwise returns an empty string.
func singleHexColor(colors []string) string {
if len(colors) != 1 {
return ""
}
return trimHexColor(colors[0])
}
// newDrawingShape creates the base shape element with geometry, style and text
// body properties.
func newDrawingShape(opts *Shape, cNvPrID int, shapeColor, lineColor string) xdrSp {
name := "Shape " + strconv.Itoa(cNvPrID)
if len(opts.Format.Name) > 0 {
name = opts.Format.Name
}
return xdrSp{
Macro: opts.Macro,
NvSpPr: &xdrNvSpPr{
CNvPr: &xlsxCNvPr{
ID: cNvPrID,
Name: name,
Descr: opts.Format.AltText,
},
CNvSpPr: &xdrCNvSpPr{
TxBox: true,
},
},
SpPr: &xlsxSpPr{
Xfrm: xlsxXfrm{
Ext: xlsxPositiveSize2D{
Cx: int(opts.Width) * EMU,
Cy: int(opts.Height) * EMU,
},
},
PrstGeom: xlsxPrstGeom{
Prst: opts.Type,
},
},
Style: &xdrStyle{
LnRef: setShapeRef(lineColor, 2),
FillRef: setShapeRef(shapeColor, 1),
EffectRef: setShapeRef("", 0),
FontRef: &aFontRef{
Idx: "minor",
SchemeClr: &attrValString{
Val: stringPtr("tx1"),
},
},
},
TxBody: &xdrTxBody{
BodyPr: &aBodyPr{
VertOverflow: "clip",
HorzOverflow: "clip",
Wrap: "none",
RtlCol: false,
Anchor: "t",
},
},
}
}
// applyShapeLineAndFill sets the outline and the solid fill transparency of the
// shape by given shape options.
func applyShapeLineAndFill(shape *xdrSp, opts *Shape, shapeColor string) {
shape.SpPr.Ln = opts.Line.drawChartLn()
if shape.SpPr.Ln == nil && opts.Line.Width != defaultLineWidth {
shape.SpPr.Ln = &aLn{W: ptToEMUs(opts.Line.Width)}
}
if opts.Fill.Transparency > 0 {
val := (100 - opts.Fill.Transparency) * 1000
shape.SpPr.SolidFill = &aSolidFill{SrgbClr: &aSrgbClr{
Val: stringPtr(shapeColor),
Alpha: &attrValInt{Val: &val},
}}
}
}
// buildShapeParagraphs creates the text paragraphs of the shape, a default
// blank paragraph is used when no paragraph is specified.
func (f *File) buildShapeParagraphs(opts *Shape) ([]*aP, error) {
defaultFont, err := f.GetDefaultFont()
if err != nil {
return nil, err
}
if len(opts.Paragraph) < 1 {
opts.Paragraph = []RichTextRun{
{
Font: &Font{
Bold: false,
Italic: false,
Underline: "none",
Family: defaultFont,
Size: 11,
Color: "000000",
},
Text: " ",
},
}
}
paragraphs := make([]*aP, 0, len(opts.Paragraph))
for _, p := range opts.Paragraph {
paragraphs = append(paragraphs, newShapeParagraph(p))
}
return paragraphs, nil
}
// shapeUnderline returns the supported underline type, "none" by default.
func shapeUnderline(underline string) string {
if idx := inStrSlice(supportedDrawingUnderlineTypes, underline, true); idx != -1 {
return supportedDrawingUnderlineTypes[idx]
}
return "none"
}
// newShapeParagraph creates a text paragraph by given rich text run.
func newShapeParagraph(p RichTextRun) *aP {
font := &Font{}
if p.Font != nil {
font = p.Font
}
text := p.Text
if text == "" {
text = " "
}
paragraph := &aP{
R: &aR{
RPr: aRPr{
I: font.Italic,
B: font.Bold,
Lang: "en-US",
AltLang: "en-US",
U: shapeUnderline(font.Underline),
Sz: font.Size * 100,
Latin: &xlsxCTTextFont{Typeface: font.Family},
Ea: &xlsxCTTextFont{Typeface: font.Family},
Cs: &xlsxCTTextFont{Typeface: font.Family},
},
T: text,
},
EndParaRPr: &aEndParaRPr{
Lang: "en-US",
},
}
if srgbClr := trimHexColor(font.Color); len(srgbClr) == 6 {
paragraph.R.RPr.SolidFill = &aSolidFill{
SrgbClr: &aSrgbClr{
Val: stringPtr(srgbClr),
},
}
}
return paragraph
}
// appendDrawingAnchor appends the cell anchor to the drawing content by given
// positioning type.
func appendDrawingAnchor(content *xlsxWsDr, cellAnchor *xdrCellAnchor, positioning string) {
if positioning == "oneCell" {
content.OneCellAnchor = append(content.OneCellAnchor, cellAnchor)
return
}
content.TwoCellAnchor = append(content.TwoCellAnchor, cellAnchor)
}
// setShapeRef provides a function to set color with hex model by given actual
// color value.
func setShapeRef(color string, i int) *aRef {
if color == "" {
return &aRef{
Idx: 0,
ScrgbClr: &aScrgbClr{
R: 0,
G: 0,
B: 0,
},
}
}
return &aRef{
Idx: i,
SrgbClr: &attrValString{
Val: stringPtr(trimHexColor(color)),
},
}
}
// GetShapes provides the method to get all shapes in a sheet by given worksheet
// name.
func (f *File) GetShapes(sheet string) ([]Shape, error) {
var (
cells []string
shapes []Shape
wsDr *xlsxWsDr
)
ws, err := f.workSheetReader(sheet)
if err != nil {
return shapes, err
}
if ws.Drawing == nil {
return shapes, nil
}
target := f.getSheetRelationshipsTargetByID(sheet, ws.Drawing.RID)
drawingXML := strings.TrimPrefix(strings.ReplaceAll(target, "..", "xl"), "/")
if wsDr, _, err = f.drawingParser(drawingXML); err != nil {
return shapes, err
}
for _, anchor := range wsDr.OneCellAnchor {
f.extractCellAnchorShape(sheet, anchor, &cells, &shapes)
}
for _, anchor := range wsDr.TwoCellAnchor {
f.extractCellAnchorShape(sheet, anchor, &cells, &shapes)
}
return shapes, err
}
// extractCellAnchorShape extracts shape from a cell anchor and appends it to
// shapes if valid and not duplicate.
func (f *File) extractCellAnchorShape(sheet string, anchor *xdrCellAnchor, cells *[]string, shapes *[]Shape) {
if anchor.GraphicFrame == "" {
if shape := f.extractShapeFromAnchor(sheet, anchor); shape != nil && inStrSlice(*cells, shape.Cell, true) == -1 {
*cells = append(*cells, shape.Cell)
*shapes = append(*shapes, *shape)
return
}
}
if shape := f.extractShapeFromDecodeAnchor(sheet, anchor.GraphicFrame); shape != nil && inStrSlice(*cells, shape.Cell, true) == -1 {
*cells = append(*cells, shape.Cell)
*shapes = append(*shapes, *shape)
}
}
// extractShapeFromAnchor extracts shape data from a cell anchor.
func (f *File) extractShapeFromAnchor(sheet string, anchor *xdrCellAnchor) *Shape {
var (
cell string
err error
shape *Shape
)
if anchor.From == nil || anchor.Sp == nil {
return shape
}
if cell, err = CoordinatesToCellName(anchor.From.Col+1, anchor.From.Row+1); err != nil {
return shape
}
shape = &Shape{
Cell: cell,
Type: anchor.Sp.SpPr.PrstGeom.Prst,
Macro: anchor.Sp.Macro,
Format: GraphicOptions{
Name: anchor.Sp.NvSpPr.CNvPr.Name,
AltText: anchor.Sp.NvSpPr.CNvPr.Descr,
ScaleX: defaultDrawingScale,
ScaleY: defaultDrawingScale,
},
Line: LineOptions{Width: defaultLineWidth},
}
if anchor.To != nil {
width, height := f.getCellAnchorShapeSize(sheet, anchor)
shape.Width, shape.Height, shape.Format.ScaleX, shape.Format.ScaleY = f.getShapeSizeAndScale(width, height, anchor.Sp.SpPr.Xfrm.Ext.Cx, anchor.Sp.SpPr.Xfrm.Ext.Cy)
} else {
shape.Format.Positioning = "oneCell"
if anchor.Ext != nil {
width, height := anchor.Ext.Cx/EMU, anchor.Ext.Cy/EMU
shape.Width, shape.Height, shape.Format.ScaleX, shape.Format.ScaleY = f.getShapeSizeAndScale(width, height, anchor.Sp.SpPr.Xfrm.Ext.Cx, anchor.Sp.SpPr.Xfrm.Ext.Cy)
}
}
if anchor.ClientData != nil {
shape.Format.PrintObject = boolPtr(anchor.ClientData.FPrintsWithSheet)
shape.Format.Locked = boolPtr(anchor.ClientData.FLocksWithSheet)
}
anchor.Sp.extractShapeFill(shape)
shape.Paragraph = anchor.Sp.TxBody.extractRichTextRun()
return shape
}
// extractShapeFromDecodeAnchor extracts shape data from a decoded cell anchor.
func (f *File) extractShapeFromDecodeAnchor(sheet, graphicFrame string) *Shape {
var (
cell string
err error
shape *Shape
deCellAnchor = new(decodeCellAnchor)
)
_ = f.xmlNewDecoder(strings.NewReader("<decodeCellAnchor>" + graphicFrame + "</decodeCellAnchor>")).Decode(&deCellAnchor)
if deCellAnchor.From == nil || deCellAnchor.Sp == nil {
return shape
}
if cell, err = CoordinatesToCellName(deCellAnchor.From.Col+1, deCellAnchor.From.Row+1); err != nil {
return shape
}
shape = &Shape{
Cell: cell,
Type: deCellAnchor.Sp.SpPr.PrstGeom.Prst,
Macro: deCellAnchor.Sp.Macro,
Format: GraphicOptions{
Name: deCellAnchor.Sp.NvSpPr.CNvPr.Name,
AltText: deCellAnchor.Sp.NvSpPr.CNvPr.Descr,
ScaleX: defaultDrawingScale,
ScaleY: defaultDrawingScale,
},
Line: LineOptions{Width: defaultLineWidth},
}
if deCellAnchor.To != nil {
width, height := f.getDecodeCellAnchorShapeSize(sheet, deCellAnchor)
shape.Width, shape.Height, shape.Format.ScaleX, shape.Format.ScaleY = f.getShapeSizeAndScale(width, height, deCellAnchor.Sp.SpPr.Xfrm.Ext.Cx, deCellAnchor.Sp.SpPr.Xfrm.Ext.Cy)
} else {
shape.Format.Positioning = "oneCell"
if deCellAnchor.Ext != nil {
width, height := deCellAnchor.Ext.Cx/EMU, deCellAnchor.Ext.Cy/EMU
shape.Width, shape.Height, shape.Format.ScaleX, shape.Format.ScaleY = f.getShapeSizeAndScale(width, height, deCellAnchor.Sp.SpPr.Xfrm.Ext.Cx, deCellAnchor.Sp.SpPr.Xfrm.Ext.Cy)
}
}
if deCellAnchor.ClientData != nil {
shape.Format.PrintObject = boolPtr(deCellAnchor.ClientData.FPrintsWithSheet)
shape.Format.Locked = boolPtr(deCellAnchor.ClientData.FLocksWithSheet)
}
deCellAnchor.Sp.extractShapeFill(shape)
shape.Paragraph = deCellAnchor.Sp.TxBody.extractRichTextRun()
return shape
}
// extractShapeFill extracts shape fill properties.
func (sp *xdrSp) extractShapeFill(shape *Shape) {
if sp.SpPr != nil {
if sp.SpPr.SolidFill != nil && sp.SpPr.SolidFill.SrgbClr != nil {
if sp.SpPr.SolidFill.SrgbClr.Val != nil {
shape.Fill.Color = []string{*sp.SpPr.SolidFill.SrgbClr.Val}
}
if sp.SpPr.SolidFill.SrgbClr.Alpha != nil && sp.SpPr.SolidFill.SrgbClr.Alpha.Val != nil {
shape.Fill.Transparency = 100 - (*sp.SpPr.SolidFill.SrgbClr.Alpha.Val / 1000)
}
}
if sp.SpPr.Ln != nil && sp.SpPr.Ln.W != 0 {
shape.Line.Width = float64(sp.SpPr.Ln.W) / 12700
}
}
if sp.Style != nil && sp.Style.LnRef != nil && sp.Style.LnRef.SrgbClr != nil && sp.Style.LnRef.SrgbClr.Val != nil {
shape.Line.Fill = Fill{Type: "pattern", Pattern: 1, Color: []string{*sp.Style.LnRef.SrgbClr.Val}}
}
}
// extractShapeFill extracts shape fill properties.
func (sp *decodeSp) extractShapeFill(shape *Shape) {
if sp.SpPr != nil {
if sp.SpPr.SolidFill != nil && sp.SpPr.SolidFill.SrgbClr != nil {
if sp.SpPr.SolidFill.SrgbClr.Val != nil {
shape.Fill.Color = []string{*sp.SpPr.SolidFill.SrgbClr.Val}
}
if sp.SpPr.SolidFill.SrgbClr.Alpha != nil && sp.SpPr.SolidFill.SrgbClr.Alpha.Val != nil {
shape.Fill.Transparency = 100 - (*sp.SpPr.SolidFill.SrgbClr.Alpha.Val / 1000)
}
}
if sp.SpPr.Ln.W != 0 {
shape.Line.Width = float64(sp.SpPr.Ln.W) / 12700
}
}
if sp.Style != nil && sp.Style.LnRef != nil && sp.Style.LnRef.SrgbClr != nil && sp.Style.LnRef.SrgbClr.Val != nil {
shape.Line.Fill = Fill{Type: "pattern", Pattern: 1, Color: []string{*sp.Style.LnRef.SrgbClr.Val}}
}
}
// extractRichTextRun extracts rich text runs from the text body.
func (tb *xdrTxBody) extractRichTextRun() []RichTextRun {
var paragraphs []RichTextRun
for _, p := range tb.P {
run := RichTextRun{}
if p.R != nil {
run.Text = p.R.T
run.Font = &Font{
Bold: p.R.RPr.B,
Italic: p.R.RPr.I,
Underline: p.R.RPr.U,
}
if p.R.RPr.Sz > 0 {
run.Font.Size = p.R.RPr.Sz / 100
}
if p.R.RPr.Latin != nil {
run.Font.Family = p.R.RPr.Latin.Typeface
}
if p.R.RPr.SolidFill != nil && p.R.RPr.SolidFill.SrgbClr != nil && p.R.RPr.SolidFill.SrgbClr.Val != nil {
run.Font.Color = *p.R.RPr.SolidFill.SrgbClr.Val
}
}
paragraphs = append(paragraphs, run)
}
return paragraphs
}
// extractRichTextRun extracts rich text runs from decoded text body.
func (tb *decodeTxBody) extractRichTextRun() []RichTextRun {
var paragraphs []RichTextRun
for _, p := range tb.P {
run := RichTextRun{}
if p.R != nil {
run.Text = p.R.T
run.Font = &Font{
Bold: p.R.RPr.B,
Italic: p.R.RPr.I,
Underline: p.R.RPr.U,
}
if p.R.RPr.Sz > 0 {
run.Font.Size = p.R.RPr.Sz / 100
}
if p.R.RPr.Latin != nil {
run.Font.Family = p.R.RPr.Latin.Typeface
}
if p.R.RPr.SolidFill != nil && p.R.RPr.SolidFill.SrgbClr != nil && p.R.RPr.SolidFill.SrgbClr.Val != nil {
run.Font.Color = *p.R.RPr.SolidFill.SrgbClr.Val
}
}
paragraphs = append(paragraphs, run)
}
return paragraphs
}
// getCellAnchorShapeSize calculates the shape size in pixels by given cell
// anchor.
func (f *File) getCellAnchorShapeSize(sheet string, cellAnchor *xdrCellAnchor) (int, int) {
var width, height int
for col := cellAnchor.From.Col; col < cellAnchor.To.Col; col++ {
width += f.getColWidth(sheet, col+1)
}
width += cellAnchor.To.ColOff/EMU - cellAnchor.From.ColOff/EMU
for row := cellAnchor.From.Row; row < cellAnchor.To.Row; row++ {
height += f.getRowHeight(sheet, row+1)
}
height += cellAnchor.To.RowOff/EMU - cellAnchor.From.RowOff/EMU
return width, height
}
// getDecodeCellAnchorShapeSize calculates the shape size in pixels by given
// decode cell anchor.
func (f *File) getDecodeCellAnchorShapeSize(sheet string, deCellAnchor *decodeCellAnchor) (int, int) {
var width, height int
for col := deCellAnchor.From.Col; col < deCellAnchor.To.Col; col++ {
width += f.getColWidth(sheet, col+1)
}
width += deCellAnchor.To.ColOff/EMU - deCellAnchor.From.ColOff/EMU
for row := deCellAnchor.From.Row; row < deCellAnchor.To.Row; row++ {
height += f.getRowHeight(sheet, row+1)
}
height += deCellAnchor.To.RowOff/EMU - deCellAnchor.From.RowOff/EMU
return width, height
}
// getShapeSizeAndScale calculates the shape size and scale by given actual
// width and height in pixels, and the original cx and cy in EMU.
func (f *File) getShapeSizeAndScale(w, h, cx, cy int) (uint, uint, float64, float64) {
var (
width, height uint
scaleX, scaleY = defaultDrawingScale, defaultDrawingScale
)
if cx > 0 && cy > 0 {
width = uint(cx / EMU)
height = uint(cy / EMU)
if width > 0 {
scaleX = float64(w) / float64(width)
}
if height > 0 {
scaleY = float64(h) / float64(height)
}
}
return width, height, scaleX, scaleY
}