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