28package com.jogamp.graph.font;
30import java.io.BufferedInputStream;
32import java.io.FileInputStream;
33import java.io.IOException;
34import java.io.InputStream;
35import java.security.PrivilegedAction;
37import com.jogamp.common.net.Uri;
38import com.jogamp.common.util.IOUtil;
39import com.jogamp.common.util.PropertyAccess;
40import com.jogamp.common.util.ReflectionUtil;
41import com.jogamp.common.util.SecurityUtil;
42import com.jogamp.common.util.cache.TempJarCache;
44import jogamp.graph.font.FontConstructor;
45import jogamp.graph.font.SymbolsFontLoader;
46import jogamp.graph.font.UbuntuFontLoader;
57 private static final String FontConstructorPropKey =
"jogamp.graph.font.ctor";
58 private static final String DefaultFontConstructor =
"jogamp.graph.font.typecast.TypecastFontConstructor";
66 private static final FontConstructor fontConstr;
74 String fontImplName = PropertyAccess.getProperty(FontConstructorPropKey,
true);
75 if(
null == fontImplName) {
76 fontImplName = DefaultFontConstructor;
78 fontConstr = (FontConstructor) ReflectionUtil.createInstance(fontImplName,
FontFactory.class.getClassLoader());
85 public static final FontSet get(
final int font) {
88 return SymbolsFontLoader.get();
90 return UbuntuFontLoader.get();
100 public static final Font get(
final File file)
throws IOException {
101 return fontConstr.create(file);
117 public static final Font get(
final InputStream stream,
final int streamLen,
final boolean closeStream)
throws IOException {
119 return fontConstr.create(stream, streamLen);
139 public static final Font get(
final InputStream stream,
final boolean closeStream)
throws IOException {
140 final IOException[] ioe = {
null };
141 final int[] streamLen = { 0 };
142 final File tempFile[] = {
null };
144 final InputStream bis = SecurityUtil.doPrivileged(
new PrivilegedAction<InputStream>() {
146 public InputStream run() {
147 InputStream bis =
null;
149 tempFile[0] = IOUtil.createTempFile(
"jogl.font",
".ttf",
false);
150 streamLen[0] = IOUtil.copyStream2File(stream, tempFile[0]);
151 if( 0 == streamLen[0] ) {
152 throw new IOException(
"Font stream has zero bytes");
154 bis =
new BufferedInputStream(
new FileInputStream(tempFile[0]), streamLen[0]);
155 }
catch (
final IOException e) {
157 if(
null != tempFile[0] ) {
158 tempFile[0].delete();
164 IOUtil.close(stream, ioe, System.err);
169 if(
null != ioe[0] ) {
173 throw new IOException(
"Could not cache font stream");
176 return fontConstr.create(bis, streamLen[0]);
181 if(
null != tempFile[0] ) {
182 SecurityUtil.doPrivileged(
new PrivilegedAction<Object>() {
184 public Object run() {
185 tempFile[0].delete();
192 public static final Font get(
final Class<?> context,
final String fname,
final boolean useTempJarCache)
throws IOException {
193 InputStream stream =
null;
194 if( useTempJarCache ) {
196 final Uri uri = TempJarCache.getResourceUri(fname);
197 stream =
null != uri ? uri.toURL().openConnection().getInputStream() :
null;
198 }
catch (
final Exception e) {
199 throw new IOException(e);
202 stream = IOUtil.getResource(fname, context.getClassLoader(), context).getInputStream();
204 if(
null != stream ) {
205 return FontFactory.get ( stream, true ) ;
211 if( Character.isWhitespace(c) ) {
214 if( 0 == c || Character.isISOControl(c) ) {
217 final Character.UnicodeBlock block = Character.UnicodeBlock.of( c );
218 return block !=
null && block != Character.UnicodeBlock.SPECIALS;
225 }
catch(
final IOException ioe) {
226 ioe.printStackTrace();
235 }
catch(
final IOException ioe) {
236 ioe.printStackTrace();
247 private static Font fallbackFont =
null;
The optional property jogamp.graph.font.ctor allows user to specify the FontConstructor implementatio...
static synchronized void setFallbackFont(final Font f)
Registers given Font as the default fallback font.
static final FontSet getDefault()
static synchronized Font getFallbackFont()
Returns registered fallback Font, maybe null.
static final int SYMBOLS
Symbols is the default symbol font family and contains rounded material symbol fonts,...
static final int UBUNTU
Ubuntu is the default font family, {@value}.
static Font getDefaultFont()
Returns default Font of default FontSet or null if n/a.
static Font getSymbolsFont()
Returns the default symbols Font or null if n/a.
static boolean isPrintableChar(final char c)
Font getDefault()
Returns the family FAMILY_REGULAR with STYLE_NONE as retrieved with get(int, int).
Interface wrapper for font implementation.