commit e6262f6023aba2df14b9f86976dcf57364f33acc
parent a57a0acee894cdc0975d74bba1cbb995fbf5e2f5
Author: Jack Mordaunt <jackmordaunt@gmail.com>
Date: Sat, 17 Feb 2018 23:48:29 +0100
[*] Expanded the OsType to associate the types with their sizes.
Diffstat:
| M | icns.go | | | 57 | +++++++++++++++++++++++++++++++++++---------------------- |
| M | writer.go | | | 8 | ++++---- |
2 files changed, 39 insertions(+), 26 deletions(-)
diff --git a/icns.go b/icns.go
@@ -71,7 +71,7 @@ func NewIconSet(img image.Image, interp InterpolationFunction, format string) (*
}
icons := []*Icon{}
for _, size := range sizesFrom(biggest) {
- t, ok := getType(size)
+ t, ok := getTypeFromSize(size)
if !ok {
continue
}
@@ -139,28 +139,41 @@ func sizesFrom(max uint) []uint {
}
// OsType is a 4 character identifier used to differentiate icon types.
-type OsType string
+type OsType struct {
+ ID string
+ Size uint
+}
+
+var osTypes = []OsType{
+ {ID: "ic10", Size: uint(1024)},
+ {ID: "ic14", Size: uint(512)},
+ {ID: "ic13", Size: uint(256)},
+ {ID: "ic07", Size: uint(128)},
+ {ID: "ic12", Size: uint(64)},
+ {ID: "ic11", Size: uint(32)},
+}
-// getType returns the type for the given icon size (in px).
+// getTypeFromSize returns the type for the given icon size (in px).
// The boolean indicates whether the type exists.
-func getType(size uint) (OsType, bool) {
- // 'types' is a map of the OSTypes we care about.
- // All dimensions are considered as retina.
- //
- // Todo(jackmordaunt): Not sure if only retina is sufficient. Should all
- // types be handled? `iconutil` uses file names to determine whether a
- // retina image is desired eg: "icon_256x256@2.png", without such a hint
- // how can you disambiguate 256x256 standard vs 256x256 retina?
- // Do we even need to consider standard sizes over retina?
- // For now, just retina types are considered.
- types := map[uint]OsType{
- 1024: "ic10",
- 512: "ic14",
- 256: "ic13",
- 128: "ic07",
- 64: "ic12",
- 32: "ic11",
+func getTypeFromSize(size uint) (OsType, bool) {
+ for _, t := range osTypes {
+ if t.Size == size {
+ return t, true
+ }
+ }
+ return OsType{}, false
+}
+
+func getTypeFromID(ID string) (OsType, bool) {
+ for _, t := range osTypes {
+ if t.ID == ID {
+ return t, true
+ }
}
- v, ok := types[size]
- return v, ok
+ return OsType{}, false
+}
+
+func osTypeFromID(ID string) OsType {
+ t, _ := getTypeFromID(ID)
+ return t
}
diff --git a/writer.go b/writer.go
@@ -68,10 +68,10 @@ func encodeImage(img image.Image, format string) ([]byte, error) {
func (i *Icon) writeHeader(wr io.Writer) (int64, error) {
if !i.headerSet {
defer func() { i.headerSet = true }()
- i.header[0] = i.Type[0]
- i.header[1] = i.Type[1]
- i.header[2] = i.Type[2]
- i.header[3] = i.Type[3]
+ i.header[0] = i.Type.ID[0]
+ i.header[1] = i.Type.ID[1]
+ i.header[2] = i.Type.ID[2]
+ i.header[3] = i.Type.ID[3]
length := uint32(len(i.data) + 8)
writeUint32(i.header[4:8], length)
}