jdk/src/share/native/sun/font/layout/LigatureSubstProc.cpp
author prr
Thu, 07 Mar 2013 10:02:20 -0800
changeset 16891 91e99bed64ae
parent 16890 36b30720a997
permissions -rw-r--r--
8001031: Better font processing Reviewed-by: srl, vadim
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3935
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3935
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3935
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3935
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3935
diff changeset
    22
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 *
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
 *
16890
36b30720a997 8004986: Better handling of glyph table
prr
parents: 5506
diff changeset
    28
 * (C) Copyright IBM Corp. 1998-2013 - All Rights Reserved
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "LETypes.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "MorphTables.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include "StateTables.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include "MorphStateTables.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include "SubtableProcessor.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include "StateTableProcessor.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include "LigatureSubstProc.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#include "LEGlyphStorage.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#include "LESwaps.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
3935
afcdb712a9c5 6501644: sync LayoutEngine *code* structure to match ICU
srl
parents: 2
diff changeset
    42
U_NAMESPACE_BEGIN
afcdb712a9c5 6501644: sync LayoutEngine *code* structure to match ICU
srl
parents: 2
diff changeset
    43
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#define ExtendedComplement(m) ((le_int32) (~((le_uint32) (m))))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#define SignBit(m) ((ExtendedComplement(m) >> 1) & (le_int32)(m))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
#define SignExtend(v,m) (((v) & SignBit(m))? ((v) | ExtendedComplement(m)): (v))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
3935
afcdb712a9c5 6501644: sync LayoutEngine *code* structure to match ICU
srl
parents: 2
diff changeset
    48
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(LigatureSubstitutionProcessor)
afcdb712a9c5 6501644: sync LayoutEngine *code* structure to match ICU
srl
parents: 2
diff changeset
    49
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
    50
  LigatureSubstitutionProcessor::LigatureSubstitutionProcessor(const LEReferenceTo<MorphSubtableHeader> &morphSubtableHeader, LEErrorCode &success)
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
    51
: StateTableProcessor(morphSubtableHeader, success), ligatureSubstitutionHeader(morphSubtableHeader, success)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
{
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
    53
    if(LE_FAILURE(success)) return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    ligatureActionTableOffset = SWAPW(ligatureSubstitutionHeader->ligatureActionTableOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    componentTableOffset = SWAPW(ligatureSubstitutionHeader->componentTableOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    ligatureTableOffset = SWAPW(ligatureSubstitutionHeader->ligatureTableOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
    58
    entryTable = LEReferenceToArrayOf<LigatureSubstitutionStateEntry>(stHeader, success, entryTableOffset, LE_UNBOUNDED_ARRAY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
LigatureSubstitutionProcessor::~LigatureSubstitutionProcessor()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
void LigatureSubstitutionProcessor::beginStateTable()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    m = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
3935
afcdb712a9c5 6501644: sync LayoutEngine *code* structure to match ICU
srl
parents: 2
diff changeset
    70
ByteOffset LigatureSubstitutionProcessor::processStateEntry(LEGlyphStorage &glyphStorage, le_int32 &currGlyph, EntryTableIndex index)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
{
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
    72
  LEErrorCode success = LE_NO_ERROR;
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
    73
  const LigatureSubstitutionStateEntry *entry = entryTable.getAlias(index, success);
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
    74
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    ByteOffset newState = SWAPW(entry->newStateOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    le_int16 flags = SWAPW(entry->flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    if (flags & lsfSetComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (++m >= nComponents) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            m = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        componentStack[m] = currGlyph;
16890
36b30720a997 8004986: Better handling of glyph table
prr
parents: 5506
diff changeset
    84
    } else if ( m == -1) {
36b30720a997 8004986: Better handling of glyph table
prr
parents: 5506
diff changeset
    85
        // bad font- skip this glyph.
36b30720a997 8004986: Better handling of glyph table
prr
parents: 5506
diff changeset
    86
        currGlyph++;
36b30720a997 8004986: Better handling of glyph table
prr
parents: 5506
diff changeset
    87
        return newState;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    ByteOffset actionOffset = flags & lsfActionOffsetMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    if (actionOffset != 0) {
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
    93
      LEReferenceTo<LigatureActionEntry> ap(stHeader, success, actionOffset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        LigatureActionEntry action;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        le_int32 offset, i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        le_int32 stack[nComponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        le_int16 mm = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            le_uint32 componentGlyph = componentStack[m--];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
   102
            action = SWAPL(*ap.getAlias());
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
   103
            ap.addObject(success); // ap++
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            if (m < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                m = nComponents - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            offset = action & lafComponentOffsetMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            if (offset != 0) {
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
   111
              LEReferenceToArrayOf<le_int16> offsetTable(stHeader, success, 2 * SignExtend(offset, lafComponentOffsetMask), LE_UNBOUNDED_ARRAY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
   113
              if(LE_FAILURE(success)) {
16890
36b30720a997 8004986: Better handling of glyph table
prr
parents: 5506
diff changeset
   114
                  currGlyph++;
36b30720a997 8004986: Better handling of glyph table
prr
parents: 5506
diff changeset
   115
                  LE_DEBUG_BAD_FONT("off end of ligature substitution header");
36b30720a997 8004986: Better handling of glyph table
prr
parents: 5506
diff changeset
   116
                  return newState; // get out! bad font
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
   117
              }
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
   118
              if(componentGlyph > glyphStorage.getGlyphCount()) {
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
   119
                LE_DEBUG_BAD_FONT("preposterous componentGlyph");
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
   120
                currGlyph++;
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
   121
                return newState; // get out! bad font
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
   122
              }
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
   123
              i += SWAPW(offsetTable.getObject(LE_GET_GLYPH(glyphStorage[componentGlyph]), success));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                if (action & (lafLast | lafStore))  {
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
   126
                  LEReferenceTo<TTGlyphID> ligatureOffset(stHeader, success, i);
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
   127
                  TTGlyphID ligatureGlyph = SWAPW(*ligatureOffset.getAlias());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
   129
                  glyphStorage[componentGlyph] = LE_SET_GLYPH(glyphStorage[componentGlyph], ligatureGlyph);
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
   130
                  if(mm==nComponents) {
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
   131
                    LE_DEBUG_BAD_FONT("exceeded nComponents");
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
   132
                    mm--; // don't overrun the stack.
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
   133
                  }
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
   134
                  stack[++mm] = componentGlyph;
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
   135
                  i = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                } else {
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
   137
                  glyphStorage[componentGlyph] = LE_SET_GLYPH(glyphStorage[componentGlyph], 0xFFFF);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
16890
36b30720a997 8004986: Better handling of glyph table
prr
parents: 5506
diff changeset
   140
#if LE_ASSERT_BAD_FONT
36b30720a997 8004986: Better handling of glyph table
prr
parents: 5506
diff changeset
   141
            if(m<0) {
36b30720a997 8004986: Better handling of glyph table
prr
parents: 5506
diff changeset
   142
              LE_DEBUG_BAD_FONT("m<0")
36b30720a997 8004986: Better handling of glyph table
prr
parents: 5506
diff changeset
   143
            }
36b30720a997 8004986: Better handling of glyph table
prr
parents: 5506
diff changeset
   144
#endif
36b30720a997 8004986: Better handling of glyph table
prr
parents: 5506
diff changeset
   145
        } while (!(action & lafLast)  && (m>=0) ); // stop if last bit is set, or if run out of items
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        while (mm >= 0) {
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
   148
          if (++m >= nComponents) {
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
   149
            m = 0;
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
   150
          }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 16890
diff changeset
   152
          componentStack[m] = stack[mm--];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    if (!(flags & lsfDontAdvance)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // should handle reverse too!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        currGlyph += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    return newState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
void LigatureSubstitutionProcessor::endStateTable()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
}
3935
afcdb712a9c5 6501644: sync LayoutEngine *code* structure to match ICU
srl
parents: 2
diff changeset
   167
afcdb712a9c5 6501644: sync LayoutEngine *code* structure to match ICU
srl
parents: 2
diff changeset
   168
U_NAMESPACE_END