Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom types for the meta table #1244

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Custom types for the meta table #1244

wants to merge 4 commits into from

Conversation

cmyr
Copy link
Member

@cmyr cmyr commented Nov 14, 2024

This is based on #1237; I think it makes sense to use custom types for the actual data in this table, but I wouldn't have been able to explain what to do without trying it first, so here we are.

(also this needs tests)

@cmyr cmyr force-pushed the more-meta-impl branch 2 times, most recently from 122c333 to c6096cf Compare November 14, 2024 19:53
This commit adds the code necessary to read and write the `meta` table,
including its codegen definition, manual extras to handle the variable
length data fields, and plumbing to surface it within `read-fonts` and
`write-fonts`.

Where possible, this commit borrows liberally from the implementation of
other tables, especially `name` (for its handling of variable length
data).

This is an early implementation, and as such requires manual and
automated testing. Additionally, there is an implementation of the
FontWrite trait for &[u8] that may be redundant.
This adds types and the required impls that can represent the expected
contents of the 'meta' table.

This has to be handwritten, because the 'meta' table contains different
payloads depending on the included tag for each record.

I think having these as real types will provide a significantly better
API though, and avoid us writing invalid data.

The actual implementations for these types are very spartan; eventually
I imagine having parsing & constructing code for the ScriptLangTag
struct that ensures it conforms to the spec.
}
};

let len: u32 = len.try_into().unwrap();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love the unwrap, is this common in write-fonts? Should file an issue to make write_into fallible and remove that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By design FontWrite is infallible; we do all the error checking in an earlier validation pass.

In generated code, we do add a check that an array's length can be represented by the type that stores the length, but I didn't bother hand-writing that here, given that any single font table's length must be representable in 32 bits or else its offset cannot be stored in the root table directory.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should comment as much and do an infallible production of len?

Copy link
Collaborator

@rsheeter rsheeter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM subject to tests to prove it works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants