jdk/src/share/native/sun/font/layout/KernTable.cpp
author prr
Thu, 07 Mar 2013 10:02:20 -0800
changeset 16891 91e99bed64ae
parent 8742 849c6970689b
child 18118 aa253c91de2e
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
 *
3935
afcdb712a9c5 6501644: sync LayoutEngine *code* structure to match ICU
srl
parents: 2
diff changeset
    28
 *
7486
6a36b1ebc620 6886358: layout code update
srl
parents: 5506
diff changeset
    29
 * (C) Copyright IBM Corp. 2004-2010 - All Rights Reserved
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "KernTable.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include "LEFontInstance.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include "LEGlyphStorage.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include "LESwaps.h"
7486
6a36b1ebc620 6886358: layout code update
srl
parents: 5506
diff changeset
    38
#include "OpenTypeUtilities.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#define DEBUG 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
3935
afcdb712a9c5 6501644: sync LayoutEngine *code* structure to match ICU
srl
parents: 2
diff changeset
    44
U_NAMESPACE_BEGIN
afcdb712a9c5 6501644: sync LayoutEngine *code* structure to match ICU
srl
parents: 2
diff changeset
    45
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
struct PairInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
  le_uint32 key;   // sigh, MSVC compiler gags on union here
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
  le_int16  value; // fword, kern value in funits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
#define KERN_PAIRINFO_SIZE 6
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
    51
LE_CORRECT_SIZE(PairInfo, KERN_PAIRINFO_SIZE)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
struct Subtable_0 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
  le_uint16 nPairs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
  le_uint16 searchRange;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
  le_uint16 entrySelector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
  le_uint16 rangeShift;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
#define KERN_SUBTABLE_0_HEADER_SIZE 8
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
    59
LE_CORRECT_SIZE(Subtable_0, KERN_SUBTABLE_0_HEADER_SIZE)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
// Kern table version 0 only
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
struct SubtableHeader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
  le_uint16 version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
  le_uint16 length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
  le_uint16 coverage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
#define KERN_SUBTABLE_HEADER_SIZE 6
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
    68
LE_CORRECT_SIZE(SubtableHeader, KERN_SUBTABLE_HEADER_SIZE)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
// Version 0 only, version 1 has different layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
struct KernTableHeader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
  le_uint16 version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
  le_uint16 nTables;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
#define KERN_TABLE_HEADER_SIZE 4
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
    76
LE_CORRECT_SIZE(KernTableHeader, KERN_TABLE_HEADER_SIZE)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
#define COVERAGE_HORIZONTAL 0x1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
#define COVERAGE_MINIMUM 0x2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
#define COVERAGE_CROSS 0x4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
#define COVERAGE_OVERRIDE 0x8
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * This implementation has support for only one subtable, so if the font has
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * multiple subtables, only the first will be used.  If this turns out to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * be a problem in practice we should add it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * This also supports only version 0 of the kern table header, only
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * Apple supports the latter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * This implementation isn't careful about the kern table flags, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * might invoke kerning when it is not supposed to.  That too I'm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * leaving for a bug fix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * TODO: support multiple subtables
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * TODO: respect header flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 */
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
    98
KernTable::KernTable(const LETableReference& base, LEErrorCode &success)
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
    99
  : pairs(), pairsSwapped(NULL), fTable(base)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
{
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   101
  if(LE_FAILURE(success) || (fTable.isEmpty())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
#if DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    fprintf(stderr, "no kern data\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
  }
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   107
  LEReferenceTo<KernTableHeader> header(fTable, success);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
#if DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
  // dump first 32 bytes of header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
  for (int i = 0; i < 64; ++i) {
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   112
    fprintf(stderr, "%0.2x ", ((const char*)header.getAlias())[i]&0xff);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    if (((i+1)&0xf) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
      fprintf(stderr, "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    } else if (((i+1)&0x7) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
      fprintf(stderr, "  ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   121
  if(LE_FAILURE(success)) return;
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   122
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   123
  if (!header.isEmpty() && header->version == 0 && SWAPW(header->nTables) > 0) {
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   124
    LEReferenceTo<SubtableHeader> subhead(header, success, KERN_TABLE_HEADER_SIZE);
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   125
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   126
    if (LE_SUCCESS(success) && !subhead.isEmpty() && subhead->version == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
      coverage = SWAPW(subhead->coverage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
      if (coverage & COVERAGE_HORIZONTAL) { // only handle horizontal kerning
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   129
        LEReferenceTo<Subtable_0> table(subhead, success, KERN_SUBTABLE_HEADER_SIZE);
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   130
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   131
        if(table.isEmpty() || LE_FAILURE(success)) return;
7486
6a36b1ebc620 6886358: layout code update
srl
parents: 5506
diff changeset
   132
6a36b1ebc620 6886358: layout code update
srl
parents: 5506
diff changeset
   133
        nPairs        = SWAPW(table->nPairs);
6a36b1ebc620 6886358: layout code update
srl
parents: 5506
diff changeset
   134
6a36b1ebc620 6886358: layout code update
srl
parents: 5506
diff changeset
   135
#if 0   // some old fonts have bad values here...
6a36b1ebc620 6886358: layout code update
srl
parents: 5506
diff changeset
   136
        searchRange   = SWAPW(table->searchRange);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        entrySelector = SWAPW(table->entrySelector);
7486
6a36b1ebc620 6886358: layout code update
srl
parents: 5506
diff changeset
   138
        rangeShift    = SWAPW(table->rangeShift);
6a36b1ebc620 6886358: layout code update
srl
parents: 5506
diff changeset
   139
#else
6a36b1ebc620 6886358: layout code update
srl
parents: 5506
diff changeset
   140
        entrySelector = OpenTypeUtilities::highBit(nPairs);
6a36b1ebc620 6886358: layout code update
srl
parents: 5506
diff changeset
   141
        searchRange   = (1 << entrySelector) * KERN_PAIRINFO_SIZE;
6a36b1ebc620 6886358: layout code update
srl
parents: 5506
diff changeset
   142
        rangeShift    = (nPairs * KERN_PAIRINFO_SIZE) - searchRange;
6a36b1ebc620 6886358: layout code update
srl
parents: 5506
diff changeset
   143
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   145
        if(LE_SUCCESS(success) && nPairs>0) {
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   146
          // pairs is an instance member, and table is on the stack.
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   147
          // set 'pairs' based on table.getAlias(). This will range check it.
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   148
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   149
          pairs = LEReferenceToArrayOf<PairInfo>(fTable, // based on overall table
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   150
                                                 success,
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   151
                                                 (const PairInfo*)table.getAlias(),  // subtable 0 + ..
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   152
                                                 KERN_SUBTABLE_0_HEADER_SIZE,  // .. offset of header size
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   153
                                                 nPairs); // count
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   154
        }
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   155
        if (LE_SUCCESS(success) && pairs.isValid()) {
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   156
            pairsSwapped =  (PairInfo*)(malloc(nPairs*sizeof(PairInfo)));
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   157
            PairInfo *p = (PairInfo*)pairsSwapped;
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   158
            for (int i = 0; LE_SUCCESS(success) && i < nPairs; i++, p++) {
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   159
              memcpy(p, pairs.getAlias(i,success), KERN_PAIRINFO_SIZE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
              p->key = SWAPL(p->key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            }
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   162
            fTable.getFont()->setKernPairs((void*)pairsSwapped); // store it
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   165
#if 0
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   166
        fprintf(stderr, "coverage: %0.4x nPairs: %d pairs %p\n", coverage, nPairs, pairs.getAlias());
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   167
        fprintf(stderr, "  searchRange: %d entrySelector: %d rangeShift: %d\n", searchRange, entrySelector, rangeShift);
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   168
        fprintf(stderr, "[[ ignored font table entries: range %d selector %d shift %d ]]\n", SWAPW(table->searchRange), SWAPW(table->entrySelector), SWAPW(table->rangeShift));
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   169
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
#if DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        fprintf(stderr, "coverage: %0.4x nPairs: %d pairs 0x%x\n", coverage, nPairs, pairs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        fprintf(stderr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
          "  searchRange(pairs): %d entrySelector: %d rangeShift(pairs): %d\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
          searchRange, entrySelector, rangeShift);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
          // dump part of the pair list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
          char ids[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
          for (int i = 256; --i >= 0;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            LEGlyphID id = font->mapCharToGlyph(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            if (id < 256) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
              ids[id] = (char)i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
          PairInfo *p = pairs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
          for (int i = 0; i < nPairs; ++i, p++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            le_uint32 k = p->key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            le_uint16 left = (k >> 16) & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            le_uint16 right = k & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            if (left < 256 && right < 256) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
              char c = ids[left];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
              if (c > 0x20 && c < 0x7f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                fprintf(stderr, "%c/", c & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
              } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                fprintf(stderr, "%0.2x/", c & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
              c = ids[right];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
              if (c > 0x20 && c < 0x7f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                fprintf(stderr, "%c ", c & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
              } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                fprintf(stderr, "%0.2x ", c & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 * Process the glyph positions.  The positions array has two floats for each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 * glyph, plus a trailing pair to mark the end of the last glyph.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 */
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   217
void KernTable::process(LEGlyphStorage& storage, LEErrorCode &success)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
{
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   219
  if(LE_FAILURE(success)) return;
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   220
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   221
  if (pairsSwapped) {
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   222
    success = LE_NO_ERROR;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    le_uint32 key = storage[0]; // no need to mask off high bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    float adjust = 0;
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   226
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   227
    for (int i = 1, e = storage.getGlyphCount(); LE_SUCCESS(success)&&  i < e; ++i) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
      key = key << 16 | (storage[i] & 0xffff);
3935
afcdb712a9c5 6501644: sync LayoutEngine *code* structure to match ICU
srl
parents: 2
diff changeset
   229
afcdb712a9c5 6501644: sync LayoutEngine *code* structure to match ICU
srl
parents: 2
diff changeset
   230
      // argh, to do a binary search, we need to have the pair list in sorted order
afcdb712a9c5 6501644: sync LayoutEngine *code* structure to match ICU
srl
parents: 2
diff changeset
   231
      // but it is not in sorted order on win32 platforms because of the endianness difference
afcdb712a9c5 6501644: sync LayoutEngine *code* structure to match ICU
srl
parents: 2
diff changeset
   232
      // so either I have to swap the element each time I examine it, or I have to swap
afcdb712a9c5 6501644: sync LayoutEngine *code* structure to match ICU
srl
parents: 2
diff changeset
   233
      // all the elements ahead of time and store them in the font
afcdb712a9c5 6501644: sync LayoutEngine *code* structure to match ICU
srl
parents: 2
diff changeset
   234
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   235
      const PairInfo* p = pairsSwapped;
8742
849c6970689b 7017324: Kerning crash in JDK 7 since ICU layout update
srl
parents: 7486
diff changeset
   236
      const PairInfo* tp = (const PairInfo*)(p + (rangeShift/KERN_PAIRINFO_SIZE)); /* rangeshift is in original table bytes */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
      if (key > tp->key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        p = tp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
#if DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
      fprintf(stderr, "binary search for %0.8x\n", key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
      le_uint32 probe = searchRange;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
      while (probe > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        probe >>= 1;
8742
849c6970689b 7017324: Kerning crash in JDK 7 since ICU layout update
srl
parents: 7486
diff changeset
   248
        tp = (const PairInfo*)(p + (probe/KERN_PAIRINFO_SIZE));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        le_uint32 tkey = tp->key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
#if DEBUG
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   251
        fprintf(stdout, "   %.3d (%0.8x)\n", (tp - pairsSwapped), tkey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (tkey <= key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
          if (tkey == key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            le_int16 value = SWAPW(tp->value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
#if DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            fprintf(stdout, "binary found kerning pair %x:%x at %d, value: 0x%x (%g)\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    storage[i-1], storage[i], i, value & 0xffff, font->xUnitsToPoints(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            fflush(stdout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            // Have to undo the device transform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            // REMIND either find a way to do this only if there is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            // device transform, or a faster way, such as moving the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            // entire kern table up to Java.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            LEPoint pt;
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   266
            pt.fX = fTable.getFont()->xUnitsToPoints(value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            pt.fY = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
16891
91e99bed64ae 8001031: Better font processing
prr
parents: 8742
diff changeset
   269
            fTable.getFont()->getKerningAdjustment(pt);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            adjust += pt.fX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
          p = tp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
      storage.adjustPosition(i, adjust, 0, success);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    storage.adjustPosition(storage.getGlyphCount(), adjust, 0, success);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
}
3935
afcdb712a9c5 6501644: sync LayoutEngine *code* structure to match ICU
srl
parents: 2
diff changeset
   282
afcdb712a9c5 6501644: sync LayoutEngine *code* structure to match ICU
srl
parents: 2
diff changeset
   283
U_NAMESPACE_END
afcdb712a9c5 6501644: sync LayoutEngine *code* structure to match ICU
srl
parents: 2
diff changeset
   284