@@ -960,7 +960,6 @@ func parseFormatStyleSet(style *Style) (*Style, error) {
960
960
func (f * File ) NewStyle (style * Style ) (int , error ) {
961
961
var (
962
962
fs * Style
963
- font * xlsxFont
964
963
err error
965
964
cellXfsID , fontID , borderID , fillID int
966
965
)
@@ -991,11 +990,10 @@ func (f *File) NewStyle(style *Style) (int, error) {
991
990
numFmtID := newNumFmt (s , fs )
992
991
993
992
if fs .Font != nil {
994
- fontID , _ = f .getFontID (s , fs )
993
+ fontID = f .getFontID (s , fs )
995
994
if fontID == - 1 {
996
995
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 ())
999
997
fontID = s .Fonts .Count - 1
1000
998
}
1001
999
}
@@ -1505,37 +1503,32 @@ func (f *File) extractFills(fl *xlsxFill, s *xlsxStyleSheet, style *Style) {
1505
1503
1506
1504
// extractFont provides a function to extract font styles settings by given
1507
1505
// 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"
1536
1519
}
1537
- style .Font = & font
1538
1520
}
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
1539
1532
}
1540
1533
1541
1534
// extractNumFmt provides a function to extract number format by given styles
@@ -1628,7 +1621,7 @@ func (f *File) GetStyle(idx int) (*Style, error) {
1628
1621
f .extractBorders (s .Borders .Border [* xf .BorderID ], s , style )
1629
1622
}
1630
1623
if extractStyleCondFuncs ["font" ](xf , s ) {
1631
- f . extractFont (s .Fonts .Font [* xf .FontID ], s , style )
1624
+ style . Font = extractFont (s .Fonts .Font [* xf .FontID ])
1632
1625
}
1633
1626
if extractStyleCondFuncs ["alignment" ](xf , s ) {
1634
1627
f .extractAlignment (xf .Alignment , s , style )
@@ -1645,16 +1638,13 @@ func (f *File) GetStyle(idx int) (*Style, error) {
1645
1638
func (f * File ) getStyleID (ss * xlsxStyleSheet , style * Style ) (int , error ) {
1646
1639
var (
1647
1640
err error
1648
- fontID int
1649
1641
styleID = - 1
1650
1642
)
1651
1643
if ss .CellXfs == nil {
1652
1644
return styleID , err
1653
1645
}
1654
1646
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 )
1658
1648
if style .CustomNumFmt != nil {
1659
1649
numFmtID = getCustomNumFmtID (ss , style )
1660
1650
}
@@ -1700,7 +1690,7 @@ func (f *File) NewConditionalStyle(style *Style) (int, error) {
1700
1690
dxf .Border = newBorders (fs )
1701
1691
}
1702
1692
if fs .Font != nil {
1703
- dxf .Font , _ = f . newFont (fs )
1693
+ dxf .Font = fs . Font . newFont ()
1704
1694
}
1705
1695
if fs .Protection != nil {
1706
1696
dxf .Protection = newProtection (fs )
@@ -1735,7 +1725,7 @@ func (f *File) GetConditionalStyle(idx int) (*Style, error) {
1735
1725
}
1736
1726
f .extractFills (xf .Fill , s , style )
1737
1727
f .extractBorders (xf .Border , s , style )
1738
- f . extractFont (xf .Font , s , style )
1728
+ style . Font = extractFont (xf .Font )
1739
1729
f .extractAlignment (xf .Alignment , s , style )
1740
1730
f .extractProtection (xf .Protection , s , style )
1741
1731
if xf .NumFmt != nil {
@@ -1820,23 +1810,18 @@ func (f *File) readDefaultFont() (*xlsxFont, error) {
1820
1810
1821
1811
// getFontID provides a function to get font ID.
1822
1812
// 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 {
1825
1814
fontID := - 1
1826
1815
if styleSheet .Fonts == nil || style .Font == nil {
1827
- return fontID , err
1816
+ return fontID
1828
1817
}
1829
1818
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 ()) {
1835
1820
fontID = idx
1836
- return fontID , err
1821
+ return fontID
1837
1822
}
1838
1823
}
1839
- return fontID , err
1824
+ return fontID
1840
1825
}
1841
1826
1842
1827
// newFontColor set font color by given styles.
@@ -1869,35 +1854,37 @@ func newFontColor(font *Font) *xlsxColor {
1869
1854
1870
1855
// newFont provides a function to add font style by given cell format
1871
1856
// 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
1876
1860
}
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 )},
1880
1863
Family : & attrValInt {Val : intPtr (2 )},
1881
1864
}
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 )}
1885
1867
}
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 }
1888
1870
}
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 }
1893
1880
}
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 ]) }
1896
1883
}
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 }
1899
1886
}
1900
- return & fnt , err
1887
+ return & font
1901
1888
}
1902
1889
1903
1890
// getNumFmtID provides a function to get number format code ID.
0 commit comments