📄 styledparagraph.java
字号:
/* * @(#)StyledParagraph.java 1.4 99/12/14 * (C) Copyright IBM Corp. 1999, All rights reserved. */package java.awt.font;import java.text.AttributedCharacterIterator;import java.text.Annotation;import java.awt.Font;import java.awt.Toolkit;import sun.awt.font.Decoration;import java.util.Vector;import java.util.Hashtable;import java.util.Map;import java.text.AttributedCharacterIterator;import sun.awt.font.FontResolver;import java.awt.im.InputMethodHighlight;/** * This class stores Font, GraphicAttribute, and Decoration intervals * on a paragraph of styled text. * <p> * Currently, this class is optimized for a small number of intervals * (preferrably 1). */final class StyledParagraph { // the length of the paragraph private int length; // If there is a single Decoration for the whole paragraph, it // is stored here. Otherwise this field is ignored. private Decoration decoration; // If there is a single Font or GraphicAttribute for the whole // paragraph, it is stored here. Otherwise this field is ignored. private Object font; // If there are multiple Decorations in the paragraph, they are // stored in this Vector, in order. Otherwise this vector and // the decorationStarts array are null. private Vector decorations; // If there are multiple Decorations in the paragraph, // decorationStarts[i] contains the index where decoration i // starts. For convenience, there is an extra entry at the // end of this array with the length of the paragraph. int[] decorationStarts; // If there are multiple Fonts/GraphicAttributes in the paragraph, // they are // stored in this Vector, in order. Otherwise this vector and // the fontStarts array are null. private Vector fonts; // If there are multiple Fonts/GraphicAttributes in the paragraph, // fontStarts[i] contains the index where decoration i // starts. For convenience, there is an extra entry at the // end of this array with the length of the paragraph. int[] fontStarts; private static int INITIAL_SIZE = 8; /** * Create a new StyledParagraph over the given styled text. * @param aci an iterator over the text * @param chars the characters extracted from aci */ public StyledParagraph(AttributedCharacterIterator aci, char[] chars) { int start = aci.getBeginIndex(); int end = aci.getEndIndex(); length = end - start; int index = start; aci.first(); do { final int nextRunStart = aci.getRunLimit(); final int localIndex = index-start; Map attributes = aci.getAttributes(); attributes = addInputMethodAttrs(attributes); Decoration d = Decoration.getDecoration(attributes); addDecoration(d, localIndex); Object f = getGraphicOrFont(attributes); if (f == null) { addFonts(chars, attributes, localIndex, nextRunStart-start); } else { addFont(f, localIndex); } aci.setIndex(nextRunStart); index = nextRunStart; } while (index < end); // Add extra entries to starts arrays with the length // of the paragraph. 'this' is used as a dummy value // in the Vector. if (decorations != null) { decorationStarts = addToVector(this, length, decorations, decorationStarts); } if (fonts != null) { fontStarts = addToVector(this, length, fonts, fontStarts); } } /** * Adjust indices in starts to reflect an insertion after pos. * Any index in starts greater than pos will be increased by 1. */ private static void insertInto(int pos, int[] starts, int numStarts) { while (starts[--numStarts] > pos) { starts[numStarts] += 1; } } /** * Return a StyledParagraph reflecting the insertion of a single character * into the text. This method will attempt to reuse the given paragraph, * but may create a new paragraph. * @param aci an iterator over the text. The text should be the same as the * text used to create (or most recently update) oldParagraph, with * the exception of inserting a single character at insertPos. * @param chars the characters in aci * @param insertPos the index of the new character in aci * @param oldParagraph a StyledParagraph for the text in aci before the * insertion */ public static StyledParagraph insertChar(AttributedCharacterIterator aci, char[] chars, int insertPos, StyledParagraph oldParagraph) { // If the styles at insertPos match those at insertPos-1, // oldParagraph will be reused. Otherwise we create a new // paragraph. char ch = aci.setIndex(insertPos); int relativePos = Math.max(insertPos - aci.getBeginIndex() - 1, 0); Map attributes = addInputMethodAttrs(aci.getAttributes()); Decoration d = Decoration.getDecoration(attributes); if (!oldParagraph.getDecorationAt(relativePos).equals(d)) { return new StyledParagraph(aci, chars); } Object f = getGraphicOrFont(attributes); if (f == null) { FontResolver resolver = FontResolver.getInstance(); int fontIndex = resolver.getFontIndex(ch); f = resolver.getFont(fontIndex, attributes); } if (!oldParagraph.getFontOrGraphicAt(relativePos).equals(f)) { return new StyledParagraph(aci, chars); } // insert into existing paragraph oldParagraph.length += 1; if (oldParagraph.decorations != null) { insertInto(relativePos, oldParagraph.decorationStarts, oldParagraph.decorations.size()); } if (oldParagraph.fonts != null) { insertInto(relativePos, oldParagraph.fontStarts, oldParagraph.fonts.size()); } return oldParagraph; } /** * Adjust indices in starts to reflect a deletion after deleteAt. * Any index in starts greater than deleteAt will be increased by 1. * It is the caller's responsibility to make sure that no 0-length * runs result. */ private static void deleteFrom(int deleteAt, int[] starts, int numStarts) { while (starts[--numStarts] > deleteAt) { starts[numStarts] -= 1; } } /** * Return a StyledParagraph reflecting the insertion of a single character * into the text. This method will attempt to reuse the given paragraph, * but may create a new paragraph. * @param aci an iterator over the text. The text should be the same as the * text used to create (or most recently update) oldParagraph, with * the exception of deleting a single character at deletePos. * @param chars the characters in aci * @param deletePos the index where a character was removed * @param oldParagraph a StyledParagraph for the text in aci before the * insertion */ public static StyledParagraph deleteChar(AttributedCharacterIterator aci, char[] chars, int deletePos, StyledParagraph oldParagraph) { // We will reuse oldParagraph unless there was a length-1 run // at deletePos. We could do more work and check the individual // Font and Decoration runs, but we don't right now... deletePos -= aci.getBeginIndex(); if (oldParagraph.decorations == null && oldParagraph.fonts == null) { oldParagraph.length -= 1; return oldParagraph; } if (oldParagraph.getRunLimit(deletePos) == deletePos+1) { if (deletePos == 0 || oldParagraph.getRunLimit(deletePos-1) == deletePos) { return new StyledParagraph(aci, chars); } } oldParagraph.length -= 1; if (oldParagraph.decorations != null) { deleteFrom(deletePos, oldParagraph.decorationStarts, oldParagraph.decorations.size()); } if (oldParagraph.fonts != null) { deleteFrom(deletePos, oldParagraph.fontStarts, oldParagraph.fonts.size()); } return oldParagraph; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -