use of sun.font.Font2D in project jdk8u_jdk by JetBrains.
the class Font method canDisplayUpTo.
/**
* Indicates whether or not this <code>Font</code> can display a
* specified <code>String</code>. For strings with Unicode encoding,
* it is important to know if a particular font can display the
* string. This method returns an offset into the <code>String</code>
* <code>str</code> which is the first character this
* <code>Font</code> cannot display without using the missing glyph
* code. If the <code>Font</code> can display all characters, -1 is
* returned.
* @param str a <code>String</code> object
* @return an offset into <code>str</code> that points
* to the first character in <code>str</code> that this
* <code>Font</code> cannot display; or <code>-1</code> if
* this <code>Font</code> can display all characters in
* <code>str</code>.
* @since 1.2
*/
public int canDisplayUpTo(String str) {
Font2D font2d = getFont2D();
int len = str.length();
for (int i = 0; i < len; i++) {
char c = str.charAt(i);
if (font2d.canDisplay(c)) {
continue;
}
if (!Character.isHighSurrogate(c)) {
return i;
}
if (!font2d.canDisplay(str.codePointAt(i))) {
return i;
}
i++;
}
return -1;
}
use of sun.font.Font2D in project jdk8u_jdk by JetBrains.
the class Font method canDisplayUpTo.
/**
* Indicates whether or not this <code>Font</code> can display
* the characters in the specified <code>text</code>
* starting at <code>start</code> and ending at
* <code>limit</code>. This method is a convenience overload.
* @param text the specified array of <code>char</code> values
* @param start the specified starting offset (in
* <code>char</code>s) into the specified array of
* <code>char</code> values
* @param limit the specified ending offset (in
* <code>char</code>s) into the specified array of
* <code>char</code> values
* @return an offset into <code>text</code> that points
* to the first character in <code>text</code> that this
* <code>Font</code> cannot display; or <code>-1</code> if
* this <code>Font</code> can display all characters in
* <code>text</code>.
* @since 1.2
*/
public int canDisplayUpTo(char[] text, int start, int limit) {
Font2D font2d = getFont2D();
for (int i = start; i < limit; i++) {
char c = text[i];
if (font2d.canDisplay(c)) {
continue;
}
if (!Character.isHighSurrogate(c)) {
return i;
}
if (!font2d.canDisplay(Character.codePointAt(text, i, limit))) {
return i;
}
i++;
}
return -1;
}
use of sun.font.Font2D in project jdk8u_jdk by JetBrains.
the class WPathGraphics method drawString.
/**
* Renders the text specified by the specified <code>String</code>,
* using the current <code>Font</code> and <code>Paint</code> attributes
* in the <code>Graphics2D</code> context.
* The baseline of the first character is at position
* (<i>x</i>, <i>y</i>) in the User Space.
* The rendering attributes applied include the <code>Clip</code>,
* <code>Transform</code>, <code>Paint</code>, <code>Font</code> and
* <code>Composite</code> attributes. For characters in script systems
* such as Hebrew and Arabic, the glyphs can be rendered from right to
* left, in which case the coordinate supplied is the location of the
* leftmost character on the baseline.
* @param s the <code>String</code> to be rendered
* @param x, y the coordinates where the <code>String</code>
* should be rendered
* @see #setPaint
* @see java.awt.Graphics#setColor
* @see java.awt.Graphics#setFont
* @see #setTransform
* @see #setComposite
* @see #setClip
*/
@Override
public void drawString(String str, float x, float y, Font font, FontRenderContext frc, float targetW) {
if (str.length() == 0) {
return;
}
if (WPrinterJob.shapeTextProp) {
super.drawString(str, x, y, font, frc, targetW);
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.
* Similarly if layout is needed based on the text, then we
* delegate to TextLayout if possible, or failing that we delegate
* upwards to filled shapes.
*/
boolean layoutNeeded = strNeedsTextLayout(str, font);
if ((font.hasLayoutAttributes() || layoutNeeded) && !printingGlyphVector) {
TextLayout layout = new TextLayout(str, font, frc);
layout.draw(this, x, y);
return;
} else if (layoutNeeded) {
super.drawString(str, x, y, font, frc, targetW);
return;
}
AffineTransform deviceTransform = getTransform();
AffineTransform fontTransform = new AffineTransform(deviceTransform);
fontTransform.concatenate(font.getTransform());
int transformType = fontTransform.getType();
/* Use GDI for the text if the graphics transform is something
* for which we can obtain a suitable GDI font.
* A flip or shearing transform on the graphics or a transform
* on the font force us to decompose the text into a shape.
*/
boolean directToGDI = ((transformType != AffineTransform.TYPE_GENERAL_TRANSFORM) && ((transformType & AffineTransform.TYPE_FLIP) == 0));
WPrinterJob wPrinterJob = (WPrinterJob) getPrinterJob();
try {
wPrinterJob.setTextColor((Color) getPaint());
} catch (ClassCastException e) {
// peek should detect such paints.
directToGDI = false;
}
if (!directToGDI) {
super.drawString(str, x, y, font, frc, targetW);
return;
}
/* Now we have checked everything is OK to go through GDI as text
* with the exception of testing GDI can find and use the font. That
* is handled in the textOut() call.
*/
/* Compute the starting position of the string in
* device space.
*/
Point2D.Float userpos = new Point2D.Float(x, y);
Point2D.Float devpos = new Point2D.Float();
/* Already have the translate from the deviceTransform,
* but the font may have a translation component too.
*/
if (font.isTransformed()) {
AffineTransform fontTx = font.getTransform();
float translateX = (float) (fontTx.getTranslateX());
float translateY = (float) (fontTx.getTranslateY());
if (Math.abs(translateX) < 0.00001)
translateX = 0f;
if (Math.abs(translateY) < 0.00001)
translateY = 0f;
userpos.x += translateX;
userpos.y += translateY;
}
deviceTransform.transform(userpos, devpos);
if (getClip() != null) {
deviceClip(getClip().getPathIterator(deviceTransform));
}
/* Get the font size in device coordinates.
* The size needed is the font height scaled to device space.
* Although we have already tested that there is no shear,
* there may be a non-uniform scale, so the width of the font
* does not scale equally with the height. That is handled
* by specifying an 'average width' scale to GDI.
*/
float fontSize = font.getSize2D();
double devResX = wPrinterJob.getXRes();
double devResY = wPrinterJob.getYRes();
double fontDevScaleY = devResY / DEFAULT_USER_RES;
int orient = getPageFormat().getOrientation();
if (orient == PageFormat.LANDSCAPE || orient == PageFormat.REVERSE_LANDSCAPE) {
double tmp = devResX;
devResX = devResY;
devResY = tmp;
}
double devScaleX = devResX / DEFAULT_USER_RES;
double devScaleY = devResY / DEFAULT_USER_RES;
fontTransform.scale(1.0 / devScaleX, 1.0 / devScaleY);
Point2D.Double pty = new Point2D.Double(0.0, 1.0);
fontTransform.deltaTransform(pty, pty);
double scaleFactorY = Math.sqrt(pty.x * pty.x + pty.y * pty.y);
float scaledFontSizeY = (float) (fontSize * scaleFactorY * fontDevScaleY);
Point2D.Double ptx = new Point2D.Double(1.0, 0.0);
fontTransform.deltaTransform(ptx, ptx);
double scaleFactorX = Math.sqrt(ptx.x * ptx.x + ptx.y * ptx.y);
float awScale = getAwScale(scaleFactorX, scaleFactorY);
int iangle = getAngle(ptx);
ptx = new Point2D.Double(1.0, 0.0);
deviceTransform.deltaTransform(ptx, ptx);
double advanceScaleX = Math.sqrt(ptx.x * ptx.x + ptx.y * ptx.y);
pty = new Point2D.Double(0.0, 1.0);
deviceTransform.deltaTransform(pty, pty);
double advanceScaleY = Math.sqrt(pty.x * pty.x + pty.y * pty.y);
Font2D font2D = FontUtilities.getFont2D(font);
if (font2D instanceof TrueTypeFont) {
textOut(str, font, (TrueTypeFont) font2D, frc, scaledFontSizeY, iangle, awScale, advanceScaleX, advanceScaleY, x, y, devpos.x, devpos.y, targetW);
} else if (font2D instanceof CompositeFont) {
/* Composite fonts are made up of multiple fonts and each
* substring that uses a particular component font needs to
* be separately sent to GDI.
* This works for standard composite fonts, alternate ones,
* Fonts that are a physical font backed by a standard composite,
* and with fallback fonts.
*/
CompositeFont compFont = (CompositeFont) font2D;
float userx = x, usery = y;
float devx = devpos.x, devy = devpos.y;
char[] chars = str.toCharArray();
int len = chars.length;
int[] glyphs = new int[len];
compFont.getMapper().charsToGlyphs(len, chars, glyphs);
int startChar = 0, endChar = 0, slot = 0;
while (endChar < len) {
startChar = endChar;
slot = glyphs[startChar] >>> 24;
while (endChar < len && ((glyphs[endChar] >>> 24) == slot)) {
endChar++;
}
String substr = new String(chars, startChar, endChar - startChar);
PhysicalFont slotFont = compFont.getSlotFont(slot);
textOut(substr, font, slotFont, frc, scaledFontSizeY, iangle, awScale, advanceScaleX, advanceScaleY, userx, usery, devx, devy, 0f);
Rectangle2D bds = font.getStringBounds(substr, frc);
float xAdvance = (float) bds.getWidth();
userx += xAdvance;
userpos.x += xAdvance;
deviceTransform.transform(userpos, devpos);
devx = devpos.x;
devy = devpos.y;
}
} else {
super.drawString(str, x, y, font, frc, targetW);
}
}
use of sun.font.Font2D in project jdk8u_jdk by JetBrains.
the class WPathGraphics method platformFontCount.
/* A return value of 0 would mean font not available to GDI, or the
* it can't be used for this string.
* A return of 1 means it is suitable, including for composites.
* We check that the transform in effect is doable with GDI, and that
* this is a composite font AWT can handle, or a physical font GDI
* can handle directly. Its possible that some strings may ultimately
* fail the more stringent tests in drawString but this is rare and
* also that method will always succeed, as if the font isn't available
* it will use outlines via a superclass call. Also it is only called for
* the default render context (as canDrawStringToWidth() will return
* false. That is why it ignores the frc and width arguments.
*/
@Override
protected int platformFontCount(Font font, String str) {
AffineTransform deviceTransform = getTransform();
AffineTransform fontTransform = new AffineTransform(deviceTransform);
fontTransform.concatenate(getFont().getTransform());
int transformType = fontTransform.getType();
/* Test if GDI can handle the transform */
boolean directToGDI = ((transformType != AffineTransform.TYPE_GENERAL_TRANSFORM) && ((transformType & AffineTransform.TYPE_FLIP) == 0));
if (!directToGDI) {
return 0;
}
/* Since all windows fonts are available, and the JRE fonts
* are also registered. Only the Font.createFont() case is presently
* unknown to GDI. Those can be registered too, although that
* code does not exist yet, it can be added too, so we should not
* fail that case. Just do a quick check whether its a TrueTypeFont
* - ie not a Type1 font etc, and let drawString() resolve the rest.
*/
Font2D font2D = FontUtilities.getFont2D(font);
if (font2D instanceof CompositeFont || font2D instanceof TrueTypeFont) {
return 1;
} else {
return 0;
}
}
use of sun.font.Font2D in project jdk8u_jdk by JetBrains.
the class Font method canDisplayUpTo.
/**
* Indicates whether or not this <code>Font</code> can display the
* text specified by the <code>iter</code> starting at
* <code>start</code> and ending at <code>limit</code>.
*
* @param iter a {@link CharacterIterator} object
* @param start the specified starting offset into the specified
* <code>CharacterIterator</code>.
* @param limit the specified ending offset into the specified
* <code>CharacterIterator</code>.
* @return an offset into <code>iter</code> that points
* to the first character in <code>iter</code> that this
* <code>Font</code> cannot display; or <code>-1</code> if
* this <code>Font</code> can display all characters in
* <code>iter</code>.
* @since 1.2
*/
public int canDisplayUpTo(CharacterIterator iter, int start, int limit) {
Font2D font2d = getFont2D();
char c = iter.setIndex(start);
for (int i = start; i < limit; i++, c = iter.next()) {
if (font2d.canDisplay(c)) {
continue;
}
if (!Character.isHighSurrogate(c)) {
return i;
}
char c2 = iter.next();
// c2 could be CharacterIterator.DONE which is not a low surrogate.
if (!Character.isLowSurrogate(c2)) {
return i;
}
if (!font2d.canDisplay(Character.toCodePoint(c, c2))) {
return i;
}
i++;
}
return -1;
}
Aggregations