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