1 /*
   2  * Copyright (c) 1998, 2013, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 /*
  26  *
  27  * (C) Copyright IBM Corp. 1998-2003 All Rights Reserved
  28  */
  29 
  30 package sun.font;
  31 
  32 import java.awt.Font;
  33 
  34 import java.awt.font.FontRenderContext;
  35 import java.text.Bidi;
  36 
  37   /**
  38    * A factory for text labels.  Basically this just holds onto the stuff that
  39    * doesn't change-- the render context, context, and bidi info for the context-- and gets
  40    * called for each subrange you want to create.
  41    *
  42    * @see Font
  43    * @see FontRenderContext
  44    * @see java.awt.font.GlyphVector
  45    * @see TextLabel
  46    * @see ExtendedTextLabel
  47    * @see Bidi
  48    * @see java.awt.font.TextLayout
  49    */
  50 
  51 public class TextLabelFactory {
  52   private FontRenderContext frc;
  53   private char[] text;
  54   private Bidi bidi;
  55   private Bidi lineBidi;
  56   private int flags;
  57   private int lineStart;
  58   private int lineLimit;
  59 
  60   /**
  61    * Initialize a factory to produce glyph arrays.
  62    * @param frc the FontRenderContext to use for the arrays to be produced.
  63    * @param text the text of the paragraph.
  64    * @param bidi the bidi information for the paragraph text, or null if the
  65    * entire text is left-to-right text.
  66    */
  67   public TextLabelFactory(FontRenderContext frc,
  68                           char[] text,
  69                           Bidi bidi,
  70                           int flags) {
  71     this.frc = frc;
  72     this.text = text.clone();
  73     this.bidi = bidi;
  74     this.flags = flags;
  75     this.lineBidi = bidi;
  76     this.lineStart = 0;
  77     this.lineLimit = text.length;
  78   }
  79 
  80   public FontRenderContext getFontRenderContext() {
  81     return frc;
  82   }
  83 
  84   public Bidi getLineBidi() {
  85     return lineBidi;
  86   }
  87 
  88   /**
  89    * Set a line context for the factory.  Shaping only occurs on this line.
  90    * Characters are ordered as they would appear on this line.
  91    * @param lineStart the index within the text of the start of the line.
  92    * @param lineLimit the index within the text of the limit of the line.
  93    */
  94   public void setLineContext(int lineStart, int lineLimit) {
  95     this.lineStart = lineStart;
  96     this.lineLimit = lineLimit;
  97     if (bidi != null) {
  98       lineBidi = bidi.createLineBidi(lineStart, lineLimit);
  99     }
 100   }
 101 
 102   /**
 103    * Create an extended glyph array for the text between start and limit.
 104    *
 105    * @param font the font to use to generate glyphs and character positions.
 106    * @param start the start of the subrange for which to create the glyph array
 107    * @param limit the limit of the subrange for which to create glyph array
 108    *
 109    * Start and limit must be within the bounds of the current line.  If no
 110    * line context has been set, the entire text is used as the current line.
 111    * The text between start and limit will be treated as though it all has
 112    * the same bidi level (and thus the same directionality) as the character
 113    * at start.  Clients should ensure that all text between start and limit
 114    * has the same bidi level for the current line.
 115    */
 116   public ExtendedTextLabel createExtended(Font font,
 117                                           CoreMetrics lm,
 118                                           Decoration decorator,
 119                                           int start,
 120                                           int limit) {
 121 
 122     if (start >= limit || start < lineStart || limit > lineLimit) {
 123       throw new IllegalArgumentException("bad start: " + start + " or limit: " + limit);
 124     }
 125 
 126     int level = lineBidi == null ? 0 : lineBidi.getLevelAt(start - lineStart);
 127     int linedir = (lineBidi == null || lineBidi.baseIsLeftToRight()) ? 0 : 1;
 128     int layoutFlags = flags & ~0x9; // remove bidi, line direction flags
 129     if ((level & 0x1) != 0) layoutFlags |= 1; // rtl
 130     if ((linedir & 0x1) != 0) layoutFlags |= 8; // line rtl
 131 
 132     TextSource source = new StandardTextSource(text, start, limit - start, lineStart, lineLimit - lineStart, level, layoutFlags, font, frc, lm);
 133     return new ExtendedTextSourceLabel(source, decorator);
 134   }
 135 
 136   /**
 137    * Create a simple glyph array for the text between start and limit.
 138    *
 139    * @param font the font to use to generate glyphs and character positions.
 140    * @param start the start of the subrange for which to create the glyph array
 141    * @param limit the limit of the subrange for which to create glyph array
 142    */
 143   public TextLabel createSimple(Font font,
 144                                 CoreMetrics lm,
 145                                 int start,
 146                                 int limit) {
 147 
 148     if (start >= limit || start < lineStart || limit > lineLimit) {
 149       throw new IllegalArgumentException("bad start: " + start + " or limit: " + limit);
 150     }
 151 
 152     int level = lineBidi == null ? 0 : lineBidi.getLevelAt(start - lineStart);
 153     int linedir = (lineBidi == null || lineBidi.baseIsLeftToRight()) ? 0 : 1;
 154     int layoutFlags = flags & ~0x9; // remove bidi, line direction flags
 155     if ((level & 0x1) != 0) layoutFlags |= 1; // rtl
 156     if ((linedir & 0x1) != 0) layoutFlags |= 8; // line rtl
 157     TextSource source = new StandardTextSource(text, start, limit - start, lineStart, lineLimit - lineStart, level, layoutFlags, font, frc, lm);
 158     return new TextSourceLabel(source);
 159   }
 160 }