方案一:

使用css3  font-face,详细参照:http://www.w3cplus.com/content/css3-font-face

方案二:

文字转svg path,在html中使用svg标签绘制文字

String fontPath="D:/IdeaProjects/text2svg/fonts/站酷酷黑.TTF";
String text="我们";
try {ResultData  resultData = Text2SvgUtils.toConvert(fontPath, text);System.out.println(resultData);
} catch (Exception e) {e.printStackTrace();
}
创建Font用时:172
总用时:179ms
ResultData{fontFace=FontFace{fontFamily='HuXiaoBo_KuHei', unitsPerEm=1000, panose='2 1 6 0 3 1 1 1 1 1', ascent=859, descent=-141}, charGlyphList=[CharGlyph{source=我, glyphName='uni6211', unicode='我', horizAdvX=1000, arabicForm='', d='M36 212V319L245 338V432H31V517L63 500H245V558L64 555L35 538V635L491 644Q500 644 511 647T526 664V563L373 560V500H580L567 648H722L704 629L715 500H836L828 633L810 652H945L953 500H970V412Q965 425 955 428T935 432H720L732 286L944 339L967 369V255L740 199L746 124H936L966 141V57H729H727H619L609 166L547 151Q538 146 531 138T519 115V233L601 253L586 432H373V350L513 364L542 399V274L373 260V57H278H277H39V141L70 124H245V249L67 234L36 212Z', missing=false}, CharGlyph{source=们, glyphName='uni4EEC', unicode='们', horizAdvX=1000, arabicForm='', d='M957 55Q952 65 941 69T921 73H680V159L712 142H829V568H571V654L602 637H957V55ZM242 75H113V438H43L136 647L118 666H267L202 512H242V75ZM426 74H298V477L280 496H426V74ZM552 532H414L342 647L309 666H470L552 532Z', missing=false}]}

html中svg:

<div class="words" words="我们" oldwords="我们" webfontid="3"> <span><svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1000 1000" style="width: 24.06015037593985px;height: 24.06015037593985px; fill: currentColor; overflow:hidden;"><path d="M813 319L922 355V262L846 236L918 54H782L731 197L477 111V209L700 282L636 458H347V349L479 376V282L347 255V54H129L104 100H229V230L69 197V294L229 326V458H69V506H229V610L89 604V651L491 666L465 619L347 614V506H619L561 666H676L740 506H931L905 458H758L813 319ZM781 540L759 657H876L899 540H781Z" transform="translate(0, 860) scale(1, -1)"/></svg></span>  <span><svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1000 1000" style="width: 24.06015037593985px;height: 24.06015037593985px; fill: currentColor; overflow:hidden;"><path d="M313 668L208 451H255V53H139V404H62L194 668H313ZM823 645H937V55H685L670 99H823V645ZM474 53H338V501H474V53ZM492 526H367L341 663H466L492 526ZM823 645L797 597H511V645H823Z" transform="translate(0, 860) scale(1, -1)"/></svg></span> </div>

其中 transform="translate(0, 860) scale(1, -1)"  // 860 参照: ResultData中的ascent

JAVA代码:参照 https://github.com/ahdshao/text-to-svg

方案三:

方案二每次执行需要用时100+ms,用时过长。 解决用时问题,将文字path数据持久化到Redis(字体下的文字path几乎永久不会改变,只会新增)提供查询服务。

执行效率10ms以内,命中jvm 0ms以内

1、使用apach batik 工具将TTF文件转成SVG文件

java -jar batik-ttf2svg.jar C:\Users\zhaodahao\Desktop\字体包\站酷快乐体.ttf  -l 32 -h 65374 -o C:\Users\zhaodahao\Desktop\test\站酷快乐体.svg

2、使用SAX解析SVG文件

           log.info("开始解析svg文件:" + xmlPath);//获取SAX分析器的工厂实例,专门负责创建SAXParser分析器SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();//获取SAXParser分析器的实例SAXParser saxParser = saxParserFactory.newSAXParser();XmlSAXHandler saxHandler = new XmlSAXHandler();saxParser.parse(new File(xmlPath), saxHandler);log.info("解析 " + name + ".svg 用时:" + (System.currentTimeMillis() - start));log.info("WebIcon数量:" + saxHandler.getWebIcons().size());long ss = System.currentTimeMillis();// 清空缓存中的数据jimClient.del(RedisTypeEnum.FONT_DATA.getCode() + webFontId);// 保存字体path数据缓存batchAddWebIcon(webFontId, saxHandler.getWebIcons(), 10000);log.info("保存字体数据 用时:" + (System.currentTimeMillis() - ss));WebFont webFont=new WebFont();webFont.setId(webFontId);webFont.setDescName(name);webFont.setFontFace(saxHandler.getFontFace());//添加字体类型缓存jimClient.hSet(RedisTypeEnum.FONT_TYPE.getCode(),webFontId+"",gson.toJson(webFont));return true;
package com.zdh.xml.handler;import com.zdh.webfont.common.entity.WebIcon;
import com.zdh.webfont.common.util.UnicodeConverter;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;/*** 字体SVG解析器* Date: 16-10-25* Time: 上午10:56*/
public class XmlSAXHandler extends DefaultHandler {private List<WebIcon> webIcons = new ArrayList<WebIcon>();private Map<String,String> fontFace=new HashMap<String, String>();@Overridepublic void startDocument() throws SAXException {System.out.println("---->startDocument() is invoked...");}@Overridepublic void endDocument() throws SAXException {System.out.println("---->endDocument() is invoked...");}@Overridepublic void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {if (qName.equalsIgnoreCase("font-face")){for (int i=0;i<attributes.getLength();i++) {fontFace.put(attributes.getQName(i),attributes.getValue(i));}}if (qName.equalsIgnoreCase("glyph")){/* <glyph glyph-name="numbersign" unicode="#" horiz-adv-x="188" d="M23 121v26h36l11 54h31l-10 -54h30l10 54h32l-11 -54h29v-26h-34l-9 -49h30v-26h-35l-13 -67h-31l13 67h-30l-13 -67h-32l13 67h-30v26h35l10 49h-32zM86 121l-9 -49h30l9 49h-30z" />*/String code = attributes.getValue("unicode");if (code !=null){if (code.startsWith("&#x")){    //   code = òcode=code.substring(3,code.length()-1);}else{code= UnicodeConverter.charToUnicode(code.charAt(0));}WebIcon icon = new WebIcon();icon.setName(attributes.getValue("glyph-name"));icon.setUnicode(code);icon.setPath(attributes.getValue("d"));webIcons.add(icon);}}}public List<WebIcon> getWebIcons() {return webIcons;}public Map<String, String> getFontFace() {return fontFace;}
}

网页中嵌入任意web字体解决方案相关推荐

  1. woff, 在网页中嵌入任意字体的解决方案

    woff, 在网页中嵌入任意字体的解决方案 http://topic.csdn.net/u/20110705/10/98e79aba-1ddf-42fa-84a9-b79ec494cb69.html ...

  2. 在网页中嵌入任意字体(特殊字体/自定义字体)的解决方案

    在网页中嵌入任意字体(特殊字体/自定义字体)的解决方案 参考文章: (1)在网页中嵌入任意字体(特殊字体/自定义字体)的解决方案 (2)https://www.cnblogs.com/yuwenson ...

  3. 在网页中嵌入任意字体的解决方案---google在线字体库应用

    在网页中嵌入任意字体的解决方案---google在线字体库应用 参考文章: (1)在网页中嵌入任意字体的解决方案---google在线字体库应用 (2)https://www.cnblogs.com/ ...

  4. 在网页中嵌入任意字体的解决方案 (insert any font)

    字体使用是网页设计中不可或缺的一部分.经常地,我们希望在网页中使用某一特定字体,但是该字体并非主流操作系统的内置字体,这样用户在浏览页面的 时候就有可能看不到真实的设计.美工设计师最常做的办法是把想要 ...

  5. 如何在网页中嵌入自己想要的字体(实例下载)

    记得以前在国外那些人的网站看到那些字体怎么那么漂亮.而且不像电脑里面自带的字体.原来是通过这个方法来实现的.厉害,我觉得中文那些比较好看的字体,如果能都看到那该多好,不过看中文的TTF字体10M左右. ...

  6. 如何在网页中嵌入自己想要的字体(推荐阅读)

    字体使用是网页设计中不可或缺的一部分.网页是文字的载体,我们希望在网页中使用某一特定字体,但是该字体并非主流操作系统的内置字体,这样用户在浏览页面的时候就有可能看不到真实的设计. 美工设计师最常做的办 ...

  7. 网页中使用任意字体之实际操作

    之前对"在网页中嵌入非系统自带字体"做过一点研究,虽然技术上能实现,但是对国内来说,没有太大的实际意义,因为一个中文体文件起码20M+,而且各个浏览器支持的字体文件还都不一样,也就 ...

  8. flash 嵌入html代码,flash嵌入html在html网页代码中嵌入Flash文件的解决方案(下).doc...

    flash嵌入html在html网页代码中嵌入Flash文件的解决方案(下).doc flash嵌入 在 网页代码中嵌入Flash文件的解决方案(下) 在 代码中嵌入Flash文件一直都是广大web爱 ...

  9. 网页中嵌入flash的最佳方案

    http://solomon.athost.net/?p=549 网页中嵌入flash的最佳方案 各种浏览器差异悬殊,开发者应如何在网页中嵌入flash才是最佳方案?本文介绍的原理亦被应用在swfob ...

最新文章

  1. 中国互联网+政务建设发展现状及市场规模预测报告2022-2027年版
  2. Android消息处理机制(Handler、Looper、MessageQueue与Message)
  3. mysql 条件 函数_mysql 函数 时间函数,数学函数,字符串函数,条件判断函数
  4. Spring 处理请求和响应相关的注解
  5. 优化在深度学习中的挑战
  6. 阿里云邀您参加 2020 年数据湖高峰会议
  7. 3.30华为笔试第三题
  8. excel数据处理技巧笔记
  9. ad20drc错误_AD错误中英文对照
  10. LED显示屏工程招标常见控标十八绝招!
  11. GitHub哔哩哔哩(bilibili)高清视频下载
  12. 苹果6严重卡顿_苹果手机iOS系统: 如果开放系统降级通道会怎么样?
  13. https证书错误或者过期
  14. 知识:在遥远的海王星和天王星内部,居然会下钻石雨。
  15. beaglebone black下接nrf24l01与RFID标签的通信(基于EZSDK linux平台)
  16. web前端期末大作业 html+css+javascript网页设计实例 企业网站制作 (绿色植物网站设计)...
  17. 如何安装和设置Eufy Lumos Wi-Fi智能灯泡
  18. 一维数据二维化的办法汇总(一)
  19. 抓强势股MACD大牛启动通达信指标公式 无未来 不加密
  20. 2016012028 赵莉 散列函数的应用及其安全性

热门文章

  1. android连接wifi后移动数据,手机连接WiFi的时候,数据网络开着会耗费流量吗
  2. 那些三十五岁失业的安卓程序员,后来都干什么去了?
  3. EasyPoi导出Excel,完整代码+案例(100%能导出——导不出来砍我)
  4. 【数据结构学习】有关B树
  5. volatile 和 synchronized 详解
  6. 13种时间管理的方法
  7. 用实际项目分析高并发-抢霸王餐(川味观)
  8. 根据ReportPainter报表分配的事务代码(T-CODE)查找对应的报表组(GR51/GR52)报表库(GR21/GR22)报表(GRR1/GRR2)
  9. 单个方程的不动点迭代法
  10. word图片无法保存问题