📄 gcstringprinter.java
字号:
} lineInfo.lineOutputed = outputLine.toString(); lines.add(lineInfo); if (DEBUG) { System.out.println("replace prev line with: " + outputLine.toString()); } } else { if (DEBUG) { System.out.println("No Excess"); } } cutoff = true; return false; } else { lines.add(lineInfo); } sLine = lineInfo.excessPos >= 0 && wrap ? sLine.substring(lineInfo.excessPos) : null; } else { if (DEBUG) { System.out.println("Line process resulted in no text: " + sLine); } lines.add(lineInfo); currentCharPos++; break; //return false; } currentCharPos += lineInfo.excessPos >= 0 ? lineInfo.excessPos : lineInfo.lineOutputed.length(); //System.out.println("output: " + lineInfo.lineOutputed.length() + ";" // + lineInfo.lineOutputed + ";xc=" + lineInfo.excessPos + ";ccp=" + currentCharPos); //System.out.println("lineo=" + lineInfo.lineOutputed.length() + ";" + sLine.length() ); } while (sLine != null); if (string.length() > posNewLine && string.charAt(posNewLine) == '\r' && string.charAt(posNewLine + 1) == '\n') { posNewLine++; } posLastNewLine = posNewLine + 1; currentCharPos = posLastNewLine; pos1 = string.indexOf('\n', posLastNewLine); pos2 = string.indexOf('\r', posLastNewLine); if (pos2 == -1) { pos2 = pos1; } posNewLine = Math.min(pos1, pos2); if (posNewLine < 0) { posNewLine = string.length(); } } } finally { if (!skipClip && !noDraw) { gc.setClipping(oldClipping); } if (lines.size() > 0) { // rebuild full text to get the exact y-extent of the output // this may be different (but shouldn't be!) than the height of each // line StringBuffer fullText = new StringBuffer(string.length() + 10); for (Iterator iter = lines.iterator(); iter.hasNext();) { LineInfo lineInfo = (LineInfo) iter.next(); if (fullText.length() > 0) { fullText.append('\n'); } fullText.append(lineInfo.lineOutputed); } //size = gc.textExtent(fullText.toString()); for (Iterator iter = lines.iterator(); iter.hasNext();) { LineInfo lineInfo = (LineInfo) iter.next(); size.x += Math.max(lineInfo.width, size.x); size.y += lineInfo.height; } if ((swtFlags & (SWT.BOTTOM)) != 0) { rectDraw.y = rectDraw.y + rectDraw.height - size.y; } else if ((swtFlags & SWT.TOP) == 0) { // center vert rectDraw.y = rectDraw.y + (rectDraw.height - size.y) / 2; } if (!noDraw || listUrlInfo != null) { for (Iterator iter = lines.iterator(); iter.hasNext();) { LineInfo lineInfo = (LineInfo) iter.next(); try { drawLine(gc, lineInfo, swtFlags, rectDraw, noDraw); } catch (Throwable t) { t.printStackTrace(); } } } } } return size.y <= printArea.height; } /** * @param hasMoreElements * @param line * * @since 3.0.0.7 */ private LineInfo processLine(final GC gc, final LineInfo lineInfo, final Rectangle printArea, final boolean wrap, final boolean fullLinesOnly, boolean hasMoreElements) { if (lineInfo.originalLine.length() == 0) { lineInfo.lineOutputed = ""; lineInfo.height = gc.stringExtent(GOOD_STRING).y; return lineInfo; } StringBuffer outputLine = new StringBuffer(); int excessPos = -1; if (images != null || lineInfo.originalLine.length() > MAX_LINE_LEN || gc.stringExtent(lineInfo.originalLine).x > printArea.width) { if (DEBUG) { System.out.println("Line to process: " + lineInfo.originalLine); } StringBuffer space = new StringBuffer(1); if (!wrap && images == null) { if (DEBUG) { System.out.println("No Wrap.. doing all in one line"); } String sProcessedLine = lineInfo.originalLine.length() > MAX_LINE_LEN ? lineInfo.originalLine.substring(0, MAX_LINE_LEN) : lineInfo.originalLine; // if it weren't for the elipses, we could do: // outputLine.append(sProcessedLine); excessPos = processWord(gc, lineInfo.originalLine, sProcessedLine, printArea, wrap, lineInfo, outputLine, space); } else { int posLastWordStart = 0; int posWordStart = lineInfo.originalLine.indexOf(' '); if (posWordStart < 0) { posWordStart = lineInfo.originalLine.length(); } // Process line word by word int curPos = 0; while (posWordStart >= 0 && posLastWordStart < lineInfo.originalLine.length()) { String word = lineInfo.originalLine.substring(posLastWordStart, posWordStart); if (word.length() == 0) { excessPos = -1; outputLine.append(' '); } for (int i = 0; i < word.length(); i += MAX_WORD_LEN) { String subWord; int endPos = i + MAX_WORD_LEN; if (endPos > word.length()) { subWord = word.substring(i); } else { subWord = word.substring(i, endPos); } excessPos = processWord(gc, lineInfo.originalLine, subWord, printArea, wrap, lineInfo, outputLine, space); if (DEBUG) { System.out.println(" with word [" + subWord + "] len is " + lineInfo.width + "(" + printArea.width + ") w/excess " + excessPos); } if (excessPos >= 0) { excessPos += curPos; break; } if (endPos <= word.length()) { space.setLength(0); } curPos += subWord.length() + 1; } if (excessPos >= 0) { break; } posLastWordStart = posWordStart + 1; posWordStart = lineInfo.originalLine.indexOf(' ', posLastWordStart); if (posWordStart < 0) { posWordStart = lineInfo.originalLine.length(); } } } } else { outputLine.append(lineInfo.originalLine); } if (!wrap && hasMoreElements && excessPos >= 0) { outputLine.replace(outputLine.length() - 1, outputLine.length(), ".."); cutoff = true; } //drawLine(gc, outputLine, swtFlags, rectDraw); // if (!wrap) { // return hasMoreElements; // } lineInfo.excessPos = excessPos; lineInfo.lineOutputed = outputLine.toString(); return lineInfo; } /** * @param int Position of part of word that didn't fit * * @since 3.0.0.7 */ private int processWord(final GC gc, final String sLine, String word, final Rectangle printArea, final boolean wrap, final LineInfo lineInfo, StringBuffer outputLine, final StringBuffer space) { if (word.length() == 0) { space.append(' '); return -1; } //System.out.println("PW: " + word); if (images != null && word.length() >= 2 && word.charAt(0) == '%') { int imgIdx = word.charAt(1) - '0'; if (images.length > imgIdx && imgIdx >= 0 && images[imgIdx] != null) { Image img = images[imgIdx]; Rectangle bounds = img.getBounds(); if (imageScales != null && imageScales.length > imgIdx) { bounds.width = (int) (bounds.width * imageScales[imgIdx]); bounds.height = (int) (bounds.height * imageScales[imgIdx]); } Point spaceExtent = gc.stringExtent(space.toString()); int newWidth = lineInfo.width + bounds.width + spaceExtent.x; if (newWidth > printArea.width) { if (bounds.width + spaceExtent.x < printArea.width || lineInfo.width > 0) { return 0; } } if (lineInfo.imageIndexes == null) { lineInfo.imageIndexes = new int[] { imgIdx }; } lineInfo.width = newWidth; lineInfo.height = Math.max(bounds.height, lineInfo.height); outputLine.append(space); outputLine.append(word); if (space.length() > 0) { space.delete(0, space.length()); } space.append(' '); return -1; } } Point ptWordSize = gc.stringExtent(word + " "); boolean bWordLargerThanWidth = ptWordSize.x > printArea.width; int targetWidth = lineInfo.width + ptWordSize.x; if (targetWidth > printArea.width) { // word is longer than space avail, split int endIndex = word.length(); long diff = endIndex; while (targetWidth != printArea.width) { diff = (diff >> 1) + (diff % 2); if (diff <= 0) { diff = 1; } //System.out.println("diff=" + diff + ";e=" + endIndex + ";tw=" + targetWidth + ";paw= " + printArea.width); if (targetWidth > printArea.width) { endIndex -= diff; if (endIndex < 1) { endIndex = 1; } } else { endIndex += diff; if (endIndex > word.length()) { endIndex = word.length(); } } ptWordSize = gc.stringExtent(word.substring(0, endIndex) + " "); targetWidth = lineInfo.width + ptWordSize.x; if (diff <= 1) { break; } } ; if (endIndex == 0) { endIndex = 1; } if (targetWidth > printArea.width && endIndex > 1) { endIndex--; ptWordSize = gc.stringExtent(word.substring(0, endIndex) + " "); } if (DEBUG) { System.out.println("excess starts at " + endIndex + "(" + ptWordSize.x + "px) of " + word.length() + ". " + (ptWordSize.x + lineInfo.width) + "/" + printArea.width + "; wrap?" + wrap); } if (endIndex > 0 && outputLine.length() > 0) { outputLine.append(space); } if (endIndex == 0 && outputLine.length() == 0) { endIndex = 1; } if (wrap && ptWordSize.x < printArea.width && !bWordLargerThanWidth) { // whole word is excess return 0; } outputLine.append(word.substring(0, endIndex)); if (!wrap) { int len = outputLine.length(); if (len == 0) { if (word.length() > 0) { outputLine.append(word.charAt(0)); } else if (sLine.length() > 0) { outputLine.append(sLine.charAt(0)); } } else if (len > 1) { outputLine.replace(outputLine.length() - 1, outputLine.length(), ".."); cutoff = true; } } //drawLine(gc, outputLine, swtFlags, rectDraw); if (DEBUG) { System.out.println("excess " + word.substring(endIndex)); } return endIndex; } lineInfo.width += ptWordSize.x; if (lineInfo.width > printArea.width) { if (space.length() > 0) { space.delete(0, space.length()); } if (!wrap) { int len = outputLine.length(); if (len == 0) { if (word.length() > 0) { outputLine.append(word.charAt(0)); } else if (sLine.length() > 0) { outputLine.append(sLine.charAt(0)); } } else if (len > 1) { outputLine.replace(outputLine.length() - 1, outputLine.length(), ".."); cutoff = true; } return -1; } else { return 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -