~andreafeletto/zig-schrift

a57b9325038015a21fbec2329f06e40cc0a9ff67 — Andrea Feletto 1 year, 9 months ago
version 0.10.2
3 files changed, 136 insertions(+), 0 deletions(-)

A LICENSE
A README.md
A schrift.zig
A  => LICENSE +21 -0
@@ 1,21 @@
MIT License

Copyright (c) 2022 Andrea Feletto

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

A  => README.md +17 -0
@@ 1,17 @@
# zig-schrift

Idiomatic [zig] bindings for [libschrift].

## Dependencies

* [zig] 0.10.0
* [libschrift] 0.10.2

## Tests

```sh
zig test schrift.zig -lc -lschrift
```

[zig]: https://ziglang.org
[libschrift]: https://github.com/tomolt/libschrift

A  => schrift.zig +98 -0
@@ 1,98 @@
const std = @import("std");
const mem = std.mem;
const testing = std.testing;

const c = @cImport(@cInclude("stdint.h"));

pub const SFT = extern struct {
    font: *Font,
    x_scale: f64,
    y_scale: f64,
    x_offset: f64,
    y_offset: f64,
    flags: c_int,

    extern fn sft_lmetrics(sft: *const SFT, buffer: *LMetrics) c_int;
    pub fn lmetrics(self: *const SFT, buffer: *LMetrics) !void {
        const ret = sft_lmetrics(self, buffer);
        if (ret != 0) return error.SchriftLMetrics;
    }

    extern fn sft_lookup(sft: *const SFT, codepoint: UChar, buffer: *Glyph) c_int;
    pub fn lookup(self: *const SFT, codepoint: UChar, buffer: *Glyph) !void {
        const ret = sft_lookup(self, codepoint, buffer);
        if (ret != 0) return error.SchriftLookup;
    }

    extern fn sft_gmetrics(sft: *const SFT, glyph: Glyph, buffer: *GMetrics) c_int;
    pub fn gmetrics(self: *const SFT, glyph: Glyph, buffer: *GMetrics) !void {
        const ret = sft_gmetrics(self, glyph, buffer);
        if (ret != 0) return error.SchriftGMetrics;
    }

    extern fn sft_kerning(sft: *const SFT, left: Glyph, right: Glyph, buffer: *Kerning) c_int;
    pub fn kerning(self: *const SFT, left: Glyph, right: Glyph, buffer: *Kerning) !void {
        const ret = sft_kerning(self, left, right, buffer);
        if (ret != 0) return error.SchriftKerning;
    }

    extern fn sft_render(sft: *const SFT, glyph: Glyph, image: Image) c_int;
    pub fn render(self: *const SFT, glyph: Glyph, image: Image) !void {
        const ret = sft_render(self, glyph, image);
        if (ret != 0) return error.SchriftRender;
    }
};

pub const Font = opaque {
    extern fn sft_loadmem(data: [*]const anyopaque, size: usize) ?*Font;
    pub fn loadMem(data: []const u8) !*Font {
        const ptr = @ptrCast([*]const anyopaque, data.ptr);
        return sft_loadmem(ptr, data.len) orelse error.SchriftFontLoading;
    }

    extern fn sft_loadfile(filename: [*:0]const u8) ?*Font;
    pub fn loadFileZ(filename: [:0]const u8) !*Font {
        return sft_loadfile(filename.ptr) orelse error.SchriftFontLoading;
    }

    extern fn sft_freefont(font: *Font) void;
    pub const destroy = sft_freefont;
};

pub const UChar = c.uint_least32_t;

pub const Glyph = c.uint_fast32_t;

pub const LMetrics = extern struct {
    ascender: f64,
    descender: f64,
    line_gap: f64,
};

pub const GMetrics = extern struct {
    advance_width: f64,
    left_side_bearing: f64,
    y_offset: c_int,
    min_width: c_int,
    min_height: c_int,
};

pub const Kerning = extern struct {
    x_shift: f64,
    y_shift: f64,
};

pub const Image = extern struct {
    pixels: [*]anyopaque,
    width: c_int,
    height: c_int,
};

extern fn sft_version() [*:0]const u8;
pub fn version() [:0]const u8 {
    return mem.span(sft_version());
}

test "declarations" {
    testing.refAllDecls(@This());
}