From 37e9a94e19c3a3f1c1017a51a0a870b6580403ee Mon Sep 17 00:00:00 2001 From: paulklinkenberg Date: Fri, 13 Apr 2018 13:18:30 +0200 Subject: [PATCH 1/2] Allow TrueTypeFont filepath in "font" attribute for function imageDrawText() --- .../src/org/lucee/extension/image/Image.java | 12 +++--- .../lucee/extension/image/font/FontUtil.java | 41 ++++++++++++++++++- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/source/java/src/org/lucee/extension/image/Image.java b/source/java/src/org/lucee/extension/image/Image.java index 64d32ca..ae7182d 100644 --- a/source/java/src/org/lucee/extension/image/Image.java +++ b/source/java/src/org/lucee/extension/image/Image.java @@ -575,12 +575,12 @@ public void drawString(String text, int x, int y, Struct attr) throws PageExcept if (attr != null && attr.size()>0) { // font - String font=eng().getCastUtil().toString(attr.get("font","")).toLowerCase().trim(); - if(!eng().getStringUtil().isEmpty(font)) { - font=FontUtil.getFont(font).getFontName(); + String fontName=eng().getCastUtil().toString(attr.get("font","")).toLowerCase().trim(); + if(eng().getStringUtil().isEmpty(fontName)) { + fontName = "Serif"; } - else font = "Serif"; - + Font font = FontUtil.getFont(fontName); + // alpha //float alpha=eng().getCastUtil().toFloatValue(attr.get("alpha",null),1F); @@ -610,7 +610,7 @@ public void drawString(String text, int x, int y, Struct attr) throws PageExcept boolean underline = eng().getCastUtil().toBooleanValue(attr.get("underline",Boolean.FALSE)); AttributedString as = new AttributedString(text); - as.addAttribute(TextAttribute.FONT, new Font(font, style, size)); + as.addAttribute(TextAttribute.FONT, font.deriveFont(style, size)); if(strikethrough) as.addAttribute(TextAttribute.STRIKETHROUGH,TextAttribute.STRIKETHROUGH_ON); if(underline) as.addAttribute(TextAttribute.UNDERLINE,TextAttribute.UNDERLINE_ON); Graphics2D g = getGraphics(); diff --git a/source/java/src/org/lucee/extension/image/font/FontUtil.java b/source/java/src/org/lucee/extension/image/font/FontUtil.java index 31e30a0..f5afd18 100644 --- a/source/java/src/org/lucee/extension/image/font/FontUtil.java +++ b/source/java/src/org/lucee/extension/image/font/FontUtil.java @@ -1,14 +1,18 @@ package org.lucee.extension.image.font; import java.awt.Font; +import java.awt.FontFormatException; import java.awt.FontMetrics; import java.awt.Graphics2D; import java.awt.GraphicsEnvironment; import java.awt.image.BufferedImage; +import java.io.IOException; import java.io.Serializable; import java.util.Iterator; +import lucee.commons.io.res.Resource; import lucee.loader.engine.CFMLEngineFactory; +import lucee.loader.engine.CFMLEngine; import lucee.runtime.exp.PageException; import lucee.runtime.type.Array; @@ -55,7 +59,42 @@ public static String toString(Font font) { return font.getFontName(); } - public static Font getFont(String font, Font defaultValue) { + public static Font getFont(String font, Font defaultValue) throws PageException { + CFMLEngine eng = CFMLEngineFactory.getInstance(); + + /* check if given font is a path to a ttf file (check extension, then check existence) */ + if(!eng.getStringUtil().isEmpty(font) && font.length() > 4 + && font.substring(font.length()-4).toLowerCase() == ".ttf") { + + /* Check if the font is a valid path */ + Resource res; + try { + res = eng.getCastUtil().toResource(font); + if (res.exists() && res.isFile()) { + eng.getThreadPageContext().getConfig().getSecurityManager().checkFileLocation(res); + } + } catch (PageException ee) { + res = null; + } + + if (res != null) { + /* return the font */ + try { + return Font.createFont(Font.TRUETYPE_FONT, res.getInputStream()); + } catch(IOException e) { + throw CFMLEngineFactory.getInstance().getExceptionUtil().createExpressionException( + "Font file could not be read" + ,"The font file at ["+res.getAbsolutePath()+"] could not be read: " + e.getMessage()); + } catch(FontFormatException e) { + throw CFMLEngineFactory.getInstance().getExceptionUtil().createExpressionException( + "Invalid ttf font file" + ,"A FontFormat exception occurred while trying to use the font file at" + + " [" + res.getAbsolutePath() + "]. " + + "Make sure the file is a TrueType font. Exception: " + e.getMessage()); + } + } + } + Font f=Font.decode(font); if(f!=null) return f; // font name From c51dca805eb0fdd384ee92d7cf7b8d51edfdec95 Mon Sep 17 00:00:00 2001 From: paulklinkenberg Date: Fri, 13 Apr 2018 13:33:46 +0200 Subject: [PATCH 2/2] Captcha function now also accepts ttf filepaths for the "fonts" argument --- .../src/org/lucee/extension/image/MarpleCaptcha.java | 6 +++++- .../src/org/lucee/extension/image/captcha/Captcha.java | 9 ++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/source/java/src/org/lucee/extension/image/MarpleCaptcha.java b/source/java/src/org/lucee/extension/image/MarpleCaptcha.java index ae02a1f..841a42a 100644 --- a/source/java/src/org/lucee/extension/image/MarpleCaptcha.java +++ b/source/java/src/org/lucee/extension/image/MarpleCaptcha.java @@ -59,6 +59,10 @@ public BufferedImage generate(String text,int width, int height, String[] fonts, @Override public Font getFont(String font, Font defaultValue) { - return FontUtil.getFont(font,defaultValue); + try { + return FontUtil.getFont(font,defaultValue); + } catch(PageException e){ + return Font.decode(font); + } } } \ No newline at end of file diff --git a/source/java/src/org/lucee/extension/image/captcha/Captcha.java b/source/java/src/org/lucee/extension/image/captcha/Captcha.java index 93b2c6e..055e98b 100644 --- a/source/java/src/org/lucee/extension/image/captcha/Captcha.java +++ b/source/java/src/org/lucee/extension/image/captcha/Captcha.java @@ -25,6 +25,9 @@ import javax.imageio.ImageIO; +import lucee.runtime.exp.PageException; +import org.lucee.extension.image.font.FontUtil; + /** * concrete captcha implementation */ @@ -38,7 +41,11 @@ public final class Captcha extends AbstractCaptcha { @Override public Font getFont(String font, Font defaultValue) { - return Font.decode(font); + try { + return FontUtil.getFont(font,defaultValue); + } catch(PageException e){ + return Font.decode(font); + } } /**