📄 text.java
字号:
int width, Font font) { int numLines = 0; if (text == null || text.length == 0) { return 0; } int[] inout = new int[GNL_NUM_PARAMS]; inout[GNL_FONT_HEIGHT] = font.getHeight(); inout[GNL_WIDTH] = width; inout[GNL_OPTIONS] = Text.NORMAL; inout[GNL_ELLIP_WIDTH] = font.charsWidth(ellipsis, 0, 3); inout[GNL_LINE_START] = 0; inout[GNL_LINE_END] = 0; inout[GNL_NEW_LINE_START] = 0; inout[GNL_OFFSET] = offset; inout[GNL_LINE_WIDTH] = 0; int widest = 0; do { numLines++; inout[GNL_NUM_LINES] = numLines; getNextLine(text, font, inout); if (inout[GNL_LINE_WIDTH] > width && offset == 0) { return width; } 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); return widest; } /** * Calculate the starting and ending points for a new line of * text given the font and input parameters. Beware of the * multiple returns statements within the body. * * @param text text to process. this must not be null * @param font font to use for width information * @param inout an array of input parameters corresponing to the * GNL_ constants * @return true if the text had to be truncated, false otherwise */ private static boolean getNextLine(char[] text, Font font, int[] inout) { // // this inner loop will set lineEnd and newLineStart to // the proper values so that a line is broken correctly // int curLoc = inout[GNL_LINE_START]; boolean foundBreak = false; int leftWidth = 0; inout[GNL_LINE_WIDTH] = 0; while (curLoc < text.length) { // // a newLine forces a break and immediately terminates // the loop // // a space will be remembered as a possible place to break // if (text[curLoc] == '\n') { inout[GNL_LINE_END] = curLoc; inout[GNL_NEW_LINE_START] = curLoc + 1; break; } else if (text[curLoc] == ' ') { inout[GNL_LINE_END] = curLoc; inout[GNL_NEW_LINE_START] = curLoc + 1; foundBreak = true; } // // if the text is longer than one line then we // cut the word at a word boundary if possible, // otherwise the word is broken. // inout[GNL_LINE_WIDTH] += font.charWidth(text[curLoc]); if (((inout[GNL_OPTIONS] & TRUNCATE) == TRUNCATE) && ((inout[GNL_NUM_LINES] + 1) * inout[GNL_FONT_HEIGHT] > inout[GNL_HEIGHT]) && (inout[GNL_LINE_WIDTH] + inout[GNL_OFFSET] + inout[GNL_ELLIP_WIDTH] > inout[GNL_WIDTH])) { leftWidth = font.charsWidth(text, curLoc + 1, text.length - curLoc - 1); // // we are on the last line and at the point where // we will need to put an ellipsis if we can't fit // the rest of the line // // if the rest of the line will fit, then don't // put an ellipsis // if (inout[GNL_OFFSET] + inout[GNL_LINE_WIDTH] + leftWidth > inout[GNL_WIDTH]) { inout[GNL_LINE_WIDTH] += inout[GNL_ELLIP_WIDTH]; /* if (!foundBreak) { inout[GNL_LINE_END] = curLoc; inout[GNL_NEW_LINE_START] = curLoc; } */ inout[GNL_LINE_END] = curLoc; inout[GNL_NEW_LINE_START] = curLoc; return true; } else { inout[GNL_LINE_WIDTH] += leftWidth; inout[GNL_LINE_END] = text.length; inout[GNL_NEW_LINE_START] = text.length; return false; } } else if (inout[GNL_OFFSET] + inout[GNL_LINE_WIDTH] > inout[GNL_WIDTH]) { if (!foundBreak) { if (inout[GNL_OFFSET] > 0) { // move to the next line which will have 0 offset inout[GNL_LINE_END] = inout[GNL_LINE_START]; inout[GNL_NEW_LINE_START] = inout[GNL_LINE_START]; } else { // the line is too long and we need to break it inout[GNL_LINE_END] = curLoc; inout[GNL_NEW_LINE_START] = curLoc; } } return false; } curLoc++; } inout[GNL_LINE_END] = curLoc; return false; } /** * Utility method to calculate the width in which 2 strings can fit * given the strings, fonts and maximum width in which those strings * should fit. Returned value is either the passed in width or * a smaller. * The offset in pixels for the first string is 0, second string is * laid out right after the first one with padding in between * equal to the passed in value. * * @param firstStr the first string to use. * @param secondStr the first string to use. * @param width the available width for the text * @param firstFont the font to render the first string in * @param secondFont the font to render the second string in * @param pad the padding that should be used between strings * @return the width in which both strings would fit * given the maximum width */ public static int getTwoStringsWidth(String firstStr, String secondStr, Font firstFont, Font secondFont, int width, int pad) { if (((firstStr == null || firstStr.length() == 0) && (secondStr == null || secondStr.length() == 0)) || (width <= 0)) { return 0; } int[] inout = new int[GNL_NUM_PARAMS]; char[] text; int offset = 0; int widest = 0; int numLines = 0; if (firstStr != null && firstStr.length() > 0) { text = firstStr.toCharArray(); inout[GNL_FONT_HEIGHT] = firstFont.getHeight(); inout[GNL_WIDTH] = width; inout[GNL_OPTIONS] = Text.NORMAL; inout[GNL_ELLIP_WIDTH] = firstFont.charsWidth(ellipsis, 0, 3); inout[GNL_LINE_START] = 0; inout[GNL_LINE_END] = 0; inout[GNL_NEW_LINE_START] = 0; inout[GNL_OFFSET] = offset; inout[GNL_LINE_WIDTH] = 0; do { numLines++; inout[GNL_NUM_LINES] = numLines; getNextLine(text, firstFont, inout); 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] < firstStr.length()); offset = inout[GNL_LINE_WIDTH]; } if (secondStr != null && secondStr.length() > 0) { if (offset > 0) { offset += pad; } text = secondStr.toCharArray(); if (numLines > 0) { numLines--; } inout[GNL_FONT_HEIGHT] = secondFont.getHeight(); inout[GNL_WIDTH] = width; inout[GNL_OPTIONS] = Text.NORMAL; inout[GNL_ELLIP_WIDTH] = secondFont.charsWidth(ellipsis, 0, 3); inout[GNL_LINE_START] = 0; inout[GNL_LINE_END] = 0; inout[GNL_NEW_LINE_START] = 0; inout[GNL_OFFSET] = offset; inout[GNL_LINE_WIDTH] = 0; do { numLines++; inout[GNL_NUM_LINES] = numLines; getNextLine(text, secondFont, inout); if (inout[GNL_OFFSET] + inout[GNL_LINE_WIDTH] > widest) { widest = inout[GNL_OFFSET] + inout[GNL_LINE_WIDTH]; } inout[GNL_LINE_START] = inout[GNL_NEW_LINE_START]; inout[GNL_OFFSET] = 0; } while (inout[GNL_LINE_END] < secondStr.length()); } return widest; } /** * Utility method to calculate the heightin which two strings can fit * given the strings, fonts and available width. * The offset in pixels for the first string is 0, second string is * laid out right after the first one with padding in between * equal to the passed in value. * * @param firstStr the first string to use (can be null or empty) * @param secondStr the first string to use (can be null or empty) * @param width the available width for the text * @param firstFont the font to render the first string in (non-null) * @param secondFont the font to render the second string in (non-null) * @param pad the padding that should be used between strings * @return the height in which both strings would fit * given the passed in width */ public static int getTwoStringsHeight(String firstStr, String secondStr, Font firstFont, Font secondFont, int width, int pad) { if (((firstStr == null || firstStr.length() == 0) && (secondStr == null || secondStr.length() == 0)) || (width <= 0)) { return 0; } int[] inout = new int[GNL_NUM_PARAMS]; char[] text; int offset = 0; int widest = 0; int numLines = 0; int height = 0; int fontHeight = 0; if (firstStr != null && firstStr.length() > 0) { text = firstStr.toCharArray(); fontHeight = firstFont.getHeight(); inout[GNL_FONT_HEIGHT] = fontHeight; inout[GNL_WIDTH] = width; inout[GNL_OPTIONS] = Text.NORMAL; inout[GNL_ELLIP_WIDTH] = firstFont.charsWidth(ellipsis, 0, 3); inout[GNL_LINE_START] = 0; inout[GNL_LINE_END] = 0; inout[GNL_NEW_LINE_START] = 0; inout[GNL_OFFSET] = 0; inout[GNL_LINE_WIDTH] = 0; do { numLines++; height += fontHeight; inout[GNL_NUM_LINES] = numLines; getNextLine(text, firstFont, inout); inout[GNL_LINE_START] = inout[GNL_NEW_LINE_START]; } while (inout[GNL_LINE_END] < firstStr.length()); offset = inout[GNL_LINE_WIDTH]; if (secondStr == null || secondStr.length() == 0) { // last \n in the two strings should be ignored if (firstStr.charAt(firstStr.length() - 1) == '\n') { height -= fontHeight; } return height; } } // Second string is not null and it is not empty if (secondStr != null && secondStr.length() > 0) { if (offset > 0) { offset += pad; } text = secondStr.toCharArray(); fontHeight = secondFont.getHeight(); // Line that has the end of the first string and the beginning // of the second one is a special one; // We have to make sure that it is not counted twice and that // the right font height is beeing added (the max of the two) if (numLines > 0) { numLines--; if (inout[GNL_FONT_HEIGHT] > fontHeight) { height -= fontHeight; } else { height -= inout[GNL_FONT_HEIGHT]; } } inout[GNL_FONT_HEIGHT] = fontHeight; inout[GNL_WIDTH] = width; inout[GNL_OPTIONS] = Text.NORMAL; inout[GNL_ELLIP_WIDTH] = secondFont.charsWidth(ellipsis, 0, 3); inout[GNL_LINE_START] = 0; inout[GNL_LINE_END] = 0; inout[GNL_NEW_LINE_START] = 0; inout[GNL_OFFSET] = offset; inout[GNL_LINE_WIDTH] = 0; do { numLines++; height += fontHeight; inout[GNL_NUM_LINES] = numLines; getNextLine(text, secondFont, inout); inout[GNL_LINE_START] = inout[GNL_NEW_LINE_START]; inout[GNL_OFFSET] = 0; } while (inout[GNL_LINE_END] < secondStr.length()); // last \n should be ignored if (secondStr.charAt(secondStr.length() - 1) == '\n') { height -= fontHeight; } } return height; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -