Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

vue大屏字体问题

大家都知道,大屏展示为了更好看,往往都会引入特殊字体文件,但涉及到汉子字体的话,字体文件会比较大。网上说的通过spider压缩字体,但并不能动态压缩,所以想问下大家都是怎么处理字体的?
比如字体文件:思源黑体


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

可以服务端跑一个接口,接受汉字,返回从字体包中拆出的字体.
参考我这个DEMO

浏览器端:

@font-face {
    font-family: "my-font";
    src: url('/font?str=天地玄黄宇宙洪荒') format('truetype');
}
<p style="font-family: 'my-font'">天地玄黄 宇宙洪荒</p>

服务端(nodejs)

// route.js
var express = require("express");
var router = express.Router();
const font = require("../src/font");

/* GET home page. */
router.get("/", function (req, res, next) {
  res.render("index", {title: "Express"});
});

router.get("/font", async function (req, res, next) {
  if (!req.query.str) {
    res.send("str is required!");
    return;
  }
  const result = await font.format(req.query.str);
  if (!result.success) {
    res.send("format failed!");
    return;
  }
  res.send(result.data);
});

module.exports = router;
// font.js
const Fontmin = require("fontmin");
const format = (text) => {
  return new Promise(resolve => {
    const fontmin = new Fontmin()
      .src("assets/font.ttf")
      .use(Fontmin.glyph({
        text,
        hinting: false,
      }));

    fontmin.run((err, data) => {
      if (err) {
        console.log("error", err);
        resolve({success: false, data: err});
        return;
      }
      resolve({success: true, data: data[0]._contents});
    });
  });
};


module.exports = {
  format
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...