要了解ttf字体文件的原理更方便对代码的理解
package com.maoyan.movie.ttf.encode;public class PostTableHeader {public long format; public long italicAngle;public int  underlinePosition;public int  underlineThichness;public long  isFixedPitch;public long  minMemType42;public long  maxMemType42;public long  minMemType1;public long  maxMemType1;}package com.maoyan.movie.ttf.encode;public class TableDirectory {public String name; //table namepublic  int checkSum; //Check sumpublic int offset; //Offset from beginning of filepublic int length; //length of the table in bytes
}package com.maoyan.movie.ttf.encode;import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;/*** @author 作者 :朱海川 ZH519080@163.com* @version 创建时间:2016年10月26日 下午10:29:55 类说明*/
public class TTFCodeParse {public TTFCodeParse() {// TODO Auto-generated constructor stub}public static int COPYRIGHT = 0;public static int FAMILY_NAME = 1;public static int FONT_SUBFAMILY_NAME = 2;public static int UNIQUE_FONT_IDENTIFIER = 3;public static int FULL_FONT_NAME = 4;public static int VERSION = 5;public static int POSTSCRIPT_NAME = 6;public static int TRADEMARK = 7;public static int MANUFACTURER = 8;public static int DESIGNER = 9;public static int DESCRIPTION = 10;public static int URL_VENDOR = 11;public static int URL_DESIGNER = 12;public static int LICENSE_DESCRIPTION = 13;public static int LICENSE_INFO_URL = 14;public String uni = "";private Map<Integer, String> fontProperties = new HashMap<Integer, String>();public String getFontName() {if (fontProperties.containsKey(FULL_FONT_NAME)) {return fontProperties.get(FULL_FONT_NAME);} else if (fontProperties.containsKey(FAMILY_NAME)) {return fontProperties.get(FAMILY_NAME);} else {return null;}}public String getFontPropertie(int nameID) {if (fontProperties.containsKey(nameID)) {return fontProperties.get(nameID);} else {return null;}}public Map<Integer, String> getFontProperties() {return fontProperties;}public String parseInner(String fileName) throws IOException {RandomAccessFile randomAccessFile = new RandomAccessFile(fileName, "r");ArrayList<String> arrayList = new ArrayList<String>();// Attribute attribute = new Attribute();int majorVersion = randomAccessFile.readShort();int minorVersion = randomAccessFile.readShort();int numOfTables = randomAccessFile.readShort();// if (majorVersion != 1 || minorVersion != 0) {// return ;// }// jump to TableDirectory structrandomAccessFile.seek(12);boolean found = false;byte[] buff = new byte[4];TableDirectory tableDirectory = new TableDirectory();for (int i = 0; i < numOfTables; i++) {randomAccessFile.read(buff);tableDirectory.name = new String(buff);tableDirectory.checkSum = randomAccessFile.readInt();tableDirectory.offset = randomAccessFile.readInt();tableDirectory.length = randomAccessFile.readInt();// System.out.println("*******************" + tableDirectory.name);if ("post".equalsIgnoreCase(tableDirectory.name)) {found = true;// System.out.println("talbe: post found!");// break;} else if (tableDirectory.name == null || tableDirectory.name.length() == 0) {break;}}// not found table of name//// if (!found) {// return;// }randomAccessFile.seek(tableDirectory.offset);if (found) {PostTableHeader postTableHeader = new PostTableHeader();postTableHeader.format = ttfGetFixed(randomAccessFile);postTableHeader.italicAngle = ttfGetFixed(randomAccessFile);postTableHeader.underlinePosition = ttfGetSHORT(randomAccessFile);postTableHeader.underlineThichness = ttfGetSHORT(randomAccessFile);postTableHeader.isFixedPitch = ttfGetLONG(randomAccessFile);postTableHeader.minMemType42 = ttfGetLONG(randomAccessFile);postTableHeader.maxMemType42 = ttfGetLONG(randomAccessFile);postTableHeader.minMemType1 = ttfGetLONG(randomAccessFile);postTableHeader.maxMemType1 = ttfGetLONG(randomAccessFile);if (postTableHeader.format == 0x00020000) {int numGlyphs = ttfGetSHORT(randomAccessFile);int[] glyphNameIndex = new int[numGlyphs];for (int i = 0; i < numGlyphs; i++) {glyphNameIndex[i] = ttfGetSHORT(randomAccessFile);}// long pos = randomAccessFile.getFilePointer();// randomAccessFile.seek(pos + 1);randomAccessFile.skipBytes(1);for (int i = 0; i < numGlyphs; i++) {if (glyphNameIndex[i] <= 257) {/* do nothing for standard Mac glyf name */} else if (glyphNameIndex[i] <= 32767) {/** non-standard glyf name is stored as a Pascal string* in the file i.e. the first byte is the length of the* string but the string is not ended with a null* character*/int len = ttfGetCHAR(randomAccessFile);byte[] bf = new byte[len];if (len > 0)randomAccessFile.read(bf);String uniCoding = new String(bf, Charset.forName("utf-8"));arrayList.add(uniCoding);}}}}// arrayList.toString().substring(4, 92).replaceAll(" ", "");// System.out.println(arrayList.toString().substring(4, 92).replaceAll("// ", ""));String maoyanencode = arrayList.toString().substring(4, 92).replaceAll(" ", "").replaceAll("uni", "").toLowerCase();return maoyanencode;}@Overridepublic String toString() {// TODO Auto-generated method stubreturn fontProperties.toString();}public static int ttfGetCHAR(RandomAccessFile file) {int cc = 0;try {cc = file.readUnsignedByte();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return cc;}public static int ttfGetSHORT(RandomAccessFile file) {int cc = 0;try {cc = file.readUnsignedByte() << 8;cc |= file.readUnsignedByte();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return (int) cc;}public static long ttfGetLONG(RandomAccessFile file) {int cc = 0;try {cc = file.readUnsignedByte() << 24;cc |= file.readUnsignedByte() << 16;cc |= file.readUnsignedByte() << 8;cc |= file.readUnsignedByte();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return (long) cc;}public static long ttfGetFixed(RandomAccessFile file) {return (long) ttfGetLONG(file);}public void FixedSplit(long f, long b[]) {b[0] = f & 0xff00;b[1] = f >> 16;}}package com.maoyan.movie.ttf.encode;import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;/*** @author 作者 E-mail: ZH519080@163.com* @version 创建时间:2016年11月10日 下午11:01:39 类说明 :*/
public class DownParseTTF {//解析ttf字体文件public String parseTTF(String htmlsource) {TTFCodeParse ttfCodeParse = new TTFCodeParse();//解析电影的源码html文件Document document = Jsoup.parse(htmlsource);Elements headStyle = document.select("head style");Document headStyleDocu = Jsoup.parse(headStyle.html());String ttfFigureCode = "";if (!headStyleDocu.body().text().contains(".ttf")) {return "没有ttf文件解析";}else {//获取ttf字体文件下载链接String ttfURL = "http://" + headStyleDocu.body().text().substring(244, 310);//获取ttf字体文件的名称String ttfName = ttfURL.substring(33, ttfURL.length());//本地存放地址String savePath = "F:\\ttf\\";try {URL url = new URL(ttfURL);// 获取url链接反应HttpURLConnection conn = (HttpURLConnection) url.openConnection();// 设置超时间为10秒conn.setConnectTimeout(10 * 1000);// 防止屏蔽程序抓取而返回403错误conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0");// 得到输入流InputStream inputStream = conn.getInputStream();// 获取自己数组byte[] getData = readInputStream(inputStream);// 文件保存位置File saveDir = new File(savePath);if (!saveDir.exists()) {saveDir.mkdir();}File file = new File(saveDir + File.separator + ttfName);FileOutputStream fos = new FileOutputStream(file);fos.write(getData);//得到ttf字的编码,猫眼电影中的ttf文件是对0到9的解析编码,找到其一一对应的顺序ttfFigureCode = ttfCodeParse.parseInner(file.toString());if (fos != null) {fos.close();}if (inputStream != null) {inputStream.close();}// return code;} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return ttfFigureCode;}//        return ttfFigureCode;}public static byte[] readInputStream(InputStream inputStream) throws IOException {byte[] buffer = new byte[1024];int len = 0;ByteArrayOutputStream bos = new ByteArrayOutputStream();while ((len = inputStream.read(buffer)) != -1) {bos.write(buffer, 0, len);}bos.close();return bos.toByteArray();}}

java 解析ttf字体文件相关推荐

  1. ttf文件 python 打开_[译]JS解析TTF字体文件

    把字体拖到下边的方框,获取其中的奥妙!点此获取示例ttf字体文件. TTF文件拖到这里 在这篇文章,我们计划操作如下: 将字体文件拖入网页,并读取之 尽管ttf文件是为C语言读取设计的,但我们仍试图解 ...

  2. LVGL学习之路——基于lv_lib_freetype库的TTF字体文件动态加载中文字体(阿里普惠字体)

    前言   在学习lvgl中,在英文字体上很多人都用过,但是中文字体往往需要靠取模去实现.那么我就在想,如何像windows那样加载动态的字体呢,这样想做多大字体都行.于是就开始了字体的移植. 什么是t ...

  3. IDEA Java解析GeoJson.json文件

    IDEA Java解析GeoJson.json文件 一.遇到的问题 1. 无法导入成功 2. org.geotools.StyleFactory is not an ImageIO SPI class ...

  4. WPF解析TTF 字体

    偶遇需要自己解析 TTF 字体并显示,此做... using System; using System.Collections.Generic; using System.Drawing.Text; ...

  5. 【Flutter】Icons 组件 ( FlutterIcon 下载图标 | 自定义 svg 图标生成 ttf 字体文件 | 使用下载的 ttf 图标文件 )

    文章目录 一.FlutterIcon 下载图标 二.自定义 svg 图标生成 ttf 字体文件 三.使用下载的 ttf 图标文件 四.完整代码示例 五.相关资源 一.FlutterIcon 下载图标 ...

  6. 如何编辑ttf字体文件

    libfreetype的目标是以最小的内存最快的速度,读取和渲染字体.因此libfreetype并不适宜用来编辑ttf字体文件. 编辑字体文件,可以用FontCreator.微软fonttools.f ...

  7. .ttf字体文件引入vue项目及使用

    出自文章:.ttf字体文件引入vue项目及使用 UED要求使用非系统自带字体 1.在项目的assets文件夹中创建fonts文件夹,将下载好的.ttf字体文件放进去,并创建font.css文件 2.在 ...

  8. win7使用android字体文件,处置win7系统安装TTF字体文件的详细步骤

    技术小编发觉操作系统在使用途中会有win7系统安装TTF字体文件的问题,但是很多朋友还不了解win7系统安装TTF字体文件的情况该怎么操作,今天我就来将win7系统安装TTF字体文件的方法分享给你们, ...

  9. css如何引用.ttf字体文件|@font-face

    这里复习一下如何在css文件里引用.ttf字体文件. 如下图,红色圈起来的标题需要使用productsans字体,绿色圈起来的小标题需要使用roboto字体,字体文件已下载到fonts文件夹里,接下来 ...

最新文章

  1. 在读博士的第八年,她破解了量子计算领域最基本的问题之一
  2. 配置oracle服务器以从外部机器访问oracle
  3. React中跨域问题的完美解决方案
  4. JavaScript实现topologicalSort拓扑排序算法(附完整源码)
  5. 1010. 一元多项式求导
  6. C++ 内建函数对象
  7. Java线程(七):锁对象Lock-同步问题更完美的处理方式 .
  8. P30年订单或超2000万 正与苹果抢流水线
  9. w2金融股票分析— matplotlib库
  10. Photoshop7.0安装步骤
  11. 训练网络时指定gpu显卡
  12. SpringSecurity自定义多Provider时提示No AuthenticationProvider found for问题的解决方案与原理(一)
  13. 频繁发送socket命令返回:[WinError 10054] 远程主机强迫关闭了一个现有的连接
  14. 利用Arcgis制作图像分割数据集
  15. 无法更新运行时文件夹共享状态_关于iPadOS 13.4更新的5件事
  16. ios系统有哪些好用的思维导图软件?
  17. 利用turtle库绘制五角星(以及填充)
  18. PTA Swan学院社团招新(sort排序)
  19. 为什么TFN F1和F7光时域反射仪市占率那么高,为什么?
  20. 笨方法学Python笔记(7)

热门文章

  1. MySQL数据和日志的刷盘机制以及双一配置
  2. 采用comsol分析加筋圆筒的声振响应
  3. noip计算机复赛比赛试题,NOIP2018复赛成绩19日可查!
  4. 计算机硬盘的管理员权限设置,设置恢复在win10中改磁盘名字需要管理员权限的图文...
  5. seo外链怎么发:如何发布高质量外链
  6. AdSec 中开裂梁截面刚度计算
  7. 水安ABC考试多选练习题库(4)
  8. 古老密码---凯撒密码
  9. 16周岁人类个体的思维碎片
  10. PDF密码怎么解除?教你一键解密的方法!