001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.print.impl.provider;
042:
043: import java.awt.Color;
044: import java.awt.Font;
045: import java.awt.Graphics2D;
046: import java.awt.font.TextAttribute;
047: import java.awt.font.TextLayout;
048:
049: import java.text.AttributedCharacterIterator;
050: import java.text.CharacterIterator;
051: import java.util.ArrayList;
052: import java.util.List;
053:
054: import org.openide.text.AttributedCharacters;
055:
056: import org.netbeans.modules.print.impl.util.Option;
057: import static org.netbeans.modules.print.impl.util.UI.*;
058:
059: /**
060: * @author Vladimir Yaroslavskiy
061: * @version 2006.05.30
062: */
063: final class ComponentLine {
064:
065: ComponentLine(AttributedCharacterIterator it, Font defaultFont,
066: Color defaultColor) {
067: for (char c = it.first(); c != CharacterIterator.DONE; c = it
068: .next()) {
069: Font font = (Font) it.getAttribute(TextAttribute.FONT);
070: Color color = (Color) it
071: .getAttribute(TextAttribute.FOREGROUND);
072: mySymbols.add(new Symbol(c, createFont(font, defaultFont),
073: createColor(color, defaultColor)));
074: }
075: checkSpaces(defaultFont, defaultColor);
076: }
077:
078: ComponentLine(String text, Font font, Color color) {
079: for (int i = 0; i < text.length(); i++) {
080: mySymbols.add(new Symbol(text.charAt(i), font, color));
081: }
082: checkSpaces(font, color);
083: }
084:
085: private ComponentLine(List<Symbol> symbols, Font font, Color color) {
086: mySymbols = symbols;
087: checkSpaces(font, color);
088: }
089:
090: private Font createFont(Font attrFont, Font defaultFont) {
091: if (!Option.getDefault().isUseFont()) {
092: return defaultFont;
093: }
094: String name = defaultFont.getName();
095: int size = defaultFont.getSize();
096: int style = attrFont.getStyle();
097: return new Font(name, style, size);
098: }
099:
100: private Color createColor(Color attrColor, Color defaultColor) {
101: if (Option.getDefault().isUseColor()) {
102: return attrColor;
103: }
104: return defaultColor;
105: }
106:
107: private void checkSpaces(Font font, Color color) {
108: int i = length() - 1;
109: myFont = font;
110: myColor = color;
111:
112: while (i >= 1 && mySymbols.get(i).getChar() == ' ') {
113: i--;
114: }
115: mySymbols = mySymbols.subList(0, i + 1);
116:
117: if (length() == 0) {
118: mySymbols.add(new Symbol(' ', font, color));
119: }
120: }
121:
122: void prepend(String text) {
123: Font firstFont = mySymbols.get(0).getFont();
124:
125: String name = firstFont.getName();
126: int size = firstFont.getSize();
127:
128: Font font = new Font(name, Font.PLAIN, size);
129:
130: for (int i = text.length() - 1; i >= 0; i--) {
131: mySymbols.add(0, new Symbol(text.charAt(i), font, myColor));
132: }
133: }
134:
135: boolean isEmpty() {
136: for (int i = 0; i < length(); i++) {
137: if (mySymbols.get(i).getChar() != ' ') {
138: return false;
139: }
140: }
141: return true;
142: }
143:
144: int length() {
145: return mySymbols.size();
146: }
147:
148: ComponentLine substring(int index1, int index2) {
149: List<Symbol> list = new ArrayList<Symbol>();
150:
151: for (int i = index1; i < index2; i++) {
152: list.add(mySymbols.get(i));
153: }
154: return new ComponentLine(list, myFont, myColor);
155: }
156:
157: ComponentLine substring(int index) {
158: return substring(index, mySymbols.size());
159: }
160:
161: int getAscent() {
162: return (int) Math.ceil(getTextLayout().getAscent());
163: }
164:
165: int getDescent() {
166: return (int) Math.ceil(getTextLayout().getDescent());
167: }
168:
169: int getLeading() {
170: return (int) Math.ceil(getTextLayout().getLeading());
171: }
172:
173: int getWidth() {
174: int offset = getOffset();
175:
176: if (offset > 0) {
177: offset = 0;
178: }
179: //out(this + " " + getOffset());
180: return (int) Math.ceil(getTextLayout().getBounds().getMaxX()
181: - offset);
182: }
183:
184: int getOffset() {
185: return (int) Math.ceil(getTextLayout().getBounds().getX());
186: }
187:
188: void draw(Graphics2D g, int x, int y) {
189: getTextLayout().draw(g, x, y);
190: }
191:
192: private TextLayout getTextLayout() {
193: if (myTextLayout == null) {
194: //out();
195: //out("TEXT : '" + this + "'");
196: myTextLayout = new TextLayout(getIterator(),
197: Option.FONT_RENDER_CONTEXT);
198: }
199: return myTextLayout;
200: }
201:
202: private AttributedCharacterIterator getIterator() {
203: AttributedCharacters characters = new AttributedCharacters();
204:
205: for (int i = 0; i < length(); i++) {
206: characters.append(mySymbols.get(i).getChar(), mySymbols
207: .get(i).getFont(), mySymbols.get(i).getColor());
208: }
209: return characters.iterator();
210: }
211:
212: int lastIndexOf(char c, int index) {
213: for (int i = index; i >= 0; i--) {
214: if (mySymbols.get(i).getChar() == c) {
215: return i;
216: }
217: }
218: return -1;
219: }
220:
221: public String toString() {
222: StringBuffer buffer = new StringBuffer();
223:
224: for (int i = 0; i < length(); i++) {
225: buffer.append(mySymbols.get(i).getChar());
226: }
227: return buffer.toString();
228: }
229:
230: void show() {
231: for (int i = 0; i < length(); i++) {
232: out(mySymbols.get(i));
233: }
234: }
235:
236: // --------------------------------
237: private static final class Symbol {
238:
239: Symbol(char c, Font font, Color color) {
240: myChar = c;
241: myFont = font;
242: myColor = color;
243: }
244:
245: char getChar() {
246: return myChar;
247: }
248:
249: Font getFont() {
250: return myFont;
251: }
252:
253: Color getColor() {
254: return myColor;
255: }
256:
257: void setColor(Color color) {
258: myColor = color;
259: }
260:
261: public String toString() {
262: return "'" + // NOI18N
263: myChar + "' " + // NOI18N
264: getString(myFont) + " " + // NOI18N
265: getString(myColor);
266: }
267:
268: private String getString(Color color) {
269: return "(" + // NOI18N
270: color.getRed() + ", " + // NOI18N
271: color.getGreen() + ", " + // NOI18N
272: color.getBlue() + ")"; // NOI18N
273: }
274:
275: private String getString(Font font) {
276: String style = ""; // NOI18N
277:
278: if (font.isBold()) {
279: style += "bold"; // NOI18N
280: }
281: if (font.isItalic()) {
282: style += " italic"; // NOI18N
283: } else {
284: style += " plain"; // NOI18N
285: }
286: return "[" + // NOI18N
287: font.getName() + ", " + // NOI18N
288: style + ", " + // NOI18N
289: font.getSize() + "]"; // NOI18N
290: }
291:
292: private char myChar;
293: private Font myFont;
294: private Color myColor;
295: }
296:
297: private Font myFont;
298: private Color myColor;
299: private TextLayout myTextLayout;
300: private List<Symbol> mySymbols = new ArrayList<Symbol>();
301: }
|