jdk/src/share/native/sun/font/layout/GlyphPositionAdjustments.cpp
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 3935 afcdb712a9c5
permissions -rw-r--r--
Initial load
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * have any questions.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * (C) Copyright IBM Corp. 1998-2005 - All Rights Reserved
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 "GlyphPositionAdjustments.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include "LEGlyphStorage.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include "LEFontInstance.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#define CHECK_ALLOCATE_ARRAY(array, type, size) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    if (array == NULL) { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        array = (type *) new type[size]; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
GlyphPositionAdjustments::GlyphPositionAdjustments(le_int32 glyphCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    : fGlyphCount(glyphCount), fEntryExitPoints(NULL), fAdjustments(NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    fAdjustments = (Adjustment *) new Adjustment[glyphCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
GlyphPositionAdjustments::~GlyphPositionAdjustments()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    delete[] fEntryExitPoints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    delete[] fAdjustments;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
const LEPoint *GlyphPositionAdjustments::getEntryPoint(le_int32 index, LEPoint &entryPoint) const
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    if (fEntryExitPoints == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    return fEntryExitPoints[index].getEntryPoint(entryPoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
const LEPoint *GlyphPositionAdjustments::getExitPoint(le_int32 index, LEPoint &exitPoint)const
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    if (fEntryExitPoints == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    return fEntryExitPoints[index].getExitPoint(exitPoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
void GlyphPositionAdjustments::setEntryPoint(le_int32 index, LEPoint &newEntryPoint, le_bool baselineIsLogicalEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    CHECK_ALLOCATE_ARRAY(fEntryExitPoints, EntryExitPoint, fGlyphCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    fEntryExitPoints[index].setEntryPoint(newEntryPoint, baselineIsLogicalEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
void GlyphPositionAdjustments::setExitPoint(le_int32 index, LEPoint &newExitPoint, le_bool baselineIsLogicalEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    CHECK_ALLOCATE_ARRAY(fEntryExitPoints, EntryExitPoint, fGlyphCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    fEntryExitPoints[index].setExitPoint(newExitPoint, baselineIsLogicalEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
void GlyphPositionAdjustments::setCursiveGlyph(le_int32 index, le_bool baselineIsLogicalEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    CHECK_ALLOCATE_ARRAY(fEntryExitPoints, EntryExitPoint, fGlyphCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    fEntryExitPoints[index].setCursiveGlyph(baselineIsLogicalEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
void GlyphPositionAdjustments::applyCursiveAdjustments(LEGlyphStorage &glyphStorage, le_bool rightToLeft, const LEFontInstance *fontInstance)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    if (! hasCursiveGlyphs()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    le_int32 start = 0, end = fGlyphCount, dir = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    le_int32 firstExitPoint = -1, lastExitPoint = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    LEPoint entryAnchor, exitAnchor, pixels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    LEGlyphID lastExitGlyphID = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    float baselineAdjustment = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    // This removes a possible warning about
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    // using exitAnchor before it's been initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    exitAnchor.fX = exitAnchor.fY = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    if (rightToLeft) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        start = fGlyphCount - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        end = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        dir = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    for (le_int32 i = start; i != end; i += dir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        LEGlyphID glyphID = glyphStorage[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (isCursiveGlyph(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            if (lastExitPoint >= 0 && getEntryPoint(i, entryAnchor) != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                float anchorDiffX = exitAnchor.fX - entryAnchor.fX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                float anchorDiffY = exitAnchor.fY - entryAnchor.fY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                baselineAdjustment += anchorDiffY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                adjustYPlacement(i, baselineAdjustment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                if (rightToLeft) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    LEPoint secondAdvance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    fontInstance->getGlyphAdvance(glyphID, pixels);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    fontInstance->pixelsToUnits(pixels, secondAdvance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    adjustXAdvance(i, -(anchorDiffX + secondAdvance.fX));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    LEPoint firstAdvance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    fontInstance->getGlyphAdvance(lastExitGlyphID, pixels);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    fontInstance->pixelsToUnits(pixels, firstAdvance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    adjustXAdvance(lastExitPoint, anchorDiffX - firstAdvance.fX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            lastExitPoint = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            if (getExitPoint(i, exitAnchor) != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                if (firstExitPoint < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    firstExitPoint = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                lastExitGlyphID = glyphID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                if (baselineIsLogicalEnd(i) && firstExitPoint >= 0 && lastExitPoint >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    le_int32 limit = lastExitPoint + dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    for (le_int32 j = firstExitPoint; j != limit; j += dir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                        if (isCursiveGlyph(j)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                            adjustYPlacement(j, -baselineAdjustment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                firstExitPoint = lastExitPoint = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                baselineAdjustment = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
LEPoint *GlyphPositionAdjustments::EntryExitPoint::getEntryPoint(LEPoint &entryPoint) const
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    if (fFlags & EEF_HAS_ENTRY_POINT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        entryPoint = fEntryPoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return &entryPoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
LEPoint *GlyphPositionAdjustments::EntryExitPoint::getExitPoint(LEPoint &exitPoint) const
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    if (fFlags & EEF_HAS_EXIT_POINT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        exitPoint = fExitPoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        return &exitPoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
}