jdk/src/share/classes/sun/font/GlyphLayout.java
author sherman
Tue, 30 Aug 2011 11:53:11 -0700
changeset 10419 12c063b39232
parent 5506 202f599c92aa
child 18256 79d3f8278a38
permissions -rw-r--r--
7084245: Update usages of InternalError to use exception chaining Summary: to use new InternalError constructor with cause chainning Reviewed-by: alanb, ksrini, xuelei, neugens Contributed-by: sebastian.sickelmann@gmx.de
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: 3928
diff changeset
     2
 * Copyright (c) 2003, 2008, 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: 3928
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: 3928
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: 3928
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3928
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3928
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * (C) Copyright IBM Corp. 1999-2003 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * The original version of this source code and documentation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * copyrighted and owned by IBM. These materials are provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * under terms of a License Agreement between IBM and Sun.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * This technology is protected by multiple US and International
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * patents. This notice and attribution to IBM may not be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * GlyphLayout is used to process a run of text into a run of run of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * glyphs, optionally with position and char mapping info.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * The text has already been processed for numeric shaping and bidi.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * The run of text that layout works on has a single bidi level.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * also has a single font/style.  Some operations need context to work
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * on (shaping, script resolution) so context for the text run text is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * provided.  It is assumed that the text array contains sufficient
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * context, and the offset and count delimit the portion of the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * that needs to actually be processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * The font might be a composite font.  Layout generally requires
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * tables from a single physical font to operate, and so it must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * resolve the 'single' font run into runs of physical fonts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Some characters are supported by several fonts of a composite, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * in order to properly emulate the glyph substitution behavior of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * single physical font, these characters might need to be mapped to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * different physical fonts.  The script code that is assigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * characters normally considered 'common script' can be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * resolve which physical font to use for these characters. The input
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * to the char to glyph mapper (which assigns physical fonts as it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * processes the glyphs) should include the script code, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * mapper should operate on runs of a single script.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * To perform layout, call get() to get a new (or reuse an old)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * GlyphLayout, call layout on it, then call done(GlyphLayout) when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * finished.  There's no particular problem if you don't call done,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * but it assists in reuse of the GlyphLayout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
package sun.font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
import java.lang.ref.SoftReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
import java.awt.Font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
import java.awt.font.FontRenderContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
import java.awt.font.GlyphVector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
import java.awt.geom.AffineTransform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
import java.awt.geom.NoninvertibleTransformException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
import java.awt.geom.Point2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
import java.util.concurrent.ConcurrentHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
import static java.lang.Character.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
public final class GlyphLayout {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // data for glyph vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private GVData _gvdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    // cached glyph layout data for reuse
559
7f92d623aa8a 6611637: NullPointerException in sun.font.GlyphLayout$EngineRecord.init
prr
parents: 2
diff changeset
    88
    private static volatile GlyphLayout cache;  // reusable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private LayoutEngineFactory _lef;  // set when get is called, unset when done is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private TextRecord _textRecord;    // the text we're working on, used by iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private ScriptRun _scriptRuns;     // iterator over script runs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private FontRunIterator _fontRuns; // iterator over physical fonts in a composite
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private int _ercount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private ArrayList _erecords;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private Point2D.Float _pt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private FontStrikeDesc _sd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private float[] _mat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private int _typo_flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private int _offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public static final class LayoutEngineKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        private Font2D font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        private int script;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        private int lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        LayoutEngineKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        LayoutEngineKey(Font2D font, int script, int lang) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            init(font, script, lang);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        void init(Font2D font, int script, int lang) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            this.font = font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            this.script = script;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            this.lang = lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        LayoutEngineKey copy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            return new LayoutEngineKey(font, script, lang);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        Font2D font() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            return font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        int script() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            return script;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        int lang() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            return lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        public boolean equals(Object rhs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            if (this == rhs) return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            if (rhs == null) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                LayoutEngineKey that = (LayoutEngineKey)rhs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                return this.script == that.script &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                       this.lang == that.lang &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                       this.font.equals(that.font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            catch (ClassCastException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            return script ^ lang ^ font.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public static interface LayoutEngineFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
         * Given a font, script, and language, determine a layout engine to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        public LayoutEngine getEngine(Font2D font, int script, int lang);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
         * Given a key, determine a layout engine to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        public LayoutEngine getEngine(LayoutEngineKey key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public static interface LayoutEngine {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
         * Given a strike descriptor, text, rtl flag, and starting point, append information about
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
         * glyphs, positions, and character indices to the glyphvector data, and advance the point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
         * If the GVData does not have room for the glyphs, throws an IndexOutOfBoundsException and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
         * leave pt and the gvdata unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        public void layout(FontStrikeDesc sd, float[] mat, int gmask,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                           int baseIndex, TextRecord text, int typo_flags, Point2D.Float pt, GVData data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Return a new instance of GlyphLayout, using the provided layout engine factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * If null, the system layout engine factory will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public static GlyphLayout get(LayoutEngineFactory lef) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (lef == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            lef = SunLayoutEngine.instance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        GlyphLayout result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        synchronized(GlyphLayout.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            if (cache != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                result = cache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                cache = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            result = new GlyphLayout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        result._lef = lef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Return the old instance of GlyphLayout when you are done.  This enables reuse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * of GlyphLayout objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public static void done(GlyphLayout gl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        gl._lef = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        cache = gl; // object reference assignment is thread safe, it says here...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    private static final class SDCache {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        public Font key_font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        public FontRenderContext key_frc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        public AffineTransform dtx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        public AffineTransform invdtx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        public AffineTransform gtx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        public Point2D.Float delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        public FontStrikeDesc sd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        private SDCache(Font font, FontRenderContext frc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            key_font = font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            key_frc = frc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            // !!! add getVectorTransform and hasVectorTransform to frc?  then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            // we could just skip this work...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            dtx = frc.getTransform();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            dtx.setTransform(dtx.getScaleX(), dtx.getShearY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                             dtx.getShearX(), dtx.getScaleY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                             0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            if (!dtx.isIdentity()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    invdtx = dtx.createInverse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                catch (NoninvertibleTransformException e) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   236
                    throw new InternalError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            float ptSize = font.getSize2D();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            if (font.isTransformed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                gtx = font.getTransform();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                gtx.scale(ptSize, ptSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                delta = new Point2D.Float((float)gtx.getTranslateX(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                                          (float)gtx.getTranslateY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                gtx.setTransform(gtx.getScaleX(), gtx.getShearY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                                 gtx.getShearX(), gtx.getScaleY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                                 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                gtx.preConcatenate(dtx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                delta = ZERO_DELTA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                gtx = new AffineTransform(dtx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                gtx.scale(ptSize, ptSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            /* Similar logic to that used in SunGraphics2D.checkFontInfo().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
             * Whether a grey (AA) strike is needed is size dependent if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
             * AA mode is 'gasp'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            int aa =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                FontStrikeDesc.getAAHintIntVal(frc.getAntiAliasingHint(),
3928
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 2393
diff changeset
   262
                                               FontUtilities.getFont2D(font),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                                               (int)Math.abs(ptSize));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            int fm = FontStrikeDesc.getFMHintIntVal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                (frc.getFractionalMetricsHint());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            sd = new FontStrikeDesc(dtx, gtx, font.getStyle(), aa, fm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        private static final Point2D.Float ZERO_DELTA = new Point2D.Float();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        private static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            SoftReference<ConcurrentHashMap<SDKey, SDCache>> cacheRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        private static final class SDKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            private final Font font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            private final FontRenderContext frc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            private final int hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            SDKey(Font font, FontRenderContext frc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                this.font = font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                this.frc = frc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                this.hash = font.hashCode() ^ frc.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    SDKey rhs = (SDKey)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                        hash == rhs.hash &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                        font.equals(rhs.font) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                        frc.equals(rhs.frc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                catch (ClassCastException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        public static SDCache get(Font font, FontRenderContext frc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            // It is possible a translation component will be in the FRC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            // It doesn't affect us except adversely as we would consider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            // FRC's which are really the same to be different. If we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            // detect a translation component, then we need to exclude it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            // by creating a new transform which excludes the translation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            if (frc.isTransformed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                AffineTransform transform = frc.getTransform();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                if (transform.getTranslateX() != 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    transform.getTranslateY() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                    transform = new AffineTransform(transform.getScaleX(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                                                    transform.getShearY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                                                    transform.getShearX(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                                                    transform.getScaleY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                                                    0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    frc = new FontRenderContext(transform,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                                                frc.getAntiAliasingHint(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                                                frc.getFractionalMetricsHint()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                                                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            SDKey key = new SDKey(font, frc); // garbage, yuck...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            ConcurrentHashMap<SDKey, SDCache> cache = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            SDCache res = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            if (cacheRef != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                cache = cacheRef.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                if (cache != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    res = cache.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            if (res == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                res = new SDCache(font, frc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                if (cache == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    cache = new ConcurrentHashMap<SDKey, SDCache>(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    cacheRef = new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                       SoftReference<ConcurrentHashMap<SDKey, SDCache>>(cache);
2393
ea28f24e1708 6745225: Memory leak while drawing Attributed String
prr
parents: 715
diff changeset
   341
                } else if (cache.size() >= 512) {
ea28f24e1708 6745225: Memory leak while drawing Attributed String
prr
parents: 715
diff changeset
   342
                    cache.clear();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                cache.put(key, res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * Create a glyph vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @param font the font to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @param frc the font render context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @param text the text, including optional context before start and after start + count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @param offset the start of the text to lay out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @param count the length of the text to lay out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @param flags bidi and context flags {@see #java.awt.Font}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @param result a StandardGlyphVector to modify, can be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @return the layed out glyphvector, if result was passed in, it is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    public StandardGlyphVector layout(Font font, FontRenderContext frc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                                      char[] text, int offset, int count,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                                      int flags, StandardGlyphVector result)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        if (text == null || offset < 0 || count < 0 || (count > text.length - offset)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        init(count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        // need to set after init
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        // go through the back door for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if (font.hasLayoutAttributes()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            AttributeValues values = ((AttributeMap)font.getAttributes()).getValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            if (values.getKerning() != 0) _typo_flags |= 0x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            if (values.getLigatures() != 0) _typo_flags |= 0x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        _offset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        // use cache now - can we use the strike cache for this?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        SDCache txinfo = SDCache.get(font, frc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        _mat[0] = (float)txinfo.gtx.getScaleX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        _mat[1] = (float)txinfo.gtx.getShearY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        _mat[2] = (float)txinfo.gtx.getShearX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        _mat[3] = (float)txinfo.gtx.getScaleY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        _pt.setLocation(txinfo.delta);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        int lim = offset + count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        int min = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        int max = text.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        if (flags != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            if ((flags & Font.LAYOUT_RIGHT_TO_LEFT) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
              _typo_flags |= 0x80000000; // RTL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            if ((flags & Font.LAYOUT_NO_START_CONTEXT) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                min = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            if ((flags & Font.LAYOUT_NO_LIMIT_CONTEXT) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                max = lim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        int lang = -1; // default for now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
3928
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 2393
diff changeset
   410
        Font2D font2D = FontUtilities.getFont2D(font);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        _textRecord.init(text, offset, lim, min, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        int start = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        if (font2D instanceof CompositeFont) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            _scriptRuns.init(text, offset, count); // ??? how to handle 'common' chars
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            _fontRuns.init((CompositeFont)font2D, text, offset, lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            while (_scriptRuns.next()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                int limit = _scriptRuns.getScriptLimit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                int script = _scriptRuns.getScriptCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                while (_fontRuns.next(script, limit)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    Font2D pfont = _fontRuns.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    /* layout can't deal with NativeFont instances. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                     * native font is assumed to know of a suitable non-native
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                     * substitute font. This currently works because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                     * its consistent with the way NativeFonts delegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                     * in other cases too.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    if (pfont instanceof NativeFont) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                        pfont = ((NativeFont)pfont).getDelegateFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    int gmask = _fontRuns.getGlyphMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    int pos = _fontRuns.getPos();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    nextEngineRecord(start, pos, script, lang, pfont, gmask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    start = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            _scriptRuns.init(text, offset, count); // ??? don't worry about 'common' chars
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            while (_scriptRuns.next()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                int limit = _scriptRuns.getScriptLimit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                int script = _scriptRuns.getScriptCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                nextEngineRecord(start, limit, script, lang, font2D, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                start = limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        int ix = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        int stop = _ercount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        int dir = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        if (_typo_flags < 0) { // RTL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            ix = stop - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            stop = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            dir = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        //        _sd.init(dtx, gtx, font.getStyle(), frc.isAntiAliased(), frc.usesFractionalMetrics());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        _sd = txinfo.sd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        for (;ix != stop; ix += dir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            EngineRecord er = (EngineRecord)_erecords.get(ix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    er.layout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                catch (IndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    _gvdata.grow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        //        if (txinfo.invdtx != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        //            _gvdata.adjustPositions(txinfo.invdtx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        //        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        StandardGlyphVector gv = _gvdata.createGlyphVector(font, frc, result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        //        System.err.println("Layout returns: " + gv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        return gv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    // private methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    private GlyphLayout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        this._gvdata = new GVData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        this._textRecord = new TextRecord();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        this._scriptRuns = new ScriptRun();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        this._fontRuns = new FontRunIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        this._erecords = new ArrayList(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        this._pt = new Point2D.Float();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        this._sd = new FontStrikeDesc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        this._mat = new float[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    private void init(int capacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        this._typo_flags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        this._ercount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        this._gvdata.init(capacity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    private void nextEngineRecord(int start, int limit, int script, int lang, Font2D font, int gmask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        EngineRecord er = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        if (_ercount == _erecords.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            er = new EngineRecord();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            _erecords.add(er);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            er = (EngineRecord)_erecords.get(_ercount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        er.init(start, limit, font, script, lang, gmask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        ++_ercount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * Storage for layout to build glyph vector data, then generate a real GlyphVector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    public static final class GVData {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        public int _count; // number of glyphs, >= number of chars
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        public int _flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        public int[] _glyphs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        public float[] _positions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        public int[] _indices;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        private static final int UNINITIALIZED_FLAGS = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        public void init(int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            _count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            _flags = UNINITIALIZED_FLAGS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            if (_glyphs == null || _glyphs.length < size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                if (size < 20) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                    size = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                _glyphs = new int[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                _positions = new float[size * 2 + 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                _indices = new int[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        public void grow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            grow(_glyphs.length / 4); // always grows because min length is 20
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        public void grow(int delta) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            int size = _glyphs.length + delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            int[] nglyphs = new int[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            System.arraycopy(_glyphs, 0, nglyphs, 0, _count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            _glyphs = nglyphs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            float[] npositions = new float[size * 2 + 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            System.arraycopy(_positions, 0, npositions, 0, _count * 2 + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            _positions = npositions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            int[] nindices = new int[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            System.arraycopy(_indices, 0, nindices, 0, _count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            _indices = nindices;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        public void adjustPositions(AffineTransform invdtx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            invdtx.transform(_positions, 0, _positions, 0, _count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        public StandardGlyphVector createGlyphVector(Font font, FontRenderContext frc, StandardGlyphVector result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            // !!! default initialization until we let layout engines do it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            if (_flags == UNINITIALIZED_FLAGS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                _flags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                if (_count > 1) { // if only 1 glyph assume LTR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                    boolean ltr = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                    boolean rtl = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                    int rtlix = _count; // rtl index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                    for (int i = 0; i < _count && (ltr || rtl); ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                        int cx = _indices[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                        ltr = ltr && (cx == i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                        rtl = rtl && (cx == --rtlix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                    if (rtl) _flags |= GlyphVector.FLAG_RUN_RTL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                    if (!rtl && !ltr) _flags |= GlyphVector.FLAG_COMPLEX_GLYPHS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                // !!! layout engines need to tell us whether they performed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                // position adjustments. currently they don't tell us, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                // we must assume they did
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                _flags |= GlyphVector.FLAG_HAS_POSITION_ADJUSTMENTS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            int[] glyphs = new int[_count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            System.arraycopy(_glyphs, 0, glyphs, 0, _count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            float[] positions = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            if ((_flags & GlyphVector.FLAG_HAS_POSITION_ADJUSTMENTS) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                positions = new float[_count * 2 + 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                System.arraycopy(_positions, 0, positions, 0, positions.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            int[] indices = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            if ((_flags & GlyphVector.FLAG_COMPLEX_GLYPHS) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                indices = new int[_count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                System.arraycopy(_indices, 0, indices, 0, _count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                result = new StandardGlyphVector(font, frc, glyphs, positions, indices, _flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                result.initGlyphVector(font, frc, glyphs, positions, indices, _flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * Utility class to keep track of script runs, which may have to be reordered rtl when we're
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * finished.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    private final class EngineRecord {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        private int start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        private int limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        private int gmask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        private int eflags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        private LayoutEngineKey key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        private LayoutEngine engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        EngineRecord() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            key = new LayoutEngineKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        void init(int start, int limit, Font2D font, int script, int lang, int gmask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            this.start = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            this.limit = limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            this.gmask = gmask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            this.key.init(font, script, lang);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            this.eflags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            // only request canonical substitution if we have combining marks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            for (int i = start; i < limit; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                int ch = _textRecord.text[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                if (isHighSurrogate((char)ch) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                    i < limit - 1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                    isLowSurrogate(_textRecord.text[i+1])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                    // rare case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                    ch = toCodePoint((char)ch,_textRecord.text[++i]); // inc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                int gc = getType(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                if (gc == NON_SPACING_MARK ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    gc == ENCLOSING_MARK ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    gc == COMBINING_SPACING_MARK) { // could do range test also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                    this.eflags = 0x4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            this.engine = _lef.getEngine(key); // flags?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        void layout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            _textRecord.start = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            _textRecord.limit = limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            engine.layout(_sd, _mat, gmask, start - _offset, _textRecord,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                          _typo_flags | eflags, _pt, _gvdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
}