1 package jimm.datavision.layout; 2 import jimm.datavision.*; 3 import jimm.datavision.field.*; 4 import jimm.datavision.layout.LineDrawer; 5 import java.io.*; 6 7 12 public class LaTeXLE extends LayoutEngine implements LineDrawer { 13 14 protected double linethickness; 15 16 public LaTeXLE(PrintWriter out) { 17 super(out); 18 linethickness = 0; 19 } 20 21 24 protected void doStart() { 25 String latexPaperSize = report.getPaperFormat().getLaTeXPaperSizeString(); 26 out.println("\\documentclass[11pt" + 27 (latexPaperSize == null ? "" : ("," + latexPaperSize)) + 28 "]{article}\n" + 29 "\n" + 30 "\\oddsidemargin -1in\n" + 31 "\\evensidemargin -1in\n" + 32 "\\textwidth " + pageWidth() + "pt\n" + 33 "\\headheight -0.5pt\n" + 34 "\\headsep 0pt\n" + 35 "\\topmargin 0.0in\n" + 36 "\\textheight " + pageHeight() + "pt\n" + 37 "\n" + 38 "\\begin{document}\n" + 39 "\\pagestyle{empty}\n" + 40 "\\setlength{\\unitlength}{1pt}\n" + 41 "% Generated by DataVision version " + info.Version + "\n" + 42 "% " + info.URL); 43 } 44 45 48 protected void doEnd() { 49 out.println("\\end{document}"); 50 } 51 52 55 protected void doStartPage() { 56 if (pageNumber > 1) 57 out.println("\\newpage"); 58 out.println("% ======== Page " + pageNumber + " ========"); 59 out.println("\\begin{picture}(" + pageWidth() + "," + pageHeight() + ")"); 60 } 61 62 65 protected void doEndPage() { 66 out.println("\\end{picture}"); 67 } 68 69 74 protected void doOutputField(Field field) { 75 String fieldAsString = field.toString(); 76 if (fieldAsString == null || fieldAsString.length() == 0) { 77 makeBorders(field); 78 out.println(); 79 return; 80 } 81 82 Format format = field.getFormat(); 83 Rectangle bounds = field.getBounds(); 84 85 putField(field); 87 88 String [] size = selectFontSize(format); 91 if (size != null) out.print(size[0]); 92 93 String align = ""; 95 switch (format.getAlign()) { 96 case Format.ALIGN_CENTER: 97 align = "\\begin{center}"; 98 break; 99 case Format.ALIGN_RIGHT: 100 align = "\\raggedleft "; 101 break; 102 } 103 out.print("\\begin{minipage}[t]{" + bounds.width + "pt}" + align); 104 105 110 if (format.isBold()) out.print("\\textbf{"); 112 if (format.isItalic()) out.print("\\textit{"); 113 if (format.isUnderline()) out.print("\\underline{"); 114 115 out.print(makeSafe(fieldAsString)); 117 118 if (format.isUnderline()) out.print("}"); 120 if (format.isItalic()) out.print("}"); 121 if (format.isBold()) out.print("}"); 122 123 if (format.getAlign() == Format.ALIGN_CENTER) out.print("\\end{center}"); 124 125 129 out.print("\\end{minipage}"); 130 131 if (size != null) out.print(size[1]); 132 133 out.print("}"); 135 136 makeBorders(field); 138 139 out.println(); 140 } 141 142 147 protected void doOutputImage(ImageField field) {} 148 149 154 protected void doOutputLine(Line line) { 155 drawLine(line, null); 156 } 157 158 164 protected String [] selectFontSize(Format format) { 165 if (format == null || format.getSize() == 0) 166 return null; 167 String [] size = new String [2]; 168 size[0] = "{\\fontsize{" + format.getSize() + "pt}{" 169 + (format.getSize() * 1.2) + "pt}"; 170 size[1] = "}"; 171 return size; 172 } 173 174 177 protected void makeBorders(Field field) { 178 field.getBorderOrDefault().eachLine(this, null); 179 } 180 181 186 public void drawLine(Line line, Object arg) { 187 Point p0 = line.getPoint(0); 188 Point p1 = line.getPoint(1); 189 double xdiff = p1.x - p0.x; 190 double ydiff = p1.y - p0.y; 191 192 if (xdiff == 0 && ydiff == 0) 193 return; 194 195 double xslope, yslope; 196 if ( ydiff == 0) { if (p1.x < p0.x) 198 xslope = -1; 199 else if (p1.x > p0.x) 200 xslope = 1; 201 else 202 xslope = 0; 203 yslope = 0; 204 } 205 else if (xdiff == 0) { xslope = 0; 207 if (p1.y < p0.y) 208 yslope = 1; 209 else if (p1.y > p0.y) 210 yslope = -1; 211 else 212 yslope = 0; 213 } 214 else { 215 xslope = xdiff; 218 yslope = ydiff; 219 double[] slopes = pickNearestSlope(xslope / yslope); 221 if (slopes == null) { 222 xslope = 1; 223 yslope = 0; 224 } 225 else { 226 xslope = slopes[0]; 227 yslope = slopes[1]; 228 } 229 } 230 setLineThickness(line.getThickness()); 231 putLine(line, xslope, yslope); 232 } 233 234 243 protected double[] pickNearestSlope(double slope) { 244 boolean found = false; 245 double xySlopeAbs = 0, xSlope = 0, ySlope = 0; 246 for (int x = -6; x <= 6; ++x) { 247 for (int y = -6; y <= 6; ++y) { 248 if (!found || xySlopeAbs > (slope - Math.abs(x / y))) { 249 xySlopeAbs = Math.abs(slope - (x / y)); 250 xSlope = x; 251 ySlope = y; 252 found = true; 253 } 254 } 255 } 256 if (!found) return null; 257 258 double[] answer = new double[2]; 259 answer[0] = xSlope; 260 answer[1] = ySlope; 261 return answer; 262 } 263 264 269 protected void setLineThickness(double t) { 270 if (linethickness != t) { 271 linethickness = t; 272 out.print("\\linethickness{" + linethickness + "pt}"); 273 } 274 } 275 276 279 protected void putField(Field field) { 280 double y; 281 Rectangle bounds = field.getBounds(); 282 if (currentSection.getArea().getArea() == SectionArea.PAGE_FOOTER) { 283 y = currentSection.getOutputHeight() - bounds.y; 285 } 286 else { 287 y = pageHeight() - (pageHeightUsed + bounds.y); 288 } 289 290 y -= field.getOutputHeight(); 293 294 out.print("\\put(" + bounds.x + "," + y + "){"); 295 } 296 297 305 protected void putLine(Line line, double xslope, double yslope) { 306 double y; 307 Point p0 = line.getPoint(0); 308 if (currentSection.getArea().getArea() == SectionArea.PAGE_FOOTER) { 309 y = p0.y; 311 } 312 else { 313 y = pageHeight() - (pageHeightUsed + p0.y); 314 } 315 316 out.println("\\put(" + p0.x + "," + y + "){\\line(" + (int)xslope + "," 319 + (int)yslope + "){" + line.length() + "}}"); 320 } 321 322 329 protected String makeSafe(String str) { 330 StringBuffer buf = new StringBuffer (); 331 int len = str.length(); 332 for (int i = 0; i < len; ++i) { 333 char c = str.charAt(i); 334 switch (c) { 335 case '\\': 336 buf.append("$\\backslash$"); 337 break; 338 case '~': 339 buf.append("\\textasciitilde"); 340 break; 341 case '#': case '$': case '%': case '&': case '_': 342 case '^': case '{': case '}': 343 buf.append('\\'); 344 buf.append(c); 345 break; 346 default: 347 buf.append(c); 348 break; 349 } 350 } 351 return buf.toString(); 352 } 353 354 } 355 | Popular Tags |