1 33 34 package edu.rice.cs.drjava.model.print; 35 36 import java.util.*; 37 import java.awt.print.*; 38 import java.awt.*; 39 import java.awt.font.*; 40 import java.text.*; 41 42 50 public class DrJavaBook implements Pageable { 51 52 private ArrayList<PagePrinter> _pagePrinters; 53 private PageFormat _format; 54 private String _fileName; 55 56 public static final Font PRINT_FONT = new Font("Monospaced", Font.PLAIN, 9); 57 public static final Font FOOTER_FONT = new Font("Monospaced", Font.PLAIN, 8); 58 public static final Font LINE_FONT = new Font("Monospaced", Font.ITALIC, 8); 59 public float LINE_NUM_WIDTH; 60 61 private static FontRenderContext DEFAULT_FRC = new FontRenderContext(null, false, true); 62 63 64 public DrJavaBook(String text, String fileName, PageFormat format) { 65 _pagePrinters = new ArrayList<PagePrinter>(); 66 _format = format; 67 _fileName = fileName; 68 69 TextLayout textl = new TextLayout("XXX ", LINE_FONT, DEFAULT_FRC); 70 LINE_NUM_WIDTH = textl.getAdvance(); 71 72 setUpPagePrinters(text); 73 } 74 75 80 private void setUpPagePrinters(String text) { 81 int linenum = 0; 82 int reallinenum = 1; 83 String thisText; 84 FontRenderContext frc = new FontRenderContext(null, false, true); 85 86 TextLayout textl = new TextLayout("X", PRINT_FONT, frc); 88 float lineHeight = textl.getLeading() + textl.getAscent(); 89 int linesPerPage = (int) (_format.getImageableHeight() / lineHeight) - 1; 90 91 HashMap<TextAttribute,Object > map = new HashMap<TextAttribute,Object >(); map.put(TextAttribute.FONT, PRINT_FONT); 93 94 char[] carriageReturn = {(char) 10}; 95 String lineSeparator = new String (carriageReturn); 96 97 try { 98 thisText = text.substring(0, text.indexOf(lineSeparator)); 99 text = text.substring(text.indexOf(lineSeparator) + 1); 100 } 101 catch (StringIndexOutOfBoundsException e) { 102 thisText = text; 103 text = ""; 104 } 105 106 int page = 0; 107 PagePrinter thisPagePrinter = new PagePrinter(page, _fileName, this); 108 _pagePrinters.add(thisPagePrinter); 109 110 while (! (thisText.equals("") && (text.equals("")))) { 112 if (thisText.equals("")) thisText = " "; 113 114 AttributedCharacterIterator charIterator = (new AttributedString(thisText, map)).getIterator(); 115 LineBreakMeasurer measurer = new LineBreakMeasurer(charIterator, frc); 116 117 boolean isCarryLine = false; 118 119 while (measurer.getPosition() < charIterator.getEndIndex()) { 121 TextLayout pageNumber = new TextLayout(" ", LINE_FONT, DEFAULT_FRC); 122 123 if (! isCarryLine) 124 pageNumber = new TextLayout("" + reallinenum, LINE_FONT, DEFAULT_FRC); 125 126 thisPagePrinter.add(measurer.nextLayout((float) _format.getImageableWidth() - LINE_NUM_WIDTH), pageNumber); 128 129 linenum++; 130 if (linenum == (linesPerPage * (page+1))) 132 { 133 page++; 134 thisPagePrinter = new PagePrinter(page, _fileName, this); 135 _pagePrinters.add(thisPagePrinter); 136 } 137 138 isCarryLine = true; 139 } 140 141 reallinenum++; 142 143 try { 145 thisText = text.substring(0, text.indexOf(lineSeparator)); 146 text = text.substring(text.indexOf(lineSeparator) + 1); 147 } catch (StringIndexOutOfBoundsException e) { 148 thisText = text; 149 text = ""; 150 } 151 } 152 } 153 154 155 public int getNumberOfPages() { return _pagePrinters.size(); } 156 157 161 public PageFormat getPageFormat(int pageIndex) { return _format; } 162 163 167 public Printable getPrintable(int pageIndex) { return _pagePrinters.get(pageIndex); } 168 169 } 170 | Popular Tags |