43dd3c508ddf042d21f9ff1bdb32e35b1e70aadf
[mir.git] / source / mircoders / producer / PDFPreFormattingProducerNode.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with the com.oreilly.servlet library, any library
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24  * the above that use the same license as the above), and distribute linked
25  * combinations including the two.  You must obey the GNU General Public
26  * License in all respects for all of the code used other than the above
27  * mentioned libraries.  If you modify this file, you may extend this exception
28  * to your version of the file, but you are not obligated to do so.  If you do
29  * not wish to do so, delete this exception statement from your version.
30  */
31
32 package mircoders.producer;
33
34 import java.util.*;
35 import java.io.*;
36 import mir.util.*;
37 import mir.producer.*;
38 import mir.entity.*;
39 import mir.entity.adapter.*;
40 import mircoders.entity.*;
41 import mircoders.storage.*;
42
43 //because images are returned as a template model!(maybe not needed after all!)
44 //import freemarker.template.*;
45
46
47 public class PDFPreFormattingProducerNode implements ProducerNode {
48   private String contentKey;
49   private int numLinesBetweenImages;
50   private float contentAreaWidthCM;  
51   private float characterWidthCM;  
52   private float pixelWidthCM;
53   private float lineHeightCM;
54
55   public PDFPreFormattingProducerNode(String aContentKey,String someNumLinesBetweenImages,String aContentAreaWidthCM,String aCharacterWidthCM,String aPixelWidthCM,String aLineHeightCM) {
56     contentKey = aContentKey;
57     numLinesBetweenImages=(new Integer(someNumLinesBetweenImages)).intValue();
58     contentAreaWidthCM=(new Float(aContentAreaWidthCM)).floatValue();
59     characterWidthCM=(new Float(aCharacterWidthCM)).floatValue();
60     pixelWidthCM=(new Float(aPixelWidthCM)).floatValue();
61     lineHeightCM=(new Float(aLineHeightCM)).floatValue();
62     
63     //    float characterWidthCM = 0.17F;
64     //float contentAreaWidthCM = 16;
65     //float pixelWidthCM = .03F;
66     //float lineHeightCM = .5F;
67   }
68
69   public void produce(Map aValueMap, String aVerb, PrintWriter aLogger) throws ProducerFailure {
70     Object data;
71     Entity entity;
72
73     try {
74       data = ParameterExpander.findValueForKey( aValueMap, contentKey );
75
76       if (! (data instanceof EntityAdapter)) {
77         throw new ProducerFailure("ContentMarkingProducerNode: value of '"+contentKey+"' is not an EntityAdapter, but an " + data.getClass().getName(), null);
78       }
79
80       entity = ((EntityAdapter) data).getEntity();
81       if (! (entity instanceof EntityContent)) {
82         throw new ProducerFailure("ContentMarkingProducerNode: value of '"+contentKey+"' is not a content EntityAdapter, but a " + entity.getClass().getName() + " adapter", null);
83       }
84
85       int currentPosition = 0;
86
87       //int numLinesBetweenImages=3;
88       
89
90
91       
92       int numCharsInAnImagelessRow = (new Float(numLinesBetweenImages * (contentAreaWidthCM/characterWidthCM))).intValue();
93
94       boolean outOfText = false;
95
96       ArrayList brokenUpContent = new ArrayList();  
97       
98
99       EntityList images=DatabaseContentToMedia.getInstance().getImages((EntityContent)entity);
100       String theContent = ((EntityContent) entity).getValue("content_data"); 
101       if (images == null){
102           HashMap row = new HashMap();
103           row.put("text",theContent);
104           row.put("hasImage","0");
105           brokenUpContent.add(row);
106       }
107       if (images != null){
108           //need to add checks for out of content!
109           HashMap row0 = new HashMap();
110           if (numCharsInAnImagelessRow>(theContent).length()){
111               row0.put("text",theContent);
112               outOfText = true;
113           }
114           else {
115               //break on words so we don't split html entities
116               int lastSpaceAt = theContent.lastIndexOf(" ",numCharsInAnImagelessRow);
117               row0.put("text",theContent.substring(0,lastSpaceAt));
118               currentPosition=lastSpaceAt;
119           }
120           row0.put("hasImage","0");
121           brokenUpContent.add(row0);
122           aLogger.println("CP1 is "+ currentPosition);
123           while(images.hasNext()){
124               HashMap row1 = new HashMap();
125               HashMap row2 = new HashMap();
126               EntityImages currentImage=(EntityImages) images.next();
127               float img_width=(new Float(currentImage.getValue("img_width"))).floatValue();
128               float img_height=(new Float(currentImage.getValue("img_height"))).floatValue();
129               
130               //oversize images must be shrunk
131               if (img_width>400){
132                   img_height=(new Float((new Float(img_height*(400.0F/img_width))).intValue())).floatValue();
133                   img_width=400.0F;
134               }
135
136
137               //calculate how much text goes in the column
138               float text_widthCM = contentAreaWidthCM-(img_width*pixelWidthCM);
139               float number_of_lines = img_height*pixelWidthCM/lineHeightCM; //don't worry we will make it an int 
140               int text_amount= (new Float((text_widthCM/characterWidthCM)*number_of_lines)).intValue();
141
142               row1.put("text_widthCM",Float.toString(text_widthCM));
143
144
145
146               row1.put("img_width",Float.toString(img_width));
147               row1.put("img_height",Float.toString(img_height));
148               
149               aLogger.println("img_width " +Float.toString(img_width));
150               aLogger.println("img_height "+Float.toString(img_height));
151               
152               row1.put("img_src",currentImage.getValue("source"));
153               row1.put("hasImage","1");
154               if (! outOfText){
155                   try {
156                       int lastSpaceAt = theContent.lastIndexOf(" ",currentPosition+text_amount);
157                       row1.put("text",theContent.substring(currentPosition,lastSpaceAt));
158                       currentPosition=lastSpaceAt;
159                   }
160                   catch (IndexOutOfBoundsException e){
161                       row1.put("text",theContent.substring(currentPosition));
162                       outOfText = true;
163                           }
164               }
165               aLogger.println("CP2 is "+ currentPosition);
166               brokenUpContent.add(row1);
167               
168               if (! outOfText){
169                   try {
170                       int lastSpaceAt = theContent.lastIndexOf(" ",currentPosition+numCharsInAnImagelessRow);
171                       row2.put("text",theContent.substring(currentPosition,lastSpaceAt));
172                       currentPosition=lastSpaceAt;
173                   }
174                   catch (IndexOutOfBoundsException e){
175                       row2.put("text",theContent.substring(currentPosition));
176                       outOfText = true;
177                           }
178               }
179               row2.put("hasImage","0");
180               brokenUpContent.add(row2);
181
182               aLogger.println("CP3 is "+ currentPosition);
183           }
184           HashMap row3 = new HashMap();
185           if (! outOfText){
186               row3.put("text",theContent.substring(currentPosition));
187               row3.put("hasImage","0");
188               brokenUpContent.add(row3);
189           }
190           
191       }
192       
193       
194       
195       
196       
197       ParameterExpander.setValueForKey(
198                                        aValueMap,
199                                        "data.formatted_content",
200                                        new CachingRewindableIterator(brokenUpContent.iterator())
201                                        );
202
203
204     }
205     catch (Throwable t) {
206       aLogger.println("Error while formatting content for PDF: " + t.getMessage());
207       t.printStackTrace(aLogger);
208
209       throw new ProducerFailure(t.getMessage(), t);
210     }
211   }
212
213   public Set buildVerbSet() {
214     return new HashSet();
215   }
216 }
217
218
219
220