List of usage examples for java.text AttributedString getIterator
public AttributedCharacterIterator getIterator()
From source file:MainClass.java
public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Font serifFont = new Font("Serif", Font.PLAIN, 32); AttributedString as = new AttributedString("www.java2s.com"); as.addAttribute(TextAttribute.FONT, serifFont); Image image = createImage();/* w w w. ja v a 2 s.c o m*/ ImageGraphicAttribute imageAttribute = new ImageGraphicAttribute(image, GraphicAttribute.TOP_ALIGNMENT); as.addAttribute(TextAttribute.CHAR_REPLACEMENT, imageAttribute, 5, 6); g2.drawString(as.getIterator(), 20, 120); }
From source file:image.writer.ImageWriterExample1.java
/** * Paints a few things on a Graphics2D instance. * @param g2d the Graphics2D instance/*from w w w . ja v a 2s . co m*/ * @param pageNum a page number */ protected void paintSome(Graphics2D g2d, int pageNum) { //Paint a bounding box g2d.drawRect(0, 0, 400, 200); //A few rectangles rotated and with different color Graphics2D copy = (Graphics2D) g2d.create(); int c = 12; for (int i = 0; i < c; i++) { float f = ((i + 1) / (float) c); Color col = new Color(0.0f, 1 - f, 0.0f); copy.setColor(col); copy.fillRect(70, 90, 50, 50); copy.rotate(-2 * Math.PI / (double) c, 70, 90); } copy.dispose(); //Some text copy = (Graphics2D) g2d.create(); copy.rotate(-0.25); copy.setColor(Color.RED); copy.setFont(new Font("sans-serif", Font.PLAIN, 36)); copy.drawString("Hello world!", 140, 140); copy.setColor(Color.RED.darker()); copy.setFont(new Font("serif", Font.PLAIN, 36)); copy.drawString("Hello world!", 140, 180); copy.dispose(); //Try attributed text AttributedString aString = new AttributedString("This is attributed text."); aString.addAttribute(TextAttribute.FAMILY, "SansSerif"); aString.addAttribute(TextAttribute.FAMILY, "Serif", 8, 18); aString.addAttribute(TextAttribute.FOREGROUND, Color.orange, 8, 18); g2d.drawString(aString.getIterator(), 250, 170); g2d.drawString("Page: " + pageNum, 250, 190); }
From source file:forge.view.arcane.util.OutlinedLabel.java
/** {@inheritDoc} */ @Override/*w w w . j a v a 2s .com*/ 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:Main.java
public void paint(Graphics g) { String input = "this is a test.this is a test.this is a test.this is a test."; AttributedString attributedString = new AttributedString(input); attributedString.addAttribute(TextAttribute.FONT, (Font) UIManager.get("Label.font")); Color color = (Color) UIManager.get("Label.foreground"); attributedString.addAttribute(TextAttribute.FOREGROUND, color); super.paint(g); Graphics2D g2d = (Graphics2D) g; int width = getSize().width; int x = 10;/* w w w . j av a 2s . c om*/ int y = 30; AttributedCharacterIterator characterIterator = attributedString.getIterator(); FontRenderContext fontRenderContext = g2d.getFontRenderContext(); LineBreakMeasurer measurer = new LineBreakMeasurer(characterIterator, fontRenderContext); while (measurer.getPosition() < characterIterator.getEndIndex()) { TextLayout textLayout = measurer.nextLayout(width); y += textLayout.getAscent(); textLayout.draw(g2d, x, y); y += textLayout.getDescent() + textLayout.getLeading(); } }
From source file:CodePointInputMethod.java
/** * Send the committed text to the client. *///ww w . j a v a 2 s . c om private void sendCommittedText() { AttributedString as = new AttributedString(buffer.toString()); context.dispatchInputMethodEvent(InputMethodEvent.INPUT_METHOD_TEXT_CHANGED, as.getIterator(), buffer.length(), TextHitInfo.leading(insertionPoint), null); buffer.setLength(0); insertionPoint = 0; format = UNSET; }
From source file:CodePointInputMethod.java
/** * Send the composed text to the client. *///w ww . jav a 2 s . c o m private void sendComposedText() { AttributedString as = new AttributedString(buffer.toString()); as.addAttribute(TextAttribute.INPUT_METHOD_HIGHLIGHT, InputMethodHighlight.SELECTED_RAW_TEXT_HIGHLIGHT); context.dispatchInputMethodEvent(InputMethodEvent.INPUT_METHOD_TEXT_CHANGED, as.getIterator(), 0, TextHitInfo.leading(insertionPoint), null); }
From source file:com.pronoiahealth.olhie.server.services.BookCoverImageService.java
/** * Create an image that contains text/*from www . j a v a 2 s .co m*/ * * @param height * @param width * @param text * @param textColor * @param center * @param fontMap * @param type * @return */ private BufferedImage createText(int height, int width, String text, String textColor, boolean center, Map<TextAttribute, Object> fontMap, int type) { BufferedImage img = new BufferedImage(width, height, type); Graphics2D g2d = null; try { g2d = (Graphics2D) img.getGraphics(); // Create attributed text AttributedString txt = new AttributedString(text, fontMap); // Set graphics color g2d.setColor(Color.decode(textColor)); // Create a new LineBreakMeasurer from the paragraph. // It will be cached and re-used. AttributedCharacterIterator paragraph = txt.getIterator(); int paragraphStart = paragraph.getBeginIndex(); int paragraphEnd = paragraph.getEndIndex(); FontRenderContext frc = g2d.getFontRenderContext(); LineBreakMeasurer lineMeasurer = new LineBreakMeasurer(paragraph, frc); // Set break width to width of Component. float breakWidth = (float) width; float drawPosY = 0; // Set position to the index of the first character in the // paragraph. lineMeasurer.setPosition(paragraphStart); // Get lines until the entire paragraph has been displayed. while (lineMeasurer.getPosition() < paragraphEnd) { // Retrieve next layout. A cleverer program would also cache // these layouts until the component is re-sized. TextLayout layout = lineMeasurer.nextLayout(breakWidth); // Compute pen x position. If the paragraph is right-to-left we // will align the TextLayouts to the right edge of the panel. // Note: drawPosX is always where the LEFT of the text is // placed. float drawPosX = layout.isLeftToRight() ? 0 : breakWidth - layout.getAdvance(); if (center == true) { double xOffSet = (width - layout.getBounds().getWidth()) / 2; drawPosX = drawPosX + new Float(xOffSet); } // Move y-coordinate by the ascent of the layout. drawPosY += layout.getAscent(); // Draw the TextLayout at (drawPosX, drawPosY). layout.draw(g2d, drawPosX, drawPosY); // Move y-coordinate in preparation for next layout. drawPosY += layout.getDescent() + layout.getLeading(); } } finally { if (g2d != null) { g2d.dispose(); } } return img; }
From source file:net.sf.jasperreports.engine.fill.SimpleTextLineWrapper.java
protected int measureExactLineBreakIndex(float width, int endLimit, boolean requireWord) { //FIXME would it be faster to create and cache a LineBreakMeasurer for the whole paragraph? Map<Attribute, Object> attributes = new HashMap<Attribute, Object>(); // we only need the font as it includes the size and style attributes.put(TextAttribute.FONT, fontInfo.fontInfo.font); String textLine = paragraphText.substring(paragraphPosition, endLimit); AttributedString attributedLine = new AttributedString(textLine, attributes); // we need a fresh iterator for the line BreakIterator breakIterator = paragraphTruncateAtChar ? BreakIterator.getCharacterInstance() : BreakIterator.getLineInstance(); LineBreakMeasurer breakMeasurer = new LineBreakMeasurer(attributedLine.getIterator(), breakIterator, context.getFontRenderContext()); int breakIndex = breakMeasurer.nextOffset(width, endLimit - paragraphPosition, requireWord) + paragraphPosition;//from w ww. ja v a2 s . com if (logTrace) { log.trace("exact line break index measured at " + (paragraphOffset + breakIndex)); } return breakIndex; }
From source file:com.qumasoft.guitools.compare.ContentRow.java
@Override public void paint(Graphics g) { if ((getRowType() == ROWTYPE_REPLACE) && rowHadAnnotations) { Graphics2D g2 = (Graphics2D) g; String s = getText();/*from ww w . j ava 2 s .c o m*/ g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); if (s.length() > 0) { int index = 0; AttributedString attributedString = new AttributedString(s, getFont().getAttributes()); try { for (byte rType : fileACharacterTypeArray) { switch (rType) { case ContentRow.ROWTYPE_DELETE: attributedString.addAttribute(TextAttribute.STRIKETHROUGH, null, index, index + 1); break; case ContentRow.ROWTYPE_REPLACE: attributedString.addAttribute(TextAttribute.BACKGROUND, ColorManager.getReplaceCompareHiliteBackgroundColor(), index, index + 1); break; default: break; } index++; } g2.drawString(attributedString.getIterator(), 0, getFont().getSize()); } catch (java.lang.IllegalArgumentException e) { LOGGER.log(Level.WARNING, "bad replace indexes. begin index: [" + index + "] end index: [" + index + "]. String length: [" + s.length() + "]"); } } else { super.paint(g); } } else { super.paint(g); } }
From source file:com.dlya.facturews.DlyaPdfExporter2.java
/** * */// w w w . jav a 2 s . c o m protected Phrase getPhrase(AttributedString as, String text, JRPrintText textElement) { Phrase phrase = new Phrase(); int runLimit = 0; AttributedCharacterIterator iterator = as.getIterator(); Locale locale = getTextLocale(textElement); boolean firstChunk = true; while (runLimit < text.length() && (runLimit = iterator.getRunLimit()) <= text.length()) { Map<Attribute, Object> attributes = iterator.getAttributes(); Chunk chunk = getChunk(attributes, text.substring(iterator.getIndex(), runLimit), locale); if (firstChunk) { // only set anchor + bookmark for the first chunk in the text setAnchor(chunk, textElement, textElement); } JRPrintHyperlink hyperlink = textElement; if (hyperlink.getHyperlinkTypeValue() == HyperlinkTypeEnum.NONE) { hyperlink = (JRPrintHyperlink) attributes.get(JRTextAttribute.HYPERLINK); } setHyperlinkInfo(chunk, hyperlink); phrase.add(chunk); iterator.setIndex(runLimit); firstChunk = false; } return phrase; }