📄 type1font.java
字号:
}
catch (Exception e) {
// empty on purpose
}
}
}
}
/** Generates the font descriptor for this font or <CODE>null</CODE> if it is
* one of the 14 built in fonts.
* @param fontStream the indirect reference to a PdfStream containing the font or <CODE>null</CODE>
* @return the PdfDictionary containing the font descriptor or <CODE>null</CODE>
*/
private PdfDictionary getFontDescriptor(PdfIndirectReference fontStream)
{
if (builtinFont)
return null;
PdfDictionary dic = new PdfDictionary(PdfName.FONTDESCRIPTOR);
dic.put(PdfName.ASCENT, new PdfNumber(Ascender));
dic.put(PdfName.CAPHEIGHT, new PdfNumber(CapHeight));
dic.put(PdfName.DESCENT, new PdfNumber(Descender));
dic.put(PdfName.FONTBBOX, new PdfRectangle(llx, lly, urx, ury));
dic.put(PdfName.FONTNAME, new PdfName(FontName));
dic.put(PdfName.ITALICANGLE, new PdfNumber(ItalicAngle));
dic.put(PdfName.STEMV, new PdfNumber(StdVW));
if (fontStream != null)
dic.put(PdfName.FONTFILE, fontStream);
int flags = 0;
if (IsFixedPitch)
flags |= 1;
flags |= fontSpecific ? 4 : 32;
if (ItalicAngle < 0)
flags |= 64;
if (FontName.indexOf("Caps") >= 0 || FontName.endsWith("SC"))
flags |= 131072;
if (Weight.equals("Bold"))
flags |= 262144;
dic.put(PdfName.FLAGS, new PdfNumber(flags));
return dic;
}
/** Generates the font dictionary for this font.
* @return the PdfDictionary containing the font dictionary
* @param firstChar the first valid character
* @param lastChar the last valid character
* @param shortTag a 256 bytes long <CODE>byte</CODE> array where each unused byte is represented by 0
* @param fontDescriptor the indirect reference to a PdfDictionary containing the font descriptor or <CODE>null</CODE>
*/
private PdfDictionary getFontBaseType(PdfIndirectReference fontDescriptor, int firstChar, int lastChar, byte shortTag[])
{
PdfDictionary dic = new PdfDictionary(PdfName.FONT);
dic.put(PdfName.SUBTYPE, PdfName.TYPE1);
dic.put(PdfName.BASEFONT, new PdfName(FontName));
boolean stdEncoding = encoding.equals("Cp1252") || encoding.equals("MacRoman");
if (!fontSpecific || specialMap != null) {
for (int k = firstChar; k <= lastChar; ++k) {
if (!differences[k].equals(notdef)) {
firstChar = k;
break;
}
}
if (stdEncoding)
dic.put(PdfName.ENCODING, encoding.equals("Cp1252") ? PdfName.WIN_ANSI_ENCODING : PdfName.MAC_ROMAN_ENCODING);
else {
PdfDictionary enc = new PdfDictionary(PdfName.ENCODING);
PdfArray dif = new PdfArray();
boolean gap = true;
for (int k = firstChar; k <= lastChar; ++k) {
if (shortTag[k] != 0) {
if (gap) {
dif.add(new PdfNumber(k));
gap = false;
}
dif.add(new PdfName(differences[k]));
}
else
gap = true;
}
enc.put(PdfName.DIFFERENCES, dif);
dic.put(PdfName.ENCODING, enc);
}
}
if (specialMap != null || forceWidthsOutput || !(builtinFont && (fontSpecific || stdEncoding))) {
dic.put(PdfName.FIRSTCHAR, new PdfNumber(firstChar));
dic.put(PdfName.LASTCHAR, new PdfNumber(lastChar));
PdfArray wd = new PdfArray();
for (int k = firstChar; k <= lastChar; ++k) {
if (shortTag[k] == 0)
wd.add(new PdfNumber(0));
else
wd.add(new PdfNumber(widths[k]));
}
dic.put(PdfName.WIDTHS, wd);
}
if (!builtinFont && fontDescriptor != null)
dic.put(PdfName.FONTDESCRIPTOR, fontDescriptor);
return dic;
}
/** 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 {
int firstChar = ((Integer)params[0]).intValue();
int lastChar = ((Integer)params[1]).intValue();
byte shortTag[] = (byte[])params[2];
boolean subsetp = ((Boolean)params[3]).booleanValue() && subset;
if (!subsetp) {
firstChar = 0;
lastChar = shortTag.length - 1;
for (int k = 0; k < shortTag.length; ++k)
shortTag[k] = 1;
}
PdfIndirectReference ind_font = null;
PdfObject pobj = null;
PdfIndirectObject obj = null;
pobj = getFontStream();
if (pobj != null){
obj = writer.addToBody(pobj);
ind_font = obj.getIndirectReference();
}
pobj = getFontDescriptor(ind_font);
if (pobj != null){
obj = writer.addToBody(pobj);
ind_font = obj.getIndirectReference();
}
pobj = getFontBaseType(ind_font, firstChar, lastChar, shortTag);
writer.addToBody(pobj, ref);
}
/** Gets the font parameter identified by <CODE>key</CODE>. Valid values
* for <CODE>key</CODE> are <CODE>ASCENT</CODE>, <CODE>CAPHEIGHT</CODE>, <CODE>DESCENT</CODE>,
* <CODE>ITALICANGLE</CODE>, <CODE>BBOXLLX</CODE>, <CODE>BBOXLLY</CODE>, <CODE>BBOXURX</CODE>
* and <CODE>BBOXURY</CODE>.
* @param key the parameter to be extracted
* @param fontSize the font size in points
* @return the parameter in points
*/
public float getFontDescriptor(int key, float fontSize) {
switch (key) {
case AWT_ASCENT:
case ASCENT:
return Ascender * fontSize / 1000;
case CAPHEIGHT:
return CapHeight * fontSize / 1000;
case AWT_DESCENT:
case DESCENT:
return Descender * fontSize / 1000;
case ITALICANGLE:
return ItalicAngle;
case BBOXLLX:
return llx * fontSize / 1000;
case BBOXLLY:
return lly * fontSize / 1000;
case BBOXURX:
return urx * fontSize / 1000;
case BBOXURY:
return ury * fontSize / 1000;
case AWT_LEADING:
return 0;
case AWT_MAXADVANCE:
return (urx - llx) * fontSize / 1000;
}
return 0;
}
/** Gets the postscript font name.
* @return the postscript font name
*/
public String getPostscriptFontName() {
return FontName;
}
/** Gets the full name of the font. If it is a True Type font
* each array element will have {Platform ID, Platform Encoding ID,
* Language ID, font name}. The interpretation of this values can be
* found in the Open Type specification, chapter 2, in the 'name' table.<br>
* For the other fonts the array has a single element with {"", "", "",
* font name}.
* @return the full name of the font
*/
public String[][] getFullFontName() {
return new String[][]{{"", "", "", FullName}};
}
/** Gets the family name of the font. If it is a True Type font
* each array element will have {Platform ID, Platform Encoding ID,
* Language ID, font name}. The interpretation of this values can be
* found in the Open Type specification, chapter 2, in the 'name' table.<br>
* For the other fonts the array has a single element with {"", "", "",
* font name}.
* @return the family name of the font
*/
public String[][] getFamilyFontName() {
return new String[][]{{"", "", "", FamilyName}};
}
/** Checks if the font has any kerning pairs.
* @return <CODE>true</CODE> if the font has any kerning pairs
*/
public boolean hasKernPairs() {
return !KernPairs.isEmpty();
}
/**
* Sets the font name that will appear in the pdf font dictionary.
* Use with care as it can easily make a font unreadable if not embedded.
* @param name the new font name
*/
public void setPostscriptFontName(String name) {
FontName = name;
}
/**
* Sets the kerning between two Unicode chars.
* @param char1 the first char
* @param char2 the second char
* @param kern the kerning to apply in normalized 1000 units
* @return <code>true</code> if the kerning was applied, <code>false</code> otherwise
*/
public boolean setKerning(char char1, char char2, int kern) {
String first = GlyphList.unicodeToName((int)char1);
if (first == null)
return false;
String second = GlyphList.unicodeToName((int)char2);
if (second == null)
return false;
Object obj[] = (Object[])KernPairs.get(first);
if (obj == null) {
obj = new Object[]{second, new Integer(kern)};
KernPairs.put(first, obj);
return true;
}
for (int k = 0; k < obj.length; k += 2) {
if (second.equals(obj[k])) {
obj[k + 1] = new Integer(kern);
return true;
}
}
int size = obj.length;
Object obj2[] = new Object[size + 2];
System.arraycopy(obj, 0, obj2, 0, size);
obj2[size] = second;
obj2[size + 1] = new Integer(kern);
KernPairs.put(first, obj2);
return true;
}
protected int[] getRawCharBBox(int c, String name) {
Object metrics[];
if (name == null) { // font specific
metrics = (Object[])CharMetrics.get(new Integer(c));
}
else {
if (name.equals(".notdef"))
return null;
metrics = (Object[])CharMetrics.get(name);
}
if (metrics != null)
return ((int[])(metrics[3]));
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -