@@ -960,7 +960,6 @@ func parseFormatStyleSet(style *Style) (*Style, error) {
960960func (f * File ) NewStyle (style * Style ) (int , error ) {
961961 var (
962962 fs * Style
963- font * xlsxFont
964963 err error
965964 cellXfsID , fontID , borderID , fillID int
966965 )
@@ -991,11 +990,10 @@ func (f *File) NewStyle(style *Style) (int, error) {
991990 numFmtID := newNumFmt (s , fs )
992991
993992 if fs .Font != nil {
994- fontID , _ = f .getFontID (s , fs )
993+ fontID = f .getFontID (s , fs )
995994 if fontID == - 1 {
996995 s .Fonts .Count ++
997- font , _ = f .newFont (fs )
998- s .Fonts .Font = append (s .Fonts .Font , font )
996+ s .Fonts .Font = append (s .Fonts .Font , fs .Font .newFont ())
999997 fontID = s .Fonts .Count - 1
1000998 }
1001999 }
@@ -1505,37 +1503,32 @@ func (f *File) extractFills(fl *xlsxFill, s *xlsxStyleSheet, style *Style) {
15051503
15061504// extractFont provides a function to extract font styles settings by given
15071505// font styles definition.
1508- func (f * File ) extractFont (fnt * xlsxFont , s * xlsxStyleSheet , style * Style ) {
1509- if fnt != nil {
1510- var font Font
1511- if fnt .B != nil {
1512- font .Bold = fnt .B .Value ()
1513- }
1514- if fnt .I != nil {
1515- font .Italic = fnt .I .Value ()
1516- }
1517- if fnt .U != nil {
1518- if font .Underline = fnt .U .Value (); font .Underline == "" {
1519- font .Underline = "single"
1520- }
1521- }
1522- if fnt .Name != nil {
1523- font .Family = fnt .Name .Value ()
1524- }
1525- if fnt .Sz != nil {
1526- font .Size = fnt .Sz .Value ()
1527- }
1528- if fnt .Strike != nil {
1529- font .Strike = fnt .Strike .Value ()
1530- }
1531- if fnt .Color != nil {
1532- font .Color = strings .TrimPrefix (fnt .Color .RGB , "FF" )
1533- font .ColorIndexed = fnt .Color .Indexed
1534- font .ColorTheme = fnt .Color .Theme
1535- font .ColorTint = fnt .Color .Tint
1506+ func extractFont (fnt * xlsxFont ) * Font {
1507+ if fnt == nil {
1508+ return nil
1509+ }
1510+ var font Font
1511+ if fnt .Charset != nil {
1512+ font .Charset = fnt .Charset .Val
1513+ }
1514+ font .Bold = fnt .B .Value ()
1515+ font .Italic = fnt .I .Value ()
1516+ if fnt .U != nil {
1517+ if font .Underline = fnt .U .Value (); font .Underline == "" {
1518+ font .Underline = "single"
15361519 }
1537- style .Font = & font
15381520 }
1521+ font .Family = fnt .Name .Value ()
1522+ font .Size = fnt .Sz .Value ()
1523+ font .Strike = fnt .Strike .Value ()
1524+ if fnt .Color != nil {
1525+ font .Color = strings .TrimPrefix (fnt .Color .RGB , "FF" )
1526+ font .ColorIndexed = fnt .Color .Indexed
1527+ font .ColorTheme = fnt .Color .Theme
1528+ font .ColorTint = fnt .Color .Tint
1529+ }
1530+ font .VertAlign = fnt .VertAlign .Value ()
1531+ return & font
15391532}
15401533
15411534// extractNumFmt provides a function to extract number format by given styles
@@ -1628,7 +1621,7 @@ func (f *File) GetStyle(idx int) (*Style, error) {
16281621 f .extractBorders (s .Borders .Border [* xf .BorderID ], s , style )
16291622 }
16301623 if extractStyleCondFuncs ["font" ](xf , s ) {
1631- f . extractFont (s .Fonts .Font [* xf .FontID ], s , style )
1624+ style . Font = extractFont (s .Fonts .Font [* xf .FontID ])
16321625 }
16331626 if extractStyleCondFuncs ["alignment" ](xf , s ) {
16341627 f .extractAlignment (xf .Alignment , s , style )
@@ -1645,16 +1638,13 @@ func (f *File) GetStyle(idx int) (*Style, error) {
16451638func (f * File ) getStyleID (ss * xlsxStyleSheet , style * Style ) (int , error ) {
16461639 var (
16471640 err error
1648- fontID int
16491641 styleID = - 1
16501642 )
16511643 if ss .CellXfs == nil {
16521644 return styleID , err
16531645 }
16541646 numFmtID , borderID , fillID := getNumFmtID (ss , style ), getBorderID (ss , style ), getFillID (ss , style )
1655- if fontID , err = f .getFontID (ss , style ); err != nil {
1656- return styleID , err
1657- }
1647+ fontID := f .getFontID (ss , style )
16581648 if style .CustomNumFmt != nil {
16591649 numFmtID = getCustomNumFmtID (ss , style )
16601650 }
@@ -1700,7 +1690,7 @@ func (f *File) NewConditionalStyle(style *Style) (int, error) {
17001690 dxf .Border = newBorders (fs )
17011691 }
17021692 if fs .Font != nil {
1703- dxf .Font , _ = f . newFont (fs )
1693+ dxf .Font = fs . Font . newFont ()
17041694 }
17051695 if fs .Protection != nil {
17061696 dxf .Protection = newProtection (fs )
@@ -1735,7 +1725,7 @@ func (f *File) GetConditionalStyle(idx int) (*Style, error) {
17351725 }
17361726 f .extractFills (xf .Fill , s , style )
17371727 f .extractBorders (xf .Border , s , style )
1738- f . extractFont (xf .Font , s , style )
1728+ style . Font = extractFont (xf .Font )
17391729 f .extractAlignment (xf .Alignment , s , style )
17401730 f .extractProtection (xf .Protection , s , style )
17411731 if xf .NumFmt != nil {
@@ -1820,23 +1810,18 @@ func (f *File) readDefaultFont() (*xlsxFont, error) {
18201810
18211811// getFontID provides a function to get font ID.
18221812// If given font does not exist, will return -1.
1823- func (f * File ) getFontID (styleSheet * xlsxStyleSheet , style * Style ) (int , error ) {
1824- var err error
1813+ func (f * File ) getFontID (styleSheet * xlsxStyleSheet , style * Style ) int {
18251814 fontID := - 1
18261815 if styleSheet .Fonts == nil || style .Font == nil {
1827- return fontID , err
1816+ return fontID
18281817 }
18291818 for idx , fnt := range styleSheet .Fonts .Font {
1830- font , err := f .newFont (style )
1831- if err != nil {
1832- return fontID , err
1833- }
1834- if reflect .DeepEqual (* fnt , * font ) {
1819+ if reflect .DeepEqual (* fnt , * style .Font .newFont ()) {
18351820 fontID = idx
1836- return fontID , err
1821+ return fontID
18371822 }
18381823 }
1839- return fontID , err
1824+ return fontID
18401825}
18411826
18421827// newFontColor set font color by given styles.
@@ -1869,35 +1854,37 @@ func newFontColor(font *Font) *xlsxColor {
18691854
18701855// newFont provides a function to add font style by given cell format
18711856// settings.
1872- func (f * File ) newFont (style * Style ) (* xlsxFont , error ) {
1873- var err error
1874- if style .Font .Size < MinFontSize {
1875- style .Font .Size = 11
1857+ func (fnt * Font ) newFont () * xlsxFont {
1858+ if fnt .Size < MinFontSize {
1859+ fnt .Size = 11
18761860 }
1877- fnt := xlsxFont {
1878- Sz : & attrValFloat {Val : float64Ptr (style .Font .Size )},
1879- Name : & attrValString {Val : stringPtr (style .Font .Family )},
1861+ font := xlsxFont {
1862+ Sz : & attrValFloat {Val : float64Ptr (fnt .Size )},
18801863 Family : & attrValInt {Val : intPtr (2 )},
18811864 }
1882- fnt .Color = newFontColor (style .Font )
1883- if style .Font .Bold {
1884- fnt .B = & attrValBool {Val : & style .Font .Bold }
1865+ if fnt .Family != "" {
1866+ font .Name = & attrValString {Val : stringPtr (fnt .Family )}
18851867 }
1886- if style . Font . Italic {
1887- fnt . I = & attrValBool {Val : & style . Font . Italic }
1868+ if fnt . Charset != nil {
1869+ font . Charset = & attrValInt {Val : fnt . Charset }
18881870 }
1889- if * fnt .Name .Val == "" {
1890- if * fnt .Name .Val , err = f .GetDefaultFont (); err != nil {
1891- return & fnt , err
1892- }
1871+ font .Color = newFontColor (fnt )
1872+ if fnt .Bold {
1873+ font .B = & attrValBool {Val : & fnt .Bold }
1874+ }
1875+ if fnt .Italic {
1876+ font .I = & attrValBool {Val : & fnt .Italic }
1877+ }
1878+ if fnt .Strike {
1879+ font .Strike = & attrValBool {Val : & fnt .Strike }
18931880 }
1894- if style . Font . Strike {
1895- fnt . Strike = & attrValBool {Val : & style . Font . Strike }
1881+ if idx := inStrSlice ( supportedUnderlineTypes , fnt . Underline , true ); idx != - 1 {
1882+ font . U = & attrValString {Val : stringPtr ( supportedUnderlineTypes [ idx ]) }
18961883 }
1897- if idx := inStrSlice (supportedUnderlineTypes , style . Font . Underline , true ); idx != - 1 {
1898- fnt . U = & attrValString {Val : stringPtr ( supportedUnderlineTypes [ idx ]) }
1884+ if inStrSlice ([] string { "baseline" , "superscript" , "subscript" }, fnt . VertAlign , true ) != - 1 {
1885+ font . VertAlign = & attrValString {Val : & fnt . VertAlign }
18991886 }
1900- return & fnt , err
1887+ return & font
19011888}
19021889
19031890// getNumFmtID provides a function to get number format code ID.
0 commit comments