summaryrefslogtreecommitdiffstats
path: root/kbarcode/dsrichtext.h
blob: 8381419bf1e5c41ed4dd4db3c9d5bd408279bccb (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
/***************************************************************************
                          dsrichtext.h  -  description
                             -------------------
    begin                : Fre Okt 17 2003
    copyright            : (C) 2003 by Dominik Seichter
    email                : domseichter@web.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef DSRICHTEXT_H
#define DSRICHTEXT_H

#define TQT_TEXT_BUG
/*#undef TQT_TEXT_BUG*/
#ifdef TQT_TEXT_BUG

#include <tqcolor.h>
#include <tqfont.h>
#include <tqstring.h>
#include <tqvaluelist.h>

class DSRichText;

/** This class represents one word,
  * formated with a font, a color and an
  * tqalignment.
  */
struct formated_word {
    TQString text;
    TQFont font;
    TQColor color;
    int tqalignment;
    bool line;
};

typedef TQValueList<formated_word> WordList;

/** This structure represents a single line.
  * Every line contains a list of formated_words.
  * Wordwrapping has been applied before the line
  * was created.
  * lineSpacing, ascent and leading come from the
  * biggest font in the line.
  */ 
struct formated_line {
    int width;
    int lineSpacing;
    int ascent;
    int leading;
    bool line;

    WordList formats;
};
typedef TQValueList<formated_line> LineList;

class TQColor;
class TQPainter;

/** A richtext rendering class, which draws a piece
  * of HTML formated richtext on any TQPainter.
  * This class works with all printer resolutions compared
  * to the buggy TQSimpleRichText
  *
  * rendering the richtext happens in  3 steps:
  * <ul>
  * <li> the HTML data is parsed into text_format structures. Each
  *       of these text_format structures may represend multiple lines, but
  *       also single words. This depends on how long a formated text is.
  *       this step happens in the constructor and in parseParagraph()</li>
  * <li> the text_format structures are parsed into line structures.
  *       In this step the wordwrapping is calculated. Each line structure
  *       has again text_format structures. This is necessary because a line
  *       of text may of course contain different formattings. A line struct is always
  *       a single line of drawn text, not more and not less.
  *       happens in draw() and fillLines()</li>
  * <li> the line structure is drawn line by line using TQPainter.
  *       happens in draw()</li>
  * </ul>
  *
  * Printing to the screen is easy and the usage does not differ from
  * TQSimpleRichText. Drawing on TQPrinter is a little bit more complicated.
  * The most important thing is that you may not use TQPainter::scale().
  * A small example on how to print on a TQPrinter:
  * <pre>
  *  TQPrinter printer( TQPrinter::HighResolution );
  *  TQPainter painter( &printer );
  *  TQPaintDeviceMetrics metrics( &printer );
  *  double scalex = (double)metrics.logicalDpiX() / (double)TQPaintDevice::x11AppDpiX();
  *  double scaley = (double)metrics.logicalDpiY() / (double)TQPaintDevice::x11AppDpiY();
  *
  *  DSRichText dsr( "&lt;html&gt;&lt;p&gt;Hallo World&lt;/p&gt;&lt;/html&gt;" );
  *  dsr.setWidth( 200 ); // in screenresolution coordinates
  *  dsr.setHeight( 80 );  // in screenresolution coordinates
  *  painter.translate( 10 * scalex, 10 * scaley ); // draw at 10, 10 instead of 0, 0
  *  dsr.scale( scalex, scaley );
  *  dsr.draw( &painter );
  * </pre>
  *
  *@author Dominik Seichter
  */
class DSRichText {
    public:
        /** construct a DSRichText object. @p t is formated text as produces by TQTextEdit.
          */
        DSRichText( const TQString & t );
        ~DSRichText();

        void setX( int x );
        void setY( int y );
        
        /** draw the formated text on the TQPainter* @p p
          */
        void draw( TQPainter* p );
        /** set the width of the clipping rectangle
          */
        void setWidth( int width );
        /** set the height of the clipping rectangle
          */
        void setHeight( int height );
        /** scale everything. This is necessary to print on devices
          * with another resolution than the screen. TQPainter::scale() won't work.
          */
        void setScale( double x, double y ) {
            sx = x; sy = y;
        };

    private:
        /** parse a single pare of <p></p> elements
          */
        bool parseParagraph();
        /** remove <p> and </p> from the TQString @p t and return it.
          * This function is also responsible for getting the
          * correct tqalignment of the paragraph.
          */
        TQString parseParagraphTag( const TQString & t, int* tqalignment );
        /** parse the align attribute of a <p> tag and return the corresponding TQt tqalignment value.
          */
        int parseAlignment( const TQString & align );
        /** parse the css style attribute of a <span> tag and return a matching TQFont for these
          * style. The font color is saved in the TQColor* @p color.
          */
        TQFont parseStyle( const TQString & s, TQColor* color );
        TQString parse( const TQString & t, const TQString & find, const TQString & end, int start );
        void parseWords( const TQString & t, formated_word* w, WordList* words );
        void initFormat( formated_word* f, int tqalignment );
        void initLine( TQValueList<formated_line>* l );
        void updateSpacing( TQValueList<formated_line>* l, TQFontMetrics* fm );
        /** draw the line @p line justified as blockquote
          */
        void drawJustified( formated_line* line );
        /** calculate the y-deviation needed, because of different font sizes in this line
          */
        inline int yDeviation( const formated_line* line );
        /** replace HTML escape sequences such as &lt; to their real character meaning (i.e. < )
          */
        TQString replaceEscapeSequences( const TQString & t );

        /** fill the line structures with data
          */
        void fillLines();

        int pos; // current position in text
        int start; // start of a tag
        int end; // end of a tag
        int x; // x position
        int y; // y position
        int w; // width of the text element
        int h; // height of the text element
        int xpos; // x position at the beginning
        int ypos; // y position at the beginning
        
        double sx;
        double sy;
        
        TQString text;
        TQFont m_base;
        TQColor m_color;
        TQPainter* painter;

        TQValueList<LineList> line_p;
        TQValueList<WordList> word_p;
};

#endif // TQT_TEXT_BUG
#endif