comparison org.eclipse.jface.text/src/org/eclipse/jface/internal/text/html/HTMLPrinter.d @ 12:bc29606a740c

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