Java源码示例:com.sun.javafx.scene.text.TextLayout

示例1
/**
 * @return the number of lines in the text
 */
public int getLastLine() {
	TextLayout textlayout = (TextLayout) invoke(getTextLayout, textflow);
	GlyphList[] glyphlist = textlayout.getRuns();
	float lasty = 0;
	int lines = 0;
	for (int i = 0; i < glyphlist.length; i++) {
		GlyphList thisglyphlist = glyphlist[i];
		int glyphcount = thisglyphlist.getGlyphCount();
		if (glyphcount == 0)
			glyphcount = 1;
		if (lasty != thisglyphlist.getLocation().y) {
			lasty = thisglyphlist.getLocation().y;
			lines++;
		}

	}
	return lines;
}
 
示例2
/**
 * @return get the line on which the caret is
 */
public int getLineForCarret() {
	TextLayout textlayout = (TextLayout) invoke(getTextLayout, textflow);
	GlyphList[] glyphlist = textlayout.getRuns();
	int partialselection = selectionintextflow;
	float lasty = 0;
	int lines = 0;
	for (int i = 0; i < glyphlist.length; i++) {
		GlyphList thisglyphlist = glyphlist[i];
		int glyphcount = thisglyphlist.getGlyphCount();
		if (glyphcount == 0)
			glyphcount = 1;

		if (lasty != thisglyphlist.getLocation().y) {
			lasty = thisglyphlist.getLocation().y;
			lines++;
		}
		if (thisglyphlist.getGlyphCount() >= partialselection)
			return lines;
		partialselection -= glyphcount;

	}
	return -1;
}
 
示例3
/**
 * selects the char corresponding to points x and y
 * 
 * @param x x mouse click coordinate
 * @param y y mouse clock coordinate
 * @return the closest character index if hit
 */
public int getCharSelectionOnCoordinates(double x, double y) {
	Point2D pointinscreen = new Point2D(x, y);
	Point2D localpoint = textflow.screenToLocal(pointinscreen);
	TextLayout textlayout = (TextLayout) invoke(getTextLayout, textflow);
	TextLine[] textlines = (TextLine[]) invoke(getLines, textlayout);
	logger.finest("found textlines number = " + textlines);

	HitInfo hit = textlayout.getHitInfo((float) (localpoint.getX() - textflow.getInsets().getLeft()),
			(float) (localpoint.getY() - textflow.getInsets().getLeft()));
	if (hit != null) {
		logger.finest("click detected and found hit " + hit.getCharIndex() + " - for (" + localpoint.getX() + ","
				+ localpoint.getY() + ")");

	} else {
		logger.finest("click detected and did not find hit");
	}
	if (getCharNb() > 0) {
		return hit.getCharIndex() + 1;
	} else {
		return hit.getCharIndex();
	}

}
 
示例4
protected void displaySelection() {
	logger.finest(" - DisplaySelection - Try to print " + dragstartindex + " - " + dragendindex);
	if (this.dragstartindex >= 0)
		if (this.dragendindex >= 0)
			if (this.dragendindex > this.dragstartindex) {
				hideSelection();
				TextLayout textlayout = (TextLayout) invoke(getTextLayout, textflow);
				PathElement[] highlight = textlayout.getRange(this.dragstartindex, this.dragendindex,
						TextLayout.TYPE_TEXT, 0, 0);
				hightlightpath = new Path(highlight);
				hightlightpath.setManaged(false);
				hightlightpath.setFill(Color.web("#222235", 0.2));
				hightlightpath.setStrokeWidth(0);
				if (title)
					hightlightpath.setTranslateY(6);
				if (bulletpoint) {
					hightlightpath.setTranslateY(2);
					hightlightpath.setTranslateX(5);

				}
				textflow.getChildren().add(hightlightpath);
				textflow.requestLayout();
				textflow.requestFocus();
			}
}
 
示例5
/**
 * @return true if the current selection is the first line, false else
 */
public boolean isSelectionAtFirstLine() {
	TextLayout textlayout = (TextLayout) invoke(getTextLayout, textflow);
	GlyphList[] glyphlist = textlayout.getRuns();
	if (glyphlist[0].getGlyphCount() >= selectionintextflow)
		return true;
	return false;
}
 
示例6
/**
 * @return the xoffset for the selection. Paragraphs, depending on size, have
 *         differrent offsets (titles to the left, bullet points to the right)
 */
public float getXOffSetForSelection() {

	TextLayout textlayout = (TextLayout) invoke(getTextLayout, textflow);
	GlyphList[] glyphlist = textlayout.getRuns();
	int partialselection = selectionintextflow;

	for (int i = 0; i < glyphlist.length; i++) {
		GlyphList thisglyphlist = glyphlist[i];
		int glyphcount = thisglyphlist.getGlyphCount();
		if (glyphcount == 0)
			glyphcount = 1; // this is for return carriage

		if (thisglyphlist.getGlyphCount() >= partialselection) {
			float xmargin = 0;
			if (title) {
				xmargin = 10;
			}
			if (bulletpoint) {
				xmargin = 25;
			}

			float xoffsetforchar = (float) (thisglyphlist.getGlyphCount() > 0
					? thisglyphlist.getPosX(partialselection)
					: 0) + thisglyphlist.getLocation().x - 7 + xmargin;
			return xoffsetforchar;
		}
		partialselection -= glyphcount;
	}
	return -1;
}
 
示例7
/**
 * sets the selection for the given line, with the given caret index
 * 
 * @param line       an integer between 0 and the index of last line
 * @param caretindex actual position of thecaret in pixels
 */
public void setSelectionat(int line, float caretindex) {
	logger.finest(" -- set selection at " + line + " line for caretindex = " + caretindex);
	TextLayout textlayout = (TextLayout) invoke(getTextLayout, textflow);
	GlyphList[] glyphlist = textlayout.getRuns();

	float lasty = 0;
	int currentline = 0;
	int finalcharacterselection = 0;
	for (int i = 0; i < glyphlist.length; i++) {
		GlyphList thisglyphlist = glyphlist[i];
		int glyphcount = thisglyphlist.getGlyphCount();
		if (glyphcount == 0)
			glyphcount = 1;
		if (lasty != thisglyphlist.getLocation().y) {
			lasty = thisglyphlist.getLocation().y;
			currentline++;
		}

		if (currentline == line) {
			logger.finest("Found line for glyphindex = " + i);
			int previousindex = 0;
			for (int j = 0; j < thisglyphlist.getGlyphCount(); j++) {
				float xmargin = 0;
				if (title) {
					xmargin = 10;
				}
				if (bulletpoint) {
					xmargin = 25;
				}
				Float currentx = thisglyphlist.getPosX(j) + thisglyphlist.getLocation().x - 7 + xmargin;

				if (currentx > caretindex) {
					logger.finest("  -- !!! -- weird logic currentx>caretindex " + currentx + "/" + caretindex);
					finalcharacterselection += previousindex;
					selectionintextflow = finalcharacterselection;
					logger.finest("Selection in text flow = " + selectionintextflow);
					displayCaretAt(selectionintextflow);
					return;
				} else {
					logger.finest(" -- !!! -- weird logic currentx<=caretindex " + currentx + "/" + caretindex);
				}
				previousindex++;
			}

		}
		if (currentline > line) {
			selectionintextflow = finalcharacterselection - 1;
			logger.finest("Selection in text flow = " + selectionintextflow);
			displayCaretAt(selectionintextflow);
			return;
		}
		finalcharacterselection += glyphcount;
	}
	selectionintextflow = finalcharacterselection;
	logger.finest("Selection in text flow = " + selectionintextflow);
	displayCaretAt(selectionintextflow);
	return;
}
 
示例8
protected TextLayout getTextLayout(TextFlow t)
{
	return textLayoutMethod.invoke(t);
}
 
示例9
public PathElement[] getRange(TextFlow t, int start, int end)
{
	return getTextLayout(t).getRange(start, end, TextLayout.TYPE_TEXT, 0.0f, 0.0f);
}