下面列出了java.awt.Font#isItalic ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public boolean handle(TextTag textTag, FontTag font, char character) {
String fontName = font.getFontNameIntag();
if (!FontTag.getInstalledFontsByFamily().containsKey(fontName)) {
return false;
}
Map<String, Font> faces = FontTag.getInstalledFontsByFamily().get(fontName);
Font f = null;
for (String face : faces.keySet()) {
Font ff = faces.get(face);
if (ff.isBold() == font.isBold() && ff.isItalic() == font.isItalic()) {
f = ff;
break;
}
}
if (f == null) {
f = faces.get(faces.keySet().iterator().next());
}
if (!f.canDisplay(character)) {
return false;
}
font.addCharacter(character, f);
return true;
}
private static String toString(IdeOption o, Object obj) {
switch (o.type) {
case BOOL:
return obj.toString();
case COLOR:
return Integer.toString(((Color) obj).getRGB());
case FILE:
return ((File) obj).toURI().toString();
case FONT:
Font f = (Font) obj;
String s = "";
if (f.isBold()) {
s += "BOLD";
}
if (f.isItalic()) {
s += "ITALIC";
}
return f.getName() + " " + s + " " + f.getSize();
case LF:
return obj.toString();
case NAT:
return obj.toString();
default:
return Util.anomaly();
}
}
static void printFont(PrintWriter html, Font font) {
String style = "";
if (font.isBold() && font.isItalic()) {
style = "Bold & Italic";
} else if (font.isBold()) {
style = "Bold";
} else if (font.isItalic()) {
style = "Italic";
}
html.println("<td>Font: " + font.getFamily() + " " + font.getSize() + " " + style + "</td>");
int w = 300, h = 30;
BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = img.createGraphics();
Composite old = g2.getComposite();
g2.setComposite(AlphaComposite.Clear);
g2.fillRect(0, 0, w, h);
g2.setComposite(old);
g2.setColor(Color.BLACK);
g2.setFont(font);
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2.drawString("The quick brown fox jumps over the lazy dog.", 5, 20);
g2.dispose();
html.println("<td>" + saveImage(img) + "</td>");
}
private void writeStyle( Font font ) throws XMLStreamException
{
StringBuilder style = new StringBuilder();
style.append( "font-family:" ).append( font.getName() ).append( ';' );
style.append( " font-size:" ).append( font.getSize() ).append( "px;" );
if ( font.isBold() )
{
style.append( " font-weight:bold;" );
}
else
{
style.append( " font-weight:normal;" );
}
if ( font.isItalic() )
{
style.append( " font-style:italic;" );
}
else
{
style.append( " font-style:normal;" );
}
style.append( " stroke:none;" );
attribute( "style", style.toString() );
}
@Nonnull
private static String font2style(@Nonnull final Font font) {
final StringBuilder result = new StringBuilder();
final String fontStyle = font.isItalic() ? "italic" : "normal";
final String fontWeight = font.isBold() ? "bold" : "normal";
final String fontSize = DOUBLE.format(font.getSize2D()) + "px";
final String fontFamily = fontFamilyToSVG(font);
result.append("font-family: ").append(fontFamily).append(';').append(NEXT_LINE);
result.append("font-size: ").append(fontSize).append(';').append(NEXT_LINE);
result.append("font-style: ").append(fontStyle).append(';').append(NEXT_LINE);
result.append("font-weight: ").append(fontWeight).append(';').append(NEXT_LINE);
return result.toString();
}
private void dumpFont( PrintWriter out, Font font ) {
String strStyle = font.isBold()
? font.isItalic() ? "bolditalic" : "bold"
: font.isItalic() ? "italic" : "plain";
out.printf( "%s %s %d %s",
font.getName(), strStyle, font.getSize(),
dumpClass( font ) );
}
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 updateFontButton(@Nonnull final JButton button, @Nonnull final Font font) {
final String strStyle;
if (font.isBold()) {
strStyle = font.isItalic() ? "bolditalic" : "bold"; //NOI18N
} else {
strStyle = font.isItalic() ? "italic" : "plain"; //NOI18N
}
button.setText(font.getName() + ", " + strStyle + ", " + font.getSize()); //NOI18N
}
/**
* Converts the value to string using {@link Strings#toString(Object)}
* method and then stores it.
* There is get methods for values that are a String, an Integer, a Boolean,
* a Font, a List of String and a Map of String*String.
*/
@Override
public Object put(Object key, Object value) {
if(value instanceof Font){
Font font = (Font)value;
String family = font.getFamily();
int size = font.getSize();
boolean italic = font.isItalic();
boolean bold = font.isBold();
value = family + "#" + size + "#" + italic + "#" + bold;
}
return super.put(key.toString(), Strings.toString(value));
}
public static String convertFontToCss(Font font) {
StringBuilder sb = new StringBuilder();
sb.append("font-family:").append(font.getFamily()).append(",Sans-serif;");
if(font.isItalic()) {
sb.append("font-style:italic;");
}
if(font.isBold()) {
sb.append("font-weight:bold;");
}
sb.append("font-size:" + font.getSize() + "px;");
return sb.toString();
}
/**
* Sets the font to be used for drawing text.
*
* @param font the font ({@code null} is permitted but ignored).
*
* @see #getFont()
*/
@Override
public void setFont(Font font) {
if (font == null) {
return;
}
this.font = font;
FontWeight weight = font.isBold() ? FontWeight.BOLD : FontWeight.NORMAL;
FontPosture posture = font.isItalic()
? FontPosture.ITALIC : FontPosture.REGULAR;
this.gc.setFont(javafx.scene.text.Font.font(font.getFamily(),
weight, posture, font.getSize()));
}
private String getFontStyle(final Font font) {
if (font.isBold() && font.isItalic()) {
return "bold-italic";
}
if (font.isBold()) {
return "bold";
}
if (font.isItalic()) {
return "italic";
}
return "plain";
}
/**
* Sets the font to be used for drawing text.
*
* @param font the font ({@code null} is permitted but ignored).
*
* @see #getFont()
*/
@Override
public void setFont(Font font) {
if (font == null) {
return;
}
this.font = font;
FontWeight weight = font.isBold() ? FontWeight.BOLD : FontWeight.NORMAL;
FontPosture posture = font.isItalic()
? FontPosture.ITALIC : FontPosture.REGULAR;
this.gc.setFont(javafx.scene.text.Font.font(font.getFamily(),
weight, posture, font.getSize()));
}
private String getFontStyle() {
Font font = g.getFont();
if (font.isPlain()) {
return "Plain";
} else if (font.isItalic() && font.isBold()) {
return "ItalicBold";
} else if (font.isBold()) {
return "Bold";
} else if (font.isItalic()) {
return "Italic";
}
return "";
}
/**
* Select the style in the style component
*
* @param font Font where to get the style to search
*/
private void SelectInStyleList(Font font) {
int index = 0;
if (font.isPlain())
index = 0;
else if (font.isBold() && !font.isItalic())
index = 1;
else if (!font.isBold() && font.isItalic())
index = 2;
else if (font.isBold() && font.isItalic())
index = 3;
jStyleList.setSelectedIndex(index);
jStyleList.ensureIndexIsVisible(index);
}
/**
* Compute a most appropriate width of the given text layout.
*/
public static float getWidth(TextLayout textLayout, String textLayoutText, Font font) {
// For italic fonts the textLayout.getAdvance() includes some extra horizontal space.
// On the other hand index2X() for TL.getCharacterCount() is width along baseline
// so when TL ends with e.g. 'd' char the end of 'd' char is cut off.
float width;
int tlLen = textLayoutText.length();
if (!font.isItalic() ||
tlLen == 0 ||
Character.isWhitespace(textLayoutText.charAt(tlLen - 1)) ||
Bidi.requiresBidi(textLayoutText.toCharArray(), 0, textLayoutText.length()))
{
width = textLayout.getAdvance();
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("TLUtils.getWidth(\"" + CharSequenceUtilities.debugText(textLayoutText) + // NOI18N
"\"): Using TL.getAdvance()=" + width + // NOI18N
// textLayoutDump(textLayout) +
'\n');
}
} else {
// Compute pixel bounds (with frc being null - means use textLayout's frc; and with default bounds)
Rectangle pixelBounds = textLayout.getPixelBounds(null, 0, 0);
width = (float) pixelBounds.getMaxX();
// On Mac OS X with retina displays the TL.getPixelBounds() give incorrect results. Luckily
// TL.getAdvance() gives a correct result in that case.
// Therefore use a minimum of both values (on all platforms).
float tlAdvance = textLayout.getAdvance();
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("TLUtils.getWidth(\"" + CharSequenceUtilities.debugText(textLayoutText) + // NOI18N
"\"): Using minimum of TL.getPixelBounds().getMaxX()=" + width + // NOI18N
" or TL.getAdvance()=" + tlAdvance +
textLayoutDump(textLayout) +
'\n');
}
width = Math.min(width, tlAdvance);
}
// For RTL text the hit-info of the first char is above the hit-info of ending char.
// However textLayout.isLeftToRight() returns true in case of mixture of LTR and RTL text
// in a single textLayout.
// Ceil the width to avoid rendering artifacts.
width = (float) Math.ceil(width);
return width;
}
private String createStyle (String element, String selector, Font font, Color fg, Color bg, boolean useDefaults) {
StringBuffer sb = new StringBuffer();
if (element != null) {
sb.append (element);
sb.append (WS);
}
if (selector != null) {
sb.append (DOT);
sb.append (selector);
sb.append (WS);
}
sb.append (ST_BEGIN);
boolean first = true;
if ((!useDefaults || !fg.equals(getDefaultColor())) && fg != null) {
sb.append (ST_COLOR);
sb.append (getHtmlColor(fg));
first = false;
}
if ((!useDefaults || !bg.equals (getDefaultBackgroundColor())) && bg != null) {
if (!first) {
sb.append (ST_SEPARATOR);
}
sb.append (ST_BGCOLOR);
sb.append (getHtmlColor(bg));
first = false;
}
if ((!useDefaults || !font.equals (getDefaultFont())) && font != null) {
if (!first) {
sb.append (ST_SEPARATOR);
}
sb.append (ST_FONT_FAMILY);
switch(font.getFamily()) {
case Font.MONOSPACED:
sb.append (FF_MONOSPACE);
break;
case Font.SERIF:
sb.append (FF_SERIF);
break;
case Font.SANS_SERIF:
sb.append (FF_SANSSERIF);
break;
case Font.DIALOG:
sb.append (FF_SANSSERIF);
break;
case Font.DIALOG_INPUT:
sb.append (FF_MONOSPACE);
break;
default:
sb.append (font.getFamily()); //TODO: Locale should go here
}
if (font.isBold()) {
sb.append (ST_SEPARATOR);
sb.append (ST_BOLD);
}
if (font.isItalic()) {
sb.append (ST_SEPARATOR);
sb.append (ST_ITALIC);
}
Font df = getDefaultFont();
if (df != null && df.getSize() != font.getSize()) {
sb.append (ST_SEPARATOR);
sb.append (ST_SIZE);
sb.append (String.valueOf(font.getSize()));
}
}
sb.append (ST_END);
return sb.toString();
}
/**
* 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;
}
/**
* Returns a BaseFont which can be used to represent the given AWT Font
*
* @param font the font to be converted
* @return a BaseFont which has similar properties to the provided Font
*/
public BaseFont awtToPdf(Font font) {
try {
BaseFontParameters p = getBaseFontParameters(font.getFontName());
if (p != null)
return BaseFont.createFont(p.fontName, p.encoding, p.embedded, p.cached, p.ttfAfm, p.pfb);
String fontKey = null;
String logicalName = font.getName();
if (logicalName.equalsIgnoreCase("DialogInput") || logicalName.equalsIgnoreCase("Monospaced") || logicalName.equalsIgnoreCase("Courier")) {
if (font.isItalic()) {
if (font.isBold()) {
fontKey = BaseFont.COURIER_BOLDOBLIQUE;
} else {
fontKey = BaseFont.COURIER_OBLIQUE;
}
} else {
if (font.isBold()) {
fontKey = BaseFont.COURIER_BOLD;
} else {
fontKey = BaseFont.COURIER;
}
}
} else if (logicalName.equalsIgnoreCase("Serif") || logicalName.equalsIgnoreCase("TimesRoman")) {
if (font.isItalic()) {
if (font.isBold()) {
fontKey = BaseFont.TIMES_BOLDITALIC;
} else {
fontKey = BaseFont.TIMES_ITALIC;
}
} else {
if (font.isBold()) {
fontKey = BaseFont.TIMES_BOLD;
} else {
fontKey = BaseFont.TIMES_ROMAN;
}
}
} else { // default, this catches Dialog and SansSerif
if (font.isItalic()) {
if (font.isBold()) {
fontKey = BaseFont.HELVETICA_BOLDOBLIQUE;
} else {
fontKey = BaseFont.HELVETICA_OBLIQUE;
}
} else {
if (font.isBold()) {
fontKey = BaseFont.HELVETICA_BOLD;
} else {
fontKey = BaseFont.HELVETICA;
}
}
}
return BaseFont.createFont(fontKey, BaseFont.CP1252, false);
}
catch (Exception e) {
throw new ExceptionConverter(e);
}
}
public static PDType1Font getPDFont(Font javaFont) {
switch (javaFont.getFamily()) {
case Font.SERIF:
if (javaFont.isPlain()) {
return PDType1Font.TIMES_ROMAN;
} else if (javaFont.isBold()) {
if (javaFont.isItalic()) {
return PDType1Font.TIMES_BOLD_ITALIC;
} else {
return PDType1Font.TIMES_BOLD;
}
} else {
return PDType1Font.TIMES_ITALIC;
}
case Font.SANS_SERIF:
if (javaFont.isPlain()) {
return PDType1Font.HELVETICA;
} else if (javaFont.isBold()) {
if (javaFont.isItalic()) {
return PDType1Font.HELVETICA_BOLD_OBLIQUE;
} else {
return PDType1Font.HELVETICA_BOLD;
}
} else {
return PDType1Font.HELVETICA_OBLIQUE;
}
case Font.MONOSPACED:
if (javaFont.isPlain()) {
return PDType1Font.COURIER;
} else if (javaFont.isBold()) {
if (javaFont.isItalic()) {
return PDType1Font.COURIER_BOLD_OBLIQUE;
} else {
return PDType1Font.COURIER_BOLD;
}
} else {
return PDType1Font.COURIER_OBLIQUE;
}
case Font.DIALOG:
case Font.DIALOG_INPUT:
return PDType1Font.SYMBOL;
default:
throw new DSSException("The font is not supported! Please use DSSFileFont implementation for custom fonts.");
}
}