View | Details | Raw Unified | Return to bug 51351
Collapse All | Expand All

(-)src/org/apache/poi/hwpf/extractor/WordToFoUtils.java (-74 / +120 lines)
Lines 9-14 Link Here
9
import org.apache.poi.hwpf.usermodel.CharacterProperties;
9
import org.apache.poi.hwpf.usermodel.CharacterProperties;
10
import org.apache.poi.hwpf.usermodel.CharacterRun;
10
import org.apache.poi.hwpf.usermodel.CharacterRun;
11
import org.apache.poi.hwpf.usermodel.Paragraph;
11
import org.apache.poi.hwpf.usermodel.Paragraph;
12
import org.apache.poi.hwpf.usermodel.Picture;
12
import org.apache.poi.hwpf.usermodel.Range;
13
import org.apache.poi.hwpf.usermodel.Range;
13
import org.apache.poi.hwpf.usermodel.Section;
14
import org.apache.poi.hwpf.usermodel.Section;
14
import org.apache.poi.hwpf.usermodel.SectionProperties;
15
import org.apache.poi.hwpf.usermodel.SectionProperties;
Lines 179-184 Link Here
179
	}
180
	}
180
    }
181
    }
181
182
183
    public static String getJustification(int js) {
184
        switch (js) {
185
        case 0:
186
            return "start";
187
        case 1:
188
            return "center";
189
        case 2:
190
            return "end";
191
        case 3:
192
        case 4:
193
            return "justify";
194
        case 5:
195
            return "center";
196
        case 6:
197
            return "left";
198
        case 7:
199
            return "start";
200
        case 8:
201
            return "end";
202
        case 9:
203
            return "justify";
204
        }
205
        return "";
206
    }
207
182
    public static String getListItemNumberLabel(int number, int format) {
208
    public static String getListItemNumberLabel(int number, int format) {
183
209
184
	if (format != 0)
210
	if (format != 0)
Lines 244-291 Link Here
244
    }
270
    }
245
271
246
    public static void setCharactersProperties(final CharacterRun characterRun,
272
    public static void setCharactersProperties(final CharacterRun characterRun,
247
	    final Element inline) {
273
            final Element inline) {
248
	final CharacterProperties clonedProperties = characterRun
274
        final CharacterProperties clonedProperties = characterRun
249
		.cloneProperties();
275
                .cloneProperties();
250
	StringBuilder textDecorations = new StringBuilder();
276
        StringBuilder textDecorations = new StringBuilder();
251
277
252
	setBorder(inline, clonedProperties.getBrc(), EMPTY);
278
        setBorder(inline, clonedProperties.getBrc(), EMPTY);
253
279
254
	if (characterRun.isCapitalized()) {
280
        if (characterRun.isCapitalized()) {
255
	    inline.setAttribute("text-transform", "uppercase");
281
            inline.setAttribute("text-transform", "uppercase");
256
	}
282
        }
257
	if (characterRun.isHighlighted()) {
283
        if (characterRun.isHighlighted()) {
258
	    inline.setAttribute("background-color",
284
            inline.setAttribute("background-color",
259
		    getColor(clonedProperties.getIcoHighlight()));
285
                    getColor(clonedProperties.getIcoHighlight()));
260
	}
286
        }
261
	if (characterRun.isStrikeThrough()) {
287
        if (characterRun.isStrikeThrough()) {
262
	    if (textDecorations.length() > 0)
288
            if (textDecorations.length() > 0)
263
		textDecorations.append(" ");
289
                textDecorations.append(" ");
264
	    textDecorations.append("line-through");
290
            textDecorations.append("line-through");
265
	}
291
        }
266
	if (characterRun.isShadowed()) {
292
        if (characterRun.isShadowed()) {
267
	    inline.setAttribute("text-shadow", characterRun.getFontSize() / 24
293
            inline.setAttribute("text-shadow", characterRun.getFontSize() / 24
268
		    + "pt");
294
                    + "pt");
269
	}
295
        }
270
	if (characterRun.isSmallCaps()) {
296
        if (characterRun.isSmallCaps()) {
271
	    inline.setAttribute("font-variant", "small-caps");
297
            inline.setAttribute("font-variant", "small-caps");
272
	}
298
        }
273
	if (characterRun.getSubSuperScriptIndex() == 1) {
299
        if (characterRun.getSubSuperScriptIndex() == 1) {
274
	    inline.setAttribute("baseline-shift", "super");
300
            inline.setAttribute("baseline-shift", "super");
275
	    inline.setAttribute("font-size", "smaller");
301
            inline.setAttribute("font-size", "smaller");
276
	}
302
        }
277
	if (characterRun.getSubSuperScriptIndex() == 2) {
303
        if (characterRun.getSubSuperScriptIndex() == 2) {
278
	    inline.setAttribute("baseline-shift", "sub");
304
            inline.setAttribute("baseline-shift", "sub");
279
	    inline.setAttribute("font-size", "smaller");
305
            inline.setAttribute("font-size", "smaller");
280
	}
306
        }
281
	if (characterRun.getUnderlineCode() > 0) {
307
        if (characterRun.getUnderlineCode() > 0) {
282
	    if (textDecorations.length() > 0)
308
            if (textDecorations.length() > 0)
283
		textDecorations.append(" ");
309
                textDecorations.append(" ");
284
	    textDecorations.append("underline");
310
            textDecorations.append("underline");
285
	}
311
        }
286
	if (textDecorations.length() > 0) {
312
        if (characterRun.isVanished()) {
287
	    inline.setAttribute("text-decoration", textDecorations.toString());
313
            inline.setAttribute("visibility", "hidden");
288
	}
314
        }
315
        if (textDecorations.length() > 0) {
316
            inline.setAttribute("text-decoration", textDecorations.toString());
317
        }
289
    }
318
    }
290
319
291
    public static void setFontFamily(final Element element,
320
    public static void setFontFamily(final Element element,
Lines 335-374 Link Here
335
    }
364
    }
336
365
337
    public static void setJustification(Paragraph paragraph,
366
    public static void setJustification(Paragraph paragraph,
338
	    final Element element) {
367
            final Element element) {
339
	final int justification = paragraph.getJustification();
368
        String justification = getJustification(paragraph.getJustification());
340
	switch (justification) {
369
        if (isNotEmpty(justification))
341
	case 0:
370
            element.setAttribute("text-align", justification);
342
	    element.setAttribute("text-align", "start");
343
	    break;
344
	case 1:
345
	    element.setAttribute("text-align", "center");
346
	    break;
347
	case 2:
348
	    element.setAttribute("text-align", "end");
349
	    break;
350
	case 3:
351
	    element.setAttribute("text-align", "justify");
352
	    break;
353
	case 4:
354
	    element.setAttribute("text-align", "justify");
355
	    break;
356
	case 5:
357
	    element.setAttribute("text-align", "center");
358
	    break;
359
	case 6:
360
	    element.setAttribute("text-align", "left");
361
	    break;
362
	case 7:
363
	    element.setAttribute("text-align", "start");
364
	    break;
365
	case 8:
366
	    element.setAttribute("text-align", "end");
367
	    break;
368
	case 9:
369
	    element.setAttribute("text-align", "justify");
370
	    break;
371
	}
372
    }
371
    }
373
372
374
    public static void setParagraphProperties(Paragraph paragraph, Element block) {
373
    public static void setParagraphProperties(Paragraph paragraph, Element block) {
Lines 399-404 Link Here
399
	block.setAttribute("white-space-collapse", "false");
398
	block.setAttribute("white-space-collapse", "false");
400
    }
399
    }
401
400
401
    public static void setPictureProperties(Picture picture,
402
            Element graphicElement) {
403
        final int aspectRatioX = picture.getAspectRatioX();
404
        final int aspectRatioY = picture.getAspectRatioY();
405
406
        if (aspectRatioX > 0) {
407
            graphicElement.setAttribute("content-width", ((picture.getDxaGoal()
408
                    * aspectRatioX / 100) / WordToFoUtils.TWIPS_PER_PT)
409
                    + "pt");
410
        } else
411
            graphicElement.setAttribute("content-width",
412
                    (picture.getDxaGoal() / WordToFoUtils.TWIPS_PER_PT) + "pt");
413
414
        if (aspectRatioY > 0)
415
            graphicElement
416
                    .setAttribute("content-height", ((picture.getDyaGoal()
417
                            * aspectRatioY / 100) / WordToFoUtils.TWIPS_PER_PT)
418
                            + "pt");
419
        else
420
            graphicElement.setAttribute("content-height",
421
                    (picture.getDyaGoal() / WordToFoUtils.TWIPS_PER_PT) + "pt");
422
423
        if (aspectRatioX <= 0 || aspectRatioY <= 0) {
424
            graphicElement.setAttribute("scaling", "uniform");
425
        } else {
426
            graphicElement.setAttribute("scaling", "non-uniform");
427
        }
428
429
        graphicElement.setAttribute("vertical-align", "text-bottom");
430
431
        if (picture.getDyaCropTop() != 0 || picture.getDxaCropRight() != 0
432
                || picture.getDyaCropBottom() != 0
433
                || picture.getDxaCropLeft() != 0) {
434
            int rectTop = picture.getDyaCropTop() / WordToFoUtils.TWIPS_PER_PT;
435
            int rectRight = picture.getDxaCropRight()
436
                    / WordToFoUtils.TWIPS_PER_PT;
437
            int rectBottom = picture.getDyaCropBottom()
438
                    / WordToFoUtils.TWIPS_PER_PT;
439
            int rectLeft = picture.getDxaCropLeft()
440
                    / WordToFoUtils.TWIPS_PER_PT;
441
            graphicElement.setAttribute("clip", "rect(" + rectTop + "pt, "
442
                    + rectRight + "pt, " + rectBottom + "pt, " + rectLeft
443
                    + "pt)");
444
            graphicElement.setAttribute("oveerflow", "hidden");
445
        }
446
    }
447
402
    public static void setTableCellProperties(TableRow tableRow,
448
    public static void setTableCellProperties(TableRow tableRow,
403
	    TableCell tableCell, Element element, boolean toppest,
449
	    TableCell tableCell, Element element, boolean toppest,
404
	    boolean bottomest, boolean leftest, boolean rightest) {
450
	    boolean bottomest, boolean leftest, boolean rightest) {
(-)src/org/apache/poi/hwpf/extractor/WordToFoExtractor.java (-82 / +116 lines)
Lines 279-341 Link Here
279
	}
279
	}
280
    }
280
    }
281
281
282
    @SuppressWarnings("unused")
282
    /**
283
    protected void processImage(Element currentBlock, Picture picture) {
283
     * This method shall store image bytes in external file and convert it if
284
	// no default implementation -- skip
284
     * necessary. Images shall be stored using PNG format (for bitmap) or SVG
285
     * (for vector). Other formats may be not supported by your XSL FO
286
     * processor.
287
     * <p>
288
     * Please note the
289
     * {@link WordToFoUtils#setPictureProperties(Picture, Element)} method.
290
     * 
291
     * @param currentBlock
292
     *            currently processed FO element, like <tt>fo:block</tt>. Shall
293
     *            be used as parent of newly created
294
     *            <tt>fo:external-graphic</tt> or
295
     *            <tt>fo:instream-foreign-object</tt>
296
     * @param inlined
297
     *            if image is inlined
298
     * @param picture
299
     *            HWPF object, contained picture data and properties
300
     */
301
    protected void processImage(Element currentBlock, boolean inlined,
302
            Picture picture) {
303
        // no default implementation -- skip
285
    }
304
    }
286
305
287
    protected void processParagraph(HWPFDocument hwpfDocument,
306
    protected void processParagraph(HWPFDocument hwpfDocument,
288
	    Element parentFopElement, int currentTableLevel,
307
            Element parentFopElement, int currentTableLevel,
289
	    Paragraph paragraph, String bulletText) {
308
            Paragraph paragraph, String bulletText) {
290
	final Element block = createBlock();
309
        final Element block = createBlock();
291
	parentFopElement.appendChild(block);
310
        parentFopElement.appendChild(block);
292
311
293
	WordToFoUtils.setParagraphProperties(paragraph, block);
312
        WordToFoUtils.setParagraphProperties(paragraph, block);
294
313
295
	final int charRuns = paragraph.numCharacterRuns();
314
        final int charRuns = paragraph.numCharacterRuns();
296
315
297
	if (charRuns == 0) {
316
        if (charRuns == 0) {
298
	    return;
317
            return;
299
	}
318
        }
300
319
301
	final String pFontName;
320
        final String pFontName;
302
	final int pFontSize;
321
        final int pFontSize;
303
	final boolean pBold;
322
        final boolean pBold;
304
	final boolean pItalic;
323
        final boolean pItalic;
305
	{
324
        {
306
	    CharacterRun characterRun = paragraph.getCharacterRun(0);
325
            CharacterRun characterRun = paragraph.getCharacterRun(0);
307
	    pFontSize = characterRun.getFontSize() / 2;
326
            pFontSize = characterRun.getFontSize() / 2;
308
	    pFontName = characterRun.getFontName();
327
            pFontName = characterRun.getFontName();
309
	    pBold = characterRun.isBold();
328
            pBold = characterRun.isBold();
310
	    pItalic = characterRun.isItalic();
329
            pItalic = characterRun.isItalic();
311
	}
330
        }
312
	WordToFoUtils.setFontFamily(block, pFontName);
331
        WordToFoUtils.setFontFamily(block, pFontName);
313
	WordToFoUtils.setFontSize(block, pFontSize);
332
        WordToFoUtils.setFontSize(block, pFontSize);
314
	WordToFoUtils.setBold(block, pBold);
333
        WordToFoUtils.setBold(block, pBold);
315
	WordToFoUtils.setItalic(block, pItalic);
334
        WordToFoUtils.setItalic(block, pItalic);
316
335
317
	StringBuilder lineText = new StringBuilder();
336
        StringBuilder lineText = new StringBuilder();
318
337
319
	if (WordToFoUtils.isNotEmpty(bulletText)) {
338
        if (WordToFoUtils.isNotEmpty(bulletText)) {
320
	    Element inline = createInline();
339
            Element inline = createInline();
321
	    block.appendChild(inline);
340
            block.appendChild(inline);
322
341
323
	    Text textNode = createText(bulletText);
342
            Text textNode = createText(bulletText);
324
	    inline.appendChild(textNode);
343
            inline.appendChild(textNode);
325
344
326
	    lineText.append(bulletText);
345
            lineText.append(bulletText);
327
	}
346
        }
347
348
        for (int c = 0; c < charRuns; c++) {
349
            CharacterRun characterRun = paragraph.getCharacterRun(c);
350
351
            if (hwpfDocument.getPicturesTable().hasPicture(characterRun)) {
352
                Picture picture = hwpfDocument.getPicturesTable()
353
                        .extractPicture(characterRun, true);
328
354
329
	for (int c = 0; c < charRuns; c++) {
355
                processImage(block, characterRun.text().charAt(0) == 0x01,
330
	    CharacterRun characterRun = paragraph.getCharacterRun(c);
356
                        picture);
357
                continue;
358
            }
331
359
332
	    String text = characterRun.text();
360
	    String text = characterRun.text();
333
	    if (text.getBytes().length == 0)
361
	    if (text.getBytes().length == 0)
334
		continue;
362
		continue;
335
363
336
	    if (text.getBytes()[0] == FIELD_BEGIN_MARK) {
364
            if (text.getBytes()[0] == FIELD_BEGIN_MARK) {
337
		int skipTo = tryImageWithinField(hwpfDocument, paragraph, c,
365
                /*
338
			block);
366
                 * check if we have a field with calculated image as a result.
367
                 * MathType equation, for example.
368
                 */
369
                int skipTo = tryImageWithinField(hwpfDocument, paragraph, c,
370
                        block);
339
371
340
		if (skipTo != c) {
372
		if (skipTo != c) {
341
		    c = skipTo;
373
		    c = skipTo;
Lines 550-608 Link Here
550
    }
582
    }
551
583
552
    protected int tryImageWithinField(HWPFDocument hwpfDocument,
584
    protected int tryImageWithinField(HWPFDocument hwpfDocument,
553
	    Paragraph paragraph, int beginMark, Element currentBlock) {
585
            Paragraph paragraph, int beginMark, Element currentBlock) {
554
	int separatorMark = -1;
586
        int separatorMark = -1;
555
	int pictureMark = -1;
587
        int pictureMark = -1;
556
	int endMark = -1;
588
        int pictureChar = Integer.MIN_VALUE;
557
	for (int c = beginMark + 1; c < paragraph.numCharacterRuns(); c++) {
589
        int endMark = -1;
558
	    CharacterRun characterRun = paragraph.getCharacterRun(c);
590
        for (int c = beginMark + 1; c < paragraph.numCharacterRuns(); c++) {
591
            CharacterRun characterRun = paragraph.getCharacterRun(c);
559
592
560
	    String text = characterRun.text();
593
            String text = characterRun.text();
561
	    if (text.getBytes().length == 0)
594
            if (text.getBytes().length == 0)
562
		continue;
595
                continue;
563
596
564
	    if (text.getBytes()[0] == FIELD_SEPARATOR_MARK) {
597
            if (text.getBytes()[0] == FIELD_SEPARATOR_MARK) {
565
		if (separatorMark != -1) {
598
                if (separatorMark != -1) {
566
		    // double;
599
                    // double;
567
		    return beginMark;
600
                    return beginMark;
568
		}
601
                }
569
602
570
		separatorMark = c;
603
                separatorMark = c;
571
		continue;
604
                continue;
572
	    }
605
            }
573
606
574
	    if (text.getBytes()[0] == FIELD_END_MARK) {
607
            if (text.getBytes()[0] == FIELD_END_MARK) {
575
		if (endMark != -1) {
608
                if (endMark != -1) {
576
		    // double;
609
                    // double;
577
		    return beginMark;
610
                    return beginMark;
578
		}
611
                }
579
612
580
		endMark = c;
613
                endMark = c;
581
		break;
614
                break;
582
	    }
615
            }
583
616
584
	    if (hwpfDocument.getPicturesTable().hasPicture(characterRun)) {
617
            if (hwpfDocument.getPicturesTable().hasPicture(characterRun)) {
585
		if (pictureMark != -1) {
618
                if (c != -1) {
586
		    // double;
619
                    // double;
587
		    return beginMark;
620
                    return beginMark;
588
		}
621
                }
589
622
590
		pictureMark = c;
623
                pictureMark = c;
591
		continue;
624
                pictureChar = characterRun.text().charAt(0);
592
	    }
625
                continue;
593
	}
626
            }
627
        }
594
628
595
	if (separatorMark == -1 || pictureMark == -1 || endMark == -1)
629
        if (separatorMark == -1 || pictureMark == -1 || endMark == -1)
596
	    return beginMark;
630
            return beginMark;
597
631
598
	final CharacterRun pictureRun = paragraph.getCharacterRun(pictureMark);
632
        final CharacterRun pictureRun = paragraph.getCharacterRun(pictureMark);
599
	final Picture picture = hwpfDocument.getPicturesTable().extractPicture(
633
        final Picture picture = hwpfDocument.getPicturesTable().extractPicture(
600
		pictureRun, true);
634
                pictureRun, true);
601
	processImage(currentBlock, picture);
602
635
603
	return endMark;
636
        processImage(currentBlock, pictureChar == 0x01, picture);
604
    }
605
637
638
        return endMark;
639
    }
606
640
607
    /**
641
    /**
608
     * Java main() interface to interact with WordToFoExtractor
642
     * Java main() interface to interact with WordToFoExtractor

Return to bug 51351