📄 basefont.java
字号:
else if (isCJKFont)
fontBuilt = new CJKFont(name, encoding, embedded);
else
throw new DocumentException("Font '" + name + "' with '" + encoding + "' is not recognized.");
synchronized (fontCache) {
fontFound = (BaseFont)fontCache.get(key);
if (fontFound != null)
return fontFound;
fontCache.put(key, fontBuilt);
}
return fontBuilt;
}
/**
* Gets the name without the modifiers Bold, Italic or BoldItalic.
* @param name the full name of the font
* @return the name without the modifiers Bold, Italic or BoldItalic
*/
protected static String getBaseName(String name)
{
if (name.endsWith(",Bold"))
return name.substring(0, name.length() - 5);
else if (name.endsWith(",Italic"))
return name.substring(0, name.length() - 7);
else if (name.endsWith(",BoldItalic"))
return name.substring(0, name.length() - 11);
else
return name;
}
/**
* Normalize the encoding names. "winansi" is changed to "Cp1252" and
* "macroman" is changed to "MacRoman".
* @param enc the encoding to be normalized
* @return the normalized encoding
*/
protected static String normalizeEncoding(String enc)
{
if (enc.equals("winansi") || enc.equals(""))
return CP1252;
else if (enc.equals("macroman"))
return MACROMAN;
else
return enc;
}
/**
* Creates the <CODE>widths</CODE> and the <CODE>differences</CODE> arrays
* @throws UnsupportedEncodingException the encoding is not supported
*/
protected void createEncoding() throws UnsupportedEncodingException
{
if (fontSpecific) {
for (int k = 0; k < 256; ++k)
widths[k] = getRawWidth(k, null);
}
else {
String s;
String name;
char c;
byte b[] = new byte[1];
for (int k = 0; k < 256; ++k)
{
b[0] = (byte)k;
s = new String(b, encoding);
if (s.length() > 0) {
c = s.charAt(0);
}
else {
c = '?';
}
name = GlyphList.unicodeToName((int)c);
if (name == null)
name = notdef;
differences[k] = name;
unicodeDifferences[k] = c;
widths[k] = getRawWidth((int)c, name);
}
}
}
/**
* Gets the width from the font according to the Unicode char <CODE>c</CODE>
* or the <CODE>name</CODE>. If the <CODE>name</CODE> is null it's a symbolic font.
* @param c the unicode char
* @param name the glyph name
* @return the width of the char
*/
protected abstract int getRawWidth(int c, String name);
/**
* Gets the kerning between two Unicode chars.
* @param char1 the first char
* @param char2 the second char
* @return the kerning to be applied
*/
public abstract int getKerning(char char1, char char2);
/**
* Gets the width of a <CODE>char</CODE> in normalized 1000 units.
* @param char1 the unicode <CODE>char</CODE> to get the width of
* @return the width in normalized 1000 units
*/
public int getWidth(char char1)
{
return getWidth(new String(new char[]{char1}));
}
/**
* Gets the width of a <CODE>String</CODE> in normalized 1000 units.
* @param text the <CODE>String</CODE> to get the witdth of
* @return the width in normalized 1000 units
*/
public int getWidth(String text)
{
int total = 0;
try {
byte mbytes[] = text.getBytes(encoding);
for (int k = 0; k < mbytes.length; ++k)
total += widths[0xff & mbytes[k]];
}
catch (UnsupportedEncodingException e) {
}
return total;
}
/**
* Gets the width of a <CODE>String</CODE> in points.
* @param text the <CODE>String</CODE> to get the witdth of
* @param fontSize the font size
* @return the width in points
*/
public float getWidthPoint(String text, float fontSize)
{
return (float)getWidth(text) * 0.001f * fontSize;
}
/**
* Gets the width of a <CODE>char</CODE> in points.
* @param char1 the <CODE>char</CODE> to get the witdth of
* @param fontSize the font size
* @return the width in points
*/
public float getWidthPoint(char char1, float fontSize)
{
return getWidth(char1) * 0.001f * fontSize;
}
/**
* Converts a <CODE>String</CODE> to a </CODE>byte</CODE> array according
* to the font's encoding.
* @param text the <CODE>String</CODE> to be converted
* @return an array of <CODE>byte</CODE> representing the conversion according to the font's encoding
*/
byte[] convertToBytes(String text)
{
try {
return text.getBytes(encoding);
}
catch (UnsupportedEncodingException e) {
// Panic! We should not be here
e.printStackTrace();
return text.getBytes();
}
}
/** 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
*/
abstract void writeFont(PdfWriter writer, PdfIndirectReference ref, Object params[]) throws DocumentException, IOException;
/** Gets the encoding used to convert <CODE>String</CODE> into <CODE>byte[]</CODE>.
* @return the encoding name
*/
public String getEncoding()
{
return encoding;
}
/** 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 abstract float getFontDescriptor(int key, float fontSize);
/** Gets the font type. The font types can be: FONT_TYPE_T1,
* FONT_TYPE_TT, FONT_TYPE_CJK and FONT_TYPE_TTUNI.
* @return the font type
*/
public int getFontType() {
return fontType;
}
/** Gets the embedded flag.
* @return <CODE>true</CODE> if the font is embedded.
*/
public boolean isEmbedded() {
return embedded;
}
/** Gets the symbolic flag of the font.
* @return <CODE>true</CODE> if the font is symbolic
*/
public boolean isFontSpecific() {
return fontSpecific;
}
/** Creates a unique subset prefix to be added to the font name when the font is embedded and subset.
* @return the subset prefix
*/
String createSubsetPrefix() {
synchronized(subsetPrefix) {
for (int k = 0; k < subsetPrefix.length - 1; ++k) {
int c = subsetPrefix[k];
if (c == 'Z')
subsetPrefix[k] = 'A';
else {
subsetPrefix[k] = (char)(c + 1);
break;
}
}
return new String(subsetPrefix);
}
}
/** Gets the Unicode character corresponding to the byte output to the pdf stream.
* @param index the byte index
* @return the Unicode character
*/
char getUnicodeDifferences(int index) {
return unicodeDifferences[index];
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -