下面列出了java.awt.Font#hasLayoutAttributes ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Constructs a <code>TextLayout</code> from a <code>String</code>
* and a {@link Font}. All the text is styled using the specified
* <code>Font</code>.
* <p>
* The <code>String</code> must specify a single paragraph of text,
* because an entire paragraph is required for the bidirectional
* algorithm.
* @param string the text to display
* @param font a <code>Font</code> used to style the text
* @param frc contains information about a graphics device which is needed
* to measure the text correctly.
* Text measurements can vary slightly depending on the
* device resolution, and attributes such as antialiasing. This
* parameter does not specify a translation between the
* <code>TextLayout</code> and user space.
*/
public TextLayout(String string, Font font, FontRenderContext frc) {
if (font == null) {
throw new IllegalArgumentException("Null font passed to TextLayout constructor.");
}
if (string == null) {
throw new IllegalArgumentException("Null string passed to TextLayout constructor.");
}
if (string.length() == 0) {
throw new IllegalArgumentException("Zero length string passed to TextLayout constructor.");
}
Map<? extends Attribute, ?> attributes = null;
if (font.hasLayoutAttributes()) {
attributes = font.getAttributes();
}
char[] text = string.toCharArray();
if (sameBaselineUpTo(font, text, 0, text.length) == text.length) {
fastInit(text, font, attributes, frc);
} else {
AttributedString as = attributes == null
? new AttributedString(string)
: new AttributedString(string, attributes);
as.addAttribute(TextAttribute.FONT, font);
standardInit(as.getIterator(), text, frc);
}
}
public AffineTransform getBaselineTransform() {
Font font = source.getFont();
if (font.hasLayoutAttributes()) {
return AttributeValues.getBaselineTransform(font.getAttributes());
}
return null;
}
/**
* Constructs a <code>TextLayout</code> from a <code>String</code>
* and a {@link Font}. All the text is styled using the specified
* <code>Font</code>.
* <p>
* The <code>String</code> must specify a single paragraph of text,
* because an entire paragraph is required for the bidirectional
* algorithm.
* @param string the text to display
* @param font a <code>Font</code> used to style the text
* @param frc contains information about a graphics device which is needed
* to measure the text correctly.
* Text measurements can vary slightly depending on the
* device resolution, and attributes such as antialiasing. This
* parameter does not specify a translation between the
* <code>TextLayout</code> and user space.
*/
public TextLayout(String string, Font font, FontRenderContext frc) {
if (font == null) {
throw new IllegalArgumentException("Null font passed to TextLayout constructor.");
}
if (string == null) {
throw new IllegalArgumentException("Null string passed to TextLayout constructor.");
}
if (string.length() == 0) {
throw new IllegalArgumentException("Zero length string passed to TextLayout constructor.");
}
Map<? extends Attribute, ?> attributes = null;
if (font.hasLayoutAttributes()) {
attributes = font.getAttributes();
}
char[] text = string.toCharArray();
if (sameBaselineUpTo(font, text, 0, text.length) == text.length) {
fastInit(text, font, attributes, frc);
} else {
AttributedString as = attributes == null
? new AttributedString(string)
: new AttributedString(string, attributes);
as.addAttribute(TextAttribute.FONT, font);
standardInit(as.getIterator(), text, frc);
}
}
private float getTracking(Font font) {
if (font.hasLayoutAttributes()) {
AttributeValues values = ((AttributeMap)font.getAttributes()).getValues();
return values.getTracking();
}
return 0;
}
/**
* Constructs a <code>TextLayout</code> from a <code>String</code>
* and a {@link Font}. All the text is styled using the specified
* <code>Font</code>.
* <p>
* The <code>String</code> must specify a single paragraph of text,
* because an entire paragraph is required for the bidirectional
* algorithm.
* @param string the text to display
* @param font a <code>Font</code> used to style the text
* @param frc contains information about a graphics device which is needed
* to measure the text correctly.
* Text measurements can vary slightly depending on the
* device resolution, and attributes such as antialiasing. This
* parameter does not specify a translation between the
* <code>TextLayout</code> and user space.
*/
public TextLayout(String string, Font font, FontRenderContext frc) {
if (font == null) {
throw new IllegalArgumentException("Null font passed to TextLayout constructor.");
}
if (string == null) {
throw new IllegalArgumentException("Null string passed to TextLayout constructor.");
}
if (string.length() == 0) {
throw new IllegalArgumentException("Zero length string passed to TextLayout constructor.");
}
Map<? extends Attribute, ?> attributes = null;
if (font.hasLayoutAttributes()) {
attributes = font.getAttributes();
}
char[] text = string.toCharArray();
if (sameBaselineUpTo(font, text, 0, text.length) == text.length) {
fastInit(text, font, attributes, frc);
} else {
AttributedString as = attributes == null
? new AttributedString(string)
: new AttributedString(string, attributes);
as.addAttribute(TextAttribute.FONT, font);
standardInit(as.getIterator(), text, frc);
}
}
public AffineTransform getBaselineTransform() {
Font font = source.getFont();
if (font.hasLayoutAttributes()) {
return AttributeValues.getBaselineTransform(font.getAttributes());
}
return null;
}
private float getTracking(Font font) {
if (font.hasLayoutAttributes()) {
AttributeValues values = ((AttributeMap)font.getAttributes()).getValues();
return values.getTracking();
}
return 0;
}
public AffineTransform getBaselineTransform() {
Font font = source.getFont();
if (font.hasLayoutAttributes()) {
return AttributeValues.getBaselineTransform(font.getAttributes());
}
return null;
}
/**
* Constructs a <code>TextLayout</code> from a <code>String</code>
* and a {@link Font}. All the text is styled using the specified
* <code>Font</code>.
* <p>
* The <code>String</code> must specify a single paragraph of text,
* because an entire paragraph is required for the bidirectional
* algorithm.
* @param string the text to display
* @param font a <code>Font</code> used to style the text
* @param frc contains information about a graphics device which is needed
* to measure the text correctly.
* Text measurements can vary slightly depending on the
* device resolution, and attributes such as antialiasing. This
* parameter does not specify a translation between the
* <code>TextLayout</code> and user space.
*/
public TextLayout(String string, Font font, FontRenderContext frc) {
if (font == null) {
throw new IllegalArgumentException("Null font passed to TextLayout constructor.");
}
if (string == null) {
throw new IllegalArgumentException("Null string passed to TextLayout constructor.");
}
if (string.length() == 0) {
throw new IllegalArgumentException("Zero length string passed to TextLayout constructor.");
}
Map<? extends Attribute, ?> attributes = null;
if (font.hasLayoutAttributes()) {
attributes = font.getAttributes();
}
char[] text = string.toCharArray();
if (sameBaselineUpTo(font, text, 0, text.length) == text.length) {
fastInit(text, font, attributes, frc);
} else {
AttributedString as = attributes == null
? new AttributedString(string)
: new AttributedString(string, attributes);
as.addAttribute(TextAttribute.FONT, font);
standardInit(as.getIterator(), text, frc);
}
}
private float getTracking(Font font) {
if (font.hasLayoutAttributes()) {
AttributeValues values = ((AttributeMap)font.getAttributes()).getValues();
return values.getTracking();
}
return 0;
}
private float getTracking(Font font) {
if (font.hasLayoutAttributes()) {
AttributeValues values = ((AttributeMap)font.getAttributes()).getValues();
return values.getTracking();
}
return 0;
}
/**
* Constructs a <code>TextLayout</code> from a <code>String</code>
* and a {@link Font}. All the text is styled using the specified
* <code>Font</code>.
* <p>
* The <code>String</code> must specify a single paragraph of text,
* because an entire paragraph is required for the bidirectional
* algorithm.
* @param string the text to display
* @param font a <code>Font</code> used to style the text
* @param frc contains information about a graphics device which is needed
* to measure the text correctly.
* Text measurements can vary slightly depending on the
* device resolution, and attributes such as antialiasing. This
* parameter does not specify a translation between the
* <code>TextLayout</code> and user space.
*/
public TextLayout(String string, Font font, FontRenderContext frc) {
if (font == null) {
throw new IllegalArgumentException("Null font passed to TextLayout constructor.");
}
if (string == null) {
throw new IllegalArgumentException("Null string passed to TextLayout constructor.");
}
if (string.length() == 0) {
throw new IllegalArgumentException("Zero length string passed to TextLayout constructor.");
}
Map<? extends Attribute, ?> attributes = null;
if (font.hasLayoutAttributes()) {
attributes = font.getAttributes();
}
char[] text = string.toCharArray();
if (sameBaselineUpTo(font, text, 0, text.length) == text.length) {
fastInit(text, font, attributes, frc);
} else {
AttributedString as = attributes == null
? new AttributedString(string)
: new AttributedString(string, attributes);
as.addAttribute(TextAttribute.FONT, font);
standardInit(as.getIterator(), text, frc);
}
}
/**
* Constructs a <code>TextLayout</code> from a <code>String</code>
* and a {@link Font}. All the text is styled using the specified
* <code>Font</code>.
* <p>
* The <code>String</code> must specify a single paragraph of text,
* because an entire paragraph is required for the bidirectional
* algorithm.
* @param string the text to display
* @param font a <code>Font</code> used to style the text
* @param frc contains information about a graphics device which is needed
* to measure the text correctly.
* Text measurements can vary slightly depending on the
* device resolution, and attributes such as antialiasing. This
* parameter does not specify a translation between the
* <code>TextLayout</code> and user space.
*/
public TextLayout(String string, Font font, FontRenderContext frc) {
if (font == null) {
throw new IllegalArgumentException("Null font passed to TextLayout constructor.");
}
if (string == null) {
throw new IllegalArgumentException("Null string passed to TextLayout constructor.");
}
if (string.length() == 0) {
throw new IllegalArgumentException("Zero length string passed to TextLayout constructor.");
}
Map<? extends Attribute, ?> attributes = null;
if (font.hasLayoutAttributes()) {
attributes = font.getAttributes();
}
char[] text = string.toCharArray();
if (sameBaselineUpTo(font, text, 0, text.length) == text.length) {
fastInit(text, font, attributes, frc);
} else {
AttributedString as = attributes == null
? new AttributedString(string)
: new AttributedString(string, attributes);
as.addAttribute(TextAttribute.FONT, font);
standardInit(as.getIterator(), text, frc);
}
}
public AffineTransform getBaselineTransform() {
Font font = source.getFont();
if (font.hasLayoutAttributes()) {
return AttributeValues.getBaselineTransform(font.getAttributes());
}
return null;
}
private float getTracking(Font font) {
if (font.hasLayoutAttributes()) {
AttributeValues values = ((AttributeMap)font.getAttributes()).getValues();
return values.getTracking();
}
return 0;
}
public AffineTransform getBaselineTransform() {
Font font = source.getFont();
if (font.hasLayoutAttributes()) {
return AttributeValues.getBaselineTransform(font.getAttributes());
}
return null;
}
/**
* Constructs a <code>TextLayout</code> from a <code>String</code>
* and a {@link Font}. All the text is styled using the specified
* <code>Font</code>.
* <p>
* The <code>String</code> must specify a single paragraph of text,
* because an entire paragraph is required for the bidirectional
* algorithm.
* @param string the text to display
* @param font a <code>Font</code> used to style the text
* @param frc contains information about a graphics device which is needed
* to measure the text correctly.
* Text measurements can vary slightly depending on the
* device resolution, and attributes such as antialiasing. This
* parameter does not specify a translation between the
* <code>TextLayout</code> and user space.
*/
public TextLayout(String string, Font font, FontRenderContext frc) {
if (font == null) {
throw new IllegalArgumentException("Null font passed to TextLayout constructor.");
}
if (string == null) {
throw new IllegalArgumentException("Null string passed to TextLayout constructor.");
}
if (string.length() == 0) {
throw new IllegalArgumentException("Zero length string passed to TextLayout constructor.");
}
Map<? extends Attribute, ?> attributes = null;
if (font.hasLayoutAttributes()) {
attributes = font.getAttributes();
}
char[] text = string.toCharArray();
if (sameBaselineUpTo(font, text, 0, text.length) == text.length) {
fastInit(text, font, attributes, frc);
} else {
AttributedString as = attributes == null
? new AttributedString(string)
: new AttributedString(string, attributes);
as.addAttribute(TextAttribute.FONT, font);
standardInit(as.getIterator(), text, frc);
}
}
/**
* Constructs a <code>TextLayout</code> from a <code>String</code>
* and a {@link Font}. All the text is styled using the specified
* <code>Font</code>.
* <p>
* The <code>String</code> must specify a single paragraph of text,
* because an entire paragraph is required for the bidirectional
* algorithm.
* @param string the text to display
* @param font a <code>Font</code> used to style the text
* @param frc contains information about a graphics device which is needed
* to measure the text correctly.
* Text measurements can vary slightly depending on the
* device resolution, and attributes such as antialiasing. This
* parameter does not specify a translation between the
* <code>TextLayout</code> and user space.
*/
public TextLayout(String string, Font font, FontRenderContext frc) {
if (font == null) {
throw new IllegalArgumentException("Null font passed to TextLayout constructor.");
}
if (string == null) {
throw new IllegalArgumentException("Null string passed to TextLayout constructor.");
}
if (string.length() == 0) {
throw new IllegalArgumentException("Zero length string passed to TextLayout constructor.");
}
Map<? extends Attribute, ?> attributes = null;
if (font.hasLayoutAttributes()) {
attributes = font.getAttributes();
}
char[] text = string.toCharArray();
if (sameBaselineUpTo(font, text, 0, text.length) == text.length) {
fastInit(text, font, attributes, frc);
} else {
AttributedString as = attributes == null
? new AttributedString(string)
: new AttributedString(string, attributes);
as.addAttribute(TextAttribute.FONT, font);
standardInit(as.getIterator(), text, frc);
}
}
protected void drawString(String str, float x, float y,
Font font, FontRenderContext frc, float w) {
if (str.length() == 0) {
return;
}
/* If the Font has layout attributes we need to delegate to TextLayout.
* TextLayout renders text as GlyphVectors. We try to print those
* using printer fonts - ie using Postscript text operators so
* we may be reinvoked. In that case the "!printingGlyphVector" test
* prevents us recursing and instead sends us into the body of the
* method where we can safely ignore layout attributes as those
* are already handled by TextLayout.
*/
if (font.hasLayoutAttributes() && !printingGlyphVector) {
TextLayout layout = new TextLayout(str, font, frc);
layout.draw(this, x, y);
return;
}
Font oldFont = getFont();
if (!oldFont.equals(font)) {
setFont(font);
} else {
oldFont = null;
}
boolean drawnWithPS = false;
float translateX = 0f, translateY = 0f;
boolean fontisTransformed = getFont().isTransformed();
if (fontisTransformed) {
AffineTransform fontTx = getFont().getTransform();
int transformType = fontTx.getType();
/* TYPE_TRANSLATION is a flag bit but we can do "==" here
* because we want to detect when its just that bit set and
*
*/
if (transformType == AffineTransform.TYPE_TRANSLATION) {
translateX = (float)(fontTx.getTranslateX());
translateY = (float)(fontTx.getTranslateY());
if (Math.abs(translateX) < 0.00001) translateX = 0f;
if (Math.abs(translateY) < 0.00001) translateY = 0f;
fontisTransformed = false;
}
}
boolean directToPS = !fontisTransformed;
if (!PSPrinterJob.shapeTextProp && directToPS) {
PSPrinterJob psPrinterJob = (PSPrinterJob) getPrinterJob();
if (psPrinterJob.setFont(getFont())) {
/* Set the text color.
* We should not be in this shape printing path
* if the application is drawing with non-solid
* colors. We should be in the raster path. Because
* we are here in the shape path, the cast of the
* paint to a Color should be fine.
*/
try {
psPrinterJob.setColor((Color)getPaint());
} catch (ClassCastException e) {
if (oldFont != null) {
setFont(oldFont);
}
throw new IllegalArgumentException(
"Expected a Color instance");
}
psPrinterJob.setTransform(getTransform());
psPrinterJob.setClip(getClip());
drawnWithPS = psPrinterJob.textOut(this, str,
x+translateX, y+translateY,
font, frc, w);
}
}
/* The text could not be converted directly to PS text
* calls so decompose the text into a shape.
*/
if (drawnWithPS == false) {
if (oldFont != null) {
setFont(oldFont);
oldFont = null;
}
super.drawString(str, x, y, font, frc, w);
}
if (oldFont != null) {
setFont(oldFont);
}
}
protected void drawString(String str, float x, float y,
Font font, FontRenderContext frc, float w) {
if (str.length() == 0) {
return;
}
/* If the Font has layout attributes we need to delegate to TextLayout.
* TextLayout renders text as GlyphVectors. We try to print those
* using printer fonts - ie using Postscript text operators so
* we may be reinvoked. In that case the "!printingGlyphVector" test
* prevents us recursing and instead sends us into the body of the
* method where we can safely ignore layout attributes as those
* are already handled by TextLayout.
*/
if (font.hasLayoutAttributes() && !printingGlyphVector) {
TextLayout layout = new TextLayout(str, font, frc);
layout.draw(this, x, y);
return;
}
Font oldFont = getFont();
if (!oldFont.equals(font)) {
setFont(font);
} else {
oldFont = null;
}
boolean drawnWithPS = false;
float translateX = 0f, translateY = 0f;
boolean fontisTransformed = getFont().isTransformed();
if (fontisTransformed) {
AffineTransform fontTx = getFont().getTransform();
int transformType = fontTx.getType();
/* TYPE_TRANSLATION is a flag bit but we can do "==" here
* because we want to detect when its just that bit set and
*
*/
if (transformType == AffineTransform.TYPE_TRANSLATION) {
translateX = (float)(fontTx.getTranslateX());
translateY = (float)(fontTx.getTranslateY());
if (Math.abs(translateX) < 0.00001) translateX = 0f;
if (Math.abs(translateY) < 0.00001) translateY = 0f;
fontisTransformed = false;
}
}
boolean directToPS = !fontisTransformed;
if (!PSPrinterJob.shapeTextProp && directToPS) {
PSPrinterJob psPrinterJob = (PSPrinterJob) getPrinterJob();
if (psPrinterJob.setFont(getFont())) {
/* Set the text color.
* We should not be in this shape printing path
* if the application is drawing with non-solid
* colors. We should be in the raster path. Because
* we are here in the shape path, the cast of the
* paint to a Color should be fine.
*/
try {
psPrinterJob.setColor((Color)getPaint());
} catch (ClassCastException e) {
if (oldFont != null) {
setFont(oldFont);
}
throw new IllegalArgumentException(
"Expected a Color instance");
}
psPrinterJob.setTransform(getTransform());
psPrinterJob.setClip(getClip());
drawnWithPS = psPrinterJob.textOut(this, str,
x+translateX, y+translateY,
font, frc, w);
}
}
/* The text could not be converted directly to PS text
* calls so decompose the text into a shape.
*/
if (drawnWithPS == false) {
if (oldFont != null) {
setFont(oldFont);
oldFont = null;
}
super.drawString(str, x, y, font, frc, w);
}
if (oldFont != null) {
setFont(oldFont);
}
}