StandardTextSource.java revision 0
0N/A/*
0N/A * Copyright 1998-2003 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation. Sun designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Sun in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A */
0N/A/*
0N/A *
0N/A * (C) Copyright IBM Corp. 1998-2003 - All Rights Reserved
0N/A */
0N/A
0N/Apackage sun.font;
0N/A
0N/Aimport java.awt.Font;
0N/Aimport java.awt.font.FontRenderContext;
0N/Aimport java.awt.font.LineMetrics;
0N/A
0N/Apublic class StandardTextSource extends TextSource {
0N/A char[] chars;
0N/A int start;
0N/A int len;
0N/A int cstart;
0N/A int clen;
0N/A int level; // assumed all uniform
0N/A int flags; // see GlyphVector.java
0N/A Font font;
0N/A FontRenderContext frc;
0N/A CoreMetrics cm;
0N/A
0N/A /**
0N/A * Create a simple implementation of a TextSource.
0N/A *
0N/A * Chars is an array containing clen chars in the context, in
0N/A * logical order, contiguously starting at cstart. Start and len
0N/A * represent that portion of the context representing the true
0N/A * source; start, like cstart, is relative to the start of the
0N/A * character array.
0N/A *
0N/A * Level is the bidi level (0-63 for the entire context. Flags is
0N/A * the layout flags. Font is the font, frc is the render context,
0N/A * and lm is the line metrics for the entire source text, but not
0N/A * necessarily the context.
0N/A */
0N/A public StandardTextSource(char[] chars,
0N/A int start,
0N/A int len,
0N/A int cstart,
0N/A int clen,
0N/A int level,
0N/A int flags,
0N/A Font font,
0N/A FontRenderContext frc,
0N/A CoreMetrics cm) {
0N/A if (chars == null) {
0N/A throw new IllegalArgumentException("bad chars: null");
0N/A }
0N/A if (cstart < 0) {
0N/A throw new IllegalArgumentException("bad cstart: " + cstart);
0N/A }
0N/A if (start < cstart) {
0N/A throw new IllegalArgumentException("bad start: " + start + " for cstart: " + cstart);
0N/A }
0N/A if (clen < 0) {
0N/A throw new IllegalArgumentException("bad clen: " + clen);
0N/A }
0N/A if (cstart + clen > chars.length) {
0N/A throw new IllegalArgumentException("bad clen: " + clen + " cstart: " + cstart + " for array len: " + chars.length);
0N/A }
0N/A if (len < 0) {
0N/A throw new IllegalArgumentException("bad len: " + len);
0N/A }
0N/A if ((start + len) > (cstart + clen)) {
0N/A throw new IllegalArgumentException("bad len: " + len + " start: " + start + " for cstart: " + cstart + " clen: " + clen);
0N/A }
0N/A if (font == null) {
0N/A throw new IllegalArgumentException("bad font: null");
0N/A }
0N/A if (frc == null) {
0N/A throw new IllegalArgumentException("bad frc: null");
0N/A }
0N/A
0N/A this.chars = chars;
0N/A this.start = start;
0N/A this.len = len;
0N/A this.cstart = cstart;
0N/A this.clen = clen;
0N/A this.level = level;
0N/A this.flags = flags;
0N/A this.font = font;
0N/A this.frc = frc;
0N/A
0N/A if (cm != null) {
0N/A this.cm = cm;
0N/A } else {
0N/A LineMetrics metrics = font.getLineMetrics(chars, cstart, clen, frc);
0N/A this.cm = ((FontLineMetrics)metrics).cm;
0N/A }
0N/A }
0N/A
0N/A /** Create a StandardTextSource whose context is coextensive with the source. */
0N/A public StandardTextSource(char[] chars,
0N/A int start,
0N/A int len,
0N/A int level,
0N/A int flags,
0N/A Font font,
0N/A FontRenderContext frc,
0N/A CoreMetrics cm) {
0N/A this(chars, start, len, start, len, level, flags, font, frc, cm);
0N/A }
0N/A
0N/A /** Create a StandardTextSource whose context and source are coextensive with the entire char array. */
0N/A public StandardTextSource(char[] chars,
0N/A int level,
0N/A int flags,
0N/A Font font,
0N/A FontRenderContext frc) {
0N/A this(chars, 0, chars.length, 0, chars.length, level, flags, font, frc, null);
0N/A }
0N/A
0N/A /** Create a StandardTextSource whose context and source are all the text in the String. */
0N/A public StandardTextSource(String str,
0N/A int level,
0N/A int flags,
0N/A Font font,
0N/A FontRenderContext frc) {
0N/A this(str.toCharArray(), 0, str.length(), 0, str.length(), level, flags, font, frc, null);
0N/A }
0N/A
0N/A // TextSource API
0N/A
0N/A public char[] getChars() {
0N/A return chars;
0N/A }
0N/A
0N/A public int getStart() {
0N/A return start;
0N/A }
0N/A
0N/A public int getLength() {
0N/A return len;
0N/A }
0N/A
0N/A public int getContextStart() {
0N/A return cstart;
0N/A }
0N/A
0N/A public int getContextLength() {
0N/A return clen;
0N/A }
0N/A
0N/A public int getLayoutFlags() {
0N/A return flags;
0N/A }
0N/A
0N/A public int getBidiLevel() {
0N/A return level;
0N/A }
0N/A
0N/A public Font getFont() {
0N/A return font;
0N/A }
0N/A
0N/A public FontRenderContext getFRC() {
0N/A return frc;
0N/A }
0N/A
0N/A public CoreMetrics getCoreMetrics() {
0N/A return cm;
0N/A }
0N/A
0N/A public TextSource getSubSource(int start, int length, int dir) {
0N/A if (start < 0 || length < 0 || (start + length) > len) {
0N/A throw new IllegalArgumentException("bad start (" + start + ") or length (" + length + ")");
0N/A }
0N/A
0N/A int level = this.level;
0N/A if (dir != TextLineComponent.UNCHANGED) {
0N/A boolean ltr = (flags & 0x8) == 0;
0N/A if (!(dir == TextLineComponent.LEFT_TO_RIGHT && ltr) &&
0N/A !(dir == TextLineComponent.RIGHT_TO_LEFT && !ltr)) {
0N/A throw new IllegalArgumentException("direction flag is invalid");
0N/A }
0N/A level = ltr? 0 : 1;
0N/A }
0N/A
0N/A return new StandardTextSource(chars, this.start + start, length, cstart, clen, level, flags, font, frc, cm);
0N/A }
0N/A
0N/A public String toString() {
0N/A return toString(WITH_CONTEXT);
0N/A }
0N/A
0N/A public String toString(boolean withContext) {
0N/A StringBuffer buf = new StringBuffer(super.toString());
0N/A buf.append("[start:");
0N/A buf.append(start);
0N/A buf.append(", len:" );
0N/A buf.append(len);
0N/A buf.append(", cstart:");
0N/A buf.append(cstart);
0N/A buf.append(", clen:" );
0N/A buf.append(clen);
0N/A buf.append(", chars:\"");
0N/A int chStart, chLimit;
0N/A if (withContext == WITH_CONTEXT) {
0N/A chStart = cstart;
0N/A chLimit = cstart + clen;
0N/A }
0N/A else {
0N/A chStart = start;
0N/A chLimit = start + len;
0N/A }
0N/A for (int i = chStart; i < chLimit; ++i) {
0N/A if (i > chStart) {
0N/A buf.append(" ");
0N/A }
0N/A buf.append(Integer.toHexString(chars[i]));
0N/A }
0N/A buf.append("\"");
0N/A buf.append(", level:");
0N/A buf.append(level);
0N/A buf.append(", flags:");
0N/A buf.append(flags);
0N/A buf.append(", font:");
0N/A buf.append(font);
0N/A buf.append(", frc:");
0N/A buf.append(frc);
0N/A buf.append(", cm:");
0N/A buf.append(cm);
0N/A buf.append("]");
0N/A
0N/A return buf.toString();
0N/A }
0N/A}