Skip to content

Commit

Permalink
update to zig 0.12.0-dev.3180 + fixes
Browse files Browse the repository at this point in the history
- Support RGBA(u8) type in all backends
  • Loading branch information
kdchambers committed Mar 20, 2024
1 parent 2410c83 commit 2056086
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 218 deletions.
27 changes: 13 additions & 14 deletions src/backends/fontana.zig
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ pub fn PenConfigInternal(comptime options: api.PenConfigOptionsInternal) type {
writer_interface: anytype,
) !void {
var cursor = placement;
const texture_width_height: f32 = @as(f32, @floatFromInt(self.atlas.size));
var i: usize = 0;
const right_codepoint_opt: ?u8 = null;
while (i < codepoints.len) : (i += 1) {
Expand All @@ -96,10 +95,10 @@ pub fn PenConfigInternal(comptime options: api.PenConfigOptionsInternal) type {
const glyph_metrics = self.font.glyphMetricsFromCodepoint(codepoint);
const glyph_texture_extent = self.textureExtentFromCodepoint(codepoint);
const texture_extent = types.Extent2DNative{
.x = @as(f32, @floatFromInt(glyph_texture_extent.x)) / texture_width_height,
.y = @as(f32, @floatFromInt(glyph_texture_extent.y)) / texture_width_height,
.width = @as(f32, @floatFromInt(glyph_texture_extent.width)) / texture_width_height,
.height = @as(f32, @floatFromInt(glyph_texture_extent.height)) / texture_width_height,
.x = @as(f32, @floatFromInt(glyph_texture_extent.x)),
.y = @as(f32, @floatFromInt(glyph_texture_extent.y)),
.width = @as(f32, @floatFromInt(glyph_texture_extent.width)),
.height = @as(f32, @floatFromInt(glyph_texture_extent.height)),
};

const screen_extent = types.Extent2DNative{
Expand All @@ -108,14 +107,14 @@ pub fn PenConfigInternal(comptime options: api.PenConfigOptionsInternal) type {
.width = @as(f32, @floatCast(@as(f64, @floatFromInt(glyph_texture_extent.width)) * screen_scale.horizontal)),
.height = @as(f32, @floatCast(@as(f64, @floatFromInt(glyph_texture_extent.height)) * screen_scale.vertical)),
};
try writer_interface.write(screen_extent, texture_extent);
const x_correction: f32 = writer_interface.write(screen_extent, texture_extent);
const advance_x: f64 = blk: {
if (right_codepoint_opt) |right_codepoint| {
break :blk self.font.kernPairAdvance(codepoint, right_codepoint) orelse glyph_metrics.advance_x;
}
break :blk glyph_metrics.advance_x;
};
cursor.x += @as(f32, @floatCast(advance_x * screen_scale.horizontal));
cursor.x += @as(f32, @floatCast(advance_x * screen_scale.horizontal)) + x_correction;
}
}

Expand All @@ -126,7 +125,6 @@ pub fn PenConfigInternal(comptime options: api.PenConfigOptionsInternal) type {
screen_scale: types.Scale2D,
writer_interface: anytype,
) !void {
const texture_width_height: f32 = @as(f32, @floatFromInt(self.atlas_ref.size));
var i: usize = 0;
const right_codepoint_opt: ?u8 = null;
var descent_max: f64 = 0;
Expand Down Expand Up @@ -187,10 +185,10 @@ pub fn PenConfigInternal(comptime options: api.PenConfigOptionsInternal) type {
const glyph_metrics = self.glyphMetricsFromCodepoint(codepoint);
const glyph_texture_extent = self.textureExtentFromCodepoint(codepoint);
const texture_extent = types.Extent2DNative{
.x = @as(f32, @floatFromInt(glyph_texture_extent.x)) / texture_width_height,
.y = @as(f32, @floatFromInt(glyph_texture_extent.y)) / texture_width_height,
.width = @as(f32, @floatFromInt(glyph_texture_extent.width)) / texture_width_height,
.height = @as(f32, @floatFromInt(glyph_texture_extent.height)) / texture_width_height,
.x = @as(f32, @floatFromInt(glyph_texture_extent.x)),
.y = @as(f32, @floatFromInt(glyph_texture_extent.y)),
.width = @as(f32, @floatFromInt(glyph_texture_extent.width)),
.height = @as(f32, @floatFromInt(glyph_texture_extent.height)),
};

const screen_extent = types.Extent2DNative{
Expand All @@ -199,14 +197,14 @@ pub fn PenConfigInternal(comptime options: api.PenConfigOptionsInternal) type {
.width = @as(f32, @floatCast(@as(f64, @floatFromInt(glyph_texture_extent.width)) * screen_scale.horizontal)),
.height = @as(f32, @floatCast(@as(f64, @floatFromInt(glyph_texture_extent.height)) * screen_scale.vertical)),
};
try writer_interface.write(screen_extent, texture_extent);
const x_correction: f32 = writer_interface.write(screen_extent, texture_extent);
const advance_x: f64 = blk: {
if (right_codepoint_opt) |right_codepoint| {
break :blk self.kernPairAdvance(codepoint, right_codepoint) orelse glyph_metrics.advance_x;
}
break :blk glyph_metrics.advance_x;
};
cursor.x += @as(f32, @floatCast(advance_x * screen_scale.horizontal));
cursor.x += @as(f32, @floatCast(advance_x * screen_scale.horizontal)) + x_correction;
}
}

Expand Down Expand Up @@ -262,6 +260,7 @@ pub fn FontConfig(comptime options: api.FontOptions) type {

return PenConfigInternal(.{
.type_overrides = .{
.Dimensions2DNative = options.type_overrides.Dimensions2DNative,
.Extent2DPixel = options.type_overrides.Extent2DPixel,
.Extent2DNative = options.type_overrides.Extent2DNative,
.Coordinates2DNative = options.type_overrides.Coordinates2DNative,
Expand Down
53 changes: 27 additions & 26 deletions src/backends/freetype.zig
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,23 @@ pub fn PenConfigInternal(comptime options: api.PenConfigOptionsInternal) type {
while (y < bitmap_height) : (y += 1) {
var x: usize = 0;
while (x < bitmap_width) : (x += 1) {
const value = @as(f32, @floatFromInt(bitmap_pixels[x + (y * bitmap_width)])) / 255;
const value: u8 = bitmap_pixels[x + (y * bitmap_width)];
const index: usize = (placement.x + x) + ((y + placement.y) * texture_size);
// TODO: Detect type using comptime
const use_transparency: bool = @hasField(types.Pixel, "a");
if (@hasField(types.Pixel, "r"))
texture_pixels[index].r = if (use_transparency) 0.8 else value;

if (@hasField(types.Pixel, "g"))
texture_pixels[index].g = if (use_transparency) 0.8 else value;

if (@hasField(types.Pixel, "b"))
texture_pixels[index].b = if (use_transparency) 0.8 else value;

if (use_transparency) {
texture_pixels[index].a = value;
switch (comptime options.pixel_format) {
.r8 => texture_pixels[index] = value,
.r8g8b8a8 => {
texture_pixels[index].r = 200;
texture_pixels[index].g = 200;
texture_pixels[index].b = 200;
texture_pixels[index].a = value;
},
.r32g32b32a32 => {
texture_pixels[index].r = 0.8;
texture_pixels[index].g = 0.8;
texture_pixels[index].b = 0.8;
texture_pixels[index].a = @as(f32, @floatFromInt(value)) / 255.0;
},
else => unreachable,
}
}
}
Expand All @@ -134,7 +136,6 @@ pub fn PenConfigInternal(comptime options: api.PenConfigOptionsInternal) type {
self.points_per_pixel,
);
var cursor = placement;
const texture_width_height: f32 = @as(f32, @floatFromInt(self.atlas_ref.size));
const face = self.backend_ref.face;

const has_kerning = face.face_flags.kerning;
Expand Down Expand Up @@ -179,18 +180,18 @@ pub fn PenConfigInternal(comptime options: api.PenConfigOptionsInternal) type {
if (codepoint != ' ') {
const glyph_texture_extent = self.textureExtentFromCodepoint(codepoint);
const texture_extent = types.Extent2DNative{
.x = @as(f32, @floatFromInt(glyph_texture_extent.x)) / texture_width_height,
.y = @as(f32, @floatFromInt(glyph_texture_extent.y)) / texture_width_height,
.width = @as(f32, @floatFromInt(glyph_texture_extent.width)) / texture_width_height,
.height = @as(f32, @floatFromInt(glyph_texture_extent.height)) / texture_width_height,
.x = @as(f32, @floatFromInt(glyph_texture_extent.x)),
.y = @as(f32, @floatFromInt(glyph_texture_extent.y)),
.width = @as(f32, @floatFromInt(glyph_texture_extent.width)),
.height = @as(f32, @floatFromInt(glyph_texture_extent.height)),
};
const screen_extent = types.Extent2DNative{
.x = @as(f32, @floatCast(cursor.x + (x_offset * screen_scale.horizontal))) + leftside_bearing,
.y = @as(f32, @floatCast(cursor.y + (y_offset * screen_scale.vertical))),
.width = @as(f32, @floatCast(@as(f64, @floatFromInt(glyph_texture_extent.width)) * screen_scale.horizontal)),
.height = @as(f32, @floatCast(@as(f64, @floatFromInt(glyph_texture_extent.height)) * screen_scale.vertical)),
};
try writer_interface.write(screen_extent, texture_extent);
cursor.x += writer_interface.write(screen_extent, texture_extent);
}
cursor.x += @as(f32, @floatCast(advance * screen_scale.horizontal));
previous_codepoint = codepoint;
Expand All @@ -211,7 +212,6 @@ pub fn PenConfigInternal(comptime options: api.PenConfigOptionsInternal) type {
self.points_per_pixel,
self.points_per_pixel,
);
const texture_width_height: f32 = @as(f32, @floatFromInt(self.atlas_ref.size));
const face = self.backend_ref.face;

const RenderedTextMetrics = struct {
Expand Down Expand Up @@ -302,18 +302,18 @@ pub fn PenConfigInternal(comptime options: api.PenConfigOptionsInternal) type {
if (codepoint != ' ') {
const glyph_texture_extent = self.textureExtentFromCodepoint(codepoint);
const texture_extent = types.Extent2DNative{
.x = @as(f32, @floatFromInt(glyph_texture_extent.x)) / texture_width_height,
.y = @as(f32, @floatFromInt(glyph_texture_extent.y)) / texture_width_height,
.width = @as(f32, @floatFromInt(glyph_texture_extent.width)) / texture_width_height,
.height = @as(f32, @floatFromInt(glyph_texture_extent.height)) / texture_width_height,
.x = @as(f32, @floatFromInt(glyph_texture_extent.x)),
.y = @as(f32, @floatFromInt(glyph_texture_extent.y)),
.width = @as(f32, @floatFromInt(glyph_texture_extent.width)),
.height = @as(f32, @floatFromInt(glyph_texture_extent.height)),
};
const screen_extent = types.Extent2DNative{
.x = @as(f32, @floatCast(cursor.x + (x_offset * screen_scale.horizontal))) + leftside_bearing,
.y = @as(f32, @floatCast(cursor.y + (y_offset * screen_scale.vertical))),
.width = @as(f32, @floatCast(@as(f64, @floatFromInt(glyph_texture_extent.width)) * screen_scale.horizontal)),
.height = @as(f32, @floatCast(@as(f64, @floatFromInt(glyph_texture_extent.height)) * screen_scale.vertical)),
};
try writer_interface.write(screen_extent, texture_extent);
cursor.x += writer_interface.write(screen_extent, texture_extent);
}
cursor.x += @as(f32, @floatCast(advance * screen_scale.horizontal));
previous_codepoint = codepoint;
Expand Down Expand Up @@ -359,6 +359,7 @@ pub fn FontConfig(comptime options: api.FontOptions) type {
.Coordinates2DNative = options.type_overrides.Coordinates2DNative,
.Scale2D = options.type_overrides.Scale2D,
.Pixel = PixelType,
.Dimensions2DNative = options.type_overrides.Dimensions2DNative,
},
.BackendType = FontType,
.pixel_format = pen_options.pixel_format,
Expand Down
Loading

0 comments on commit 2056086

Please sign in to comment.