comparison 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
comparison
equal deleted inserted replaced
161:f8d52b926852 162:1a5b8f8129df
37 * <p> 37 * <p>
38 * Moved into this package from <code>dwtx.jface.internal.text.revisions</code>.</p> 38 * Moved into this package from <code>dwtx.jface.internal.text.revisions</code>.</p>
39 */ 39 */
40 public class HTMLPrinter { 40 public class HTMLPrinter {
41 41
42 private static RGB BG_COLOR_RGB= new RGB(255, 255, 225); // RGB value of info bg color on WindowsXP 42 private static RGB BG_COLOR_RGB_;
43 private static RGB FG_COLOR_RGB= new RGB(0, 0, 0); // RGB value of info fg color on WindowsXP 43 private static RGB FG_COLOR_RGB_;
44 44
45 45 private static RGB BG_COLOR_RGB(){
46 static this() { 46 COLOR_RGB_init();
47 implMissing( __FILE__, __LINE__ ); // lazy init needed for Display 47 return BG_COLOR_RGB_;
48 final Display display= Display.getDefault(); 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();
49 if (display !is null && !display.isDisposed()) { 63 if (display !is null && !display.isDisposed()) {
50 try { 64 try {
51 display.asyncExec(new class() Runnable { 65 display.asyncExec( dgRunnable( (Display display_){
52 /* 66 BG_COLOR_RGB_= display_.getSystemColor(DWT.COLOR_INFO_BACKGROUND).getRGB();
53 * @see java.lang.Runnable#run() 67 FG_COLOR_RGB_= display_.getSystemColor(DWT.COLOR_INFO_FOREGROUND).getRGB();
54 */ 68 }, display ));
55 public void run() {
56 BG_COLOR_RGB= display.getSystemColor(DWT.COLOR_INFO_BACKGROUND).getRGB();
57 FG_COLOR_RGB= display.getSystemColor(DWT.COLOR_INFO_FOREGROUND).getRGB();
58 }
59 });
60 } catch (DWTError err) { 69 } catch (DWTError err) {
61 // see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=45294 70 // see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=45294
62 if (err.code !is DWT.ERROR_DEVICE_DISPOSED) 71 if (err.code !is DWT.ERROR_DEVICE_DISPOSED)
63 throw err; 72 throw err;
64 } 73 }
101 char[] readBuffer= new char[2048]; 110 char[] readBuffer= new char[2048];
102 111
103 try { 112 try {
104 int n= rd.read(readBuffer); 113 int n= rd.read(readBuffer);
105 while (n > 0) { 114 while (n > 0) {
106 buffer.append(readBuffer, 0, n); 115 buffer.append(readBuffer[ 0 .. n ]);
107 n= rd.read(readBuffer); 116 n= rd.read(readBuffer);
108 } 117 }
109 return buffer.toString(); 118 return buffer.toString();
110 } catch (IOException x) { 119 } catch (IOException x) {
111 } 120 }
125 134
126 appendStyleSheetURL(pageProlog, styleSheet); 135 appendStyleSheetURL(pageProlog, styleSheet);
127 136
128 appendColors(pageProlog, fgRGB, bgRGB); 137 appendColors(pageProlog, fgRGB, bgRGB);
129 138
130 buffer.insert(position, pageProlog.toString()); 139 buffer.select(position,0);
140 buffer.replace(pageProlog.toString());
131 } 141 }
132 142
133 private static void appendColors(StringBuffer pageProlog, RGB fgRGB, RGB bgRGB) { 143 private static void appendColors(StringBuffer pageProlog, RGB fgRGB, RGB bgRGB) {
134 pageProlog.append("<body text=\""); //$NON-NLS-1$ 144 pageProlog.append("<body text=\""); //$NON-NLS-1$
135 appendColor(pageProlog, fgRGB); 145 appendColor(pageProlog, fgRGB);
163 styleBuf.append('"'); 173 styleBuf.append('"');
164 } 174 }
165 175
166 // Find insertion index 176 // Find insertion index
167 // a) within existing body tag with trailing space 177 // a) within existing body tag with trailing space
168 int index= buffer.indexOf("<body "); //$NON-NLS-1$ 178 int index= buffer.slice().indexOf("<body "); //$NON-NLS-1$
169 if (index !is -1) { 179 if (index !is -1) {
170 buffer.insert(index+5, styleBuf); 180 buffer.select(index+5, 0);
181 buffer.replace(styleBuf);
171 return; 182 return;
172 } 183 }
173 184
174 // b) within existing body tag without attributes 185 // b) within existing body tag without attributes
175 index= buffer.indexOf("<body>"); //$NON-NLS-1$ 186 index= buffer.slice().indexOf("<body>"); //$NON-NLS-1$
176 if (index !is -1) { 187 if (index !is -1) {
177 buffer.insert(index+5, ' '); 188 buffer.select(index+5, 0);
178 buffer.insert(index+6, styleBuf); 189 buffer.replace( " " );
190 buffer.select(index+6, 0);
191 buffer.replace(styleBuf);
179 return; 192 return;
180 } 193 }
181 } 194 }
182 195
183 private static void appendStyleSheetURL(StringBuffer buffer, String styleSheet) { 196 private static void appendStyleSheetURL(StringBuffer buffer, String styleSheet) {
204 217
205 public static void insertPageProlog(StringBuffer buffer, int position) { 218 public static void insertPageProlog(StringBuffer buffer, int position) {
206 StringBuffer pageProlog= new StringBuffer(60); 219 StringBuffer pageProlog= new StringBuffer(60);
207 pageProlog.append("<html>"); //$NON-NLS-1$ 220 pageProlog.append("<html>"); //$NON-NLS-1$
208 appendColors(pageProlog, FG_COLOR_RGB, BG_COLOR_RGB); 221 appendColors(pageProlog, FG_COLOR_RGB, BG_COLOR_RGB);
209 buffer.insert(position, pageProlog.toString()); 222 buffer.select(position, 0);
223 buffer.replace(pageProlog.toString());
210 } 224 }
211 225
212 public static void insertPageProlog(StringBuffer buffer, int position, URL styleSheetURL) { 226 public static void insertPageProlog(StringBuffer buffer, int position, URL styleSheetURL) {
213 StringBuffer pageProlog= new StringBuffer(300); 227 StringBuffer pageProlog= new StringBuffer(300);
214 pageProlog.append("<html>"); //$NON-NLS-1$ 228 pageProlog.append("<html>"); //$NON-NLS-1$
215 appendStyleSheetURL(pageProlog, styleSheetURL); 229 appendStyleSheetURL(pageProlog, styleSheetURL);
216 appendColors(pageProlog, FG_COLOR_RGB, BG_COLOR_RGB); 230 appendColors(pageProlog, FG_COLOR_RGB, BG_COLOR_RGB);
217 buffer.insert(position, pageProlog.toString()); 231 buffer.select(position, 0);
232 buffer.replace(pageProlog.toString());
218 } 233 }
219 234
220 public static void insertPageProlog(StringBuffer buffer, int position, String styleSheet) { 235 public static void insertPageProlog(StringBuffer buffer, int position, String styleSheet) {
221 insertPageProlog(buffer, position, null, null, styleSheet); 236 insertPageProlog(buffer, position, null, null, styleSheet);
222 } 237 }
288 public static String convertTopLevelFont(String styles, FontData fontData) { 303 public static String convertTopLevelFont(String styles, FontData fontData) {
289 bool bold= (fontData.getStyle() & DWT.BOLD) !is 0; 304 bool bold= (fontData.getStyle() & DWT.BOLD) !is 0;
290 bool italic= (fontData.getStyle() & DWT.ITALIC) !is 0; 305 bool italic= (fontData.getStyle() & DWT.ITALIC) !is 0;
291 306
292 // See: https://bugs.eclipse.org/bugs/show_bug.cgi?id=155993 307 // See: https://bugs.eclipse.org/bugs/show_bug.cgi?id=155993
293 String size= Integer.toString(fontData.getHeight()) + ("carbon".equals(DWT.getPlatform()) ? "px" : "pt"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ 308 String size= Integer.toString(fontData.getHeight()) ~ ("carbon".equals(DWT.getPlatform()) ? "px" : "pt"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
294 309
295 String family= "'" + fontData.getName() + "',sans-serif"; //$NON-NLS-1$ //$NON-NLS-2$ 310 String family= "'" ~ fontData.getName() ~ "',sans-serif"; //$NON-NLS-1$ //$NON-NLS-2$
296 styles= styles.replaceFirst("(html\\s*\\{.*(?:\\s|;)font-size:\\s*)\\d+pt(\\;?.*\\})", "$1" + size + "$2"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ 311 styles= styles.replaceFirst("(html\\s*\\{.*(?:\\s|;)font-size:\\s*)\\d+pt(\\;?.*\\})", "$1" ~ size ~ "$2"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
297 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$ 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$
298 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$ 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$
299 styles= styles.replaceFirst("(html\\s*\\{.*(?:\\s|;)font-family:\\s*).+?(;.*\\})", "$1" + family + "$2"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ 314 styles= styles.replaceFirst("(html\\s*\\{.*(?:\\s|;)font-family:\\s*).+?(;.*\\})", "$1" ~ family ~ "$2"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
300 return styles; 315 return styles;
301 } 316 }
302 } 317 }