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
|
/*
* 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.complexscripts.bidi;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;
import org.apache.fop.area.inline.Anchor;
import org.apache.fop.area.inline.InlineArea;
import org.apache.fop.area.inline.InlineBlockParent;
import org.apache.fop.area.inline.InlineParent;
import org.apache.fop.area.inline.InlineViewport;
import org.apache.fop.area.inline.Leader;
import org.apache.fop.area.inline.Space;
import org.apache.fop.area.inline.SpaceArea;
import org.apache.fop.area.inline.UnresolvedPageNumber;
import org.apache.fop.area.inline.WordArea;
import org.apache.fop.util.CharUtilities;
// CSOFF: EmptyForIteratorPadCheck
// CSOFF: InnerAssignmentCheck
// CSOFF: NoWhitespaceAfterCheck
// CSOFF: SimplifyBooleanReturnCheck
/**
* The <code>InlineRun</code> class is a utility class, the instances of which are used
* to capture a sequence of reordering levels associated with an inline area.
*
* <p>This work was originally authored by Glenn Adams (gadams@apache.org).</p>
*/
public class InlineRun {
private InlineArea inline;
private int[] levels;
private int minLevel;
private int maxLevel;
private int reversals;
/**
* Primary constructor.
* @param inline which generated this inline run
* @param levels levels array
*/
public InlineRun ( InlineArea inline, int[] levels ) {
assert inline != null;
assert levels != null;
this.inline = inline;
this.levels = levels;
setMinMax ( levels );
}
/**
* Alternate constructor.
* @param inline which generated this inline run
* @param level for each index
* @param count of indices
*/
public InlineRun ( InlineArea inline, int level, int count ) {
this ( inline, makeLevels ( level, count ) );
}
/**
* Obtain inline area that generated this inline run.
* @return inline area that generated this inline run.
*/
public InlineArea getInline() {
return inline;
}
/**
* Obtain minimum bidi level for this run.
* @return minimum bidi level
*/
public int getMinLevel() {
return minLevel;
}
/**
* Obtain maximum bidi level for this run.
* @return maximum bidi level
*/
public int getMaxLevel() {
return maxLevel;
}
private void setMinMax ( int[] levels ) {
int mn = Integer.MAX_VALUE;
int mx = Integer.MIN_VALUE;
if ( ( levels != null ) && ( levels.length > 0 ) ) {
for ( int i = 0, n = levels.length; i < n; i++ ) {
int l = levels [ i ];
if ( l < mn ) {
mn = l;
}
if ( l > mx ) {
mx = l;
}
}
} else {
mn = mx = -1;
}
this.minLevel = mn;
this.maxLevel = mx;
}
/**
* Determine if this run has homogenous (same valued) bidi levels.
* @return true if homogenous
*/
public boolean isHomogenous() {
return minLevel == maxLevel;
}
/**
* Split this inline run into homogenous runs.
* @return list of new runs
*/
public List split() {
List runs = new Vector();
for ( int i = 0, n = levels.length; i < n; ) {
int l = levels [ i ];
int s = i;
int e = s;
while ( e < n ) {
if ( levels [ e ] != l ) {
break;
} else {
e++;
}
}
if ( s < e ) {
runs.add ( new InlineRun ( inline, l, e - s ) );
}
i = e;
}
assert runs.size() < 2 : "heterogeneous inlines not yet supported!!";
return runs;
}
/**
* Update a min/max array to correspond with this run's min/max values.
* @param mm reference to min/max array
*/
public void updateMinMax ( int[] mm ) {
if ( minLevel < mm[0] ) {
mm[0] = minLevel;
}
if ( maxLevel > mm[1] ) {
mm[1] = maxLevel;
}
}
/**
* Determine if run needs mirroring.
* @return true if run is homogenous and odd (i.e., right to left)
*/
public boolean maybeNeedsMirroring() {
return ( minLevel == maxLevel ) && ( ( minLevel & 1 ) != 0 );
}
/**
* Reverse run (by incrementing reversal count, not actually reversing).
*/
public void reverse() {
reversals++;
}
/**
* Reverse inline area if it is a word area and it requires
* reversal.
* @param mirror if true then also mirror characters
*/
public void maybeReverseWord ( boolean mirror ) {
if ( inline instanceof WordArea ) {
WordArea w = (WordArea) inline;
// if not already reversed, then reverse now
if ( ! w.isReversed() ) {
if ( ( reversals & 1 ) != 0 ) {
w.reverse ( mirror );
} else if ( mirror && maybeNeedsMirroring() ) {
w.mirror();
}
}
}
}
@Override
public boolean equals ( Object o ) {
if ( o instanceof InlineRun ) {
InlineRun ir = (InlineRun) o;
if ( ir.inline != inline ) {
return false;
} else if ( ir.minLevel != minLevel ) {
return false;
} else if ( ir.maxLevel != maxLevel ) {
return false;
} else if ( ( ir.levels != null ) && ( levels != null ) ) {
if ( ir.levels.length != levels.length ) {
return false;
} else {
for ( int i = 0, n = levels.length; i < n; i++ ) {
if ( ir.levels[i] != levels[i] ) {
return false;
}
}
return true;
}
} else if ( ( ir.levels == null ) && ( levels == null ) ) {
return true;
} else {
return false;
}
} else {
return false;
}
}
@Override
public int hashCode() {
int l = ( inline != null ) ? inline.hashCode() : 0;
l = ( l ^ minLevel ) + ( l << 19 );
l = ( l ^ maxLevel ) + ( l << 11 );
return l;
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer( "RR: { type = \'" );
char c;
String content = null;
if ( inline instanceof WordArea ) {
c = 'W';
content = ( (WordArea) inline ) .getWord();
} else if ( inline instanceof SpaceArea ) {
c = 'S';
content = ( (SpaceArea) inline ) .getSpace();
} else if ( inline instanceof Anchor ) {
c = 'A';
} else if ( inline instanceof Leader ) {
c = 'L';
} else if ( inline instanceof Space ) {
c = 'S';
} else if ( inline instanceof UnresolvedPageNumber ) {
c = '#';
content = ( (UnresolvedPageNumber) inline ) .getText();
} else if ( inline instanceof InlineBlockParent ) {
c = 'B';
} else if ( inline instanceof InlineViewport ) {
c = 'V';
} else if ( inline instanceof InlineParent ) {
c = 'I';
} else {
c = '?';
}
sb.append ( c );
sb.append ( "\', levels = \'" );
sb.append ( generateLevels ( levels ) );
sb.append ( "\', min = " );
sb.append ( minLevel );
sb.append ( ", max = " );
sb.append ( maxLevel );
sb.append ( ", reversals = " );
sb.append ( reversals );
sb.append ( ", content = <" );
sb.append ( CharUtilities.toNCRefs ( content ) );
sb.append ( "> }" );
return sb.toString();
}
private String generateLevels ( int[] levels ) {
StringBuffer lb = new StringBuffer();
int maxLevel = -1;
int numLevels = levels.length;
for ( int i = 0; i < numLevels; i++ ) {
int l = levels [ i ];
if ( l > maxLevel ) {
maxLevel = l;
}
}
if ( maxLevel < 0 ) {
// leave level buffer empty
} else if ( maxLevel < 10 ) {
// use string of decimal digits
for ( int i = 0; i < numLevels; i++ ) {
lb.append ( (char) ( '0' + levels [ i ] ) );
}
} else {
// use comma separated list
boolean first = true;
for ( int i = 0; i < numLevels; i++ ) {
if ( first ) {
first = false;
} else {
lb.append(',');
}
lb.append ( levels [ i ] );
}
}
return lb.toString();
}
private static int[] makeLevels ( int level, int count ) {
int[] levels = new int [ count ];
Arrays.fill ( levels, level );
return levels;
}
}
|