text.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 1,274 行 · 第 1/3 页
JAVA
1,274 行
return true; } /** * Paints text from a TextInfo structure. * * @param info the TextInfo struct * @param g the Graphics to paint with * @param str the text to paint * @param font the font to use in painting the text * @param fgColor foreground color * @param fgHColor foreground hilight color * @param w the available width for the text * @param h the available height for the text * @param offset the first line pixel offset * @param options any of NORMAL | INVERT | HYPERLINK | TRUNCATE * @param cursor text cursor object */ public static void paintText(TextInfo info, Graphics g, String str, Font font, int fgColor, int fgHColor, int w, int h, int offset, int options, TextCursor cursor) { // NOTE paint not called if TextInfo struct fails g.setFont(font); g.setColor(fgColor); char[] text = str.toCharArray(); int fontHeight = font.getHeight(); if (cursor != null && cursor.visible == false) { cursor = null; } int currentLine = info.topVis; int height = currentLine * fontHeight; int y = 0; if (ScreenSkin.RL_DIRECTION) { offset = w - offset; } while (currentLine < (info.topVis + info.visLines)) { height += fontHeight; y += fontHeight; g.drawChars(text, info.lineStart[currentLine], info.lineEnd[currentLine] - info.lineStart[currentLine], offset, y, Graphics.BOTTOM | ScreenSkin.TEXT_ORIENT); // draw the vertical cursor indicator if needed // update the cursor.x and cursor.y info if (cursor != null && cursor.option == PAINT_USE_CURSOR_INDEX && cursor.index >= info.lineStart[currentLine] && cursor.index <= info.lineEnd[currentLine]) { int off = offset; if (cursor.index > info.lineStart[currentLine]) { if (ScreenSkin.RL_DIRECTION) { off -= font.charsWidth(text, info.lineStart[currentLine], cursor.index - info.lineStart[currentLine]); } else { off += font.charsWidth(text, info.lineStart[currentLine], cursor.index - info.lineStart[currentLine]); } } cursor.x = off; cursor.y = height; cursor.width = 1; // IMPL_NOTE: must these always be set? cursor.height = fontHeight; cursor.paint(g); cursor = null; } if (ScreenSkin.RL_DIRECTION) { offset = w; } else { offset = 0; } currentLine++; } } /** * Paints the text, linewrapping when necessary. * * @param g the Graphics to use to paint with * @param str the text to paint * @param font the font to use to paint the text * @param fgColor foreground color * @param fgHColor foreground highlight color * @param w the available width for the text * @param h the available height for the text * @param offset the first line pixel offset * @param options any of NORMAL | INVERT | HYPERLINK | TRUNCATE * @param cursor text cursor object to use to draw vertical bar * @return the width of the last line painted */ public static int paint(Graphics g, String str, Font font, int fgColor, int fgHColor, int w, int h, int offset, int options, TextCursor cursor) { if (w <= 0 || (cursor == null && (str == null || str.length() == 0))) { return 0; } if (str == null) { str = ""; } g.setFont(font); g.setColor(fgColor); char[] text = str.toCharArray(); int fontHeight = font.getHeight(); if (cursor != null && cursor.visible == false) { cursor = null; } int[] inout = initGNL(font, w, h, options, offset ); if (ScreenSkin.RL_DIRECTION) { offset = w - offset; } int numLines = 0; int height = 0; do { numLines++; height += fontHeight; if (height > h) { break; } inout[GNL_NUM_LINES] = numLines; boolean truncate = getNextLine(text, font, inout); int lineStart = inout[GNL_LINE_START]; int lineEnd = inout[GNL_LINE_END]; int newLineStart = inout[GNL_NEW_LINE_START]; // // now we can get around to actually draw the text // lineStart is the array index of the first character to // start drawing, while lineEnd is the index just after // the last character to draw. // if (lineEnd > lineStart) { if ((options & INVERT) == INVERT) { g.setColor(fgHColor); } else { g.setColor(fgColor); } if ((options & HYPERLINK) == HYPERLINK) { drawHyperLink(g, offset, height, inout[GNL_LINE_WIDTH]); } // // we are given x,y coordinates and we must calculate // the best array index to put the cursor // if (cursor != null && cursor.option == PAINT_GET_CURSOR_INDEX && cursor.x >= 0 && cursor.y == height) { int bestIndex = lineStart; int bestX = offset; int curX = offset; int curY = height; // // draw one character at a time and check its position // against the supplied coordinates in cursor // for (int i = lineStart; i < lineEnd; i++) { char ch = text[i]; g.drawChar(ch, curX, curY, Graphics.BOTTOM | ScreenSkin.TEXT_ORIENT); if (Math.abs(curX - cursor.preferredX) < Math.abs(bestX - cursor.preferredX)) { bestIndex = i; bestX = curX; } if (ScreenSkin.RL_DIRECTION) { curX -= font.charWidth(ch); } else { curX += font.charWidth(ch); } } if (Math.abs(curX - cursor.preferredX) < Math.abs(bestX - cursor.preferredX)) { bestIndex = lineEnd; bestX = curX; } // // draw the ellipsis // if (truncate) { g.drawChar(truncationMark, curX, curY, Graphics.BOTTOM | ScreenSkin.TEXT_ORIENT); } cursor.index = bestIndex; cursor.x = bestX; cursor.y = height; cursor.option = PAINT_USE_CURSOR_INDEX; } else { g.drawChars(text, lineStart, lineEnd - lineStart, offset, height, Graphics.BOTTOM | ScreenSkin.TEXT_ORIENT); // // draw the ellipsis // if (truncate) { g.drawChar(truncationMark, offset + font.charsWidth( text, lineStart, lineEnd), height, Graphics.BOTTOM | ScreenSkin.TEXT_ORIENT); } } } // // try to draw a vertical cursor indicator // if (cursor != null && cursor.option == PAINT_USE_CURSOR_INDEX && cursor.index >= lineStart && cursor.index <= lineEnd) { int off = offset; if (cursor.index > lineStart) { off += font.charsWidth(text, lineStart, cursor.index - lineStart); } cursor.x = off; cursor.y = height; cursor.width = 1; cursor.height = fontHeight; cursor.paint(g); cursor = null; } inout[GNL_LINE_START] = newLineStart; inout[GNL_OFFSET] = 0; if (ScreenSkin.RL_DIRECTION) { offset = w; } else { offset = 0; } } while (inout[GNL_LINE_END] < text.length); return inout[GNL_LINE_WIDTH]; } /** * Draws a hyperlink image. * * @param g the graphics to use to draw the image * @param x the x location of the image * @param y the y location of the image * @param w the width of the hyperlink image */ public static void drawHyperLink(Graphics g, int x, int y, int w) { if (StringItemSkin.IMAGE_LINK == null) { // System.err.println("Hyperlink image is null"); return; } int linkHeight = StringItemSkin.IMAGE_LINK.getHeight(); int linkWidth = StringItemSkin.IMAGE_LINK.getWidth(); int oldClipX = g.getClipX(); int oldClipW = g.getClipWidth(); int oldClipY = g.getClipY(); int oldClipH = g.getClipHeight(); if (ScreenSkin.RL_DIRECTION) { x -= w; } g.clipRect(x, oldClipY, w, oldClipH); // Then, loop from the end of the string to the beginning, // drawing the image as we go for (int j = x + w - linkWidth, first = x - linkWidth; j > first; j -= linkWidth) { g.drawImage(StringItemSkin.IMAGE_LINK, j, y, Graphics.BOTTOM | ScreenSkin.TEXT_ORIENT); } g.setClip(oldClipX, oldClipY, oldClipW, oldClipH); } /** * Gets the height in pixels and the width of the widest line in pixels * for the given string, calculated based on the availableWidth. * size[WIDTH] and size[HEIGHT] should be set by this method. * @param size The array that holds Item content size and location * in Item internal bounds coordinate system. * @param availableWidth The width available for this Item * @param str the string to render * @param font the font to use to render the string * @param offset the pixel offset for the first line * */ public static void getSizeForWidth(int[] size, int availableWidth, String str, Font font, int offset) { // Case 0: null or empty string, no height if (str == null || str.length() == 0 || availableWidth <= 0) { size[HEIGHT] = 0; size[WIDTH] = 0; return; } char[] text = str.toCharArray(); int[] inout = initGNL(font, availableWidth, 0, Text.NORMAL, offset); int numLines = 0; int widest = 0; boolean widthFound = false; do { numLines++; inout[GNL_NUM_LINES] = numLines; getNextLine(text, font, inout); if (!widthFound) { // a long line with no spaces if (inout[GNL_LINE_WIDTH] > availableWidth && offset == 0) { widest = availableWidth; widthFound = true; } else if (inout[GNL_LINE_WIDTH] > widest) { widest = inout[GNL_LINE_WIDTH]; } } inout[GNL_LINE_START] = inout[GNL_NEW_LINE_START]; inout[GNL_OFFSET] = 0; } while (inout[GNL_LINE_END] < text.length); size[WIDTH] = widest; size[HEIGHT] = font.getHeight() * numLines; // return values in size[] } /** * Gets the height in pixels to render the given string. * @param str the string to render * @param font the font to use to render the string * @param w the available width for the string * @param offset the pixel offset for the first line * @return the height in pixels required to render this string completely */ // IMPL_NOTE - could remove and use getSizeForWidth() public static int getHeightForWidth(String str, Font font, int w, int offset) { int[] tmpSize = new int[] {0, 0, 0, 0}; getSizeForWidth(tmpSize, w, str, font, offset); return tmpSize[HEIGHT]; } /** * Utility method to retrieve the length of the longest line of the * text given the width. this may not necessarily be the entire * string if there are line breaks or word wraps. * * @param str the String to use. * @param offset a pixel offset for the first line * @param width the available width for the text * @param font the font to render the text in * @return the length of the longest line given the width */ // IMPL_NOTE - could remove and use getSizeForWidth() public static int getWidestLineWidth(String str, int offset, int width, Font font) { int[] tmpSize = new int[] {0, 0, 0, 0}; if (ScreenSkin.RL_DIRECTION) { offset = width - offset; } getSizeForWidth(tmpSize, width, str, font, offset); return tmpSize[WIDTH]; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?