Skip to content

Commit 3de713f

Browse files
FOP-3280: Optimise memory when font has no kerning
1 parent a8fb5df commit 3de713f

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

fop-core/src/main/java/org/apache/fop/layoutmgr/inline/TextLayoutManager.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private PendingChange(final GlyphMapping mapping, final int index) {
9898
* be used to influence the start position of the first letter. The entry i+1 defines the
9999
* cursor advancement after the character i. A null entry means no special advancement.
100100
*/
101-
private final MinOptMax[] letterSpaceAdjustArray; //size = textArray.length + 1
101+
private MinOptMax[] letterSpaceAdjustArray; //size = textArray.length + 1
102102

103103
/** Font used for the space between words. */
104104
private Font spaceFont;
@@ -142,7 +142,6 @@ private PendingChange(final GlyphMapping mapping, final int index) {
142142
*/
143143
public TextLayoutManager(FOText node, FOUserAgent userAgent) {
144144
foText = node;
145-
letterSpaceAdjustArray = new MinOptMax[node.length() + 1];
146145
mappings = new ArrayList<GlyphMapping>();
147146
this.userAgent = userAgent;
148147
}
@@ -267,7 +266,7 @@ private void addMappingAreas(GlyphMapping mapping, int wordSpaceCount, int lette
267266
}
268267

269268
for (int i = mapping.startIndex; i < mapping.endIndex; i++) {
270-
MinOptMax letterSpaceAdjustment = letterSpaceAdjustArray[i + 1];
269+
MinOptMax letterSpaceAdjustment = getLetterSpaceAdjustment(i + 1);
271270
if (letterSpaceAdjustment != null && letterSpaceAdjustment.isElastic()) {
272271
letterSpaceCount++;
273272
}
@@ -659,8 +658,7 @@ private void addLetterAdjust(GlyphMapping wordMapping) {
659658
int j = letterSpaceAdjustIndex + i;
660659
if (j > 0) {
661660
int k = wordMapping.startIndex + i;
662-
MinOptMax adj = (k < letterSpaceAdjustArray.length)
663-
? letterSpaceAdjustArray [ k ] : null;
661+
MinOptMax adj = getLetterSpaceAdjustment(k);
664662
letterSpaceAdjust [ j ] = (adj == null) ? 0 : adj.getOpt();
665663
}
666664
if (letterSpaceCount > 0) {
@@ -975,6 +973,9 @@ private GlyphMapping processWord(final int alignment, final KnuthSequence sequen
975973
char breakOpportunityChar = breakOpportunity ? ch : 0;
976974
char precedingChar = prevMapping != null && !prevMapping.isSpace
977975
&& prevMapping.endIndex > 0 ? foText.charAt(prevMapping.endIndex - 1) : 0;
976+
if (letterSpaceAdjustArray == null && font.hasKerning()) {
977+
letterSpaceAdjustArray = new MinOptMax[foText.length() + 1];
978+
}
978979
GlyphMapping mapping = GlyphMapping.doGlyphMapping(foText, thisStart, lastIndex, font,
979980
letterSpaceIPD, letterSpaceAdjustArray, precedingChar, breakOpportunityChar,
980981
endsWithHyphen, level, false, false, retainControls);
@@ -1076,7 +1077,7 @@ public void hyphenate(Position pos, HyphContext hyphContext) {
10761077
newIPD = newIPD.plus(font.getCharWidth(cp));
10771078
//if (i > startIndex) {
10781079
if (i < stopIndex) {
1079-
MinOptMax letterSpaceAdjust = letterSpaceAdjustArray[i + 1];
1080+
MinOptMax letterSpaceAdjust = getLetterSpaceAdjustment(i + 1);
10801081
if (i == stopIndex - 1 && hyphenFollows) {
10811082
//the letter adjust here needs to be handled further down during
10821083
//element generation because it depends on hyph/no-hyph condition
@@ -1388,7 +1389,7 @@ private void addElementsForAWordFragment(List baseList, int alignment, GlyphMapp
13881389
MinOptMax widthIfNoBreakOccurs = null;
13891390
if (mapping.endIndex < foText.length()) {
13901391
//Add in kerning in no-break condition
1391-
widthIfNoBreakOccurs = letterSpaceAdjustArray[mapping.endIndex];
1392+
widthIfNoBreakOccurs = getLetterSpaceAdjustment(mapping.endIndex);
13921393
}
13931394
//if (mapping.breakIndex)
13941395

@@ -1523,4 +1524,10 @@ public String toString() {
15231524
+ "}";
15241525
}
15251526

1527+
protected MinOptMax getLetterSpaceAdjustment(int i) {
1528+
if (letterSpaceAdjustArray == null || i >= letterSpaceAdjustArray.length) {
1529+
return null;
1530+
}
1531+
return letterSpaceAdjustArray[i];
1532+
}
15261533
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/* $Id$ */
19+
package org.apache.fop.layoutmgr.inline;
20+
21+
import org.junit.Assert;
22+
import org.junit.Test;
23+
24+
public class TextLayoutManagerTestCase {
25+
@Test
26+
public void testLetterSpaceAdjustArray() {
27+
Assert.assertNull(new TextLayoutManager(null, null).getLetterSpaceAdjustment(1));
28+
}
29+
}

0 commit comments

Comments
 (0)