001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.commons.lang.font; 020 021import java.awt.Font; 022import java.awt.FontMetrics; 023import java.awt.Graphics2D; 024import java.awt.GraphicsEnvironment; 025import java.awt.image.BufferedImage; 026import java.util.Iterator; 027 028import lucee.runtime.exp.ExpressionException; 029import lucee.runtime.op.Duplicator; 030import lucee.runtime.type.Array; 031import lucee.runtime.type.ArrayImpl; 032 033public class FontUtil { 034 035 private static Array fonts; 036 private static Graphics2D graphics; 037 038 public synchronized static Array getAvailableFontsAsStringArray() { 039 Iterator<Object> it = getAvailableFonts(false).valueIterator(); 040 Array arr=new ArrayImpl(); 041 while(it.hasNext()) { 042 arr.appendEL(((Font)it.next()).getFontName()); 043 } 044 return arr; 045 } 046 private synchronized static Array getAvailableFonts(boolean duplicate) { 047 if (fonts == null) { 048 049 fonts = new ArrayImpl(); 050 GraphicsEnvironment graphicsEvn = GraphicsEnvironment.getLocalGraphicsEnvironment(); 051 Font[] availableFonts = graphicsEvn.getAllFonts(); 052 for (int i = 0; i < availableFonts.length; i++) { 053 fonts.appendEL(availableFonts[i]); 054 } 055 056 } 057 if(!duplicate) return fonts; 058 return (Array) Duplicator.duplicate(fonts,false); 059 } 060 061 public static String toString(Font font) { 062 if(font==null) return null; 063 return font.getFontName(); 064 } 065 066 public static Font getFont(String font, Font defaultValue) { 067 Font f=Font.decode(font); 068 if(f!=null) return f; 069 // font name 070 Iterator<Object> it = getAvailableFonts(false).valueIterator(); 071 while(it.hasNext()) { 072 f=(Font) it.next(); 073 if(f.getFontName().equalsIgnoreCase(font)) return f; 074 } 075 // family 076 it = getAvailableFonts(false).valueIterator(); 077 while(it.hasNext()) { 078 f=(Font) it.next(); 079 if(f.getFamily().equalsIgnoreCase(font)) return f; 080 } 081 return defaultValue; 082 } 083 084 public static Font getFont(String font) throws ExpressionException { 085 Font f = getFont(font,null); 086 if(f!=null) return f; 087 throw new ExpressionException("no font with name ["+font+"] available" 088 ,"to get available fonts call function ImageFonts()"); 089 } 090 091 public static FontMetrics getFontMetrics(Font font) { 092 if(graphics==null) { 093 graphics = new BufferedImage(1, 1,BufferedImage.TYPE_INT_ARGB).createGraphics(); 094 } 095 return graphics.getFontMetrics(font); 096 } 097}