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

📄 richtextrun.java

📁 介绍java核心技术的pio编程方法以及基本概念
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* ====================================================================   Licensed to the Apache Software Foundation (ASF) under one or more   contributor license agreements.  See the NOTICE file distributed with   this work for additional information regarding copyright ownership.   The ASF licenses this file to You under the Apache License, Version 2.0   (the "License"); you may not use this file except in compliance with   the License.  You may obtain a copy of the License at       http://www.apache.org/licenses/LICENSE-2.0   Unless required by applicable law or agreed to in writing, software   distributed under the License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   See the License for the specific language governing permissions and   limitations under the License.==================================================================== */        package org.apache.poi.hslf.usermodel;import java.awt.Color;import org.apache.poi.hslf.model.MasterSheet;import org.apache.poi.hslf.model.Shape;import org.apache.poi.hslf.model.Sheet;import org.apache.poi.hslf.model.TextRun;import org.apache.poi.hslf.model.textproperties.BitMaskTextProp;import org.apache.poi.hslf.model.textproperties.CharFlagsTextProp;import org.apache.poi.hslf.model.textproperties.ParagraphFlagsTextProp;import org.apache.poi.hslf.model.textproperties.TextProp;import org.apache.poi.hslf.model.textproperties.TextPropCollection;import org.apache.poi.hslf.record.ColorSchemeAtom;/** * Represents a run of text, all with the same style *  * TODO: finish all the getters and setters to the *  font/character/paragraph properties (currently only *  has some of them)  */public class RichTextRun {	/** The TextRun we belong to */	private TextRun parentRun;	/** The SlideShow we belong to */	private SlideShow slideShow;		/** Where in the parent TextRun we start from */	private int startPos;		/** How long a string (in the parent TextRun) we represent */	private int length;	    private String _fontname;    /**	 * Our paragraph and character style.	 * Note - we may share these styles with other RichTextRuns	 */	private TextPropCollection paragraphStyle;	private TextPropCollection characterStyle;	private boolean sharingParagraphStyle;	private boolean sharingCharacterStyle;	/**	 * Create a new wrapper around a (currently not)	 *  rich text string	 * @param parent	 * @param startAt	 * @param len	 */	public RichTextRun(TextRun parent, int startAt, int len) {		this(parent, startAt, len, null, null, false, false);	}	/**	 * Create a new wrapper around a rich text string	 * @param parent The parent TextRun	 * @param startAt The start position of this run	 * @param len The length of this run	 * @param pStyle The paragraph style property collection	 * @param cStyle The character style property collection	 * @param pShared The paragraph styles are shared with other runs	 * @param cShared The character styles are shared with other runs	 */	public RichTextRun(TextRun parent, int startAt, int len, 	TextPropCollection pStyle,  TextPropCollection cStyle,	boolean pShared, boolean cShared) {		parentRun = parent;		startPos = startAt;		length = len;		paragraphStyle = pStyle;		characterStyle = cStyle;		sharingParagraphStyle = pShared;		sharingCharacterStyle = cShared;	}	/**	 * Supply (normally default) textprops, and if they're shared, 	 *  when a run gets them 	 */	public void supplyTextProps(TextPropCollection pStyle,  TextPropCollection cStyle, boolean pShared, boolean cShared) {		if(paragraphStyle != null || characterStyle != null) {			throw new IllegalStateException("Can't call supplyTextProps if run already has some");		}		paragraphStyle = pStyle;		characterStyle = cStyle;		sharingParagraphStyle = pShared;		sharingCharacterStyle = cShared;	}	/**	 * Supply the SlideShow we belong to	 */	public void supplySlideShow(SlideShow ss) {		slideShow = ss;        if (_fontname != null) {            setFontName(_fontname);            _fontname = null;        }	}		/**	 * Get the length of the text	 */	public int getLength() {		return length;	}		/**	 * Fetch the text, in output suitable form	 */	public String getText() {		return parentRun.getText().substring(startPos, startPos+length);	}	/**	 * Fetch the text, in raw storage form	 */	public String getRawText() {		return parentRun.getRawText().substring(startPos, startPos+length);	}		/**	 * Change the text	 */	public void setText(String text) {		length = text.length();		parentRun.changeTextInRichTextRun(this,text);	}		/**	 * Tells the RichTextRun its new position in the parent TextRun	 * @param startAt	 */	public void updateStartPosition(int startAt) {		startPos = startAt;	}			// --------------- Internal helpers on rich text properties -------		/**	 * Fetch the value of the given flag in the CharFlagsTextProp.	 * Returns false if the CharFlagsTextProp isn't present, since the	 *  text property won't be set if there's no CharFlagsTextProp.	 */	private boolean isCharFlagsTextPropVal(int index) {		return getFlag(true, index);	}    private boolean getFlag(boolean isCharacter, int index) {        TextPropCollection props;        String propname;        if (isCharacter){            props = characterStyle;            propname = CharFlagsTextProp.NAME;        } else {            props = paragraphStyle;            propname = ParagraphFlagsTextProp.NAME;        }        BitMaskTextProp prop = null;        if (props != null){            prop = (BitMaskTextProp)props.findByName(propname);        }        if (prop == null){            Sheet sheet = parentRun.getSheet();            int txtype = parentRun.getRunType();            MasterSheet master = sheet.getMasterSheet();            if (master != null)                prop = (BitMaskTextProp)master.getStyleAttribute(txtype, getIndentLevel(), propname, isCharacter);        }        return prop == null ? false : prop.getSubValue(index);    }	/**	 * Set the value of the given flag in the CharFlagsTextProp, adding	 *  it if required. 	 */	private void setCharFlagsTextPropVal(int index, boolean value) {        setFlag(true, index, value);	}    public void setFlag(boolean isCharacter, int index, boolean value) {        TextPropCollection props;        String propname;        if (isCharacter){            props = characterStyle;            propname = CharFlagsTextProp.NAME;        } else {            props = paragraphStyle;            propname = ParagraphFlagsTextProp.NAME;        }		// Ensure we have the StyleTextProp atom we're going to need		if(props == null) {			parentRun.ensureStyleAtomPresent();            props = isCharacter ? characterStyle : paragraphStyle;		}		BitMaskTextProp prop = (BitMaskTextProp) fetchOrAddTextProp(props, propname);		prop.setSubValue(value,index);    }	/**	 * Returns the named TextProp, either by fetching it (if it exists) or adding it	 *  (if it didn't)	 * @param textPropCol The TextPropCollection to fetch from / add into	 * @param textPropName The name of the TextProp to fetch/add	 */	private TextProp fetchOrAddTextProp(TextPropCollection textPropCol, String textPropName) {		// Fetch / Add the TextProp		TextProp tp = textPropCol.findByName(textPropName);		if(tp == null) {			tp = textPropCol.addWithName(textPropName);		}		return tp;	}		/**	 * Fetch the value of the given Character related TextProp. 	 * Returns -1 if that TextProp isn't present. 	 * If the TextProp isn't present, the value from the appropriate 	 *  Master Sheet will apply.	 */	private int getCharTextPropVal(String propName) {        TextProp prop = null;        if (characterStyle != null){            prop = characterStyle.findByName(propName);        }        if (prop == null){

⌨️ 快捷键说明

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