Skip to content

Commit

Permalink
Move CreateImageFromSVG into the svg package
Browse files Browse the repository at this point in the history
  • Loading branch information
richardwilkes committed Jul 30, 2024
1 parent 8baeaee commit 20c29c7
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 42 deletions.
3 changes: 2 additions & 1 deletion packaging/internal/package_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"text/template"

"github.com/richardwilkes/gcs/v5/model/gurps"
"github.com/richardwilkes/gcs/v5/svg"
"github.com/richardwilkes/toolbox/cmdline"
"github.com/richardwilkes/toolbox/errs"
"github.com/richardwilkes/toolbox/formats/icon"
Expand Down Expand Up @@ -68,7 +69,7 @@ func writeICNS(dstPath string, img image.Image) (err error) {
func writeDocICNS(dir string, base image.Image) error {
for i := range gurps.KnownFileTypes {
if fi := &gurps.KnownFileTypes[i]; fi.IsGCSData {
overlay, err := CreateImageFromSVG(fi.SVG, 512)
overlay, err := svg.CreateImageFromSVG(fi.SVG, 512)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion packaging/internal/package_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"time"

"github.com/richardwilkes/gcs/v5/model/gurps"
"github.com/richardwilkes/gcs/v5/svg"
"github.com/richardwilkes/gcs/v5/ux"
"github.com/richardwilkes/toolbox/cmdline"
"github.com/richardwilkes/toolbox/errs"
Expand Down Expand Up @@ -114,7 +115,7 @@ func addWindowsIcon(rs *winres.ResourceSet) error {
for i := range gurps.KnownFileTypes {
if fi := &gurps.KnownFileTypes[i]; fi.IsGCSData {
var overlay image.Image
if overlay, err = CreateImageFromSVG(fi.SVG, 512); err != nil {
if overlay, err = svg.CreateImageFromSVG(fi.SVG, 512); err != nil {
return err
}
var extIcon *winres.Icon
Expand Down
38 changes: 0 additions & 38 deletions packaging/internal/svg_image.go

This file was deleted.

23 changes: 23 additions & 0 deletions svg/svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@
package svg

import (
"bytes"
_ "embed"
"fmt"
"image"

"github.com/richardwilkes/toolbox/errs"
"github.com/richardwilkes/unison"
"github.com/srwiley/oksvg"
"github.com/srwiley/rasterx"
)

// Pre-defined SVG images used by GCS.
Expand Down Expand Up @@ -253,3 +259,20 @@ var (
weightData string
Weight = unison.MustSVGFromContentString(weightData)
)

// CreateImageFromSVG turns one of our svg-as-a-path objects into an actual SVG document, then renders it into an image
// at the specified square size. Note that this is not currently GPU accelerated, as I haven't added the necessary bits
// to unison to support scribbling into arbitrary offscreen images yet.
func CreateImageFromSVG(svg *unison.SVG, size int) (image.Image, error) {
var buffer bytes.Buffer
fmt.Fprintf(&buffer, `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 %f %f"><path d="%s"/></svg>`,
svg.Size().Width, svg.Size().Height, svg.PathScaledTo(1).ToSVGString(true))
icon, err := oksvg.ReadIconStream(&buffer)
if err != nil {
return nil, errs.Wrap(err)
}
icon.SetTarget(0, 0, float64(size), float64(size))
img := image.NewRGBA(image.Rect(0, 0, size, size))
icon.Draw(rasterx.NewDasher(size, size, rasterx.NewScannerGV(size, size, img, img.Bounds())), 1)
return img, nil
}
3 changes: 2 additions & 1 deletion ux/platform_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"strings"

"github.com/richardwilkes/gcs/v5/model/gurps"
"github.com/richardwilkes/gcs/v5/svg"
"github.com/richardwilkes/toolbox/cmdline"
"github.com/richardwilkes/toolbox/errs"
"github.com/richardwilkes/toolbox/formats/icon"
Expand Down Expand Up @@ -99,7 +100,7 @@ func installDesktopIcons() error {
for i := range gurps.KnownFileTypes {
if fi := &gurps.KnownFileTypes[i]; fi.IsGCSData {
var overlay image.Image
overlay, err = CreateImageFromSVG(fi.SVG, 128)
overlay, err = svg.CreateImageFromSVG(fi.SVG, 128)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion ux/platform_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"syscall"

"github.com/richardwilkes/gcs/v5/model/gurps"
"github.com/richardwilkes/gcs/v5/svg"
"github.com/richardwilkes/toolbox/cmdline"
"github.com/richardwilkes/toolbox/errs"
"github.com/richardwilkes/toolbox/formats/icon"
Expand Down Expand Up @@ -67,7 +68,7 @@ func configureRegistry() error {
if fi := &gurps.KnownFileTypes[i]; fi.IsGCSData {
// Create the doc icon
var overlay image.Image
if overlay, err = CreateImageFromSVG(fi.SVG, 128); err != nil {
if overlay, err = svg.CreateImageFromSVG(fi.SVG, 128); err != nil {
return err
}
docPath := filepath.Join(appDataDir, fi.Extensions[0][1:]+".ico")
Expand Down

0 comments on commit 20c29c7

Please sign in to comment.