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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
/* ====================================================================
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.
==================================================================== */
package org.apache.poi.hwpf.converter;
import org.apache.poi.hwpf.usermodel.BorderCode;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Picture;
import org.apache.poi.hwpf.usermodel.TableCell;
import org.apache.poi.hwpf.usermodel.TableRow;
import org.apache.poi.util.Beta;
import org.w3c.dom.Element;
@Beta
public class WordToFoUtils extends AbstractWordUtils
{
static void compactInlines( Element blockElement )
{
compactChildNodesR( blockElement, "fo:inline" );
}
public static void setBold( final Element element, final boolean bold )
{
element.setAttribute( "font-weight", bold ? "bold" : "normal" );
}
public static void setBorder( Element element, BorderCode borderCode,
String where )
{
if ( element == null )
throw new IllegalArgumentException( "element is null" );
if ( borderCode == null || borderCode.isEmpty() )
return;
if ( isEmpty( where ) )
{
element.setAttribute( "border-style", getBorderType( borderCode ) );
element.setAttribute( "border-color",
getColor( borderCode.getColor() ) );
element.setAttribute( "border-width", getBorderWidth( borderCode ) );
}
else
{
element.setAttribute( "border-" + where + "-style",
getBorderType( borderCode ) );
element.setAttribute( "border-" + where + "-color",
getColor( borderCode.getColor() ) );
element.setAttribute( "border-" + where + "-width",
getBorderWidth( borderCode ) );
}
}
public static void setCharactersProperties(
final CharacterRun characterRun, final Element inline )
{
StringBuilder textDecorations = new StringBuilder();
setBorder( inline, characterRun.getBorder(), EMPTY );
if ( characterRun.getIco24() != -1 )
{
inline.setAttribute( "color", getColor24( characterRun.getIco24() ) );
}
/* XLS FO 1.1 doesn't support opacity -- sergey */
// final int opacity = (int) ( characterRun.getIco24() & 0xFF000000l )
// >>> 24;
// if ( opacity != 0 && opacity != 0xFF )
// {
// inline.setAttribute( "opacity",
// getOpacity( characterRun.getIco24() ) );
// }
if ( characterRun.isCapitalized() )
{
inline.setAttribute( "text-transform", "uppercase" );
}
if ( characterRun.isHighlighted() )
{
inline.setAttribute( "background-color",
getColor( characterRun.getHighlightedColor() ) );
}
if ( characterRun.isStrikeThrough() )
{
if ( textDecorations.length() > 0 )
textDecorations.append( " " );
textDecorations.append( "line-through" );
}
if ( characterRun.isShadowed() )
{
inline.setAttribute( "text-shadow", characterRun.getFontSize() / 24
+ "pt" );
}
if ( characterRun.isSmallCaps() )
{
inline.setAttribute( "font-variant", "small-caps" );
}
if ( characterRun.getSubSuperScriptIndex() == 1 )
{
inline.setAttribute( "baseline-shift", "super" );
inline.setAttribute( "font-size", "smaller" );
}
if ( characterRun.getSubSuperScriptIndex() == 2 )
{
inline.setAttribute( "baseline-shift", "sub" );
inline.setAttribute( "font-size", "smaller" );
}
if ( characterRun.getUnderlineCode() > 0 )
{
if ( textDecorations.length() > 0 )
textDecorations.append( " " );
textDecorations.append( "underline" );
}
if ( characterRun.isVanished() )
{
inline.setAttribute( "visibility", "hidden" );
}
if ( textDecorations.length() > 0 )
{
inline.setAttribute( "text-decoration", textDecorations.toString() );
}
}
public static void setFontFamily( final Element element,
final String fontFamily )
{
if ( isEmpty( fontFamily ) )
return;
element.setAttribute( "font-family", fontFamily );
}
public static void setFontSize( final Element element, final int fontSize )
{
element.setAttribute( "font-size", String.valueOf( fontSize ) );
}
public static void setIndent( Paragraph paragraph, Element block )
{
if ( paragraph.getFirstLineIndent() != 0 )
{
block.setAttribute(
"text-indent",
paragraph.getFirstLineIndent()
/ TWIPS_PER_PT
+ "pt" );
}
if ( paragraph.getIndentFromLeft() != 0 )
{
block.setAttribute(
"start-indent",
paragraph.getIndentFromLeft()
/ TWIPS_PER_PT
+ "pt" );
}
if ( paragraph.getIndentFromRight() != 0 )
{
block.setAttribute(
"end-indent",
paragraph.getIndentFromRight()
/ TWIPS_PER_PT
+ "pt" );
}
if ( paragraph.getSpacingBefore() != 0 )
{
block.setAttribute(
"space-before",
paragraph.getSpacingBefore() / TWIPS_PER_PT
+ "pt" );
}
if ( paragraph.getSpacingAfter() != 0 )
{
block.setAttribute( "space-after",
paragraph.getSpacingAfter() / TWIPS_PER_PT
+ "pt" );
}
}
public static void setItalic( final Element element, final boolean italic )
{
element.setAttribute( "font-style", italic ? "italic" : "normal" );
}
public static void setJustification( Paragraph paragraph,
final Element element )
{
String justification = getJustification( paragraph.getJustification() );
if ( isNotEmpty( justification ) )
element.setAttribute( "text-align", justification );
}
public static void setLanguage( final CharacterRun characterRun,
final Element inline )
{
if ( characterRun.getLanguageCode() != 0 )
{
final String language = getLanguage( characterRun.getLanguageCode() );
if ( isNotEmpty( language ) )
inline.setAttribute( "language", language );
}
}
public static void setParagraphProperties( Paragraph paragraph,
Element block )
{
setIndent( paragraph, block );
setJustification( paragraph, block );
setBorder( block, paragraph.getBottomBorder(), "bottom" );
setBorder( block, paragraph.getLeftBorder(), "left" );
setBorder( block, paragraph.getRightBorder(), "right" );
setBorder( block, paragraph.getTopBorder(), "top" );
if ( paragraph.pageBreakBefore() )
{
block.setAttribute( "break-before", "page" );
}
block.setAttribute( "hyphenate",
String.valueOf( paragraph.isAutoHyphenated() ) );
if ( paragraph.keepOnPage() )
{
block.setAttribute( "keep-together.within-page", "always" );
}
if ( paragraph.keepWithNext() )
{
block.setAttribute( "keep-with-next.within-page", "always" );
}
block.setAttribute( "linefeed-treatment", "preserve" );
block.setAttribute( "white-space-collapse", "false" );
}
public static void setPictureProperties( Picture picture,
Element graphicElement )
{
final int horizontalScale = picture.getHorizontalScalingFactor();
final int verticalScale = picture.getVerticalScalingFactor();
if ( horizontalScale > 0 )
{
graphicElement
.setAttribute( "content-width", ( ( picture.getDxaGoal()
* horizontalScale / 1000 ) / TWIPS_PER_PT )
+ "pt" );
}
else
graphicElement.setAttribute( "content-width",
( picture.getDxaGoal() / TWIPS_PER_PT ) + "pt" );
if ( verticalScale > 0 )
graphicElement
.setAttribute( "content-height", ( ( picture.getDyaGoal()
* verticalScale / 1000 ) / TWIPS_PER_PT )
+ "pt" );
else
graphicElement.setAttribute( "content-height",
( picture.getDyaGoal() / TWIPS_PER_PT ) + "pt" );
if ( horizontalScale <= 0 || verticalScale <= 0 )
{
graphicElement.setAttribute( "scaling", "uniform" );
}
else
{
graphicElement.setAttribute( "scaling", "non-uniform" );
}
graphicElement.setAttribute( "vertical-align", "text-bottom" );
if ( picture.getDyaCropTop() != 0 || picture.getDxaCropRight() != 0
|| picture.getDyaCropBottom() != 0
|| picture.getDxaCropLeft() != 0 )
{
int rectTop = picture.getDyaCropTop() / TWIPS_PER_PT;
int rectRight = picture.getDxaCropRight() / TWIPS_PER_PT;
int rectBottom = picture.getDyaCropBottom() / TWIPS_PER_PT;
int rectLeft = picture.getDxaCropLeft() / TWIPS_PER_PT;
graphicElement.setAttribute( "clip", "rect(" + rectTop + "pt, "
+ rectRight + "pt, " + rectBottom + "pt, " + rectLeft
+ "pt)" );
graphicElement.setAttribute( "overflow", "hidden" );
}
}
public static void setTableCellProperties( TableRow tableRow,
TableCell tableCell, Element element, boolean toppest,
boolean bottomest, boolean leftest, boolean rightest )
{
element.setAttribute( "width", ( tableCell.getWidth() / TWIPS_PER_INCH )
+ "in" );
element.setAttribute( "padding-start",
( tableRow.getGapHalf() / TWIPS_PER_INCH ) + "in" );
element.setAttribute( "padding-end",
( tableRow.getGapHalf() / TWIPS_PER_INCH ) + "in" );
BorderCode top = tableCell.getBrcTop() != null
&& tableCell.getBrcTop().getBorderType() != 0 ? tableCell
.getBrcTop() : toppest ? tableRow.getTopBorder() : tableRow
.getHorizontalBorder();
BorderCode bottom = tableCell.getBrcBottom() != null
&& tableCell.getBrcBottom().getBorderType() != 0 ? tableCell
.getBrcBottom() : bottomest ? tableRow.getBottomBorder()
: tableRow.getHorizontalBorder();
BorderCode left = tableCell.getBrcLeft() != null
&& tableCell.getBrcLeft().getBorderType() != 0 ? tableCell
.getBrcLeft() : leftest ? tableRow.getLeftBorder() : tableRow
.getVerticalBorder();
BorderCode right = tableCell.getBrcRight() != null
&& tableCell.getBrcRight().getBorderType() != 0 ? tableCell
.getBrcRight() : rightest ? tableRow.getRightBorder()
: tableRow.getVerticalBorder();
setBorder( element, bottom, "bottom" );
setBorder( element, left, "left" );
setBorder( element, right, "right" );
setBorder( element, top, "top" );
}
public static void setTableRowProperties( TableRow tableRow,
Element tableRowElement )
{
if ( tableRow.getRowHeight() > 0 )
{
tableRowElement.setAttribute( "height",
( tableRow.getRowHeight() / TWIPS_PER_INCH ) + "in" );
}
if ( !tableRow.cantSplit() )
{
tableRowElement.setAttribute( "keep-together.within-column",
"always" );
}
}
}
|