Skip to content

Commit

Permalink
This closes #1820, converted styleFillVariants from slice to func (#1821
Browse files Browse the repository at this point in the history
)
  • Loading branch information
edwardfward authored Feb 24, 2024
1 parent ee2ef15 commit 02b84a9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
47 changes: 27 additions & 20 deletions styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -1071,25 +1071,28 @@ var (
"gray0625",
}
// styleFillVariants list all preset variants of the fill style.
styleFillVariants = []xlsxGradientFill{
{Degree: 90, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
{Degree: 270, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
{Degree: 90, Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}},
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
{Degree: 180, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
{Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}},
{Degree: 45, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
{Degree: 255, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
{Degree: 45, Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}},
{Degree: 135, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
{Degree: 315, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
{Degree: 135, Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}},
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path"},
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Left: 1, Right: 1},
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Bottom: 1, Top: 1},
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Bottom: 1, Left: 1, Right: 1, Top: 1},
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Bottom: 0.5, Left: 0.5, Right: 0.5, Top: 0.5},
styleFillVariants = func() []xlsxGradientFill {
return []xlsxGradientFill{
{Degree: 90, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
{Degree: 270, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
{Degree: 90, Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}},
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
{Degree: 180, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
{Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}},
{Degree: 45, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
{Degree: 255, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
{Degree: 45, Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}},
{Degree: 135, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
{Degree: 315, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
{Degree: 135, Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}},
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path"},
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Left: 1, Right: 1},
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Bottom: 1, Top: 1},
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Bottom: 1, Left: 1, Right: 1, Top: 1},
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Bottom: 0.5, Left: 0.5, Right: 0.5, Top: 0.5},
}
}

// getXfIDFuncs provides a function to get xfID by given style.
getXfIDFuncs = map[string]func(int, xlsxXf, *Style) bool{
"numFmt": func(numFmtID int, xf xlsxXf, style *Style) bool {
Expand Down Expand Up @@ -1132,6 +1135,7 @@ var (
return reflect.DeepEqual(xf.Protection, newProtection(style)) && xf.ApplyProtection != nil && *xf.ApplyProtection
},
}

// extractStyleCondFuncs provides a function set to returns if shoudle be
// extract style definition by given style.
extractStyleCondFuncs = map[string]func(xlsxXf, *xlsxStyleSheet) bool{
Expand All @@ -1157,6 +1161,7 @@ var (
return xf.ApplyProtection == nil || (xf.ApplyProtection != nil && *xf.ApplyProtection)
},
}

// drawContFmtFunc defines functions to create conditional formats.
drawContFmtFunc = map[string]func(p int, ct, ref, GUID string, fmtCond *ConditionalFormatOptions) (*xlsxCfRule, *xlsxX14CfRule){
"cellIs": drawCondFmtCellIs,
Expand All @@ -1176,6 +1181,7 @@ var (
"expression": drawCondFmtExp,
"iconSet": drawCondFmtIconSet,
}

// extractContFmtFunc defines functions to get conditional formats.
extractContFmtFunc = map[string]func(*File, *xlsxCfRule, *xlsxExtLst) ConditionalFormatOptions{
"cellIs": func(f *File, c *xlsxCfRule, extLst *xlsxExtLst) ConditionalFormatOptions {
Expand Down Expand Up @@ -1233,6 +1239,7 @@ var (
return f.extractCondFmtIconSet(c, extLst)
},
}

// validType defined the list of valid validation types.
validType = map[string]string{
"cell": "cellIs",
Expand Down Expand Up @@ -1456,7 +1463,7 @@ func (f *File) extractFills(fl *xlsxFill, s *xlsxStyleSheet, style *Style) {
var fill Fill
if fl.GradientFill != nil {
fill.Type = "gradient"
for shading, variants := range styleFillVariants {
for shading, variants := range styleFillVariants() {
if fl.GradientFill.Bottom == variants.Bottom &&
fl.GradientFill.Degree == variants.Degree &&
fl.GradientFill.Left == variants.Left &&
Expand Down Expand Up @@ -2024,7 +2031,7 @@ func newFills(style *Style, fg bool) *xlsxFill {
if len(style.Fill.Color) != 2 || style.Fill.Shading < 0 || style.Fill.Shading > 16 {
break
}
gradient := styleFillVariants[style.Fill.Shading]
gradient := styleFillVariants()[style.Fill.Shading]
gradient.Stop[0].Color.RGB = getPaletteColor(style.Fill.Color[0])
gradient.Stop[1].Color.RGB = getPaletteColor(style.Fill.Color[1])
if len(gradient.Stop) == 3 {
Expand Down
13 changes: 11 additions & 2 deletions styles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,16 @@ func TestNewStyle(t *testing.T) {
_, err = f.NewStyle(nil)
assert.NoError(t, err)

// Test gradient fills
f = NewFile()
styleID1, err := f.NewStyle(&Style{Fill: Fill{Type: "gradient", Color: []string{"FFFFFF", "4E71BE"}, Shading: 1, Pattern: 1}})
assert.NoError(t, err)
styleID2, err := f.NewStyle(&Style{Fill: Fill{Type: "gradient", Color: []string{"FF0000", "4E71BE"}, Shading: 1, Pattern: 1}})
assert.NoError(t, err)
assert.NotEqual(t, styleID1, styleID2)

var exp string
f = NewFile()
_, err = f.NewStyle(&Style{CustomNumFmt: &exp})
assert.Equal(t, ErrCustomNumFmt, err)
_, err = f.NewStyle(&Style{Font: &Font{Family: strings.Repeat("s", MaxFontFamilyLength+1)}})
Expand All @@ -356,7 +365,7 @@ func TestNewStyle(t *testing.T) {
CustomNumFmt: &numFmt,
})
assert.NoError(t, err)
assert.Equal(t, 2, styleID)
assert.Equal(t, 1, styleID)

assert.NotNil(t, f.Styles)
assert.NotNil(t, f.Styles.CellXfs)
Expand All @@ -371,7 +380,7 @@ func TestNewStyle(t *testing.T) {
NumFmt: 32, // must not be in currencyNumFmt
})
assert.NoError(t, err)
assert.Equal(t, 3, styleID)
assert.Equal(t, 2, styleID)

assert.NotNil(t, f.Styles)
assert.NotNil(t, f.Styles.CellXfs)
Expand Down

0 comments on commit 02b84a9

Please sign in to comment.