font/freetype: avoid overflows with u8 font size

This commit is contained in:
Mitchell Hashimoto
2024-03-27 20:45:55 -07:00
parent 461b16ce34
commit 552c97eea4
2 changed files with 3 additions and 2 deletions

View File

@ -40,7 +40,8 @@ pub const DesiredSize = struct {
// Converts points to pixels // Converts points to pixels
pub fn pixels(self: DesiredSize) u16 { pub fn pixels(self: DesiredSize) u16 {
// 1 point = 1/72 inch // 1 point = 1/72 inch
return (self.points * self.ydpi) / 72; const points_u16: u16 = @intCast(self.points);
return (points_u16 * self.ydpi) / 72;
} }
}; };

View File

@ -137,7 +137,7 @@ pub const Face = struct {
// to what the user requested. Otherwise, we can choose an arbitrary // to what the user requested. Otherwise, we can choose an arbitrary
// pixel size. // pixel size.
if (face.isScalable()) { if (face.isScalable()) {
const size_26dot6 = @as(i32, @intCast(size.points << 6)); // mult by 64 const size_26dot6 = @as(i32, @intCast(size.points)) << 6; // mult by 64
try face.setCharSize(0, size_26dot6, size.xdpi, size.ydpi); try face.setCharSize(0, size_26dot6, size.xdpi, size.ydpi);
} else try selectSizeNearest(face, size.pixels()); } else try selectSizeNearest(face, size.pixels());
} }