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
|
/*
* 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.layoutmgr.inline;
import java.util.LinkedList;
import java.util.List;
import org.apache.fop.area.Trait;
import org.apache.fop.area.inline.TextArea;
import org.apache.fop.fo.flow.Character;
import org.apache.fop.fo.properties.CommonBorderPaddingBackground;
import org.apache.fop.fonts.Font;
import org.apache.fop.fonts.FontSelector;
import org.apache.fop.layoutmgr.InlineKnuthSequence;
import org.apache.fop.layoutmgr.KnuthElement;
import org.apache.fop.layoutmgr.KnuthGlue;
import org.apache.fop.layoutmgr.KnuthPenalty;
import org.apache.fop.layoutmgr.KnuthSequence;
import org.apache.fop.layoutmgr.LayoutContext;
import org.apache.fop.layoutmgr.LeafPosition;
import org.apache.fop.layoutmgr.Position;
import org.apache.fop.layoutmgr.TraitSetter;
import org.apache.fop.traits.MinOptMax;
import org.apache.fop.traits.SpaceVal;
import org.apache.fop.util.CharUtilities;
/**
* LayoutManager for the fo:character formatting object
*/
public class CharacterLayoutManager extends LeafNodeLayoutManager {
private MinOptMax letterSpaceIPD;
private int hyphIPD;
private Font font;
private CommonBorderPaddingBackground borderProps = null;
/**
* Constructor
*
* @param node the fo:character formatting object
*/
public CharacterLayoutManager(Character node) {
super(node);
}
/** {@inheritDoc} */
public void initialize() {
Character fobj = (Character)this.fobj;
font = FontSelector.selectFontForCharacter(fobj, this);
SpaceVal ls = SpaceVal.makeLetterSpacing(fobj.getLetterSpacing());
letterSpaceIPD = ls.getSpace();
hyphIPD = fobj.getCommonHyphenation().getHyphIPD(font);
borderProps = fobj.getCommonBorderPaddingBackground();
setCommonBorderPaddingBackground(borderProps);
TextArea chArea = getCharacterInlineArea(fobj);
chArea.setBaselineOffset(font.getAscender());
setCurrentArea(chArea);
}
private TextArea getCharacterInlineArea(Character node) {
TextArea text = new TextArea();
char ch = node.getCharacter();
if (CharUtilities.isAnySpace(ch)) {
// add space unless it's zero-width:
if (!CharUtilities.isZeroWidthSpace(ch)) {
text.addSpace(ch, 0, CharUtilities.isAdjustableSpace(ch));
}
} else {
text.addWord(String.valueOf(ch), 0);
}
TraitSetter.setProducerID(text, node.getId());
TraitSetter.addTextDecoration(text, node.getTextDecoration());
TraitSetter.addPtr(text, node.getPtr()); // used for accessibility
return text;
}
/** {@inheritDoc} */
public List getNextKnuthElements(LayoutContext context, int alignment) {
MinOptMax ipd;
curArea = get(context);
KnuthSequence seq = new InlineKnuthSequence();
if (curArea == null) {
setFinished(true);
return null;
}
Character fobj = (Character)this.fobj;
ipd = MinOptMax.getInstance(font.getCharWidth(fobj.getCharacter()));
curArea.setIPD(ipd.getOpt());
curArea.setBPD(font.getAscender() - font.getDescender());
TraitSetter.addFontTraits(curArea, font);
curArea.addTrait(Trait.COLOR, fobj.getColor());
// TODO: may need some special handling for fo:character
alignmentContext = new AlignmentContext(font
, font.getFontSize()
, fobj.getAlignmentAdjust()
, fobj.getAlignmentBaseline()
, fobj.getBaselineShift()
, fobj.getDominantBaseline()
, context.getAlignmentContext());
addKnuthElementsForBorderPaddingStart(seq);
// create the AreaInfo object to store the computed values
areaInfo = new AreaInfo((short) 0, ipd, false, alignmentContext);
// node is a fo:Character
if (letterSpaceIPD.isStiff()) {
// constant letter space, only return a box
seq.add(new KnuthInlineBox(areaInfo.ipdArea.getOpt(), areaInfo.alignmentContext,
notifyPos(new LeafPosition(this, 0)), false));
} else {
// adjustable letter space, return a sequence of elements;
// at the moment the character is supposed to have no letter spaces,
// but returning this sequence allows us to change only one element
// if addALetterSpaceTo() is called
seq.add(new KnuthInlineBox(areaInfo.ipdArea.getOpt(), areaInfo.alignmentContext,
notifyPos(new LeafPosition(this, 0)), false));
seq.add(new KnuthPenalty(0, KnuthElement.INFINITE, false,
new LeafPosition(this, -1), true));
seq.add(new KnuthGlue(0, 0, 0,
new LeafPosition(this, -1), true));
seq.add(new KnuthInlineBox(0, null,
notifyPos(new LeafPosition(this, -1)), true));
}
addKnuthElementsForBorderPaddingEnd(seq);
LinkedList returnList = new LinkedList();
returnList.add(seq);
setFinished(true);
return returnList;
}
/** {@inheritDoc} */
public String getWordChars(Position pos) {
return ((TextArea) curArea).getText();
}
/** {@inheritDoc} */
public void hyphenate(Position pos, HyphContext hc) {
if (hc.getNextHyphPoint() == 1) {
// the character ends a syllable
areaInfo.bHyphenated = true;
isSomethingChanged = true;
} else {
// hc.getNextHyphPoint() returned -1 (no more hyphenation points)
// or a number > 1;
// the character does not end a syllable
}
hc.updateOffset(1);
}
/** {@inheritDoc} */
public boolean applyChanges(List oldList) {
setFinished(false);
return isSomethingChanged;
}
/** {@inheritDoc} */
public List getChangedKnuthElements(List oldList, int alignment) {
if (isFinished()) {
return null;
}
LinkedList returnList = new LinkedList();
addKnuthElementsForBorderPaddingStart(returnList);
if (letterSpaceIPD.isStiff() || areaInfo.iLScount == 0) {
// constant letter space, or no letter space
returnList.add(new KnuthInlineBox(areaInfo.ipdArea.getOpt(),
areaInfo.alignmentContext,
notifyPos(new LeafPosition(this, 0)), false));
if (areaInfo.bHyphenated) {
returnList.add
(new KnuthPenalty(hyphIPD, KnuthPenalty.FLAGGED_PENALTY, true,
new LeafPosition(this, -1), false));
}
} else {
// adjustable letter space
returnList.add(new KnuthInlineBox(areaInfo.ipdArea.getOpt()
- areaInfo.iLScount * letterSpaceIPD.getOpt(), areaInfo.alignmentContext,
notifyPos(new LeafPosition(this, 0)), false));
returnList.add(new KnuthPenalty(0, KnuthElement.INFINITE, false,
new LeafPosition(this, -1), true));
returnList.add(new KnuthGlue(letterSpaceIPD.mult(areaInfo.iLScount),
new LeafPosition(this, -1), true));
returnList.add
(new KnuthInlineBox(0, null, notifyPos(new LeafPosition(this, -1)), true));
if (areaInfo.bHyphenated) {
returnList.add(new KnuthPenalty(hyphIPD, KnuthPenalty.FLAGGED_PENALTY, true,
new LeafPosition(this, -1), false));
}
}
addKnuthElementsForBorderPaddingEnd(returnList);
setFinished(true);
return returnList;
}
}
|