aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/apache/fop/fo/pagination/SimplePageMaster.java
blob: 243474bb5177bc95e56d1ade9e60372ac1d759f8 (plain)
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
/*
 * $Id$
 * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
 * For details on use and redistribution please refer to the
 * LICENSE file included with these sources.
 */

package org.apache.fop.fo.pagination;

// FOP
import org.apache.fop.fo.*;
import org.apache.fop.fo.properties.*;
import org.apache.fop.area.CTM;
import org.apache.fop.datatypes.FODimension;
import org.apache.fop.area.PageViewport;
import org.apache.fop.area.Page;
import org.apache.fop.area.RegionViewport;
import org.apache.fop.area.RegionReference;
import org.apache.fop.layout.MarginProps;
import org.apache.fop.layout.PageMaster;
import org.apache.fop.apps.FOPException;

import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.util.HashMap;
import java.util.Iterator;
import org.xml.sax.Attributes;

/**
 * A simple-page-master formatting object.
 * This creates a simple page from the specified regions
 * and attributes.
 */
public class SimplePageMaster extends FObj {
    /**
     * Page regions (regionClass, Region)
     */
    private HashMap _regions;

    PageMaster pageMaster;
    String masterName;

    public SimplePageMaster(FONode parent) {
        super(parent);
    }

    public void handleAttrs(Attributes attlist) throws FOPException {
        super.handleAttrs(attlist);

        if (parent.getName().equals("fo:layout-master-set")) {
            LayoutMasterSet layoutMasterSet = (LayoutMasterSet)parent;
            masterName = this.properties.get("master-name").getString();
            if (masterName == null) {
                getLogger().warn("simple-page-master does not have "
                                       + "a master-name and so is being ignored");
            } else {
                layoutMasterSet.addSimplePageMaster(this);
            }
        } else {
            throw new FOPException("fo:simple-page-master must be child "
                                   + "of fo:layout-master-set, not "
                                   + parent.getName());
        }
        _regions = new HashMap();
    }

    /**
     * At the end of this element read all the information and create
     * the page master.
     */
    protected void end() {
        int pageWidth =
            this.properties.get("page-width").getLength().mvalue();
        int pageHeight =
            this.properties.get("page-height").getLength().mvalue();
        // this.properties.get("reference-orientation");
        // this.properties.get("writing-mode");

        // Get absolute margin properties (top, left, bottom, right)
        MarginProps mProps = propMgr.getMarginProps();

    /* Create the page reference area rectangle in first quadrant coordinates
     * (ie, 0,0 is at bottom,left of the "page media" and y increases
     * when moving towards the top of the page.
     * The media rectangle itself is (0,0,pageWidth,pageHeight).
     */
    Rectangle pageRefRect =
        new Rectangle(mProps.marginLeft, mProps.marginTop,
              pageWidth - mProps.marginLeft - mProps.marginRight,
              pageHeight - mProps.marginTop - mProps.marginBottom);

    // ??? KL shouldn't this take the viewport too???
    Page page = new Page();  // page reference area

        // Set up the CTM on the page reference area based on writing-mode
        // and reference-orientation
    FODimension reldims=new FODimension(0,0);
    CTM pageCTM = propMgr.getCTMandRelDims(pageRefRect, reldims);

    // Create a RegionViewport/ reference area pair for each page region

    boolean bHasBody=false;

        for (Iterator regenum = _regions.values().iterator();
                regenum.hasNext(); ) {
            Region r = (Region)regenum.next();
        RegionViewport rvp = r.makeRegionViewport(reldims, pageCTM);
        rvp.setRegion(r.makeRegionReferenceArea(rvp.getViewArea()));
        page.setRegion(r.getRegionAreaClass(), rvp);
        if (r.getRegionAreaClass() == RegionReference.BODY) {
        bHasBody = true;
        }
        }

    if (!bHasBody) {
            getLogger().error("simple-page-master has no region-body");
        }

    this.pageMaster = new PageMaster(new PageViewport(page,
                       new Rectangle(0,0,
                             pageWidth,pageHeight)));

    //  _regions = null; // PageSequence access SimplePageMaster....
        children = null;
        properties = null;
    }

    public boolean generatesReferenceAreas() {
        return true;
    }

    public PageMaster getPageMaster() {
        return this.pageMaster;
    }

    public PageMaster getNextPageMaster() {
        return this.pageMaster;
    }

    public String getMasterName() {
        return masterName;
    }

    protected void addChild(FONode child) {
        if (child instanceof Region) {
            addRegion((Region)child);
        } else {
            getLogger().error("SimplePageMaster cannot have child of type " +
                child.getName());
        }
    }

    protected void addRegion(Region region) {
        String key = region.getRegionClass();
        if (_regions.containsKey(key)) {
            getLogger().error("Only one region of class "
                              + key
                              + " allowed within a simple-page-master.");
            // throw new FOPException("Only one region of class "
//                                    + key
//                                    + " allowed within a simple-page-master.");
        } else {
            _regions.put(key, region);
        }
    }

    public Region getRegion(String regionClass) {
        return (Region)_regions.get(regionClass);
    }

    protected HashMap getRegions() {
        return _regions;
    }

    protected boolean regionNameExists(String regionName) {
        for (Iterator regenum = _regions.values().iterator();
                regenum.hasNext(); ) {
            Region r = (Region)regenum.next();
            if (r.getRegionName().equals(regionName)) {
                return true;
            }
        }
        return false;
    }
}