Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions firefly/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,30 @@ package firefly
import "unsafe"

// A file loaded from the filesystem.
type File struct {
Raw []byte
type File []byte

func (f File) Bytes() []byte {
return []byte(f)
}

// Convert the File to a Font.
func (f File) Font() Font {
return Font{f.Raw}
return Font{f}
}

// Convert the File to an Image.
func (f File) Image() Image {
return Image{f.Raw}
return Image{f}
}

// Check if the file was loaded.
func (f File) Exists() bool {
return len(f.Raw) != 0
return len(f) != 0
}

// Ensure that the loaded file exists.
func (f File) Must() File {
if len(f.Raw) == 0 {
if len(f) == 0 {
panic("file not found")
}
return f
Expand Down Expand Up @@ -74,15 +76,15 @@ func loadAllocFile(path string) File {
pathPtr := unsafe.Pointer(unsafe.StringData(path))
fileSize := getFileSize(pathPtr, uint32(len(path)))
if fileSize == 0 {
return File{nil}
return File(nil)
}
buf := make([]byte, fileSize)
buf := make(File, fileSize)
bufPtr := unsafe.Pointer(unsafe.SliceData(buf))
loadFile(
pathPtr, uint32(len(path)),
bufPtr, uint32(len(buf)),
)
return File{buf}
return buf
}

// Load the file into the given buffer.
Expand All @@ -94,9 +96,9 @@ func loadFileInto(path string, buf []byte) File {
bufPtr, uint32(len(buf)),
)
if fileSize == 0 {
return File{nil}
return File(nil)
}
return File{buf[:fileSize]}
return File(buf[:fileSize])
}

// Write a file into the app data dir.
Expand Down
18 changes: 9 additions & 9 deletions firefly/graphics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/firefly-zero/firefly-go/firefly"
)

var testImage = []byte{
var testImage = firefly.File{
0x22, // magic number
0x04, 0x00, // image width
0x01, // transparency
Expand All @@ -22,21 +22,21 @@ func TestImage_GetPixel(t *testing.T) {
P := firefly.P
tests := []struct {
name string
raw []byte
file firefly.File
pixel firefly.Point
want firefly.Color
}{
{name: "negative point", raw: testImage, pixel: P(-1, -1), want: firefly.ColorNone},
{name: "point out of bounds", raw: testImage, pixel: P(100, 100), want: firefly.ColorNone},
{name: "x0y0", raw: testImage, pixel: P(0, 0), want: firefly.ColorBlack},
{name: "x1y1", raw: testImage, pixel: P(1, 1), want: firefly.ColorLightGreen},
{name: "x2y3", raw: testImage, pixel: P(2, 3), want: firefly.ColorGray},
{name: "negative point", file: testImage, pixel: P(-1, -1), want: firefly.ColorNone},
{name: "point out of bounds", file: testImage, pixel: P(100, 100), want: firefly.ColorNone},
{name: "x0y0", file: testImage, pixel: P(0, 0), want: firefly.ColorBlack},
{name: "x1y1", file: testImage, pixel: P(1, 1), want: firefly.ColorLightGreen},
{name: "x2y3", file: testImage, pixel: P(2, 3), want: firefly.ColorGray},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
image := firefly.File{test.raw}.Image()
image := test.file.Image()
got := image.GetPixel(test.pixel)
if got != test.want {
t.Errorf("pixel: {%d, %d}, want %s, but got %s", test.pixel.X, test.pixel.Y, test.want, got)
Expand All @@ -47,7 +47,7 @@ func TestImage_GetPixel(t *testing.T) {

func TestImagePixels(t *testing.T) {
t.Parallel()
image := firefly.File{testImage}.Image()
image := testImage.Image()
got := image.Pixels()
want := 16
if got != want {
Expand Down
4 changes: 2 additions & 2 deletions firefly/sudo/sudo.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ func GetFileSize(path string) int {
func LoadFile(path string) firefly.File {
pathPtr := unsafe.Pointer(unsafe.StringData(path))
fileSize := getFileSize(pathPtr, uint32(len(path)))
raw := make([]byte, fileSize)
raw := make(firefly.File, fileSize)
rawPtr := unsafe.Pointer(unsafe.SliceData(raw))
loadFile(
pathPtr, uint32(len(path)),
rawPtr, fileSize,
)
return firefly.File{Raw: raw}
return raw
}

func RemoveFile(path string) {
Expand Down