-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Initial dynamic icon implementation
- Loading branch information
Showing
9 changed files
with
196 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package dynamic_icon | ||
|
||
import ( | ||
"bytes" | ||
_ "embed" | ||
"errors" | ||
"fmt" | ||
"image" | ||
"os" | ||
|
||
"github.com/gabe565/nightscout-menu-bar/internal/config" | ||
"github.com/gabe565/nightscout-menu-bar/internal/nightscout" | ||
"github.com/golang/freetype" | ||
"github.com/golang/freetype/truetype" | ||
) | ||
|
||
var ( | ||
//go:embed Roboto-Bold.ttf | ||
embeddedFont []byte | ||
|
||
face *truetype.Font | ||
|
||
ErrInvalidColor = errors.New("invalid color") | ||
) | ||
|
||
func Generate(p *nightscout.Properties) ([]byte, error) { | ||
if face == nil { | ||
var b []byte | ||
if config.Default.DynamicIcon.FontFile == "" { | ||
b = embeddedFont | ||
} else { | ||
var err error | ||
if b, err = os.ReadFile(config.Default.DynamicIcon.FontFile); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
var err error | ||
if face, err = freetype.ParseFont(b); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
img := image.NewRGBA(image.Rectangle{Max: image.Point{X: 32, Y: 32}}) | ||
c := freetype.NewContext() | ||
c.SetFont(face) | ||
c.SetFontSize(config.Default.DynamicIcon.FontSize) | ||
c.SetClip(img.Bounds()) | ||
c.SetDst(img) | ||
switch config.Default.DynamicIcon.FontColor { | ||
case "white": | ||
c.SetSrc(image.White) | ||
case "black": | ||
c.SetSrc(image.Black) | ||
default: | ||
return nil, fmt.Errorf("%w: %s", ErrInvalidColor, config.Default.DynamicIcon.FontColor) | ||
} | ||
|
||
pt := freetype.Pt(0, config.Default.DynamicIcon.YOffset+int(c.PointToFixed(config.Default.DynamicIcon.FontSize)>>6)) | ||
if _, err := c.DrawString(p.Bgnow.DisplayBg(), pt); err != nil { | ||
return nil, err | ||
} | ||
|
||
var buf bytes.Buffer | ||
if err := encode(&buf, img); err != nil { | ||
return nil, err | ||
} | ||
|
||
return buf.Bytes(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//go:build !windows | ||
|
||
package dynamic_icon | ||
|
||
import ( | ||
"image" | ||
"image/png" | ||
"io" | ||
) | ||
|
||
func encode(w io.Writer, img image.Image) error { | ||
return png.Encode(w, img) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package dynamic_icon | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"image" | ||
"image/png" | ||
"io" | ||
) | ||
|
||
type iconDir struct { | ||
reserved uint16 | ||
imageType uint16 | ||
numImages uint16 | ||
} | ||
|
||
type iconDirEntry struct { | ||
imageWidth uint8 | ||
imageHeight uint8 | ||
numColors uint8 | ||
reserved uint8 | ||
colorPlanes uint16 | ||
bitsPerPixel uint16 | ||
sizeInBytes uint32 | ||
offset uint32 | ||
} | ||
|
||
func newIcondir() iconDir { | ||
return iconDir{ | ||
imageType: 1, | ||
numImages: 1, | ||
} | ||
} | ||
|
||
func newIcondirentry() iconDirEntry { | ||
return iconDirEntry{ | ||
colorPlanes: 1, // windows is supposed to not mind 0 or 1, but other icon files seem to have 1 here | ||
bitsPerPixel: 32, // can be 24 for bitmap or 24/32 for png. Set to 32 for now | ||
offset: 22, // 6 iconDir + 16 iconDirEntry, next image will be this image size + 16 iconDirEntry, etc | ||
} | ||
} | ||
|
||
func encode(w io.Writer, img image.Image) error { | ||
dir := newIcondir() | ||
entry := newIcondirentry() | ||
|
||
var buf bytes.Buffer | ||
if err := png.Encode(&buf, img); err != nil { | ||
return err | ||
} | ||
entry.sizeInBytes = uint32(buf.Len()) | ||
|
||
bounds := img.Bounds() | ||
entry.imageWidth = uint8(bounds.Dx()) | ||
entry.imageHeight = uint8(bounds.Dy()) | ||
|
||
if err := binary.Write(w, binary.LittleEndian, dir); err != nil { | ||
return err | ||
} | ||
if err := binary.Write(w, binary.LittleEndian, entry); err != nil { | ||
return err | ||
} | ||
|
||
if _, err := buf.WriteTo(w); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters