Skip to content

Commit b1a930e

Browse files
committed
Revert changes for SVG image parser, and update TIF file for fix generated TestSetSheetBackgroundFromBytes.xlsx can not open in Office 2007
1 parent d14ff7c commit b1a930e

File tree

5 files changed

+12
-130
lines changed

5 files changed

+12
-130
lines changed

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
module github.com/xuri/excelize/v2
22

3-
go 1.23.3
3+
go 1.24.0
44

55
require (
66
github.com/richardlehane/mscfb v1.0.4
77
github.com/stretchr/testify v1.11.1
88
github.com/tiendc/go-deepcopy v1.7.1
99
github.com/xuri/efp v0.0.1
1010
github.com/xuri/nfp v0.0.2-0.20250530014748-2ddeb826f9a9
11-
golang.org/x/crypto v0.41.0
11+
golang.org/x/crypto v0.43.0
1212
golang.org/x/image v0.25.0
13-
golang.org/x/net v0.43.0
14-
golang.org/x/text v0.28.0
13+
golang.org/x/net v0.46.0
14+
golang.org/x/text v0.30.0
1515
)
1616

1717
require (

go.sum

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@ github.com/xuri/efp v0.0.1 h1:fws5Rv3myXyYni8uwj2qKjVaRP30PdjeYe2Y6FDsCL8=
1515
github.com/xuri/efp v0.0.1/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
1616
github.com/xuri/nfp v0.0.2-0.20250530014748-2ddeb826f9a9 h1:+C0TIdyyYmzadGaL/HBLbf3WdLgC29pgyhTjAT/0nuE=
1717
github.com/xuri/nfp v0.0.2-0.20250530014748-2ddeb826f9a9/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
18-
golang.org/x/crypto v0.41.0/go.mod h1:pO5AFd7FA68rFak7rOAGVuygIISepHftHnr8dr6+sUc=
1918
golang.org/x/crypto v0.43.0 h1:dduJYIi3A3KOfdGOHX8AVZ/jGiyPa3IbBozJ5kNuE04=
2019
golang.org/x/crypto v0.43.0/go.mod h1:BFbav4mRNlXJL4wNeejLpWxB7wMbc79PdRGhWKncxR0=
2120
golang.org/x/image v0.25.0 h1:Y6uW6rH1y5y/LK1J8BPWZtr6yZ7hrsy6hFrXjgsc2fQ=
2221
golang.org/x/image v0.25.0/go.mod h1:tCAmOEGthTtkalusGp1g3xa2gke8J6c2N565dTyl9Rs=
23-
golang.org/x/net v0.43.0/go.mod h1:vhO1fvI4dGsIjh73sWfUVjj3N7CA9WkKJNQm2svM6Jg=
2422
golang.org/x/net v0.46.0 h1:giFlY12I07fugqwPuWJi68oOnpfqFnJIJzaIIm2JVV4=
2523
golang.org/x/net v0.46.0/go.mod h1:Q9BGdFy1y4nkUwiLvT5qtyhAnEHgnQ/zd8PfU6nc210=
26-
golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU=
2724
golang.org/x/text v0.30.0 h1:yznKA/E9zq54KzlzBEAWn1NXSQ8DIp/NYMy88xJjl4k=
2825
golang.org/x/text v0.30.0/go.mod h1:yDdHFIX9t+tORqspjENWgzaCVXgk0yYnYuSZ8UzzBVM=
2926
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

picture.go

Lines changed: 3 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ import (
1515
"bytes"
1616
"encoding/xml"
1717
"image"
18-
"image/color"
1918
"io"
20-
"math"
2119
"os"
2220
"path"
2321
"path/filepath"
@@ -238,19 +236,9 @@ func (f *File) AddPictureFromBytes(sheet, cell string, pic *Picture) error {
238236
return ErrParameterInvalid
239237
}
240238
options := parseGraphicOptions(pic.Format)
241-
var img image.Config
242-
if ext == ".svg" {
243-
cfg, err := svgDecodeConfig(pic.File)
244-
if err != nil {
245-
return err
246-
}
247-
img = cfg
248-
} else {
249-
cfg, _, err := image.DecodeConfig(bytes.NewReader(pic.File))
250-
if err != nil {
251-
return err
252-
}
253-
img = cfg
239+
img, _, err := image.DecodeConfig(bytes.NewReader(pic.File))
240+
if err != nil {
241+
return err
254242
}
255243
// Read sheet data
256244
f.mu.Lock()
@@ -298,109 +286,6 @@ func (f *File) AddPictureFromBytes(sheet, cell string, pic *Picture) error {
298286
return err
299287
}
300288

301-
// --- SVG helpers ---
302-
303-
type svgRoot struct {
304-
Width string `xml:"width,attr"`
305-
Height string `xml:"height,attr"`
306-
ViewBox string `xml:"viewBox,attr"`
307-
}
308-
309-
// svgUnitToPx converts a numeric string with an optional unit suffix
310-
// (e.g. "16px", "12pt", "1in") to pixels using a 96-DPI scale,
311-
// which is the standard DPI for Office/Excel.
312-
func svgUnitToPx(s string) (float64, bool) {
313-
s = strings.TrimSpace(s)
314-
if s == "" {
315-
return 0, false
316-
}
317-
i := len(s)
318-
for i > 0 && (s[i-1] < '0' || s[i-1] > '9') && s[i-1] != '.' {
319-
i--
320-
}
321-
num := strings.TrimSpace(s[:i])
322-
unit := strings.ToLower(strings.TrimSpace(s[i:]))
323-
324-
v, err := strconv.ParseFloat(num, 64)
325-
if err != nil {
326-
return 0, false
327-
}
328-
switch unit {
329-
case "", "px":
330-
return v, true
331-
case "pt":
332-
return v * (96.0 / 72.0), true
333-
case "in":
334-
return v * 96.0, true
335-
case "mm":
336-
return v * (96.0 / 25.4), true
337-
case "cm":
338-
return v * (96.0 / 2.54), true
339-
default:
340-
return 0, false
341-
}
342-
}
343-
344-
// svgDecodeConfig extracts approximate image dimensions for SVG files
345-
// based on the <svg> element's width/height attributes or its viewBox.
346-
// Only the root <svg ...> element is parsed; the SVG content is not rendered.
347-
func svgDecodeConfig(b []byte) (image.Config, error) {
348-
var root svgRoot
349-
dec := xml.NewDecoder(bytes.NewReader(b))
350-
dec.Strict = false
351-
dec.AutoClose = xml.HTMLAutoClose
352-
dec.Entity = xml.HTMLEntity
353-
354-
// Read only the root <svg> element and decode its attributes.
355-
for {
356-
tok, err := dec.Token()
357-
if err != nil {
358-
return image.Config{}, err
359-
}
360-
if se, ok := tok.(xml.StartElement); ok && strings.EqualFold(se.Name.Local, "svg") {
361-
if err := dec.DecodeElement(&root, &se); err != nil && err != io.EOF {
362-
return image.Config{}, err
363-
}
364-
break
365-
}
366-
}
367-
368-
// 1) Use width/height attributes if both are present and valid.
369-
if wpx, okW := svgUnitToPx(root.Width); okW {
370-
if hpx, okH := svgUnitToPx(root.Height); okH {
371-
return image.Config{
372-
ColorModel: color.RGBAModel,
373-
Width: int(math.Max(1, math.Round(wpx))),
374-
Height: int(math.Max(1, math.Round(hpx))),
375-
}, nil
376-
}
377-
}
378-
379-
// 2) Otherwise, try to infer dimensions from the viewBox attribute:
380-
// "minX minY width height"
381-
if root.ViewBox != "" {
382-
parts := strings.Fields(root.ViewBox)
383-
if len(parts) == 4 {
384-
if vw, err1 := strconv.ParseFloat(parts[2], 64); err1 == nil && vw > 0 {
385-
if vh, err2 := strconv.ParseFloat(parts[3], 64); err2 == nil && vh > 0 {
386-
return image.Config{
387-
ColorModel: color.RGBAModel,
388-
Width: int(math.Max(1, math.Round(vw))),
389-
Height: int(math.Max(1, math.Round(vh))),
390-
}, nil
391-
}
392-
}
393-
}
394-
}
395-
396-
// 3) Fallback to a default icon-sized bounding box if nothing is specified.
397-
return image.Config{
398-
ColorModel: color.RGBAModel,
399-
Width: 16,
400-
Height: 16,
401-
}, nil
402-
}
403-
404289
// addSheetLegacyDrawing provides a function to add legacy drawing element to
405290
// xl/worksheets/sheet%d.xml by given worksheet name and relationship index.
406291
func (f *File) addSheetLegacyDrawing(sheet string, rID int) {

sheet.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1755,11 +1755,11 @@ func (f *File) GetPageLayout(sheet string) (PageLayoutOptions, error) {
17551755
//
17561756
// You can also use the function in RefersTo. For example:
17571757
//
1758-
// err := f.SetDefinedName(&excelize.DefinedName{
1759-
// Name: "CustomRange",
1760-
// RefersTo: "Sheet1!$A$2+Sheet1!$D$5",
1761-
// Scope: "Sheet1",
1762-
// })
1758+
// err := f.SetDefinedName(&excelize.DefinedName{
1759+
// Name: "CustomRange",
1760+
// RefersTo: "Sheet1!$A$2+Sheet1!$D$5",
1761+
// Scope: "Sheet1",
1762+
// })
17631763
func (f *File) SetDefinedName(definedName *DefinedName) error {
17641764
if definedName.Name == "" || definedName.RefersTo == "" {
17651765
return ErrParameterInvalid

test/images/excel.tif

100644100755
-845 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)