tiny.filigree.font.metrics
Defined in font.
API (7)
Actions
Public operations.
Types and contracts
Public types and contracts.
Source
Source: lib/filigree/src/font/metrics.zig
zig
const std = @import("std");const binary = @import("binary.zig");const directory = @import("directory.zig");const model = @import("model.zig");const FontError = model.FontError;const Table = binary.Table;const readI16 = binary.readI16;const readU16 = binary.readU16;pub const VerticalMetrics = struct { table: ?Table, count: u16,};pub const Vorg = struct { table: Table, default_y: i16, record_count: u16, pub fn init(data: []const u8, vorg_table: Table) FontError!Vorg { if (vorg_table.len < 8) return error.InvalidFont; const major = try readU16(data, vorg_table.offset); const minor = try readU16(data, vorg_table.offset + 2); if (major != 1 or minor != 0) return error.InvalidFont; const record_count = try readU16(data, vorg_table.offset + 6); if (@as(u64, record_count) * 4 > @as(u64, vorg_table.len - 8)) return error.InvalidFont; const result = Vorg{ .table = vorg_table, .default_y = try readI16(data, vorg_table.offset + 4), .record_count = record_count, }; var previous: ?u16 = null; for (0..record_count) |record_index| { const glyph_id = try readU16(data, result.recordOffset(record_index)); if (previous) |previous_glyph| { if (glyph_id <= previous_glyph) return error.InvalidFont; } previous = glyph_id; } return result; } pub fn originY(self: Vorg, data: []const u8, glyph_id: u32) FontError!i16 { if (glyph_id > std.math.maxInt(u16)) return self.default_y; const wanted: u16 = @intCast(glyph_id); var left: usize = 0; var right: usize = self.record_count; while (left < right) { const mid = left + (right - left) / 2; const offset = self.recordOffset(mid); const current = try readU16(data, offset); if (wanted < current) { right = mid; } else if (wanted > current) { left = mid + 1; } else { return try readI16(data, offset + 2); } } return self.default_y; } fn recordOffset(self: Vorg, record_index: usize) usize { return self.table.offset + 8 + record_index * 4; }};pub const HorizontalMetrics = struct { ascender: i16, descender: i16, line_gap: i16,};pub fn verticalMetrics(data: []const u8, directory_offset: usize, num_glyphs: u16) VerticalMetrics { const vhea = directory.optionalAt(data, directory_offset, "vhea") orelse return .{ .table = null, .count = 0 }; const vmtx = directory.optionalAt(data, directory_offset, "vmtx") orelse return .{ .table = null, .count = 0 }; if (vhea.len < 36) return .{ .table = null, .count = 0 }; const count = readU16(data, vhea.offset + 34) catch return .{ .table = null, .count = 0 }; if (count == 0 or count > num_glyphs) return .{ .table = null, .count = 0 }; if (@as(usize, count) > vmtx.len / 4) return .{ .table = null, .count = 0 }; return .{ .table = vmtx, .count = count };}pub fn horizontalTypoMetrics(data: []const u8, directory_offset: usize) ?HorizontalMetrics { const os2 = directory.optionalAt(data, directory_offset, "OS/2") orelse return null; if (os2.len < 74) return null; return .{ .ascender = readI16(data, os2.offset + 68) catch return null, .descender = readI16(data, os2.offset + 70) catch return null, .line_gap = readI16(data, os2.offset + 72) catch return null, };}Source: lib/filigree/src/font/root.zig:13
zig
pub const metrics = @import("metrics.zig");Audit
| Definitions | 8 |
|---|---|
| Public names | 8 |
| Members | 8 |
| Version | 26.7.0 |
| Revision | daab053ee433 |