1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 /** 18 * @author Ilya S. Okomin 19 * @version $Revision$ 20 */ 21 22 package java.awt.font; 23 24 import java.awt.Graphics2D; 25 import java.awt.geom.Rectangle2D; 26 27 import org.apache.harmony.awt.internal.nls.Messages; 28 29 /** 30 * The GraphicAttribute abstract class provides an opportunity to insert 31 * graphical elements in printed text. 32 * 33 * @since Android 1.0 34 */ 35 public abstract class GraphicAttribute { 36 37 /** 38 * The Constant TOP_ALIGNMENT indicates using the top line to calculate 39 * placement of graphics. 40 */ 41 public static final int TOP_ALIGNMENT = -1; 42 43 /** 44 * The Constant BOTTOM_ALIGNMENT indicates using the bottom line to 45 * calculate placement of graphics. 46 */ 47 public static final int BOTTOM_ALIGNMENT = -2; 48 49 /** 50 * The Constant ROMAN_BASELINE indicates the placement of the roman baseline 51 * with respect to the graphics origin. 52 */ 53 public static final int ROMAN_BASELINE = 0; 54 55 /** 56 * The Constant CENTER_BASELINE indicates the placement of the center 57 * baseline with respect to the graphics origin. 58 */ 59 public static final int CENTER_BASELINE = 1; 60 61 /** 62 * The Constant HANGING_BASELINE indicates the placement of the hanging 63 * baseline with respect to the graphics origin. 64 */ 65 public static final int HANGING_BASELINE = 2; 66 67 // the alignment of this GraphicAttribute 68 /** 69 * The alignment. 70 */ 71 private int alignment; 72 73 /** 74 * Instantiates a new graphic attribute with the specified alignment. 75 * 76 * @param align 77 * the specified alignment. 78 */ GraphicAttribute(int align)79 protected GraphicAttribute(int align) { 80 if ((align < BOTTOM_ALIGNMENT) || (align > HANGING_BASELINE)) { 81 // awt.198=Illegal alignment argument 82 throw new IllegalArgumentException(Messages.getString("awt.198")); //$NON-NLS-1$ 83 } 84 this.alignment = align; 85 } 86 87 /** 88 * Draws the GraphicAttribute at the specified location. 89 * 90 * @param graphics 91 * the Graphics. 92 * @param x 93 * the X coordinate of GraphicAttribute location. 94 * @param y 95 * the Y coordinate of GraphicAttribute location. 96 */ draw(Graphics2D graphics, float x, float y)97 public abstract void draw(Graphics2D graphics, float x, float y); 98 99 /** 100 * Gets the GraphicAttribute's advance. It's the distance from the point at 101 * which the graphic is rendered and the point where the next character or 102 * graphic is rendered. 103 * 104 * @return the GraphicAttribute's advance. 105 */ getAdvance()106 public abstract float getAdvance(); 107 108 /** 109 * Gets the alignment of this GraphicAttribute. 110 * 111 * @return the alignment of this GraphicAttribute. 112 */ getAlignment()113 public final int getAlignment() { 114 return this.alignment; 115 } 116 117 /** 118 * Gets the ascent of this GraphicAttribute. 119 * 120 * @return the ascent of this GraphicAttribute. 121 */ getAscent()122 public abstract float getAscent(); 123 124 /** 125 * Gets the bounds of this GraphicAttribute. 126 * 127 * @return the bounds of this GraphicAttribute. 128 */ getBounds()129 public Rectangle2D getBounds() { 130 float ascent = getAscent(); 131 float advance = getAdvance(); 132 float descent = getDescent(); 133 134 // Default implementation - see API documentation. 135 return new Rectangle2D.Float(0, -ascent, advance, ascent + descent); 136 } 137 138 /** 139 * Gets the descent of this GraphicAttribute. 140 * 141 * @return the descent of this GraphicAttribute. 142 */ getDescent()143 public abstract float getDescent(); 144 145 /** 146 * Gets the GlyphJustificationInfo of this GraphicAttribute. 147 * 148 * @return the GlyphJustificationInfo of this GraphicAttribute. 149 */ getJustificationInfo()150 public GlyphJustificationInfo getJustificationInfo() { 151 152 /* 153 * Default implementation. Since documentation doesn't describe default 154 * values, they were calculated based on 1.5 release behavior and can be 155 * obtained using next test sample: // Create GraphicAttribute class 156 * implementation public class MyGraphicAttribute extends 157 * GraphicAttribute { protected MyGraphicAttribute(int align) { 158 * super(align); } public float getDescent() { return 0; } public float 159 * getAdvance() { return 1; } public void draw(Graphics2D g2, float x, 160 * float y) { } public float getAscent() { return 0; } } 161 * MyGraphicAttribute myGA = gat.new MyGraphicAttribute(0); // print 162 * justification parameters 163 * System.out.println(myGA.getJustificationInfo().growAbsorb); 164 * System.out.println(myGA.getJustificationInfo().shrinkAbsorb); 165 * System.out.println(myGA.getJustificationInfo().growLeftLimit); 166 * System.out.println(myGA.getJustificationInfo().growPriority); 167 * System.out.println(myGA.getJustificationInfo().growRightLimit); 168 * System.out.println(myGA.getJustificationInfo().shrinkLeftLimit); 169 * System.out.println(myGA.getJustificationInfo().shrinkPriority); 170 * System.out.println(myGA.getJustificationInfo().shrinkRightLimit); 171 * System.out.println(myGA.getJustificationInfo().weight); 172 */ 173 float advance = getAdvance(); 174 return new GlyphJustificationInfo(advance, false, 175 GlyphJustificationInfo.PRIORITY_INTERCHAR, advance / 3, advance / 3, false, 176 GlyphJustificationInfo.PRIORITY_WHITESPACE, 0, 0); 177 } 178 179 } 180