📄 gcstringprinter.java
字号:
} //drawLine(gc, outputLine, swtFlags, rectDraw); } if (outputLine.length() > 0) { outputLine.append(space); } outputLine.append(word); if (space.length() > 0) { space.delete(0, space.length()); } space.append(' '); return -1; } /** * printArea is updated to the position of the next row * * @param gc * @param outputLine * @param swtFlags * @param printArea * @param noDraw */ private void drawLine(GC gc, LineInfo lineInfo, int swtFlags, Rectangle printArea, boolean noDraw) { String text = lineInfo.lineOutputed; // TODO: ensure width and height have values if (lineInfo.width == 0 || lineInfo.height == 0) { Point gcExtent = gc.stringExtent(text);; if (lineInfo.width == 0) { lineInfo.width = gcExtent.x; } if (lineInfo.height == 0) { lineInfo.height = gcExtent.y; } } Point drawSize = new Point(lineInfo.width, lineInfo.height); int x0; if ((swtFlags & SWT.RIGHT) > 0) { x0 = printArea.x + printArea.width - drawSize.x; } else if ((swtFlags & SWT.CENTER) > 0) { x0 = printArea.x + (printArea.width - drawSize.x) / 2; } else { x0 = printArea.x; } int y0 = printArea.y; int lineInfoRelEndPos = lineInfo.relStartPos + lineInfo.lineOutputed.length(); int relStartPos = lineInfo.relStartPos; int lineStartPos = 0; URLInfo urlInfo = null; boolean drawURL = hasHitUrl(); if (drawURL) { URLInfo[] hitUrlInfo = getHitUrlInfo(); int nextHitUrlInfoPos = 0; while (drawURL) { drawURL = false; for (int i = nextHitUrlInfoPos; i < hitUrlInfo.length; i++) { urlInfo = hitUrlInfo[i]; drawURL = (urlInfo.relStartPos < lineInfoRelEndPos) && (urlInfo.relStartPos + urlInfo.titleLength > relStartPos) && (relStartPos >= lineInfo.relStartPos) && (relStartPos < lineInfoRelEndPos); if (drawURL) { nextHitUrlInfoPos = i + 1; break; } } if (!drawURL) { break; } //int numHitUrlsAlready = urlInfo.hitAreas == null ? 0 : urlInfo.hitAreas.size(); // draw text before url int i = lineStartPos + urlInfo.relStartPos - relStartPos; //System.out.println("numHitUrlsAlready = " + numHitUrlsAlready + ";i=" + i); if (i > 0 && i > lineStartPos && i <= text.length()) { String s = text.substring(lineStartPos, i); //gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_RED)); x0 += drawText(gc, s, x0, y0, lineInfo.height, null, noDraw).x; relStartPos += (i - lineStartPos); lineStartPos += (i - lineStartPos); //System.out.println("|" + s + "|" + textExtent.x); } // draw url text int end = i + urlInfo.titleLength; if (i < 0) { i = 0; } //System.out.println("end=" + end + ";" + text.length() + ";titlelen=" + urlInfo.titleLength); if (end > text.length()) { end = text.length(); } String s = text.substring(i, end); relStartPos += (end - i); lineStartPos += (end - i); Point pt = null; //System.out.println("|" + s + "|"); Color fgColor = null; if (!noDraw) { fgColor = gc.getForeground(); if (urlInfo.urlColor != null) { gc.setForeground(urlInfo.urlColor); } else if (urlColor != null) { gc.setForeground(urlColor); } } if (urlInfo.hitAreas == null) { urlInfo.hitAreas = new ArrayList(1); } pt = drawText(gc, s, x0, y0, lineInfo.height, urlInfo.hitAreas, noDraw); if (!noDraw) { gc.setForeground(fgColor); } if (urlInfo.hitAreas == null) { urlInfo.hitAreas = new ArrayList(1); } //gc.drawRectangle(new Rectangle(x0, y0, pt.x, lineInfo.height)); x0 += pt.x; } } // draw text after url if (lineStartPos < text.length()) { String s = text.substring(lineStartPos); if (!noDraw) { drawText(gc, s, x0, y0, lineInfo.height, null, noDraw); } } printArea.y += drawSize.y; } private Point drawText(GC gc, String s, int x, int y, int height, List hitAreas, boolean nodraw) { Point textExtent; if (images != null) { int pctPos = s.indexOf('%'); int lastPos = 0; int w = 0; int h = 0; while (pctPos >= 0) { if (pctPos >= 0 && s.length() > pctPos + 1) { int imgIdx = s.charAt(pctPos + 1) - '0'; if (imgIdx >= images.length || imgIdx < 0 || images[imgIdx] == null) { String sStart = s.substring(lastPos, pctPos + 1); textExtent = gc.textExtent(sStart); int centerY = y + (height / 2 - textExtent.y / 2); if (hitAreas != null) { hitAreas.add(new Rectangle(x, centerY, textExtent.x, textExtent.y)); } if (!nodraw) { gc.drawText(sStart, x, centerY, true); } x += textExtent.x; w += textExtent.x; h = Math.max(h, textExtent.y); lastPos = pctPos + 1; pctPos = s.indexOf('%', pctPos + 1); continue; } String sStart = s.substring(lastPos, pctPos); textExtent = gc.textExtent(sStart); int centerY = y + (height / 2 - textExtent.y / 2); if (!nodraw) { gc.drawText(sStart, x, centerY, true); } x += textExtent.x; w += textExtent.x; h = Math.max(h, textExtent.y); if (hitAreas != null) { hitAreas.add(new Rectangle(x, centerY, textExtent.x, textExtent.y)); } //System.out.println("drawimage: " + x + "x" + y + ";idx=" + imgIdx); Rectangle imgBounds = images[imgIdx].getBounds(); float scale = 1.0f; if (imageScales != null && imageScales.length > imgIdx) { scale = imageScales[imgIdx]; } int scaleImageWidth = (int) (imgBounds.width * scale); int scaleImageHeight = (int) (imgBounds.height * scale); centerY = y + (height / 2 - scaleImageHeight / 2); if (hitAreas != null) { hitAreas.add(new Rectangle(x, centerY, scaleImageWidth, scaleImageHeight)); } if (!nodraw) { //gc.drawImage(images[imgIdx], x, centerY); gc.drawImage(images[imgIdx], 0, 0, imgBounds.width, imgBounds.height, x, centerY, scaleImageWidth, scaleImageHeight); } x += scaleImageWidth; w += scaleImageWidth; h = Math.max(h, scaleImageHeight); } lastPos = pctPos + 2; pctPos = s.indexOf('%', lastPos); } if (s.length() >= lastPos) { String sEnd = s.substring(lastPos); textExtent = gc.textExtent(sEnd); int centerY = y + (height / 2 - textExtent.y / 2); if (hitAreas != null) { hitAreas.add(new Rectangle(x, centerY, textExtent.x, textExtent.y)); } if (!nodraw) { gc.drawText(sEnd, x, centerY, true); } x += textExtent.x; w += textExtent.x; h = Math.max(h, textExtent.y); } return new Point(w, h); } if (!nodraw) { gc.drawText(s, x, y, true); } textExtent = gc.textExtent(s); if (hitAreas != null) { hitAreas.add(new Rectangle(x, y, textExtent.x, textExtent.y)); } return textExtent; } /** * */ public GCStringPrinter(GC gc, String string, Rectangle printArea, boolean skipClip, boolean fullLinesOnly, int swtFlags) { this.gc = gc; this.string = string; this.printArea = printArea; this.swtFlags = swtFlags; printFlags = 0; if (skipClip) { printFlags |= FLAG_SKIPCLIP; } if (fullLinesOnly) { printFlags |= FLAG_FULLLINESONLY; } } public GCStringPrinter(GC gc, String string, Rectangle printArea, int printFlags, int swtFlags) { this.gc = gc; this.string = string; this.printArea = printArea; this.swtFlags = swtFlags; this.printFlags = printFlags; } public boolean printString() { return _printString(); } public boolean printString(int printFlags) { int oldPrintFlags = this.printFlags; printFlags |= printFlags; boolean b = _printString(); this.printFlags = oldPrintFlags; return b; } public void calculateMetrics() { int oldPrintFlags = printFlags; printFlags |= FLAG_NODRAW; _printString(); printFlags = oldPrintFlags; } /** * @param rectangle * * @since 3.0.4.3 */ public void printString(GC gc, Rectangle rectangle, int swtFlags) { this.gc = gc; int printFlags = this.printFlags; if (printArea.width == rectangle.width) { printFlags |= FLAG_KEEP_URL_INFO; } printArea = rectangle; this.swtFlags = swtFlags; printString(printFlags); } public Point getCalculatedSize() { return size; } public Color getUrlColor() { return urlColor; } public void setUrlColor(Color urlColor) { this.urlColor = urlColor; } public URLInfo getHitUrl(int x, int y) { if (listUrlInfo == null || listUrlInfo.size() == 0) { return null; } for (Iterator iter = listUrlInfo.iterator(); iter.hasNext();) { URLInfo urlInfo = (URLInfo) iter.next(); if (urlInfo.hitAreas != null) { for (Iterator iter2 = urlInfo.hitAreas.iterator(); iter2.hasNext();) { Rectangle r = (Rectangle) iter2.next(); if (r.contains(x, y)) { return urlInfo; } } } } return null; } public URLInfo[] getHitUrlInfo() { if (listUrlInfo == null) { return new URLInfo[0]; } return (URLInfo[]) listUrlInfo.toArray(new URLInfo[0]); } public boolean hasHitUrl() { return listUrlInfo != null && listUrlInfo.size() > 0; } public boolean isCutoff() { return cutoff; } public void setImages(Image[] images) { this.images = images; } public float[] getImageScales() { return imageScales; } public void setImageScales(float[] imageScales) { this.imageScales = imageScales; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -