下面列出了java.awt.font.TextLayout#hitTestChar ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private int calcHitPosition(final int caretPosition, final double x, final double y,
final double zoomFactor) {
boolean switched = false;
int lp = m_mouse_pressed_y;
int lr = m_mouse_released_y;
if (lp > lr) {
lp = m_mouse_released_y;
lr = m_mouse_pressed_y;
switched = true;
}
final int linecount = m_content.getLineCount();
final double height = (float) m_content.getLineHeight();
int maxIndex = caretPosition;
for (int line = lp; line <= lr; line++) {
double deltaY = 0;
if (switched) {
deltaY = height * zoomFactor * line;
} else {
deltaY = -(height * zoomFactor * (linecount - line));
}
final TextLayout textLayout = m_content.getLineContent(line).getTextLayout();
final TextHitInfo hitInfo =
textLayout.hitTestChar((float) x, (float) (y + deltaY), textLayout.getBounds());
final int insertionIndex = hitInfo.getInsertionIndex();
if ((caretPosition < insertionIndex) && (insertionIndex > maxIndex)) {
maxIndex = insertionIndex;
}
}
return maxIndex;
}
TextHitInfo getLocationOffset(int x, int y) {
TextLayout layout = composedTextLayout;
if (layout == null) {
return null;
} else {
Point location = getLocationOnScreen();
x -= location.x + TEXT_ORIGIN_X;
y -= location.y + TEXT_ORIGIN_Y;
if (layout.getBounds().contains(x, y)) {
return layout.hitTestChar(x, y);
} else {
return null;
}
}
}
public void runTest(Object ctx, int numReps) {
TLExContext tlctx = (TLExContext)ctx;
TextLayout tl = tlctx.tl;
int numhits = tlctx.hits.length;
Rectangle2D lb = tlctx.lb;
TextHitInfo hit;
for (int i = 0; i <= numhits; ++i) {
float x = (float)(lb.getMinX() + lb.getWidth() * i / numhits);
float y = (float)(lb.getMinY() + lb.getHeight() * i / numhits);
hit = tl.hitTestChar(x, y, lb);
}
}
TextHitInfo getLocationOffset(int x, int y) {
TextLayout layout = composedTextLayout;
if (layout == null) {
return null;
} else {
Point location = getLocationOnScreen();
x -= location.x + TEXT_ORIGIN_X;
y -= location.y + TEXT_ORIGIN_Y;
if (layout.getBounds().contains(x, y)) {
return layout.hitTestChar(x, y);
} else {
return null;
}
}
}
public void runTest(Object ctx, int numReps) {
TLExContext tlctx = (TLExContext)ctx;
TextLayout tl = tlctx.tl;
int numhits = tlctx.hits.length;
Rectangle2D lb = tlctx.lb;
TextHitInfo hit;
for (int i = 0; i <= numhits; ++i) {
float x = (float)(lb.getMinX() + lb.getWidth() * i / numhits);
float y = (float)(lb.getMinY() + lb.getHeight() * i / numhits);
hit = tl.hitTestChar(x, y, lb);
}
}
TextHitInfo getLocationOffset(int x, int y) {
TextLayout layout = composedTextLayout;
if (layout == null) {
return null;
} else {
Point location = getLocationOnScreen();
x -= location.x + TEXT_ORIGIN_X;
y -= location.y + TEXT_ORIGIN_Y;
if (layout.getBounds().contains(x, y)) {
return layout.hitTestChar(x, y);
} else {
return null;
}
}
}
public void runTest(Object ctx, int numReps) {
TLExContext tlctx = (TLExContext)ctx;
TextLayout tl = tlctx.tl;
int numhits = tlctx.hits.length;
Rectangle2D lb = tlctx.lb;
TextHitInfo hit;
for (int i = 0; i <= numhits; ++i) {
float x = (float)(lb.getMinX() + lb.getWidth() * i / numhits);
float y = (float)(lb.getMinY() + lb.getHeight() * i / numhits);
hit = tl.hitTestChar(x, y, lb);
}
}
public void runTest(Object ctx, int numReps) {
TLExContext tlctx = (TLExContext)ctx;
TextLayout tl = tlctx.tl;
int numhits = tlctx.hits.length;
Rectangle2D lb = tlctx.lb;
TextHitInfo hit;
for (int i = 0; i <= numhits; ++i) {
float x = (float)(lb.getMinX() + lb.getWidth() * i / numhits);
float y = (float)(lb.getMinY() + lb.getHeight() * i / numhits);
hit = tl.hitTestChar(x, y, lb);
}
}
static TextHitInfo x2RelOffset(TextLayout textLayout, float x) {
TextHitInfo hit;
x -= EXTRA_MARGIN_WIDTH;
if (x >= textLayout.getAdvance()) {
hit = TextHitInfo.trailing(textLayout.getCharacterCount());
} else {
hit = textLayout.hitTestChar(x, 0); // What about backward bias -> with higher offsets it may go back visually
}
return hit;
}
public void runTest(Object ctx, int numReps) {
TLExContext tlctx = (TLExContext)ctx;
TextLayout tl = tlctx.tl;
int numhits = tlctx.hits.length;
Rectangle2D lb = tlctx.lb;
TextHitInfo hit;
for (int i = 0; i <= numhits; ++i) {
float x = (float)(lb.getMinX() + lb.getWidth() * i / numhits);
float y = (float)(lb.getMinY() + lb.getHeight() * i / numhits);
hit = tl.hitTestChar(x, y, lb);
}
}
public void runTest(Object ctx, int numReps) {
TLExContext tlctx = (TLExContext)ctx;
TextLayout tl = tlctx.tl;
int numhits = tlctx.hits.length;
Rectangle2D lb = tlctx.lb;
TextHitInfo hit;
for (int i = 0; i <= numhits; ++i) {
float x = (float)(lb.getMinX() + lb.getWidth() * i / numhits);
float y = (float)(lb.getMinY() + lb.getHeight() * i / numhits);
hit = tl.hitTestChar(x, y, lb);
}
}
TextHitInfo getLocationOffset(int x, int y) {
TextLayout layout = composedTextLayout;
if (layout == null) {
return null;
} else {
Point location = getLocationOnScreen();
x -= location.x + TEXT_ORIGIN_X;
y -= location.y + TEXT_ORIGIN_Y;
if (layout.getBounds().contains(x, y)) {
return layout.hitTestChar(x, y);
} else {
return null;
}
}
}
TextHitInfo getLocationOffset(int x, int y) {
TextLayout layout = composedTextLayout;
if (layout == null) {
return null;
} else {
Point location = getLocationOnScreen();
x -= location.x + TEXT_ORIGIN_X;
y -= location.y + TEXT_ORIGIN_Y;
if (layout.getBounds().contains(x, y)) {
return layout.hitTestChar(x, y);
} else {
return null;
}
}
}
public void runTest(Object ctx, int numReps) {
TLExContext tlctx = (TLExContext)ctx;
TextLayout tl = tlctx.tl;
int numhits = tlctx.hits.length;
Rectangle2D lb = tlctx.lb;
TextHitInfo hit;
for (int i = 0; i <= numhits; ++i) {
float x = (float)(lb.getMinX() + lb.getWidth() * i / numhits);
float y = (float)(lb.getMinY() + lb.getHeight() * i / numhits);
hit = tl.hitTestChar(x, y, lb);
}
}
TextHitInfo getLocationOffset(int x, int y) {
TextLayout layout = composedTextLayout;
if (layout == null) {
return null;
} else {
Point location = getLocationOnScreen();
x -= location.x + TEXT_ORIGIN_X;
y -= location.y + TEXT_ORIGIN_Y;
if (layout.getBounds().contains(x, y)) {
return layout.hitTestChar(x, y);
} else {
return null;
}
}
}
public void runTest(Object ctx, int numReps) {
TLExContext tlctx = (TLExContext)ctx;
TextLayout tl = tlctx.tl;
int numhits = tlctx.hits.length;
Rectangle2D lb = tlctx.lb;
TextHitInfo hit;
for (int i = 0; i <= numhits; ++i) {
float x = (float)(lb.getMinX() + lb.getWidth() * i / numhits);
float y = (float)(lb.getMinY() + lb.getHeight() * i / numhits);
hit = tl.hitTestChar(x, y, lb);
}
}
public void runTest(Object ctx, int numReps) {
TLExContext tlctx = (TLExContext)ctx;
TextLayout tl = tlctx.tl;
int numhits = tlctx.hits.length;
Rectangle2D lb = tlctx.lb;
TextHitInfo hit;
for (int i = 0; i <= numhits; ++i) {
float x = (float)(lb.getMinX() + lb.getWidth() * i / numhits);
float y = (float)(lb.getMinY() + lb.getHeight() * i / numhits);
hit = tl.hitTestChar(x, y, lb);
}
}
TextHitInfo getLocationOffset(int x, int y) {
TextLayout layout = composedTextLayout;
if (layout == null) {
return null;
} else {
Point location = getLocationOnScreen();
x -= location.x + TEXT_ORIGIN_X;
y -= location.y + TEXT_ORIGIN_Y;
if (layout.getBounds().contains(x, y)) {
return layout.hitTestChar(x, y);
} else {
return null;
}
}
}
TextHitInfo getLocationOffset(int x, int y) {
TextLayout layout = composedTextLayout;
if (layout == null) {
return null;
} else {
Point location = getLocationOnScreen();
x -= location.x + TEXT_ORIGIN_X;
y -= location.y + TEXT_ORIGIN_Y;
if (layout.getBounds().contains(x, y)) {
return layout.hitTestChar(x, y);
} else {
return null;
}
}
}
public void runTest(Object ctx, int numReps) {
TLExContext tlctx = (TLExContext)ctx;
TextLayout tl = tlctx.tl;
int numhits = tlctx.hits.length;
Rectangle2D lb = tlctx.lb;
TextHitInfo hit;
for (int i = 0; i <= numhits; ++i) {
float x = (float)(lb.getMinX() + lb.getWidth() * i / numhits);
float y = (float)(lb.getMinY() + lb.getHeight() * i / numhits);
hit = tl.hitTestChar(x, y, lb);
}
}