diff --git a/firefly/fs.go b/firefly/fs.go index c892a1c..be7968c 100644 --- a/firefly/fs.go +++ b/firefly/fs.go @@ -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 @@ -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. @@ -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. diff --git a/firefly/graphics_test.go b/firefly/graphics_test.go index 9d81eca..e199f12 100644 --- a/firefly/graphics_test.go +++ b/firefly/graphics_test.go @@ -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 @@ -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) @@ -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 { diff --git a/firefly/sudo/sudo.go b/firefly/sudo/sudo.go index 5733b9f..0842c01 100644 --- a/firefly/sudo/sudo.go +++ b/firefly/sudo/sudo.go @@ -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) {