📄 truetypefontsubset.java
字号:
/*
* $Id: TrueTypeFontSubSet.java 2366 2006-09-14 23:10:58Z xlv $
* $Name$
*
* Copyright 2001, 2002 Paulo Soares
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
package com.lowagie.text.pdf;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import com.lowagie.text.DocumentException;
import com.lowagie.text.ExceptionConverter;
/** Subsets a True Type font by removing the unneeded glyphs from
* the font.
*
* @author Paulo Soares (psoares@consiste.pt)
*/
class TrueTypeFontSubSet {
static final String tableNamesSimple[] = {"cvt ", "fpgm", "glyf", "head",
"hhea", "hmtx", "loca", "maxp", "prep"};
static final String tableNamesCmap[] = {"cmap", "cvt ", "fpgm", "glyf", "head",
"hhea", "hmtx", "loca", "maxp", "prep"};
static final String tableNamesExtra[] = {"OS/2", "cmap", "cvt ", "fpgm", "glyf", "head",
"hhea", "hmtx", "loca", "maxp", "name, prep"};
static final int entrySelectors[] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4};
static final int TABLE_CHECKSUM = 0;
static final int TABLE_OFFSET = 1;
static final int TABLE_LENGTH = 2;
static final int HEAD_LOCA_FORMAT_OFFSET = 51;
static final int ARG_1_AND_2_ARE_WORDS = 1;
static final int WE_HAVE_A_SCALE = 8;
static final int MORE_COMPONENTS = 32;
static final int WE_HAVE_AN_X_AND_Y_SCALE = 64;
static final int WE_HAVE_A_TWO_BY_TWO = 128;
/** Contains the location of the several tables. The key is the name of
* the table and the value is an <CODE>int[3]</CODE> where position 0
* is the checksum, position 1 is the offset from the start of the file
* and position 2 is the length of the table.
*/
protected HashMap tableDirectory;
/** The file in use.
*/
protected RandomAccessFileOrArray rf;
/** The file name.
*/
protected String fileName;
protected boolean includeCmap;
protected boolean includeExtras;
protected boolean locaShortTable;
protected int locaTable[];
protected HashMap glyphsUsed;
protected ArrayList glyphsInList;
protected int tableGlyphOffset;
protected int newLocaTable[];
protected byte newLocaTableOut[];
protected byte newGlyfTable[];
protected int glyfTableRealSize;
protected int locaTableRealSize;
protected byte outFont[];
protected int fontPtr;
protected int directoryOffset;
/** Creates a new TrueTypeFontSubSet
* @param directoryOffset The offset from the start of the file to the table directory
* @param fileName the file name of the font
* @param glyphsUsed the glyphs used
* @param includeCmap <CODE>true</CODE> if the table cmap is to be included in the generated font
*/
TrueTypeFontSubSet(String fileName, RandomAccessFileOrArray rf, HashMap glyphsUsed, int directoryOffset, boolean includeCmap, boolean includeExtras) {
this.fileName = fileName;
this.rf = rf;
this.glyphsUsed = glyphsUsed;
this.includeCmap = includeCmap;
this.includeExtras = includeExtras;
this.directoryOffset = directoryOffset;
glyphsInList = new ArrayList(glyphsUsed.keySet());
}
/** Does the actual work of subsetting the font.
* @throws IOException on error
* @throws DocumentException on error
* @return the subset font
*/
byte[] process() throws IOException, DocumentException {
try {
rf.reOpen();
createTableDirectory();
readLoca();
flatGlyphs();
createNewGlyphTables();
locaTobytes();
assembleFont();
return outFont;
}
finally {
try {
rf.close();
}
catch (Exception e) {
// empty on purpose
}
}
}
protected void assembleFont() throws IOException {
int tableLocation[];
int fullFontSize = 0;
String tableNames[];
if (includeExtras)
tableNames = tableNamesExtra;
else {
if (includeCmap)
tableNames = tableNamesCmap;
else
tableNames = tableNamesSimple;
}
int tablesUsed = 2;
int len = 0;
for (int k = 0; k < tableNames.length; ++k) {
String name = tableNames[k];
if (name.equals("glyf") || name.equals("loca"))
continue;
tableLocation = (int[])tableDirectory.get(name);
if (tableLocation == null)
continue;
++tablesUsed;
fullFontSize += (tableLocation[TABLE_LENGTH] + 3) & (~3);
}
fullFontSize += newLocaTableOut.length;
fullFontSize += newGlyfTable.length;
int ref = 16 * tablesUsed + 12;
fullFontSize += ref;
outFont = new byte[fullFontSize];
fontPtr = 0;
writeFontInt(0x00010000);
writeFontShort(tablesUsed);
int selector = entrySelectors[tablesUsed];
writeFontShort((1 << selector) * 16);
writeFontShort(selector);
writeFontShort((tablesUsed - (1 << selector)) * 16);
for (int k = 0; k < tableNames.length; ++k) {
String name = tableNames[k];
tableLocation = (int[])tableDirectory.get(name);
if (tableLocation == null)
continue;
writeFontString(name);
if (name.equals("glyf")) {
writeFontInt(calculateChecksum(newGlyfTable));
len = glyfTableRealSize;
}
else if (name.equals("loca")) {
writeFontInt(calculateChecksum(newLocaTableOut));
len = locaTableRealSize;
}
else {
writeFontInt(tableLocation[TABLE_CHECKSUM]);
len = tableLocation[TABLE_LENGTH];
}
writeFontInt(ref);
writeFontInt(len);
ref += (len + 3) & (~3);
}
for (int k = 0; k < tableNames.length; ++k) {
String name = tableNames[k];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -