Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save aspose-com-gists/6da51850e2fd906f0b49121d2976e244 to your computer and use it in GitHub Desktop.

Select an option

Save aspose-com-gists/6da51850e2fd906f0b49121d2976e244 to your computer and use it in GitHub Desktop.
Loading and saving, font metrics, Latin symbols, reading and updating the name table.

TrueType and OpenType Fonts Examples

Load and save TrueType fonts, read font and glyph metrics, detect basic Latin letters and inspect font embedding flags with Aspose.Font for Java.

Examples

  • Load a TTF font from a file.
  • Load a font from bytes and save it to disk.
  • Read font metrics and the bounding box and width of the glyph for A.
  • Check whether a font contains all 52 basic Latin letters (A–Z and a–z).
  • Read embedding restrictions from the font's OS/2 table.

Related Documentation

Related Resources

Requirements

  • JDK 8 or later.
  • Aspose.Font for Java library.
  • Input font files and a writable output directory for saving. Adapt getDataDir(), getOutputDir() and the font paths to your application.
Aspose.Font for Java – TrueType and OpenType Fonts Examples
// Learn more: https://docs.aspose.com/font/java/glyphs-and-metrics-truetype-opentype-fonts/
String fileName = Paths.get(getDataDir(), "..", "TTF", "montserrat-bold.ttf").normalize().toString();
FontDefinition fd = new FontDefinition(FontType.TTF,
new FontFileDefinition("ttf", new FileSystemStreamSource(fileName)));
TtfFont font = (TtfFont) Font.open(fd);
boolean latinText = true;
// Check all 52 basic Latin letters; do not include punctuation between Z and a.
for (char code : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray()) {
GlyphId gid = font.getEncoding().decodeToGid(code);
if (gid == null || gid.equals(GlyphUInt32Id.getNotDefId())) {
latinText = false;
}
}
if (latinText) {
System.out.println(MessageFormat.format("Font {0} supports basic Latin letters.", font.getFontName()));
} else {
System.out.println(MessageFormat.format("Basic Latin letters are not supported by font {0}.", font.getFontName()));
}
// Learn more: https://docs.aspose.com/font/java/glyphs-and-metrics-truetype-opentype-fonts/
String fileName = Paths.get(getDataDir(), "..", "TTF", "montserrat-bold.ttf").normalize().toString();
FontDefinition fd = new FontDefinition(FontType.TTF,
new FontFileDefinition("ttf", new FileSystemStreamSource(fileName)));
TtfFont font = (TtfFont) Font.open(fd);
LicenseFlags licenseFlags = null;
if (font.getTtfTables().getOs2Table() != null) {
licenseFlags = font.getTtfTables().getOs2Table().getLicenseFlags();
}
if (licenseFlags == null || licenseFlags.isFSTypeAbsent()) {
System.out.println(MessageFormat.format("Font {0} has no embedded license restrictions.", font.getFontName()));
} else if (licenseFlags.isEditableEmbedding()) {
System.out.println(MessageFormat.format("Font {0} permits editable embedding.", font.getFontName()));
} else if (licenseFlags.isInstallableEmbedding()) {
System.out.println(MessageFormat.format("Font {0} permits installable embedding.", font.getFontName()));
} else if (licenseFlags.isPreviewAndPrintEmbedding()) {
System.out.println(MessageFormat.format("Font {0} permits preview and print embedding.", font.getFontName()));
} else if (licenseFlags.isRestrictedLicenseEmbedding()) {
System.out.println(MessageFormat.format("Font {0} has restricted embedding.", font.getFontName()));
}
// Learn more: https://docs.aspose.com/font/java/glyphs-and-metrics-truetype-opentype-fonts/
String fileName = Paths.get(getDataDir(), "..", "TTF", "montserrat-bold.ttf").normalize().toString();
FontDefinition fd = new FontDefinition(FontType.TTF,
new FontFileDefinition("ttf", new FileSystemStreamSource(fileName)));
TtfFont font = (TtfFont) Font.open(fd);
System.out.println("Font name: " + font.getFontName());
System.out.println("Glyph count: " + font.getNumGlyphs());
System.out.println(MessageFormat.format(
"Font metrics: ascender = {0}, descender = {1}, typo ascender = {2}, typo descender = {3}, UnitsPerEm = {4}",
font.getMetrics().getAscender(), font.getMetrics().getDescender(),
font.getMetrics().getTypoAscender(), font.getMetrics().getTypoDescender(),
font.getMetrics().getUnitsPerEM()));
// Locate the Unicode cmap and TrueType outlines for the glyph for A.
TtfCMapFormatBaseTable cmapTable = null;
if (font.getTtfTables().getCMapTable() != null) {
cmapTable = font.getTtfTables().getCMapTable().findUnicodeTable();
}
if (cmapTable != null && font.getTtfTables().getGlyfTable() != null) {
System.out.println("Font cmap Unicode table: PlatformID = " + cmapTable.getPlatformId()
+ ", PlatformSpecificID = " + cmapTable.getPlatformSpecificId());
long glyphIndex = cmapTable.getGlyphIndex('A');
if (glyphIndex != 0) {
Glyph glyph = font.getGlyphById(glyphIndex);
if (glyph != null) {
System.out.println(MessageFormat.format(
"Glyph BBox: Xmin = {0}, Xmax = {1}, Ymin = {2}, Ymax = {3}",
glyph.getGlyphBBox().getXMin(), glyph.getGlyphBBox().getXMax(),
glyph.getGlyphBBox().getYMin(), glyph.getGlyphBBox().getYMax()));
System.out.println("Width: " + font.getMetrics().getGlyphWidth(new GlyphUInt32Id(glyphIndex)));
}
}
}
// Learn more: https://docs.aspose.com/font/java/loading-saving-truetype-opentype-fonts/
String fileName = Paths.get(getDataDir(), "..", "TTF", "montserrat-bold.ttf").normalize().toString();
FontDefinition fd = new FontDefinition(FontType.TTF,
new FontFileDefinition("ttf", new FileSystemStreamSource(fileName)));
TtfFont font = (TtfFont) Font.open(fd);
// Learn more: https://docs.aspose.com/font/java/loading-saving-truetype-opentype-fonts/
byte[] fontMemoryData = Files.readAllBytes(
Paths.get(getDataDir(), "..", "TTF", "montserrat-bold.ttf").normalize());
FontDefinition fd = new FontDefinition(FontType.TTF,
new FontFileDefinition("ttf", new ByteContentStreamSource(fontMemoryData)));
TtfFont font = (TtfFont) Font.open(fd);
// Work with the loaded font, then save it to disk.
String outputFile = Paths.get(getOutputDir(), "montserrat-bold-saved.ttf").toString();
font.save(outputFile);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment