jdk/src/share/classes/sun/font/TextLabelFactory.java
author vadim
Wed, 23 Oct 2013 08:56:56 +0400
changeset 21235 38393a2aaf88
parent 5506 202f599c92aa
child 23010 6dadb192ad81
permissions -rw-r--r--
8001173: [findbugs] Evaluate FindBug output for sun.font.CompositeFont, sun.font.CompositeFontDescriptor Reviewed-by: prr, bae
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.text.Bidi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
   * A factory for text labels.  Basically this just holds onto the stuff that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
   * doesn't change-- the render context, context, and bidi info for the context-- and gets
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
   * called for each subrange you want to create.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
   * @see Font
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
   * @see FontRenderContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
   * @see GlyphVector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
   * @see TextLabel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
   * @see ExtendedTextLabel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
   * @see Bidi
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
   * @see TextLayout
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public class TextLabelFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
  private FontRenderContext frc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
  private char[] text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
  private Bidi bidi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
  private Bidi lineBidi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
  private int flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
  private int lineStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
  private int lineLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
   * Initialize a factory to produce glyph arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
   * @param frc the FontRenderContext to use for the arrays to be produced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
   * @param text the text of the paragraph.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
   * @param bidi the bidi information for the paragraph text, or null if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
   * entire text is left-to-right text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
  public TextLabelFactory(FontRenderContext frc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                          char[] text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                          Bidi bidi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                          int flags) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    this.frc = frc;
21235
38393a2aaf88 8001173: [findbugs] Evaluate FindBug output for sun.font.CompositeFont, sun.font.CompositeFontDescriptor
vadim
parents: 5506
diff changeset
    72
    this.text = text.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    this.bidi = bidi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    this.flags = flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    this.lineBidi = bidi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    this.lineStart = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    this.lineLimit = text.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
  public FontRenderContext getFontRenderContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    return frc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
  public Bidi getLineBidi() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    return lineBidi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
   * Set a line context for the factory.  Shaping only occurs on this line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
   * Characters are ordered as they would appear on this line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
   * @param lineStart the index within the text of the start of the line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
   * @param lineLimit the index within the text of the limit of the line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
  public void setLineContext(int lineStart, int lineLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    this.lineStart = lineStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    this.lineLimit = lineLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    if (bidi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
      lineBidi = bidi.createLineBidi(lineStart, lineLimit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
   * Create an extended glyph array for the text between start and limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
   * @param font the font to use to generate glyphs and character positions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
   * @param start the start of the subrange for which to create the glyph array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
   * @param limit the limit of the subrange for which to create glyph array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
   * Start and limit must be within the bounds of the current line.  If no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
   * line context has been set, the entire text is used as the current line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
   * The text between start and limit will be treated as though it all has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
   * the same bidi level (and thus the same directionality) as the character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
   * at start.  Clients should ensure that all text between start and limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
   * has the same bidi level for the current line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
  public ExtendedTextLabel createExtended(Font font,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                          CoreMetrics lm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                          Decoration decorator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                          int start,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                                          int limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    if (start >= limit || start < lineStart || limit > lineLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
      throw new IllegalArgumentException("bad start: " + start + " or limit: " + limit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    int level = lineBidi == null ? 0 : lineBidi.getLevelAt(start - lineStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    int linedir = (lineBidi == null || lineBidi.baseIsLeftToRight()) ? 0 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    int layoutFlags = flags & ~0x9; // remove bidi, line direction flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    if ((level & 0x1) != 0) layoutFlags |= 1; // rtl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    if ((linedir & 0x1) != 0) layoutFlags |= 8; // line rtl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    TextSource source = new StandardTextSource(text, start, limit - start, lineStart, lineLimit - lineStart, level, layoutFlags, font, frc, lm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    return new ExtendedTextSourceLabel(source, decorator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
   * Create a simple glyph array for the text between start and limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
   * @param font the font to use to generate glyphs and character positions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
   * @param start the start of the subrange for which to create the glyph array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
   * @param limit the limit of the subrange for which to create glyph array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
  public TextLabel createSimple(Font font,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                                CoreMetrics lm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                int start,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                                int limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    if (start >= limit || start < lineStart || limit > lineLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
      throw new IllegalArgumentException("bad start: " + start + " or limit: " + limit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    int level = lineBidi == null ? 0 : lineBidi.getLevelAt(start - lineStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    int linedir = (lineBidi == null || lineBidi.baseIsLeftToRight()) ? 0 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    int layoutFlags = flags & ~0x9; // remove bidi, line direction flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    if ((level & 0x1) != 0) layoutFlags |= 1; // rtl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    if ((linedir & 0x1) != 0) layoutFlags |= 8; // line rtl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    TextSource source = new StandardTextSource(text, start, limit - start, lineStart, lineLimit - lineStart, level, layoutFlags, font, frc, lm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    return new TextSourceLabel(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
}