freetype: ~99% Core-API Coverage

- breaking structure changes
- optimazed examples
- more tests
This commit is contained in:
Ali Chraghi 2022-06-07 00:12:29 +04:30 committed by Stephen Gutekanst
parent db377459c4
commit 04a0a79ef6
21 changed files with 1176 additions and 549 deletions

View file

@ -33,7 +33,7 @@ const OutlinePrinter = struct {
}
pub fn outlineExists(self: Self) bool {
const outline = self.face.glyph.outline() orelse return false;
const outline = self.face.glyph().outline() orelse return false;
if (outline.numContours() <= 0 or outline.numPoints() <= 0)
return false;
outline.check() catch return false;
@ -48,7 +48,7 @@ const OutlinePrinter = struct {
.yx = 0 * multiplier,
.yy = -1 * multiplier,
};
self.face.glyph.outline().?.transform(matrix);
self.face.glyph().outline().?.transform(matrix);
}
pub fn extractOutline(self: *Self) !void {
@ -61,12 +61,12 @@ const OutlinePrinter = struct {
.shift = 0,
.delta = 0,
};
try self.face.glyph.outline().?.decompose(self, callbacks);
try self.face.glyph().outline().?.decompose(self, callbacks);
try self.path_stream.writer().writeAll("' fill='#000'/>");
}
pub fn computeViewBox(self: *Self) !void {
const boundingBox = try self.face.glyph.outline().?.bbox();
const boundingBox = try self.face.glyph().outline().?.bbox();
self.xMin = boundingBox.xMin;
self.yMin = boundingBox.yMin;
self.width = boundingBox.xMax - boundingBox.xMin;
@ -99,7 +99,7 @@ const OutlinePrinter = struct {
self.path_stream.writer().print("C {d} {d}, {d} {d}, {d} {d}\t", .{ control_0.x, control_0.y, control_1.x, control_1.y, to.x, to.y }) catch unreachable;
}
pub fn run(self: *Self, symbol: u8) !void {
pub fn run(self: *Self, symbol: u32) !void {
try self.face.loadChar(symbol, .{ .no_scale = true, .no_bitmap = true });
if (!self.outlineExists())
@ -118,5 +118,5 @@ pub fn main() !void {
var outline_printer = try OutlinePrinter.init(file);
defer outline_printer.deinit();
try outline_printer.run('a');
try outline_printer.run(@as(u32, 'ë'));
}

View file

@ -1,52 +1,27 @@
// zig build run-example-single-glyph -- B
const std = @import("std");
const freetype = @import("freetype");
const WIDTH = 32;
const HEIGHT = 24;
fn drawBitmap(bitmap: freetype.Bitmap, x: usize, y: usize) [HEIGHT][WIDTH]u8 {
var figure = std.mem.zeroes([HEIGHT][WIDTH]u8);
var p: usize = 0;
var q: usize = 0;
const w = bitmap.width();
const x_max = x + w;
const y_max = y + bitmap.rows();
var i: usize = 0;
while (i < x_max - x) : (i += 1) {
var j: usize = 0;
while (j < y_max - y) : (j += 1) {
if (i < WIDTH and j < HEIGHT) {
figure[j][i] |= bitmap.buffer()[q * w + p];
q += 1;
}
}
q = 0;
p += 1;
}
return figure;
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);
const lib = try freetype.Library.init();
defer lib.deinit();
const face = try lib.newFace("upstream/assets/FiraSans-Regular.ttf", 0);
defer face.deinit();
try face.setCharSize(40 * 64, 0, 50, 0);
try face.loadChar('@', .{ .render = true });
const glyph = face.glyph;
const x = @intCast(usize, glyph.bitmapLeft());
const y = HEIGHT - @intCast(usize, glyph.bitmapTop());
var figure = drawBitmap(glyph.bitmap(), x, y);
try face.setCharSize(60 * 48, 0, 50, 0);
try face.loadChar(args[1][0], .{ .render = true });
const bitmap = face.glyph().bitmap();
var i: usize = 0;
while (i < HEIGHT) : (i += 1) {
while (i < bitmap.rows()) : (i += 1) {
var j: usize = 0;
while (j < WIDTH) : (j += 1) {
const char: u8 = switch (figure[i][j]) {
while (j < bitmap.width()) : (j += 1) {
const char: u8 = switch (bitmap.buffer()[i * bitmap.width() + j]) {
0 => ' ',
1...128 => ';',
else => '#',