Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

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

Select an option

Save aspose-com-gists/553fc444fb5698905304db9ceced9937 to your computer and use it in GitHub Desktop.
Loading Type1 fonts, font metrics, Latin symbols and Type1 text rendering.

Type1 Fonts Examples

Load Type1 fonts, read their metrics, check basic Latin letters and render text with a custom glyph outline painter using Aspose.Font for Java.

Examples

  • Load a Type1 font from a PFB file.
  • Read the font name, glyph count and metrics.
  • Check support for all 52 basic Latin letters (A–Z and a–z) using named Type1 glyph IDs.
  • Render text to PNG images in two color schemes using Java 2D, a custom outline painter and glyph positioning helpers.

The examples use courier.pfb. Adapt getDataDir(), getOutputDir() and the relative font path to your application. Combine the rendering example with the drawText, fontWidthToImageWidth and Type1GlyphOutlinePainter snippets in the same class.

Related Documentation

Related Resources

Requirements

  • JDK 8 or later with Java 2D and ImageIO available; rendering also works in headless mode.
  • Aspose.Font for Java library.
  • An input Type1 font and a writable output directory for rendered images.
Aspose.Font for Java – Type1 Fonts Examples
// Learn more: https://docs.aspose.com/font/java/glyphs-and-metrics-type1-fonts/
String fileName = Paths.get(getDataDir(), "..", "Type1", "courier.pfb").normalize().toString();
FontDefinition fd = new FontDefinition(FontType.Type1,
new FontFileDefinition("pfb", new FileSystemStreamSource(fileName)));
Type1Font font = (Type1Font) Font.open(fd);
boolean supportsLatinLetters = true;
for (char code : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray()) {
GlyphId gid = font.getEncoding().decodeToGid(code);
// Type1 glyph IDs are names, not the numeric IDs used by TrueType.
if (gid == null || gid.equals(GlyphStringId.getNotDefId())
|| font.getGlyphById(gid) == null) {
supportsLatinLetters = false;
break;
}
}
System.out.println(supportsLatinLetters
? "Font " + font.getFontName() + " supports basic Latin letters."
: "Basic Latin letters are not fully supported by font " + font.getFontName() + ".");
// Learn more: https://docs.aspose.com/font/java/text-rendering-type1-font/
private static void drawText(String text, IFont font, double fontSize,
Paint backgroundBrush, Paint textBrush, String outputFile) throws IOException {
GlyphId[] glyphIds = new GlyphId[text.length()];
for (int i = 0; i < text.length(); i++) {
glyphIds[i] = font.getEncoding().decodeToGid(text.charAt(i));
}
double dpi = 300;
double resolutionCorrection = dpi / 72;
BufferedImage outputBitmap = new BufferedImage(960, 720, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = outputBitmap.createGraphics();
try {
graphics.setPaint(backgroundBrush);
graphics.fillRect(0, 0, outputBitmap.getWidth(), outputBitmap.getHeight());
graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
GlyphId previousGlyphId = null;
double glyphXCoordinate = 0;
double glyphYCoordinate = fontSize * resolutionCorrection;
for (GlyphId glyphId : glyphIds) {
if (glyphId == null) {
continue;
}
Glyph glyph = font.getGlyphAccessor().getGlyphById(glyphId);
if (glyph == null) {
continue;
}
if (previousGlyphId != null) {
double kerning = font.getMetrics().getKerningValue(previousGlyphId, glyphId)
/ glyph.getSourceResolution() * fontSize * resolutionCorrection;
double advance = fontWidthToImageWidth(font.getMetrics().getGlyphWidth(previousGlyphId),
glyph.getSourceResolution(), fontSize, dpi);
glyphXCoordinate += kerning + advance;
}
GeneralPath path = new GeneralPath();
IGlyphRenderer renderer = new GlyphOutlineRenderer(new Type1GlyphOutlinePainter(path));
TransformationMatrix matrix = new TransformationMatrix(new double[] {
fontSize * resolutionCorrection, 0, 0, -fontSize * resolutionCorrection,
glyphXCoordinate, glyphYCoordinate
});
renderer.renderGlyph(font, glyphId, matrix);
path.setWindingRule(GeneralPath.WIND_NON_ZERO);
graphics.setPaint(textBrush);
graphics.fill(path);
previousGlyphId = glyphId;
}
} finally {
graphics.dispose();
}
if (!ImageIO.write(outputBitmap, "png", new File(outputFile))) {
throw new IOException("No PNG writer is available.");
}
}
// Learn more: https://docs.aspose.com/font/java/text-rendering-type1-font/
private static double fontWidthToImageWidth(double width, int fontSourceResolution,
double fontSize, double dpi) {
return width / fontSourceResolution * fontSize * (dpi / 72);
}
// Learn more: https://docs.aspose.com/font/java/glyphs-and-metrics-type1-fonts/
String fileName = Paths.get(getDataDir(), "..", "Type1", "courier.pfb").normalize().toString();
FontDefinition fd = new FontDefinition(FontType.Type1,
new FontFileDefinition("pfb", new FileSystemStreamSource(fileName)));
Type1Font font = (Type1Font) 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()));
// Learn more: https://docs.aspose.com/font/java/text-rendering-type1-font/
private static final class Type1GlyphOutlinePainter implements IGlyphOutlinePainter {
private final GeneralPath path;
private Type1GlyphOutlinePainter(GeneralPath path) {
this.path = path;
}
@Override
public void moveTo(MoveTo moveTo) {
path.moveTo(moveTo.getX(), moveTo.getY());
}
@Override
public void lineTo(LineTo lineTo) {
path.lineTo(lineTo.getX(), lineTo.getY());
}
@Override
public void curveTo(CurveTo curveTo) {
path.curveTo(curveTo.getX1(), curveTo.getY1(), curveTo.getX2(), curveTo.getY2(),
curveTo.getX3(), curveTo.getY3());
}
@Override
public void closePath() {
path.closePath();
}
}
// Learn more: https://docs.aspose.com/font/java/loading-saving-type1-fonts/
String fileName = Paths.get(getDataDir(), "..", "Type1", "courier.pfb").normalize().toString();
FontDefinition fd = new FontDefinition(FontType.Type1,
new FontFileDefinition("pfb", new FileSystemStreamSource(fileName)));
Type1Font font = (Type1Font) Font.open(fd);
// Learn more: https://docs.aspose.com/font/java/text-rendering-type1-font/
String fileName = Paths.get(getDataDir(), "..", "Type1", "courier.pfb").normalize().toString();
FontDefinition fd = new FontDefinition(FontType.Type1,
new FontFileDefinition("pfb", new FileSystemStreamSource(fileName)));
Type1Font font = (Type1Font) Font.open(fd);
String firstOutput = Paths.get(getOutputDir(), "hello1_type1_out.png").toString();
String secondOutput = Paths.get(getOutputDir(), "hello2_type1_out.png").toString();
drawText("Hello world", font, 14, Color.WHITE, Color.BLACK, firstOutput);
drawText("Hello world", font, 14, Color.YELLOW, Color.RED, secondOutput);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment