jdk/src/share/classes/sun/font/TextLabelFactory.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 21235 38393a2aaf88
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1998, 2003, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * (C) Copyright IBM Corp. 1998-2003 All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
package sun.font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.Font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.font.FontRenderContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.font.LineMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.text.Bidi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
   * A factory for text labels.  Basically this just holds onto the stuff that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
   * doesn't change-- the render context, context, and bidi info for the context-- and gets
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
   * called for each subrange you want to create.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
   * @see Font
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
   * @see FontRenderContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
   * @see GlyphVector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
   * @see TextLabel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
   * @see ExtendedTextLabel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
   * @see Bidi
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
   * @see TextLayout
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public class TextLabelFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
  private FontRenderContext frc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
  private char[] text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
  private Bidi bidi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
  private Bidi lineBidi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
  private int flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
  private int lineStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
  private int lineLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
   * Initialize a factory to produce glyph arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
   * @param frc the FontRenderContext to use for the arrays to be produced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
   * @param text the text of the paragraph.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
   * @param bidi the bidi information for the paragraph text, or null if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
   * entire text is left-to-right text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
  public TextLabelFactory(FontRenderContext frc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                          char[] text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                          Bidi bidi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                          int flags) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    this.frc = frc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    this.text = text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    this.bidi = bidi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    this.flags = flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    this.lineBidi = bidi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    this.lineStart = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    this.lineLimit = text.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
  public FontRenderContext getFontRenderContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    return frc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
  public char[] getText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    return text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
  public Bidi getParagraphBidi() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    return bidi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
  public Bidi getLineBidi() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    return lineBidi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
  public int getLayoutFlags() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    return flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
  public int getLineStart() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    return lineStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
  public int getLineLimit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    return lineLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
   * Set a line context for the factory.  Shaping only occurs on this line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
   * Characters are ordered as they would appear on this line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
   * @param lineStart the index within the text of the start of the line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
   * @param lineLimit the index within the text of the limit of the line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
  public void setLineContext(int lineStart, int lineLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    this.lineStart = lineStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    this.lineLimit = lineLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    if (bidi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
      lineBidi = bidi.createLineBidi(lineStart, lineLimit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
   * Create an extended glyph array for the text between start and limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
   * @param font the font to use to generate glyphs and character positions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
   * @param start the start of the subrange for which to create the glyph array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
   * @param limit the limit of the subrange for which to create glyph array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
   * Start and limit must be within the bounds of the current line.  If no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
   * line context has been set, the entire text is used as the current line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
   * The text between start and limit will be treated as though it all has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
   * the same bidi level (and thus the same directionality) as the character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
   * at start.  Clients should ensure that all text between start and limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
   * has the same bidi level for the current line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
  public ExtendedTextLabel createExtended(Font font,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                                          CoreMetrics lm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                          Decoration decorator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                                          int start,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                          int limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    if (start >= limit || start < lineStart || limit > lineLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
      throw new IllegalArgumentException("bad start: " + start + " or limit: " + limit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    int level = lineBidi == null ? 0 : lineBidi.getLevelAt(start - lineStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    int linedir = (lineBidi == null || lineBidi.baseIsLeftToRight()) ? 0 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    int layoutFlags = flags & ~0x9; // remove bidi, line direction flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    if ((level & 0x1) != 0) layoutFlags |= 1; // rtl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    if ((linedir & 0x1) != 0) layoutFlags |= 8; // line rtl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    TextSource source = new StandardTextSource(text, start, limit - start, lineStart, lineLimit - lineStart, level, layoutFlags, font, frc, lm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    return new ExtendedTextSourceLabel(source, decorator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
   * Create a simple glyph array for the text between start and limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
   * @param font the font to use to generate glyphs and character positions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
   * @param start the start of the subrange for which to create the glyph array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
   * @param limit the limit of the subrange for which to create glyph array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
  public TextLabel createSimple(Font font,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                CoreMetrics lm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                                int start,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                                int limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    if (start >= limit || start < lineStart || limit > lineLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
      throw new IllegalArgumentException("bad start: " + start + " or limit: " + limit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    int level = lineBidi == null ? 0 : lineBidi.getLevelAt(start - lineStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    int linedir = (lineBidi == null || lineBidi.baseIsLeftToRight()) ? 0 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    int layoutFlags = flags & ~0x9; // remove bidi, line direction flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    if ((level & 0x1) != 0) layoutFlags |= 1; // rtl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    if ((linedir & 0x1) != 0) layoutFlags |= 8; // line rtl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    TextSource source = new StandardTextSource(text, start, limit - start, lineStart, lineLimit - lineStart, level, layoutFlags, font, frc, lm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    return new TextSourceLabel(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
}