author | llornkcor <llornkcor> | 2002-09-14 12:19:03 (UTC) |
---|---|---|
committer | llornkcor <llornkcor> | 2002-09-14 12:19:03 (UTC) |
commit | d01f8da912a2bed9de1943d174ec10dba505bf38 (patch) (unidiff) | |
tree | 2e09e6dff15e9cc29e730219b2cb0b2773fb4370 /noncore/apps/opie-reader/CDrawBuffer.cpp | |
parent | e82423eb0aa113cf8accd92a698cf236d9b069a6 (diff) | |
download | opie-d01f8da912a2bed9de1943d174ec10dba505bf38.zip opie-d01f8da912a2bed9de1943d174ec10dba505bf38.tar.gz opie-d01f8da912a2bed9de1943d174ec10dba505bf38.tar.bz2 |
add this too
Diffstat (limited to 'noncore/apps/opie-reader/CDrawBuffer.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/apps/opie-reader/CDrawBuffer.cpp | 211 |
1 files changed, 211 insertions, 0 deletions
diff --git a/noncore/apps/opie-reader/CDrawBuffer.cpp b/noncore/apps/opie-reader/CDrawBuffer.cpp new file mode 100644 index 0000000..2ceb2d5 --- a/dev/null +++ b/noncore/apps/opie-reader/CDrawBuffer.cpp | |||
@@ -0,0 +1,211 @@ | |||
1 | |||
2 | #include "CDrawBuffer.h" | ||
3 | #include "FontControl.h" | ||
4 | #include <qfontmetrics.h> | ||
5 | #include <qpainter.h> | ||
6 | |||
7 | void CDrawBuffer::setright(CDrawBuffer& rhs, int f) | ||
8 | { | ||
9 | int i; | ||
10 | // qDebug("Trying 1:%d:%s", f, (const char*)toQString(rhs.data())); | ||
11 | len = rhs.len; | ||
12 | m_maxstyle = rhs.m_maxstyle; | ||
13 | m_ascent = rhs.m_ascent; | ||
14 | m_descent = rhs.m_descent; | ||
15 | m_lineSpacing = rhs.m_lineSpacing; | ||
16 | while (!segs.isEmpty()) segs.erase(0); | ||
17 | for (CList<textsegment>::iterator iter = rhs.segs.begin(); iter != rhs.segs.end(); ) | ||
18 | { | ||
19 | CList<textsegment>::iterator next = iter; | ||
20 | iter++; | ||
21 | if (iter == rhs.segs.end() || iter->start > f) | ||
22 | { | ||
23 | int st = next->start-f; | ||
24 | if (st < 0) st = 0; | ||
25 | segs.push_back(textsegment(st,next->style)); | ||
26 | } | ||
27 | } | ||
28 | for (i = f; rhs[i] != '\0'; i++) (*this)[i-f] = rhs[i]; | ||
29 | (*this)[i-f] = '\0'; | ||
30 | len = i; | ||
31 | // qDebug("Tried 1"); | ||
32 | } | ||
33 | |||
34 | CDrawBuffer& CDrawBuffer::operator=(CDrawBuffer& rhs) | ||
35 | { | ||
36 | int i; | ||
37 | // qDebug("Trying 2"); | ||
38 | len = rhs.len; | ||
39 | m_maxstyle = rhs.m_maxstyle; | ||
40 | m_ascent = rhs.m_ascent; | ||
41 | m_descent = rhs.m_descent; | ||
42 | m_lineSpacing = rhs.m_lineSpacing; | ||
43 | while (!segs.isEmpty()) segs.erase(0); | ||
44 | for (CList<textsegment>::iterator iter = rhs.segs.begin(); iter != rhs.segs.end(); iter++) | ||
45 | { | ||
46 | segs.push_back(*iter); | ||
47 | } | ||
48 | for (i = 0; rhs[i] != '\0'; i++) (*this)[i] = rhs[i]; | ||
49 | (*this)[i] = '\0'; | ||
50 | len = i; | ||
51 | // qDebug("Tried 2"); | ||
52 | return *this; | ||
53 | } | ||
54 | |||
55 | CDrawBuffer& CDrawBuffer::operator=(const tchar*sztmp) | ||
56 | { | ||
57 | int i; | ||
58 | while (!segs.isEmpty()) segs.erase(0); | ||
59 | segs.push_back(textsegment(0, CStyle())); | ||
60 | for (i = 0; sztmp[i] != '\0'; i++) (*this)[i] = sztmp[i]; | ||
61 | (*this)[i] = '\0'; | ||
62 | len = i; | ||
63 | return *this; | ||
64 | } | ||
65 | |||
66 | void CDrawBuffer::empty() | ||
67 | { | ||
68 | len = 0; | ||
69 | (*this)[0] = 0; | ||
70 | while (!segs.isEmpty()) segs.erase(0); | ||
71 | segs.push_back(textsegment(0,CStyle())); | ||
72 | m_maxstyle = m_ascent = m_descent = m_lineSpacing = 0; | ||
73 | } | ||
74 | |||
75 | void CDrawBuffer::addch(tchar ch, CStyle _style/* = ucFontBase*/) | ||
76 | { | ||
77 | if (len == 0) | ||
78 | { | ||
79 | int thissize = fc->getsize(_style); | ||
80 | m_maxstyle = thissize; | ||
81 | m_ascent = fc->ascent(_style); | ||
82 | m_descent = fc->descent(_style); | ||
83 | m_lineSpacing = fc->lineSpacing(_style); | ||
84 | segs.first().start = 0; | ||
85 | segs.first().style = _style; | ||
86 | } | ||
87 | else if (_style != segs.last().style) | ||
88 | { | ||
89 | int thissize = fc->getsize(_style); | ||
90 | if (thissize > m_maxstyle) | ||
91 | { | ||
92 | m_maxstyle = thissize; | ||
93 | m_ascent = fc->ascent(_style); | ||
94 | m_descent = fc->descent(_style); | ||
95 | m_lineSpacing = fc->lineSpacing(_style); | ||
96 | } | ||
97 | segs.push_back(textsegment(len, _style)); | ||
98 | } | ||
99 | (*this)[len++] = ch; | ||
100 | } | ||
101 | |||
102 | void CDrawBuffer::truncate(int n) | ||
103 | { | ||
104 | len = n; | ||
105 | (*this)[n] = 0; | ||
106 | } | ||
107 | |||
108 | int CDrawBuffer::width(int numchars = -1) | ||
109 | { | ||
110 | int currentx = 0, end = 0; | ||
111 | QString text = toQString(data()); | ||
112 | CList<textsegment>::iterator textstart = segs.begin(); | ||
113 | CList<textsegment>::iterator textend = textstart; | ||
114 | do | ||
115 | { | ||
116 | textend++; | ||
117 | end = (textend != segs.end()) ? textend->start : length(); | ||
118 | if (numchars >= 0 && end > numchars) | ||
119 | { | ||
120 | end = numchars; | ||
121 | } | ||
122 | CStyle currentstyle = textstart->style; | ||
123 | QFont f(fc->name(), fc->getsize(currentstyle), (currentstyle.isBold()) ? QFont::Bold : QFont::Normal, (currentstyle.isItalic()) ); | ||
124 | QString str = text.mid(textstart->start, end-textstart->start); | ||
125 | QFontMetrics fm(f); | ||
126 | currentx += fm.width(str); | ||
127 | textstart = textend; | ||
128 | } | ||
129 | while (textend != segs.end() && end != numchars); | ||
130 | return currentx; | ||
131 | } | ||
132 | |||
133 | void CDrawBuffer::render(QPainter* _p, int _y, bool _bMono, int _charWidth, int scwidth) | ||
134 | { | ||
135 | int currentx = 0; | ||
136 | QString text = toQString(data()); | ||
137 | CList<textsegment>::iterator textstart = segs.begin(); | ||
138 | StyleType align = textstart->style.getJustify(); | ||
139 | switch (align) | ||
140 | { | ||
141 | case CStyle::m_AlignRight: | ||
142 | { | ||
143 | // int linelength = width(); | ||
144 | currentx = scwidth - width(); | ||
145 | } | ||
146 | break; | ||
147 | case CStyle::m_AlignCentre: | ||
148 | { | ||
149 | // int linelength = width(); | ||
150 | currentx = (scwidth - width())/2; | ||
151 | } | ||
152 | break; | ||
153 | case CStyle::m_AlignJustify: | ||
154 | case CStyle::m_AlignLeft: | ||
155 | break; | ||
156 | } | ||
157 | CList<textsegment>::iterator textend = textstart; | ||
158 | do | ||
159 | { | ||
160 | textend++; | ||
161 | int end = (textend != segs.end()) ? textend->start : length(); | ||
162 | CStyle currentstyle = textstart->style; | ||
163 | QFont f(fc->name(), fc->getsize(currentstyle), (currentstyle.isBold()) ? QFont::Bold : QFont::Normal, (currentstyle.isItalic()) ); | ||
164 | _p->setFont(f); | ||
165 | QString str = text.mid(textstart->start, end-textstart->start); | ||
166 | _p->setPen(QColor(currentstyle.Red(), currentstyle.Green(), currentstyle.Blue())); | ||
167 | if (_bMono) | ||
168 | { | ||
169 | for (int i = 0; i < str.length(); i++) | ||
170 | { | ||
171 | _p->drawText( currentx + i*_charWidth, _y, QString(str[i])); | ||
172 | } | ||
173 | currentx += str.length()*_charWidth; | ||
174 | } | ||
175 | else | ||
176 | { | ||
177 | _p->drawText( currentx, _y, str); | ||
178 | QFontMetrics fm(f); | ||
179 | currentx += fm.width(str); | ||
180 | } | ||
181 | textstart = textend; | ||
182 | } | ||
183 | while (textend != segs.end()); | ||
184 | } | ||
185 | |||
186 | CStyle CDrawBuffer::laststyle() | ||
187 | { | ||
188 | return segs.last().style; | ||
189 | } | ||
190 | |||
191 | bool CDrawBuffer::isLink(int numchars, size_t& tgt) | ||
192 | { | ||
193 | int end = 0; | ||
194 | CStyle currentstyle; | ||
195 | CList<textsegment>::iterator textstart = segs.begin(); | ||
196 | CList<textsegment>::iterator textend = textstart; | ||
197 | do | ||
198 | { | ||
199 | textend++; | ||
200 | end = (textend != segs.end()) ? textend->start : length(); | ||
201 | if (numchars >= 0 && end > numchars) | ||
202 | { | ||
203 | end = numchars; | ||
204 | } | ||
205 | currentstyle = textstart->style; | ||
206 | textstart = textend; | ||
207 | } | ||
208 | while (textend != segs.end() && end != numchars); | ||
209 | tgt = currentstyle.getData(); | ||
210 | return currentstyle.getLink(); | ||
211 | } | ||