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