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

📄 textlayout.java

📁 源码为Eclipse开源开发平台桌面开发工具SWT的源代码,
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
 *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> * </ul> */public void setFont (Font font) {	checkLayout();	if (font != null && font.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);	if (this.font == font) return;	if (font != null && font.equals(this.font)) return;	freeRuns();	this.font = font;}/** * Sets the orientation of the receiver, which must be one * of <code>SWT.LEFT_TO_RIGHT</code> or <code>SWT.RIGHT_TO_LEFT</code>. * <p> * * @param orientation new orientation style *  * @exception SWTException <ul> *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> * </ul> */public void setOrientation (int orientation) {	checkLayout();	int mask = SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT;	orientation &= mask;	if (orientation == 0) return;	if ((orientation & SWT.LEFT_TO_RIGHT) != 0) orientation = SWT.LEFT_TO_RIGHT;	if (this.orientation == orientation) return;	this.orientation = orientation;	freeRuns();}/** * Sets the offsets of the receiver's text segments. Text segments are used to * override the default behaviour of the bidirectional algorithm. * Bidirectional reordering can happen within a text segment but not  * between two adjacent segments. * Each text segment is determined by two consecutive offsets in the  * <code>segments</code> arrays. The first element of the array should  * always be zero and the last one should always be equals to length of * the text. * <p> *  * @param segments the text segments offset *  * @exception SWTException <ul> *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> * </ul> */public void setSegments(int[] segments) {	checkLayout();	if (this.segments == null && segments == null) return;	if (this.segments != null && segments !=null) {		if (this.segments.length == segments.length) {			int i;			for (i = 0; i <segments.length; i++) {				if (this.segments[i] != segments[i]) break;			}			if (i == segments.length) return;		}	}	freeRuns();	this.segments = segments;}/** * Sets the line spacing of the receiver.  The line spacing * is the space left between lines. * * @param spacing the new line spacing  * * @exception IllegalArgumentException <ul> *    <li>ERROR_INVALID_ARGUMENT - if the spacing is negative</li> * </ul> * @exception SWTException <ul> *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> * </ul> */public void setSpacing (int spacing) {	checkLayout();	if (spacing < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);	if (this.lineSpacing == spacing) return;	freeRuns();	this.lineSpacing = spacing;}/** * Sets the style of the receiver for the specified range.  Styles previously * set for that range will be overwritten.  The start and end offsets are * inclusive and will be clamped if out of range. *  * @param style the style * @param start the start offset * @param end the end offset *  * @exception SWTException <ul> *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> * </ul> */public void setStyle (TextStyle style, int start, int end) {	checkLayout();	int length = text.length();	if (length == 0) return;	if (start > end) return;	start = Math.min(Math.max(0, start), length - 1);	end = Math.min(Math.max(0, end), length - 1);	int low = -1;	int high = styles.length;	while (high - low > 1) {		int index = (high + low) / 2;		if (start <= styles[index].start) {			high = index;		} else {			low = index;		}	}	if (0 <= high && high < styles.length) {		StyleItem item = styles[high];		if (item.start == start && styles[high + 1].start - 1 == end) {			if (style == null) {				if (item.style == null) return;			} else {				if (style.equals(item.style)) return;			}		}	}	freeRuns();	int count = 0, i;	StyleItem[] newStyles = new StyleItem[styles.length + 2];	for (i = 0; i < styles.length; i++) {		StyleItem item = styles[i];		if (item.start >= start) break;		newStyles[count++] = item;	}	StyleItem newItem = new StyleItem();	newItem.start = start;	newItem.style = style;	newStyles[count++] = newItem;	if (styles[i].start > end) {		newItem = new StyleItem();		newItem.start = end + 1;		newItem.style = styles[i -1].style;		newStyles[count++] = newItem;	} else {		for (; i<styles.length; i++) {			StyleItem item = styles[i];			if (item.start > end) break;		}		if (end != styles[i].start - 1) {			i--;			styles[i].start = end + 1;		}	}	for (; i<styles.length; i++) {		StyleItem item = styles[i];		if (item.start > end) newStyles[count++] = item;	}	if (newStyles.length != count) {		styles = new StyleItem[count];		System.arraycopy(newStyles, 0, styles, 0, count);	} else {		styles = newStyles;	}}/** * Sets the receiver's tab list. Each value in the tab list specifies * the space in pixels from the origin of the text layout to the respective * tab stop.  The last tab stop width is repeated continuously. *  * @param tabs the new tab list *  * @exception SWTException <ul> *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> * </ul> */public void setTabs (int[] tabs) {	checkLayout();	if (this.tabs == null && tabs == null) return;	if (this.tabs != null && tabs !=null) {		if (this.tabs.length == tabs.length) {			int i;			for (i = 0; i <tabs.length; i++) {				if (this.tabs[i] != tabs[i]) break;			}			if (i == tabs.length) return;		}	}	freeRuns();	this.tabs = tabs;} /** * Sets the receiver's text. * * @param text the new text * * @exception IllegalArgumentException <ul> *    <li>ERROR_NULL_ARGUMENT - if the text is null</li> * </ul> * @exception SWTException <ul> *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> * </ul> */public void setText (String text) {	checkLayout();	if (text == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);	if (text.equals(this.text)) return;	freeRuns();	this.text = text;	styles = new StyleItem[2];	styles[0] = new StyleItem();	styles[1] = new StyleItem();		styles[1].start = text.length();}/** * Sets the line width of the receiver, which determines how * text should be wrapped and aligned. The default value is * <code>-1</code> which means wrapping is disabled. * * @param width the new width  * * @exception IllegalArgumentException <ul> *    <li>ERROR_INVALID_ARGUMENT - if the width is <code>0</code> or less than <code>-1</code></li> * </ul> * @exception SWTException <ul> *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> * </ul> *  * @see #setAlignment(int) */public void setWidth (int width) {	checkLayout();	if (width < -1 || width == 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);	if (this.wrapWidth == width) return;	freeRuns();	this.wrapWidth = width;}boolean shape (int hdc, StyleItem run, char[] chars, int[] glyphCount, int maxGlyphs) {	int hr = OS.ScriptShape(hdc, run.psc, chars, chars.length, maxGlyphs, run.analysis, run.glyphs, run.clusters, run.visAttrs, glyphCount);	run.glyphCount = glyphCount[0];	if (hr != OS.USP_E_SCRIPT_NOT_IN_FONT) {		SCRIPT_FONTPROPERTIES fp = new SCRIPT_FONTPROPERTIES ();		fp.cBytes = SCRIPT_FONTPROPERTIES.sizeof;		OS.ScriptGetFontProperties(hdc, run.psc, fp);		short[] glyphs = new short[glyphCount[0]];		OS.MoveMemory(glyphs, run.glyphs, glyphs.length * 2);		int i;		for (i = 0; i < glyphs.length; i++) {			if (glyphs[i] == fp.wgDefault) break;		}		if (i == glyphs.length) return true;	}	if (run.psc != 0) {		OS.ScriptFreeCache(run.psc);		glyphCount[0] = 0;		OS.MoveMemory(run.psc, glyphCount, 4);	}	run.glyphCount = 0;	return false;}/*  * Generate glyphs for one Run. */void shape (final int hdc, final StyleItem run) {	final int[] buffer = new int[1];	final char[] chars = new char[run.length];	segmentsText.getChars(run.start, run.start + run.length, chars, 0);	final int maxGlyphs = (chars.length * 3 / 2) + 16;	final int hHeap = OS.GetProcessHeap();	run.glyphs = OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, maxGlyphs * 2);	run.clusters = OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, maxGlyphs * 2);	run.visAttrs = OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, maxGlyphs * SCRIPT_VISATTR_SIZEOF);	run.psc = OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, 4);	if (!shape(hdc, run, chars, buffer,  maxGlyphs)) {		final int script = run.analysis.eScript;		final int hFont = OS.GetCurrentObject(hdc, OS.OBJ_FONT);		final LOGFONT logFont = OS.IsUnicode ? (LOGFONT)new LOGFONTW () : new LOGFONTA ();		OS.GetObject(hFont, LOGFONT.sizeof, logFont);		LOGFONT cachedLogFont = device.logFontsCache != null ? device.logFontsCache[script] : null;		if (cachedLogFont != null) {			cachedLogFont.lfHeight = logFont.lfHeight;			cachedLogFont.lfWeight = logFont.lfWeight;			cachedLogFont.lfItalic = logFont.lfItalic;			cachedLogFont.lfWidth = logFont.lfWidth;			int newFont = OS.CreateFontIndirect(cachedLogFont);			OS.SelectObject(hdc, newFont);			OS.ScriptShape(hdc, run.psc, chars, chars.length, maxGlyphs, run.analysis, run.glyphs, run.clusters, run.visAttrs, buffer);			run.glyphCount = buffer[0];			run.fallbackFont = newFont;		} else {			final LOGFONT newLogFont = OS.IsUnicode ? (LOGFONT)new LOGFONTW () : new LOGFONTA ();			if (device.logFontsCache == null) device.logFontsCache = new LOGFONT[device.scripts.length];			SCRIPT_PROPERTIES properties = new SCRIPT_PROPERTIES();			OS.MoveMemory(properties, device.scripts[script], SCRIPT_PROPERTIES.sizeof);			int charSet = properties.fAmbiguousCharSet ? OS.DEFAULT_CHARSET : properties.bCharSet;			Object object = new Object () {				public int EnumFontFamExProc(int lpelfe, int lpntme, int FontType, int lParam) {					OS.MoveMemory(newLogFont, lpelfe, LOGFONT.sizeof);					if (FontType == OS.RASTER_FONTTYPE) return 1;					newLogFont.lfHeight = logFont.lfHeight;					newLogFont.lfWeight = logFont.lfWeight;					newLogFont.lfItalic = logFont.lfItalic;					newLogFont.lfWidth = logFont.lfWidth;					int newFont = OS.CreateFontIndirect(newLogFont);					OS.SelectObject(hdc, newFont);					if (shape(hdc, run, chars, buffer, maxGlyphs)) {						run.fallbackFont = newFont;						LOGFONT cacheLogFont = OS.IsUnicode ? (LOGFONT)new LOGFONTW () : new LOGFONTA ();						OS.MoveMemory(cacheLogFont, lpelfe, LOGFONT.sizeof);						device.logFontsCache[script] = cacheLogFont;						return 0;					}					OS.SelectObject(hdc, hFont);					OS.DeleteObject(newFont);					return 1;				}			};			Callback callback = new Callback(object, "EnumFontFamExProc", 4); //$NON-NLS-1$			int address = callback.getAddress();			if (address == 0) SWT.error(SWT.ERROR_NO_MORE_CALLBACKS);			newLogFont.lfCharSet = (byte)charSet;			OS.EnumFontFamiliesEx(hdc, newLogFont, address, 0, 0);			callback.dispose();			if (run.fallbackFont == 0) {				OS.ScriptShape(hdc, run.psc, chars, chars.length, maxGlyphs, run.analysis, run.glyphs, run.clusters, run.visAttrs, buffer);				device.logFontsCache[script] = logFont;				run.glyphCount = buffer[0];			}				}	}	int[] abc = new int[3];	run.advances = OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, run.glyphCount * 4);	run.goffsets = OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, run.glyphCount * GOFFSET_SIZEOF);	OS.ScriptPlace(hdc, run.psc, run.glyphs, run.glyphCount, run.visAttrs, run.analysis, run.advances, run.goffsets, abc);	run.width = abc[0] + abc[1] + abc[2];	TEXTMETRIC lptm = OS.IsUnicode ? (TEXTMETRIC)new TEXTMETRICW() : new TEXTMETRICA();	OS.GetTextMetrics(hdc, lptm);	run.ascent = lptm.tmAscent;	run.descent = lptm.tmDescent;}int validadeOffset(int offset, int step) {	offset += step;	if (segments != null && segments.length > 2) {		for (int i = 0; i < segments.length; i++) {			if (translateOffset(segments[i]) - 1 == offset) {				offset += step;				break;			}		}	}		return offset;}int translateOffset(int offset) {	if (segments == null) return offset;	int nSegments = segments.length;	if (nSegments <= 1) return offset;	int length = text.length();	if (length == 0) return offset;	if (nSegments == 2) {		if (segments[0] == 0 && segments[1] == length) return offset;	}	for (int i = 0; i < nSegments && offset - i >= segments[i]; i++) {		offset++;	}		return offset;}int untranslateOffset(int offset) {	if (segments == null) return offset;	int nSegments = segments.length;	if (nSegments <= 1) return offset;	int length = text.length();	if (length == 0) return offset;	if (nSegments == 2) {		if (segments[0] == 0 && segments[1] == length) return offset;	}	for (int i = 0; i < nSegments && offset > segments[i]; i++) {		offset--;	}	return offset;}}

⌨️ 快捷键说明

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