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

Add support of base64 fonts #740

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

vladimir-g
Copy link

This pull request updates parsing of base64-encoded data URI and adds support of base64 TTF fonts. This may close #153 and #682.

Data URI parsing

Current implementation of data URI parser is too simple and doesn't allow mediatype to contain additional params, like charset or such. RFC 2397 says:

The URLs are of the form:
data:[<mediatype>][;base64],<data>
...
mediatype  := [ type "/" subtype ] *( ";" parameter )

Most solutions from web recommend using URI format like data:font/ttf;charset=utf8;base64,etc for fonts. I don't really know how these additional params must be used, but I guess it is better to allow parser to work with them properly. Current regex-based parser breaks on them. New implementation just stores them inside wrapper object for further use.

I've decided to drop regex in favor of simpler parsing solution because it isn't easy to get repeated capture groups in one go. Proper regex would also be not very readable I think.

Fonts support

Fonts in base64 actually could've been supported even without changing data URI parsing. When font-family CSS rule contains src with data URI it's already properly parsed by current implementation and B64InlineURI file-like object is created. Only thing that prevents fonts from being used is font-adding code that expects real file and checks file extensions.

Proposed implementation checks that font file is a B64InlineURI class and uses parsed mime type to detect font type.

One issue that remains is that only TTF fonts are supported in base64 form (using font/ttf mimetype). Support of afm and pfb doesn't work without heavy modification of code. Current implementation expects both afm and pfb to be located somewhere with same basename, and this couldn't happen when font is provided in base64 form in single URL.

RFC 2397 allows additional parameters inside mediatype part, separated
by semicolons. Previous implementation fails to parse URI completely
when these parameters are encountered.
Now fonts can be used inside CSS @font-face declaration in
base64-endoded form with 'font/ttf' mimetype.
Example: "src:url('data:font/ttf;charset=utf-8;base64,B64DATA');"
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.

Unable to render Base64 CSS Font.
1 participant