javascript - 如何通过 webfont loader 获取字体 src url

标签 javascript phantomjs fabricjs webfonts webfont-loader

我使用 webfont-loader 通过 css 文件动态加载字体,代码如下

WebFont.load({
    custom: {
        families: 'Lobster',
        urls:'mydomain.com/lobster.css'
    }
}

在css文件中,它是这样的

@font-face {
    font-family: 'Lobster';
    src: url('Lobster_1.3-webfont.woff') format('woff'),
         url('Lobster_1.3-webfont.ttf') format('truetype');
    font-weight: normal;
    font-style: normal;
}

我想知道是否有办法获取字体 url,例如在 css 中定义的“Lobster_1.3-webfont.ttf”和“Lobster_1.3-webfont.woff”。

在 webfont-loader 中,没有这样的 api,如果没有直接的方法,我必须先将 css 作为文本文件加载,但我真的不想这样做

你们有什么线索吗?

PS:我需要字体 url 的原因是因为我使用 fabric.js 创建 svg 文件并通过节点端的 phantom.js 将其呈现为 PDF,但看起来 phantom.js 不支持带有 @ 的 svg导入 url('xxx.css')。所以我必须将 @font-face 与 src 一起使用。

最佳答案

好吧,假设您使用的是您选择的字体子集,用户未更新,因此您知道当前使用的字体的路径。 Fabricjs 有一个您可以填充的内部对象,称为:

fabric.fontPaths = { };

如果你在那里存储 url 信息:

fabric.fontPaths = {Lobster: 'Lobster_1.3-webfont.woff'};

您将在导出时将字体嵌入到 svg 中。 这应该允许您在 phantomjs 中正确加载 svg。

关于javascript - 如何通过 webfont loader 获取字体 src url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39071236/

相关文章:

node.js - React Nodejs - jsx 中的自定义 @font-face 用于 PDF 生成

javascript - 通过在 phantomjs 上运行 highcharts-convert.js 生成图像

javascript - 将每个对象的 X,Y 坐标获取到 Fabric.js 中的数组中

javascript - 从 MYSQL 到 jquery FLOT 的图表

javascript - 从复选框循环遍历 json 对象

javascript - 如何在 php 中动态使用多个 bootstrap collapse 元素

javascript - 按顺序打开 Angular 应用程序页面 - 第二个 page.open 回调不会被调用

jquery - 避免 Canvas fabricjs 的剪辑区域溢出

javascript - 如何使用 Fabric.js 免费画圆圈?

javascript - 有没有办法让 jQuery UI slider 从顶部而不是底部的 0 开始?