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;
34 import java.util.ArrayList;
35 import java.util.HashMap;
38 import mir.entity.Entity;
39 import mir.entity.EntityList;
40 import mir.entity.adapter.EntityAdapter;
41 import mir.log.LoggerToWriterAdapter;
42 import mir.log.LoggerWrapper;
43 import mir.misc.StringUtil;
44 import mir.producer.ProducerFailure;
45 import mir.producer.ProducerNode;
46 import mir.util.CachingRewindableIterator;
47 import mir.util.HTMLRoutines;
48 import mir.util.ParameterExpander;
49 import mircoders.entity.EntityContent;
50 import mircoders.entity.EntityImages;
51 import mircoders.storage.DatabaseContentToMedia;
53 //because images are returned as a template model!(maybe not needed after all!)
54 //import freemarker.template.*;
57 public class PDFPreFormattingProducerNode implements ProducerNode {
58 private String contentKey;
59 private int numLinesBetweenImages;
60 private float contentAreaWidthCM;
61 private float characterWidthCM;
62 private float pixelWidthCM;
63 private float lineHeightCM;
65 public PDFPreFormattingProducerNode(String aContentKey,String someNumLinesBetweenImages,String aContentAreaWidthCM,String aCharacterWidthCM,String aPixelWidthCM,String aLineHeightCM) {
66 contentKey = aContentKey;
67 numLinesBetweenImages=(new Integer(someNumLinesBetweenImages)).intValue();
68 contentAreaWidthCM=(new Float(aContentAreaWidthCM)).floatValue();
69 characterWidthCM=(new Float(aCharacterWidthCM)).floatValue();
70 pixelWidthCM=(new Float(aPixelWidthCM)).floatValue();
71 lineHeightCM=(new Float(aLineHeightCM)).floatValue();
73 // float characterWidthCM = 0.17F;
74 //float contentAreaWidthCM = 16;
75 //float pixelWidthCM = .03F;
76 //float lineHeightCM = .5F;
79 public void produce(Map aValueMap, String aVerb, LoggerWrapper aLogger) throws ProducerFailure {
84 data = ParameterExpander.findValueForKey( aValueMap, contentKey );
86 if (! (data instanceof EntityAdapter)) {
87 throw new ProducerFailure("PDFPreFormattingProducerNode: value of '"+contentKey+"' is not an EntityAdapter, but an " + data.getClass().getName(), null);
90 entity = ((EntityAdapter) data).getEntity();
91 if (! (entity instanceof EntityContent)) {
92 throw new ProducerFailure("PDFPreFormattingProducerNode: value of '"+contentKey+"' is not a content EntityAdapter, but a " + entity.getClass().getName() + " adapter", null);
95 int currentPosition = 0;
97 //int numLinesBetweenImages=3;
102 int numCharsInAnImagelessRow = (new Float(numLinesBetweenImages * (contentAreaWidthCM/characterWidthCM))).intValue();
104 boolean outOfText = false;
106 ArrayList brokenUpContent = new ArrayList();
109 EntityList images=DatabaseContentToMedia.getInstance().getImages((EntityContent)entity);
110 String theContent = ((EntityContent) entity).getValue("content_data");
111 //remove pesky characters
112 theContent = HTMLRoutines.encodeXML(theContent);
113 //put in the <BR> tags so we can turn them to empty blocks
114 theContent = StringUtil.convertNewline2Break(theContent);
117 Map row = new HashMap();
118 row.put("text",theContent);
119 row.put("hasImage","0");
120 brokenUpContent.add(row);
123 //need to add checks for out of content!
124 Map row0 = new HashMap();
125 if (numCharsInAnImagelessRow>(theContent).length()){
126 row0.put("text",theContent);
130 //break on words so we don't split html entities
131 int lastSpaceAt = theContent.lastIndexOf(" ",numCharsInAnImagelessRow);
132 row0.put("text",theContent.substring(0,lastSpaceAt));
133 currentPosition=lastSpaceAt;
135 row0.put("hasImage","0");
136 brokenUpContent.add(row0);
137 aLogger.debug("CP1 is "+ currentPosition);
138 while(images.hasNext()){
139 Map row1 = new HashMap();
140 Map row2 = new HashMap();
141 EntityImages currentImage=(EntityImages) images.next();
142 float img_width=(new Float(currentImage.getValue("img_width"))).floatValue();
143 float img_height=(new Float(currentImage.getValue("img_height"))).floatValue();
145 //oversize images must be shrunk
147 img_height=(new Float((new Float(img_height*(250.0F/img_width))).intValue())).floatValue();
152 //calculate how much text goes in the column(use 8 pixels to pad the column)
153 float text_widthCM = contentAreaWidthCM-((img_width+8)*pixelWidthCM);
154 float number_of_lines = img_height*pixelWidthCM/lineHeightCM; //don't worry we will make it an int
155 //add one line for image description
156 int text_amount= (new Float((text_widthCM/characterWidthCM)*(number_of_lines+1))).intValue();
158 row1.put("text_widthCM",Float.toString(text_widthCM));
160 row1.put("img_title",currentImage.getValue("title"));
162 row1.put("img_width",Float.toString(img_width));
163 row1.put("img_height",Float.toString(img_height));
165 aLogger.debug("img_width " +Float.toString(img_width));
166 aLogger.debug("img_height "+Float.toString(img_height));
168 row1.put("img_src",currentImage.getValue("publish_path"));
169 row1.put("hasImage","1");
172 int lastSpaceAt = theContent.lastIndexOf(" ",currentPosition+text_amount);
173 row1.put("text",theContent.substring(currentPosition,lastSpaceAt));
174 currentPosition=lastSpaceAt;
176 catch (IndexOutOfBoundsException e){
177 row1.put("text",theContent.substring(currentPosition));
181 aLogger.debug("CP2 is "+ currentPosition);
182 brokenUpContent.add(row1);
186 int lastSpaceAt = theContent.lastIndexOf(" ",currentPosition+numCharsInAnImagelessRow);
187 row2.put("text",theContent.substring(currentPosition,lastSpaceAt));
188 currentPosition=lastSpaceAt;
190 catch (IndexOutOfBoundsException e){
191 row2.put("text",theContent.substring(currentPosition));
195 row2.put("hasImage","0");
196 brokenUpContent.add(row2);
198 aLogger.debug("CP3 is "+ currentPosition);
200 Map row3 = new HashMap();
202 row3.put("text",theContent.substring(currentPosition));
203 row3.put("hasImage","0");
204 brokenUpContent.add(row3);
213 ParameterExpander.setValueForKey(
215 "data.formatted_content",
216 new CachingRewindableIterator(brokenUpContent.iterator())
221 catch (Throwable t) {
222 aLogger.error("Error while formatting content for PDF: " + t.getMessage());
223 t.printStackTrace(new PrintWriter(new LoggerToWriterAdapter(aLogger, LoggerWrapper.DEBUG_MESSAGE)));