public class PDFPreFormattingProducerNode implements ProducerNode {
private String contentKey;
private int numLinesBetweenImages;
- private float contentAreaWidthCM;
- private float characterWidthCM;
+ private float contentAreaWidthCM;
+ private float characterWidthCM;
private float pixelWidthCM;
private float lineHeightCM;
characterWidthCM=(new Float(aCharacterWidthCM)).floatValue();
pixelWidthCM=(new Float(aPixelWidthCM)).floatValue();
lineHeightCM=(new Float(aLineHeightCM)).floatValue();
-
+
// float characterWidthCM = 0.17F;
//float contentAreaWidthCM = 16;
//float pixelWidthCM = .03F;
int currentPosition = 0;
//int numLinesBetweenImages=3;
-
-
+
+
int numCharsInAnImagelessRow = (new Float(numLinesBetweenImages * (contentAreaWidthCM/characterWidthCM))).intValue();
boolean outOfText = false;
- ArrayList brokenUpContent = new ArrayList();
-
+ ArrayList brokenUpContent = new ArrayList();
+
EntityList images=DatabaseContentToMedia.getInstance().getImages((EntityContent)entity);
- String theContent = ((EntityContent) entity).getValue("content_data");
+ String theContent = ((EntityContent) entity).getValue("content_data");
if (images == null){
- HashMap row = new HashMap();
- row.put("text",theContent);
- row.put("hasImage","0");
- brokenUpContent.add(row);
+ HashMap row = new HashMap();
+ row.put("text",theContent);
+ row.put("hasImage","0");
+ brokenUpContent.add(row);
}
if (images != null){
- //need to add checks for out of content!
- HashMap row0 = new HashMap();
- if (numCharsInAnImagelessRow>(theContent).length()){
- row0.put("text",theContent);
- outOfText = true;
- }
- else {
- //break on words so we don't split html entities
- int lastSpaceAt = theContent.lastIndexOf(" ",numCharsInAnImagelessRow);
- row0.put("text",theContent.substring(0,lastSpaceAt));
- currentPosition=lastSpaceAt;
- }
- row0.put("hasImage","0");
- brokenUpContent.add(row0);
- aLogger.println("CP1 is "+ currentPosition);
- while(images.hasNext()){
- HashMap row1 = new HashMap();
- HashMap row2 = new HashMap();
- EntityImages currentImage=(EntityImages) images.next();
- float img_width=(new Float(currentImage.getValue("img_width"))).floatValue();
- float img_height=(new Float(currentImage.getValue("img_height"))).floatValue();
-
- //oversize images must be shrunk
- if (img_width>400){
- img_height=(new Float((new Float(img_height*(400.0F/img_width))).intValue())).floatValue();
- img_width=400.0F;
- }
-
-
- //calculate how much text goes in the column(use 8 pixels to pad the column)
- float text_widthCM = contentAreaWidthCM-((img_width+8)*pixelWidthCM);
- float number_of_lines = img_height*pixelWidthCM/lineHeightCM; //don't worry we will make it an int
- //add one line for image description
- int text_amount= (new Float((text_widthCM/characterWidthCM)*(number_of_lines+1))).intValue();
-
- row1.put("text_widthCM",Float.toString(text_widthCM));
-
- row1.put("img_title",currentImage.getValue("title"));
-
- row1.put("img_width",Float.toString(img_width));
- row1.put("img_height",Float.toString(img_height));
-
- aLogger.println("img_width " +Float.toString(img_width));
- aLogger.println("img_height "+Float.toString(img_height));
-
- row1.put("img_src",currentImage.getValue("publish_path"));
- row1.put("hasImage","1");
- if (! outOfText){
- try {
- int lastSpaceAt = theContent.lastIndexOf(" ",currentPosition+text_amount);
- row1.put("text",theContent.substring(currentPosition,lastSpaceAt));
- currentPosition=lastSpaceAt;
- }
- catch (IndexOutOfBoundsException e){
- row1.put("text",theContent.substring(currentPosition));
- outOfText = true;
- }
- }
- aLogger.println("CP2 is "+ currentPosition);
- brokenUpContent.add(row1);
-
- if (! outOfText){
- try {
- int lastSpaceAt = theContent.lastIndexOf(" ",currentPosition+numCharsInAnImagelessRow);
- row2.put("text",theContent.substring(currentPosition,lastSpaceAt));
- currentPosition=lastSpaceAt;
- }
- catch (IndexOutOfBoundsException e){
- row2.put("text",theContent.substring(currentPosition));
- outOfText = true;
- }
- }
- row2.put("hasImage","0");
- brokenUpContent.add(row2);
-
- aLogger.println("CP3 is "+ currentPosition);
- }
- HashMap row3 = new HashMap();
- if (! outOfText){
- row3.put("text",theContent.substring(currentPosition));
- row3.put("hasImage","0");
- brokenUpContent.add(row3);
- }
-
+ //need to add checks for out of content!
+ HashMap row0 = new HashMap();
+ if (numCharsInAnImagelessRow>(theContent).length()){
+ row0.put("text",theContent);
+ outOfText = true;
+ }
+ else {
+ //break on words so we don't split html entities
+ int lastSpaceAt = theContent.lastIndexOf(" ",numCharsInAnImagelessRow);
+ row0.put("text",theContent.substring(0,lastSpaceAt));
+ currentPosition=lastSpaceAt;
+ }
+ row0.put("hasImage","0");
+ brokenUpContent.add(row0);
+ aLogger.println("CP1 is "+ currentPosition);
+ while(images.hasNext()){
+ HashMap row1 = new HashMap();
+ HashMap row2 = new HashMap();
+ EntityImages currentImage=(EntityImages) images.next();
+ float img_width=(new Float(currentImage.getValue("img_width"))).floatValue();
+ float img_height=(new Float(currentImage.getValue("img_height"))).floatValue();
+
+ //oversize images must be shrunk
+ if (img_width>400){
+ img_height=(new Float((new Float(img_height*(400.0F/img_width))).intValue())).floatValue();
+ img_width=400.0F;
+ }
+
+
+ //calculate how much text goes in the column(use 8 pixels to pad the column)
+ float text_widthCM = contentAreaWidthCM-((img_width+8)*pixelWidthCM);
+ float number_of_lines = img_height*pixelWidthCM/lineHeightCM; //don't worry we will make it an int
+ //add one line for image description
+ int text_amount= (new Float((text_widthCM/characterWidthCM)*(number_of_lines+1))).intValue();
+
+ row1.put("text_widthCM",Float.toString(text_widthCM));
+
+ row1.put("img_title",currentImage.getValue("title"));
+
+ row1.put("img_width",Float.toString(img_width));
+ row1.put("img_height",Float.toString(img_height));
+
+ aLogger.println("img_width " +Float.toString(img_width));
+ aLogger.println("img_height "+Float.toString(img_height));
+
+ row1.put("img_src",currentImage.getValue("publish_path"));
+ row1.put("hasImage","1");
+ if (! outOfText){
+ try {
+ int lastSpaceAt = theContent.lastIndexOf(" ",currentPosition+text_amount);
+ row1.put("text",theContent.substring(currentPosition,lastSpaceAt));
+ currentPosition=lastSpaceAt;
+ }
+ catch (IndexOutOfBoundsException e){
+ row1.put("text",theContent.substring(currentPosition));
+ outOfText = true;
+ }
+ }
+ aLogger.println("CP2 is "+ currentPosition);
+ brokenUpContent.add(row1);
+
+ if (! outOfText){
+ try {
+ int lastSpaceAt = theContent.lastIndexOf(" ",currentPosition+numCharsInAnImagelessRow);
+ row2.put("text",theContent.substring(currentPosition,lastSpaceAt));
+ currentPosition=lastSpaceAt;
+ }
+ catch (IndexOutOfBoundsException e){
+ row2.put("text",theContent.substring(currentPosition));
+ outOfText = true;
+ }
+ }
+ row2.put("hasImage","0");
+ brokenUpContent.add(row2);
+
+ aLogger.println("CP3 is "+ currentPosition);
+ }
+ HashMap row3 = new HashMap();
+ if (! outOfText){
+ row3.put("text",theContent.substring(currentPosition));
+ row3.put("hasImage","0");
+ brokenUpContent.add(row3);
+ }
+
}
-
-
-
-
-
+
+
+
+
+
ParameterExpander.setValueForKey(
- aValueMap,
- "data.formatted_content",
- new CachingRewindableIterator(brokenUpContent.iterator())
- );
+ aValueMap,
+ "data.formatted_content",
+ new CachingRewindableIterator(brokenUpContent.iterator())
+ );
}
throw new ProducerFailure(t.getMessage(), t);
}
}
-
- public Set buildVerbSet() {
- return new HashSet();
- }
}