📄 truetypefontunicode.java
字号:
boolean firstTime = true;
for (int k = 0; k < metrics.length; ++k) {
int metric[] = (int[])metrics[k];
if (metric[1] == 1000)
continue;
int m = metric[0];
if (m == lastNumber + 1) {
buf.append(' ').append(metric[1]);
}
else {
if (!firstTime) {
buf.append(']');
}
firstTime = false;
buf.append(m).append('[').append(metric[1]);
}
lastNumber = m;
}
if (buf.length() > 1) {
buf.append("]]");
dic.put(PdfName.W, new PdfLiteral(buf.toString()));
}
}
return dic;
}
/** Generates the font dictionary.
* @param descendant the descendant dictionary
* @param subsetPrefix the subset prefix
* @param toUnicode the ToUnicode stream
* @return the stream
*/
private PdfDictionary getFontBaseType(PdfIndirectReference descendant, String subsetPrefix, PdfIndirectReference toUnicode) {
PdfDictionary dic = new PdfDictionary(PdfName.FONT);
dic.put(PdfName.SUBTYPE, PdfName.TYPE0);
// The PDF Reference manual advises to add -encoding to CID font names
if (cff)
dic.put(PdfName.BASEFONT, new PdfName(subsetPrefix+fontName+"-"+encoding));
//dic.put(PdfName.BASEFONT, new PdfName(subsetPrefix+fontName));
else
dic.put(PdfName.BASEFONT, new PdfName(subsetPrefix + fontName));
//dic.put(PdfName.BASEFONT, new PdfName(fontName));
dic.put(PdfName.ENCODING, new PdfName(encoding));
dic.put(PdfName.DESCENDANTFONTS, new PdfArray(descendant));
if (toUnicode != null)
dic.put(PdfName.TOUNICODE, toUnicode);
return dic;
}
/** The method used to sort the metrics array.
* @param o1 the first element
* @param o2 the second element
* @return the comparisation
*/
public int compare(Object o1, Object o2) {
int m1 = ((int[])o1)[0];
int m2 = ((int[])o2)[0];
if (m1 < m2)
return -1;
if (m1 == m2)
return 0;
return 1;
}
/** Outputs to the writer the font dictionaries and streams.
* @param writer the writer for this document
* @param ref the font indirect reference
* @param params several parameters that depend on the font type
* @throws IOException on error
* @throws DocumentException error in generating the object
*/
void writeFont(PdfWriter writer, PdfIndirectReference ref, Object params[]) throws DocumentException, IOException {
HashMap longTag = (HashMap)params[0];
addRangeUni(longTag, true, subset);
Object metrics[] = longTag.values().toArray();
Arrays.sort(metrics, this);
PdfIndirectReference ind_font = null;
PdfObject pobj = null;
PdfIndirectObject obj = null;
// sivan: cff
if (cff) {
RandomAccessFileOrArray rf2 = new RandomAccessFileOrArray(rf);
byte b[] = new byte[cffLength];
try {
rf2.reOpen();
rf2.seek(cffOffset);
rf2.readFully(b);
} finally {
try {
rf2.close();
} catch (Exception e) {
// empty on purpose
}
}
if (subset || subsetRanges != null) {
CFFFontSubset cff = new CFFFontSubset(new RandomAccessFileOrArray(b),longTag);
b = cff.Process( (cff.getNames())[0] );
}
pobj = new StreamFont(b, "CIDFontType0C");
obj = writer.addToBody(pobj);
ind_font = obj.getIndirectReference();
} else {
byte[] b;
if (subset || directoryOffset != 0) {
TrueTypeFontSubSet sb = new TrueTypeFontSubSet(fileName, new RandomAccessFileOrArray(rf), longTag, directoryOffset, false, false);
b = sb.process();
}
else {
b = getFullFont();
}
int lengths[] = new int[]{b.length};
pobj = new StreamFont(b, lengths);
obj = writer.addToBody(pobj);
ind_font = obj.getIndirectReference();
}
String subsetPrefix = "";
if (subset)
subsetPrefix = createSubsetPrefix();
PdfDictionary dic = getFontDescriptor(ind_font, subsetPrefix);
obj = writer.addToBody(dic);
ind_font = obj.getIndirectReference();
pobj = getCIDFontType2(ind_font, subsetPrefix, metrics);
obj = writer.addToBody(pobj);
ind_font = obj.getIndirectReference();
pobj = getToUnicode(metrics);
PdfIndirectReference toUnicodeRef = null;
if (pobj != null) {
obj = writer.addToBody(pobj);
toUnicodeRef = obj.getIndirectReference();
}
pobj = getFontBaseType(ind_font, subsetPrefix, toUnicodeRef);
writer.addToBody(pobj, ref);
}
/** A forbidden operation. Will throw a null pointer exception.
* @param text the text
* @return always <CODE>null</CODE>
*/
byte[] convertToBytes(String text)
{
return null;
}
/**
* Checks if a character exists in this font.
* @param c the character to check
* @return <CODE>true</CODE> if the character has a glyph,
* <CODE>false</CODE> otherwise
*/
public boolean charExists(char c) {
HashMap map = null;
if (fontSpecific)
map = cmap10;
else
map = cmap31;
if (map == null)
return false;
if (fontSpecific) {
if ((c & 0xff00) == 0 || (c & 0xff00) == 0xf000)
return map.get(new Integer(c & 0xff)) != null;
else
return false;
}
else
return map.get(new Integer(c)) != null;
}
/**
* Sets the character advance.
* @param c the character
* @param advance the character advance normalized to 1000 units
* @return <CODE>true</CODE> if the advance was set,
* <CODE>false</CODE> otherwise
*/
public boolean setCharAdvance(char c, int advance) {
HashMap map = null;
if (fontSpecific)
map = cmap10;
else
map = cmap31;
if (map == null)
return false;
int m[] = null;
if (fontSpecific) {
if ((c & 0xff00) == 0 || (c & 0xff00) == 0xf000)
m = (int[])map.get(new Integer(c & 0xff));
else
return false;
}
else
m = (int[])map.get(new Integer(c));
if (m == null)
return false;
else
m[1] = advance;
return true;
}
public int[] getCharBBox(char c) {
if (bboxes == null)
return null;
HashMap map = null;
if (fontSpecific)
map = cmap10;
else
map = cmap31;
if (map == null)
return null;
int m[] = null;
if (fontSpecific) {
if ((c & 0xff00) == 0 || (c & 0xff00) == 0xf000)
m = (int[])map.get(new Integer(c & 0xff));
else
return null;
}
else
m = (int[])map.get(new Integer(c));
if (m == null)
return null;
return bboxes[m[0]];
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -