|
| 1 | +/******************************************************************************** |
| 2 | + * Copyright (c) 2020 [Open Lowcode SAS](https://openlowcode.com/) |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0 . |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + ********************************************************************************/ |
| 10 | + |
| 11 | +package org.openlowcode.tools.pdf; |
| 12 | + |
| 13 | +import java.awt.Color; |
| 14 | +import java.io.IOException; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.logging.Logger; |
| 17 | + |
| 18 | +import org.openlowcode.tools.pdf.PDFPage.BoxTextContent; |
| 19 | +import org.openlowcode.tools.pdf.PDFPageBand.PartialPrintFeedback; |
| 20 | + |
| 21 | + |
| 22 | +/** |
| 23 | + * A section of text with portions being potentially formatted with color, bold |
| 24 | + * or italic |
| 25 | + * |
| 26 | + * @author <a href="https://openlowcode.com/" rel="nofollow">Open Lowcode |
| 27 | + * SAS</a> |
| 28 | + * @since 1.3 |
| 29 | + */ |
| 30 | +public class FormattableSectionText |
| 31 | + implements |
| 32 | + PDFPageBandSection { |
| 33 | + private boolean firstprint; |
| 34 | + private boolean compactprint; |
| 35 | + private ArrayList<FormattedText> sectionstoprint; |
| 36 | + |
| 37 | + private int activesectionindex; |
| 38 | + private String activesectionremainingtext; |
| 39 | + |
| 40 | + @SuppressWarnings("unused") |
| 41 | + private static Logger logger = Logger.getLogger(FormattableSectionText.class.getName()); |
| 42 | + |
| 43 | + /** |
| 44 | + * create a formattable section text with provided sections |
| 45 | + * @param sections sections to add in the formattable section text |
| 46 | + * @param compactprint if true, margins between paragraphs are reduced |
| 47 | + */ |
| 48 | + public FormattableSectionText(FormattedText[] sections, boolean compactprint) { |
| 49 | + sectionstoprint = new ArrayList<FormattedText>(); |
| 50 | + if (sections != null) |
| 51 | + for (int i = 0; i < sections.length; i++) |
| 52 | + sectionstoprint.add(sections[i]); |
| 53 | + this.firstprint = true; |
| 54 | + this.compactprint = compactprint; |
| 55 | + if (sectionstoprint.size()>0) { |
| 56 | + activesectionindex=0; |
| 57 | + activesectionremainingtext = sectionstoprint.get(0).getText(); |
| 58 | + } |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * create a formattable section text with provided sections |
| 64 | + * @param compactprint if true, margins between paragraphs are reduced |
| 65 | + */ |
| 66 | + public FormattableSectionText(boolean compactprint) { |
| 67 | + sectionstoprint = new ArrayList<FormattedText>(); |
| 68 | + this.firstprint = true; |
| 69 | + this.compactprint = compactprint; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * adds a FormattedText |
| 74 | + * @param texttoadd |
| 75 | + */ |
| 76 | + public void addFormattedText(FormattedText texttoadd) { |
| 77 | + logger.finest(" >> Adding formattted text "); |
| 78 | + this.sectionstoprint.add(texttoadd); |
| 79 | + if (sectionstoprint.size()==1) { |
| 80 | + logger.finest(" >> index 1, adding text to print = "+sectionstoprint.get(0).getText()); |
| 81 | + activesectionindex=0; |
| 82 | + activesectionremainingtext = sectionstoprint.get(0).getText(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void print( |
| 88 | + PDFPageBand pageband, |
| 89 | + PDFPage currentpage, |
| 90 | + float mmfromtopforsection, |
| 91 | + float leftinmm, |
| 92 | + float rightinmm) throws IOException { |
| 93 | + |
| 94 | + BoxTextContent lastfeedback=null; |
| 95 | + for (int i=0;i<sectionstoprint.size();i++) { |
| 96 | + FormattedText text = sectionstoprint.get(i); |
| 97 | + logger.finest(" >>> starting print full , mm offset = "+(lastfeedback!=null?lastfeedback.getMmWrittenOnLastLine():0)); |
| 98 | + lastfeedback = PDFPage.calculateBoxAndMaybeWriteText(leftinmm,mmfromtopforsection, rightinmm,text.getText(), true,false, |
| 99 | + 0, currentpage, text.getTextType(), false,this.compactprint,text.getColor(), |
| 100 | + (lastfeedback!=null?lastfeedback.getMmWrittenOnLastLine():0), |
| 101 | + (lastfeedback!=null?lastfeedback.getNblines()-1:0), |
| 102 | + (lastfeedback!=null?lastfeedback.getNbparagraph()-1:0)); |
| 103 | + } |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public float getSectionHeight(float leftinmm, float rightinmm) throws IOException { |
| 109 | + if (sectionstoprint.size()==0) return 0; |
| 110 | + BoxTextContent lastfeedback=null; |
| 111 | + for (int i=0;i<sectionstoprint.size();i++) { |
| 112 | + FormattedText text = sectionstoprint.get(i); |
| 113 | + |
| 114 | + lastfeedback = PDFPage.calculateBoxAndMaybeWriteText(leftinmm,0, rightinmm,text.getText(), false,false, |
| 115 | + 0, null, text.getTextType(), false,this.compactprint,text.getColor(), |
| 116 | + (lastfeedback!=null?lastfeedback.getMmWrittenOnLastLine():0), |
| 117 | + (lastfeedback!=null?lastfeedback.getNblines()-1:0), |
| 118 | + (lastfeedback!=null?lastfeedback.getNbparagraph()-1:0)); |
| 119 | + } |
| 120 | + return lastfeedback.getHeight(); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public boolean breakableSection() { |
| 125 | + return true; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public PartialPrintFeedback printPartial( |
| 130 | + PDFPageBand pageband, |
| 131 | + float spaceleft, |
| 132 | + PDFPage currentpage, |
| 133 | + float mmfromtopforsection, |
| 134 | + float leftinmm, |
| 135 | + float rightinmm) throws IOException { |
| 136 | + logger.finest(" >>>> print partial"); |
| 137 | + BoxTextContent lastfeedback = null; |
| 138 | + for (int i=this.activesectionindex;i<this.sectionstoprint.size();i++) { |
| 139 | + FormattedText activetext = this.sectionstoprint.get(i); |
| 140 | + lastfeedback = PDFPage.calculateBoxAndMaybeWriteText(leftinmm,mmfromtopforsection, rightinmm,this.activesectionremainingtext, true,true, |
| 141 | + spaceleft, currentpage, activetext.getTextType(), !firstprint,this.compactprint,activetext.getColor(), |
| 142 | + (lastfeedback!=null?lastfeedback.getMmWrittenOnLastLine():0), |
| 143 | + (lastfeedback!=null?lastfeedback.getNblines()-1:0), |
| 144 | + (lastfeedback!=null?lastfeedback.getNbparagraph()-1:0)); |
| 145 | + logger.finest(" ----> printed one text, nblines = "+lastfeedback.getNblines()+", nbparagraph = "+lastfeedback.getNbparagraph()+", mm = "+lastfeedback.getMmWrittenOnLastLine()); |
| 146 | + firstprint=false; |
| 147 | + if (lastfeedback.getTextleftout().length()>0) { |
| 148 | + this.activesectionremainingtext=lastfeedback.getTextleftout(); |
| 149 | + return new PartialPrintFeedback(0, false); |
| 150 | + } |
| 151 | + if (i<this.sectionstoprint.size()-1) { |
| 152 | + this.activesectionindex++; |
| 153 | + this.activesectionremainingtext=this.sectionstoprint.get(activesectionindex).getText(); |
| 154 | + } |
| 155 | + } |
| 156 | + return new PartialPrintFeedback(mmfromtopforsection+lastfeedback.getHeight(),true); |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public String dropContentSample() { |
| 161 | + return "FormattableSectionText"; |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public void setParentDocument(PDFDocument document) { |
| 166 | + // do nothing |
| 167 | + |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public void initialize() { |
| 172 | + // do nothing |
| 173 | + |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * A text with formatting indicators (bold, italic or color) |
| 178 | + * |
| 179 | + * @author <a href="https://openlowcode.com/" rel="nofollow">Open Lowcode |
| 180 | + * SAS</a> |
| 181 | + * @since 1.3 |
| 182 | + * |
| 183 | + */ |
| 184 | + public static class FormattedText { |
| 185 | + private boolean italic; |
| 186 | + private boolean bold; |
| 187 | + private Color color; |
| 188 | + private String text; |
| 189 | + |
| 190 | + /** |
| 191 | + * creates a formatted text |
| 192 | + * @param italic if true, italic |
| 193 | + * @param bold if true, bold |
| 194 | + * @param color selected color (if null, will be black) |
| 195 | + * @param text text to print with the given formatting |
| 196 | + */ |
| 197 | + public FormattedText(boolean italic, boolean bold, Color color, String text) { |
| 198 | + super(); |
| 199 | + this.italic = italic; |
| 200 | + this.bold = bold; |
| 201 | + this.color = color; |
| 202 | + this.text = text; |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * @return generates the text type as defined in PDFPage |
| 207 | + */ |
| 208 | + public int getTextType() { |
| 209 | + if (italic) if (bold) return PDFPage.TEXTTYPE_PLAIN_BOLD_ITALIC; |
| 210 | + if (italic) return PDFPage.TEXTTYPE_PLAIN_ITALIC; |
| 211 | + if (bold) return PDFPage.TEXTTYPE_PLAIN_BOLD; |
| 212 | + return PDFPage.TEXTTYPE_PLAIN; |
| 213 | + |
| 214 | + |
| 215 | + } |
| 216 | + |
| 217 | + /** |
| 218 | + * @return true if italic |
| 219 | + */ |
| 220 | + public boolean isItalic() { |
| 221 | + return italic; |
| 222 | + } |
| 223 | + |
| 224 | + /** |
| 225 | + * @return true if bold |
| 226 | + */ |
| 227 | + public boolean isBold() { |
| 228 | + return bold; |
| 229 | + } |
| 230 | + |
| 231 | + /** |
| 232 | + * @return true if color |
| 233 | + */ |
| 234 | + public Color getColor() { |
| 235 | + return color; |
| 236 | + } |
| 237 | + |
| 238 | + /** |
| 239 | + * @return the text to print |
| 240 | + */ |
| 241 | + public String getText() { |
| 242 | + return text; |
| 243 | + } |
| 244 | + |
| 245 | + } |
| 246 | +} |
0 commit comments