View Javadoc

1   /*
2    * Copyright 1999-2006 Faculty of Mathematics, Physics and Informatics, Comenius
3    * University, Bratislava. This file is protected by the Mozilla Public License
4    * version 1.1 (the License); you may not use this file except in compliance
5    * with the License. You may obtain a copy of the License at
6    * http://euromath2.sourceforge.net/license.html Unless required by applicable
7    * law or agreed to in writing, software distributed under the License is
8    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
9    * KIND, either express or implied. See the License for the specific language
10   * governing permissions and limitations under the License.
11   */
12  package sk.uniba.euromath.foRenderer;
13  
14  import java.awt.geom.Rectangle2D;
15  import java.io.IOException;
16  import java.io.OutputStream;
17  import java.util.ArrayList;
18  import java.util.List;
19  
20  import org.apache.fop.apps.FOPException;
21  import org.apache.fop.area.Area;
22  import org.apache.fop.area.CTM;
23  import org.apache.fop.area.Page;
24  import org.apache.fop.area.PageViewport;
25  import org.apache.fop.area.RegionViewport;
26  import org.apache.fop.fo.Constants;
27  import org.apache.fop.fonts.FontInfo;
28  import org.apache.fop.render.AbstractRenderer;
29  import org.eclipse.core.runtime.IStatus;
30  import org.eclipse.swt.graphics.FontData;
31  import org.eclipse.swt.widgets.Display;
32  
33  import sk.uniba.euromath.EuroMath;
34  import sk.uniba.euromath.foRenderer.figures.FOPFigure;
35  import sk.uniba.euromath.foRenderer.figures.FOPFigureFactory;
36  import sk.uniba.euromath.foRenderer.figures.PageDescriptor;
37  import sk.uniba.euromath.foRenderer.figures.PageFigure;
38  import sk.uniba.euromath.foRenderer.figures.TextAreaFigure;
39  import sk.uniba.euromath.foTools.SWTFontRepository;
40  
41  /***
42   * Renderer for FOP areas into Draw2d figures.
43   * 
44   * @author Martin Kollar, Tomáš Studva 23.8.2005
45   */
46  public class Draw2dRenderer extends AbstractRenderer implements
47                  IRenderingStateProvider {
48          /***
49           * Available fonts for rendering, configured by this renderer.
50           */
51          protected FontInfo fontInfo;
52  
53          /***
54           * Repository of all available fonts - SWT fonts.
55           */
56          protected SWTFontRepository fontRepository;
57  
58          protected List<PageFigure> pageList = new ArrayList<PageFigure>();
59  
60          protected FOPFigureFactory figureFactory;
61  
62          /***
63           * FO renderer instance.
64           */
65          protected final FORenderer renderer;
66  
67          public Draw2dRenderer(FORenderer renderer) {
68                  this.renderer = renderer;
69                  this.figureFactory = new FOPFigureFactory(this);
70          }
71  
72          private void firePageCreated(PageFigure page) {
73  
74          }
75  
76          /***
77           * @param page
78           *                FO PageViewPort
79           * @throws IOException
80           * @throws FOPException
81           */
82          public void renderPage(PageViewport pageViewport) throws IOException,
83                          FOPException {
84                  PageDescriptor desc = new PageDescriptor(pageViewport);
85                  PageFigure newPageFigure = new PageFigure(desc.getPageSize(),
86                                  desc.getMargin());
87                  super.renderPage(pageViewport);
88                  this.pageList.add(newPageFigure);
89                  firePageCreated(newPageFigure);
90  
91                  int viewports[] = { Constants.FO_REGION_BEFORE,
92                                  Constants.FO_REGION_START,
93                                  Constants.FO_REGION_BODY,
94                                  Constants.FO_REGION_END,
95                                  Constants.FO_REGION_AFTER };
96                  RegionViewport regionViewport;
97  
98                  // creates figureTree for not null regionViewPorts
99                  Page page = desc.getPage();
100                 if (page != null) {
101                         for (int i = 0; i < viewports.length; i++) {
102                                 regionViewport = page
103                                                 .getRegionViewport(viewports[i]);
104                                 if (regionViewport != null) {
105                                         FOPFigure figure = makeFigureTree(regionViewport);
106                                         newPageFigure.add(figure);
107                                 }
108                         }
109                 }
110         }
111 
112         protected FOPFigure makeFigureTree(Area area) {
113                 FOPFigure parentFigure = getFactory().buildFigure(area);
114 
115                 List childAreas = getFactory().getBuilder(area.getClass())
116                                 .getChildrenAreas();
117                 if (parentFigure instanceof TextAreaFigure)
118                         childAreas.clear();
119                 for (Object childArea : childAreas) {
120                         FOPFigure figure = makeFigureTree((Area) childArea);
121                         // parentFigure.add(figure);
122                         if (figure instanceof TextAreaFigure) {
123                                 // figure.setID(parentFigure.getID());
124                                 // if (figure.getID() == null) {
125                                 // this can happen when TextArea is not
126                                 // encapsulated with
127                                 // InlineParentExt <fo:pagenumber/> or some
128                                 // texts made after
129                                 // xslt transformation
130                                 // }
131                         }
132                 }
133                 return parentFigure;
134         }
135 
136         /***
137          * Setups font info by available SWT fonts, by helper SWT repository.
138          * Renderer is responsible prior rendering to provide and setup
139          * available fonts for rendering. Later in pre-rendering is checked by
140          * fop, if neeeded fonts are available, if not then missing is replaced
141          * by some another.<br>
142          * Available fonts are directly mapped to system fonts, no other font
143          * is done, except defaul font.
144          */
145         public void setupFontInfo(FontInfo fontInfo) {
146                 this.fontInfo = fontInfo;
147                 this.fontRepository = new SWTFontRepository(Display
148                                 .getCurrent());
149                 // font registration counter
150                 int fontIndex = 0;
151 
152                 // setup default font
153                 this.fontInfo.addFontProperties("F0", "any", "normal", 400);
154                 this.fontInfo.addMetrics("F0", this.fontRepository
155                                 .getMetrics(this.fontRepository
156                                                 .getDefaultFontData()));
157                 fontIndex++;
158 
159                 // setup all available fonts
160                 for (FontData fontData : this.fontRepository
161                                 .getAvailableFontDatas()) {
162                         fontInfo
163                                         .addFontProperties(
164                                                         "F" + fontIndex,
165                                                         SWTFontRepository
166                                                                         .getFontTriplet(fontData));
167                         fontInfo.addMetrics("F" + fontIndex,
168                                         this.fontRepository
169                                                         .getMetrics(fontData));
170                         fontIndex++;
171                 }
172         }
173 
174         /***
175          * SWT font repository getter.
176          * 
177          * @return SWT font repository
178          */
179         public SWTFontRepository getFontRepository() {
180                 return this.fontRepository;
181         }
182 
183         /*
184          * (non-Javadoc)
185          * 
186          * @see sk.uniba.euromath.fo.RenderingStateProvider#getBlockIP()
187          */
188         public int getBlockIP() {
189                 // return currentBlockIPPosition;
190                 return 0;
191         }
192 
193         /*
194          * (non-Javadoc)
195          * 
196          * @see sk.uniba.euromath.fo.RenderingStateProvider#getBP()
197          */
198         public int getBP() {
199                 return currentBPPosition;
200         }
201 
202         /*
203          * (non-Javadoc)
204          * 
205          * @see sk.uniba.euromath.fo.RenderingStateProvider#getContainingBP()
206          */
207         public int getContainingBP() {
208                 return containingBPPosition;
209         }
210 
211         /*
212          * (non-Javadoc)
213          * 
214          * @see sk.uniba.euromath.fo.RenderingStateProvider#getContainingIP()
215          */
216         public int getContainingIP() {
217                 return containingIPPosition;
218         }
219 
220         /*
221          * (non-Javadoc)
222          * 
223          * @see sk.uniba.euromath.fo.RenderingStateProvider#getFontInfo()
224          */
225         public FontInfo getFontInfo() {
226                 return fontInfo;
227         }
228 
229         /*
230          * (non-Javadoc)
231          * 
232          * @see sk.uniba.euromath.fo.RenderingStateProvider#getIP()
233          */
234         public int getIP() {
235                 return currentIPPosition;
236         }
237 
238         /*
239          * (non-Javadoc)
240          * 
241          * @see org.apache.fop.render.Renderer#startRenderer(java.io.OutputStream)
242          */
243         public void startRenderer(OutputStream outputStream) throws IOException {
244                 super.startRenderer(outputStream);
245                 this.pageList.clear();
246         }
247 
248         private FOPFigureFactory getFactory() {
249                 return this.figureFactory;
250         }
251 
252         /*
253          * (non-Javadoc)
254          * 
255          * @see org.apache.fop.render.AbstractRenderer#endVParea()
256          */
257         @Override
258         protected void endVParea() {
259                 // TODO Studva> Tato metoda pribudla v predkovi, pozri sa na nu
260                 // plz
261         }
262 
263         /*
264          * (non-Javadoc)
265          * 
266          * @see org.apache.fop.render.AbstractRenderer#startVParea(org.apache.fop.area.CTM,
267          *      java.awt.geom.Rectangle2D)
268          */
269         @Override
270         protected void startVParea(CTM ctm, Rectangle2D clippingRect) {
271                 // TODO Studva> Tato metoda pribudla v predkovi, pozri sa na nu
272                 // plz
273 
274         }
275 
276         private void log(String logText) {
277                 EuroMath.log(IStatus.INFO, 0, logText, null);
278         }
279 
280         private void log(Exception e) {
281                 EuroMath.log(IStatus.ERROR, 0, "error encountered", e);
282         }
283 
284         /***
285          * @return list of Pages
286          */
287         public List<PageFigure> getPages() {
288                 return this.pageList;
289         }
290 
291         /***
292          * Getter for FO renderer.
293          * 
294          * @return FO renderer instance.
295          */
296         public FORenderer getFORenderer() {
297                 return this.renderer;
298         }
299 
300 }