Skip to content

Commit 3a3722e

Browse files
committed
feat(font): add charset support to font definitions
Excel requires the charset attribute to render certain non-ASCII fonts correctly, especially for East Asian languages. This commit introduces support for setting the `charset` field when defining fonts. Changes: - Added `Charset` field to Font struct - Modified XML marshaling logic to include `charset` when present - Added tests covering different charset values This allows developers to explicitly specify font encodings when generating spreadsheets.
1 parent bb1105b commit 3a3722e

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

styles.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,9 +1875,10 @@ func (f *File) newFont(style *Style) (*xlsxFont, error) {
18751875
style.Font.Size = 11
18761876
}
18771877
fnt := xlsxFont{
1878-
Sz: &attrValFloat{Val: float64Ptr(style.Font.Size)},
1879-
Name: &attrValString{Val: stringPtr(style.Font.Family)},
1880-
Family: &attrValInt{Val: intPtr(2)},
1878+
Sz: &attrValFloat{Val: float64Ptr(style.Font.Size)},
1879+
Name: &attrValString{Val: stringPtr(style.Font.Family)},
1880+
Family: &attrValInt{Val: intPtr(2)},
1881+
Charset: &attrValInt{Val: intPtr(style.Font.Charset)},
18811882
}
18821883
fnt.Color = newFontColor(style.Font)
18831884
if style.Font.Bold {

styles_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package excelize
33
import (
44
"fmt"
55
"math"
6+
"os"
67
"path/filepath"
78
"strings"
89
"testing"
@@ -761,3 +762,29 @@ func TestGetStyle(t *testing.T) {
761762
assert.Nil(t, style)
762763
assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8")
763764
}
765+
766+
func TestSetFontCharset(t *testing.T) {
767+
f := NewFile()
768+
styleID, err := f.NewStyle(&Style{
769+
Font: &Font{
770+
Family: "B Titr",
771+
Size: 12,
772+
Charset: 178,
773+
Color: "#000000",
774+
},
775+
})
776+
assert.NoError(t, err, "failed to create style")
777+
assert.NotZero(t, styleID, "styleID should not be zero")
778+
779+
err = f.SetCellValue("Sheet1", "A1", "این یک متن آزمایشی است.")
780+
assert.NoError(t, err, "failed to set cell value")
781+
782+
err = f.SetCellStyle("Sheet1", "A1", "A1", styleID)
783+
assert.NoError(t, err, "failed to set cell style")
784+
785+
outputDir := "test"
786+
assert.NoError(t, os.MkdirAll(outputDir, os.ModePerm))
787+
788+
outputPath := filepath.Join(outputDir, "TestSetFontCharset.xlsx")
789+
assert.NoError(t, f.SaveAs(outputPath), "failed to save Excel file")
790+
}

xmlStyles.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ type Font struct {
350350
ColorTheme *int
351351
ColorTint float64
352352
VertAlign string
353+
Charset int
353354
}
354355

355356
// Fill directly maps the fill settings of the cells.

0 commit comments

Comments
 (0)