I am using Apache POI to convert pptx slide to image. In the pptx slide, I have Japanese text in GE Inspira font that is not available in my system (comment out ge.registerFont(font) to simulate that). The generated image shows the Japanese text in a default font (see image here). What font is that and where is this default font set?
When I register the font, the Japanese text appears as boxes (see image here). This is because GE Inspira font does not support Japanese characters. Is there a way to force POI to use the default font for Japanese text?
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.poi.sl.usermodel.TextParagraph.TextAlign;
import org.apache.poi.sl.usermodel.VerticalAlignment;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFTextBox;
import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
public class UnicodePPT {
public static void main(String[] args) throws Exception {
// create a sample pptx
XMLSlideShow ss = new XMLSlideShow();
Dimension pgsize = ss.getPageSize();
XSLFSlide slide = ss.createSlide();
XSLFTextBox tb = slide.createTextBox();
// tb.setShapeType(XSLFShapeType.HEART);
int shapeSize = 150;
tb.setAnchor(new Rectangle((int)(pgsize.getWidth() / 2 - shapeSize / 2), (int)(pgsize.getHeight()
/ 2
- shapeSize
/ 2), shapeSize, shapeSize));
tb.setLineWidth(2);
tb.setLineColor(Color.BLACK);
XSLFTextParagraph par = tb.addNewTextParagraph();
tb.setVerticalAlignment(VerticalAlignment.DISTRIBUTED);
par.setTextAlign(TextAlign.CENTER);
XSLFTextRun run = par.addNewTextRun();
run.setText("ゴミ箱");
run.setFontFamily("GE Inspira");
run.setFontSize(12.0);
// set the font
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
InputStream is = new FileInputStream("src/test/resources/GEInspRg.TTF");
Font font = Font.createFont(Font.TRUETYPE_FONT, is);
is.close();
ge.registerFont(font);
// render it
double zoom = 2; // magnify it by 2
AffineTransform at = new AffineTransform();
at.setToScale(zoom, zoom);
BufferedImage img = new BufferedImage((int)Math.ceil(pgsize.width * zoom),
(int)Math.ceil(pgsize.height * zoom), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
graphics.setTransform(at);
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
slide.draw(graphics);
FileOutputStream fos = new FileOutputStream("src/test/resources/unicodeppt.png");
javax.imageio.ImageIO.write(img, "png", fos);
fos.close();
}
}
As mentioned in the comments, the Inspira GE font is not an unicode font, therefore a fallback is needed. If no specific fallback font is specified, Font.SANS_SERIF will be used. For the logical fonts like SANS_SERIF there is java internal fallback configuration. Unfortunately the automatically added Lucida fonts do not support Chinese (Simplified), Chinese (Traditional), Japanese, and Korean.
So you should provide a unicode font, e.g. Mona, Code2000 and Arial Unicode MS are a good picks - wikipedia has also good collection. The mapping is provided as POI-specific rendering hint in the form of
Map<String,String>
, where thekey
is original font family andvalue
the substitution font. You can also specify"*"
as a key, to catch-all fonts. Beside theFONT_FALLBACK
, there's also aFONT_MAP
hint to map all the occurrences, i.e. not only the missing glyphs.This will be available in POI 3.16-beta2 (ETA February 2017) or you temporarily use the trunk - another option is to provide your own DrawTextParagraph via a customized DrawFactory
To find out, if your font is capable of rendering your chars/glpyhs, you need to open it in the Windows character map tool or some other font tool like FontForge and check if there's a glyph at the unicode block.