Issue #2189 Added a FontType option when initializing a font from a file.

Amend: Added abstract awt font class, deprecated initializeFont not specifying FontType, added comments

Change-Id: Ia5608cb7ffd0b760b3922e65107e66c971fa3e63

Former-commit-id: c386533867 [formerly 9250700e425983156dd2d226556647d6a2bf43af]
Former-commit-id: 3381c0016e
This commit is contained in:
Max Schenkelberg 2013-07-18 13:19:17 -05:00
parent d479258121
commit a8972da606
9 changed files with 252 additions and 202 deletions

View file

@ -21,6 +21,7 @@ package com.raytheon.uf.viz.core;
import java.awt.geom.Rectangle2D;
import java.awt.image.RenderedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@ -41,6 +42,8 @@ import com.raytheon.uf.viz.core.data.resp.NumericImageData;
import com.raytheon.uf.viz.core.drawables.ColorMapLoader;
import com.raytheon.uf.viz.core.drawables.IDescriptor;
import com.raytheon.uf.viz.core.drawables.IFont;
import com.raytheon.uf.viz.core.drawables.IFont.FontType;
import com.raytheon.uf.viz.core.drawables.IFont.Style;
import com.raytheon.uf.viz.core.drawables.IImage;
import com.raytheon.uf.viz.core.drawables.IShadedShape;
import com.raytheon.uf.viz.core.drawables.IWireframeShape;
@ -62,6 +65,7 @@ import com.raytheon.uf.viz.core.exception.VizException;
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* Jun 25, 2012 bsteffen Initial creation
* Jul 18, 2013 2189 mschenke Added ability to specify font type
*
* </pre>
*
@ -82,6 +86,11 @@ public abstract class AbstractGraphicsTarget implements IGraphicsTarget {
extensionManager = new GraphicsExtensionManager(this);
}
@Override
public IFont initializeFont(File fontFile, float size, Style[] styles) {
return initializeFont(fontFile, FontType.TRUETYPE, size, styles);
}
@Override
public boolean drawRasters(PaintProperties paintProps,
DrawableImage... images) throws VizException {

View file

@ -59,6 +59,7 @@ import com.raytheon.uf.viz.core.exception.VizException;
* track any needed updates to the Target extents.
* This functionality is primarily used by the
* Feature Following Zoom Tool at this time.
* 7/18/13 #2189 mschenke Added ability to specify font type
*
* </pre>
*
@ -234,8 +235,10 @@ public interface IGraphicsTarget extends IImagingExtension {
IFont.Style[] styles);
/**
* Create a font object from a truetype font
* Create a font object from a truetype font file
*
* @deprecated {@link #initializeFont(File, com.raytheon.uf.viz.core.drawables.IFont.FontType, float, com.raytheon.uf.viz.core.drawables.IFont.Style[])}
* should be used instead
*
* @param fontFile
* the truetype font
@ -245,9 +248,27 @@ public interface IGraphicsTarget extends IImagingExtension {
* the font styles
* @return a prepared font reference
*/
@Deprecated
public abstract IFont initializeFont(File fontFile, float size,
IFont.Style[] styles);
/**
* Create a font object from font file
*
*
* @param fontFile
* the font file
* @param type
* the type of the font file
* @param size
* the size in points
* @param styles
* the font styles
* @return a prepared font reference
*/
public abstract IFont initializeFont(File fontFile, IFont.FontType type,
float size, IFont.Style[] styles);
/**
* Draw a raster to a target, given an extent and an alpha (transparency)
* value. Assumes synchronous operation.

View file

@ -0,0 +1,165 @@
/**
* This software was developed and / or modified by Raytheon Company,
* pursuant to Contract DG133W-05-CQ-1067 with the US Government.
*
* U.S. EXPORT CONTROLLED TECHNICAL DATA
* This software product contains export-restricted data whose
* export/transfer/disclosure is restricted by U.S. law. Dissemination
* to non-U.S. persons whether in the United States or abroad requires
* an export license or other authorization.
*
* Contractor Name: Raytheon Company
* Contractor Address: 6825 Pine Street, Suite 340
* Mail Stop B8
* Omaha, NE 68106
* 402.291.0100
*
* See the AWIPS II Master Rights File ("Master Rights File.pdf") for
* further licensing information.
**/
package com.raytheon.uf.viz.core.drawables;
import java.awt.Font;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* Simple abstract base class for an AWT-based IFont implementations
*
* <pre>
*
* SOFTWARE HISTORY
*
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* Jul 24, 2013 2189 mschenke Initial creation
*
* </pre>
*
* @author mschenke
* @version 1.0
*/
public abstract class AbstractAWTFont implements IFont {
protected Font font;
protected boolean scaleFont;
protected boolean smoothing;
protected AbstractAWTFont(String fontName, float fontSize, Style[] styles) {
this(new Font(fontName, toAwtStyle(styles), (int) fontSize));
}
protected AbstractAWTFont(File fontFile, FontType fontType, float fontSize,
Style[] styles) {
this(createFont(fontFile, fontType, fontSize, styles));
}
protected AbstractAWTFont(Font font) {
this.font = font;
}
@Override
public final String getFontName() {
return font.getName();
}
/*
* (non-Javadoc)
*
* @see com.raytheon.uf.viz.core.drawables.IFont#getStyle()
*/
@Override
public final Style[] getStyle() {
return toVizStyles(font.getStyle());
}
/*
* (non-Javadoc)
*
* @see com.raytheon.uf.viz.core.drawables.IFont#setSmoothing(boolean)
*/
@Override
public final void setSmoothing(boolean smooth) {
this.smoothing = smooth;
}
/*
* (non-Javadoc)
*
* @see com.raytheon.uf.viz.core.drawables.IFont#getSmoothing()
*/
@Override
public final boolean getSmoothing() {
return smoothing;
}
/*
* (non-Javadoc)
*
* @see com.raytheon.uf.viz.core.drawables.IFont#isScaleFont()
*/
@Override
public final boolean isScaleFont() {
return scaleFont;
}
/*
* (non-Javadoc)
*
* @see com.raytheon.uf.viz.core.drawables.IFont#setScaleFont(boolean)
*/
@Override
public final void setScaleFont(boolean scaleFont) {
this.scaleFont = scaleFont;
}
protected static Font createFont(File fontFile, FontType fontType,
float fontSize, Style[] styles) {
try {
return Font.createFont(toAwtFontType(fontType), fontFile)
.deriveFont(fontSize).deriveFont(toAwtStyle(styles));
} catch (Exception e) {
throw new IllegalArgumentException("Bad Font File", e);
}
}
protected static int toAwtFontType(FontType type) {
switch (type) {
case TYPE1:
return Font.TYPE1_FONT;
case TRUETYPE:
default:
return Font.TRUETYPE_FONT;
}
}
protected static int toAwtStyle(Style[] styles) {
int styleInt = Font.PLAIN;
if (styles == null || styles.length == 0) {
return styleInt;
}
for (Style style : styles) {
if (style == Style.BOLD) {
styleInt |= Font.BOLD;
} else if (style == Style.ITALIC) {
styleInt |= Font.ITALIC;
}
}
return styleInt;
}
protected static Style[] toVizStyles(int style) {
List<Style> styles = new ArrayList<Style>();
if ((style & Font.BOLD) != 0) {
styles.add(Style.BOLD);
}
if ((style & Font.ITALIC) != 0) {
styles.add(Style.ITALIC);
}
return styles.toArray(new Style[0]);
}
}

View file

@ -29,7 +29,7 @@ package com.raytheon.uf.viz.core.drawables;
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* May 7, 2007 chammack Initial Creation.
*
* Jul 18, 2013 2189 mschenke Added ability to specify font type
* </pre>
*
* @author chammack
@ -43,6 +43,11 @@ public interface IFont {
BOLD, ITALIC
};
/** Type of font */
public static enum FontType {
TRUETYPE, TYPE1
}
/**
* Return the name of the font
*

View file

@ -23,9 +23,8 @@ import java.awt.Font;
import java.awt.FontFormatException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.raytheon.uf.viz.core.drawables.AbstractAWTFont;
import com.raytheon.uf.viz.core.drawables.IFont;
/**
@ -39,25 +38,20 @@ import com.raytheon.uf.viz.core.drawables.IFont;
*
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* Jun 1, 2012 bsteffen Initial creation
* Jun 1, 2012 bsteffen Initial creation
* Jul 24, 2013 2189 mschenke Refactored to share common awt font code
*
* </pre>
*
* @author bsteffen
* @version 1.0
*/
public class KmlFont implements IFont {
private Font font;
public class KmlFont extends AbstractAWTFont implements IFont {
private float magnification;
private boolean scaleFont;
private boolean smoothing;
public KmlFont(Font font) {
this.font = font;
super(font);
this.magnification = 1.0f;
}
@ -67,7 +61,6 @@ public class KmlFont implements IFont {
public KmlFont(String fontName) {
this(new Font(fontName, Font.PLAIN, 10));
}
public KmlFont(String fontName, float fontSize) {
@ -78,15 +71,10 @@ public class KmlFont implements IFont {
this(new Font(fontName, toAwtStyle(styles), (int) fontSize));
}
public KmlFont(File fontFile, float fontSize, Style[] styles)
throws FontFormatException, IOException {
this(Font.createFont(Font.TRUETYPE_FONT, fontFile).deriveFont(fontSize)
.deriveFont(toAwtStyle(styles)));
}
@Override
public String getFontName() {
return this.font.getFontName();
public KmlFont(File fontFile, FontType fontType, float fontSize,
Style[] styles) throws FontFormatException, IOException {
this(Font.createFont(toAwtFontType(fontType), fontFile)
.deriveFont(fontSize).deriveFont(toAwtStyle(styles)));
}
@Override
@ -94,11 +82,6 @@ public class KmlFont implements IFont {
return this.font.getSize2D();
}
@Override
public Style[] getStyle() {
return toVizStyles(font.getStyle());
}
@Override
public void dispose() {
@ -128,26 +111,6 @@ public class KmlFont implements IFont {
return magnification;
}
@Override
public void setSmoothing(boolean smooth) {
this.smoothing = smooth;
}
@Override
public boolean getSmoothing() {
return smoothing;
}
@Override
public boolean isScaleFont() {
return scaleFont;
}
@Override
public void setScaleFont(boolean scaleFont) {
this.scaleFont = scaleFont;
}
public Font getFont() {
return font;
}
@ -156,30 +119,4 @@ public class KmlFont implements IFont {
this.font = font;
}
private static int toAwtStyle(Style[] styles) {
int styleInt = Font.PLAIN;
if (styles == null || styles.length == 0) {
return styleInt;
}
for (Style style : styles) {
if (style == Style.BOLD) {
styleInt |= Font.BOLD;
} else if (style == Style.ITALIC) {
styleInt |= Font.ITALIC;
}
}
return styleInt;
}
private static Style[] toVizStyles(int style) {
List<Style> styles = new ArrayList<Style>();
if ((style & Font.BOLD) != 0) {
styles.add(Style.BOLD);
}
if ((style & Font.ITALIC) != 0) {
styles.add(Style.ITALIC);
}
return styles.toArray(new Style[0]);
}
}

View file

@ -48,6 +48,7 @@ import com.raytheon.uf.viz.core.IExtent;
import com.raytheon.uf.viz.core.IView;
import com.raytheon.uf.viz.core.data.IRenderedImageCallback;
import com.raytheon.uf.viz.core.drawables.IFont;
import com.raytheon.uf.viz.core.drawables.IFont.FontType;
import com.raytheon.uf.viz.core.drawables.IFont.Style;
import com.raytheon.uf.viz.core.drawables.IImage;
import com.raytheon.uf.viz.core.drawables.IShadedShape;
@ -134,9 +135,10 @@ public class KmlGraphicsTarget extends AbstractGraphicsTarget {
}
@Override
public KmlFont initializeFont(File fontFile, float size, Style[] styles) {
public IFont initializeFont(File fontFile, FontType type, float size,
Style[] styles) {
try {
return new KmlFont(fontFile, size, styles);
return new KmlFont(fontFile, type, size, styles);
} catch (FontFormatException e) {
statusHandler.handle(Priority.PROBLEM, e.getLocalizedMessage(), e);
} catch (IOException e) {

View file

@ -60,6 +60,7 @@ import com.raytheon.uf.viz.core.data.IRenderedImageCallback;
import com.raytheon.uf.viz.core.data.resp.NumericImageData;
import com.raytheon.uf.viz.core.drawables.IDescriptor;
import com.raytheon.uf.viz.core.drawables.IFont;
import com.raytheon.uf.viz.core.drawables.IFont.FontType;
import com.raytheon.uf.viz.core.drawables.IFont.Style;
import com.raytheon.uf.viz.core.drawables.IImage;
import com.raytheon.uf.viz.core.drawables.IShadedShape;
@ -246,8 +247,24 @@ public class DispatchGraphicsTarget extends DispatchingObject<IGraphicsTarget>
* float, com.raytheon.uf.viz.core.drawables.IFont.Style[])
*/
public IFont initializeFont(File fontFile, float size, Style[] styles) {
return new DispatchingFont(wrappedObject.initializeFont(fontFile, size,
styles), getDispatcher(), fontFile);
return initializeFont(fontFile, FontType.TRUETYPE, size, styles);
}
/**
* @param fontFile
* @param type
* @param size
* @param styles
* @return
* @see
* com.raytheon.uf.viz.core.IGraphicsTarget#initializeFont(java.io.File,
* com.raytheon.uf.viz.core.drawables.IFont.FontType float,
* com.raytheon.uf.viz.core.drawables.IFont.Style[])
*/
public IFont initializeFont(File fontFile, FontType type, float size,
Style[] styles) {
return new DispatchingFont(wrappedObject.initializeFont(fontFile, type,
size, styles), getDispatcher(), fontFile);
}
/**

View file

@ -20,9 +20,9 @@
package com.raytheon.viz.core.gl.internal;
import java.awt.Font;
import java.io.File;
import com.raytheon.uf.viz.core.drawables.AbstractAWTFont;
import com.raytheon.uf.viz.core.drawables.IFont;
import com.raytheon.viz.core.gl.IGLFont;
import com.sun.opengl.util.j2d.TextRenderer;
@ -36,6 +36,7 @@ import com.sun.opengl.util.j2d.TextRenderer;
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* May 7, 2007 chammack Initial Creation.
* Jul 24, 2013 2189 mschenke Refactored to share common awt font code
*
* </pre>
*
@ -43,78 +44,33 @@ import com.sun.opengl.util.j2d.TextRenderer;
* @version 1.0
*/
public class GLFont implements IGLFont {
public class GLFont extends AbstractAWTFont implements IGLFont {
private boolean disposed = false;
private String fontName;
private float fontSize;
private float currentFontSize;
private Style[] styles;
private Font font;
private TextRenderer textRenderer;
private boolean smoothing = true;
private File fontFile;
private FontType fontType;
private float magnification = 1.0f;
private boolean scaleFont = true;
public GLFont() {
;
}
public GLFont(File font, float fontSize, Style[] styles) {
try {
this.fontName = font.getName();
this.font = Font.createFont(Font.TRUETYPE_FONT, font).deriveFont(
fontSize);
this.currentFontSize = this.fontSize = fontSize;
this.styles = styles;
if (styles != null && styles.length > 0) {
for (Style style : styles) {
if (style == Style.BOLD) {
this.font = this.font.deriveFont(Font.BOLD);
} else if (style == Style.ITALIC) {
this.font = this.font.deriveFont(Font.ITALIC);
}
}
}
} catch (Exception e) {
e.printStackTrace();
throw new IllegalArgumentException("Bad font file", e);
}
public GLFont(File font, FontType type, float fontSize, Style[] styles) {
super(font, type, fontSize, styles);
this.fontFile = font;
this.fontType = type;
this.fontSize = fontSize;
this.textRenderer = TextRendererCache.getRenderer(this.font);
}
public GLFont(String fontName, float fontSize, Style[] styles) {
this.fontName = fontName;
this.currentFontSize = this.fontSize = fontSize;
this.styles = styles;
int style = Font.PLAIN;
if (styles != null) {
for (Style s : styles) {
if (s == IFont.Style.BOLD) {
style = (Font.BOLD | style);
} else if (s == IFont.Style.ITALIC) {
style = (Font.ITALIC | style);
}
}
}
this.font = new Font(this.fontName, style, (int) fontSize);
super(fontName, fontSize, styles);
this.fontSize = fontSize;
this.textRenderer = TextRendererCache.getRenderer(this.font);
}
@ -127,15 +83,6 @@ public class GLFont implements IGLFont {
disposeInternal();
}
/*
* (non-Javadoc)
*
* @see com.raytheon.viz.core.drawables.IFont#getFontName()
*/
public String getFontName() {
return this.fontName;
}
/*
* (non-Javadoc)
*
@ -145,15 +92,6 @@ public class GLFont implements IGLFont {
return currentFontSize;
}
/*
* (non-Javadoc)
*
* @see com.raytheon.viz.core.drawables.IFont#getStyle()
*/
public Style[] getStyle() {
return this.styles;
}
public TextRenderer getTextRenderer() {
return textRenderer;
}
@ -168,9 +106,9 @@ public class GLFont implements IGLFont {
GLFont newFont = null;
if (this.fontFile != null) {
// File based construction
newFont = new GLFont(this.fontFile, size, styles);
newFont = new GLFont(this.fontFile, fontType, size, getStyle());
} else {
newFont = new GLFont(this.fontName, size, styles);
newFont = new GLFont(getFontName(), size, getStyle());
}
return newFont;
@ -215,46 +153,6 @@ public class GLFont implements IGLFont {
return magnification;
}
/*
* (non-Javadoc)
*
* @see com.raytheon.uf.viz.core.drawables.IFont#getSmoothing()
*/
@Override
public boolean getSmoothing() {
return smoothing;
}
/*
* (non-Javadoc)
*
* @see com.raytheon.uf.viz.core.drawables.IFont#setSmoothing(boolean)
*/
@Override
public void setSmoothing(boolean smoothing) {
this.smoothing = smoothing;
}
/*
* (non-Javadoc)
*
* @see com.raytheon.uf.viz.core.drawables.IFont#isScaleFont()
*/
@Override
public boolean isScaleFont() {
return scaleFont;
}
/*
* (non-Javadoc)
*
* @see com.raytheon.uf.viz.core.drawables.IFont#setScaleFont(boolean)
*/
@Override
public void setScaleFont(boolean scaleFont) {
this.scaleFont = scaleFont;
}
/*
* (non-Javadoc)
*

View file

@ -71,6 +71,7 @@ import com.raytheon.uf.viz.core.data.IColorMapDataRetrievalCallback;
import com.raytheon.uf.viz.core.data.IRenderedImageCallback;
import com.raytheon.uf.viz.core.drawables.IDescriptor;
import com.raytheon.uf.viz.core.drawables.IFont;
import com.raytheon.uf.viz.core.drawables.IFont.FontType;
import com.raytheon.uf.viz.core.drawables.IFont.Style;
import com.raytheon.uf.viz.core.drawables.IImage;
import com.raytheon.uf.viz.core.drawables.IShadedShape;
@ -1224,15 +1225,10 @@ public class GLTarget extends AbstractGraphicsTarget implements IGLTarget {
return new GLImage(imageCallback, GLDefaultImagingExtension.class);
}
/*
* (non-Javadoc)
*
* @see com.raytheon.viz.core.IGraphicsTarget#initializeFont(java.io.File,
* float, com.raytheon.viz.core.drawables.IFont.Style[])
*/
@Override
public IFont initializeFont(File fontFile, float size, Style[] styles) {
return new GLFont(fontFile, size, styles);
public IFont initializeFont(File fontFile, FontType type, float size,
Style[] styles) {
return new GLFont(fontFile, type, size, styles);
}
/*