blob: 56f5807b9ffa524e3385bdddb488a6c6cfffccd3 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1998-2003 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25/*
26 *
27 * (C) Copyright IBM Corp. 1998-2003 - All Rights Reserved
28 */
29
30package sun.font;
31
32import java.awt.Font;
33import java.awt.font.FontRenderContext;
34import java.awt.font.LineMetrics;
35
36public class StandardTextSource extends TextSource {
37 char[] chars;
38 int start;
39 int len;
40 int cstart;
41 int clen;
42 int level; // assumed all uniform
43 int flags; // see GlyphVector.java
44 Font font;
45 FontRenderContext frc;
46 CoreMetrics cm;
47
48 /**
49 * Create a simple implementation of a TextSource.
50 *
51 * Chars is an array containing clen chars in the context, in
52 * logical order, contiguously starting at cstart. Start and len
53 * represent that portion of the context representing the true
54 * source; start, like cstart, is relative to the start of the
55 * character array.
56 *
57 * Level is the bidi level (0-63 for the entire context. Flags is
58 * the layout flags. Font is the font, frc is the render context,
59 * and lm is the line metrics for the entire source text, but not
60 * necessarily the context.
61 */
62 public StandardTextSource(char[] chars,
63 int start,
64 int len,
65 int cstart,
66 int clen,
67 int level,
68 int flags,
69 Font font,
70 FontRenderContext frc,
71 CoreMetrics cm) {
72 if (chars == null) {
73 throw new IllegalArgumentException("bad chars: null");
74 }
75 if (cstart < 0) {
76 throw new IllegalArgumentException("bad cstart: " + cstart);
77 }
78 if (start < cstart) {
79 throw new IllegalArgumentException("bad start: " + start + " for cstart: " + cstart);
80 }
81 if (clen < 0) {
82 throw new IllegalArgumentException("bad clen: " + clen);
83 }
84 if (cstart + clen > chars.length) {
85 throw new IllegalArgumentException("bad clen: " + clen + " cstart: " + cstart + " for array len: " + chars.length);
86 }
87 if (len < 0) {
88 throw new IllegalArgumentException("bad len: " + len);
89 }
90 if ((start + len) > (cstart + clen)) {
91 throw new IllegalArgumentException("bad len: " + len + " start: " + start + " for cstart: " + cstart + " clen: " + clen);
92 }
93 if (font == null) {
94 throw new IllegalArgumentException("bad font: null");
95 }
96 if (frc == null) {
97 throw new IllegalArgumentException("bad frc: null");
98 }
99
100 this.chars = chars;
101 this.start = start;
102 this.len = len;
103 this.cstart = cstart;
104 this.clen = clen;
105 this.level = level;
106 this.flags = flags;
107 this.font = font;
108 this.frc = frc;
109
110 if (cm != null) {
111 this.cm = cm;
112 } else {
113 LineMetrics metrics = font.getLineMetrics(chars, cstart, clen, frc);
114 this.cm = ((FontLineMetrics)metrics).cm;
115 }
116 }
117
118 /** Create a StandardTextSource whose context is coextensive with the source. */
119 public StandardTextSource(char[] chars,
120 int start,
121 int len,
122 int level,
123 int flags,
124 Font font,
125 FontRenderContext frc,
126 CoreMetrics cm) {
127 this(chars, start, len, start, len, level, flags, font, frc, cm);
128 }
129
130 /** Create a StandardTextSource whose context and source are coextensive with the entire char array. */
131 public StandardTextSource(char[] chars,
132 int level,
133 int flags,
134 Font font,
135 FontRenderContext frc) {
136 this(chars, 0, chars.length, 0, chars.length, level, flags, font, frc, null);
137 }
138
139 /** Create a StandardTextSource whose context and source are all the text in the String. */
140 public StandardTextSource(String str,
141 int level,
142 int flags,
143 Font font,
144 FontRenderContext frc) {
145 this(str.toCharArray(), 0, str.length(), 0, str.length(), level, flags, font, frc, null);
146 }
147
148 // TextSource API
149
150 public char[] getChars() {
151 return chars;
152 }
153
154 public int getStart() {
155 return start;
156 }
157
158 public int getLength() {
159 return len;
160 }
161
162 public int getContextStart() {
163 return cstart;
164 }
165
166 public int getContextLength() {
167 return clen;
168 }
169
170 public int getLayoutFlags() {
171 return flags;
172 }
173
174 public int getBidiLevel() {
175 return level;
176 }
177
178 public Font getFont() {
179 return font;
180 }
181
182 public FontRenderContext getFRC() {
183 return frc;
184 }
185
186 public CoreMetrics getCoreMetrics() {
187 return cm;
188 }
189
190 public TextSource getSubSource(int start, int length, int dir) {
191 if (start < 0 || length < 0 || (start + length) > len) {
192 throw new IllegalArgumentException("bad start (" + start + ") or length (" + length + ")");
193 }
194
195 int level = this.level;
196 if (dir != TextLineComponent.UNCHANGED) {
197 boolean ltr = (flags & 0x8) == 0;
198 if (!(dir == TextLineComponent.LEFT_TO_RIGHT && ltr) &&
199 !(dir == TextLineComponent.RIGHT_TO_LEFT && !ltr)) {
200 throw new IllegalArgumentException("direction flag is invalid");
201 }
202 level = ltr? 0 : 1;
203 }
204
205 return new StandardTextSource(chars, this.start + start, length, cstart, clen, level, flags, font, frc, cm);
206 }
207
208 public String toString() {
209 return toString(WITH_CONTEXT);
210 }
211
212 public String toString(boolean withContext) {
213 StringBuffer buf = new StringBuffer(super.toString());
214 buf.append("[start:");
215 buf.append(start);
216 buf.append(", len:" );
217 buf.append(len);
218 buf.append(", cstart:");
219 buf.append(cstart);
220 buf.append(", clen:" );
221 buf.append(clen);
222 buf.append(", chars:\"");
223 int chStart, chLimit;
224 if (withContext == WITH_CONTEXT) {
225 chStart = cstart;
226 chLimit = cstart + clen;
227 }
228 else {
229 chStart = start;
230 chLimit = start + len;
231 }
232 for (int i = chStart; i < chLimit; ++i) {
233 if (i > chStart) {
234 buf.append(" ");
235 }
236 buf.append(Integer.toHexString(chars[i]));
237 }
238 buf.append("\"");
239 buf.append(", level:");
240 buf.append(level);
241 buf.append(", flags:");
242 buf.append(flags);
243 buf.append(", font:");
244 buf.append(font);
245 buf.append(", frc:");
246 buf.append(frc);
247 buf.append(", cm:");
248 buf.append(cm);
249 buf.append("]");
250
251 return buf.toString();
252 }
253}