📄 int.html
字号:
on either side.Spaces are either left-to-right or right-to-left characters depending on what is next to them. If the characters on both sides of a space are the same kind of character, the space is that kind of character too. Spaces between Arabic words are treated like Arabic characters, and spaces between English words are treated like English characters. When the characters on both sides are different, spaces are treated like the overall direction of the paragraph:If the paragraph as a whole is left-to-right, the space is left-to-right, and if the paragraph as a whole is right-to-left, the space is right-to-left.<P>In the Hit Test sample, the overall text is right-to-left. The spaces on each side of <I>Hello</I> each have one neighbor that is left-to-right (the English) and one that is right-to-left (the Hebrew). Because the text is right-to-left,the spaces are right-to-left too, and the split carets appear next to the <I>o</I> and <I>H</I> because the spaces being right-to-left belong to the right-to-left text on either side. <P><A NAME="testing"></A><H3>Hit Testing</H3>In code, a point returned by a mouse click is passed to the <CODE>TextLayout.hitTestChar</CODE> method, which returns a <CODE>TextHitInfo</CODE> object that represents the character and side of the character where the end user clicked. If the end user clicks on the <I>o</I>, the hit is to position 5.<P><IMG SRC="./Art/RightCO.gif"><P>However, in the source text, position 5 is before the <I>H</I>, sothe <CODE>TextLayout</CODE> object checks the <CODE>TextHitInfo</CODE>object to find out what side of the character the hit is on and displays the dual carets if the hit is on the side of the <I>o</I> towardsthe Hebrew. If the hit is on the side of the <I>o</I> towards the<I>l</I>, a single caret is drawn between the <I>l</I> and the<I>o</I> to indicate that the end user will insert English textat that location.<P>The source text is initialized the way the words are spoken, andnot the way they are printed. The source text looks like this:<PRE>"\u05D0\u05E0\u05D9 Hello \u05DC\u05D0 \u05DE\u05D1\u05D9\ u05DF " +"\u05E2\u05D1\u05E8\u05D9\u05EA Arabic \u0644\u0645\u062C\ u0645\u0648\u0639\u0629", map);</PRE><P>The first three unicode characters and the space <CODE>\u05D0\u05E0\u05D9 </CODE>define what you see on the display starting on the right side up to the <I>o</I>.You can see that the <I>H</I> is next in the source code, but the <I>o</I> isnext on the display. This is because in the right-to-left run, the Englishword <I>Hello</I> is turned around to display correctly in an embeddedleft-to-right run. <P>Determining the location of the insertion point is taken care ofby the <CODE>TextLayout</CODE> object. All you need to do in your code is specify the caret colors, get the mouse click location, return a <CODE>TextHitInfo</CODE> object, and draw the layout and carets. <P>Here is the code to initialize the colors for the strongand weak carets:<BLOCKQUOTE><PRE>private static final Color STRONG_CARET_COLOR = Color.red;private static final Color WEAK_CARET_COLOR = Color.black;</PRE></BLOCKQUOTE>Here is the code that draws the <CODE>TextLayout</CODE> andthe carets. The <CODE>insertionPoint</CODE> variable isretrieved from a <CODE>TextHitInfo</CODE> object in the<CODE>HitTestMouseListener</CODE> method.If the insertion point is not between text running in differentdirections, only the strong caret draws.<PRE>// Draw textLayout.textLayout.draw(graphics2D, 0, 0);// Retrieve caret Shapes for insertionIndex.Shape[] carets = textLayout.getCaretShapes(insertionIndex);// Draw the carets. carets[0] is the strong caret, and// is never null. carets[1], if it is not null, is the// weak caret.graphics2D.setColor(STRONG_CARET_COLOR);graphics2D.draw(carets[0]);if (carets[1] != null) { graphics2D.setColor(WEAK_CARET_COLOR); graphics2D.draw(carets[1]);}</PRE>Here is the <CODE>HitTestMouseListener</CODE> method: <PRE>private class HitTestMouseListener extends MouseAdapter { /** * Compute the character position of the mouse click. */ public void mouseClicked(MouseEvent e) { Point2D origin = computeLayoutOrigin(); // Compute the mouse click location relative to // textLayout's origin. float clickX = (float) (e.getX() - origin.getX()); float clickY = (float) (e.getY() - origin.getY()); // Get the character position of the mouse click. TextHitInfo currentHit = textLayout.hitTestChar(clickX, clickY); insertionIndex = currentHit.getInsertionIndex(); // Repaint the Component so the new caret(s) // will be displayed. repaint(); }}</PRE>And here is the complete <A HREF="./Code/HitTestSample.java">HitTestSample.java</A>source code.<A NAME="highlighting"></A><H3>Selection Highlighting</H3>The next figure shows how acontiguous range of characters in the source text (on the top)might not map to a contiguous highlight region (on the bottom)on screen if the selection range includes left-to-right and right-to-leftcharacters. When the Arabic text is turned around to run right-to-left onthe display, the selected portion of the Arabic text is not contiguous with the <I>is</I> and space before it, althout these charactersare contiguous in the source text.<P><IMG SRC="./Art/lhigh.gif"><P>The next figure shows how a contiguous highlight region on the display (on the bottom) might not map to a single contiguous rangeof characters in the source text (on the top).This point is illustrated in the next figure.<P><IMG SRC="./Art/vhigh.gif"><P>A <CODE>TextLayout</CODE> object provides two strategies for selection highlighting to handle these two situations: logical highlighting and visualhighlighting.<P>With logical highlighting, the selected characters are always contiguousin the source text, and the highlight region is allowed to be discontiguouson the display. With visual highlighting, there might be more than onerange of selected characters, but the highlight region is always contiguous. <P>Logical highlighting is simpler for programmers to use because the selectedcharacters are always contiguous in the source. The<CODE>TextLayout.getLogicalHighlightShape</CODE> method takes two insertionoffsets and returns a <CODE>Shape</CODE> that represents the highlightregion marked by the two offsets. A recommended way to show highlightingis to fill the <CODE>Shape</CODE> with the highlight color, and then drawthe <CODE>TextLayout</CODE> over the <CODE>Shape</CODE>.<P>Here is the code to get and draw the selection range:<PRE>// Retrieve highlight region for selection range.Shape highlight = textLayout.getLogicalHighlightShape( anchorEnd, activeEnd);// Fill the highlight region with the highlight color.graphics2D.setColor(HIGHLIGHT_COLOR);graphics2D.fill(highlight);</PRE>Here is the complete <A HREF="./Code/SelectionSample.java">SelectionSample.java</A>source code.<A NAME="moving"></A><H3>Moving the Caret</H3>In bidirectional text, the cursor should move smoothly throughthe text on the display in the direction that corresponds to the direction of the Arrow key being pressed. The problem, is that right-to-left textis positioned in the source text in the direction it is spokenand not in the direction it is displayed. For a caret tohave a smooth journey across the display, the character offsetdoes not move smoothly through the source text. This point isillustrated by the figure. <P><IMG SRC="./Art/image33.gif"><P>Progressing through the three screen positions shown on the bottomof the figure from left-to-right corresponds to progressing throughthe character offsets in the source text in the order of 7, 19, and 18.<P>The <CODE>TextLayout</CODE> object handles the details for you. All your code needs to do is update the insertion index in response toan arrow key press:<PRE>private class ArrowKeyListener extends KeyAdapter { /*** Update the insertion index in response to an arrow key. */ private void handleArrowKey(boolean rightArrow) { TextHitInfo newPosition; if (rightArrow) { newPosition = textLayout.getNextRightHit(insertionIndex); } else { newPosition = textLayout.getNextLeftHit(insertionIndex); } // getNextRightHit() / getNextLeftHit() will // return null if there is not a caret position // to the right (left) of the current position. if (newPosition != null) { // Update insertionIndex. insertionIndex = newPosition.getInsertionIndex(); // Repaint the Component so the new caret(s) // will be displayed. repaint(); } } public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); if (keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_RIGHT) { handleArrowKey(keyCode == KeyEvent.VK_RIGHT); } }}</PRE>Here is the complete <A HREF="./Code/ArrowKeySample.java">ArrowKeySample.java</A>source code.<A NAME="multi"></A><H3>Multiline Text</H3>As you learned in the <A HREF="style.html#multiple">Draw MultipleLines of Text</A> section of Lesson 2,a <CODE>LineBreakMeasurer</CODE> breaks a paragraphof styled text into lines to fit into the display area. The <CODE>LineBreakMeasurer</CODE>object encapsulates enough information about bidirectionaltext to produce a correct <CODE>TextLayout</CODE> without any additionalcode on your part.<P>Here is the complete <A HREF="./Code/LineBreakSample.java">LineBreakSample.java</A> source code. </FONT></TD></TR></TABLE><!-- ================ --><!-- End Main Content --><!-- ================ --></FONT></TD></TR></TABLE><!-- Copyright Insert --><BR CLEAR="ALL"><FORM ACTION="/cgi-bin/search.cgi" METHOD="POST"><TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="5"> <TR> <TD VALIGN="TOP"> <P ALIGN=CENTER> <FONT SIZE="-1" COLOR="#999999" FACE="Verdana, Arial, Helvetica, sans-serif"> [ This page was updated: <!-- new date --> 12-Nov-99 ]</font></P> </TD> </TR> <TR> <TD BGCOLOR="#CCCCCC"> <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD> </TR> <TR> <TD> <CENTER> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> <A HREF="http://java.sun.com/products/">Products & APIs</A> | <A HREF="/developer/index.html">Developer Connection</A> | <A HREF="/developer/infodocs/index.shtml">Docs & Training</A> | <A HREF="/developer/support/index.html">Online Support</A><BR> <A HREF="/developer/community/index.html">Community Discussion</A> | <A HREF="http://java.sun.com/industry/">Industry News</A> | <A HREF="http://java.sun.com/solutions">Solutions Marketplace</A> | <A HREF="http://java.sun.com/casestudies">Case Studies</A> </FONT> </CENTER> </TD> </TR> <TR> <TD BGCOLOR="#CCCCCC"> <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD> </TR> <TR> <TD ALIGN="CENTER"> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> <A HREF="http://java.sun.com/docs/glossary.html">Glossary</A> - <A HREF="http://java.sun.com/applets/">Applets</A> - <A HREF="http://java.sun.com/docs/books/tutorial/">Tutorial</A> - <A HREF="http://java.sun.com/jobs/">Employment</A> - <A HREF="http://java.sun.com/nav/business/">Business & Licensing</A> - <A HREF="http://java.sun.com/javastore/">Java Store</A> - <A HREF="http://java.sun.com/casestudies/">Java in the Real World</A> </FONT> </TD> </TR> <TR> <TD> <CENTER> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> <a href="/siteinfo/faq.html">FAQ</a> | <a href="/feedback/index.html">Feedback</a> | <a href="http://www.dynamicdiagrams.net/mapa/cgi-bin/help.tcl?db=javasoft&dest=http://java.sun.com/">Map</a> | <A HREF="http://java.sun.com/a-z/index.html">A-Z Index</A> </FONT> </CENTER> </TD> </TR> <TR> <TD> <TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="0"> <TR> <TD WIDTH="50%"> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> For more information on Java technology<BR> and other software from Sun Microsystems, call:<BR> </FONT> <FONT SIZE="-1" FACE="Verdana, Arial, Helvetica, sans-serif"> (800) 786-7638<BR></FONT> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> Outside the U.S. and Canada, dial your country's <A HREF="http://www.att.com/business_traveler/attdirecttollfree/">AT&T Direct Access Number</A> first.<BR> </FONT> </TD> <TD ALIGN="RIGHT" WIDTH="50%"> <A HREF="http://www.sun.com"><IMG SRC="/images/lgsun.gif" width="64" height="30" border="0" ALT="Sun Microsystems, Inc."></A><BR> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> Copyright © 1995-99 <A HREF="http://www.sun.com">Sun Microsystems, Inc.</A><BR> All Rights Reserved. <a href="http://www.sun.com/share/text/SMICopyright.html">Legal Terms</a>. <A HREF="http://www.sun.com/privacy/">Privacy Policy</A>. </FONT> </TD> </TR> </TABLE> </TD> </TR> </TABLE></FORM><!-- End Copyright Insert --></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -