Mercurial > projects > dwt-addons
annotate dwtx/jface/internal/text/html/HTMLPrinter.d @ 158:25f1f92fa3df
...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 26 Aug 2008 02:46:34 +0200 |
parents | 5cf141e43417 |
children | 1a5b8f8129df |
rev | line source |
---|---|
129 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2008 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.jface.internal.text.html.HTMLPrinter; | |
14 | |
131 | 15 import dwtx.jface.internal.text.html.HTML2TextReader; // packageimport |
16 import dwtx.jface.internal.text.html.BrowserInformationControl; // packageimport | |
17 import dwtx.jface.internal.text.html.SubstitutionTextReader; // packageimport | |
18 import dwtx.jface.internal.text.html.HTMLTextPresenter; // packageimport | |
19 import dwtx.jface.internal.text.html.BrowserInput; // packageimport | |
20 import dwtx.jface.internal.text.html.SingleCharReader; // packageimport | |
21 import dwtx.jface.internal.text.html.BrowserInformationControlInput; // packageimport | |
22 import dwtx.jface.internal.text.html.HTMLMessages; // packageimport | |
23 | |
24 | |
129 | 25 import dwt.dwthelper.utils; |
158 | 26 import dwtx.dwtxhelper.URL; |
129 | 27 |
28 import dwt.DWT; | |
29 import dwt.DWTError; | |
30 import dwt.graphics.FontData; | |
31 import dwt.graphics.RGB; | |
32 import dwt.widgets.Display; | |
33 | |
34 | |
35 /** | |
36 * Provides a set of convenience methods for creating HTML pages. | |
37 * <p> | |
38 * Moved into this package from <code>dwtx.jface.internal.text.revisions</code>.</p> | |
39 */ | |
40 public class HTMLPrinter { | |
41 | |
42 private static RGB BG_COLOR_RGB= new RGB(255, 255, 225); // RGB value of info bg color on WindowsXP | |
43 private static RGB FG_COLOR_RGB= new RGB(0, 0, 0); // RGB value of info fg color on WindowsXP | |
44 | |
45 | |
150 | 46 static this() { |
47 implMissing( __FILE__, __LINE__ ); // lazy init needed for Display | |
129 | 48 final Display display= Display.getDefault(); |
49 if (display !is null && !display.isDisposed()) { | |
50 try { | |
135 | 51 display.asyncExec(new class() Runnable { |
129 | 52 /* |
53 * @see java.lang.Runnable#run() | |
54 */ | |
55 public void run() { | |
56 BG_COLOR_RGB= display.getSystemColor(DWT.COLOR_INFO_BACKGROUND).getRGB(); | |
57 FG_COLOR_RGB= display.getSystemColor(DWT.COLOR_INFO_FOREGROUND).getRGB(); | |
58 } | |
59 }); | |
60 } catch (DWTError err) { | |
61 // see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=45294 | |
62 if (err.code !is DWT.ERROR_DEVICE_DISPOSED) | |
63 throw err; | |
64 } | |
65 } | |
66 } | |
67 | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
68 private this() { |
129 | 69 } |
70 | |
71 private static String replace(String text, char c, String s) { | |
72 | |
73 int previous= 0; | |
74 int current= text.indexOf(c, previous); | |
75 | |
76 if (current is -1) | |
77 return text; | |
78 | |
79 StringBuffer buffer= new StringBuffer(); | |
80 while (current > -1) { | |
81 buffer.append(text.substring(previous, current)); | |
82 buffer.append(s); | |
83 previous= current + 1; | |
84 current= text.indexOf(c, previous); | |
85 } | |
86 buffer.append(text.substring(previous)); | |
87 | |
88 return buffer.toString(); | |
89 } | |
90 | |
91 public static String convertToHTMLContent(String content) { | |
92 content= replace(content, '&', "&"); //$NON-NLS-1$ | |
93 content= replace(content, '"', """); //$NON-NLS-1$ | |
94 content= replace(content, '<', "<"); //$NON-NLS-1$ | |
95 return replace(content, '>', ">"); //$NON-NLS-1$ | |
96 } | |
97 | |
98 public static String read(Reader rd) { | |
99 | |
100 StringBuffer buffer= new StringBuffer(); | |
101 char[] readBuffer= new char[2048]; | |
102 | |
103 try { | |
104 int n= rd.read(readBuffer); | |
105 while (n > 0) { | |
106 buffer.append(readBuffer, 0, n); | |
107 n= rd.read(readBuffer); | |
108 } | |
109 return buffer.toString(); | |
110 } catch (IOException x) { | |
111 } | |
112 | |
113 return null; | |
114 } | |
115 | |
116 public static void insertPageProlog(StringBuffer buffer, int position, RGB fgRGB, RGB bgRGB, String styleSheet) { | |
117 if (fgRGB is null) | |
118 fgRGB= FG_COLOR_RGB; | |
119 if (bgRGB is null) | |
120 bgRGB= BG_COLOR_RGB; | |
121 | |
122 StringBuffer pageProlog= new StringBuffer(300); | |
123 | |
124 pageProlog.append("<html>"); //$NON-NLS-1$ | |
125 | |
126 appendStyleSheetURL(pageProlog, styleSheet); | |
127 | |
128 appendColors(pageProlog, fgRGB, bgRGB); | |
129 | |
130 buffer.insert(position, pageProlog.toString()); | |
131 } | |
132 | |
133 private static void appendColors(StringBuffer pageProlog, RGB fgRGB, RGB bgRGB) { | |
134 pageProlog.append("<body text=\""); //$NON-NLS-1$ | |
135 appendColor(pageProlog, fgRGB); | |
136 pageProlog.append("\" bgcolor=\""); //$NON-NLS-1$ | |
137 appendColor(pageProlog, bgRGB); | |
138 pageProlog.append("\">"); //$NON-NLS-1$ | |
139 } | |
140 | |
141 private static void appendColor(StringBuffer buffer, RGB rgb) { | |
142 buffer.append('#'); | |
143 appendAsHexString(buffer, rgb.red); | |
144 appendAsHexString(buffer, rgb.green); | |
145 appendAsHexString(buffer, rgb.blue); | |
146 } | |
147 | |
148 private static void appendAsHexString(StringBuffer buffer, int intValue) { | |
149 String hexValue= Integer.toHexString(intValue); | |
150 if (hexValue.length() is 1) | |
151 buffer.append('0'); | |
152 buffer.append(hexValue); | |
153 } | |
154 | |
155 public static void insertStyles(StringBuffer buffer, String[] styles) { | |
156 if (styles is null || styles.length is 0) | |
157 return; | |
158 | |
159 StringBuffer styleBuf= new StringBuffer(10 * styles.length); | |
160 for (int i= 0; styles !is null && i < styles.length; i++) { | |
161 styleBuf.append(" style=\""); //$NON-NLS-1$ | |
162 styleBuf.append(styles[i]); | |
163 styleBuf.append('"'); | |
164 } | |
165 | |
166 // Find insertion index | |
167 // a) within existing body tag with trailing space | |
168 int index= buffer.indexOf("<body "); //$NON-NLS-1$ | |
169 if (index !is -1) { | |
170 buffer.insert(index+5, styleBuf); | |
171 return; | |
172 } | |
173 | |
174 // b) within existing body tag without attributes | |
175 index= buffer.indexOf("<body>"); //$NON-NLS-1$ | |
176 if (index !is -1) { | |
177 buffer.insert(index+5, ' '); | |
178 buffer.insert(index+6, styleBuf); | |
179 return; | |
180 } | |
181 } | |
182 | |
183 private static void appendStyleSheetURL(StringBuffer buffer, String styleSheet) { | |
184 if (styleSheet is null) | |
185 return; | |
150 | 186 |
129 | 187 buffer.append("<head><style CHARSET=\"ISO-8859-1\" TYPE=\"text/css\">"); //$NON-NLS-1$ |
188 buffer.append(styleSheet); | |
189 buffer.append("</style></head>"); //$NON-NLS-1$ | |
190 } | |
150 | 191 |
129 | 192 private static void appendStyleSheetURL(StringBuffer buffer, URL styleSheetURL) { |
193 if (styleSheetURL is null) | |
194 return; | |
195 | |
196 buffer.append("<head>"); //$NON-NLS-1$ | |
197 | |
198 buffer.append("<LINK REL=\"stylesheet\" HREF= \""); //$NON-NLS-1$ | |
158 | 199 buffer.append(styleSheetURL.toString()); |
129 | 200 buffer.append("\" CHARSET=\"ISO-8859-1\" TYPE=\"text/css\">"); //$NON-NLS-1$ |
201 | |
202 buffer.append("</head>"); //$NON-NLS-1$ | |
203 } | |
204 | |
205 public static void insertPageProlog(StringBuffer buffer, int position) { | |
206 StringBuffer pageProlog= new StringBuffer(60); | |
207 pageProlog.append("<html>"); //$NON-NLS-1$ | |
208 appendColors(pageProlog, FG_COLOR_RGB, BG_COLOR_RGB); | |
209 buffer.insert(position, pageProlog.toString()); | |
210 } | |
211 | |
212 public static void insertPageProlog(StringBuffer buffer, int position, URL styleSheetURL) { | |
213 StringBuffer pageProlog= new StringBuffer(300); | |
214 pageProlog.append("<html>"); //$NON-NLS-1$ | |
215 appendStyleSheetURL(pageProlog, styleSheetURL); | |
216 appendColors(pageProlog, FG_COLOR_RGB, BG_COLOR_RGB); | |
217 buffer.insert(position, pageProlog.toString()); | |
218 } | |
150 | 219 |
129 | 220 public static void insertPageProlog(StringBuffer buffer, int position, String styleSheet) { |
221 insertPageProlog(buffer, position, null, null, styleSheet); | |
222 } | |
223 | |
224 public static void addPageProlog(StringBuffer buffer) { | |
225 insertPageProlog(buffer, buffer.length()); | |
226 } | |
227 | |
228 public static void addPageEpilog(StringBuffer buffer) { | |
229 buffer.append("</font></body></html>"); //$NON-NLS-1$ | |
230 } | |
231 | |
232 public static void startBulletList(StringBuffer buffer) { | |
233 buffer.append("<ul>"); //$NON-NLS-1$ | |
234 } | |
235 | |
236 public static void endBulletList(StringBuffer buffer) { | |
237 buffer.append("</ul>"); //$NON-NLS-1$ | |
238 } | |
239 | |
240 public static void addBullet(StringBuffer buffer, String bullet) { | |
241 if (bullet !is null) { | |
242 buffer.append("<li>"); //$NON-NLS-1$ | |
243 buffer.append(bullet); | |
244 buffer.append("</li>"); //$NON-NLS-1$ | |
245 } | |
246 } | |
247 | |
248 public static void addSmallHeader(StringBuffer buffer, String header) { | |
249 if (header !is null) { | |
250 buffer.append("<h5>"); //$NON-NLS-1$ | |
251 buffer.append(header); | |
252 buffer.append("</h5>"); //$NON-NLS-1$ | |
253 } | |
254 } | |
255 | |
256 public static void addParagraph(StringBuffer buffer, String paragraph) { | |
257 if (paragraph !is null) { | |
258 buffer.append("<p>"); //$NON-NLS-1$ | |
259 buffer.append(paragraph); | |
260 } | |
261 } | |
262 | |
263 public static void addParagraph(StringBuffer buffer, Reader paragraphReader) { | |
264 if (paragraphReader !is null) | |
265 addParagraph(buffer, read(paragraphReader)); | |
266 } | |
150 | 267 |
129 | 268 /** |
269 * Replaces the following style attributes of the font definition of the <code>html</code> | |
270 * element: | |
271 * <ul> | |
272 * <li>font-size</li> | |
273 * <li>font-weight</li> | |
274 * <li>font-style</li> | |
275 * <li>font-family</li> | |
276 * </ul> | |
277 * The font's name is used as font family, a <code>sans-serif</code> default font family is | |
278 * appended for the case that the given font name is not available. | |
279 * <p> | |
280 * If the listed font attributes are not contained in the passed style list, nothing happens. | |
281 * </p> | |
150 | 282 * |
129 | 283 * @param styles CSS style definitions |
284 * @param fontData the font information to use | |
285 * @return the modified style definitions | |
286 * @since 3.3 | |
287 */ | |
288 public static String convertTopLevelFont(String styles, FontData fontData) { | |
289 bool bold= (fontData.getStyle() & DWT.BOLD) !is 0; | |
290 bool italic= (fontData.getStyle() & DWT.ITALIC) !is 0; | |
150 | 291 |
129 | 292 // See: https://bugs.eclipse.org/bugs/show_bug.cgi?id=155993 |
293 String size= Integer.toString(fontData.getHeight()) + ("carbon".equals(DWT.getPlatform()) ? "px" : "pt"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | |
150 | 294 |
129 | 295 String family= "'" + fontData.getName() + "',sans-serif"; //$NON-NLS-1$ //$NON-NLS-2$ |
296 styles= styles.replaceFirst("(html\\s*\\{.*(?:\\s|;)font-size:\\s*)\\d+pt(\\;?.*\\})", "$1" + size + "$2"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | |
297 styles= styles.replaceFirst("(html\\s*\\{.*(?:\\s|;)font-weight:\\s*)\\w+(\\;?.*\\})", "$1" + (bold ? "bold" : "normal") + "$2"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ | |
298 styles= styles.replaceFirst("(html\\s*\\{.*(?:\\s|;)font-style:\\s*)\\w+(\\;?.*\\})", "$1" + (italic ? "italic" : "normal") + "$2"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ | |
299 styles= styles.replaceFirst("(html\\s*\\{.*(?:\\s|;)font-family:\\s*).+?(;.*\\})", "$1" + family + "$2"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | |
300 return styles; | |
301 } | |
302 } |