blob: 533f961ad0fca6c8407801bc0a79c5af8b102a6a [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This code is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 only, as
6 * published by the Free Software Foundation. Sun designates this
7 * particular file as subject to the "Classpath" exception as provided
8 * by Sun in the LICENSE file that accompanied this code.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
22 * have any questions.
23 *
24 */
25
26/*
27 *
28 * (C) Copyright IBM Corp. 1998-2005 - All Rights Reserved
29 *
30 */
31
32#include "LETypes.h"
33#include "GlyphPositionAdjustments.h"
34#include "LEGlyphStorage.h"
35#include "LEFontInstance.h"
36
37#define CHECK_ALLOCATE_ARRAY(array, type, size) \
38 if (array == NULL) { \
39 array = (type *) new type[size]; \
40 }
41
42GlyphPositionAdjustments::GlyphPositionAdjustments(le_int32 glyphCount)
43 : fGlyphCount(glyphCount), fEntryExitPoints(NULL), fAdjustments(NULL)
44{
45 fAdjustments = (Adjustment *) new Adjustment[glyphCount];
46}
47
48GlyphPositionAdjustments::~GlyphPositionAdjustments()
49{
50 delete[] fEntryExitPoints;
51 delete[] fAdjustments;
52}
53
54const LEPoint *GlyphPositionAdjustments::getEntryPoint(le_int32 index, LEPoint &entryPoint) const
55{
56 if (fEntryExitPoints == NULL) {
57 return NULL;
58 }
59
60 return fEntryExitPoints[index].getEntryPoint(entryPoint);
61}
62
63const LEPoint *GlyphPositionAdjustments::getExitPoint(le_int32 index, LEPoint &exitPoint)const
64{
65 if (fEntryExitPoints == NULL) {
66 return NULL;
67 }
68
69 return fEntryExitPoints[index].getExitPoint(exitPoint);
70}
71
72void GlyphPositionAdjustments::setEntryPoint(le_int32 index, LEPoint &newEntryPoint, le_bool baselineIsLogicalEnd)
73{
74 CHECK_ALLOCATE_ARRAY(fEntryExitPoints, EntryExitPoint, fGlyphCount);
75
76 fEntryExitPoints[index].setEntryPoint(newEntryPoint, baselineIsLogicalEnd);
77}
78
79void GlyphPositionAdjustments::setExitPoint(le_int32 index, LEPoint &newExitPoint, le_bool baselineIsLogicalEnd)
80{
81 CHECK_ALLOCATE_ARRAY(fEntryExitPoints, EntryExitPoint, fGlyphCount);
82
83 fEntryExitPoints[index].setExitPoint(newExitPoint, baselineIsLogicalEnd);
84}
85
86void GlyphPositionAdjustments::setCursiveGlyph(le_int32 index, le_bool baselineIsLogicalEnd)
87{
88 CHECK_ALLOCATE_ARRAY(fEntryExitPoints, EntryExitPoint, fGlyphCount);
89
90 fEntryExitPoints[index].setCursiveGlyph(baselineIsLogicalEnd);
91}
92
93void GlyphPositionAdjustments::applyCursiveAdjustments(LEGlyphStorage &glyphStorage, le_bool rightToLeft, const LEFontInstance *fontInstance)
94{
95 if (! hasCursiveGlyphs()) {
96 return;
97 }
98
99 le_int32 start = 0, end = fGlyphCount, dir = 1;
100 le_int32 firstExitPoint = -1, lastExitPoint = -1;
101 LEPoint entryAnchor, exitAnchor, pixels;
102 LEGlyphID lastExitGlyphID = 0;
103 float baselineAdjustment = 0;
104
105 // This removes a possible warning about
106 // using exitAnchor before it's been initialized.
107 exitAnchor.fX = exitAnchor.fY = 0;
108
109 if (rightToLeft) {
110 start = fGlyphCount - 1;
111 end = -1;
112 dir = -1;
113 }
114
115 for (le_int32 i = start; i != end; i += dir) {
116 LEGlyphID glyphID = glyphStorage[i];
117
118 if (isCursiveGlyph(i)) {
119 if (lastExitPoint >= 0 && getEntryPoint(i, entryAnchor) != NULL) {
120 float anchorDiffX = exitAnchor.fX - entryAnchor.fX;
121 float anchorDiffY = exitAnchor.fY - entryAnchor.fY;
122
123 baselineAdjustment += anchorDiffY;
124 adjustYPlacement(i, baselineAdjustment);
125
126 if (rightToLeft) {
127 LEPoint secondAdvance;
128
129 fontInstance->getGlyphAdvance(glyphID, pixels);
130 fontInstance->pixelsToUnits(pixels, secondAdvance);
131
132 adjustXAdvance(i, -(anchorDiffX + secondAdvance.fX));
133 } else {
134 LEPoint firstAdvance;
135
136 fontInstance->getGlyphAdvance(lastExitGlyphID, pixels);
137 fontInstance->pixelsToUnits(pixels, firstAdvance);
138
139 adjustXAdvance(lastExitPoint, anchorDiffX - firstAdvance.fX);
140 }
141 }
142
143 lastExitPoint = i;
144
145 if (getExitPoint(i, exitAnchor) != NULL) {
146 if (firstExitPoint < 0) {
147 firstExitPoint = i;
148 }
149
150 lastExitGlyphID = glyphID;
151 } else {
152 if (baselineIsLogicalEnd(i) && firstExitPoint >= 0 && lastExitPoint >= 0) {
153 le_int32 limit = lastExitPoint + dir;
154
155 for (le_int32 j = firstExitPoint; j != limit; j += dir) {
156 if (isCursiveGlyph(j)) {
157 adjustYPlacement(j, -baselineAdjustment);
158 }
159 }
160 }
161
162 firstExitPoint = lastExitPoint = -1;
163 baselineAdjustment = 0;
164 }
165 }
166 }
167}
168
169LEPoint *GlyphPositionAdjustments::EntryExitPoint::getEntryPoint(LEPoint &entryPoint) const
170{
171 if (fFlags & EEF_HAS_ENTRY_POINT) {
172 entryPoint = fEntryPoint;
173 return &entryPoint;
174 }
175
176 return NULL;
177}
178
179LEPoint *GlyphPositionAdjustments::EntryExitPoint::getExitPoint(LEPoint &exitPoint) const
180{
181 if (fFlags & EEF_HAS_EXIT_POINT) {
182 exitPoint = fExitPoint;
183 return &exitPoint;
184 }
185
186 return NULL;
187}