diff --git a/examples/1603-platform-stb-image.sx b/examples/1603-platform-stb-image.sx index de91481..94c0a37 100644 --- a/examples/1603-platform-stb-image.sx +++ b/examples/1603-platform-stb-image.sx @@ -1,5 +1,5 @@ #import "modules/std.sx"; -stb :: #import "modules/ffi/stb.sx"; +stb :: #import "vendors/stb_image/stb_image.sx"; main :: () -> i32 { w: i32 = 0; diff --git a/examples/1625-vendor-stb-image-decode.sx b/examples/1625-vendor-stb-image-decode.sx new file mode 100644 index 0000000..ea325de --- /dev/null +++ b/examples/1625-vendor-stb-image-decode.sx @@ -0,0 +1,35 @@ +// The sx library ships stb_image: `#import "vendors/stb_image/ +// stb_image.sx"` resolves through the stdlib search paths and the +// implementation compiles through the object cache. Decodes a 2x2 +// 24-bit BMP built in memory — fully deterministic: dimensions, +// channel count, and the top-left pixel (blue) are pinned. +#import "modules/std.sx"; +stb :: #import "vendors/stb_image/stb_image.sx"; + +main :: () -> i32 { + // BITMAPFILEHEADER (14) + BITMAPINFOHEADER (40) + 2 rows of + // 2 BGR pixels padded to 4-byte rows (8 each) = 70 bytes. + // Bottom row: red, green; top row: blue, white (BMP is bottom-up). + bmp : [70]u8 = .{ + 66, 77, 70, 0, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0, + 40, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 1, 0, 24, 0, + 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 255, 0, 255, 0, 0, 0, + 255, 0, 0, 255, 255, 255, 0, 0, + }; + + w : i32 = 0; + h : i32 = 0; + ch : i32 = 0; + img := stb.stbi_load_from_memory(@bmp[0], 70, @w, @h, @ch, 3); + if xx img == 0 { + print("decode failed\n"); + return 1; + } + p : [*]u8 = xx img; + print("decoded {}x{} ({} channels)\n", w, h, ch); + print("top-left pixel: {} {} {}\n", p[0], p[1], p[2]); + stb.stbi_image_free(xx img); + 0 +} diff --git a/examples/1626-vendor-stb-truetype-metrics.sx b/examples/1626-vendor-stb-truetype-metrics.sx new file mode 100644 index 0000000..c3f6e2c --- /dev/null +++ b/examples/1626-vendor-stb-truetype-metrics.sx @@ -0,0 +1,36 @@ +// The sx library ships stb_truetype: `#import "vendors/stb_truetype/ +// stb_truetype.sx"` resolves through the stdlib search paths and the +// implementation compiles through the object cache. Loads the system +// Helvetica collection and pins INVARIANTS only (font versions vary +// across macOS releases): init succeeds, the pixel-height scale is +// positive, the ascender is positive and the descender negative. +#import "modules/std.sx"; +fs :: #import "modules/std/fs.sx"; +tt :: #import "vendors/stb_truetype/stb_truetype.sx"; + +main :: () -> i32 { + data := fs.read_file("/System/Library/Fonts/Helvetica.ttc"); + if data == null { + print("font missing\n"); + return 1; + } + bytes := data!; + + off := tt.stbtt_GetFontOffsetForIndex(bytes.ptr, 0); + print("font offset >= 0: {}\n", off >= 0); + + info : *void = xx context.allocator.alloc_bytes(256); + ok := tt.stbtt_InitFont(info, bytes.ptr, off); + print("init ok: {}\n", ok != 0); + + px : f32 = 32.0; + scale := tt.stbtt_ScaleForPixelHeight(info, px); + print("scale > 0: {}\n", scale > 0.0); + + ascent : i32 = 0; + descent : i32 = 0; + linegap : i32 = 0; + tt.stbtt_GetFontVMetrics(info, @ascent, @descent, @linegap); + print("ascent > 0, descent < 0: {} {}\n", ascent > 0, descent < 0); + 0 +} diff --git a/examples/expected/1625-vendor-stb-image-decode.exit b/examples/expected/1625-vendor-stb-image-decode.exit new file mode 100644 index 0000000..573541a --- /dev/null +++ b/examples/expected/1625-vendor-stb-image-decode.exit @@ -0,0 +1 @@ +0 diff --git a/examples/expected/1625-vendor-stb-image-decode.stderr b/examples/expected/1625-vendor-stb-image-decode.stderr new file mode 100644 index 0000000..e69de29 diff --git a/examples/expected/1625-vendor-stb-image-decode.stdout b/examples/expected/1625-vendor-stb-image-decode.stdout new file mode 100644 index 0000000..a5559b0 --- /dev/null +++ b/examples/expected/1625-vendor-stb-image-decode.stdout @@ -0,0 +1,2 @@ +decoded 2x2 (3 channels) +top-left pixel: 0 0 255 diff --git a/examples/expected/1626-vendor-stb-truetype-metrics.exit b/examples/expected/1626-vendor-stb-truetype-metrics.exit new file mode 100644 index 0000000..573541a --- /dev/null +++ b/examples/expected/1626-vendor-stb-truetype-metrics.exit @@ -0,0 +1 @@ +0 diff --git a/examples/expected/1626-vendor-stb-truetype-metrics.stderr b/examples/expected/1626-vendor-stb-truetype-metrics.stderr new file mode 100644 index 0000000..e69de29 diff --git a/examples/expected/1626-vendor-stb-truetype-metrics.stdout b/examples/expected/1626-vendor-stb-truetype-metrics.stdout new file mode 100644 index 0000000..6e38258 --- /dev/null +++ b/examples/expected/1626-vendor-stb-truetype-metrics.stdout @@ -0,0 +1,4 @@ +font offset >= 0: true +init ok: true +scale > 0: true +ascent > 0, descent < 0: true true diff --git a/library/modules/ffi/stb.sx b/library/modules/ffi/stb.sx deleted file mode 100644 index 2197cce..0000000 --- a/library/modules/ffi/stb.sx +++ /dev/null @@ -1,7 +0,0 @@ -#import c { - #include "vendors/stb_image/stb_image.h"; - #source "vendors/stb_image/stb_image_impl.c"; - - #include "vendors/stb_image/stb_image_write.h"; - #source "vendors/stb_image/stb_image_write_impl.c"; -}; diff --git a/library/modules/ffi/stb_truetype.sx b/library/modules/ffi/stb_truetype.sx index b1cc40b..bcf5dcc 100644 --- a/library/modules/ffi/stb_truetype.sx +++ b/library/modules/ffi/stb_truetype.sx @@ -1,7 +1,10 @@ -#import c { - #include "vendors/stb_truetype/stb_truetype.h"; - #source "vendors/stb_truetype/stb_truetype_impl.c"; +// Font stack for the ui modules: the sx-shipped stb_truetype +// (vendors/stb_truetype/) plus the in-repo text-shaping companions +// (file_utils, kb_text_shape — repo-root vendors/, not yet promoted +// to the library). +#import "vendors/stb_truetype/stb_truetype.sx"; +#import c { #include "vendors/file_utils/file_utils.h"; #source "vendors/file_utils/file_utils.c"; diff --git a/library/modules/ui/glyph_cache.sx b/library/modules/ui/glyph_cache.sx index 7195555..275133c 100755 --- a/library/modules/ui/glyph_cache.sx +++ b/library/modules/ui/glyph_cache.sx @@ -4,7 +4,7 @@ #import "modules/gpu/types.sx"; #import "modules/gpu/api.sx"; #import "modules/ffi/stb_truetype.sx"; -#import "modules/ffi/stb.sx"; +#import "vendors/stb_image/stb_image.sx"; #import "modules/ui/types.sx"; // Cached glyph data with UV coordinates into the atlas texture diff --git a/library/vendors/stb_image/README.md b/library/vendors/stb_image/README.md new file mode 100644 index 0000000..a59a717 --- /dev/null +++ b/library/vendors/stb_image/README.md @@ -0,0 +1,20 @@ +# vendors/stb_image — image decode/encode for sx programs + +- stb_image: **v2.30**; stb_image_write: **v1.16** (version comments at + the top of each header) +- Source: (`stb_image.h`, + `stb_image_write.h`) +- License: public domain / MIT, dual (see the headers' license blocks) +- Files: `c/stb_image.h` + `c/stb_image_impl.c`, `c/stb_image_write.h` + + `c/stb_image_write_impl.c` (each impl .c defines the + `*_IMPLEMENTATION` macro and includes its header) + +`#import "vendors/stb_image/stb_image.sx"` resolves through the stdlib +search paths; the decls (`stbi_load`, `stbi_load_from_memory`, +`stbi_image_free`, `stbi_write_png`, …) are synthesized from the +headers, and the implementation compiles once per machine through sx's +object cache. `examples/1625-vendor-stb-image-decode.sx` pins an +in-memory BMP decode in the sx suite. + +To upgrade: replace the headers under `c/` with newer upstream copies, +update this file, and rebuild (the object cache keys on source bytes). diff --git a/vendors/stb_image/stb_image.h b/library/vendors/stb_image/c/stb_image.h similarity index 100% rename from vendors/stb_image/stb_image.h rename to library/vendors/stb_image/c/stb_image.h diff --git a/vendors/stb_image/stb_image_impl.c b/library/vendors/stb_image/c/stb_image_impl.c similarity index 100% rename from vendors/stb_image/stb_image_impl.c rename to library/vendors/stb_image/c/stb_image_impl.c diff --git a/vendors/stb_image/stb_image_write.h b/library/vendors/stb_image/c/stb_image_write.h similarity index 100% rename from vendors/stb_image/stb_image_write.h rename to library/vendors/stb_image/c/stb_image_write.h diff --git a/vendors/stb_image/stb_image_write_impl.c b/library/vendors/stb_image/c/stb_image_write_impl.c similarity index 100% rename from vendors/stb_image/stb_image_write_impl.c rename to library/vendors/stb_image/c/stb_image_write_impl.c diff --git a/library/vendors/stb_image/stb_image.sx b/library/vendors/stb_image/stb_image.sx new file mode 100644 index 0000000..4b7cc55 --- /dev/null +++ b/library/vendors/stb_image/stb_image.sx @@ -0,0 +1,19 @@ +// ===================================================================== +// vendors/stb_image — Sean Barrett's stb_image (decode) and +// stb_image_write (encode) as one `#import c` unit (c/, see README.md +// for versions + provenance). +// +// `#import "vendors/stb_image/stb_image.sx"` gives any sx program +// image decode/encode with no system dependency and no build flags: +// the decls are synthesized from the headers, and the implementation +// compiles through sx's content-addressed object cache — once per +// machine, not once per run. +// ===================================================================== + +#import c { + #include "c/stb_image.h"; + #source "c/stb_image_impl.c"; + + #include "c/stb_image_write.h"; + #source "c/stb_image_write_impl.c"; +}; diff --git a/library/vendors/stb_truetype/README.md b/library/vendors/stb_truetype/README.md new file mode 100644 index 0000000..d5b19c6 --- /dev/null +++ b/library/vendors/stb_truetype/README.md @@ -0,0 +1,20 @@ +# vendors/stb_truetype — font parsing/rasterization for sx programs + +- Version: **v1.26** (version comment at the top of the header) +- Source: (`stb_truetype.h`) +- License: public domain / MIT, dual (see the header's license block) +- Files: `c/stb_truetype.h` + `c/stb_truetype_impl.c` (the impl .c + defines `STB_TRUETYPE_IMPLEMENTATION` and includes the header) + +`#import "vendors/stb_truetype/stb_truetype.sx"` resolves through the +stdlib search paths; the decls (`stbtt_InitFont`, +`stbtt_ScaleForPixelHeight`, `stbtt_GetFontVMetrics`, +`stbtt_MakeGlyphBitmap`, …) are synthesized from the header, and the +implementation compiles once per machine through sx's object cache. +`stbtt_fontinfo` is opaque on the sx side: allocate a 256-byte blob +and pass its pointer (modules/ui/glyph_cache.sx is the reference +consumer; `examples/1626-vendor-stb-truetype-metrics.sx` pins font +init + metrics in the sx suite). + +To upgrade: replace `c/stb_truetype.h` with a newer upstream copy, +update this file, and rebuild (the object cache keys on source bytes). diff --git a/vendors/stb_truetype/stb_truetype.h b/library/vendors/stb_truetype/c/stb_truetype.h similarity index 100% rename from vendors/stb_truetype/stb_truetype.h rename to library/vendors/stb_truetype/c/stb_truetype.h diff --git a/vendors/stb_truetype/stb_truetype_impl.c b/library/vendors/stb_truetype/c/stb_truetype_impl.c similarity index 100% rename from vendors/stb_truetype/stb_truetype_impl.c rename to library/vendors/stb_truetype/c/stb_truetype_impl.c diff --git a/library/vendors/stb_truetype/stb_truetype.sx b/library/vendors/stb_truetype/stb_truetype.sx new file mode 100644 index 0000000..2d85ee5 --- /dev/null +++ b/library/vendors/stb_truetype/stb_truetype.sx @@ -0,0 +1,17 @@ +// ===================================================================== +// vendors/stb_truetype — Sean Barrett's stb_truetype (TTF/TTC +// parsing, glyph metrics, bitmap rasterization) as a `#import c` unit +// (c/, see README.md for version + provenance). +// +// `#import "vendors/stb_truetype/stb_truetype.sx"` gives any sx +// program font loading with no system dependency: the decls are +// synthesized from the header, and the implementation compiles +// through sx's content-addressed object cache. `stbtt_fontinfo` is +// opaque on the sx side — allocate a 256-byte blob and pass its +// pointer (see modules/ui/glyph_cache.sx for the established idiom). +// ===================================================================== + +#import c { + #include "c/stb_truetype.h"; + #source "c/stb_truetype_impl.c"; +};