/** * Copy text attributes from the supplied Font to Java2D AttributedString */ private static void copyAttributes(Font font, AttributedString str, @SuppressWarnings("SameParameterValue") int startIdx, int endIdx) { str.addAttribute(TextAttribute.FAMILY, font.getFontName(), startIdx, endIdx); str.addAttribute(TextAttribute.SIZE, (float)font.getFontHeightInPoints()); if (font.getBold()) str.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD, startIdx, endIdx); if (font.getItalic() ) str.addAttribute(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE, startIdx, endIdx); if (font.getUnderline() == Font.U_SINGLE ) str.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, startIdx, endIdx); }
public void paintComponent(Graphics g) { super.paintComponent(g); AttributedString as = new AttributedString("I love you 104 gazillion"); as.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER, 13, 14); as.addAttribute(TextAttribute.FOREGROUND, Color.RED, 2, 6); g.drawString(as.getIterator(), 20, 20); }
/** * Applies a given set of attributes to the given range of the string. * * @param attributes * the set of attributes that will be applied to this string. * @param start * the start of the range where the attribute will be applied. * @param end * the end of the range where the attribute will be applied. * @throws IllegalArgumentException * if {@code start < 0}, {@code end} is greater than the length * of this string, or if {@code start >= end}. */ public void addAttributes( Map<? extends AttributedCharacterIterator.Attribute, ?> attributes, int start, int end) { Iterator<?> it = attributes.entrySet().iterator(); while (it.hasNext()) { Map.Entry<?, ?> entry = (Map.Entry<?, ?>) it.next(); addAttribute( (AttributedCharacterIterator.Attribute) entry.getKey(), entry.getValue(), start, end); } }
public AttributedCharacterIterator iterator() { AttributedString aString = new AttributedString(String .valueOf(character)); aString.addAttribute(TextAttribute.FONT, font, 0, 1); return aString.getIterator(); }
as.addAttribute(fc.attribute, fc.value, fc.start, fc.end);
private AttributedCharacterIterator formatToCharacterIteratorImpl(Date date) { StringBuffer buffer = new StringBuffer(); ArrayList<FieldPosition> fields = new ArrayList<FieldPosition>(); // format the date, and find fields formatImpl(date, buffer, null, fields); // create and AttributedString with the formatted buffer AttributedString as = new AttributedString(buffer.toString()); // add DateFormat field attributes to the AttributedString for (FieldPosition pos : fields) { Format.Field attribute = pos.getFieldAttribute(); as.addAttribute(attribute, attribute, pos.getBeginIndex(), pos.getEndIndex()); } // return the CharacterIterator from AttributedString return as.getIterator(); }
public AttributedCharacterIterator formatToCharacterIterator(Object object) { if (object == null) { throw new NullPointerException("object == null"); } if (!(object instanceof Number)) { throw new IllegalArgumentException("object not a Number: " + object.getClass()); } Number number = (Number) object; FieldPositionIterator fpIter = new FieldPositionIterator(); String text; if (number instanceof BigInteger || number instanceof BigDecimal) { text = new String(formatDigitList(this.address, number.toString(), fpIter)); } else if (number instanceof Double || number instanceof Float) { double dv = number.doubleValue(); text = new String(formatDouble(this.address, dv, fpIter)); } else { long lv = number.longValue(); text = new String(formatLong(this.address, lv, fpIter)); } AttributedString as = new AttributedString(text); while (fpIter.next()) { Format.Field field = fpIter.field(); as.addAttribute(field, field, fpIter.start(), fpIter.limit()); } // return the CharacterIterator from AttributedString return as.getIterator(); }
/** * Replace a tab with the effective number of white spaces. */ private String tab2space(TextRun tr) { AttributedString string = new AttributedString(" "); String fontFamily = tr.getFontFamily(); if (fontFamily == null) { fontFamily = "Lucida Sans"; } string.addAttribute(TextAttribute.FAMILY, fontFamily); Double fs = tr.getFontSize(); if (fs == null) { fs = 12d; } string.addAttribute(TextAttribute.SIZE, fs.floatValue()); TextLayout l = new TextLayout(string.getIterator(), new FontRenderContext(null, true, true)); double wspace = l.getAdvance(); Double tabSz = paragraph.getDefaultTabSize(); if (tabSz == null) { tabSz = wspace*4; } int numSpaces = (int)Math.ceil(tabSz / wspace); StringBuilder buf = new StringBuilder(); for(int i = 0; i < numSpaces; i++) { buf.append(' '); } return buf.toString(); }
if ((value instanceof Annotation && runStart >= start && limit <= end) || (value != null && !(value instanceof Annotation))) { addAttribute(attribute, value, (runStart < start ? start : runStart) - start, (limit > end ? end : limit) - start);
attributedString.addAttribute(TextAttribute.FONT, g.getFont()); attributedString.addAttribute(TextAttribute.FOREGROUND, Color.black);
Object value = iterator.getAttribute(attribute); if (value != null) { addAttribute(attribute, value, start, limit);
string.addAttribute(asd.attribute, asd.value, asd.beginIndex, asd.endIndex);
@Override public void applyToAttributedString(AttributedString str, Font normal, Font italic) { if (end > start + 1) { str.addAttribute(TextAttribute.FONT, italic, start, end); } } }
as.addAttribute(TextAttribute.FOREGROUND, g.getPaint()); as.addAttribute(TextAttribute.FONT, g.getFont()); AttributedCharacterIterator aci = as.getIterator();
str.addAttribute(TextAttribute.FOREGROUND, fgPaint); str.addAttribute(TextAttribute.FAMILY, buFont.getTypeface()); str.addAttribute(TextAttribute.SIZE, fontSize);
private AttributedString getSelectedString(String str) { AttributedString string = new AttributedString(str); string.addAttribute(TextAttribute.FONT, HexView.BOLD_FONT); string.addAttribute(TextAttribute.FOREGROUND, HexView.SELECTED_COLOR); return string; }
private AttributedString getStyledText(String text) { // TODO: Find a better way to handle empty Strings (like for example paragraph line breaks) if (text.length() == 0) text = " "; AttributedString attrString = new AttributedString(text); attrString.addAttribute(TextAttribute.FONT, getFont()); if (fillColor != null) attrString.addAttribute(TextAttribute.FOREGROUND, fillColor.getAwtColor()); if (align == Align.RIGHT) { //attrString.addAttribute(TextAttribute.RUN_DIRECTION, TextAttribute.RUN_DIRECTION_RTL); } else if (align == Align.CENTER) { // TODO: Center alignment? } else if (align == Align.JUSTIFY) { attrString.addAttribute(TextAttribute.JUSTIFICATION, TextAttribute.JUSTIFICATION_FULL); } return attrString; }
@Override public void applyToAttributedString(AttributedString str, Font normal, Font italic) { Image symbolImage = ManaSymbols.getSizedManaSymbol(symbol.replace("/", ""), normal.getSize()); if (symbolImage != null) { ImageGraphicAttribute imgAttr = new ImageGraphicAttribute(symbolImage, GraphicAttribute.BOTTOM_ALIGNMENT); str.addAttribute(TextAttribute.CHAR_REPLACEMENT, imgAttr, location, location + 1); } } }
private AttributedString buildAttributedLine(String line, List<FontRange> ranges) { if (ranges.size() == 1) { // create a uniform attribute AttributedString Map<TextAttribute, Object> map = new HashMap<TextAttribute, Object>(); map.put(TextAttribute.FONT, ranges.get(0).font); AttributedString as = new AttributedString(line, map); return as; } // build a multifont attributed string AttributedString as = new AttributedString(line); for (FontRange range : ranges) { as.addAttribute(TextAttribute.FONT, range.font, range.startChar, range.endChar); } return as; }
public AttributedString generateAttributedString(Font normal, Font italic) { // Build the final attributed text using the regions // Do it in reverse order for proper handling of regions where // there are multiple attributes stacked (EG: bold + italic) AttributedString attributedRule = new AttributedString(text); if (!text.isEmpty()) { attributedRule.addAttribute(TextAttribute.FONT, normal); for (int i = regions.size() - 1; i >= 0; --i) { regions.get(i).applyToAttributedString(attributedRule, normal, italic); } } return attributedRule; } }