使用数据 URI 进行 Base64 编码的 OpenType 字体

use*_*649 13 html css base64 font-face

我想添加base64 encodedOpenType 字体作为 font-face 中的数据 URI。

到目前为止我有这个:

@font-face {
    font-family: 'test';
    src: url(data:font/otf;base64,
        // Base64 string here ...
    ) format('opentype');
}
Run Code Online (Sandbox Code Playgroud)

但我相信它没有包含正确的符合我风格的字体。

对此的任何帮助将不胜感激。

Chr*_*ers 16

尽管没有询问,但我确信有人会来这里寻找 woff2 解决方案,并且会遇到与我相同的问题。使用此站点上传文件:https://base64.guru/converter/encode/file确保使用“数据 URI”选项。将 url() 值替换为站点中的代码。

对于 CSS,我必须使用以下代码:

@font-face {
    font-family: 'MYFONT'; /*This is what your font will be named*/
    src: local('MYFONT'), /* It will check the machine for a font matching this name, if it exists it will use this. Not needed when your are writing the woff2 file since you already sending all the data. */
         url('data:@file/octet-stream;base64,d09GMgABAAAAAK6kABIAAAABgQgAAK47AAA=') /*This is the base64 code copy pasted from the site*/
         format('woff2'); /*This ensures the browser treats it as a WOFF2 file. Notice there is no comma after the url() */
}
Run Code Online (Sandbox Code Playgroud)

要使自定义字体正常工作,您需要指定它:

<div style="font-family: MYFONT">
    My new font
</div>
Run Code Online (Sandbox Code Playgroud)

我发布的实际 Base64 代码不起作用。我使用的 woff2 文件大小为 50kb,并且不需要一页又一页的 Base64 代码。


Pmp*_*mpr 10

数据 URI 始终采用以下格式:

data:[<mediatype>][;base64],<data>
Run Code Online (Sandbox Code Playgroud)

每个数据 URI的第一部分是media-type,对于Open Type字体来说是:

font/opentype
Run Code Online (Sandbox Code Playgroud)

所以,你可以这样使用它:

@font-face{
    font-family: test;
    src: url(data:font/opentype; base64, [base64 string here]);
}
Run Code Online (Sandbox Code Playgroud)

更换

[base64 string here]
Run Code Online (Sandbox Code Playgroud)

使用 Base-64 编码字符串的精确复制粘贴版本。

笔记

请注意:

  • 你应该使用font/opentypedata而不是otf
  • 您应该复制粘贴确切的 base-64 编码字符串,不要进行任何更改,例如添加空格等(我相信其中有一些空格)

在线编码工具

您可以使用工具将字体文件转换为编码字符串。