Skip to content

Commit 70f6328

Browse files
committed
Support set font style of shape text. Relate issue qax-os#38.
1 parent cf97118 commit 70f6328

File tree

7 files changed

+96
-61
lines changed

7 files changed

+96
-61
lines changed

chart.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ func (f *File) addChart(formatSet *formatChart) {
252252
Rich: &cRich{
253253
P: aP{
254254
PPr: &aPPr{
255-
DefRPr: aDefRPr{
255+
DefRPr: aRPr{
256256
Kern: 1200,
257257
Strike: "noStrike",
258258
U: "none",
@@ -292,7 +292,7 @@ func (f *File) addChart(formatSet *formatChart) {
292292
TxPr: cTxPr{
293293
P: aP{
294294
PPr: &aPPr{
295-
DefRPr: aDefRPr{
295+
DefRPr: aRPr{
296296
Kern: 1200,
297297
U: "none",
298298
Sz: 14000,
@@ -812,7 +812,7 @@ func (f *File) drawPlotAreaTxPr() *cTxPr {
812812
},
813813
P: aP{
814814
PPr: &aPPr{
815-
DefRPr: aDefRPr{
815+
DefRPr: aRPr{
816816
Sz: 900,
817817
B: false,
818818
I: false,

comment.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func (f *File) addComment(commentsXML, cell string, formatSet *formatComment) {
158158
}
159159
comments := xlsxComments{
160160
Authors: []xlsxAuthor{
161-
xlsxAuthor{
161+
{
162162
Author: formatSet.Author,
163163
},
164164
},
@@ -168,7 +168,7 @@ func (f *File) addComment(commentsXML, cell string, formatSet *formatComment) {
168168
AuthorID: 0,
169169
Text: xlsxText{
170170
R: []xlsxR{
171-
xlsxR{
171+
{
172172
RPr: &xlsxRPr{
173173
B: " ",
174174
Sz: &attrValInt{Val: 9},
@@ -180,7 +180,7 @@ func (f *File) addComment(commentsXML, cell string, formatSet *formatComment) {
180180
},
181181
T: a,
182182
},
183-
xlsxR{
183+
{
184184
RPr: &xlsxRPr{
185185
Sz: &attrValInt{Val: 9},
186186
Color: &xlsxColor{

excelize_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ func TestAddShape(t *testing.T) {
517517
t.Log(err)
518518
}
519519
xlsx.AddShape("Sheet1", "A30", `{"type":"rect","text":"Rectangle Shape"}`)
520-
xlsx.AddShape("Sheet3", "H1", `{"type":"ellipseRibbon", "color":{"line":"#4286f4","fill":"#8eb9ff"}, "height": 90}`)
520+
xlsx.AddShape("Sheet3", "H1", `{"type":"ellipseRibbon", "color":{"line":"#4286f4","fill":"#8eb9ff"}, "font":{"bold":true,"italic":true,"family":"Berlin Sans FB Demi","size":36,"color":"#777777","underline":"single"}, "height": 90}`)
521521
err = xlsx.Save()
522522
if err != nil {
523523
t.Log(err)

shape.go

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ func parseFormatShapeSet(formatSet string) *formatShape {
2222
XScale: 1.0,
2323
YScale: 1.0,
2424
},
25+
Font: formatFont{
26+
Bold: false,
27+
Italic: false,
28+
Underline: "none",
29+
Family: "Calibri",
30+
Size: 11,
31+
Color: "#000000",
32+
},
2533
Text: " ",
2634
}
2735
json.Unmarshal([]byte(formatSet), &format)
@@ -33,7 +41,7 @@ func parseFormatShapeSet(formatSet string) *formatShape {
3341
// print settings) and properties set. For example, add text box (rect shape) in
3442
// Sheet1:
3543
//
36-
// xlsx.AddShape("Sheet1", "G6", `{"type":"rect", "text":"Rectangle Shape", "color":{"line":"#4286F4","fill":"#8eb9ff"}, "width": 180, "height": 90}`)
44+
// xlsx.AddShape("Sheet1", "G6", `{"type":"rect", "text":"Rectangle Shape", "color":{"line":"#4286F4","fill":"#8eb9ff"}, "font":{"bold":true,"italic":true,"family":"Berlin Sans FB Demi","size":36,"color":"#777777","underline":"sng"}, "width": 180, "height": 90}`)
3745
//
3846
// The following shows the type of chart supported by excelize:
3947
//
@@ -225,6 +233,27 @@ func parseFormatShapeSet(formatSet string) *formatShape {
225233
// wedgeRectCallout (Callout Wedge Rectangle Shape)
226234
// wedgeRoundRectCallout (Callout Wedge Round Rectangle Shape)
227235
//
236+
// The following shows the type of text underline supported by excelize:
237+
//
238+
// none
239+
// words
240+
// sng
241+
// dbl
242+
// heavy
243+
// dotted
244+
// dottedHeavy
245+
// dash
246+
// dashHeavy
247+
// dashLong
248+
// dashLongHeavy
249+
// dotDash
250+
// dotDashHeavy
251+
// dotDotDash
252+
// dotDotDashHeavy
253+
// wavy
254+
// wavyHeavy
255+
// wavyDbl
256+
//
228257
func (f *File) AddShape(sheet, cell, format string) {
229258
formatSet := parseFormatShapeSet(format)
230259
// Read sheet data.
@@ -250,6 +279,12 @@ func (f *File) AddShape(sheet, cell, format string) {
250279

251280
// addDrawingShape
252281
func (f *File) addDrawingShape(sheet, drawingXML, cell string, formatSet *formatShape) {
282+
textUnderlineType := map[string]bool{"none": true, "words": true, "sng": true, "dbl": true, "heavy": true, "dotted": true, "dottedHeavy": true, "dash": true, "dashHeavy": true, "dashLong": true, "dashLongHeavy": true, "dotDash": true, "dotDashHeavy": true, "dotDotDash": true, "dotDotDashHeavy": true, "wavy": true, "wavyHeavy": true, "wavyDbl": true}
283+
u := formatSet.Font.Underline
284+
_, ok := textUnderlineType[u]
285+
if !ok {
286+
u = "none"
287+
}
253288
cell = strings.ToUpper(cell)
254289
fromCol := string(strings.Map(letterOnlyMapF, cell))
255290
fromRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell))
@@ -262,7 +297,7 @@ func (f *File) addDrawingShape(sheet, drawingXML, cell string, formatSet *format
262297
content.A = NameSpaceDrawingML
263298
content.Xdr = NameSpaceDrawingMLSpreadSheet
264299
cNvPrID := 1
265-
_, ok := f.XLSX[drawingXML]
300+
_, ok = f.XLSX[drawingXML]
266301
if ok { // Append Model
267302
decodeWsDr := decodeWsDr{}
268303
xml.Unmarshal([]byte(f.readXML(drawingXML)), &decodeWsDr)
@@ -331,9 +366,18 @@ func (f *File) addDrawingShape(sheet, drawingXML, cell string, formatSet *format
331366
P: &aP{
332367
R: &aR{
333368
RPr: aRPr{
369+
I: formatSet.Font.Italic,
370+
B: formatSet.Font.Bold,
334371
Lang: "en-US",
335372
AltLang: "en-US",
336-
Sz: 1100,
373+
U: u,
374+
Sz: formatSet.Font.Size * 100,
375+
Latin: &aLatin{Typeface: formatSet.Font.Family},
376+
SolidFill: &aSolidFill{
377+
SrgbClr: &attrValString{
378+
Val: strings.Replace(strings.ToUpper(formatSet.Font.Color), "#", "", -1),
379+
},
380+
},
337381
},
338382
T: formatSet.Text,
339383
},

xmlChart.go

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -108,44 +108,15 @@ type aP struct {
108108
// formatting, since they are directly applied to the paragraph and supersede
109109
// any formatting from styles.
110110
type aPPr struct {
111-
DefRPr aDefRPr `xml:"a:defRPr"`
112-
}
113-
114-
// aDefRPr directly maps the a:defRPr element. This element contains all
115-
// default run level text properties for the text runs within a containing
116-
// paragraph. These properties are to be used when overriding properties have
117-
// not been defined within the rPr element.
118-
type aDefRPr struct {
119-
AltLang string `xml:"altLang,attr,omitempty"`
120-
B bool `xml:"b,attr"`
121-
Baseline int `xml:"baseline,attr"`
122-
Bmk string `xml:"bmk,attr,omitempty"`
123-
Cap string `xml:"cap,attr,omitempty"`
124-
Dirty bool `xml:"dirty,attr,omitempty"`
125-
Err bool `xml:"err,attr,omitempty"`
126-
I bool `xml:"i,attr"`
127-
Kern int `xml:"kern,attr"`
128-
Kumimoji bool `xml:"kumimoji,attr,omitempty"`
129-
Lang string `xml:"lang,attr,omitempty"`
130-
NoProof bool `xml:"noProof,attr,omitempty"`
131-
NormalizeH bool `xml:"normalizeH,attr,omitempty"`
132-
SmtClean bool `xml:"smtClean,attr,omitempty"`
133-
SmtID uint64 `xml:"smtId,attr,omitempty"`
134-
Spc int `xml:"spc,attr"`
135-
Strike string `xml:"strike,attr,omitempty"`
136-
Sz int `xml:"sz,attr"`
137-
U string `xml:"u,attr,omitempty"`
138-
SolidFill *aSolidFill `xml:"a:solidFill"`
139-
Latin *aLatin `xml:"a:latin"`
140-
Ea *aEa `xml:"a:ea"`
141-
Cs *aCs `xml:"a:cs"`
111+
DefRPr aRPr `xml:"a:defRPr"`
142112
}
143113

144114
// aSolidFill (Solid Fill) directly maps the solidFill element. This element
145115
// specifies a solid color fill. The shape is filled entirely with the specified
146116
// color.
147117
type aSolidFill struct {
148-
SchemeClr *aSchemeClr `xml:"a:schemeClr,omitempty"`
118+
SchemeClr *aSchemeClr `xml:"a:schemeClr"`
119+
SrgbClr *attrValString `xml:"a:srgbClr"`
149120
}
150121

151122
// aSchemeClr (Scheme Color) directly maps the a:schemeClr element. This
@@ -206,9 +177,29 @@ type aR struct {
206177
// properties are defined as direct formatting, since they are directly applied
207178
// to the run and supersede any formatting from styles.
208179
type aRPr struct {
209-
Lang string `xml:"lang,attr,omitempty"`
210-
AltLang string `xml:"altLang,attr,omitempty"`
211-
Sz int `xml:"sz,attr,omitempty"`
180+
AltLang string `xml:"altLang,attr,omitempty"`
181+
B bool `xml:"b,attr"`
182+
Baseline int `xml:"baseline,attr"`
183+
Bmk string `xml:"bmk,attr,omitempty"`
184+
Cap string `xml:"cap,attr,omitempty"`
185+
Dirty bool `xml:"dirty,attr,omitempty"`
186+
Err bool `xml:"err,attr,omitempty"`
187+
I bool `xml:"i,attr"`
188+
Kern int `xml:"kern,attr"`
189+
Kumimoji bool `xml:"kumimoji,attr,omitempty"`
190+
Lang string `xml:"lang,attr,omitempty"`
191+
NoProof bool `xml:"noProof,attr,omitempty"`
192+
NormalizeH bool `xml:"normalizeH,attr,omitempty"`
193+
SmtClean bool `xml:"smtClean,attr,omitempty"`
194+
SmtID uint64 `xml:"smtId,attr,omitempty"`
195+
Spc int `xml:"spc,attr"`
196+
Strike string `xml:"strike,attr,omitempty"`
197+
Sz int `xml:"sz,attr,omitempty"`
198+
U string `xml:"u,attr,omitempty"`
199+
SolidFill *aSolidFill `xml:"a:solidFill"`
200+
Latin *aLatin `xml:"a:latin"`
201+
Ea *aEa `xml:"a:ea"`
202+
Cs *aCs `xml:"a:cs"`
212203
}
213204

214205
// cSpPr (Shape Properties) directly maps the c:spPr element. This element
@@ -579,14 +570,10 @@ type formatChart struct {
579570

580571
// formatChartLegend directly maps the format settings of the chart legend.
581572
type formatChartLegend struct {
582-
None bool `json:"none"`
583-
DeleteSeries []int `json:"delete_series"`
584-
Font struct {
585-
Size int `json:"size"`
586-
Blod bool `json:"blod"`
587-
Italic bool `json:"italic"`
588-
} `json:"font"`
589-
Layout struct {
573+
None bool `json:"none"`
574+
DeleteSeries []int `json:"delete_series"`
575+
Font formatFont `json:"font"`
576+
Layout struct {
590577
X float64 `json:"x"`
591578
Y float64 `json:"y"`
592579
Width float64 `json:"width"`

xmlDrawing.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ type formatShape struct {
349349
Width int `json:"width"`
350350
Height int `json:"height"`
351351
Format formatPicture `json:"format"`
352+
Font formatFont `json:"font"`
352353
Text string `json:"text"`
353354
Color formatShapeColor `json:"color"`
354355
}

xmlStyles.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,16 @@ type xlsxStyleColors struct {
282282
Color string `xml:",innerxml"`
283283
}
284284

285+
// formatFont directly maps the styles settings of the fonts.
286+
type formatFont struct {
287+
Bold bool `json:"bold"`
288+
Italic bool `json:"italic"`
289+
Underline string `json:"underline"`
290+
Family string `json:"family"`
291+
Size int `json:"size"`
292+
Color string `json:"color"`
293+
}
294+
285295
// formatCellStyle directly maps the styles settings of the borders.
286296
type formatCellStyle struct {
287297
Border []struct {
@@ -295,14 +305,7 @@ type formatCellStyle struct {
295305
Color []string `json:"color"`
296306
Shading int `json:"shading"`
297307
} `json:"fill"`
298-
Font *struct {
299-
Bold bool `json:"bold"`
300-
Italic bool `json:"italic"`
301-
Underline string `json:"underline"`
302-
Family string `json:"family"`
303-
Size int `json:"size"`
304-
Color string `json:"color"`
305-
} `json:"font"`
308+
Font *formatFont `json:"font"`
306309
Alignment *struct {
307310
Horizontal string `json:"horizontal"`
308311
Indent int `json:"indent"`

0 commit comments

Comments
 (0)