use of java.awt.FontFormatException in project jdk8u_jdk by JetBrains.
the class TrueTypeFont method init.
protected void init(int fIndex) throws FontFormatException {
int headerOffset = 0;
ByteBuffer buffer = readBlock(0, TTCHEADERSIZE);
try {
switch(buffer.getInt()) {
case ttcfTag:
// skip TTC version ID
buffer.getInt();
directoryCount = buffer.getInt();
if (fIndex >= directoryCount) {
throw new FontFormatException("Bad collection index");
}
fontIndex = fIndex;
buffer = readBlock(TTCHEADERSIZE + 4 * fIndex, 4);
headerOffset = buffer.getInt();
break;
case v1ttTag:
case trueTag:
case ottoTag:
break;
default:
throw new FontFormatException("Unsupported sfnt " + getPublicFileName());
}
/* Now have the offset of this TT font (possibly within a TTC)
* After the TT version/scaler type field, is the short
* representing the number of tables in the table directory.
* The table directory begins at 12 bytes after the header.
* Each table entry is 16 bytes long (4 32-bit ints)
*/
buffer = readBlock(headerOffset + 4, 2);
numTables = buffer.getShort();
directoryOffset = headerOffset + DIRECTORYHEADERSIZE;
ByteBuffer bbuffer = readBlock(directoryOffset, numTables * DIRECTORYENTRYSIZE);
IntBuffer ibuffer = bbuffer.asIntBuffer();
DirectoryEntry table;
tableDirectory = new DirectoryEntry[numTables];
for (int i = 0; i < numTables; i++) {
tableDirectory[i] = table = new DirectoryEntry();
table.tag = ibuffer.get();
/* checksum */
ibuffer.get();
table.offset = ibuffer.get();
table.length = ibuffer.get();
if (table.offset + table.length > fileSize) {
throw new FontFormatException("bad table, tag=" + table.tag);
}
}
if (getDirectoryEntry(headTag) == null) {
throw new FontFormatException("missing head table");
}
if (getDirectoryEntry(maxpTag) == null) {
throw new FontFormatException("missing maxp table");
}
if (getDirectoryEntry(hmtxTag) != null && getDirectoryEntry(hheaTag) == null) {
throw new FontFormatException("missing hhea table");
}
initNames();
} catch (Exception e) {
if (FontUtilities.isLogging()) {
FontUtilities.getLogger().severe(e.toString());
}
if (e instanceof FontFormatException) {
throw (FontFormatException) e;
} else {
throw new FontFormatException(e.toString());
}
}
if (familyName == null || fullName == null) {
throw new FontFormatException("Font name not found");
}
/* The os2_Table is needed to gather some info, but we don't
* want to keep it around (as a field) so obtain it once and
* pass it to the code that needs it.
*/
ByteBuffer os2_Table = getTableBuffer(os_2Tag);
setStyle(os2_Table);
setCJKSupport(os2_Table);
}
use of java.awt.FontFormatException in project jdk8u_jdk by JetBrains.
the class TestLayoutVsICU method getFont.
private Font getFont(String fontName, Map<String, String> fontAttrs) {
Font f;
if (false)
try {
f = Font.getFont(fontName);
if (f != null) {
if (OPT_VERBOSE) {
System.out.println("Loaded default path to " + fontName);
}
return f;
}
} catch (Throwable t) {
if (OPT_VERBOSE) {
t.printStackTrace();
System.out.println("problem loading font " + fontName + " - " + t.toString());
}
}
File homeDir = new File(System.getProperty("user.home"));
File fontDir = new File(homeDir, "fonts");
File fontFile = new File(fontDir, fontName);
//System.out.println("## trying " + fontFile.getAbsolutePath());
if (fontFile.canRead()) {
try {
if (!verifyFont(fontFile, fontAttrs)) {
System.out.println("Warning: failed to verify " + fontName);
}
f = Font.createFont(Font.TRUETYPE_FONT, fontFile);
if (f != null & OPT_VERBOSE) {
System.out.println("> loaded from " + fontFile.getAbsolutePath() + " - " + f.toString());
}
return f;
} catch (FontFormatException e) {
if (OPT_VERBOSE) {
e.printStackTrace();
System.out.println("problem loading font " + fontName + " - " + e.toString());
}
} catch (IOException e) {
if (OPT_VERBOSE) {
e.printStackTrace();
System.out.println("problem loading font " + fontName + " - " + e.toString());
}
}
}
return null;
}
use of java.awt.FontFormatException in project pcgen by PCGen.
the class Main method initPrintPreviewFonts.
private static void initPrintPreviewFonts() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String fontDir = ConfigurationSettings.getOutputSheetsDir() + File.separator + "fonts" + File.separator + "NotoSans" + File.separator;
try {
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File(fontDir + "NotoSans-Regular.ttf")));
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File(fontDir + "NotoSans-Bold.ttf")));
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File(fontDir + "NotoSans-Italic.ttf")));
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File(fontDir + "NotoSans-BoldItalic.ttf")));
} catch (IOException | FontFormatException ex) {
Logging.errorPrint("Unexpected exception loading fonts fo print p", ex);
}
}
use of java.awt.FontFormatException in project android_frameworks_base by crdroidandroid.
the class FontFamily_Delegate method loadFont.
private static Font loadFont(String path) {
if (path.startsWith(SYSTEM_FONTS)) {
String relativePath = path.substring(SYSTEM_FONTS.length());
File f = new File(sFontLocation, relativePath);
try {
return Font.createFont(Font.TRUETYPE_FONT, f);
} catch (Exception e) {
if (path.endsWith(EXTENSION_OTF) && e instanceof FontFormatException) {
// warning.
return null;
}
Bridge.getLog().fidelityWarning(LayoutLog.TAG_BROKEN, String.format("Unable to load font %1$s", relativePath), e, null);
}
} else {
Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, "Only platform fonts located in " + SYSTEM_FONTS + "can be loaded.", null, null);
}
return null;
}
use of java.awt.FontFormatException in project android_frameworks_base by crdroidandroid.
the class FontFamily_Delegate method nAddFontFromAsset.
@LayoutlibDelegate
static /*package*/
boolean nAddFontFromAsset(long nativeFamily, AssetManager mgr, String path) {
FontFamily_Delegate ffd = sManager.getDelegate(nativeFamily);
if (ffd == null) {
return false;
}
ffd.mValid = true;
if (mgr == null) {
return false;
}
if (mgr instanceof BridgeAssetManager) {
InputStream fontStream = null;
try {
AssetRepository assetRepository = ((BridgeAssetManager) mgr).getAssetRepository();
if (assetRepository == null) {
Bridge.getLog().error(LayoutLog.TAG_MISSING_ASSET, "Asset not found: " + path, null);
return false;
}
if (!assetRepository.isSupported()) {
// Don't log any warnings on unsupported IDEs.
return false;
}
// Check cache
FontInfo fontInfo = sCache.get(path);
if (fontInfo != null) {
// renew the font's lease.
sCache.put(path, fontInfo);
ffd.addFont(fontInfo);
return true;
}
fontStream = assetRepository.openAsset(path, AssetManager.ACCESS_STREAMING);
if (fontStream == null) {
Bridge.getLog().error(LayoutLog.TAG_MISSING_ASSET, "Asset not found: " + path, path);
return false;
}
Font font = Font.createFont(Font.TRUETYPE_FONT, fontStream);
fontInfo = new FontInfo();
fontInfo.mFont = font;
fontInfo.mWeight = font.isBold() ? BOLD_FONT_WEIGHT : DEFAULT_FONT_WEIGHT;
fontInfo.mIsItalic = font.isItalic();
ffd.addFont(fontInfo);
return true;
} catch (IOException e) {
Bridge.getLog().error(LayoutLog.TAG_MISSING_ASSET, "Unable to load font " + path, e, path);
} catch (FontFormatException e) {
if (path.endsWith(EXTENSION_OTF)) {
// otf fonts are not supported on the user's config (JRE version + OS)
Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, "OpenType fonts are not supported yet: " + path, null, path);
} else {
Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Unable to load font " + path, e, path);
}
} finally {
if (fontStream != null) {
try {
fontStream.close();
} catch (IOException ignored) {
}
}
}
return false;
}
// This should never happen. AssetManager is a final class (from user's perspective), and
// we've replaced every creation of AssetManager with our implementation. We create an
// exception and log it, but continue with rest of the rendering, without loading this font.
Bridge.getLog().error(LayoutLog.TAG_BROKEN, "You have found a bug in the rendering library. Please file a bug at b.android.com.", new RuntimeException("Asset Manager is not an instance of BridgeAssetManager"), null);
return false;
}
Aggregations