本文整理汇总了Java中java.awt.FontMetrics.charsWidth方法的典型用法代码示例。如果您正苦于以下问题:Java FontMetrics.charsWidth方法的具体用法?Java FontMetrics.charsWidth怎么用?Java FontMetrics.charsWidth使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.awt.FontMetrics
的用法示例。
在下文中一共展示了FontMetrics.charsWidth方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getWidthUpTo
import java.awt.FontMetrics; //导入方法依赖的package包/类
@Override
public float getWidthUpTo(int numChars, RSyntaxTextArea textArea,
TabExpander e, float x0) {
float width = x0;
FontMetrics fm = textArea.getFontMetricsForTokenType(getType());
if (fm != null) {
int w;
int currentStart = textOffset;
int endBefore = textOffset + numChars;
for (int i = currentStart; i < endBefore; i++) {
if (text[i] == '\t') {
// Since TokenMaker implementations usually group all
// adjacent whitespace into a single token, there
// aren't usually any characters to compute a width
// for here, so we check before calling.
w = i - currentStart;
if (w > 0) {
width += fm.charsWidth(text, currentStart, w);
}
currentStart = i + 1;
width = e.nextTabStop(width, 0);
}
}
// Most (non-whitespace) tokens will have characters at this
// point to get the widths for, so we don't check for w>0 (mini-
// optimization).
w = endBefore - currentStart;
width += fm.charsWidth(text, currentStart, w);
}
return width - x0;
}
示例2: runTest
import java.awt.FontMetrics; //导入方法依赖的package包/类
public void runTest(Object ctx, int numReps) {
SWContext swctx = (SWContext)ctx;
FontMetrics fm = swctx.fm;
char[] chars = swctx.chars;
int wid = 0;
do {
wid += fm.charsWidth(chars, 0, chars.length);
} while (--numReps >= 0);
}
示例3: getPosition
import java.awt.FontMetrics; //导入方法依赖的package包/类
/**
* Get the position based on the hex index, font and orientation.
*/
Point getPosition(Graphics2D g) {
Point p = getPosition();
if (getSize() == 0)
return p;
assert (g.getFont() == getFont());
FontMetrics fm = g.getFontMetrics();
int size = getLayout().getHexSize();
switch (orientation) {
case LOWER_CENTER:
case UPPER_CENTER:
case LOWER_RIGHT:
case UPPER_RIGHT:
case UPPER_LEFT:
case LOWER_LEFT:
case HEX_CENTER:
p.x += size / 2; // middle of the hex.
break;
case CENTER_RIGHT:
p.x += size; // right of hex
break;
case CENTER_LEFT:
break;
}
switch (orientation) {
case LOWER_CENTER:
case UPPER_CENTER:
case HEX_CENTER:
// text centered
p.x -= fm.charsWidth(text.toCharArray(), 0, text.length()) / 2;
break;
case UPPER_LEFT:
case LOWER_LEFT:
case CENTER_LEFT:
// right justified
p.x -= fm.charsWidth(text.toCharArray(), 0, text.length());
break;
case LOWER_RIGHT:
case UPPER_RIGHT:
case CENTER_RIGHT:
break;
}
switch (orientation) {
case LOWER_CENTER:
case LOWER_RIGHT:
case LOWER_LEFT:
p.y += size + fm.getAscent();
break;
case UPPER_CENTER:
case UPPER_RIGHT:
case UPPER_LEFT:
p.y -= fm.getDescent();
break;
case CENTER_LEFT:
case CENTER_RIGHT:
case HEX_CENTER:
p.y += size / 2 + fm.getHeight() / 2 - fm.getDescent();
break;
}
return p;
}
示例4: getListOffset
import java.awt.FontMetrics; //导入方法依赖的package包/类
@Override
public int getListOffset(RSyntaxTextArea textArea, TabExpander e,
float x0, float x) {
// If the coordinate in question is before this line's start, quit.
if (x0 >= x) {
return getOffset();
}
float currX = x0; // x-coordinate of current char.
float nextX = x0; // x-coordinate of next char.
float stableX = x0; // Cached ending x-coord. of last tab or token.
TokenImpl token = this;
int last = getOffset();
FontMetrics fm = null;
while (token != null && token.isPaintable()) {
fm = textArea.getFontMetricsForTokenType(token.getType());
char[] text = token.text;
int start = token.textOffset;
int end = start + token.textCount;
for (int i = start; i < end; i++) {
currX = nextX;
if (text[i] == '\t') {
nextX = e.nextTabStop(nextX, 0);
stableX = nextX; // Cache ending x-coord. of tab.
start = i + 1; // Do charsWidth() from next char.
}
else {
nextX = stableX + fm.charsWidth(text, start, i - start + 1);
}
if (x >= currX && x < nextX) {
if ((x - currX) < (nextX - x)) {
return last + i - token.textOffset;
}
return last + i + 1 - token.textOffset;
}
}
stableX = nextX; // Cache ending x-coordinate of token.
last += token.textCount;
token = (TokenImpl)token.getNextToken();
}
// If we didn't find anything, return the end position of the text.
return last;
}
示例5: paintImpl
import java.awt.FontMetrics; //导入方法依赖的package包/类
/**
* Does the dirty-work of actually painting the token.
*/
protected float paintImpl(Token token, Graphics2D g, float x, float y,
RSyntaxTextArea host, TabExpander e, float clipStart,
boolean selected, boolean useSTC) {
int origX = (int)x;
int textOffs = token.getTextOffset();
char[] text = token.getTextArray();
int end = textOffs + token.length();
float nextX = x;
int flushLen = 0;
int flushIndex = textOffs;
Color fg = useSTC ? host.getSelectedTextColor() :
host.getForegroundForToken(token);
Color bg = selected ? null : host.getBackgroundForToken(token);
g.setFont(host.getFontForTokenType(token.getType()));
FontMetrics fm = host.getFontMetricsForTokenType(token.getType());
for (int i=textOffs; i<end; i++) {
switch (text[i]) {
case '\t':
nextX = e.nextTabStop(
x + fm.charsWidth(text, flushIndex,flushLen), 0);
if (bg!=null) {
paintBackground(x,y, nextX-x,fm.getHeight(),
g, fm.getAscent(), host, bg);
}
if (flushLen > 0) {
g.setColor(fg);
g.drawChars(text, flushIndex, flushLen, (int)x,(int)y);
flushLen = 0;
}
flushIndex = i + 1;
x = nextX;
break;
default:
flushLen += 1;
break;
}
}
nextX = x+fm.charsWidth(text, flushIndex,flushLen);
java.awt.Rectangle r = host.getMatchRectangle();
if (flushLen>0 && nextX>=clipStart) {
if (bg!=null) {
paintBackground(x,y, nextX-x,fm.getHeight(),
g, fm.getAscent(), host, bg);
if (token.length()==1 && r!=null && r.x==x) {
((RSyntaxTextAreaUI)host.getUI()).paintMatchedBracketImpl(
g, host, r);
}
}
g.setColor(fg);
g.drawChars(text, flushIndex, flushLen, (int)x,(int)y);
}
if (host.getUnderlineForToken(token)) {
g.setColor(fg);
int y2 = (int)(y+1);
g.drawLine(origX,y2, (int)nextX,y2);
}
// Don't check if it's whitespace - some TokenMakers may return types
// other than Token.WHITESPACE for spaces (such as Token.IDENTIFIER).
// This also allows us to paint tab lines for MLC's.
if (host.getPaintTabLines() && origX==host.getMargin().left) {// && isWhitespace()) {
paintTabLines(token, origX, (int)y, (int)nextX, g, e, host);
}
return nextX;
}
示例6: paintTabLines
import java.awt.FontMetrics; //导入方法依赖的package包/类
/**
* Paints dotted "tab" lines; that is, lines that show where your caret
* would go to on the line if you hit "tab". This visual effect is usually
* done in the leading whitespace token(s) of lines.
*
* @param token The token to render.
* @param x The starting x-offset of this token. It is assumed that this
* is the left margin of the text area (may be non-zero due to
* insets), since tab lines are only painted for leading whitespace.
* @param y The baseline where this token was painted.
* @param endX The ending x-offset of this token.
* @param g The graphics context.
* @param e Used to expand tabs.
* @param host The text area.
*/
protected void paintTabLines(Token token, int x, int y, int endX,
Graphics2D g, TabExpander e, RSyntaxTextArea host) {
// We allow tab lines to be painted in more than just Token.WHITESPACE,
// i.e. for MLC's and Token.IDENTIFIERS (for TokenMakers that return
// whitespace as identifiers for performance). But we only paint tab
// lines for the leading whitespace in the token. So, if this isn't a
// WHITESPACE token, figure out the leading whitespace's length.
if (token.getType()!=Token.WHITESPACE) {
int offs = 0;
for (; offs<token.length(); offs++) {
if (!RSyntaxUtilities.isWhitespace(token.charAt(offs))) {
break; // MLC text, etc.
}
}
if (offs<2) { // Must be at least two spaces to see tab line
return;
}
//endX = x + (int)getWidthUpTo(offs, host, e, x);
endX = (int)token.getWidthUpTo(offs, host, e, 0);
}
// Get the length of a tab.
FontMetrics fm = host.getFontMetricsForTokenType(token.getType());
int tabSize = host.getTabSize();
if (tabBuf==null || tabBuf.length<tabSize) {
tabBuf = new char[tabSize];
for (int i=0; i<tabSize; i++) {
tabBuf[i] = ' ';
}
}
// Note different token types (MLC's, whitespace) could possibly be
// using different fonts, which means we can't cache the actual width
// of a tab as it may be different per-token-type. We could keep a
// per-token-type cache, but we'd have to clear it whenever they
// modified token styles.
int tabW = fm.charsWidth(tabBuf, 0, tabSize);
// Draw any tab lines. Here we're assuming that "x" is the left
// margin of the editor.
g.setColor(host.getTabLineColor());
int x0 = x + tabW;
int y0 = y - fm.getAscent();
if ((y0&1)>0) {
// Only paint on even y-pixels to prevent doubling up between lines
y0++;
}
// TODO: Go to endX (inclusive) if this token is last token in the line
Token next = token.getNextToken();
if (next==null || !next.isPaintable()) {
endX++;
}
while (x0<endX) {
int y1 = y0;
int y2 = y0 + host.getLineHeight();
while (y1<y2) {
g.drawLine(x0, y1, x0, y1);
y1 += 2;
}
//g.drawLine(x0,y0, x0,y0+host.getLineHeight());
x0 += tabW;
}
}