Method from org.apache.pdfbox.encoding.Encoding Detail: |
protected void addCharacterEncoding(int code,
COSName name) {
codeToName.put( code, name );
nameToCode.put( name, code );
}
This will add a character encoding. |
public String getCharacter(int code) throws IOException {
String character = getCharacter( getName( code ) );
return character;
}
This will get the character from the code. |
public static String getCharacter(COSName name) {
COSName baseName = name;
String character = NAME_TO_CHARACTER.get( baseName );
if( character == null )
{
String nameStr = baseName.getName();
// test for Unicode name
// (uniXXXX - XXXX must be a multiple of four;
// each representing a hexadecimal Unicode code point)
if ( nameStr.startsWith( "uni" ) )
{
StringBuilder uniStr = new StringBuilder();
for ( int chPos = 3; chPos + 4 < = nameStr.length(); chPos += 4 )
{
try
{
int characterCode = Integer.parseInt( nameStr.substring( chPos, chPos + 4), 16 );
if ( ( characterCode > 0xD7FF ) && ( characterCode < 0xE000 ) )
{
Logger.getLogger(Encoding.class.getName()).log( Level.WARNING,
"Unicode character name with not allowed code area: " +
nameStr );
}
else
{
uniStr.append( (char) characterCode );
}
}
catch (NumberFormatException nfe)
{
Logger.getLogger(Encoding.class.getName()).log( Level.WARNING,
"Not a number in Unicode character name: " +
nameStr );
}
}
character = uniStr.toString();
}
else
{
// test if we have a suffix and if so remove it
if ( nameStr.indexOf('.') > 0 )
{
nameStr = nameStr.substring( 0, nameStr.indexOf('.') );
baseName = COSName.getPDFName( nameStr );
getCharacter(baseName);
}
character = nameStr;
}
}
return character;
}
This will get the character from the name. |
public int getCode(COSName name) throws IOException {
Integer code = nameToCode.get( name );
if( code == null )
{
throw new IOException( "No character code for character name '" + name.getName() + "'" );
}
return code;
}
This will get the character code for the name. |
public Map<Integer, COSName> getCodeToNameMap() {
return Collections.unmodifiableMap(codeToName);
}
Returns an unmodifiable view of the Code2Name mapping. |
public COSName getName(int code) throws IOException {
COSName name = codeToName.get( code );
if( name == null )
{
//lets be forgiving for now
name = COSName.getPDFName( "space" );
//throw new IOException( getClass().getName() +
// ": No name for character code '" + code + "'" );
}
return name;
}
This will take a character code and get the name from the code. |
public COSName getNameFromCharacter(char c) throws IOException {
COSName name = CHARACTER_TO_NAME.get( Character.toString(c) );
if( name == null )
{
throw new IOException( "No name for character '" + c + "'" );
}
return name;
}
This will take a character code and get the name from the code. |
public Map<COSName, Integer> getNameToCodeMap() {
return Collections.unmodifiableMap(nameToCode);
}
Returns an unmodifiable view of the Name2Code mapping. |