1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* $Id$ */
package org.apache.fop.fonts;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.fop.complexscripts.fonts.GlyphPositioningTable;
import org.apache.fop.complexscripts.util.CharScript;
import org.apache.fop.traits.MinOptMax;
import org.apache.fop.util.CharUtilities;
/**
* Stores the mapping of a text fragment to glyphs, along with various information.
*/
public class GlyphMapping {
private static final Log LOG = LogFactory.getLog(GlyphMapping.class);
/** Inclusive. */
public final int startIndex;
/** Exclusive. */
public final int endIndex;
private int wordCharLength;
public final int wordSpaceCount;
public int letterSpaceCount;
public MinOptMax areaIPD;
public final boolean isHyphenated;
public final boolean isSpace;
public boolean breakOppAfter;
public final Font font;
public final int level;
public final int[][] gposAdjustments;
public final String mapping;
public GlyphMapping(int startIndex, int endIndex, int wordSpaceCount, int letterSpaceCount,
MinOptMax areaIPD, boolean isHyphenated, boolean isSpace, boolean breakOppAfter,
Font font, int level, int[][] gposAdjustments) {
this(startIndex, endIndex, wordSpaceCount, letterSpaceCount, areaIPD, isHyphenated,
isSpace, breakOppAfter, font, level, gposAdjustments, null);
}
public GlyphMapping(int startIndex, int endIndex, int wordSpaceCount, int letterSpaceCount,
MinOptMax areaIPD, boolean isHyphenated, boolean isSpace, boolean breakOppAfter,
Font font, int level, int[][] gposAdjustments, String mapping) {
assert startIndex <= endIndex;
this.startIndex = startIndex;
this.endIndex = endIndex;
this.wordCharLength = -1;
this.wordSpaceCount = wordSpaceCount;
this.letterSpaceCount = letterSpaceCount;
this.areaIPD = areaIPD;
this.isHyphenated = isHyphenated;
this.isSpace = isSpace;
this.breakOppAfter = breakOppAfter;
this.font = font;
this.level = level;
this.gposAdjustments = gposAdjustments;
this.mapping = mapping;
}
public static GlyphMapping doGlyphMapping(TextFragment text, int startIndex, int endIndex,
Font font, MinOptMax letterSpaceIPD, MinOptMax[] letterSpaceAdjustArray,
char precedingChar, char breakOpportunityChar, final boolean endsWithHyphen, int level) {
GlyphMapping mapping;
if (font.performsSubstitution() || font.performsPositioning()) {
mapping = processWordMapping(text, startIndex, endIndex, font,
breakOpportunityChar, endsWithHyphen, level);
} else {
mapping = processWordNoMapping(text, startIndex, endIndex, font,
letterSpaceIPD, letterSpaceAdjustArray, precedingChar, breakOpportunityChar, endsWithHyphen,
level);
}
return mapping;
}
private static GlyphMapping processWordMapping(TextFragment text, int startIndex,
int endIndex, final Font font, final char breakOpportunityChar,
final boolean endsWithHyphen, int level) {
int e = endIndex; // end index of word in FOText character buffer
int nLS = 0; // # of letter spaces
String script = text.getScript();
String language = text.getLanguage();
if (LOG.isDebugEnabled()) {
LOG.debug("PW: [" + startIndex + "," + endIndex + "]: {"
+ " +M"
+ ", level = " + level
+ " }");
}
// 1. extract unmapped character sequence
CharSequence ics = text.subSequence(startIndex, e);
// 2. if script is not specified (by FO property) or it is specified as 'auto',
// then compute dominant script
if ((script == null) || "auto".equals(script)) {
script = CharScript.scriptTagFromCode(CharScript.dominantScript(ics));
}
if ((language == null) || "none".equals(language)) {
language = "dflt";
}
// 3. perform mapping of chars to glyphs ... to glyphs ... to chars
CharSequence mcs = font.performSubstitution(ics, script, language);
// 4. compute glyph position adjustments on (substituted) characters
int[][] gpa;
if (font.performsPositioning()) {
// handle GPOS adjustments
gpa = font.performPositioning(mcs, script, language);
} else if (font.hasKerning()) {
// handle standard (non-GPOS) kerning adjustments
gpa = getKerningAdjustments(mcs, font);
} else {
gpa = null;
}
// 5. reorder combining marks so that they precede (within the mapped char sequence) the
// base to which they are applied; N.B. position adjustments (gpa) are reordered in place
mcs = font.reorderCombiningMarks(mcs, gpa, script, language);
// 6. compute word ipd based on final position adjustments
MinOptMax ipd = MinOptMax.ZERO;
for (int i = 0, n = mcs.length(); i < n; i++) {
int c = mcs.charAt(i);
// TODO !BMP
int w = font.getCharWidth(c);
if (w < 0) {
w = 0;
}
if (gpa != null) {
w += gpa[i][GlyphPositioningTable.Value.IDX_X_ADVANCE];
}
ipd = ipd.plus(w);
}
// [TBD] - handle letter spacing
return new GlyphMapping(startIndex, e, 0, nLS, ipd, endsWithHyphen, false,
breakOpportunityChar != 0, font, level, gpa,
CharUtilities.isSameSequence(mcs, ics) ? null : mcs.toString());
}
/**
* Given a mapped character sequence MCS, obtain glyph position adjustments from the
* font's kerning data.
*
* @param mcs mapped character sequence
* @param font applicable font
* @return glyph position adjustments (or null if no kerning)
*/
private static int[][] getKerningAdjustments(CharSequence mcs, final Font font) {
int nc = mcs.length();
// extract kerning array
int[] ka = new int[nc]; // kerning array
for (int i = 0, n = nc, cPrev = -1; i < n; i++) {
int c = mcs.charAt(i);
// TODO !BMP
if (cPrev >= 0) {
ka[i] = font.getKernValue(cPrev, c);
}
cPrev = c;
}
// was there a non-zero kerning?
boolean hasKerning = false;
for (int i = 0, n = nc; i < n; i++) {
if (ka[i] != 0) {
hasKerning = true;
break;
}
}
// if non-zero kerning, then create and return glyph position adjustment array
if (hasKerning) {
int[][] gpa = new int[nc][4];
for (int i = 0, n = nc; i < n; i++) {
if (i > 0) {
gpa [i - 1][GlyphPositioningTable.Value.IDX_X_ADVANCE] = ka[i];
}
}
return gpa;
} else {
return null;
}
}
private static GlyphMapping processWordNoMapping(TextFragment text, int startIndex, int endIndex,
final Font font, MinOptMax letterSpaceIPD, MinOptMax[] letterSpaceAdjustArray,
char precedingChar, final char breakOpportunityChar, final boolean endsWithHyphen, int level) {
boolean kerning = font.hasKerning();
MinOptMax wordIPD = MinOptMax.ZERO;
if (LOG.isDebugEnabled()) {
LOG.debug("PW: [" + startIndex + "," + endIndex + "]: {"
+ " -M"
+ ", level = " + level
+ " }");
}
for (int i = startIndex; i < endIndex; i++) {
char currentChar = text.charAt(i);
// character width
int charWidth = font.getCharWidth(currentChar);
wordIPD = wordIPD.plus(charWidth);
// kerning
if (kerning) {
int kern = 0;
if (i > startIndex) {
char previousChar = text.charAt(i - 1);
kern = font.getKernValue(previousChar, currentChar);
} else if (precedingChar != 0) {
kern = font.getKernValue(precedingChar, currentChar);
}
if (kern != 0) {
addToLetterAdjust(letterSpaceAdjustArray, i, kern);
wordIPD = wordIPD.plus(kern);
}
}
}
if (kerning
&& (breakOpportunityChar != 0)
&& !isSpace(breakOpportunityChar)
&& endIndex > 0
&& endsWithHyphen) {
int kern = font.getKernValue(text.charAt(endIndex - 1), breakOpportunityChar);
if (kern != 0) {
addToLetterAdjust(letterSpaceAdjustArray, endIndex, kern);
// TODO: add kern to wordIPD?
}
}
// shy+chars at start of word: wordLength == 0 && breakOpportunity
// shy only characters in word: wordLength == 0 && !breakOpportunity
int wordLength = endIndex - startIndex;
int letterSpaces = 0;
if (wordLength != 0) {
letterSpaces = wordLength - 1;
// if there is a break opportunity and the next one (break character)
// is not a space, it could be used as a line end;
// add one more letter space, in case other text follows
if ((breakOpportunityChar != 0) && !isSpace(breakOpportunityChar)) {
letterSpaces++;
}
}
assert letterSpaces >= 0;
wordIPD = wordIPD.plus(letterSpaceIPD.mult(letterSpaces));
// create and return the AreaInfo object
return new GlyphMapping(startIndex, endIndex, 0,
letterSpaces, wordIPD,
endsWithHyphen,
false, breakOpportunityChar != 0, font, level, null);
}
private static void addToLetterAdjust(MinOptMax[] letterSpaceAdjustArray, int index, int width) {
if (letterSpaceAdjustArray[index] == null) {
letterSpaceAdjustArray[index] = MinOptMax.getInstance(width);
} else {
letterSpaceAdjustArray[index] = letterSpaceAdjustArray[index].plus(width);
}
}
/**
* Indicates whether a character is a space in terms of this layout manager.
*
* @param ch the character
* @return true if it's a space
*/
public static boolean isSpace(final char ch) {
return ch == CharUtilities.SPACE
|| CharUtilities.isNonBreakableSpace(ch)
|| CharUtilities.isFixedWidthSpace(ch);
}
/**
* Obtain number of 'characters' contained in word. If word is mapped, then this
* number may be less than or greater than the original length (breakIndex -
* startIndex). We compute and memoize thius length upon first invocation of this
* method.
*/
public int getWordLength() {
if (wordCharLength == -1) {
if (mapping != null) {
wordCharLength = mapping.length();
} else {
assert endIndex >= startIndex;
wordCharLength = endIndex - startIndex;
}
}
return wordCharLength;
}
public void addToAreaIPD(MinOptMax idp) {
areaIPD = areaIPD.plus(idp);
}
public String toString() {
return super.toString() + "{"
+ "interval = [" + startIndex + "," + endIndex + "]"
+ ", isSpace = " + isSpace
+ ", level = " + level
+ ", areaIPD = " + areaIPD
+ ", letterSpaceCount = " + letterSpaceCount
+ ", wordSpaceCount = " + wordSpaceCount
+ ", isHyphenated = " + isHyphenated
+ ", font = " + font
+ "}";
}
}
|