freetype: Avoid dereferencing null bitmap buffer.
For some characters (eg \r) the returned glyph will have .{.rows = 0, .width=0, .pitch = 0, .buffer = null}. In zig null[0..0] causes a panic rather than returning an empty slice.
This commit is contained in:
parent
86053d9969
commit
15c71f5135
2 changed files with 8 additions and 3 deletions
|
|
@ -21,7 +21,7 @@ pub fn main() !void {
|
||||||
while (i < bitmap.rows()) : (i += 1) {
|
while (i < bitmap.rows()) : (i += 1) {
|
||||||
var j: usize = 0;
|
var j: usize = 0;
|
||||||
while (j < bitmap.width()) : (j += 1) {
|
while (j < bitmap.width()) : (j += 1) {
|
||||||
const char: u8 = switch (bitmap.buffer()[i * bitmap.width() + j]) {
|
const char: u8 = switch (bitmap.buffer().?[i * bitmap.width() + j]) {
|
||||||
0 => ' ',
|
0 => ' ',
|
||||||
1...128 => ';',
|
1...128 => ';',
|
||||||
else => '#',
|
else => '#',
|
||||||
|
|
|
||||||
|
|
@ -45,8 +45,13 @@ pub const Bitmap = struct {
|
||||||
return @intToEnum(PixelMode, self.handle.pixel_mode);
|
return @intToEnum(PixelMode, self.handle.pixel_mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn buffer(self: Bitmap) []const u8 {
|
pub fn buffer(self: Bitmap) ?[]const u8 {
|
||||||
const buffer_size = std.math.absCast(self.pitch()) * self.rows();
|
const buffer_size = std.math.absCast(self.pitch()) * self.rows();
|
||||||
return self.handle.buffer[0..buffer_size];
|
return if (self.handle.buffer == null)
|
||||||
|
// freetype returns a null pointer for zero-length allocations
|
||||||
|
// https://github.com/hexops/freetype/blob/bbd80a52b7b749140ec87d24b6c767c5063be356/freetype/src/base/ftutil.c#L135
|
||||||
|
null
|
||||||
|
else
|
||||||
|
self.handle.buffer[0..buffer_size];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue