font: CodepointMap supports clone

This commit is contained in:
Mitchell Hashimoto
2024-04-07 10:54:59 -07:00
parent 06df9b7867
commit 21a648748d
3 changed files with 28 additions and 5 deletions

View File

@ -2965,11 +2965,7 @@ pub const RepeatableCodepointMap = struct {
/// Deep copy of the struct. Required by Config. /// Deep copy of the struct. Required by Config.
pub fn clone(self: *const Self, alloc: Allocator) !Self { pub fn clone(self: *const Self, alloc: Allocator) !Self {
// TODO(fontmem): clone the codemap descriptors return .{ .map = try self.map.clone(alloc) };
return .{
.map = .{ .list = try self.map.list.clone(alloc) },
};
} }
/// Compare if two of our value are requal. Required by Config. /// Compare if two of our value are requal. Required by Config.

View File

@ -30,6 +30,18 @@ pub fn deinit(self: *CodepointMap, alloc: Allocator) void {
self.list.deinit(alloc); self.list.deinit(alloc);
} }
/// Deep copy of the struct. The given allocator is expected to
/// be an arena allocator of some sort since the struct itself
/// doesn't support fine-grained deallocation of fields.
pub fn clone(self: *const CodepointMap, alloc: Allocator) !CodepointMap {
var list = try self.list.clone(alloc);
for (list.items(.descriptor)) |*d| {
d.* = try d.clone(alloc);
}
return .{ .list = list };
}
/// Add an entry to the map. /// Add an entry to the map.
/// ///
/// For conflicting codepoints, entries added later take priority over /// For conflicting codepoints, entries added later take priority over

View File

@ -86,6 +86,21 @@ pub const Descriptor = struct {
return hasher.final(); return hasher.final();
} }
/// Deep copy of the struct. The given allocator is expected to
/// be an arena allocator of some sort since the descriptor
/// itself doesn't support fine-grained deallocation of fields.
pub fn clone(self: *const Descriptor, alloc: Allocator) !Descriptor {
// We can't do any errdefer cleanup in here. As documented we
// expect the allocator to be an arena so any errors should be
// cleaned up somewhere else.
var copy = self.*;
copy.family = if (self.family) |src| try alloc.dupeZ(u8, src) else null;
copy.style = if (self.style) |src| try alloc.dupeZ(u8, src) else null;
copy.variations = try alloc.dupe(Variation, self.variations);
return copy;
}
/// Convert to Fontconfig pattern to use for lookup. The pattern does /// Convert to Fontconfig pattern to use for lookup. The pattern does
/// not have defaults filled/substituted (Fontconfig thing) so callers /// not have defaults filled/substituted (Fontconfig thing) so callers
/// must still do this. /// must still do this.