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
|
/*
* 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 org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.fop.datatypes.Length;
import org.apache.fop.datatypes.LengthBase;
import org.apache.fop.datatypes.SimplePercentBaseContext;
import org.apache.fop.fo.Constants;
/**
* An implementation of the ScaledBaselineTable interface which calculates
* all baselines given the height above and below the dominant baseline.
*/
public class BasicScaledBaselineTable implements ScaledBaselineTable, Constants {
/** A logger for this class */
protected Log log = LogFactory.getLog(BasicScaledBaselineTable.class);
private int altitude;
private int depth;
private int xHeight;
private int dominantBaselineIdentifier;
private int writingMode;
private int dominantBaselineOffset;
private int beforeEdgeOffset;
private int afterEdgeOffset;
private static final float HANGING_BASELINE_FACTOR = 0.8f;
private static final float MATHEMATICAL_BASELINE_FACTOR = 0.5f;
/**
*
* Creates a new instance of BasicScaledBaselineTable for the given
* altitude, depth, xHeight, baseline and writingmode.
* @param altitude the height of the box or the font ascender
* @param depth the font descender or 0
* @param xHeight the font xHeight
* @param dominantBaselineIdentifier the dominant baseline given as an integer constant
* @param writingMode the writing mode given as an integer constant
*/
public BasicScaledBaselineTable(int altitude
, int depth
, int xHeight
, int dominantBaselineIdentifier
, int writingMode) {
this.altitude = altitude;
this.depth = depth;
this.xHeight = xHeight;
this.dominantBaselineIdentifier = dominantBaselineIdentifier;
this.writingMode = writingMode;
this.dominantBaselineOffset = getBaselineDefaultOffset(this.dominantBaselineIdentifier);
this.beforeEdgeOffset = altitude - dominantBaselineOffset;
this.afterEdgeOffset = depth - dominantBaselineOffset;
}
/**
* Return the dominant baseline for this baseline table.
* @return the dominant baseline
*/
public int getDominantBaselineIdentifier() {
return this.dominantBaselineIdentifier;
}
/**
* Return the writing mode for this baseline table.
* @return the writing mode
*/
public int getWritingMode() {
return this.writingMode;
}
/**
* Return the baseline offset measured from the dominant
* baseline for the given baseline.
* @param baselineIdentifier the baseline identifier
* @return the baseline offset
*/
public int getBaseline(int baselineIdentifier) {
int offset = 0;
if (!isHorizontalWritingMode()) {
switch (baselineIdentifier) {
case EN_TOP:
case EN_TEXT_TOP:
case EN_TEXT_BOTTOM:
case EN_BOTTOM:
log.warn("The given baseline is only supported for horizontal"
+ " writing modes");
return 0;
}
}
switch (baselineIdentifier) {
case EN_TOP: // fall through
case EN_BEFORE_EDGE:
offset = beforeEdgeOffset;
break;
case EN_TEXT_TOP:
case EN_TEXT_BEFORE_EDGE:
case EN_HANGING:
case EN_CENTRAL:
case EN_MIDDLE:
case EN_MATHEMATICAL:
case EN_ALPHABETIC:
case EN_IDEOGRAPHIC:
case EN_TEXT_BOTTOM:
case EN_TEXT_AFTER_EDGE:
offset = getBaselineDefaultOffset(baselineIdentifier) - dominantBaselineOffset;
break;
case EN_BOTTOM: // fall through
case EN_AFTER_EDGE:
offset = afterEdgeOffset;
break;
}
return offset;
}
private boolean isHorizontalWritingMode() {
return writingMode == EN_LR_TB || writingMode == EN_RL_TB;
}
/**
* Return the baseline offset measured from the font's default
* baseline for the given baseline.
* @param baselineIdentifier the baseline identifier
* @return the baseline offset
*/
private int getBaselineDefaultOffset(int baselineIdentifier) {
int offset = 0;
switch (baselineIdentifier) {
case EN_TEXT_BEFORE_EDGE:
offset = altitude;
break;
case EN_HANGING:
offset = (int)Math.round(altitude * HANGING_BASELINE_FACTOR);
break;
case EN_CENTRAL:
offset = (altitude - depth) / 2 + depth;
break;
case EN_MIDDLE:
offset = xHeight / 2;
break;
case EN_MATHEMATICAL:
offset = (int)Math.round(altitude * MATHEMATICAL_BASELINE_FACTOR);
break;
case EN_ALPHABETIC:
offset = 0;
break;
case EN_IDEOGRAPHIC: // Fall through
case EN_TEXT_AFTER_EDGE:
offset = depth;
break;
}
return offset;
}
/**
* {@inheritDoc}
*/
public void setBeforeAndAfterBaselines(int beforeBaseline, int afterBaseline) {
beforeEdgeOffset = beforeBaseline;
afterEdgeOffset = afterBaseline;
}
/**
* {@inheritDoc}
*/
public ScaledBaselineTable deriveScaledBaselineTable(int baselineIdentifier) {
BasicScaledBaselineTable bac
= new BasicScaledBaselineTable(altitude, depth, xHeight
, baselineIdentifier, this.writingMode);
return bac;
}
}
|