我正在使用Apache POI将pptx幻灯片转换为图像。在pptx幻灯片中,我在GE Inspira字体中有日文文本,这在我的系统中是不可用的(注释掉ge.registerFont(字体)来模拟它)。生成的图像以默认字体(see image here)显示日文文本。这是什么字体,这个默认字体集在哪里?
当我注册字体时,日文文本显示为方框(see image here)。这是因为GE Inspira字体不支持日文字符。有没有办法强制POI使用日文文本的默认字体?
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();
}
}
答案 0 :(得分:2)
正如评论中所提到的,Inspira GE字体不是unicode字体,因此需要回退。如果未指定特定的后备字体,则将使用Font.SANS_SERIF。对于像SANS_SERIF这样的逻辑字体,有java内部fallback configuration。不幸的是,自动添加了Lucida fonts do not support Chinese (Simplified), Chinese (Traditional), Japanese, and Korean.
所以你应该提供一个unicode字体,例如Mona,Code2000和Arial Unicode MS是一个不错的选择 - wikipedia也有很好的收藏。
该映射以Map<String,String>
的形式提供为POI特定的rendering hint,其中key
是原始字体系列,value
是替换字体。您还可以将"*"
指定为键,以捕获所有字体。在FONT_FALLBACK
旁边,还有一个FONT_MAP
提示来映射所有出现的内容,即不仅是缺少的字形。
这将在POI 3.16-beta2(2017年2月的ETA)中提供,或者您暂时使用trunk - 另一种选择是通过自定义DrawTextParagraph提供您自己的DrawFactory
要了解,如果您的字体能够呈现您的字符/ glpyhs,则需要在Windows character map tool或其他字体工具(如FontForge)中打开它,并检查字体是否有字形unicode block
@Test
public void unicodeRendering() throws Exception {
// create a sample pptx
XMLSlideShow ss = createSamplePPT();
Dimension pgsize = ss.getPageSize();
// set the font
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
for (String s : new String[]{"GEInspRg.ttf","mona.ttf"}) {
Font font = Font.createFont(Font.TRUETYPE_FONT, new File(s));
ge.registerFont(font);
}
Map<String,String> fallbackMap = new HashMap<String,String>();
fallbackMap.put("GE Inspira", "Mona");
// 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.setRenderingHint(Drawable.FONT_FALLBACK, fallbackMap);
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
graphics.setTransform(at);
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
ss.getSlides().get(0).draw(graphics);
javax.imageio.ImageIO.write(img, "png", new File("unicodeppt.png"));
ss.close();
}
private XMLSlideShow createSamplePPT() {
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("unicode ->\u30B4\u30DF\u7BB1<-");
run.setFontFamily("GE Inspira");
run.setFontSize(12.0);
return ss;
}