List of usage examples for java.text AttributedCharacterIterator first
public char first();
From source file:AttributedStringUtilities.java
/** * Tests two attributed strings for equality. * // w w w. j a v a 2 s.c o m * @param s1 * string 1 (<code>null</code> permitted). * @param s2 * string 2 (<code>null</code> permitted). * * @return <code>true</code> if <code>s1</code> and <code>s2</code> are * equal or both <code>null</code>, and <code>false</code> * otherwise. */ public static boolean equal(AttributedString s1, AttributedString s2) { if (s1 == null) { return (s2 == null); } if (s2 == null) { return false; } AttributedCharacterIterator it1 = s1.getIterator(); AttributedCharacterIterator it2 = s2.getIterator(); char c1 = it1.first(); char c2 = it2.first(); int start = 0; while (c1 != CharacterIterator.DONE) { int limit1 = it1.getRunLimit(); int limit2 = it2.getRunLimit(); if (limit1 != limit2) { return false; } // if maps aren't equivalent, return false Map m1 = it1.getAttributes(); Map m2 = it2.getAttributes(); if (!m1.equals(m2)) { return false; } // now check characters in the run are the same for (int i = start; i < limit1; i++) { if (c1 != c2) { return false; } c1 = it1.next(); c2 = it2.next(); } start = limit1; } return c2 == CharacterIterator.DONE; }
From source file:Main.java
/** * Serialises an <code>AttributedString</code> object. * * @param as the attributed string object (<code>null</code> permitted). * @param stream the output stream (<code>null</code> not permitted). * * @throws IOException if there is an I/O error. *//*from www. j a v a 2 s .c om*/ public static void writeAttributedString(AttributedString as, ObjectOutputStream stream) throws IOException { if (stream == null) { throw new IllegalArgumentException("Null 'stream' argument."); } if (as != null) { stream.writeBoolean(false); AttributedCharacterIterator aci = as.getIterator(); // build a plain string from aci // then write the string StringBuffer plainStr = new StringBuffer(); char current = aci.first(); while (current != CharacterIterator.DONE) { plainStr = plainStr.append(current); current = aci.next(); } stream.writeObject(plainStr.toString()); // then write the attributes and limits for each run current = aci.first(); int begin = aci.getBeginIndex(); while (current != CharacterIterator.DONE) { // write the current character - when the reader sees that this // is not CharacterIterator.DONE, it will know to read the // run limits and attributes stream.writeChar(current); // now write the limit, adjusted as if beginIndex is zero int limit = aci.getRunLimit(); stream.writeInt(limit - begin); // now write the attribute set Map atts = new HashMap(aci.getAttributes()); stream.writeObject(atts); current = aci.setIndex(limit); } // write a character that signals to the reader that all runs // are done... stream.writeChar(CharacterIterator.DONE); } else { // write a flag that indicates a null stream.writeBoolean(true); } }
From source file:forge.view.arcane.util.OutlinedLabel.java
/** {@inheritDoc} */ @Override//from w ww . j a va2 s.c om public final void paint(final Graphics g) { if (getText().length() == 0) { return; } Dimension size = getSize(); // // if( size.width < 50 ) { // g.setColor(Color.cyan); // g.drawRect(0, 0, size.width-1, size.height-1); // } Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); int textX = outlineSize, textY = 0; int wrapWidth = Math.max(0, wrap ? size.width - outlineSize * 2 : Integer.MAX_VALUE); final String text = getText(); AttributedString attributedString = new AttributedString(text); if (!StringUtils.isEmpty(text)) { attributedString.addAttribute(TextAttribute.FONT, getFont()); } AttributedCharacterIterator charIterator = attributedString.getIterator(); FontRenderContext fontContext = g2d.getFontRenderContext(); LineBreakMeasurer measurer = new LineBreakMeasurer(charIterator, BreakIterator.getWordInstance(Locale.ENGLISH), fontContext); int lineCount = 0; while (measurer.getPosition() < charIterator.getEndIndex()) { measurer.nextLayout(wrapWidth); lineCount++; if (lineCount > 2) { break; } } charIterator.first(); // Use char wrap if word wrap would cause more than two lines of text. if (lineCount > 2) { measurer = new LineBreakMeasurer(charIterator, BreakIterator.getCharacterInstance(Locale.ENGLISH), fontContext); } else { measurer.setPosition(0); } while (measurer.getPosition() < charIterator.getEndIndex()) { TextLayout textLayout = measurer.nextLayout(wrapWidth); float ascent = textLayout.getAscent(); textY += ascent; // Move down to baseline. g2d.setColor(outlineColor); g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f)); textLayout.draw(g2d, textX + outlineSize, textY - outlineSize); textLayout.draw(g2d, textX + outlineSize, textY + outlineSize); textLayout.draw(g2d, textX - outlineSize, textY - outlineSize); textLayout.draw(g2d, textX - outlineSize, textY + outlineSize); g2d.setColor(getForeground()); g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f)); textLayout.draw(g2d, textX, textY); // Move down to top of next line. textY += textLayout.getDescent() + textLayout.getLeading(); } }
From source file:org.jfree.experimental.swt.SWTGraphics2D.java
/** * Draws a string at the specified position. * * @param iterator the string./*from www .ja va 2s .c om*/ * @param x the x-coordinate. * @param y the y-coordinate. */ public void drawString(AttributedCharacterIterator iterator, int x, int y) { // for now we simply want to extract the chars from the iterator // and call an unstyled text renderer StringBuffer sb = new StringBuffer(); int numChars = iterator.getEndIndex() - iterator.getBeginIndex(); char c = iterator.first(); for (int i = 0; i < numChars; i++) { sb.append(c); c = iterator.next(); } drawString(new String(sb), x, y); }
From source file:org.apache.fop.svg.AbstractFOPTextPainter.java
/** * Paint a single text run on the Graphics2D at a given location. * @param run the text run to paint// w w w . j a v a2 s . c o m * @param g2d the Graphics2D to paint to * @param loc the current location of the "cursor" * @return the new location of the "cursor" after painting the text run */ protected Point2D paintTextRun(StrokingTextPainter.TextRun run, Graphics2D g2d, Point2D loc) { AttributedCharacterIterator aci = run.getACI(); aci.first(); updateLocationFromACI(aci, loc); AffineTransform at = g2d.getTransform(); loc = at.transform(loc, null); // font Font font = getFont(aci); if (font != null) { nativeTextHandler.setOverrideFont(font); } // color TextPaintInfo tpi = (TextPaintInfo) aci .getAttribute(GVTAttributedCharacterIterator.TextAttribute.PAINT_INFO); if (tpi == null) { return loc; } Paint foreground = tpi.fillPaint; if (foreground instanceof Color) { Color col = (Color) foreground; g2d.setColor(col); } g2d.setPaint(foreground); // text anchor TextNode.Anchor anchor = (TextNode.Anchor) aci .getAttribute(GVTAttributedCharacterIterator.TextAttribute.ANCHOR_TYPE); // text String txt = getText(aci); float advance = getStringWidth(txt, font); float tx = 0; if (anchor != null) { switch (anchor.getType()) { case TextNode.Anchor.ANCHOR_MIDDLE: tx = -advance / 2; break; case TextNode.Anchor.ANCHOR_END: tx = -advance; break; default: //nop } } // draw string double x = loc.getX(); double y = loc.getY(); try { try { nativeTextHandler.drawString(g2d, txt, (float) x + tx, (float) y); } catch (IOException ioe) { if (g2d instanceof AFPGraphics2D) { ((AFPGraphics2D) g2d).handleIOException(ioe); } } } finally { nativeTextHandler.setOverrideFont(null); } loc.setLocation(loc.getX() + advance, loc.getY()); return loc; }
From source file:org.apache.fop.svg.AbstractFOPTextPainter.java
/** * Extract the raw text from an ACI./*w ww . j av a 2 s .co m*/ * @param aci ACI to inspect * @return the extracted text */ protected String getText(AttributedCharacterIterator aci) { StringBuffer sb = new StringBuffer(aci.getEndIndex() - aci.getBeginIndex()); for (char c = aci.first(); c != CharacterIterator.DONE; c = aci.next()) { sb.append(c); } return sb.toString(); }
From source file:org.apache.fop.svg.ACIUtils.java
/** * Dumps the contents of an ACI to System.out. Used for debugging only. * @param aci the ACI to dump//from ww w . ja v a 2 s .co m */ public static void dumpAttrs(AttributedCharacterIterator aci) { aci.first(); Set<Entry<Attribute, Object>> entries = aci.getAttributes().entrySet(); for (Map.Entry<Attribute, Object> entry : entries) { if (entry.getValue() != null) { System.out.println(entry.getKey() + ": " + entry.getValue()); } } int start = aci.getBeginIndex(); System.out.print("AttrRuns: "); while (aci.current() != CharacterIterator.DONE) { int end = aci.getRunLimit(); System.out.print("" + (end - start) + ", "); aci.setIndex(end); if (start == end) { break; } start = end; } System.out.println(""); }
From source file:org.apache.fop.svg.NativeTextPainter.java
/** * Collects all characters from an {@link AttributedCharacterIterator}. * @param runaci the character iterator/*from w w w .j ava 2 s .c o m*/ * @return the characters */ protected CharSequence collectCharacters(AttributedCharacterIterator runaci) { StringBuffer chars = new StringBuffer(); for (runaci.first(); runaci.getIndex() < runaci.getEndIndex();) { chars.append(runaci.current()); runaci.next(); } return chars; }
From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfGraphics2D.java
/** * @see Graphics2D#drawString(AttributedCharacterIterator, float, float) *///from w w w .j a v a 2s .com @Override public void drawString(final AttributedCharacterIterator iter, float x, final float y) { /* * StringBuffer sb = new StringBuffer(); for(char c = iter.first(); c != AttributedCharacterIterator.DONE; c = * iter.next()) { sb.append(c); } drawString(sb.toString(),x,y); */ final StringBuilder stringbuffer = new StringBuilder(iter.getEndIndex()); for (char c = iter.first(); c != AttributedCharacterIterator.DONE; c = iter.next()) { if (iter.getIndex() == iter.getRunStart()) { if (stringbuffer.length() > 0) { drawString(stringbuffer.toString(), x, y); final FontMetrics fontmetrics = getFontMetrics(); x = (float) (x + fontmetrics.getStringBounds(stringbuffer.toString(), this).getWidth()); stringbuffer.delete(0, stringbuffer.length()); } doAttributes(iter); } stringbuffer.append(c); } drawString(stringbuffer.toString(), x, y); underline = false; }