下面列出了java.awt.Font#getName ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static String fontToString(Font font) {
int style = font.getStyle();
String styleString;
switch (style) {
case 1:
styleString = "Bold";
break;
case 2:
styleString = "Italic";
break;
case 3:
styleString = "BoldItalic";
break;
default:
styleString = "Plain";
break;
}
return font.getName() + "-" + styleString + "-" + font.getSize();
}
@Override
public void setText(String value) {
if (value == null) {
return;
}
Font font = getFont();
Color textColor = getForeground();
value = value.replaceAll("\\n\\r|\\r\\n|\\n|\\r", "<br>"); // NOI18N
value = value.replace("<code>", "<code style=\"font-size: " + font.getSize() + "pt;\">"); // NOI18N
String colorText = "rgb(" + textColor.getRed() + "," + textColor.getGreen() + "," + textColor.getBlue() + ")"; // NOI18N
String newText = "<html><body style=\"color: " + colorText + "; font-size: " + font.getSize() // NOI18N
+ "pt; font-family: " + font.getName() + ";\">" + value + "</body></html>"; // NOI18N
setDocument(getEditorKit().createDefaultDocument()); // Workaround for http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5042872
super.setText(newText);
}
/**
* Instantiate the class
* @param f Font to use.
* @param c Color to use
* @param j Justification
*/
public TextLine(Font f, Color c, int j) {
font = f;
color = c;
justification = j;
if(font==null) {
return;
}
fontname = f.getName();
fontstyle = f.getStyle();
fontsize = f.getSize();
}
private Font createFont(Font attrFont, Font defaultFont) {
if ( !Config.getDefault().isUseFont()) {
return defaultFont;
}
String name = defaultFont.getName();
int size = defaultFont.getSize();
int style = attrFont.getStyle();
return new Font(name, style, size);
}
private String getString(Font font) {
String style = ""; // NOI18N
if (font.isBold()) {
style += "bold"; // NOI18N
}
if (font.isItalic()) {
style += " italic"; // NOI18N
}
else {
style += " plain"; // NOI18N
}
return "[" + font.getName() + ", " + style + ", " + font.getSize() + "]"; // NOI18N
}
private void updateSelectedFont() {
Font currentFont = chooser.getSelectedFont();
String fontFamily =
currentFont == null ? "SansSerif" : currentFont.getName();
int fontSize = currentFont == null ? 11 : currentFont.getSize();
if (fontList.getSelectedIndex() >= 0) {
fontFamily = (String)fontList.getSelectedValue();
}
if (fontSizeField.getText().trim().length() > 0) {
try {
fontSize = Integer.parseInt(fontSizeField.getText().trim());
} catch (Exception e) {
// ignore the NumberFormatException
}
}
Map attributes = new HashMap();
attributes.put(TextAttribute.SIZE, new Float(fontSize));
attributes.put(TextAttribute.FAMILY, fontFamily);
if (boldCheck.isSelected()) {
attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
}
if (italicCheck.isSelected()) {
attributes.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
}
Font font = Font.getFont(attributes);
if (!font.equals(currentFont)) {
chooser.setSelectedFont(font);
previewPanel.setFont(font);
}
}
/**
* Initializes the contents of the dialog from the given font
* object.
*
* @param font the font from which to read the properties.
*/
public void setSelectedFont( Font font) {
if (font == null) {
throw new NullPointerException();
}
this.bold.setSelected(font.isBold());
this.italic.setSelected(font.isItalic());
String fontName = font.getName();
ListModel model = this.fontlist.getModel();
this.fontlist.clearSelection();
for (int i = 0; i < model.getSize(); i++) {
if (fontName.equals(model.getElementAt(i))) {
this.fontlist.setSelectedIndex(i);
break;
}
}
String fontSize = String.valueOf(font.getSize());
model = this.sizelist.getModel();
this.sizelist.clearSelection();
for (int i = 0; i < model.getSize(); i++) {
if (fontSize.equals(model.getElementAt(i))) {
this.sizelist.setSelectedIndex(i);
break;
}
}
}
public void setText(String value) {
txt = value;
Font font = getFont();
Color fgColor = getForeground();
Color bgColor = getBackground();
value = value.replaceAll("\\n\\r|\\r\\n|\\n|\\r", "<br>"); //NOI18N
value = value.replace("<code>", "<code style=\"font-size: " + font.getSize() + "pt;\">"); //NOI18N
String fgText = "rgb(" + fgColor.getRed() + "," + fgColor.getGreen() + "," + fgColor.getBlue() + ")"; //NOI18N
String bgText = isOpaque() ? "rgb(" + bgColor.getRed() + "," + bgColor.getGreen() + "," + bgColor.getBlue() + ")" : null; //NOI18N
String alignText = null;
switch (halign) {
case SwingConstants.CENTER:
alignText = "center"; //NOI18N
break;
case SwingConstants.RIGHT:
case SwingConstants.TRAILING:
alignText = "right"; //NOI18N
break;
}
String bodyFlags = "text=\"" + fgText + "\""; //NOI18N
if (bgText != null) bodyFlags += " bgcolor=\"" + bgText + "\""; //NOI18N
if (alignText != null) bodyFlags += " align=\"" + alignText + "\""; //NOI18N
super.setText("<html><body " + bodyFlags + " style=\"font-size: " + font.getSize() //NOI18N
+ "pt; font-family: " + font.getName() + ";\">" + value + "</body></html>"); //NOI18N
}
/**
* Checks to see if the object is an instance of <code>java.awt.Font</code>,
* and in case it is, it replaces it with the one looked up for in the font extensions.
*/
@Override
protected Object resolveObject(Object obj) throws IOException
{
Font font = (obj instanceof Font) ? (Font)obj : null;
if (font != null)
{
// We use the font.getName() method here because the name field in the java.awt.Font class is the only font name related information that gets serialized,
// along with the size and style. The font.getFontName() and font.getFamily() both return runtime calculated values, which are not accurate in case of AWT fonts
// created at runtime through font extensions (both seem to return 'Dialog').
// For AWT fonts created from font extensions using the Font.createFont(int, InputStream), the name field is set to the same value as the font.getFontName(),
// which is the recommended method to get the name of an AWT font.
String fontName = font.getName();
// We load an instance of an AWT font, even if the specified font name is not available (ignoreMissingFont=true),
// because only third-party visualization packages such as JFreeChart (chart themes) store serialized java.awt.Font objects,
// and they are responsible for the drawing as well.
// Here we rely on the utility method ability to find a font by face name, not only family name. This is because font.getName() above returns an AWT font name,
// not a font family name.
Font newFont = FontUtil.getInstance(jasperReportsContext).getAwtFontFromBundles(fontName, font.getStyle(), font.getSize2D(), null, true);
if (newFont != null)
{
return newFont.deriveFont(font.getAttributes());
}
}
return obj;
}
/** Modify the given font according to the font-mode */
private Font modifyFont(Font f) {
return new Font(
((fontMode & FONT_MODE_APPLY_NAME) != 0) ? font.getName() : f.getName(),
((fontMode & FONT_MODE_APPLY_STYLE) != 0) ? font.getStyle() : f.getStyle(),
((fontMode & FONT_MODE_APPLY_SIZE) != 0) ? font.getSize() : f.getSize()
);
}
private void initFontMap(Font f)
{
if (f == null)
{
this.widthMap = null;
this.heightMap = null;
return;
}
this.widthMap = new int[MAX_POINT*2];
this.heightMap = new int[MAX_POINT*2];
this.fontName = f.getName();
this.fontStyle = f.getStyle();
for (int i = 4, j = 0, tw = 0, th = 0; i < MAX_POINT; i++)
{
//Font workFont = f.deriveFont((float)i);
Font workFont = new Font(this.fontName, this.fontStyle, i);
FontMetrics metrics = terminal.getFontMetrics(workFont);
int w = metrics.charWidth('W');
int h = metrics.getHeight();
//int h = metrics.getAscent() + metrics.getDescent();
this.widthMap[j] = w;
this.widthMap[j+1] = i;
this.heightMap[j] = h;
this.heightMap[j+1] = i;
if ( (tw == w) && (th == h) )
break;
tw = w;
th = h;
j += 2;
}
}
/**
* Converts an object to an attribute value.
*
* @param o
* the object.
* @return the attribute value.
* @throws BeanException
* if there was an error during the conversion.
*/
public String toAttributeValue( final Object o ) throws BeanException {
if ( o == null ) {
throw new NullPointerException();
}
if ( o instanceof Font == false ) {
throw new BeanException( "Failed to convert object of type " + o.getClass() + ": Not a Font." );
}
final Font font = (Font) o;
final int fontSize = font.getSize();
final String fontName = font.getName();
final int fontStyle = font.getStyle();
return fontName + '-' + styleToString( fontStyle ) + '-' + fontSize;
}
protected Object configureValue(Object value) {
if (value instanceof Font) {
Font font = (Font)value;
if ("MS Sans Serif".equals(font.getName())) {
int size = font.getSize();
// 4950968: Workaround to mimic the way Windows maps the default
// font size of 6 pts to the smallest available bitmap font size.
// This happens mostly on Win 98/Me & NT.
int dpi;
try {
dpi = Toolkit.getDefaultToolkit().getScreenResolution();
} catch (HeadlessException ex) {
dpi = 96;
}
if (Math.round(size * 72F / dpi) < 8) {
size = Math.round(8 * dpi / 72F);
}
Font msFont = new FontUIResource("Microsoft Sans Serif",
font.getStyle(), size);
if (msFont.getName() != null &&
msFont.getName().equals(msFont.getFamily())) {
font = msFont;
} else if (size != font.getSize()) {
font = new FontUIResource("MS Sans Serif",
font.getStyle(), size);
}
}
if (FontUtilities.fontSupportsDefaultEncoding(font)) {
if (!(font instanceof UIResource)) {
font = new FontUIResource(font);
}
}
else {
font = FontUtilities.getCompositeFontUIResource(font);
}
return font;
}
return super.configureValue(value);
}
/**
* Retrieves the fake font details for a given font.
*
* @param font
* the font to lookup.
* @return the fake font.
*/
public static synchronized FontDetails getFontDetails(Font font) {
// If we haven't already identified out font metrics file,
// figure out which one to use and load it
if (fontMetricsProps == null) {
try {
fontMetricsProps = loadMetrics();
} catch (IOException e) {
throw new RuntimeException("Could not load font metrics", e);
}
}
// Grab the base name of the font they've asked about
String fontName = font.getName();
// Some fonts support plain/bold/italic/bolditalic variants
// Others have different font instances for bold etc
// (eg font.dialog.plain.* vs font.Californian FB Bold.*)
String fontStyle = "";
if (font.isPlain()) {
fontStyle += "plain";
}
if (font.isBold()) {
fontStyle += "bold";
}
if (font.isItalic()) {
fontStyle += "italic";
}
// Do we have a definition for this font with just the name?
// If not, check with the font style added
String fontHeight = FontDetails.buildFontHeightProperty(fontName);
String styleHeight = FontDetails.buildFontHeightProperty(fontName + "." + fontStyle);
if (fontMetricsProps.get(fontHeight) == null
&& fontMetricsProps.get(styleHeight) != null) {
// Need to add on the style to the font name
fontName += "." + fontStyle;
}
// Get the details on this font
FontDetails fontDetails = fontDetailsMap.get(fontName);
if (fontDetails == null) {
fontDetails = FontDetails.create(fontName, fontMetricsProps);
fontDetailsMap.put(fontName, fontDetails);
}
return fontDetails;
}
protected Object configureValue(Object value) {
if (value instanceof Font) {
Font font = (Font)value;
if ("MS Sans Serif".equals(font.getName())) {
int size = font.getSize();
// 4950968: Workaround to mimic the way Windows maps the default
// font size of 6 pts to the smallest available bitmap font size.
// This happens mostly on Win 98/Me & NT.
int dpi;
try {
dpi = Toolkit.getDefaultToolkit().getScreenResolution();
} catch (HeadlessException ex) {
dpi = 96;
}
if (Math.round(size * 72F / dpi) < 8) {
size = Math.round(8 * dpi / 72F);
}
Font msFont = new FontUIResource("Microsoft Sans Serif",
font.getStyle(), size);
if (msFont.getName() != null &&
msFont.getName().equals(msFont.getFamily())) {
font = msFont;
} else if (size != font.getSize()) {
font = new FontUIResource("MS Sans Serif",
font.getStyle(), size);
}
}
if (FontUtilities.fontSupportsDefaultEncoding(font)) {
if (!(font instanceof UIResource)) {
font = new FontUIResource(font);
}
}
else {
font = FontUtilities.getCompositeFontUIResource(font);
}
return font;
}
return super.configureValue(value);
}
protected Object configureValue(Object value) {
if (value instanceof Font) {
Font font = (Font)value;
if ("MS Sans Serif".equals(font.getName())) {
int size = font.getSize();
// 4950968: Workaround to mimic the way Windows maps the default
// font size of 6 pts to the smallest available bitmap font size.
// This happens mostly on Win 98/Me & NT.
int dpi;
try {
dpi = Toolkit.getDefaultToolkit().getScreenResolution();
} catch (HeadlessException ex) {
dpi = 96;
}
if (Math.round(size * 72F / dpi) < 8) {
size = Math.round(8 * dpi / 72F);
}
Font msFont = new FontUIResource("Microsoft Sans Serif",
font.getStyle(), size);
if (msFont.getName() != null &&
msFont.getName().equals(msFont.getFamily())) {
font = msFont;
} else if (size != font.getSize()) {
font = new FontUIResource("MS Sans Serif",
font.getStyle(), size);
}
}
if (FontUtilities.fontSupportsDefaultEncoding(font)) {
if (!(font instanceof UIResource)) {
font = new FontUIResource(font);
}
}
else {
font = FontUtilities.getCompositeFontUIResource(font);
}
return font;
}
return super.configureValue(value);
}
public static Font getDefaultFont() {
Font f = UIManager.getDefaults().getFont("TextField.font");
return new Font(f.getName(), Font.BOLD, f.getSize());
}
protected Object configureValue(Object value) {
if (value instanceof Font) {
Font font = (Font)value;
if ("MS Sans Serif".equals(font.getName())) {
int size = font.getSize();
// 4950968: Workaround to mimic the way Windows maps the default
// font size of 6 pts to the smallest available bitmap font size.
// This happens mostly on Win 98/Me & NT.
int dpi;
try {
dpi = Toolkit.getDefaultToolkit().getScreenResolution();
} catch (HeadlessException ex) {
dpi = 96;
}
if (Math.round(size * 72F / dpi) < 8) {
size = Math.round(8 * dpi / 72F);
}
Font msFont = new FontUIResource("Microsoft Sans Serif",
font.getStyle(), size);
if (msFont.getName() != null &&
msFont.getName().equals(msFont.getFamily())) {
font = msFont;
} else if (size != font.getSize()) {
font = new FontUIResource("MS Sans Serif",
font.getStyle(), size);
}
}
if (FontUtilities.fontSupportsDefaultEncoding(font)) {
if (!(font instanceof UIResource)) {
font = new FontUIResource(font);
}
}
else {
font = FontUtilities.getCompositeFontUIResource(font);
}
return font;
}
return super.configureValue(value);
}
public void setFontSize(int s) {
componentFont = new Font(componentFont.getName(), componentFont.getStyle(), s);
}
public static Font CloneFont(Font origem) {
return new Font(origem.getName(), origem.getStyle(), origem.getSize());
}