2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with any library licensed under the Apache Software License,
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23 * (or with modified versions of the above that use the same license as the above),
24 * and distribute linked combinations including the two. You must obey the
25 * GNU General Public License in all respects for all of the code used other than
26 * the above mentioned libraries. If you modify this file, you may extend this
27 * exception to your version of the file, but you are not obligated to do so.
28 * If you do not wish to do so, delete this exception statement from your version.
31 package mircoders.producer;
33 import java.io.PrintWriter;
36 import mir.entity.Entity;
37 import mir.entity.EntityBrowser;
38 import mir.entity.adapter.EntityAdapter;
39 import mir.log.LoggerToWriterAdapter;
40 import mir.log.LoggerWrapper;
41 import mir.misc.StringUtil;
42 import mir.producer.ProducerFailure;
43 import mir.producer.ProducerNode;
44 import mir.util.CachingRewindableIterator;
45 import mir.util.HTMLRoutines;
46 import mir.util.ParameterExpander;
47 import mircoders.entity.EntityContent;
48 import mircoders.entity.EntityImages;
49 import mircoders.storage.DatabaseImages;
52 public class PDFPreFormattingProducerNode implements ProducerNode {
53 private String contentKey;
54 private int numLinesBetweenImages;
55 private float contentAreaWidthCM;
56 private float characterWidthCM;
57 private float pixelWidthCM;
58 private float lineHeightCM;
60 public PDFPreFormattingProducerNode(String aContentKey,String someNumLinesBetweenImages,String aContentAreaWidthCM,String aCharacterWidthCM,String aPixelWidthCM,String aLineHeightCM) {
61 contentKey = aContentKey;
62 numLinesBetweenImages=(new Integer(someNumLinesBetweenImages)).intValue();
63 contentAreaWidthCM=(new Float(aContentAreaWidthCM)).floatValue();
64 characterWidthCM=(new Float(aCharacterWidthCM)).floatValue();
65 pixelWidthCM=(new Float(aPixelWidthCM)).floatValue();
66 lineHeightCM=(new Float(aLineHeightCM)).floatValue();
68 // float characterWidthCM = 0.17F;
69 //float contentAreaWidthCM = 16;
70 //float pixelWidthCM = .03F;
71 //float lineHeightCM = .5F;
74 public void produce(Map aValueMap, String aVerb, LoggerWrapper aLogger) throws ProducerFailure {
79 data = ParameterExpander.findValueForKey( aValueMap, contentKey );
81 if (! (data instanceof EntityAdapter)) {
82 throw new ProducerFailure("PDFPreFormattingProducerNode: value of '"+contentKey+"' is not an EntityAdapter, but an " + data.getClass().getName(), null);
85 entity = ((EntityAdapter) data).getEntity();
86 if (! (entity instanceof EntityContent)) {
87 throw new ProducerFailure("PDFPreFormattingProducerNode: value of '"+contentKey+"' is not a content EntityAdapter, but a " + entity.getClass().getName() + " adapter", null);
90 int currentPosition = 0;
92 //int numLinesBetweenImages=3;
97 int numCharsInAnImagelessRow = (new Float(numLinesBetweenImages * (contentAreaWidthCM/characterWidthCM))).intValue();
99 boolean outOfText = false;
101 ArrayList brokenUpContent = new ArrayList();
104 List extraTables = new ArrayList();
105 extraTables.add("content_x_media cxm");
106 Iterator images = new EntityBrowser(
107 DatabaseImages.getInstance(), "i", extraTables,
108 "cxm.content_id="+entity.getId()+"and cxm.media_id=i.id",
109 "i.id desc", 30, -1, 0);
111 String theContent = ((EntityContent) entity).getFieldValue("content_data");
112 //remove pesky characters
113 theContent = HTMLRoutines.encodeXML(theContent);
114 //put in the <BR> tags so we can turn them to empty blocks
115 theContent = StringUtil.convertNewline2Break(theContent);
118 Map row = new HashMap();
119 row.put("text",theContent);
120 row.put("hasImage","0");
121 brokenUpContent.add(row);
124 //need to add checks for out of content!
125 Map row0 = new HashMap();
126 if (numCharsInAnImagelessRow>(theContent).length()){
127 row0.put("text",theContent);
131 //break on words so we don't split html entities
132 int lastSpaceAt = theContent.lastIndexOf(" ",numCharsInAnImagelessRow);
133 row0.put("text",theContent.substring(0,lastSpaceAt));
134 currentPosition=lastSpaceAt;
136 row0.put("hasImage","0");
137 brokenUpContent.add(row0);
138 aLogger.debug("CP1 is "+ currentPosition);
139 while(images.hasNext()){
140 Map row1 = new HashMap();
141 Map row2 = new HashMap();
142 EntityImages currentImage=(EntityImages) images.next();
143 float img_width=(new Float(currentImage.getFieldValue("img_width"))).floatValue();
144 float img_height=(new Float(currentImage.getFieldValue("img_height"))).floatValue();
146 //oversize images must be shrunk
148 img_height=(new Float((new Float(img_height*(250.0F/img_width))).intValue())).floatValue();
153 //calculate how much text goes in the column(use 8 pixels to pad the column)
154 float text_widthCM = contentAreaWidthCM-((img_width+8)*pixelWidthCM);
155 float number_of_lines = img_height*pixelWidthCM/lineHeightCM; //don't worry we will make it an int
156 //add one line for image description
157 int text_amount= (new Float((text_widthCM/characterWidthCM)*(number_of_lines+1))).intValue();
159 row1.put("text_widthCM",Float.toString(text_widthCM));
161 row1.put("img_title",currentImage.getFieldValue("title"));
163 row1.put("img_width",Float.toString(img_width));
164 row1.put("img_height",Float.toString(img_height));
166 aLogger.debug("img_width " +Float.toString(img_width));
167 aLogger.debug("img_height "+Float.toString(img_height));
169 row1.put("img_src",currentImage.getFieldValue("publish_path"));
170 row1.put("hasImage","1");
173 int lastSpaceAt = theContent.lastIndexOf(" ",currentPosition+text_amount);
174 row1.put("text",theContent.substring(currentPosition,lastSpaceAt));
175 currentPosition=lastSpaceAt;
177 catch (IndexOutOfBoundsException e){
178 row1.put("text",theContent.substring(currentPosition));
182 aLogger.debug("CP2 is "+ currentPosition);
183 brokenUpContent.add(row1);
187 int lastSpaceAt = theContent.lastIndexOf(" ",currentPosition+numCharsInAnImagelessRow);
188 row2.put("text",theContent.substring(currentPosition,lastSpaceAt));
189 currentPosition=lastSpaceAt;
191 catch (IndexOutOfBoundsException e){
192 row2.put("text",theContent.substring(currentPosition));
196 row2.put("hasImage","0");
197 brokenUpContent.add(row2);
199 aLogger.debug("CP3 is "+ currentPosition);
201 Map row3 = new HashMap();
203 row3.put("text",theContent.substring(currentPosition));
204 row3.put("hasImage","0");
205 brokenUpContent.add(row3);
214 ParameterExpander.setValueForKey(
216 "data.formatted_content",
217 new CachingRewindableIterator(brokenUpContent.iterator())
222 catch (Throwable t) {
223 aLogger.error("Error while formatting content for PDF: " + t.getMessage());
224 t.printStackTrace(new PrintWriter(new LoggerToWriterAdapter(aLogger, LoggerWrapper.DEBUG_MESSAGE)));