summaryrefslogtreecommitdiff
path: root/noncore/apps/opie-reader/CFilter.h
blob: 594b606e232c705e9b50e3d73216baa3370cfd47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#ifndef __CFILTER_H
#define __CFILTER_H

#include "CExpander.h"
#include "CEncoding.h"

class CFilter_IFace : public CCharacterSource
{
 public:
    virtual linkType hyperlink(unsigned int n, unsigned int noff, QString& w, QString& nm) = 0;
    virtual void setparent(CCharacterSource* p) = 0;
    virtual ~CFilter_IFace() {};
    virtual void locate(unsigned int n) = 0;
    virtual bool findanchor(const QString& nm) = 0;
    virtual void saveposn(const QString& f, size_t posn) = 0;
    virtual void writeposn(const QString& f, size_t posn) = 0;
    virtual linkType forward(QString& f, size_t& loc) = 0;
    virtual linkType back(QString& f, size_t& loc) = 0;
    virtual bool hasnavigation() = 0;
    virtual int getwidth() = 0;
    virtual CCharacterSource* getparent() = 0;
    virtual unsigned long startSection() = 0;
};

class CFilter : public CFilter_IFace
{
 protected:
    CCharacterSource* parent;
 public:
  virtual QString getTableAsHtml(unsigned long loc)
    {
      qDebug("CFilter::getTableAsHtml()");
      return parent->getTableAsHtml(loc);
    }
    virtual linkType hyperlink(unsigned int n, unsigned int noff, QString& w, QString& nm)
	{
	    return parent->hyperlink(n,noff,w,nm);
	}
    CFilter() : parent(NULL) {}
    void setparent(CCharacterSource* p) { parent = p; }
    CCharacterSource* getparent() { return parent; }
    virtual ~CFilter() {};
    virtual void locate(unsigned int n)
      {
	parent->locate(n);
      }
   virtual bool findanchor(const QString& nm)
     {
       return parent->findanchor(nm);
     }
    virtual void saveposn(const QString& f, size_t posn) { parent->saveposn(f, posn); }
    virtual void writeposn(const QString& f, size_t posn) { parent->writeposn(f, posn); }
    virtual linkType forward(QString& f, size_t& loc) { return parent->forward(f, loc); }
    virtual linkType back(QString& f, size_t& loc) { return parent->back(f, loc); }
    virtual bool hasnavigation() { return parent->hasnavigation(); }
    virtual int getwidth() { return parent->getwidth(); }
    QImage* getPicture(unsigned long tgt) { return parent->getPicture(tgt); }
    QImage* getPicture(const QString& href) { return parent->getPicture(href); }
    bool getFile(const QString& href, const QString& nm) { return parent->getFile(href, nm); }
    virtual unsigned long startSection() { return parent->startSection(); }
};

class CFilterChain
{
    CExpander_Interface* expander;
    CEncoding* encoder;
    CFilter_IFace* first;
    CCharacterSource* front;
 public:
    CFilterChain(CEncoding* _e) : encoder(_e), first(NULL), front(_e) {};
    ~CFilterChain()
	{
	    CCharacterSource* p = front;
	    while (p != encoder)
	    {
		CFilter_IFace* pnext = (CFilter_IFace*)p;
		p = ((CFilter_IFace*)p)->getparent();
		delete pnext;
	    }
	    delete encoder;
	}
    linkType hyperlink(unsigned int n, unsigned int noff, QString& wrd, QString& nm)
      {
	return front->hyperlink(n, noff, wrd, nm);
      }
    QString getTableAsHtml(unsigned long loc)
      {
	return front->getTableAsHtml(loc);
      }
    void locate(unsigned int n)
      {
	front->locate(n);
      }
    void getch(tchar& ch, CStyle& sty, unsigned long& pos)
	{
	    front->getch(ch, sty, pos);
	}
    /*
    void rawgetch(tchar& ch, CStyle& sty, unsigned long& pos)
      {
	encoder->getch(ch, sty, pos);
      }
    */
    void addfilter(CFilter_IFace* p)
	{
	    if (first == NULL)
	    {
		front = first = p;
		p->setparent(encoder);
	    }
	    else
	    {
		p->setparent(front);
		front = p;
	    }
	}
    void setsource(CExpander_Interface* p)
	{
	    expander = p;
	    encoder->setparent(p);
	}
    void setencoder(CEncoding* p)
      {
	delete encoder;
	encoder = p;
	first->setparent(p);
	encoder->setparent(expander);
      }
    bool findanchor(const QString& nm)
      {
	return front->findanchor(nm);
      }
    void saveposn(const QString& f, size_t posn) { front->saveposn(f, posn); }
    void writeposn(const QString& f, size_t posn) { front->writeposn(f, posn); }
    linkType forward(QString& f, size_t& loc) { return front->forward(f, loc); }
    linkType back(QString& f, size_t& loc) { return front->back(f, loc); }
    bool hasnavigation() { return front->hasnavigation(); }
    QString about() { return QString("Filter chain (c) Tim Wentford\n")+front->about(); }
};

class stripcr : public CFilter
{
 public:
    stripcr() {}
    ~stripcr() {}
    void getch(tchar& ch, CStyle& sty, unsigned long& pos)
	{
	    do
	    {
		parent->getch(ch, sty, pos);
	    }
	    while (ch == 13);
	}
    QString about() { return QString("StripCR filter (c) Tim Wentford\n")+parent->about(); }
};

class dehyphen : public CFilter
{
    bool m_bCharWaiting;
    tchar m_nextChar;
    CStyle m_nextSty;
 public:
    dehyphen() : m_bCharWaiting(false) {}
    ~dehyphen() {}
    void getch(tchar& ch, CStyle& sty, unsigned long& pos)
	{
	    if (m_bCharWaiting)
	    {
		m_bCharWaiting = false;
		ch = m_nextChar;
		sty = m_nextSty;
		return;
	    }
	    parent->getch(ch, sty, pos);
	    if (ch != '-') return;
	    parent->getch(m_nextChar, m_nextSty, pos);
	    if (m_nextChar != 10)
	    {
		m_bCharWaiting = true;
		ch = '-';
		return;
	    }
	    parent->getch(ch, sty, pos);
	}
    QString about() { return QString("Hyphenation filter (c) Tim Wentford\n")+parent->about(); }
};

template<class A, class B>class QMap;

const int m_cmaxdepth = 8;

class htmlmark
{
  QString file;
  size_t pos;
 public:
  htmlmark() : file(), pos(0) {}
  htmlmark(const QString& _f, size_t _p) : file(_f), pos(_p) {}
  QString filename() { return file; }
  size_t posn() { return pos; }
};

class unindent : public CFilter
{
    tchar lc;
 public:
    unindent() : lc(0) {}
    ~unindent() {}
    void getch(tchar& ch, CStyle& sty, unsigned long& pos)
	{
	    if (lc == 10)
	    {
		do
		{
		    parent->getch(ch, sty, pos);
		}
		while (ch == ' ');
	    }
	    else parent->getch(ch, sty, pos);
	    lc = ch;
	    return;
	}
    QString about() { return QString("Unindent filter (c) Tim Wentford\n")+parent->about(); }
};

class CRegExpFilt;
class repara : public CFilter
{
    tchar tch;
    CRegExpFilt* flt;
 public:
    repara(const QString&);
    ~repara();
    void getch(tchar& ch, CStyle& sty, unsigned long& pos);
    QString about() { return QString("Reparagraph filter (c) Tim Wentford\n")+parent->about(); }
};

class indenter : public CFilter
{
    int amnt;
    int indent;
    CStyle lsty;
 public:
    indenter(int _a=5) : amnt(_a), indent(0) {}
    ~indenter() {}
    void getch(tchar& ch, CStyle& sty, unsigned long& pos)
	{
	    if (indent > 0)
	    {
		indent--;
		ch = ' ';
		sty = lsty;
		return;
	    }
	    parent->getch(ch, sty, pos);
	    if (ch == 10)
	    {
		indent = amnt;
		lsty = sty;
	    }
	    return;
	}
    QString about() { return QString("Indentation filter (c) Tim Wentford\n")+parent->about(); }
};

class dblspce : public CFilter
{
    bool lastlf;
    CStyle lsty;
 public:
    dblspce() : lastlf(false) {}
    ~dblspce() {}
    void getch(tchar& ch, CStyle& sty, unsigned long& pos)
	{
	    if (lastlf)
	    {
		lastlf = false;
		ch = 10;
		sty = lsty;
		return;
	    }
	    parent->getch(ch, sty, pos);
	    if (lastlf = (ch == 10))
	    {
		lsty = sty;
	    }
	    return;
	}
    QString about() { return QString("Double space (c) Tim Wentford\n")+parent->about(); }
};

class textfmt : public CFilter
{
    CStyle currentstyle;
    tchar lastchar;
    bool uselast;
    void mygetch(tchar&, CStyle&, unsigned long& pos);
 public:
    textfmt() : lastchar(0), uselast(false) {}
    ~textfmt() {}
    void getch(tchar& ch, CStyle& sty, unsigned long& pos);
    QString about() { return QString("Text formatting filter (c) Tim Wentford\n")+parent->about(); }
};

class embolden : public CFilter
{
 public:
    embolden() {}
    ~embolden() {}
    void getch(tchar& ch, CStyle& sty, unsigned long& pos)
	{
	    parent->getch(ch, sty, pos);
	    sty.setBold();
	}
    QString about() { return QString("Emboldening filter (c) Tim Wentford\n")+parent->about(); }
};

class remap : public CFilter
{
    tchar q[3];
    int offset;
    CStyle currentstyle;
 public:
    remap() : offset(0) { q[0] = 0; }
    ~remap() {}
    void getch(tchar& ch, CStyle& sty, unsigned long& pos);
    QString about() { return QString("Character remapping filter (c) Tim Wentford\n")+parent->about(); }
};

class PeanutFormatter : public CFilter
{
    CStyle currentstyle;
 public:
    ~PeanutFormatter() {}
    void getch(tchar& ch, CStyle& sty, unsigned long& pos);
    QString about() { return QString("PML filter (c) Tim Wentford\n")+parent->about(); }
};

class OnePara : public CFilter
{
    tchar m_lastchar;
 public:
    OnePara() : m_lastchar(0) {}
    ~OnePara() {}
    void getch(tchar& ch, CStyle& sty, unsigned long& pos);
    QString about() { return QString("Single space filter (c) Tim Wentford\n")+parent->about(); }
};

class DePluck : public CFilter
{
    tchar* nextpart;
    tchar m_buffer;
    int m_buffed;
    int m_current;
    bool m_debuff;
    CStyle m_laststyle;
 public:
    DePluck(tchar* t) : nextpart(t), m_buffer(0), m_buffed(0), m_current(0), m_debuff(false) {}
    ~DePluck() {}
    void getch(tchar& ch, CStyle& sty, unsigned long& pos);
    QString about() { return QString("Depluck filter (c) Tim Wentford\n")+parent->about(); }
};

class repalm : public CFilter
{
 public:
    ~repalm() {}
    void getch(tchar& ch, CStyle& sty, unsigned long& pos);
    QString about() { return QString("Repalm filter (c) Tim Wentford\n")+parent->about(); }
};

class FullJust : public CFilter
{
 public:
    void getch(tchar& ch, CStyle& sty, unsigned long& pos)
	{
	    parent->getch(ch, sty, pos);
	    if (sty.getJustify() == m_AlignLeft) sty.setFullJustify();
	}
    QString about() { return QString("Full justification filter (c) Tim Wentford\n")+parent->about(); }
};
/*
class AddSpace : public CFilter
{
    unsigned char m_espc;
 public:
    AddSpace(unsigned char s) : m_espc(s) {}
    void getch(tchar& ch, CStyle& sty, unsigned long& pos)
	{
	    parent->getch(ch, sty, pos);
	    sty.setExtraSpace(m_espc);
	}
};
*/

class QTReader;

class HighlightFilter : public CFilter
{
  QTReader* pReader;
  unsigned long lastpos, nextpos;
  unsigned char red, green, blue;
  CList<Bkmk>* bkmks;
 public:
  HighlightFilter(QTReader*);
  void getch(tchar& ch, CStyle& sty, unsigned long& pos);
  void refresh(unsigned long);
  QString about() { return QString("High-lighting filter (c) Tim Wentford\n")+parent->about(); }
};

#ifndef __STATIC
#include <dlfcn.h>

class ExternFilter : public CFilter_IFace
{
  CFilter* filt;
  void *handle;
 public:
  linkType hyperlink(unsigned int n, unsigned int noff, QString& w, QString& nm)
    {
      return filt->hyperlink(n, noff, w, nm);
    }
  QString getTableAsHtml(unsigned long loc)
    {
      qDebug("ExternFilter::getTableAsHtml()");
      return filt->getTableAsHtml(loc);
    }
  void setparent(CCharacterSource* p) { filt->setparent(p); }
  ExternFilter(const QString& nm, const QString& optional);
  ~ExternFilter()
    {
      if (filt != NULL) delete filt;
      if (handle != NULL) dlclose(handle);
    }
  void locate(unsigned int n) { filt->locate(n); }
  bool findanchor(const QString& nm)
    {
      return filt->findanchor(nm);
    }
  void saveposn(const QString& f, size_t posn) { filt->saveposn(f, posn); }
  void writeposn(const QString& f, size_t posn) { filt->writeposn(f, posn); }
  linkType forward(QString& f, size_t& loc) { return filt->forward(f, loc); }
  linkType back(QString& f, size_t& loc) { return filt->back(f, loc); }
  bool hasnavigation() { return filt->hasnavigation(); }
  int getwidth() { return filt->getwidth(); }
  CCharacterSource* getparent() { return filt->getparent(); }
  void getch(tchar& c, CStyle& s, unsigned long& l) { filt->getch(c, s, l); }
  QImage* getPicture(unsigned long tgt) { return filt->getPicture(tgt); }
  CFilter* filter() { return filt; }
  QImage* getPicture(const QString& href) { return filt->getPicture(href); }
  bool getFile(const QString& href, const QString& nm) { return filt->getFile(href, nm); }
  QString about() { return QString("Filter plug-in (c) Tim Wentford\n")+filt->about(); }
  unsigned long startSection() { return filt->startSection(); }
};
#endif

class kern : public CFilter
{
    tchar lastchar;
    bool uselast;
    CStyle laststy;
 public:
    kern() : lastchar(0), uselast(false) {}
    ~kern() {}
    void getch(tchar& ch, CStyle& sty, unsigned long& pos);
    QString about() { return QString("Kerning filter (c) Tim Wentford\n")+parent->about(); }
};

class makeInverse : public CFilter
{
 public:
  void getch(tchar& ch, CStyle& sty, unsigned long& pos);
  QString about() { return QString("Colourmap inversion filter (c) Tim Wentford\n")+parent->about(); }
};
/*
class makeNegative : public CFilter
{
 public:
  void getch(tchar& ch, CStyle& sty, unsigned long& pos);
};
*/
class setbg : public CFilter
{
  int m_r, m_g, m_b;
 public:
  setbg(int _r, int _g, int _b) : m_r(_r), m_g(_g), m_b(_b) {}
  void getch(tchar& ch, CStyle& sty, unsigned long& pos);
  QString about() { return QString("Background colour filter (c) Tim Wentford\n")+parent->about(); }
};

class setfg : public CFilter
{
  int m_r, m_g, m_b;
 public:
  setfg(int _r, int _g, int _b) : m_r(_r), m_g(_g), m_b(_b) {}
  void getch(tchar& ch, CStyle& sty, unsigned long& pos);
  QString about() { return QString("Foreground colour filter (c) Tim Wentford\n")+parent->about(); }
};

class tableLink : public CFilter
{
  QString text;
  int offset;
  int m_r, m_g, m_b;
 public:
  tableLink() : text( "See Table" ), offset(-1)
    {
    }
  void getch(tchar& ch, CStyle& sty, unsigned long& pos);
  QString about() { return QString("Table link filter (c) Tim Wentford\n")+parent->about(); }
};

class underlineLink : public CFilter
{
  bool isLink;
 public:
    underlineLink() : isLink(false) {}
    ~underlineLink() {}
    void getch(tchar& ch, CStyle& sty, unsigned long& pos);
    QString about() { return QString("Link underlining filter (c) Tim Wentford\n")+parent->about(); }
};

#endif