⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 styledtext.java

📁 源码为Eclipse开源开发平台桌面开发工具SWT的源代码,
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
			renderer.dispose();			renderer = null;		}	}	/**	 * Finish printing the indicated page.	 * 	 * @param page page that was printed	 */	void endPage(int page) {		printDecoration(page, false);		printer.endPage();	}	/**	 * Creates a <class>PrintRenderer</class> and calculate the line range	 * to print.	 */	void initializeRenderer() {		Rectangle trim = printer.computeTrim(0, 0, 0, 0);		Point dpi = printer.getDPI();				printerFont = new Font(printer, displayFontData.getName(), displayFontData.getHeight(), SWT.NORMAL);		clientArea = printer.getClientArea();		pageWidth = clientArea.width;		// one inch margin around text		clientArea.x = dpi.x + trim.x; 						clientArea.y = dpi.y + trim.y;		clientArea.width -= (clientArea.x + trim.width);		clientArea.height -= (clientArea.y + trim.height); 				gc = new GC(printer);		gc.setFont(printerFont);						renderer = new PrintRenderer(			printer, printerFont, gc, printerContent,			lineBackgrounds, lineStyles, bidiSegments, 			parent.tabLength, clientArea);		if (printOptions.header != null) {			int lineHeight = renderer.getLineHeight();			clientArea.y += lineHeight * 2;			clientArea.height -= lineHeight * 2;		}		if (printOptions.footer != null) {			clientArea.height -= renderer.getLineHeight() * 2;		}		pageSize = clientArea.height / renderer.getLineHeight();		StyledTextContent content = renderer.getContent();		startLine = 0;		if (singleLine) {			endLine = 0;		}		else {			endLine = content.getLineCount() - 1;		}		PrinterData data = printer.getPrinterData();		if (data.scope == PrinterData.PAGE_RANGE) {			startLine = (startPage - 1) * pageSize;		} 		else		if (data.scope == PrinterData.SELECTION) {			startLine = content.getLineAtOffset(selection.x);			if (selection.y > 0) {				endLine = content.getLineAtOffset(selection.x + selection.y - 1);			} 			else {				endLine = startLine - 1;			}		}	}	/**	 * Returns the printer color for the given display color.	 * </p>	 * @param color display color	 * @return color create on the printer with the same RGB values 	 * 	as the display color. 	 */	Color getPrinterColor(Color color) {		Color printerColor = null;				if (color != null) {			printerColor = (Color) printerColors.get(color);					if (printerColor == null) {				printerColor = new Color(printer, color.getRGB());				printerColors.put(color, printerColor);			}		}		return printerColor;	}	/**	 * Prints the lines in the specified page range.	 */	void print() {		StyledTextContent content = renderer.getContent();		Color background = gc.getBackground();		Color foreground = gc.getForeground();		int lineHeight = renderer.getLineHeight();		int paintY = clientArea.y;		int page = startPage;				for (int i = startLine; i <= endLine && page <= endPage; i++, paintY += lineHeight) {			String line = content.getLine(i);						if (paintY == clientArea.y) {				startPage(page);			}			renderer.drawLine(				line, i, paintY, gc, background, foreground, true);			if (paintY + lineHeight * 2 > clientArea.y + clientArea.height) {				// close full page				endPage(page);				paintY = clientArea.y - lineHeight;				page++;			}		}		if (paintY > clientArea.y) {			// close partial page			endPage(page);		}	}	/**	 * Print header or footer decorations.	 * 	 * @param page page number to print, if specified in the StyledTextPrintOptions header or footer.	 * @param header true = print the header, false = print the footer	 */	void printDecoration(int page, boolean header) {		int lastSegmentIndex = 0;		final int SegmentCount = 3;		String text;				if (header) {			text = printOptions.header;		}		else {			text = printOptions.footer;		}		if (text == null) {			return;		}		for (int i = 0; i < SegmentCount; i++) {			int segmentIndex = text.indexOf(StyledTextPrintOptions.SEPARATOR, lastSegmentIndex);			String segment;						if (segmentIndex == -1) {				segment = text.substring(lastSegmentIndex);				printDecorationSegment(segment, i, page, header);				break;			}			else {				segment = text.substring(lastSegmentIndex, segmentIndex);				printDecorationSegment(segment, i, page, header);				lastSegmentIndex = segmentIndex + StyledTextPrintOptions.SEPARATOR.length();			}		}	}	/**	 * Print one segment of a header or footer decoration.	 * Headers and footers have three different segments.	 * One each for left aligned, centered, and right aligned text.	 * 	 * @param segment decoration segment to print	 * @param alignment alignment of the segment. 0=left, 1=center, 2=right 	 * @param page page number to print, if specified in the decoration segment.	 * @param header true = print the header, false = print the footer	 */	void printDecorationSegment(String segment, int alignment, int page, boolean header) {				int pageIndex = segment.indexOf(StyledTextPrintOptions.PAGE_TAG);				if (pageIndex != -1) {			final int PageTagLength = StyledTextPrintOptions.PAGE_TAG.length();			StringBuffer buffer = new StringBuffer(segment.substring (0, pageIndex));			buffer.append (page);			buffer.append (segment.substring(pageIndex + PageTagLength));			segment = buffer.toString();		}		if (segment.length() > 0) {			int segmentWidth;			int drawX = 0;			int drawY = 0;			TextLayout layout = new TextLayout(printer);			layout.setText(segment);			layout.setFont(printerFont);			segmentWidth = layout.getLineBounds(0).width;			if (header) {				drawY = clientArea.y - renderer.getLineHeight() * 2;			}			else {				drawY = clientArea.y + clientArea.height + renderer.getLineHeight();			}			if (alignment == LEFT) {				drawX = clientArea.x;			}			else							if (alignment == CENTER) {				drawX = (pageWidth - segmentWidth) / 2;			}			else 			if (alignment == RIGHT) {				drawX = clientArea.x + clientArea.width - segmentWidth;			}			layout.draw(gc, drawX, drawY);			layout.dispose();		}	}	/**	 * Starts a print job and prints the pages specified in the constructor.	 */	public void run() {		String jobName = printOptions.jobName;				if (jobName == null) {			jobName = "Printing";		}				if (printer.startJob(jobName)) {			createPrinterColors();			initializeRenderer();			print();			dispose();			printer.endJob();					}	}	/**	 * Start printing a new page.	 * 	 * @param page page number to be started	 */	void startPage(int page) {		printer.startPage();		printDecoration(page, true);	}		}	/**	 * The <code>RTFWriter</code> class is used to write widget content as	 * rich text. The implementation complies with the RTF specification 	 * version 1.5.	 * <p>	 * toString() is guaranteed to return a valid RTF string only after 	 * close() has been called. 	 * </p>	 * <p>	 * Whole and partial lines and line breaks can be written. Lines will be	 * formatted using the styles queried from the LineStyleListener, if 	 * set, or those set directly in the widget. All styles are applied to	 * the RTF stream like they are rendered by the widget. In addition, the 	 * widget font name and size is used for the whole text.	 * </p>	 */	class RTFWriter extends TextWriter {		final int DEFAULT_FOREGROUND = 0;		final int DEFAULT_BACKGROUND = 1;		Vector colorTable = new Vector();		boolean WriteUnicode;			/**	 * Creates a RTF writer that writes content starting at offset "start"	 * in the document.  <code>start</code> and <code>length</code>can be set to specify partial 	 * lines.	 * <p>	 *	 * @param start start offset of content to write, 0 based from 	 * 	beginning of document	 * @param length length of content to write	 */	public RTFWriter(int start, int length) {		super(start, length);		colorTable.addElement(getForeground());		colorTable.addElement(getBackground());				setUnicode();	}	/**	 * Closes the RTF writer. Once closed no more content can be written.	 * <b>NOTE:</b>  <code>toString()</code> does not return a valid RTF string until 	 * <code>close()</code> has been called.	 */	public void close() {		if (isClosed() == false) {			writeHeader();			write("\n}}\0");			super.close();		}	}		/**	 * Returns the index of the specified color in the RTF color table.	 * <p>	 *	 * @param color the color	 * @param defaultIndex return value if color is null	 * @return the index of the specified color in the RTF color table	 * 	or "defaultIndex" if "color" is null.	 */	int getColorIndex(Color color, int defaultIndex) {		int index;				if (color == null) {			index = defaultIndex;		}		else {					index = colorTable.indexOf(color);			if (index == -1) {				index = colorTable.size();				colorTable.addElement(color);			}		}		return index;	}	/**	 * Determines if Unicode RTF should be written.	 * Don't write Unicode RTF on Windows 95/98/ME or NT.	 */	void setUnicode() {		final String Win95 = "windows 95";		final String Win98 = "windows 98";		final String WinME = "windows me";				final String WinNT = "windows nt";		String osName = System.getProperty("os.name").toLowerCase();		String osVersion = System.getProperty("os.version");		int majorVersion = 0;				if (osName.startsWith(WinNT) && osVersion != null) {			int majorIndex = osVersion.indexOf('.');			if (majorIndex != -1) {				osVersion = osVersion.substring(0, majorIndex);				try {					majorVersion = Integer.parseInt(osVersion);				}				catch (NumberFormatException exception) {					// ignore exception. version number remains unknown.					// will write without Unicode				}			}		}		if (osName != null &&			osName.startsWith(Win95) == false &&			osName.startsWith(Win98) == false &&			osName.startsWith(WinME) == false &&			(osName.startsWith(WinNT) == false || majorVersion > 4)) {			WriteUnicode = true;		}		else {			WriteUnicode = false;		}	}	/**	 * Appends the specified segment of "string" to the RTF data.	 * Copy from <code>start</code> up to, but excluding, <code>end</code>.	 * <p>	 *	 * @param string string to copy a segment from. Must not contain	 * 	line breaks. Line breaks should be written using writeLineDelimiter()	 * @param start start offset of segment. 0 based.	 * @param end end offset of segment	 */	void write(String string, int start, int end) {		for (int index = start; index < end; index++) {			char ch = string.charAt(index);			if (ch > 0xFF && WriteUnicode) {				// write the sub string from the last escaped character 				// to the current one. Fixes bug 21698.				if (index > start) {					write(string.substring(start, index));				}				write("\\u");				write(Integer.toString((short) ch));				write(' ');						// control word delimiter				start = index + 1;			}			else			if (ch == '}' || ch == '{' || ch == '\\') {				// write the sub string from the last escaped character 				// to the current one. Fixes bug 21698.				if (index > start) {					write(string.substring(start, index));				}				write('\\');				write(ch);				start = index + 1;			}		}		// write from the last escaped character to the end.		// Fixes bug 21698.		if (start < end) {			write(string.substring(start, end));		}	}		/**	 * Writes the RTF header including font table and color table.	 */	void writeHeader() {		StringBuffer header = new StringBuffer();		FontData fontData = getFont().getFontData()[0];		header.append("{\\rtf1\\ansi");		// specify code page, necessary for copy to work in bidi 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -