Skip to content

Commit

Permalink
修复在非windows环境下因字体不存在导致换行计算不正确的BUG
Browse files Browse the repository at this point in the history
  • Loading branch information
youseries committed Mar 14, 2018
1 parent b11b6c8 commit 39ad10d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@

import java.awt.Font;
import java.io.Serializable;
import java.math.BigDecimal;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.annotate.JsonIgnore;

import com.bstek.ureport.Utils;
import com.bstek.ureport.export.pdf.font.FontBuilder;


/**
Expand Down Expand Up @@ -192,10 +192,11 @@ public Font getFont(){
}else if(italic!=null && italic){
fontStyle=Font.ITALIC;
}
double size=fontSize * 1.1;
BigDecimal bigData=Utils.toBigDecimal(size);
int s=bigData.setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
this.font=new Font(fontFamily,fontStyle,s);
String fontName=fontFamily;
if(StringUtils.isBlank(fontName)){
fontName="宋体";
}
this.font=FontBuilder.getAwtFont(fontName, fontStyle, new Float(fontSize));
}
return this.font;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
******************************************************************************/
package com.bstek.ureport.export.pdf.font;

import java.awt.GraphicsEnvironment;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
Expand All @@ -28,6 +31,7 @@
import org.springframework.context.ApplicationContextAware;

import com.bstek.ureport.exception.ReportComputeException;
import com.bstek.ureport.exception.ReportException;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
Expand All @@ -38,7 +42,10 @@
* @since 2014年4月22日
*/
public class FontBuilder implements ApplicationContextAware{
private static ApplicationContext applicationContext;
private static final Map<String,BaseFont> fontMap=new HashMap<String,BaseFont>();
public static final Map<String,String> fontPathMap=new HashMap<String,String>();
private static List<String> systemFontNameList=new ArrayList<String>();
public static Font getFont(String fontName,int fontSize,boolean fontBold,boolean fontItalic,boolean underLine){
BaseFont baseFont=fontMap.get(fontName);
Font font=null;
Expand Down Expand Up @@ -73,7 +80,34 @@ public static Font getFont(String fontName,int fontSize,boolean fontBold,boolean
font.setStyle(fontStyle);
return font;
}

public static java.awt.Font getAwtFont(String fontName,int fontStyle,float size){
if(systemFontNameList.contains(fontName)){
return new java.awt.Font(fontName,fontStyle,new Float(size).intValue());
}
String fontPath=fontPathMap.get(fontName);
if(fontPath==null){
return null;
}
InputStream inputStream=null;
try {
inputStream=applicationContext.getResource(fontPath).getInputStream();
java.awt.Font font=java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, inputStream);
return font.deriveFont(fontStyle,size);
} catch (Exception e) {
throw new ReportException(e);
}finally{
IOUtils.closeQuietly(inputStream);
}
}

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
FontBuilder.applicationContext=applicationContext;
GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontNames=environment.getAvailableFontFamilyNames();
for(String name:fontNames){
systemFontNameList.add(name);
}
Collection<FontRegister> fontRegisters=applicationContext.getBeansOfType(FontRegister.class).values();
for(FontRegister fontReg:fontRegisters){
String fontName=fontReg.getFontName();
Expand All @@ -82,7 +116,7 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
continue;
}
try {
BaseFont baseFont=getIdentityFont(fontPath,applicationContext);
BaseFont baseFont=getIdentityFont(fontName,fontPath,applicationContext);
if(baseFont==null){
throw new ReportComputeException("Font " + fontPath + " does not exist");
}
Expand All @@ -94,7 +128,7 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
}
}

private BaseFont getIdentityFont(String fontPath,ApplicationContext applicationContext) throws DocumentException,IOException {
private BaseFont getIdentityFont(String fontFamily,String fontPath,ApplicationContext applicationContext) throws DocumentException,IOException {
if(!fontPath.startsWith(ApplicationContext.CLASSPATH_URL_PREFIX)){
fontPath=ApplicationContext.CLASSPATH_URL_PREFIX+fontPath;
}
Expand All @@ -108,6 +142,7 @@ private BaseFont getIdentityFont(String fontPath,ApplicationContext applicationC
}
InputStream inputStream=null;
try{
fontPathMap.put(fontFamily, fontPath);
inputStream=applicationContext.getResource(fontPath).getInputStream();
byte[] bytes = IOUtils.toByteArray(inputStream);
BaseFont baseFont = BaseFont.createFont(fontName, BaseFont.IDENTITY_H,BaseFont.EMBEDDED,true,bytes,null);
Expand Down

0 comments on commit 39ad10d

Please sign in to comment.