Browse Source

Don't duplicate font definition in style files.

Geoffrey J. Teale 11 years ago
parent
commit
edec1b0df0
3 changed files with 14 additions and 4 deletions
  1. 1 1
      file_test.go
  2. 3 3
      sheet_test.go
  3. 10 0
      xmlStyle.go

+ 1 - 1
file_test.go

@@ -650,7 +650,7 @@ func (l *FileSuite) TestMarshalFile(c *C) {
 	// For now we only allow simple string data in the
 	// spreadsheet.  Style support will follow.
 	expectedStyles := `<?xml version="1.0" encoding="UTF-8"?>
-<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><fonts count="2"><font><sz val="12"/><name val="Verdana"/><family val="0"/><charset val="0"/></font><font><sz val="12"/><name val="Verdana"/><family val="0"/><charset val="0"/></font></fonts><cellStyleXfs count="2"><xf applyAlignment="false" applyBorder="false" applyFont="false" applyFill="false" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="" indent="0" shrinkToFit="false" textRotation="0" vertical="" wrapText="false"/></xf><xf applyAlignment="false" applyBorder="false" applyFont="false" applyFill="false" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="0"><alignment horizontal="" indent="0" shrinkToFit="false" textRotation="0" vertical="" wrapText="false"/></xf></cellStyleXfs><cellXfs count="2"><xf applyAlignment="false" applyBorder="false" applyFont="false" applyFill="false" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="" indent="0" shrinkToFit="false" textRotation="0" vertical="" wrapText="false"/></xf><xf applyAlignment="false" applyBorder="false" applyFont="false" applyFill="false" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="0"><alignment horizontal="" indent="0" shrinkToFit="false" textRotation="0" vertical="" wrapText="false"/></xf></cellXfs></styleSheet>`
+<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><fonts count="1"><font><sz val="12"/><name val="Verdana"/><family val="0"/><charset val="0"/></font></fonts><cellStyleXfs count="2"><xf applyAlignment="false" applyBorder="false" applyFont="false" applyFill="false" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="" indent="0" shrinkToFit="false" textRotation="0" vertical="" wrapText="false"/></xf><xf applyAlignment="false" applyBorder="false" applyFont="false" applyFill="false" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="" indent="0" shrinkToFit="false" textRotation="0" vertical="" wrapText="false"/></xf></cellStyleXfs><cellXfs count="2"><xf applyAlignment="false" applyBorder="false" applyFont="false" applyFill="false" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="" indent="0" shrinkToFit="false" textRotation="0" vertical="" wrapText="false"/></xf><xf applyAlignment="false" applyBorder="false" applyFont="false" applyFill="false" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="" indent="0" shrinkToFit="false" textRotation="0" vertical="" wrapText="false"/></xf></cellXfs></styleSheet>`
 	c.Assert(parts["xl/styles.xml"], Equals, expectedStyles)
 }
 

+ 3 - 3
sheet_test.go

@@ -77,7 +77,7 @@ func (s *SheetSuite) TestMakeXLSXSheetAlsoPopulatesXLSXSTyles(c *C) {
 	styles := &xlsxStyleSheet{}
 	worksheet := sheet.makeXLSXSheet(refTable, styles)
 
-	c.Assert(styles.Fonts.Count, Equals, 2)
+	c.Assert(styles.Fonts.Count, Equals, 1)
 	c.Assert(styles.Fonts.Font[0].Sz.Val, Equals, "10")
 	c.Assert(styles.Fonts.Font[0].Name.Val, Equals, "Verdana")
 
@@ -98,7 +98,7 @@ func (s *SheetSuite) TestMakeXLSXSheetAlsoPopulatesXLSXSTyles(c *C) {
 	c.Assert(styles.CellStyleXfs.Xf[0].FillId, Equals, 0)
 	c.Assert(styles.CellStyleXfs.Xf[0].BorderId, Equals, 0)
 	// The 1st element cannot get initialised this way by accident.
-	c.Assert(styles.CellStyleXfs.Xf[1].FontId, Equals, 1)
+	c.Assert(styles.CellStyleXfs.Xf[1].FontId, Equals, 0)
 	c.Assert(styles.CellStyleXfs.Xf[1].FillId, Equals, 1)
 	c.Assert(styles.CellStyleXfs.Xf[1].BorderId, Equals, 1)
 
@@ -108,7 +108,7 @@ func (s *SheetSuite) TestMakeXLSXSheetAlsoPopulatesXLSXSTyles(c *C) {
 	c.Assert(styles.CellXfs.Xf[0].BorderId, Equals, 0)
 	// As above, we need the 1st element to make the test fail
 	// when it should.
-	c.Assert(styles.CellXfs.Xf[1].FontId, Equals, 1)
+	c.Assert(styles.CellXfs.Xf[1].FontId, Equals, 0)
 	c.Assert(styles.CellXfs.Xf[1].FillId, Equals, 1)
 	c.Assert(styles.CellXfs.Xf[1].BorderId, Equals, 1)
 

+ 10 - 0
xmlStyle.go

@@ -104,6 +104,12 @@ func (styles *xlsxStyleSheet) getNumberFormat(styleIndex int) string {
 }
 
 func (styles *xlsxStyleSheet) addFont(xFont xlsxFont) (index int) {
+	var font xlsxFont
+	for index, font = range styles.Fonts.Font {
+		if font.Equals(xFont) {
+			return index
+		}
+	}
 	styles.Fonts.Font = append(styles.Fonts.Font, xFont)
 	index = styles.Fonts.Count
 	styles.Fonts.Count += 1
@@ -294,6 +300,10 @@ type xlsxFont struct {
 	Color   xlsxColor `xml:"color,omitempty"`
 }
 
+func (font *xlsxFont) Equals(other xlsxFont) bool {
+	return font.Sz.Val == other.Sz.Val && font.Name.Val == other.Name.Val && font.Family.Val == other.Family.Val && font.Charset.Val == other.Charset.Val && font.Color.RGB == other.Color.RGB
+}
+
 func (font *xlsxFont) Marshal() (result string, err error) {
 	result = `<font>`
 	if font.Sz.Val != "" {