使用数据 URI 的 Base64 编码 OpenType 字体
Base64 encoded OpenType font-face using data URI
我想在 font-face 中添加 base64 encoded
OpenType 字体作为数据 URI。
到目前为止我有这个:
@font-face {
font-family: 'test';
src: url(data:font/otf;base64,
// Base64 string here ...
) format('opentype');
}
但我认为它没有按照我的风格正确包含字体。
如有任何帮助,我们将不胜感激。
数据 URI 总是这种格式:
data:[<mediatype>][;base64],<data>
每个 data URI 的第一部分是 media-type
,在 Open Type
字体的情况下是:
font/opentype
所以,你可以这样使用它:
@font-face{
font-family: test;
src: url(data:font/opentype; base64, [base64 string here]);
}
正在替换
[base64 string here]
使用 base-64 编码字符串的精确复制粘贴版本。
备注
请注意:
- 您应该使用
font/opentype
作为 data
,而不是 otf
。
- 您应该复制粘贴准确的 base-64 编码字符串,不要添加空格等任何更改(我相信其中有一些空格)
在线编码工具
您可以使用 this 工具,将您的字体文件转换为编码字符串。
虽然没有被问到,但我确信有人会来这里寻找 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() */
}
要使您的自定义字体正常工作,您需要指定它:
<div style="font-family: MYFONT">
My new font
</div>
我发布的实际 base64 代码不起作用。我使用的 woff2 文件是 50kb,不需要一页又一页的 base64 代码。
我想在 font-face 中添加 base64 encoded
OpenType 字体作为数据 URI。
到目前为止我有这个:
@font-face {
font-family: 'test';
src: url(data:font/otf;base64,
// Base64 string here ...
) format('opentype');
}
但我认为它没有按照我的风格正确包含字体。
如有任何帮助,我们将不胜感激。
数据 URI 总是这种格式:
data:[<mediatype>][;base64],<data>
每个 data URI 的第一部分是 media-type
,在 Open Type
字体的情况下是:
font/opentype
所以,你可以这样使用它:
@font-face{
font-family: test;
src: url(data:font/opentype; base64, [base64 string here]);
}
正在替换
[base64 string here]
使用 base-64 编码字符串的精确复制粘贴版本。
备注
请注意:
- 您应该使用
font/opentype
作为data
,而不是otf
。 - 您应该复制粘贴准确的 base-64 编码字符串,不要添加空格等任何更改(我相信其中有一些空格)
在线编码工具
您可以使用 this 工具,将您的字体文件转换为编码字符串。
虽然没有被问到,但我确信有人会来这里寻找 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() */
}
要使您的自定义字体正常工作,您需要指定它:
<div style="font-family: MYFONT">
My new font
</div>
我发布的实际 base64 代码不起作用。我使用的 woff2 文件是 50kb,不需要一页又一页的 base64 代码。