Skip to content

Commit 9db3d78

Browse files
committed
stash code
1 parent 74d0903 commit 9db3d78

File tree

2 files changed

+53
-48
lines changed

2 files changed

+53
-48
lines changed

src/lib2.zig

Lines changed: 52 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,11 @@ const Reader = struct {
200200
return gid;
201201
}
202202

203+
pub fn get_glyph_info_by_glyph_id(self: *Self, gid: u16) !Glyph {
204+
_ = self; // autofix
205+
_ = gid; // autofix
206+
}
207+
203208
pub fn get_glyph_info(self: *Self, code_point: u32) !Glyph {
204209
if (self.glyph_cache.get(code_point)) |glyph| {
205210
if (!glyph.is_empty()) {
@@ -289,6 +294,7 @@ const Subsetter = struct {
289294
}
290295
}
291296
try required_glyphs.put(0, Glyph{});
297+
292298
try self.collect_glyph_ids_recursive(&required_glyphs);
293299
var glyph_ids = try self.allocator.alloc(u16, required_glyphs.count());
294300
defer self.allocator.free(glyph_ids);
@@ -299,6 +305,8 @@ const Subsetter = struct {
299305
i += 1;
300306
}
301307
std.sort.heap(u16, glyph_ids, {}, std.sort.asc(u16));
308+
const b = try self.build_post_table(glyph_ids);
309+
defer self.allocator.free(b);
302310
}
303311

304312
fn collect_glyph_ids_recursive(self: *Self, required_glyphs: *AutoHashMap(u16, Glyph)) !void {
@@ -339,22 +347,15 @@ const Subsetter = struct {
339347
}
340348
}
341349

342-
fn build_name_table(self: *Self) []const u8 {
343-
var name_pos: usize = 0;
344-
345-
for (self.t.parser.table_records.items, 0..) |record, i| {
346-
if (record.tag == .name) {
347-
name_pos = i;
348-
break;
349-
}
350-
}
351-
352-
const name_table_offset = self.parser.table_records.items[name_pos].offset;
353-
const name_table_size = self.t.parser.table_records.items[name_pos].length;
354-
355-
const name_table = self.t.parser.buffer[name_table_offset .. name_table_offset + name_table_size];
350+
fn get_default_binary_data(self: *Self, tag: parser.TableTag, end_position: ?usize) []const u8 {
351+
const record = self.t.parser.find_table_record(tag).?;
352+
const len = if (end_position) |pos| record.offset + pos else record.offset + record.length;
353+
const table_data = self.t.parser.buffer[record.offset..len];
354+
return table_data;
355+
}
356356

357-
return name_table;
357+
fn build_name_table(self: *Self) []const u8 {
358+
return self.get_default_binary_data(.name, null);
358359
}
359360

360361
fn build_post_table(self: *Self, glyph_ids: []u16) ![]u8 {
@@ -365,40 +366,42 @@ const Subsetter = struct {
365366

366367
errdefer buffer.deinit();
367368

368-
try buffer.write(u32, post.version, .big);
369-
try buffer.write(i32, post.italic_angle, .big);
370-
try buffer.write(i16, post.underline_position, .big);
371-
try buffer.write(i16, post.underline_thickness, .big);
372-
try buffer.write(u32, post.is_fixed_pitch, .big);
373-
try buffer.write(u32, post.min_mem_type42, .big);
374-
try buffer.write(u32, post.max_mem_type42, .big);
375-
try buffer.write(u32, post.min_mem_type1, .big);
376-
try buffer.write(u32, post.max_mem_type1, .big);
369+
// const table_data = self.get_default_binary_data(.post, 32);
370+
371+
// try buffer.write_bytes(table_data);
377372

378373
if (post.v2_data) |_| {
379374
try buffer.write(u16, @intCast(glyph_ids.len), .big);
380375

381-
var has_custom_names = false;
382-
for (glyph_ids) |glyph_id| {
383-
if (post.get_glyph_index(glyph_id)) |glyph_index| {
384-
try buffer.write(u16, glyph_index, .big);
385-
if (glyph_index >= 258) {
386-
has_custom_names = true;
387-
}
388-
}
389-
}
390-
if (has_custom_names) {
391-
for (glyph_ids) |glyph_id| {
392-
if (post.get_glyph_index(glyph_id)) |glyph_index| {
393-
if (glyph_index >= 258) {
394-
if (post.get_glyph_name(glyph_id)) |glyph_name| {
395-
try buffer.write_u8(@intCast(glyph_name.len));
396-
try buffer.write_bytes(glyph_name);
397-
}
398-
}
399-
}
400-
}
401-
}
376+
// var has_custom_names = false;
377+
// for (glyph_ids) |glyph_id| {
378+
// if (post.get_glyph_index(glyph_id)) |glyph_index| {
379+
// try buffer.write(u16, glyph_index, .big);
380+
// if (glyph_index >= 258) {
381+
// has_custom_names = true;
382+
// }
383+
// }
384+
// }
385+
// if (has_custom_names) {
386+
// for (glyph_ids) |glyph_id| {
387+
// if (post.get_glyph_index(glyph_id)) |glyph_index| {
388+
// if (glyph_index >= 258) {
389+
// if (post.get_glyph_name(glyph_id)) |glyph_name| {
390+
// try buffer.write_u8(@intCast(glyph_name.len));
391+
// try buffer.write_bytes(glyph_name);
392+
// }
393+
// }
394+
// }
395+
// }
396+
// }
397+
// var has_custom_names = false;
398+
// _ = has_custom_names; // autofix
399+
400+
// for (glyph_ids) |glyph_id| {
401+
// _ = glyph_id; // autofix
402+
// //
403+
// }
404+
// std.debug.print("{any}\n", .{post.v2_data.?});
402405
}
403406

404407
return buffer.to_owned_slice();
@@ -408,14 +411,16 @@ const Subsetter = struct {
408411
test "ttf.zig" {
409412
const fs = std.fs;
410413
const allocator = std.testing.allocator;
411-
const font_file_path = fs.path.join(allocator, &.{ "./", "fonts", "LXGWBright-Light.ttf" }) catch unreachable;
414+
const font_file_path = fs.path.join(allocator, &.{ "./", "fonts", "Caveat-VariableFont_wght.ttf" }) catch unreachable;
412415
defer allocator.free(font_file_path);
413416
const file_content = try fs.cwd().readFileAlloc(allocator, font_file_path, std.math.maxInt(usize));
414417
defer allocator.free(file_content);
415418
var font = try ttf.init(allocator, file_content);
416419
defer font.deinit();
417420
var subbsetter = try font.subsetter();
418-
try subbsetter.build_subset(BuildSubsetterOptions{ .input_text = "绪方理奈" });
421+
const input_text = &[_]u8{ 0xC5, 0x84 };
422+
std.debug.print("{s}\n", .{input_text});
423+
try subbsetter.build_subset(BuildSubsetterOptions{ .input_text = input_text });
419424
// var reader = try font.reader();
420425
// const code_point: u32 = 'a';
421426
// const e = try reader.get_glyph_info(code_point);

src/parser.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ pub const Parser = struct {
304304
self.parsed_flags |= tag_bit;
305305
}
306306

307-
inline fn find_table_record(self: *Self, tag: TableTag) ?TableRecord {
307+
pub inline fn find_table_record(self: *Self, tag: TableTag) ?TableRecord {
308308
for (self.table_records.items) |record| {
309309
if (record.tag == tag) return record;
310310
}

0 commit comments

Comments
 (0)