Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow TTF filepath for function imageDrawText #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions source/java/src/org/lucee/extension/image/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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();
Expand Down
6 changes: 5 additions & 1 deletion source/java/src/org/lucee/extension/image/MarpleCaptcha.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

import javax.imageio.ImageIO;

import lucee.runtime.exp.PageException;
import org.lucee.extension.image.font.FontUtil;

/**
* concrete captcha implementation
*/
Expand All @@ -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);
}
}

/**
Expand Down
41 changes: 40 additions & 1 deletion source/java/src/org/lucee/extension/image/font/FontUtil.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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
Expand Down