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