Mercurial > projects > dwt-addons
annotate dwtx/jface/internal/text/html/HTMLPrinter.d @ 162:1a5b8f8129df
...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 08 Sep 2008 00:51:37 +0200 |
parents | 25f1f92fa3df |
children |
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 | |
162 | 42 private static RGB BG_COLOR_RGB_; |
43 private static RGB FG_COLOR_RGB_; | |
129 | 44 |
162 | 45 private static RGB BG_COLOR_RGB(){ |
46 COLOR_RGB_init(); | |
47 return BG_COLOR_RGB_; | |
48 } | |
49 private static RGB FG_COLOR_RGB(){ | |
50 COLOR_RGB_init(); | |
51 return FG_COLOR_RGB_; | |
52 } | |
53 | |
54 private static bool COLOR_RGB_init_complete = false; | |
55 private static void COLOR_RGB_init() { | |
56 if( COLOR_RGB_init_complete ){ | |
57 return; | |
58 } | |
59 COLOR_RGB_init_complete = true; | |
60 BG_COLOR_RGB_= new RGB(255, 255, 225); // RGB value of info bg color on WindowsXP | |
61 FG_COLOR_RGB_= new RGB(0, 0, 0); // RGB value of info fg color on WindowsXP | |
62 Display display= Display.getDefault(); | |
129 | 63 if (display !is null && !display.isDisposed()) { |
64 try { | |
162 | 65 display.asyncExec( dgRunnable( (Display display_){ |
66 BG_COLOR_RGB_= display_.getSystemColor(DWT.COLOR_INFO_BACKGROUND).getRGB(); | |
67 FG_COLOR_RGB_= display_.getSystemColor(DWT.COLOR_INFO_FOREGROUND).getRGB(); | |
68 }, display )); | |
129 | 69 } catch (DWTError err) { |
70 // see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=45294 | |
71 if (err.code !is DWT.ERROR_DEVICE_DISPOSED) | |
72 throw err; | |
73 } | |
74 } | |
75 } | |
76 | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
77 private this() { |
129 | 78 } |
79 | |
80 private static String replace(String text, char c, String s) { | |
81 | |
82 int previous= 0; | |
83 int current= text.indexOf(c, previous); | |
84 | |
85 if (current is -1) | |
86 return text; | |
87 | |
88 StringBuffer buffer= new StringBuffer(); | |
89 while (current > -1) { | |
90 buffer.append(text.substring(previous, current)); | |
91 buffer.append(s); | |
92 previous= current + 1; | |
93 current= text.indexOf(c, previous); | |
94 } | |
95 buffer.append(text.substring(previous)); | |
96 | |
97 return buffer.toString(); | |
98 } | |
99 | |
100 public static String convertToHTMLContent(String content) { | |
101 content= replace(content, '&', "&"); //$NON-NLS-1$ | |
102 content= replace(content, '"', """); //$NON-NLS-1$ | |
103 content= replace(content, '<', "<"); //$NON-NLS-1$ | |
104 return replace(content, '>', ">"); //$NON-NLS-1$ | |
105 } | |
106 | |
107 public static String read(Reader rd) { | |
108 | |
109 StringBuffer buffer= new StringBuffer(); | |
110 char[] readBuffer= new char[2048]; | |
111 | |
112 try { | |
113 int n= rd.read(readBuffer); | |
114 while (n > 0) { | |
162 | 115 buffer.append(readBuffer[ 0 .. n ]); |
129 | 116 n= rd.read(readBuffer); |
117 } | |
118 return buffer.toString(); | |
119 } catch (IOException x) { | |
120 } | |
121 | |
122 return null; | |
123 } | |
124 | |
125 public static void insertPageProlog(StringBuffer buffer, int position, RGB fgRGB, RGB bgRGB, String styleSheet) { | |
126 if (fgRGB is null) | |
127 fgRGB= FG_COLOR_RGB; | |
128 if (bgRGB is null) | |
129 bgRGB= BG_COLOR_RGB; | |
130 | |
131 StringBuffer pageProlog= new StringBuffer(300); | |
132 | |
133 pageProlog.append("<html>"); //$NON-NLS-1$ | |
134 | |
135 appendStyleSheetURL(pageProlog, styleSheet); | |
136 | |
137 appendColors(pageProlog, fgRGB, bgRGB); | |
138 | |
162 | 139 buffer.select(position,0); |
140 buffer.replace(pageProlog.toString()); | |
129 | 141 } |
142 | |
143 private static void appendColors(StringBuffer pageProlog, RGB fgRGB, RGB bgRGB) { | |
144 pageProlog.append("<body text=\""); //$NON-NLS-1$ | |
145 appendColor(pageProlog, fgRGB); | |
146 pageProlog.append("\" bgcolor=\""); //$NON-NLS-1$ | |
147 appendColor(pageProlog, bgRGB); | |
148 pageProlog.append("\">"); //$NON-NLS-1$ | |
149 } | |
150 | |
151 private static void appendColor(StringBuffer buffer, RGB rgb) { | |
152 buffer.append('#'); | |
153 appendAsHexString(buffer, rgb.red); | |
154 appendAsHexString(buffer, rgb.green); | |
155 appendAsHexString(buffer, rgb.blue); | |
156 } | |
157 | |
158 private static void appendAsHexString(StringBuffer buffer, int intValue) { | |
159 String hexValue= Integer.toHexString(intValue); | |
160 if (hexValue.length() is 1) | |
161 buffer.append('0'); | |
162 buffer.append(hexValue); | |
163 } | |
164 | |
165 public static void insertStyles(StringBuffer buffer, String[] styles) { | |
166 if (styles is null || styles.length is 0) | |
167 return; | |
168 | |
169 StringBuffer styleBuf= new StringBuffer(10 * styles.length); | |
170 for (int i= 0; styles !is null && i < styles.length; i++) { | |
171 styleBuf.append(" style=\""); //$NON-NLS-1$ | |
172 styleBuf.append(styles[i]); | |
173 styleBuf.append('"'); | |
174 } | |
175 | |
176 // Find insertion index | |
177 // a) within existing body tag with trailing space | |
162 | 178 int index= buffer.slice().indexOf("<body "); //$NON-NLS-1$ |
129 | 179 if (index !is -1) { |
162 | 180 buffer.select(index+5, 0); |
181 buffer.replace(styleBuf); | |
129 | 182 return; |
183 } | |
184 | |
185 // b) within existing body tag without attributes | |
162 | 186 index= buffer.slice().indexOf("<body>"); //$NON-NLS-1$ |
129 | 187 if (index !is -1) { |
162 | 188 buffer.select(index+5, 0); |
189 buffer.replace( " " ); | |
190 buffer.select(index+6, 0); | |
191 buffer.replace(styleBuf); | |
129 | 192 return; |
193 } | |
194 } | |
195 | |
196 private static void appendStyleSheetURL(StringBuffer buffer, String styleSheet) { | |
197 if (styleSheet is null) | |
198 return; | |
150 | 199 |
129 | 200 buffer.append("<head><style CHARSET=\"ISO-8859-1\" TYPE=\"text/css\">"); //$NON-NLS-1$ |
201 buffer.append(styleSheet); | |
202 buffer.append("</style></head>"); //$NON-NLS-1$ | |
203 } | |
150 | 204 |
129 | 205 private static void appendStyleSheetURL(StringBuffer buffer, URL styleSheetURL) { |
206 if (styleSheetURL is null) | |
207 return; | |
208 | |
209 buffer.append("<head>"); //$NON-NLS-1$ | |
210 | |
211 buffer.append("<LINK REL=\"stylesheet\" HREF= \""); //$NON-NLS-1$ | |
158 | 212 buffer.append(styleSheetURL.toString()); |
129 | 213 buffer.append("\" CHARSET=\"ISO-8859-1\" TYPE=\"text/css\">"); //$NON-NLS-1$ |
214 | |
215 buffer.append("</head>"); //$NON-NLS-1$ | |
216 } | |
217 | |
218 public static void insertPageProlog(StringBuffer buffer, int position) { | |
219 StringBuffer pageProlog= new StringBuffer(60); | |
220 pageProlog.append("<html>"); //$NON-NLS-1$ | |
221 appendColors(pageProlog, FG_COLOR_RGB, BG_COLOR_RGB); | |
162 | 222 buffer.select(position, 0); |
223 buffer.replace(pageProlog.toString()); | |
129 | 224 } |
225 | |
226 public static void insertPageProlog(StringBuffer buffer, int position, URL styleSheetURL) { | |
227 StringBuffer pageProlog= new StringBuffer(300); | |
228 pageProlog.append("<html>"); //$NON-NLS-1$ | |
229 appendStyleSheetURL(pageProlog, styleSheetURL); | |
230 appendColors(pageProlog, FG_COLOR_RGB, BG_COLOR_RGB); | |
162 | 231 buffer.select(position, 0); |
232 buffer.replace(pageProlog.toString()); | |
129 | 233 } |
150 | 234 |
129 | 235 public static void insertPageProlog(StringBuffer buffer, int position, String styleSheet) { |
236 insertPageProlog(buffer, position, null, null, styleSheet); | |
237 } | |
238 | |
239 public static void addPageProlog(StringBuffer buffer) { | |
240 insertPageProlog(buffer, buffer.length()); | |
241 } | |
242 | |
243 public static void addPageEpilog(StringBuffer buffer) { | |
244 buffer.append("</font></body></html>"); //$NON-NLS-1$ | |
245 } | |
246 | |
247 public static void startBulletList(StringBuffer buffer) { | |
248 buffer.append("<ul>"); //$NON-NLS-1$ | |
249 } | |
250 | |
251 public static void endBulletList(StringBuffer buffer) { | |
252 buffer.append("</ul>"); //$NON-NLS-1$ | |
253 } | |
254 | |
255 public static void addBullet(StringBuffer buffer, String bullet) { | |
256 if (bullet !is null) { | |
257 buffer.append("<li>"); //$NON-NLS-1$ | |
258 buffer.append(bullet); | |
259 buffer.append("</li>"); //$NON-NLS-1$ | |
260 } | |
261 } | |
262 | |
263 public static void addSmallHeader(StringBuffer buffer, String header) { | |
264 if (header !is null) { | |
265 buffer.append("<h5>"); //$NON-NLS-1$ | |
266 buffer.append(header); | |
267 buffer.append("</h5>"); //$NON-NLS-1$ | |
268 } | |
269 } | |
270 | |
271 public static void addParagraph(StringBuffer buffer, String paragraph) { | |
272 if (paragraph !is null) { | |
273 buffer.append("<p>"); //$NON-NLS-1$ | |
274 buffer.append(paragraph); | |
275 } | |
276 } | |
277 | |
278 public static void addParagraph(StringBuffer buffer, Reader paragraphReader) { | |
279 if (paragraphReader !is null) | |
280 addParagraph(buffer, read(paragraphReader)); | |
281 } | |
150 | 282 |
129 | 283 /** |
284 * Replaces the following style attributes of the font definition of the <code>html</code> | |
285 * element: | |
286 * <ul> | |
287 * <li>font-size</li> | |
288 * <li>font-weight</li> | |
289 * <li>font-style</li> | |
290 * <li>font-family</li> | |
291 * </ul> | |
292 * The font's name is used as font family, a <code>sans-serif</code> default font family is | |
293 * appended for the case that the given font name is not available. | |
294 * <p> | |
295 * If the listed font attributes are not contained in the passed style list, nothing happens. | |
296 * </p> | |
150 | 297 * |
129 | 298 * @param styles CSS style definitions |
299 * @param fontData the font information to use | |
300 * @return the modified style definitions | |
301 * @since 3.3 | |
302 */ | |
303 public static String convertTopLevelFont(String styles, FontData fontData) { | |
304 bool bold= (fontData.getStyle() & DWT.BOLD) !is 0; | |
305 bool italic= (fontData.getStyle() & DWT.ITALIC) !is 0; | |
150 | 306 |
129 | 307 // See: https://bugs.eclipse.org/bugs/show_bug.cgi?id=155993 |
162 | 308 String size= Integer.toString(fontData.getHeight()) ~ ("carbon".equals(DWT.getPlatform()) ? "px" : "pt"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
150 | 309 |
162 | 310 String family= "'" ~ fontData.getName() ~ "',sans-serif"; //$NON-NLS-1$ //$NON-NLS-2$ |
311 styles= styles.replaceFirst("(html\\s*\\{.*(?:\\s|;)font-size:\\s*)\\d+pt(\\;?.*\\})", "$1" ~ size ~ "$2"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | |
312 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$ | |
313 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$ | |
314 styles= styles.replaceFirst("(html\\s*\\{.*(?:\\s|;)font-family:\\s*).+?(;.*\\})", "$1" ~ family ~ "$2"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | |
129 | 315 return styles; |
316 } | |
317 } |