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

(-)src/java/org/apache/fop/area/AreaTreeParser.java (-10 / +6 lines)
Lines 20-25 Link Here
20
package org.apache.fop.area;
20
package org.apache.fop.area;
21
21
22
import java.awt.Color;
22
import java.awt.Color;
23
import java.awt.Rectangle;
23
import java.awt.geom.Rectangle2D;
24
import java.awt.geom.Rectangle2D;
24
import java.util.List;
25
import java.util.List;
25
import java.util.Map;
26
import java.util.Map;
Lines 36-52 Link Here
36
import javax.xml.transform.sax.SAXTransformerFactory;
37
import javax.xml.transform.sax.SAXTransformerFactory;
37
import javax.xml.transform.sax.TransformerHandler;
38
import javax.xml.transform.sax.TransformerHandler;
38
39
40
import org.apache.commons.logging.Log;
41
import org.apache.commons.logging.LogFactory;
42
43
import org.apache.fop.util.*;
39
import org.w3c.dom.DOMImplementation;
44
import org.w3c.dom.DOMImplementation;
40
import org.w3c.dom.Document;
45
import org.w3c.dom.Document;
41
42
import org.xml.sax.Attributes;
46
import org.xml.sax.Attributes;
43
import org.xml.sax.ContentHandler;
47
import org.xml.sax.ContentHandler;
44
import org.xml.sax.SAXException;
48
import org.xml.sax.SAXException;
45
import org.xml.sax.helpers.DefaultHandler;
49
import org.xml.sax.helpers.DefaultHandler;
46
50
47
import org.apache.commons.logging.Log;
48
import org.apache.commons.logging.LogFactory;
49
50
import org.apache.xmlgraphics.image.loader.ImageInfo;
51
import org.apache.xmlgraphics.image.loader.ImageInfo;
51
import org.apache.xmlgraphics.image.loader.ImageManager;
52
import org.apache.xmlgraphics.image.loader.ImageManager;
52
import org.apache.xmlgraphics.image.loader.ImageSessionContext;
53
import org.apache.xmlgraphics.image.loader.ImageSessionContext;
Lines 74-84 Link Here
74
import org.apache.fop.fonts.Font;
75
import org.apache.fop.fonts.Font;
75
import org.apache.fop.fonts.FontInfo;
76
import org.apache.fop.fonts.FontInfo;
76
import org.apache.fop.traits.BorderProps;
77
import org.apache.fop.traits.BorderProps;
77
import org.apache.fop.util.ColorUtil;
78
import org.apache.fop.util.ContentHandlerFactory;
79
import org.apache.fop.util.ContentHandlerFactoryRegistry;
80
import org.apache.fop.util.DefaultErrorListener;
81
import org.apache.fop.util.QName;
82
78
83
/**
79
/**
84
 * This is a parser for the area tree XML (intermediate format) which is used to reread an area
80
 * This is a parser for the area tree XML (intermediate format) which is used to reread an area
Lines 384-390 Link Here
384
                if (currentPageViewport != null) {
380
                if (currentPageViewport != null) {
385
                    throw new IllegalStateException("currentPageViewport must be null");
381
                    throw new IllegalStateException("currentPageViewport must be null");
386
                }
382
                }
387
                Rectangle2D viewArea = parseRect(attributes.getValue("bounds"));
383
                Rectangle viewArea = XMLUtil.getAttributeAsRectangle(attributes, "bounds");
388
                int pageNumber = getAttributeAsInteger(attributes, "nr", -1);
384
                int pageNumber = getAttributeAsInteger(attributes, "nr", -1);
389
                String key = attributes.getValue("key");
385
                String key = attributes.getValue("key");
390
                String pageNumberString = attributes.getValue("formatted-nr");
386
                String pageNumberString = attributes.getValue("formatted-nr");
(-)src/java/org/apache/fop/area/PageViewport.java (-5 / +8 lines)
Lines 20-26 Link Here
20
package org.apache.fop.area;
20
package org.apache.fop.area;
21
21
22
import java.awt.Rectangle;
22
import java.awt.Rectangle;
23
import java.awt.geom.Rectangle2D;
24
import java.io.ObjectOutputStream;
23
import java.io.ObjectOutputStream;
25
import java.io.ObjectInputStream;
24
import java.io.ObjectInputStream;
26
import java.util.ArrayList;
25
import java.util.ArrayList;
Lines 49-55 Link Here
49
public class PageViewport extends AreaTreeObject implements Resolvable, Cloneable {
48
public class PageViewport extends AreaTreeObject implements Resolvable, Cloneable {
50
49
51
    private Page page;
50
    private Page page;
52
    private Rectangle2D viewArea;
51
    private Rectangle viewArea;
53
    private String simplePageMasterName;
52
    private String simplePageMasterName;
54
    
53
    
55
    /**
54
    /**
Lines 105-110 Link Here
105
    public PageViewport(SimplePageMaster spm, int pageNumber, String pageStr, boolean blank) {
104
    public PageViewport(SimplePageMaster spm, int pageNumber, String pageStr, boolean blank) {
106
        this.simplePageMasterName = spm.getMasterName();
105
        this.simplePageMasterName = spm.getMasterName();
107
        this.extensionAttachments = spm.getExtensionAttachments();
106
        this.extensionAttachments = spm.getExtensionAttachments();
107
        setForeignAttributes(spm.getForeignAttributes());
108
        this.blank = blank;
108
        this.blank = blank;
109
        int pageWidth = spm.getPageWidth().getValue();
109
        int pageWidth = spm.getPageWidth().getValue();
110
        int pageHeight = spm.getPageHeight().getValue();
110
        int pageHeight = spm.getPageHeight().getValue();
Lines 123-132 Link Here
123
        if (original.extensionAttachments != null) {
123
        if (original.extensionAttachments != null) {
124
            this.extensionAttachments = new java.util.ArrayList(original.extensionAttachments);
124
            this.extensionAttachments = new java.util.ArrayList(original.extensionAttachments);
125
        }
125
        }
126
        if (original.foreignAttributes != null) {
127
            setForeignAttributes(original.foreignAttributes);
128
        }
126
        this.pageNumber = original.pageNumber;
129
        this.pageNumber = original.pageNumber;
127
        this.pageNumberString = original.pageNumberString;
130
        this.pageNumberString = original.pageNumberString;
128
        this.page = (Page)original.page.clone();
131
        this.page = (Page)original.page.clone();
129
        this.viewArea = (Rectangle2D)original.viewArea.clone();
132
        this.viewArea = new Rectangle(original.viewArea);
130
        this.simplePageMasterName = original.simplePageMasterName;
133
        this.simplePageMasterName = original.simplePageMasterName;
131
        this.blank = original.blank;
134
        this.blank = original.blank;
132
    }
135
    }
Lines 139-145 Link Here
139
     * @param simplePageMasterName name of the original simple-page-master that generated this page
142
     * @param simplePageMasterName name of the original simple-page-master that generated this page
140
     * @param blank true if this is a blank page
143
     * @param blank true if this is a blank page
141
     */
144
     */
142
    public PageViewport(Rectangle2D viewArea, int pageNumber, String pageStr, 
145
    public PageViewport(Rectangle viewArea, int pageNumber, String pageStr, 
143
            String simplePageMasterName, boolean blank) {
146
            String simplePageMasterName, boolean blank) {
144
        this.viewArea = viewArea;
147
        this.viewArea = viewArea;
145
        this.pageNumber = pageNumber;
148
        this.pageNumber = pageNumber;
Lines 165-171 Link Here
165
     * Get the view area rectangle of this viewport.
168
     * Get the view area rectangle of this viewport.
166
     * @return the rectangle for this viewport
169
     * @return the rectangle for this viewport
167
     */
170
     */
168
    public Rectangle2D getViewArea() {
171
    public Rectangle getViewArea() {
169
        return viewArea;
172
        return viewArea;
170
    }
173
    }
171
174
(-)src/java/org/apache/fop/layoutmgr/Page.java (-2 / +2 lines)
Lines 19-25 Link Here
19
19
20
package org.apache.fop.layoutmgr;
20
package org.apache.fop.layoutmgr;
21
21
22
import java.awt.geom.Rectangle2D;
22
import java.awt.Rectangle;
23
23
24
import org.apache.fop.area.PageViewport;
24
import org.apache.fop.area.PageViewport;
25
import org.apache.fop.fo.pagination.SimplePageMaster;
25
import org.apache.fop.fo.pagination.SimplePageMaster;
Lines 54-60 Link Here
54
     * @param pageNumberStr the page number (as a String) 
54
     * @param pageNumberStr the page number (as a String) 
55
     * @param blank true if this is a blank page
55
     * @param blank true if this is a blank page
56
     */
56
     */
57
    public Page(Rectangle2D viewArea, int pageNumber, String pageNumberStr, boolean blank) {
57
    public Page(Rectangle viewArea, int pageNumber, String pageNumberStr, boolean blank) {
58
        this.spm = null;
58
        this.spm = null;
59
        this.pageViewport = new PageViewport(viewArea, pageNumber, pageNumberStr, null, blank);
59
        this.pageViewport = new PageViewport(viewArea, pageNumber, pageNumberStr, null, blank);
60
    }
60
    }
(-)src/java/org/apache/fop/pdf/PDFFactory.java (-11 / +26 lines)
Lines 171-192 Link Here
171
     * PDFDocument later using addObject().
171
     * PDFDocument later using addObject().
172
     *
172
     *
173
     * @param resources resources object to use
173
     * @param resources resources object to use
174
     * @param pageWidth width of the page in points
175
     * @param pageHeight height of the page in points
176
     * @param pageIndex index of the page (zero-based)
174
     * @param pageIndex index of the page (zero-based)
175
     * @param mediaBox the MediaBox area
176
     * @param cropBox the CropBox area
177
     * @param bleedBox the BleedBox area
178
     * @param trimBox the TrimBox area
177
     *
179
     *
178
     * @return the created /Page object
180
     * @return the created /Page object
179
     */
181
     */
180
    public PDFPage makePage(PDFResources resources,
182
    public PDFPage makePage(PDFResources resources, int pageIndex,
181
                            int pageWidth, int pageHeight, int pageIndex) {
183
                            Rectangle2D mediaBox, Rectangle2D cropBox,
184
                            Rectangle2D bleedBox, Rectangle2D trimBox) {
185
        PDFPage page = new PDFPage(resources, pageIndex, mediaBox, cropBox, bleedBox, trimBox);
182
186
183
        /*
184
         * create a PDFPage with the next object number, the given
185
         * resources, contents and dimensions
186
         */
187
        PDFPage page = new PDFPage(resources,
188
                                   pageWidth, pageHeight, pageIndex);
189
190
        getDocument().assignObjectNumber(page);
187
        getDocument().assignObjectNumber(page);
191
        getDocument().getPages().addPage(page);
188
        getDocument().getPages().addPage(page);
192
        return page;
189
        return page;
Lines 200-209 Link Here
200
     * @param resources resources object to use
197
     * @param resources resources object to use
201
     * @param pageWidth width of the page in points
198
     * @param pageWidth width of the page in points
202
     * @param pageHeight height of the page in points
199
     * @param pageHeight height of the page in points
200
     * @param pageIndex index of the page (zero-based)
203
     *
201
     *
204
     * @return the created /Page object
202
     * @return the created /Page object
205
     */
203
     */
206
    public PDFPage makePage(PDFResources resources,
204
    public PDFPage makePage(PDFResources resources,
205
                            int pageWidth, int pageHeight, int pageIndex) {
206
        Rectangle2D mediaBox = new Rectangle2D.Double(0, 0, pageWidth, pageHeight);
207
        return makePage(resources, pageIndex, mediaBox, mediaBox, mediaBox, mediaBox);
208
    }
209
210
    /**
211
     * Make a /Page object. The page is assigned an object number immediately
212
     * so references can already be made. The page must be added to the
213
     * PDFDocument later using addObject().
214
     *
215
     * @param resources resources object to use
216
     * @param pageWidth width of the page in points
217
     * @param pageHeight height of the page in points
218
     *
219
     * @return the created /Page object
220
     */
221
    public PDFPage makePage(PDFResources resources,
207
                            int pageWidth, int pageHeight) {
222
                            int pageWidth, int pageHeight) {
208
        return makePage(resources, pageWidth, pageHeight, -1);
223
        return makePage(resources, pageWidth, pageHeight, -1);
209
    }
224
    }
(-)src/java/org/apache/fop/pdf/PDFPage.java (-25 / +33 lines)
Lines 38-79 Link Here
38
     * Create a /Page object
38
     * Create a /Page object
39
     *
39
     *
40
     * @param resources the /Resources object
40
     * @param resources the /Resources object
41
     * @param contents the content stream
42
     * @param pageWidth the page's width in points
43
     * @param pageHeight the page's height in points
44
     * @param pageIndex the page's zero-based index (or -1 if the page number is auto-determined)
41
     * @param pageIndex the page's zero-based index (or -1 if the page number is auto-determined)
42
     * @param mediaBox the MediaBox
43
     * @param cropBox the CropBox. If null, mediaBox is used.
44
     * @param bleedBox the BleedBox. If null, cropBox is used.
45
     * @param trimBox the TrimBox. If null, bleedBox is used.
45
     */
46
     */
46
    public PDFPage(PDFResources resources, PDFStream contents,
47
    public PDFPage(PDFResources resources, int pageIndex,
47
                   int pageWidth, int pageHeight, int pageIndex) {
48
                   Rectangle2D mediaBox, Rectangle2D cropBox,
48
49
                   Rectangle2D bleedBox, Rectangle2D trimBox) {
49
        /* generic creation of object */
50
        /* generic creation of object */
50
        super(resources);
51
        super(resources);
51
52
52
        put("Type", new PDFName("Page"));
53
        put("Type", new PDFName("Page"));
53
        /* set fields using parameters */
54
        /* set fields using parameters */
54
        setContents(contents);
55
        setSimplePageSize(mediaBox, cropBox, bleedBox, trimBox);
55
        setSimplePageSize(pageWidth, pageHeight);
56
        this.pageIndex = pageIndex;
56
        this.pageIndex = pageIndex;
57
    }
57
    }
58
58
59
    /**
59
    private void setSimplePageSize(Rectangle2D mediaBox, Rectangle2D cropBox,
60
     * Create a /Page object
60
                                   Rectangle2D bleedBox, Rectangle2D trimBox) {
61
     *
61
        setMediaBox(mediaBox);
62
     * @param resources the /Resources object
63
     * @param pageWidth the page's width in points
64
     * @param pageHeight the page's height in points
65
     * @param pageIndex the page's zero-based index (or -1 if the page number is auto-determined)
66
     */
67
    public PDFPage(PDFResources resources,
68
                   int pageWidth, int pageHeight, int pageIndex) {
69
        this(resources, null, pageWidth, pageHeight, pageIndex);
70
    }
71
62
72
    private void setSimplePageSize(int width, int height) {
63
        if (cropBox == null) {
73
        Rectangle2D box = new Rectangle2D.Double(0, 0, width, height);
64
            cropBox = mediaBox;
74
        setMediaBox(box);
65
        }
75
        setBleedBox(box); //Recommended by PDF/X
66
        setCropBox(cropBox);
76
        setTrimBox(box); //Needed for PDF/X
67
  
68
        if (bleedBox == null) {
69
            bleedBox = cropBox;
70
        }
71
        setBleedBox(bleedBox); //Recommended by PDF/X
72
73
        if (trimBox == null) {
74
            trimBox = bleedBox;
75
        }
76
        setTrimBox(trimBox); //Needed for PDF/X
77
    }
77
    }
78
    
78
    
79
    private PDFArray toPDFArray(Rectangle2D box) {
79
    private PDFArray toPDFArray(Rectangle2D box) {
Lines 88-93 Link Here
88
    public void setMediaBox(Rectangle2D box) {
88
    public void setMediaBox(Rectangle2D box) {
89
        put("MediaBox", toPDFArray(box));
89
        put("MediaBox", toPDFArray(box));
90
    }
90
    }
91
92
    /**
93
     * Sets the "CropBox" entry
94
     * @param box the bleed rectangle
95
     */
96
    public void setCropBox(Rectangle2D box) {
97
        put("CropBox", toPDFArray(box));
98
    }  
91
    
99
    
92
    /**
100
    /**
93
     * Sets the "TrimBox" entry
101
     * Sets the "TrimBox" entry
(-)src/java/org/apache/fop/render/awt/AWTRenderer.java (+11 lines)
Lines 30-35 Link Here
30
import java.awt.Color;
30
import java.awt.Color;
31
import java.awt.Dimension;
31
import java.awt.Dimension;
32
import java.awt.geom.Rectangle2D;
32
import java.awt.geom.Rectangle2D;
33
import java.awt.geom.Point2D;
33
import java.awt.print.PageFormat;
34
import java.awt.print.PageFormat;
34
import java.awt.print.Pageable;
35
import java.awt.print.Pageable;
35
import java.awt.print.Paper;
36
import java.awt.print.Paper;
Lines 46-51 Link Here
46
import org.apache.fop.render.awt.viewer.Renderable;
47
import org.apache.fop.render.awt.viewer.Renderable;
47
import org.apache.fop.render.awt.viewer.StatusListener;
48
import org.apache.fop.render.awt.viewer.StatusListener;
48
import org.apache.fop.render.java2d.Java2DRenderer;
49
import org.apache.fop.render.java2d.Java2DRenderer;
50
import org.apache.fop.render.extensions.prepress.PageScale;
49
51
50
/**
52
/**
51
 * The AWTRender outputs the pages generated by the layout engine to a Swing
53
 * The AWTRender outputs the pages generated by the layout engine to a Swing
Lines 155-160 Link Here
155
        double scaleY = scaleFactor
157
        double scaleY = scaleFactor
156
                * (25.4 / FopFactoryConfigurator.DEFAULT_TARGET_RESOLUTION)
158
                * (25.4 / FopFactoryConfigurator.DEFAULT_TARGET_RESOLUTION)
157
                / userAgent.getTargetPixelUnitToMillimeter();
159
                / userAgent.getTargetPixelUnitToMillimeter();
160
        if (getPageViewport(pageNum).getForeignAttributes() != null) {
161
            String scale = (String) getPageViewport(pageNum).getForeignAttributes().get(
162
                    PageScale.EXT_PAGE_SCALE);
163
            Point2D scales = PageScale.getScale(scale);
164
            if (scales != null) {
165
                scaleX *= scales.getX();
166
                scaleY *= scales.getY();
167
            }
168
        }
158
        int bitmapWidth = (int) ((pageWidth * scaleX) + 0.5);
169
        int bitmapWidth = (int) ((pageWidth * scaleX) + 0.5);
159
        int bitmapHeight = (int) ((pageHeight * scaleY) + 0.5);
170
        int bitmapHeight = (int) ((pageHeight * scaleY) + 0.5);
160
        return new Dimension(bitmapWidth, bitmapHeight);
171
        return new Dimension(bitmapWidth, bitmapHeight);
(-)src/java/org/apache/fop/render/extensions/prepress/PageBoundaries.java (+216 lines)
Line 0 Link Here
1
package org.apache.fop.render.extensions.prepress;
2
3
import java.awt.*;
4
import java.text.MessageFormat;
5
import java.util.Map;
6
import java.util.regex.Matcher;
7
import java.util.regex.Pattern;
8
9
import org.apache.fop.util.QName;
10
11
import org.apache.fop.fo.extensions.ExtensionElementMapping;
12
import org.apache.fop.fo.properties.FixedLength;
13
14
15
/**
16
 * This class is used to calculate the effective boundaries of a page including special-purpose
17
 * boxes used in prepress. These are specified using extension attributes:
18
 * bleedBox, trimBox and cropBox. The semantics are further described on the website.
19
 */
20
public class PageBoundaries {
21
22
    /**
23
     * The extension attribute for calculating the PDF BleedBox area - specifies the bleed width.
24
     */
25
    public static final QName EXT_BLEED
26
            = new QName(ExtensionElementMapping.URI, null, "bleed");
27
28
    /**
29
     * The extension attribute for the PDF CropBox area.
30
     */
31
    public static final QName EXT_CROP_OFFSET
32
            = new QName(ExtensionElementMapping.URI, null, "crop-offset");
33
34
    /**
35
     * The extension attribute for the PDF CropBox area.
36
     */
37
    public static final QName EXT_CROP_BOX
38
            = new QName(ExtensionElementMapping.URI, null, "crop-box");
39
40
41
    private static final Pattern SIZE_UNIT_PATTERN
42
            = Pattern.compile("^(-?\\d*\\.?\\d*)(px|in|cm|mm|pt|pc|mpt)$");
43
44
    private static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\s+");
45
46
    private Rectangle trimBox;
47
    private Rectangle bleedBox;
48
    private Rectangle mediaBox;
49
    private Rectangle cropBox;
50
51
    /**
52
     * Creates a new instance.
53
     * @param pageSize the page size (in mpt) defined by the simple-page-master.
54
     * @param bleed the bleed value (raw value as given in the property value)
55
     * @param cropOffset the crop-offset value (raw value as given in the property value)
56
     * @param cropBoxSelector the crop-box, valid values: (trim-box|bleed-box|media-box)
57
     */
58
    public PageBoundaries(Dimension pageSize, String bleed, String cropOffset,
59
            String cropBoxSelector) {
60
        calculate(pageSize, bleed, cropOffset, cropBoxSelector);
61
    }
62
63
    /**
64
     * Creates a new instance.
65
     * @param pageSize the page size (in mpt) defined by the simple-page-master.
66
     * @param foreignAttributes the foreign attributes for the page
67
     *                  (used to extract the extension attribute values)
68
     */
69
    public PageBoundaries(Dimension pageSize, Map foreignAttributes) {
70
        String bleed = (String)foreignAttributes.get(EXT_BLEED);
71
        String cropOffset = (String)foreignAttributes.get(EXT_CROP_OFFSET);
72
        String cropBoxSelector = (String)foreignAttributes.get(EXT_CROP_BOX);
73
        calculate(pageSize, bleed, cropOffset, cropBoxSelector);
74
    }
75
76
    private void calculate(Dimension pageSize, String bleed, String cropOffset,
77
            String cropBoxSelector) {
78
        this.trimBox = new Rectangle(pageSize);
79
        this.bleedBox = getBleedBoxRectangle(this.trimBox, bleed);
80
        Rectangle cropMarksBox = getCropMarksAreaRectangle(trimBox, cropOffset);
81
82
        //MediaBox includes all of the following three rectangles
83
        this.mediaBox = new Rectangle();
84
        this.mediaBox.add(this.trimBox);
85
        this.mediaBox.add(this.bleedBox);
86
        this.mediaBox.add(cropMarksBox);
87
88
        if ("trim-box".equals(cropBoxSelector)) {
89
            this.cropBox = this.trimBox;
90
        } else if ("bleed-box".equals(cropBoxSelector)) {
91
            this.cropBox = this.bleedBox;
92
        } else if ("media-box".equals(cropBoxSelector)
93
                || cropBoxSelector == null
94
                || "".equals(cropBoxSelector)) {
95
            this.cropBox = this.mediaBox;
96
        } else {
97
            final String err = "The crop-box has invalid value: {0}, "
98
                + "possible values of crop-box: (trim-box|bleed-box|media-box)";
99
            throw new IllegalArgumentException(MessageFormat.format(err,
100
                    new Object[]{cropBoxSelector}));
101
        }
102
    }
103
104
    /**
105
     * Returns the trim box for the page. This is equal to the page size given in XSL-FO.
106
     * After production the printed media is trimmed to this rectangle.
107
     * @return the trim box
108
     */
109
    public Rectangle getTrimBox() {
110
        return this.trimBox;
111
    }
112
113
    /**
114
     * Returns the bleed box for the page.
115
     * @return the bleed box
116
     */
117
    public Rectangle getBleedBox() {
118
        return this.bleedBox;
119
    }
120
121
    /**
122
     * Returns the media box for the page.
123
     * @return the media box
124
     */
125
    public Rectangle getMediaBox() {
126
        return this.mediaBox;
127
    }
128
129
    /**
130
     * Returns the crop box for the page. The crop box is used by Adobe Acrobat to select which
131
     * parts of the document shall be displayed and it also defines the rectangle to which a
132
     * RIP will clip the document. For bitmap output, this defines the size of the bitmap.
133
     * @return the crop box
134
     */
135
    public Rectangle getCropBox() {
136
        return this.cropBox;
137
    }
138
139
    /**
140
     * The BleedBox is calculated by expanding the TrimBox by the bleed widths.
141
     *
142
     * @param trimBox the TrimBox rectangle
143
     * @param bleed   the given bleed widths
144
     * @return the calculated BleedBox rectangle
145
     */
146
    private static Rectangle getBleedBoxRectangle(Rectangle trimBox, String bleed) {
147
        return getRectangleUsingOffset(trimBox, bleed);
148
    }
149
150
    /**
151
     * The MediaBox is calculated by expanding the TrimBox by the crop offsets.
152
     *
153
     * @param trimBox     the TrimBox rectangle
154
     * @param cropOffsets the given crop offsets
155
     * @return the calculated MediaBox rectangle
156
     */
157
    private static Rectangle getCropMarksAreaRectangle(Rectangle trimBox, String cropOffsets) {
158
        return getRectangleUsingOffset(trimBox, cropOffsets);
159
    }
160
161
    private static Rectangle getRectangleUsingOffset(Rectangle originalRect, String offset) {
162
        if (offset == null || "".equals(offset) || originalRect == null) {
163
            return originalRect;
164
        }
165
166
        String[] offsets = WHITESPACE_PATTERN.split(offset);
167
        int[] coords = new int[4]; // top, right, bottom, left
168
        switch (offsets.length) {
169
        case 1:
170
            coords[0] = getLengthIntValue(offsets[0]);
171
            coords[1] = coords[0];
172
            coords[2] = coords[0];
173
            coords[3] = coords[0];
174
            break;
175
        case 2:
176
            coords[0] = getLengthIntValue(offsets[0]);
177
            coords[1] = getLengthIntValue(offsets[1]);
178
            coords[2] = coords[0];
179
            coords[3] = coords[1];
180
            break;
181
        case 3:
182
            coords[0] = getLengthIntValue(offsets[0]);
183
            coords[1] = getLengthIntValue(offsets[1]);
184
            coords[2] = getLengthIntValue(offsets[2]);
185
            coords[3] = coords[1];
186
            break;
187
        case 4:
188
            coords[0] = getLengthIntValue(offsets[0]);
189
            coords[1] = getLengthIntValue(offsets[1]);
190
            coords[2] = getLengthIntValue(offsets[2]);
191
            coords[3] = getLengthIntValue(offsets[3]);
192
            break;
193
        default:
194
            // TODO throw appropriate exception that can be caught by the event
195
            // notification mechanism
196
            throw new IllegalArgumentException("Too many arguments");
197
        }
198
        return new Rectangle(originalRect.x - coords[3],
199
                originalRect.y - coords[2],
200
                originalRect.width + coords[3] + coords[1],
201
                originalRect.height + coords[0] + coords[2]);
202
    }
203
204
    private static int getLengthIntValue(final String length) {
205
        final String err = "Incorrect length value: {0}";
206
        Matcher m = SIZE_UNIT_PATTERN.matcher(length);
207
208
        if (m.find()) {
209
            return FixedLength.getInstance(Double.parseDouble(m.group(1)),
210
                    m.group(2)).getLength().getValue();
211
        } else {
212
            throw new IllegalArgumentException(MessageFormat.format(err, new Object[]{length}));
213
        }
214
    }
215
216
}
(-)src/java/org/apache/fop/render/extensions/prepress/PageScale.java (+93 lines)
Line 0 Link Here
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
/* $Id: PageScaleAttributes.java 800142 2009-08-02 19:41:37Z jeremias $ */
19
20
package org.apache.fop.render.extensions.prepress;
21
22
import java.awt.geom.Point2D;
23
import java.text.MessageFormat;
24
import java.util.regex.Pattern;
25
26
import org.apache.fop.util.QName;
27
28
import org.apache.fop.fo.extensions.ExtensionElementMapping;
29
30
31
/**
32
 * This class provides utility methods to parse the 'fox:scale' extension attribute.
33
 */
34
public final class PageScale {
35
36
    /**
37
     * The extension 'scale' attribute for the simple-page-master element.
38
     */
39
    public static final QName EXT_PAGE_SCALE
40
            = new QName(ExtensionElementMapping.URI, null, "scale");
41
42
    private static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\s+");
43
44
    /**
45
     * Utility classes should not have a public or default constructor
46
     */
47
    private PageScale() {
48
    }
49
50
    /**
51
     * Compute scale parameters from given fox:scale attribute which has the format: scaleX [scaleY]
52
     * If scaleY is not defined, it equals scaleX.
53
     * @param scale scale attribute, input format: scaleX [scaleY]
54
     * @return the pair of (sx, sy) values
55
     */
56
    public static Point2D getScale(String scale) {
57
        // TODO throw appropriate exceptions that can be caught by the event
58
        // notification mechanism
59
        final String err = "Extension 'scale' attribute has incorrect value(s): {0}";
60
61
        if (scale == null || scale.equals("")) {
62
            return null;
63
        }
64
65
        String[] scales = WHITESPACE_PATTERN.split(scale);
66
        double scaleX;
67
        try {
68
            scaleX = Double.parseDouble(scales[0]);
69
        } catch (NumberFormatException nfe) {
70
            throw new IllegalArgumentException(MessageFormat.format(err, new Object[]{scale}));
71
        }
72
        double scaleY;
73
        switch (scales.length) {
74
        case 1:
75
            scaleY = scaleX;
76
            break;
77
        case 2:
78
            try {
79
                scaleY = Double.parseDouble(scales[1]);
80
            } catch (NumberFormatException nfe) {
81
                throw new IllegalArgumentException(MessageFormat.format(err, new Object[]{scale}));
82
            }
83
            break;
84
        default:
85
            throw new IllegalArgumentException("Too many arguments");
86
        }
87
        if (scaleX <= 0 || scaleY <= 0) {
88
            throw new IllegalArgumentException(MessageFormat.format(err, new Object[]{scale}));
89
        }
90
91
        return new Point2D.Double(scaleX, scaleY);
92
    }
93
}
(-)src/java/org/apache/fop/render/java2d/Java2DRenderer.java (-11 / +30 lines)
Lines 20-30 Link Here
20
package org.apache.fop.render.java2d;
20
package org.apache.fop.render.java2d;
21
21
22
// Java
22
// Java
23
import java.awt.BasicStroke;
23
import java.awt.*;
24
import java.awt.Color;
25
import java.awt.Graphics;
26
import java.awt.Graphics2D;
27
import java.awt.RenderingHints;
28
import java.awt.font.GlyphVector;
24
import java.awt.font.GlyphVector;
29
import java.awt.geom.AffineTransform;
25
import java.awt.geom.AffineTransform;
30
import java.awt.geom.GeneralPath;
26
import java.awt.geom.GeneralPath;
Lines 73-78 Link Here
73
import org.apache.fop.render.AbstractPathOrientedRenderer;
69
import org.apache.fop.render.AbstractPathOrientedRenderer;
74
import org.apache.fop.render.Graphics2DAdapter;
70
import org.apache.fop.render.Graphics2DAdapter;
75
import org.apache.fop.render.RendererContext;
71
import org.apache.fop.render.RendererContext;
72
import org.apache.fop.render.extensions.prepress.PageBoundaries;
73
import org.apache.fop.render.extensions.prepress.PageScale;
76
import org.apache.fop.render.pdf.CTMHelper;
74
import org.apache.fop.render.pdf.CTMHelper;
77
import org.apache.fop.util.CharUtilities;
75
import org.apache.fop.util.CharUtilities;
78
76
Lines 278-284 Link Here
278
276
279
        this.currentPageViewport = pageViewport;
277
        this.currentPageViewport = pageViewport;
280
        try {
278
        try {
281
            Rectangle2D bounds = pageViewport.getViewArea();
279
            PageBoundaries boundaries = new PageBoundaries(
280
                    pageViewport.getViewArea().getSize(), pageViewport.getForeignAttributes());
281
            Rectangle bounds = boundaries.getCropBox();
282
            Rectangle bleedBox = boundaries.getBleedBox();
282
            pageWidth = (int) Math.round(bounds.getWidth() / 1000f);
283
            pageWidth = (int) Math.round(bounds.getWidth() / 1000f);
283
            pageHeight = (int) Math.round(bounds.getHeight() / 1000f);
284
            pageHeight = (int) Math.round(bounds.getHeight() / 1000f);
284
285
Lines 287-297 Link Here
287
                            + " (pageWidth " + pageWidth + ", pageHeight "
288
                            + " (pageWidth " + pageWidth + ", pageHeight "
288
                            + pageHeight + ")");
289
                            + pageHeight + ")");
289
290
290
            double scaleX = scaleFactor 
291
            // set scale factor
291
                * (25.4 / FopFactoryConfigurator.DEFAULT_TARGET_RESOLUTION) 
292
            double scaleX = scaleFactor;
293
            double scaleY = scaleFactor;
294
            String scale = (String) currentPageViewport.getForeignAttributes().get(
295
                    PageScale.EXT_PAGE_SCALE);
296
            Point2D scales = PageScale.getScale(scale);
297
            if (scales != null) {
298
                scaleX *= scales.getX();
299
                scaleY *= scales.getY();
300
            }
301
          
302
            scaleX = scaleX
303
                * (25.4f / FopFactoryConfigurator.DEFAULT_TARGET_RESOLUTION)
292
                / userAgent.getTargetPixelUnitToMillimeter();
304
                / userAgent.getTargetPixelUnitToMillimeter();
293
            double scaleY = scaleFactor
305
            scaleY = scaleY
294
                * (25.4 / FopFactoryConfigurator.DEFAULT_TARGET_RESOLUTION)
306
                * (25.4f / FopFactoryConfigurator.DEFAULT_TARGET_RESOLUTION)
295
                / userAgent.getTargetPixelUnitToMillimeter();
307
                / userAgent.getTargetPixelUnitToMillimeter();
296
            int bitmapWidth = (int) ((pageWidth * scaleX) + 0.5);
308
            int bitmapWidth = (int) ((pageWidth * scaleX) + 0.5);
297
            int bitmapHeight = (int) ((pageHeight * scaleY) + 0.5);
309
            int bitmapHeight = (int) ((pageHeight * scaleY) + 0.5);
Lines 318-336 Link Here
318
            // transform page based on scale factor supplied
330
            // transform page based on scale factor supplied
319
            AffineTransform at = graphics.getTransform();
331
            AffineTransform at = graphics.getTransform();
320
            at.scale(scaleX, scaleY);
332
            at.scale(scaleX, scaleY);
333
            at.translate(bounds.getMinX() / -1000f, bounds.getMinY() / -1000f);
321
            graphics.setTransform(at);
334
            graphics.setTransform(at);
322
335
323
            // draw page frame
336
            // draw page frame
324
            if (!transparentPageBackground) {
337
            if (!transparentPageBackground) {
325
                graphics.setColor(Color.white);
338
                graphics.setColor(Color.white);
326
                graphics.fillRect(0, 0, pageWidth, pageHeight);
339
                graphics.fillRect(
340
                        (int)Math.round(bleedBox.getMinX() / 1000f),
341
                        (int)Math.round(bleedBox.getMinY() / 1000f),
342
                        (int)Math.round(bleedBox.getWidth() / 1000f),
343
                        (int)Math.round(bleedBox.getHeight() / 1000f));
327
            }
344
            }
345
            /* why did we have this???
328
            graphics.setColor(Color.black);
346
            graphics.setColor(Color.black);
329
            graphics.drawRect(-1, -1, pageWidth + 2, pageHeight + 2);
347
            graphics.drawRect(-1, -1, pageWidth + 2, pageHeight + 2);
330
            graphics.drawLine(pageWidth + 2, 0, pageWidth + 2, pageHeight + 2);
348
            graphics.drawLine(pageWidth + 2, 0, pageWidth + 2, pageHeight + 2);
331
            graphics.drawLine(pageWidth + 3, 1, pageWidth + 3, pageHeight + 3);
349
            graphics.drawLine(pageWidth + 3, 1, pageWidth + 3, pageHeight + 3);
332
            graphics.drawLine(0, pageHeight + 2, pageWidth + 2, pageHeight + 2);
350
            graphics.drawLine(0, pageHeight + 2, pageWidth + 2, pageHeight + 2);
333
            graphics.drawLine(1, pageHeight + 3, pageWidth + 3, pageHeight + 3);
351
            graphics.drawLine(1, pageHeight + 3, pageWidth + 3, pageHeight + 3);
352
            */
334
353
335
            state = new Java2DGraphicsState(graphics, this.fontInfo, at);
354
            state = new Java2DGraphicsState(graphics, this.fontInfo, at);
336
            try {
355
            try {
(-)src/java/org/apache/fop/render/pdf/PDFRenderer.java (-11 / +56 lines)
Lines 27-32 Link Here
27
import java.awt.geom.AffineTransform;
27
import java.awt.geom.AffineTransform;
28
import java.awt.geom.Point2D;
28
import java.awt.geom.Point2D;
29
import java.awt.geom.Rectangle2D;
29
import java.awt.geom.Rectangle2D;
30
import java.awt.geom.Rectangle2D.Double;
30
import java.io.FileNotFoundException;
31
import java.io.FileNotFoundException;
31
import java.io.IOException;
32
import java.io.IOException;
32
import java.io.InputStream;
33
import java.io.InputStream;
Lines 41-46 Link Here
41
42
42
import org.apache.commons.io.IOUtils;
43
import org.apache.commons.io.IOUtils;
43
44
45
import org.apache.fop.render.extensions.prepress.PageBoundaries;
46
import org.apache.fop.render.extensions.prepress.PageScale;
44
import org.apache.xmlgraphics.image.loader.ImageException;
47
import org.apache.xmlgraphics.image.loader.ImageException;
45
import org.apache.xmlgraphics.image.loader.ImageInfo;
48
import org.apache.xmlgraphics.image.loader.ImageInfo;
46
import org.apache.xmlgraphics.image.loader.ImageManager;
49
import org.apache.xmlgraphics.image.loader.ImageManager;
Lines 739-751 Link Here
739
    private void setupPage(PageViewport page) {
742
    private void setupPage(PageViewport page) {
740
        this.pdfResources = this.pdfDoc.getResources();
743
        this.pdfResources = this.pdfDoc.getResources();
741
744
742
        Rectangle2D bounds = page.getViewArea();
745
        PageBoundaries boundaries = new PageBoundaries(page.getViewArea().getSize(), page.getForeignAttributes());
743
        double w = bounds.getWidth();
746
744
        double h = bounds.getHeight();
747
        Rectangle trimBox = boundaries.getTrimBox();
745
        currentPage = this.pdfDoc.getFactory().makePage(
748
        Rectangle bleedBox = boundaries.getBleedBox();
746
            this.pdfResources,
749
        Rectangle mediaBox = boundaries.getMediaBox();
747
            (int) Math.round(w / 1000), (int) Math.round(h / 1000),
750
        Rectangle cropBox = boundaries.getCropBox();
748
            page.getPageIndex());
751
752
        // set scale attributes
753
        double scaleX = 1;
754
        double scaleY = 1;
755
        String scale = (String) page.getForeignAttributes().get(
756
                PageScale.EXT_PAGE_SCALE);
757
        Point2D scales = PageScale.getScale(scale);
758
        if (scales != null) {
759
            scaleX = scales.getX();
760
            scaleY = scales.getY();
761
        }
762
763
//        double w = bounds.getWidth();
764
//        double h = bounds.getHeight();
765
        this.currentPage = this.pdfDoc.getFactory().makePage(
766
                this.pdfResources,
767
                page.getPageIndex(),
768
                toPointAndScale(mediaBox, scaleX, scaleY),
769
                toPointAndScale(cropBox, scaleX, scaleY),
770
                toPointAndScale(bleedBox, scaleX, scaleY),
771
                toPointAndScale(trimBox, scaleX, scaleY));
772
773
//        currentPage = this.pdfDoc.getFactory().makePage(
774
//            this.pdfResources,
775
//            (int) Math.round(w / 1000), (int) Math.round(h / 1000),
776
//            page.getPageIndex());
749
        pageReferences.put(page.getKey(), currentPage.referencePDF());
777
        pageReferences.put(page.getKey(), currentPage.referencePDF());
750
        pvReferences.put(page.getKey(), page);
778
        pvReferences.put(page.getKey(), page);
751
        
779
        
Lines 763-768 Link Here
763
        //expressed in a more space-efficient way
791
        //expressed in a more space-efficient way
764
        nums.put(page.getPageIndex(), dict);
792
        nums.put(page.getPageIndex(), dict);
765
    }
793
    }
794
795
    private Double toPointAndScale(Rectangle box, double scaleX, double scaleY) {
796
        return new Rectangle2D.Double(box.getX() * scaleX / 1000,
797
                box.getY() * scaleY / 1000,
798
                box.getWidth() * scaleX / 1000,
799
                box.getHeight() * scaleY / 1000);
800
    }
766
    
801
    
767
    /**
802
    /**
768
     * This method creates a pdf stream for the current page
803
     * This method creates a pdf stream for the current page
Lines 781-789 Link Here
781
        }
816
        }
782
        currentPageRef = currentPage.referencePDF();
817
        currentPageRef = currentPage.referencePDF();
783
818
784
        Rectangle2D bounds = page.getViewArea();
819
        Rectangle bounds = page.getViewArea();
785
        double h = bounds.getHeight();
820
        // set scale attributes
786
        pageHeight = (int) h;
821
        double scaleX = 1;
822
        double scaleY = 1;
823
        String scale = (String) page.getForeignAttributes().get(
824
                PageScale.EXT_PAGE_SCALE);
825
        Point2D scales = PageScale.getScale(scale);
826
        if (scales != null) {
827
            scaleX = scales.getX();
828
            scaleY = scales.getY();
829
        }
830
        pageHeight = bounds.height;
787
831
788
        currentStream = this.pdfDoc.getFactory()
832
        currentStream = this.pdfDoc.getFactory()
789
            .makeStream(PDFFilterList.CONTENT_FILTER, false);
833
            .makeStream(PDFFilterList.CONTENT_FILTER, false);
Lines 791-797 Link Here
791
        currentState = new PDFState();
835
        currentState = new PDFState();
792
        // Transform the PDF's default coordinate system (0,0 at lower left) to the PDFRenderer's
836
        // Transform the PDF's default coordinate system (0,0 at lower left) to the PDFRenderer's
793
        AffineTransform basicPageTransform = new AffineTransform(1, 0, 0, -1, 0,
837
        AffineTransform basicPageTransform = new AffineTransform(1, 0, 0, -1, 0,
794
                pageHeight / 1000f);
838
                (scaleY * pageHeight) / 1000f);
839
        basicPageTransform.scale(scaleX, scaleY);
795
        currentState.concatenate(basicPageTransform);
840
        currentState.concatenate(basicPageTransform);
796
        currentStream.add(CTMHelper.toPDFString(basicPageTransform, false) + " cm\n");
841
        currentStream.add(CTMHelper.toPDFString(basicPageTransform, false) + " cm\n");
797
        
842
        
(-)src/java/org/apache/fop/render/ps/PSRenderer.java (-2 / +2 lines)
Lines 1128-1135 Link Here
1128
                {page.getPageNumberString(),
1128
                {page.getPageNumberString(),
1129
                 new Integer(this.currentPageNumber)});
1129
                 new Integer(this.currentPageNumber)});
1130
1130
1131
        double pageWidth = Math.round(page.getViewArea().getWidth()) / 1000f;
1131
        double pageWidth = page.getViewArea().width / 1000f;
1132
        double pageHeight = Math.round(page.getViewArea().getHeight()) / 1000f;
1132
        double pageHeight = page.getViewArea().height / 1000f;
1133
        boolean rotate = false;
1133
        boolean rotate = false;
1134
        List pageSizes = new java.util.ArrayList();
1134
        List pageSizes = new java.util.ArrayList();
1135
        if (this.autoRotateLandscape && (pageHeight < pageWidth)) {
1135
        if (this.autoRotateLandscape && (pageHeight < pageWidth)) {
(-)src/java/org/apache/fop/util/ConversionUtils.java (+112 lines)
Line 0 Link Here
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
/* $Id: ConversionUtils.java 746664 2009-02-22 12:40:44Z jeremias $ */
19
20
package org.apache.fop.util;
21
22
/**
23
 * This class contains utility methods for conversions, like
24
 * a java.lang.String to an array of int or double.
25
 */
26
public final class ConversionUtils {
27
28
    /**
29
     * Converts the given base <code>String</code> into
30
     * an array of <code>int</code>, splitting the base along the
31
     * given separator pattern.
32
     * <em>Note: this method assumes the input is a string containing
33
     * only decimal integers, signed or unsigned, that are parsable
34
     * by <code>java.lang.Integer.parseInt(String)</code>. If this
35
     * is not the case, the resulting <code>NumberFormatException</code>
36
     * will have to be handled by the caller.</em>
37
     *
38
     * @param baseString    the base string
39
     * @param separatorPattern  the pattern separating the integer values
40
     *        (if this is <code>null</code>, the baseString is parsed as one
41
     *         integer value)
42
     * @return  an array of <code>int</code> whose size is equal to the number
43
     *          values in the input string; <code>null</code> if this number
44
     *          is equal to zero.
45
     */
46
    public static int[] toIntArray(String baseString, String separatorPattern) {
47
48
        if (baseString == null || "".equals(baseString)) {
49
            return null;
50
        }
51
52
        if (separatorPattern == null || "".equals(separatorPattern)) {
53
            return new int[] {Integer.parseInt(baseString)};
54
        }
55
56
        String[] values = baseString.split(separatorPattern);
57
        int numValues = values.length;
58
        if (numValues == 0) {
59
            return null;
60
        }
61
62
        int[] returnArray = new int[numValues];
63
        for (int i = 0; i < numValues; ++i) {
64
            returnArray[i] = Integer.parseInt(values[i]);
65
        }
66
        return returnArray;
67
68
    }
69
70
    /**
71
     * Converts the given base <code>String</code> into
72
     * an array of <code>double</code>, splitting the base along the
73
     * given separator pattern.
74
     * <em>Note: this method assumes the input is a string containing
75
     * only decimal doubles, signed or unsigned, that are parsable
76
     * by <code>java.lang.Double.parseDouble(String)</code>. If this
77
     * is not the case, the resulting <code>NumberFormatException</code>
78
     * will have to be handled by the caller.</em>
79
     *
80
     * @param baseString    the base string
81
     * @param separatorPattern  the pattern separating the integer values
82
     *        (if this is <code>null</code>, the baseString is parsed as one
83
     *         double value)
84
     * @return  an array of <code>double</code> whose size is equal to the number
85
     *          values in the input string; <code>null</code> if this number
86
     *          is equal to zero.
87
     */
88
    public static double[] toDoubleArray(String baseString, String separatorPattern) {
89
90
        if (baseString == null || "".equals(baseString)) {
91
            return null;
92
        }
93
94
        if (separatorPattern == null || "".equals(separatorPattern)) {
95
            return new double[] {Double.parseDouble(baseString)};
96
        }
97
98
        String[] values = baseString.split(separatorPattern);
99
        int numValues = values.length;
100
        if (numValues == 0) {
101
            return null;
102
        }
103
104
        double[] returnArray = new double[numValues];
105
        for (int i = 0; i < numValues; ++i) {
106
            returnArray[i] = Double.parseDouble(values[i]);
107
        }
108
        return returnArray;
109
110
    }
111
112
}
(-)src/java/org/apache/fop/util/XMLConstants.java (+52 lines)
Line 0 Link Here
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
/* $Id: XMLConstants.java 746664 2009-02-22 12:40:44Z jeremias $ */
19
20
package org.apache.fop.util;
21
22
23
/**
24
 * A collection of constants for XML handling.
25
 */
26
public interface XMLConstants {
27
28
    /** "CDATA" constant */
29
    String CDATA = "CDATA";
30
31
    /** XML namespace prefix */
32
    String XML_PREFIX = "xml";
33
    /** XML namespace URI */
34
    String XML_NAMESPACE = "http://www.w3.org/XML/1998/namespace";
35
    /** xml:space attribute */
36
    org.apache.xmlgraphics.util.QName XML_SPACE = new org.apache.xmlgraphics.util.QName(
37
            XML_NAMESPACE, XML_PREFIX, "space");
38
39
    /** XMLNS namespace prefix */
40
    String XMLNS_PREFIX = "xmlns";
41
    /** XMLNS namespace URI */
42
    String XMLNS_NAMESPACE_URI = "http://www.w3.org/2000/xmlns/";
43
44
    /** Namespace prefix for XLink */
45
    String XLINK_PREFIX = "xlink";
46
    /** XML namespace for XLink */
47
    String XLINK_NAMESPACE = "http://www.w3.org/1999/xlink";
48
    /** xlink:href attribute */
49
    org.apache.xmlgraphics.util.QName XLINK_HREF = new org.apache.xmlgraphics.util.QName(
50
            XLINK_NAMESPACE, XLINK_PREFIX, "href");
51
52
}
(-)src/java/org/apache/fop/util/XMLUtil.java (+173 lines)
Line 0 Link Here
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
/* $Id: XMLUtil.java 820672 2009-10-01 14:48:27Z jeremias $ */
19
20
package org.apache.fop.util;
21
22
import java.awt.Rectangle;
23
import java.awt.geom.Rectangle2D;
24
25
import org.xml.sax.Attributes;
26
import org.xml.sax.SAXException;
27
import org.xml.sax.helpers.AttributesImpl;
28
29
/**
30
 * A collection of utility method for XML handling.
31
 */
32
public class XMLUtil implements XMLConstants {
33
34
    /**
35
     * Returns an attribute value as a boolean value.
36
     * @param attributes the Attributes object
37
     * @param name the name of the attribute
38
     * @param defaultValue the default value if the attribute is not specified
39
     * @return the attribute value as a boolean
40
     */
41
    public static boolean getAttributeAsBoolean(Attributes attributes, String name,
42
            boolean defaultValue) {
43
        String s = attributes.getValue(name);
44
        if (s == null) {
45
            return defaultValue;
46
        } else {
47
            return Boolean.valueOf(s).booleanValue();
48
        }
49
    }
50
51
    /**
52
     * Returns an attribute value as a int value.
53
     * @param attributes the Attributes object
54
     * @param name the name of the attribute
55
     * @param defaultValue the default value if the attribute is not specified
56
     * @return the attribute value as an int
57
     */
58
    public static int getAttributeAsInt(Attributes attributes, String name,
59
            int defaultValue) {
60
        String s = attributes.getValue(name);
61
        if (s == null) {
62
            return defaultValue;
63
        } else {
64
            return Integer.parseInt(s);
65
        }
66
    }
67
68
    /**
69
     * Returns an attribute value as a int value.
70
     * @param attributes the Attributes object
71
     * @param name the name of the attribute
72
     * @return the attribute value as an int
73
     * @throws SAXException if the attribute is missing
74
     */
75
    public static int getAttributeAsInt(Attributes attributes, String name) throws SAXException {
76
        String s = attributes.getValue(name);
77
        if (s == null) {
78
            throw new SAXException("Attribute '" + name + "' is missing");
79
        } else {
80
            return Integer.parseInt(s);
81
        }
82
    }
83
84
    /**
85
     * Returns an attribute value as a Integer value.
86
     * @param attributes the Attributes object
87
     * @param name the name of the attribute
88
     * @return the attribute value as an Integer or null if the attribute is missing
89
     */
90
    public static Integer getAttributeAsInteger(Attributes attributes, String name) {
91
        String s = attributes.getValue(name);
92
        if (s == null) {
93
            return null;
94
        } else {
95
            return new Integer(s);
96
        }
97
    }
98
99
    /**
100
     * Returns an attribute value as a Rectangle2D value. The string value is expected as 4
101
     * double-precision numbers separated by whitespace.
102
     * @param attributes the Attributes object
103
     * @param name the name of the attribute
104
     * @return the attribute value as an Rectangle2D
105
     */
106
    public static Rectangle2D getAttributeAsRectangle2D(Attributes attributes, String name) {
107
        String s = attributes.getValue(name).trim();
108
        double[] values = ConversionUtils.toDoubleArray(s, "\\s");
109
        if (values.length != 4) {
110
            throw new IllegalArgumentException("Rectangle must consist of 4 double values!");
111
        }
112
        return new Rectangle2D.Double(values[0], values[1], values[2], values[3]);
113
    }
114
115
    /**
116
     * Returns an attribute value as a Rectangle value. The string value is expected as 4
117
     * integer numbers separated by whitespace.
118
     * @param attributes the Attributes object
119
     * @param name the name of the attribute
120
     * @return the attribute value as an Rectangle
121
     */
122
    public static Rectangle getAttributeAsRectangle(Attributes attributes, String name) {
123
        String s = attributes.getValue(name);
124
        if (s == null) {
125
            return null;
126
        }
127
        int[] values = ConversionUtils.toIntArray(s.trim(), "\\s");
128
        if (values.length != 4) {
129
            throw new IllegalArgumentException("Rectangle must consist of 4 int values!");
130
        }
131
        return new Rectangle(values[0], values[1], values[2], values[3]);
132
    }
133
134
    /**
135
     * Returns an attribute value as a integer array. The string value is expected as 4
136
     * integer numbers separated by whitespace.
137
     * @param attributes the Attributes object
138
     * @param name the name of the attribute
139
     * @return the attribute value as an int array
140
     */
141
    public static int[] getAttributeAsIntArray(Attributes attributes, String name) {
142
        String s = attributes.getValue(name);
143
        if (s == null) {
144
            return null;
145
        } else {
146
            return ConversionUtils.toIntArray(s.trim(), "\\s");
147
        }
148
    }
149
150
    /**
151
     * Adds an attribute to a given {@link AttributesImpl} instance.
152
     * @param atts the attributes collection
153
     * @param attribute the attribute to add
154
     * @param value the attribute's CDATA value
155
     */
156
    public static void addAttribute(AttributesImpl atts,
157
            org.apache.xmlgraphics.util.QName attribute, String value) {
158
        atts.addAttribute(attribute.getNamespaceURI(),
159
                attribute.getLocalName(), attribute.getQName(), XMLUtil.CDATA, value);
160
    }
161
162
    /**
163
     * Adds an attribute to a given {@link AttributesImpl} instance. The attribute will be
164
     * added in the default namespace.
165
     * @param atts the attributes collection
166
     * @param localName the local name of the attribute
167
     * @param value the attribute's CDATA value
168
     */
169
    public static void addAttribute(AttributesImpl atts, String localName, String value) {
170
        atts.addAttribute("", localName, localName, XMLUtil.CDATA, value);
171
    }
172
173
}
(-)test/java/org/apache/fop/render/extensions/prepress/PageScaleTest.java (+79 lines)
Line 0 Link Here
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
/* $Id: PageScaleTest.java 803440 2009-08-12 10:46:39Z vhennebert $ */
19
20
package org.apache.fop.render.extensions.prepress;
21
22
import java.awt.geom.Point2D;
23
24
import junit.framework.TestCase;
25
26
/**
27
 * Tests for the fox:scale extension property.
28
 */
29
public class PageScaleTest extends TestCase {
30
31
    /**
32
     * Default constructor.
33
     */
34
    public PageScaleTest() {
35
        super();
36
    }
37
38
    /**
39
     * Creates a test case with the given name.
40
     *
41
     * @param name name for the test case
42
     */
43
    public PageScaleTest(String name) {
44
        super(name);
45
    }
46
47
    /** 1 value is used for both x and y. */
48
    public void testScale1() {
49
        Point2D res = PageScale.getScale(".5");
50
        assertEquals(0.5, res.getX(), 0.0);
51
        assertEquals(0.5, res.getY(), 0.0);
52
    }
53
54
    /** Two values, used resp. for x and y. */
55
    public void testScale2() {
56
        Point2D res = PageScale.getScale("1. \t \n 1.2");
57
        assertEquals(1.0, res.getX(), 0.0);
58
        assertEquals(1.2, res.getY(), 0.0);
59
    }
60
61
    /** Scale must not contain units. */
62
    public void testScaleFail() {
63
        try {
64
            PageScale.getScale("0.5mm 0.5cm");
65
            fail("Expected IllegalArgumentException. Scale shouldn't contain units");
66
        } catch (IllegalArgumentException iae) {
67
            // Good!
68
        }
69
    }
70
71
    /** @{code null} is returned when scale is unspecified. */
72
    public void testScaleNull() {
73
        Point2D res = PageScale.getScale(null);
74
        assertNull("Result should be null", res);
75
        res = PageScale.getScale("");
76
        assertNull("Result should be null", res);
77
    }
78
79
}
(-)test/java/org/apache/fop/StandardTestSuite.java (+4 lines)
Lines 28-33 Link Here
28
import org.apache.fop.render.pdf.PDFEncodingTestCase;
28
import org.apache.fop.render.pdf.PDFEncodingTestCase;
29
import org.apache.fop.render.pdf.PDFsRGBSettingsTestCase;
29
import org.apache.fop.render.pdf.PDFsRGBSettingsTestCase;
30
import org.apache.fop.render.rtf.RichTextFormatTestSuite;
30
import org.apache.fop.render.rtf.RichTextFormatTestSuite;
31
import org.apache.fop.render.extensions.prepress.PageBoundariesTest;
32
import org.apache.fop.render.extensions.prepress.PageScaleTest;
31
33
32
/**
34
/**
33
 * Test suite for basic functionality of FOP.
35
 * Test suite for basic functionality of FOP.
Lines 50-55 Link Here
50
        suite.addTest(new TestSuite(PDFsRGBSettingsTestCase.class));
52
        suite.addTest(new TestSuite(PDFsRGBSettingsTestCase.class));
51
        suite.addTest(new TestSuite(TrueTypeAnsiTestCase.class));
53
        suite.addTest(new TestSuite(TrueTypeAnsiTestCase.class));
52
        suite.addTest(RichTextFormatTestSuite.suite());
54
        suite.addTest(RichTextFormatTestSuite.suite());
55
        suite.addTest(new TestSuite(PageBoundariesTest.class));
56
        suite.addTest(new TestSuite(PageScaleTest.class));
53
        //$JUnit-END$
57
        //$JUnit-END$
54
        return suite;
58
        return suite;
55
    }
59
    }

Return to bug 47311