📄 styledparagraph.java
字号:
/** * Return the index at which there is a different Font, GraphicAttribute, or * Dcoration than at the given index. * @param index a valid index in the paragraph * @return the first index where there is a change in attributes from * those at index */ public int getRunLimit(int index) { if (index < 0 || index >= length) { throw new IllegalArgumentException("index out of range"); } int limit1 = length; if (decorations != null) { int run = findRunContaining(index, decorationStarts); limit1 = decorationStarts[run+1]; } int limit2 = length; if (fonts != null) { int run = findRunContaining(index, fontStarts); limit2 = fontStarts[run+1]; } return Math.min(limit1, limit2); } /** * Return the Decoration in effect at the given index. * @param index a valid index in the paragraph * @return the Decoration at index. */ public Decoration getDecorationAt(int index) { if (index < 0 || index >= length) { throw new IllegalArgumentException("index out of range"); } if (decorations == null) { return decoration; } int run = findRunContaining(index, decorationStarts); return (Decoration) decorations.elementAt(run); } /** * Return the Font or GraphicAttribute in effect at the given index. * The client must test the type of the return value to determine what * it is. * @param index a valid index in the paragraph * @return the Font or GraphicAttribute at index. */ public Object getFontOrGraphicAt(int index) { if (index < 0 || index >= length) { throw new IllegalArgumentException("index out of range"); } if (fonts == null) { return font; } int run = findRunContaining(index, fontStarts); return fonts.elementAt(run); } /** * Return i such that starts[i] <= index < starts[i+1]. starts * must be in increasing order, with at least one element greater * than index. */ private static int findRunContaining(int index, int[] starts) { for (int i=1; true; i++) { if (starts[i] > index) { return i-1; } } } /** * Append the given Object to the given Vector. Add * the given index to the given starts array. If the * starts array does not have room for the index, a * new array is created and returned. */ private static int[] addToVector(Object obj, int index, Vector v, int[] starts) { if (!v.lastElement().equals(obj)) { v.addElement(obj); int count = v.size(); if (starts.length == count) { int[] temp = new int[starts.length*2]; System.arraycopy(starts, 0, temp, 0, starts.length); starts = temp; } starts[count-1] = index; } return starts; } /** * Add a new Decoration run with the given Decoration at the * given index. */ private void addDecoration(Decoration d, int index) { if (decorations != null) { decorationStarts = addToVector(d, index, decorations, decorationStarts); } else if (decoration == null) { decoration = d; } else { if (!decoration.equals(d)) { decorations = new Vector(INITIAL_SIZE); decorations.addElement(decoration); decorations.addElement(d); decorationStarts = new int[INITIAL_SIZE]; decorationStarts[0] = 0; decorationStarts[1] = index; } } } /** * Add a new Font/GraphicAttribute run with the given object at the * given index. */ private void addFont(Object f, int index) { if (fonts != null) { fontStarts = addToVector(f, index, fonts, fontStarts); } else if (font == null) { font = f; } else { if (!font.equals(f)) { fonts = new Vector(INITIAL_SIZE); fonts.addElement(font); fonts.addElement(f); fontStarts = new int[INITIAL_SIZE]; fontStarts[0] = 0; fontStarts[1] = index; } } } /** * Resolve the given chars into Fonts using FontResolver, then add * font runs for each. */ private void addFonts(char[] chars, Map attributes, int start, int limit) { FontResolver resolver = FontResolver.getInstance(); do { int runStart = start; int fontIndex = resolver.getFontIndex(chars[start]); for (start++; start < limit; start++) { if (resolver.getFontIndex(chars[start]) != fontIndex) { break; } } addFont(resolver.getFont(fontIndex, attributes), runStart); } while (start < limit); } /** * Return a Map with entries from oldStyles, as well as input * method entries, if any. */ static Map addInputMethodAttrs(Map oldStyles) { Object value = oldStyles.get(TextAttribute.INPUT_METHOD_HIGHLIGHT); try { if (value != null) { if (value instanceof Annotation) { value = ((Annotation)value).getValue(); } InputMethodHighlight hl; hl = (InputMethodHighlight) value; Map imStyles = null; try { imStyles = hl.getStyle(); } catch (NoSuchMethodError e) { } if (imStyles == null) { Toolkit tk = Toolkit.getDefaultToolkit(); imStyles = tk.mapInputMethodHighlight(hl); } if (imStyles != null) { Hashtable newStyles = new Hashtable(5, (float)0.9); newStyles.putAll(oldStyles); newStyles.putAll(imStyles); return newStyles; } } } catch(ClassCastException e) { } return oldStyles; } /** * Extract a GraphicAttribute or Font from the given attributes. * If attributes does not contain a GraphicAttribute, Font, or * Font family entry this method returns null. */ private static Object getGraphicOrFont(Map attributes) { Object value = attributes.get(TextAttribute.CHAR_REPLACEMENT); if (value != null) { return value; } value = attributes.get(TextAttribute.FONT); if (value != null) { return value; } if (attributes.get(TextAttribute.FAMILY) != null) { return Font.getFont(attributes); } else { return null; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -