Path: blob/master/src/demo/share/jfc/J2Ddemo/java2d/demos/Fonts/AttributedStr.java
41175 views
/*1*2* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* - Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* - Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* - Neither the name of Oracle nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS20* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,21* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR22* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR23* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,24* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,25* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR26* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF27* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING28* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/31package java2d.demos.Fonts;323334import static java.awt.Font.BOLD;35import static java.awt.Font.ITALIC;36import static java.awt.Font.PLAIN;37import static java.awt.font.TextAttribute.BACKGROUND;38import static java.awt.font.TextAttribute.CHAR_REPLACEMENT;39import static java.awt.font.TextAttribute.FONT;40import static java.awt.font.TextAttribute.FOREGROUND;41import static java.awt.font.TextAttribute.UNDERLINE;42import static java.awt.font.TextAttribute.UNDERLINE_ON;43import java.awt.Color;44import java.awt.Font;45import java.awt.GradientPaint;46import java.awt.Graphics2D;47import java.awt.Rectangle;48import java.awt.Shape;49import java.awt.TexturePaint;50import java.awt.font.FontRenderContext;51import java.awt.font.GraphicAttribute;52import java.awt.font.ImageGraphicAttribute;53import java.awt.font.LineBreakMeasurer;54import java.awt.font.ShapeGraphicAttribute;55import java.awt.font.TextLayout;56import java.awt.geom.AffineTransform;57import java.awt.geom.Ellipse2D;58import java.awt.image.BufferedImage;59import java.text.AttributedCharacterIterator;60import java.text.AttributedString;61import java2d.Surface;626364/**65* Demonstrates how to build an AttributedString and then render the66* string broken over lines.67*/68@SuppressWarnings("serial")69public class AttributedStr extends Surface {7071static final Color black = new Color(20, 20, 20);72static final Color blue = new Color(94, 105, 176);73static final Color yellow = new Color(255, 255, 140);74static final Color red = new Color(149, 43, 42);75static final Color white = new Color(240, 240, 255);76static final String text =77" A quick brown fox jumped over the lazy duke ";78static final AttributedString as = new AttributedString(text);79static AttributedCharacterIterator aci;8081static {82Shape shape = new Ellipse2D.Double(0, 25, 12, 12);83ShapeGraphicAttribute sga = new ShapeGraphicAttribute(shape,84GraphicAttribute.TOP_ALIGNMENT, false);85as.addAttribute(CHAR_REPLACEMENT, sga, 0, 1);868788Font font = new Font("sanserif", BOLD | ITALIC, 20);89int index = text.indexOf("quick");90as.addAttribute(FONT, font, index, index + 5);9192index = text.indexOf("brown");93font = new Font(Font.SERIF, BOLD, 20);94as.addAttribute(FONT, font, index, index + 5);95as.addAttribute(FOREGROUND, red, index, index + 5);9697index = text.indexOf("fox");98AffineTransform fontAT = new AffineTransform();99fontAT.rotate(Math.toRadians(10));100Font fx = new Font(Font.SERIF, BOLD, 30).deriveFont(fontAT);101as.addAttribute(FONT, fx, index, index + 1);102as.addAttribute(FONT, fx, index + 1, index + 2);103as.addAttribute(FONT, fx, index + 2, index + 3);104105fontAT.setToRotation(Math.toRadians(-4));106fx = font.deriveFont(fontAT);107index = text.indexOf("jumped");108as.addAttribute(FONT, fx, index, index + 6);109110font = new Font(Font.SERIF, BOLD | ITALIC, 30);111index = text.indexOf("over");112as.addAttribute(UNDERLINE, UNDERLINE_ON, index, index + 4);113as.addAttribute(FOREGROUND, white, index, index + 4);114as.addAttribute(FONT, font, index, text.length());115116font = new Font(Font.DIALOG, PLAIN, 20);117int i = text.indexOf("duke");118as.addAttribute(FONT, font, index, i - 1);119120BufferedImage bi = new BufferedImage(4, 4, BufferedImage.TYPE_INT_ARGB);121bi.setRGB(0, 0, 0xffffffff);122TexturePaint tp = new TexturePaint(bi, new Rectangle(0, 0, 4, 4));123as.addAttribute(BACKGROUND, tp, i, i + 4);124font = new Font(Font.SERIF, BOLD, 40);125as.addAttribute(FONT, font, i, i + 4);126}127128public AttributedStr() {129setBackground(Color.white);130131Font font = getFont("A.ttf");132if (font != null) {133font = font.deriveFont(PLAIN, 70);134} else {135font = new Font(Font.SERIF, PLAIN, 50);136}137int index = text.indexOf("A") + 1;138as.addAttribute(FONT, font, 0, index);139as.addAttribute(FOREGROUND, white, 0, index);140141font = new Font(Font.DIALOG, PLAIN, 40);142int size = getFontMetrics(font).getHeight();143BufferedImage bi =144new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);145Graphics2D big = bi.createGraphics();146big.drawImage(getImage("snooze.png"), 0, 0, size, size, null);147ImageGraphicAttribute iga =148new ImageGraphicAttribute(bi, GraphicAttribute.TOP_ALIGNMENT);149as.addAttribute(CHAR_REPLACEMENT, iga, text.length() - 1, text.length());150151aci = as.getIterator();152}153154@Override155public void render(int w, int h, Graphics2D g2) {156157float x = 5, y = 0;158FontRenderContext frc = g2.getFontRenderContext();159LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc);160161g2.setPaint(new GradientPaint(0, h, blue, w, 0, black));162g2.fillRect(0, 0, w, h);163164g2.setColor(white);165String s = "AttributedString LineBreakMeasurer";166Font font = new Font(Font.SERIF, PLAIN, 12);167TextLayout tl = new TextLayout(s, font, frc);168169tl.draw(g2, 5, y += (float) tl.getBounds().getHeight());170171g2.setColor(yellow);172173while (y < h - tl.getAscent()) {174lbm.setPosition(0);175while (lbm.getPosition() < text.length()) {176tl = lbm.nextLayout(w - x);177if (!tl.isLeftToRight()) {178x = w - tl.getAdvance();179}180tl.draw(g2, x, y += tl.getAscent());181y += tl.getDescent() + tl.getLeading();182}183}184}185186public static void main(String[] s) {187createDemoFrame(new AttributedStr());188}189}190191192