summaryrefslogtreecommitdiff
path: root/noncore/apps/opie-reader/CFilter.cpp
Unidiff
Diffstat (limited to 'noncore/apps/opie-reader/CFilter.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/apps/opie-reader/CFilter.cpp561
1 files changed, 561 insertions, 0 deletions
diff --git a/noncore/apps/opie-reader/CFilter.cpp b/noncore/apps/opie-reader/CFilter.cpp
new file mode 100644
index 0000000..c17cf61
--- a/dev/null
+++ b/noncore/apps/opie-reader/CFilter.cpp
@@ -0,0 +1,561 @@
1#include "CDrawBuffer.h"
2#include "CFilter.h"
3
4unsigned short striphtml::skip_ws()
5{
6 tchar ch;
7 CStyle sty;
8 do
9 {
10 parent->getch(ch, sty);
11 }
12 while (ch < 33);
13 return ch;
14}
15
16unsigned short striphtml::skip_ws_end()
17{
18 tchar ch;
19 CStyle sty;
20 parent->getch(ch, sty);
21 if (ch == ' ')
22 {
23 do
24 {
25 parent->getch(ch, sty);
26 }
27 while (ch != '>');
28 }
29 return ch;
30}
31
32unsigned short striphtml::parse_m()
33{
34 tchar ch;
35 CStyle sty;
36 parent->getch(ch, sty);
37 if (ch == 'm' || ch == 'M')
38 {
39 ch = skip_ws_end();
40 if (ch == '>')
41 {
42 return 0;
43 }
44 }
45 return ch;
46}
47
48void striphtml::mygetch(tchar& ch, CStyle& sty)
49{
50 parent->getch(ch, sty);
51 if (ch == 10) ch = ' ';
52}
53
54void striphtml::getch(tchar& ch, CStyle& sty)
55{
56 CStyle dummy;
57 mygetch(ch, dummy);
58 if (ch == 10) ch = ' ';
59 while (ch == '<')
60 {
61 ch = skip_ws();
62
63 switch (ch)
64 {
65 case 'p':
66 case 'P':
67 ch = skip_ws_end();
68 if (ch == '>')
69 {
70 ch = 10;
71 continue;
72 }
73 break;
74 case 'b':
75 case 'B':
76 ch = skip_ws_end();
77 if (ch == '>')
78 {
79 currentstyle.setBold();
80 mygetch(ch, dummy);
81 continue;
82 }
83 else if (ch == 'r' || ch == 'R')
84 {
85 ch = skip_ws_end();
86 if (ch == '>')
87 {
88 ch = 10;
89 continue;
90 }
91 }
92 break;
93 case 'i':
94 case 'I':
95 ch = skip_ws_end();
96 if (ch == '>')
97 {
98 currentstyle.setItalic();
99 mygetch(ch, dummy);
100 continue;
101 }
102 break;
103 case 'e':
104 case 'E':
105 if ((ch = parse_m()) == 0)
106 {
107 currentstyle.setItalic();
108 mygetch(ch, dummy);
109 continue;
110 }
111 break;
112 case 'h':
113 case 'H':
114 mygetch(ch, dummy);
115 if ('0' < ch && ch <= '9')
116 {
117 tchar hs = ch;
118 ch = skip_ws_end();
119 if (ch == '>')
120 {
121 switch (hs)
122 {
123 case '1':
124 // currentstyle = ucBold | ucFontBase+2 | (ucAlignCentre << ucAlignShift);
125 currentstyle.unset();
126 currentstyle.setFontSize(2);
127 currentstyle.setBold();
128 currentstyle.setCentreJustify();
129 break;
130 case '2':
131 // currentstyle = ucBold | ucFontBase+1;
132 currentstyle.unset();
133 currentstyle.setFontSize(1);
134 currentstyle.setBold();
135 break;
136 default:
137 // currentstyle = ucBold | ucFontBase;
138 currentstyle.unset();
139 currentstyle.setBold();
140 }
141 ch = 10;
142 // mygetch(ch, dummy);
143 continue;
144 }
145 }
146 break;
147 case '/':
148 mygetch(ch, dummy);
149 switch (ch)
150 {
151 case 'b':
152 case 'B':
153 ch = skip_ws_end();
154 if (ch == '>')
155 {
156 currentstyle.unsetBold();
157 mygetch(ch, dummy);
158 continue;
159 }
160 break;
161 case 'i':
162 case 'I':
163 ch = skip_ws_end();
164 if (ch == '>')
165 {
166 currentstyle.unsetItalic();
167 mygetch(ch, dummy);
168 continue;
169 }
170 break;
171 case 'e':
172 case 'E':
173 if ((ch = parse_m()) == 0)
174 {
175 currentstyle.unsetItalic();
176 mygetch(ch, dummy);
177 continue;
178 }
179 break;
180 case 'h':
181 case 'H':
182 mygetch(ch, dummy);
183 if ('0' < ch && ch <= '9')
184 {
185 ch = skip_ws_end();
186 if (ch == '>')
187 {
188 currentstyle.unset();
189 //mygetch(ch, dummy);
190 ch = 10;
191 continue;
192 }
193 }
194 break;
195 default:
196 break;
197 }
198 break;
199 default:
200 break;
201 }
202 while (ch != '>' && ch != UEOF)
203 {
204 mygetch(ch, dummy);
205 }
206 mygetch(ch, dummy);
207 }
208 if (ch == '&')
209 {
210 mygetch(ch, dummy);
211 if (ch == '#')
212 {
213 int id = 0;
214 mygetch(ch, dummy);
215 while (ch != ';' && ch != UEOF)
216 {
217 id = 10*id+ch-'0';
218 mygetch(ch, dummy);
219 }
220 ch = id;
221 }
222 }
223// sty = (dummy == ucFontBase) ? currentstyle : dummy;
224 sty = currentstyle;
225 return;
226}
227
228
229void textfmt::mygetch(tchar& ch, CStyle& sty)
230{
231 if (uselast)
232 {
233 ch = lastchar;
234 uselast = false;
235 }
236 else
237 {
238 parent->getch(ch, sty);
239 }
240}
241
242void textfmt::getch(tchar& ch, CStyle& sty)
243{
244 mygetch(ch, sty);
245 do
246 {
247 sty = currentstyle;
248 switch (ch)
249 {
250 case 10:
251 currentstyle.unset();
252 sty = currentstyle;
253 break;
254// Use this if you want to replace -- by em-dash
255 case '-':
256 // parent->getch(ch, sty);
257 mygetch(ch, sty);
258 if (ch == '-')
259 {
260 ch = 0x2014;
261 }
262 else
263 {
264 lastchar = ch;
265 uselast = true;
266 ch = '-';
267 }
268 break;
269 case '*':
270 if (currentstyle.isBold())
271 {
272// Already bold - time to turn it off?
273// The next two lines ensure that * follows a character but it works better without
274 // QChar c(lastchar);
275 // if ((lastchar != '*') && (c.isPunct() || c.isLetterOrNumber()))
276 if (lastchar != '*')
277 {
278 currentstyle.unsetBold();
279 CStyle dummy;
280 // parent->getch(ch, dummy);
281 mygetch(ch, dummy);
282 }
283 }
284 else
285 {
286// not bold - time to turn it on?
287 CStyle dummy;
288 // parent->getch(ch, dummy);
289 mygetch(ch, dummy);
290 QChar c(ch);
291 if ((ch != '*') && (c.isPunct() || c.isLetterOrNumber()))
292 {
293 currentstyle.setBold();
294 }
295 else
296 {
297 lastchar = ch;
298 uselast = true;
299 ch = '*';
300 }
301
302 }
303 break;
304 case '_':
305 if (currentstyle.isItalic())
306 {
307// Already bold - time to turn it off?
308// The next two lines ensure that * follows a character but it works better without
309 // QChar c(lastchar);
310 // if ((lastchar != '_') && (c.isPunct() || c.isLetterOrNumber()))
311 if (lastchar != '_')
312 {
313 currentstyle.unsetItalic();
314 CStyle dummy;
315 // parent->getch(ch, dummy);
316 mygetch(ch, dummy);
317 }
318 }
319 else
320 {
321// not bold - time to turn it on?
322 CStyle dummy;
323 // parent->getch(ch, dummy);
324 mygetch(ch, dummy);
325 QChar c(ch);
326 if ((ch != '_') && (c.isPunct() || c.isLetterOrNumber()))
327 {
328 currentstyle.setItalic();
329 }
330 else
331 {
332 lastchar = ch;
333 uselast = true;
334 ch = '_';
335 }
336
337 }
338 break;
339 }
340 }
341 while (sty != currentstyle);
342 if (!uselast) lastchar = ch;
343 return;
344}
345
346void remap::getch(tchar& ch, CStyle& sty)
347{
348 if (q[offset] != 0)
349 {
350 q[offset++];
351 sty = currentstyle;
352 return;
353 }
354 parent->getch(ch, sty);
355 switch (ch)
356 {
357 case 0x201a:
358 ch = '\'';
359 break;
360 case 0x0192:
361 ch = 'f';
362 break;
363 case 0x201e:
364 ch = '"';
365 break;
366 case 0x2026:
367 offset = 0;
368 q[0] = '.';
369 q[1] = '.';
370 q[2] = 0;
371 ch = '.'; // should be ...
372 break;
373 case 0x0160:
374 ch = 'S';
375 break;
376 case 0x2039:
377 ch = '<';
378 break;
379 case 0x0152:
380 offset = 0;
381 q[0] = 'E';
382 q[1] = 0;
383 ch = 'O';
384 break;
385 case 0x017d:
386 ch = 'Z';
387 break;
388 case 0x2018:
389 ch = '\'';
390 break;
391 case 0x2019:
392 ch = '\'';
393 break;
394 case 0x201c:
395 ch = '"';
396 break;
397 case 0x201d:
398 ch = '"';
399 break;
400 case 0x2022:
401 ch = '>';
402 break;
403 case 0x2013:
404 ch = '-';
405 break;
406 case 0x2014:
407 offset = 0;
408 q[0] = '-';
409 q[1] = 0;
410 ch = '-'; // should be --
411 break;
412 case 0x02dc:
413 ch = '~';
414 break;
415 case 0x0161:
416 ch = 's';
417 break;
418 case 0x203a:
419 ch = '>';
420 break;
421 case 0x0153:
422 offset = 0;
423 q[0] = 'e';
424 q[1] = 0;
425 ch = 'o';// should be oe
426 break;
427 case 0x017e:
428 ch = 'z';
429 break;
430 case 0x0178:
431 ch = 'Y';
432 break;
433 }
434 currentstyle = sty;
435}
436
437void PeanutFormatter::getch(tchar& ch, CStyle& sty)
438{
439 CStyle dummy;
440 currentstyle.setColour(0,0,0);
441 parent->getch(ch, dummy);
442 while (ch == '\\')
443 {
444 parent->getch(ch, dummy);
445 if (ch == '\\') break;
446 switch(ch)
447 {
448 case 'a':
449 {
450 int code = 0;
451 for (int i = 0; i < 3; i++)
452 {
453 parent->getch(ch, dummy);
454 code = 10*code + ch - '0';
455 }
456 ch = code;
457 }
458 break;
459 case 'v':
460 {
461 while (1)
462 {
463 parent->getch(ch, dummy);
464 if (ch == '\\')
465 {
466 parent->getch(ch, dummy);
467 if (ch == 'v')
468 {
469 parent->getch(ch, dummy);
470 break;
471 }
472 }
473 }
474 }
475 break;
476 case 's':
477 case 'n':
478 currentstyle.setFontSize(0);
479 parent->getch(ch,dummy);
480 break;
481 case 'p':
482 currentstyle.unset();
483 // parent->getch(ch,dummy);
484 ch = 10;
485 break;
486 case 'l':
487 if (currentstyle.getFontSize() == 1)
488 {
489 currentstyle.setFontSize(0);
490 }
491 else
492 {
493 currentstyle.setFontSize(1);
494 }
495 parent->getch(ch, dummy);
496 break;
497 case 'x':
498 if (currentstyle.getFontSize() == 0)
499 {
500 // currentstyle.unset();
501 // currentstyle.setBold();
502 currentstyle.setFontSize(1);
503 }
504 else
505 {
506 currentstyle.unset();
507 }
508 // parent->getch(ch, dummy);
509 ch = 10;
510 break;
511 case 'i':
512 if (currentstyle.isItalic())
513 {
514 currentstyle.unsetItalic();
515 }
516 else
517 {
518 currentstyle.setItalic();
519 }
520 parent->getch(ch, dummy);
521 break;
522 case 'b':
523 case 'B':
524 if (currentstyle.isBold())
525 {
526 currentstyle.unsetBold();
527 }
528 else
529 {
530 currentstyle.setBold();
531 }
532 parent->getch(ch, dummy);
533 break;
534 case 'c':
535 if (currentstyle.getJustify() == CStyle::m_AlignCentre)
536 {
537 currentstyle.setLeftJustify();
538 }
539 else
540 {
541 currentstyle.setCentreJustify();
542 }
543 parent->getch(ch, dummy);
544 break;
545 case 'r':
546 if (currentstyle.getJustify() == CStyle::m_AlignRight)
547 {
548 currentstyle.setLeftJustify();
549 }
550 else
551 {
552 currentstyle.setRightJustify();
553 }
554 parent->getch(ch, dummy);
555 break;
556 default:
557 currentstyle.setColour(255,0,0);
558 }
559 }
560 sty = currentstyle;
561}