From b1c9884f6d186bd1bfb4fc1d34061856345b8530 Mon Sep 17 00:00:00 2001 From: Harris Date: Thu, 25 Apr 2019 11:24:25 -0500 Subject: [PATCH] Add the ability to change the default font Closes #390 --- comment.go | 5 +++-- shape.go | 2 +- styles.go | 40 +++++++++++++++++++++++++++++++--------- styles_test.go | 30 +++++++++++++++++++++++++++++- templates.go | 2 +- 5 files changed, 65 insertions(+), 14 deletions(-) diff --git a/comment.go b/comment.go index 3cf0c1d..af70820 100644 --- a/comment.go +++ b/comment.go @@ -239,6 +239,7 @@ func (f *File) addComment(commentsXML, cell string, formatSet *formatComment) { }, } } + defaultFont := f.GetDefaultFont() cmt := xlsxComment{ Ref: cell, AuthorID: 0, @@ -251,7 +252,7 @@ func (f *File) addComment(commentsXML, cell string, formatSet *formatComment) { Color: &xlsxColor{ Indexed: 81, }, - RFont: &attrValString{Val: "Calibri"}, + RFont: &attrValString{Val: defaultFont}, Family: &attrValInt{Val: 2}, }, T: a, @@ -262,7 +263,7 @@ func (f *File) addComment(commentsXML, cell string, formatSet *formatComment) { Color: &xlsxColor{ Indexed: 81, }, - RFont: &attrValString{Val: "Calibri"}, + RFont: &attrValString{Val: defaultFont}, Family: &attrValInt{Val: 2}, }, T: t, diff --git a/shape.go b/shape.go index c90963c..7dc7021 100644 --- a/shape.go +++ b/shape.go @@ -381,7 +381,7 @@ func (f *File) addDrawingShape(sheet, drawingXML, cell string, formatSet *format Bold: false, Italic: false, Underline: "none", - Family: "Calibri", + Family: f.GetDefaultFont(), Size: 11, Color: "#000000", }, diff --git a/styles.go b/styles.go index fc8a290..e0e6f78 100644 --- a/styles.go +++ b/styles.go @@ -1896,7 +1896,7 @@ func (f *File) NewStyle(style string) (int, error) { if fs.Font != nil { s.Fonts.Count++ - s.Fonts.Font = append(s.Fonts.Font, setFont(fs)) + s.Fonts.Font = append(s.Fonts.Font, f.setFont(fs)) fontID = s.Fonts.Count - 1 } @@ -1932,7 +1932,7 @@ func (f *File) NewConditionalStyle(style string) (int, error) { Border: setBorders(fs), } if fs.Font != nil { - dxf.Font = setFont(fs) + dxf.Font = f.setFont(fs) } dxfStr, _ := xml.Marshal(dxf) if s.Dxfs == nil { @@ -1945,9 +1945,32 @@ func (f *File) NewConditionalStyle(style string) (int, error) { return s.Dxfs.Count - 1, nil } +// GetDefaultFont provides the default font name currently set in the workbook +// Documents generated by excelize start with Calibri +func (f *File) GetDefaultFont() string { + font := f.readDefaultFont() + return font.Name.Val +} + +// SetDefaultFont changes the default font in the workbook +func (f *File) SetDefaultFont(fontName string) { + font := f.readDefaultFont() + font.Name.Val = fontName + s := f.stylesReader() + s.Fonts.Font[0] = font + custom := true + s.CellStyles.CellStyle[0].CustomBuiltIn = &custom +} + +// readDefaultFont provides an unmarshalled font value +func (f *File) readDefaultFont() *xlsxFont { + s := f.stylesReader() + return s.Fonts.Font[0] +} + // setFont provides a function to add font style by given cell format // settings. -func setFont(formatStyle *formatStyle) *xlsxFont { +func (f *File) setFont(formatStyle *formatStyle) *xlsxFont { fontUnderlineType := map[string]string{"single": "single", "double": "double"} if formatStyle.Font.Size < 1 { formatStyle.Font.Size = 11 @@ -1955,7 +1978,7 @@ func setFont(formatStyle *formatStyle) *xlsxFont { if formatStyle.Font.Color == "" { formatStyle.Font.Color = "#000000" } - f := xlsxFont{ + fnt := xlsxFont{ B: formatStyle.Font.Bold, I: formatStyle.Font.Italic, Sz: &attrValInt{Val: formatStyle.Font.Size}, @@ -1963,15 +1986,14 @@ func setFont(formatStyle *formatStyle) *xlsxFont { Name: &attrValString{Val: formatStyle.Font.Family}, Family: &attrValInt{Val: 2}, } - if f.Name.Val == "" { - f.Name.Val = "Calibri" - f.Scheme = &attrValString{Val: "minor"} + if fnt.Name.Val == "" { + fnt.Name.Val = f.GetDefaultFont() } val, ok := fontUnderlineType[formatStyle.Font.Underline] if ok { - f.U = &attrValString{Val: val} + fnt.U = &attrValString{Val: val} } - return &f + return &fnt } // setNumFmt provides a function to check if number format code in the range diff --git a/styles_test.go b/styles_test.go index 54321bb..decfbb9 100644 --- a/styles_test.go +++ b/styles_test.go @@ -25,7 +25,7 @@ func TestStyleFill(t *testing.T) { xl := NewFile() styleID, err := xl.NewStyle(testCase.format) if err != nil { - t.Fatalf("%v", err) + t.Fatal(err) } styles := xl.stylesReader() @@ -165,3 +165,31 @@ func TestSetConditionalFormat(t *testing.T) { assert.EqualValues(t, testCase.rules, cf[0].CfRule, testCase.label) } } + +func TestNewStyle(t *testing.T) { + f := NewFile() + styleID, err := f.NewStyle(`{"font":{"bold":true,"italic":true,"family":"Berlin Sans FB Demi","size":36,"color":"#777777"}}`) + if err != nil { + t.Fatal(err) + } + styles := f.stylesReader() + fontID := styles.CellXfs.Xf[styleID].FontID + font := styles.Fonts.Font[fontID] + assert.Contains(t, font.Name.Val, "Berlin Sans FB Demi", "Stored font should contain font name") + assert.Equal(t, 2, styles.CellXfs.Count, "Should have 2 styles") +} + +func TestGetDefaultFont(t *testing.T) { + f := NewFile() + s := f.GetDefaultFont() + assert.Equal(t, s, "Calibri", "Default font should be Calibri") +} + +func TestSetDefaultFont(t *testing.T) { + f := NewFile() + f.SetDefaultFont("Ariel") + styles := f.stylesReader() + s := f.GetDefaultFont() + assert.Equal(t, s, "Ariel", "Default font should change to Ariel") + assert.Equal(t, *styles.CellStyles.CellStyle[0].CustomBuiltIn, true) +} diff --git a/templates.go b/templates.go index 17fc8d4..923cebd 100644 --- a/templates.go +++ b/templates.go @@ -27,7 +27,7 @@ const templateContentTypes = `` -const templateStyles = `` +const templateStyles = `` const templateSheet = ``