This repository has been archived by the owner on Oct 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
TWRenderManagerTcw.cpp
215 lines (181 loc) · 5.94 KB
/
TWRenderManagerTcw.cpp
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
//
// TWRenderManagerTcw.cpp
// XTBook
//
// Created by Nexhawks on 3/13/11.
// Copyright 2011 Nexhawks. All rights reserved.
//
/*
* This file is part of XTBook.
*
* XTBook 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 3 of the License, or
* (at your option) any later version.
*
* XTBook is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XTBook. If not, see <http://www.gnu.org/licenses/>.
*/
#include "TWRenderManagerTcw.h"
#include <tcw/twFont.h>
#include "TWRenderDCTcw.h"
#include "TWViewerImageManager.h"
#include <tcw/twDC.h>
#include "xpms/ImageNotLoaded.xpm"
#include <tcw/twSDLDC.h>
#include <tcw/twStrConv.h>
#include <exception>
#include "XTBFont.h"
static twDC *m_imageNotLoadedImage=NULL;
twDC *TWRenderManagerTcw::getImageNotLoadedImage(){
if(!m_imageNotLoadedImage){
m_imageNotLoadedImage=twSDLDC::loadFromXPM(ImageNotLoaded);
static_cast<twSDLDC *>(m_imageNotLoadedImage)->optimize();
}
return m_imageNotLoadedImage;
}
TWRenderManagerTcw::TWRenderManagerTcw(const twFont *font,
TWViewerImageManager *mng){
m_font=font;
m_imageManager=mng;
m_size=11;
m_alpha=256;
if(dynamic_cast<const XTBFont *>(m_font))
m_isXTBFont=true;
else
m_isXTBFont=false;
}
TWRenderManagerTcw::~TWRenderManagerTcw(){
}
static twColor twColorFromStyleColor(const TWHTMLStyleColor& styleColor){
return twRGB(styleColor.red, styleColor.green, styleColor.blue);
}
void TWRenderManagerTcw::setStyle(const TWHTMLStyleProxy &style){
m_color=twColorFromStyleColor(style.color());
m_weight=style.fontWeight();
m_size=style.fontSize().toPixels(style.inheritedFontSize(), style);
m_alpha=style.color().alpha;
if(m_alpha==255)
m_alpha=256;
if(m_size<9)
m_size=9;
m_decoration=style.textDecoration();
}
TWSize TWRenderManagerTcw::sizeForString(std::wstring const &str){
twSize size;
if(m_isXTBFont)
size=static_cast<const XTBFont *>(m_font)->measure(str, m_size);
else
size=m_font->measure(str);
return TWSize(size.w, size.h);
}
void TWRenderManagerTcw::drawString(std::wstring const &str, const TWPoint &pt, TWRenderDC *renderDC){
TWRenderDCTcw *renderDCTcw=static_cast<TWRenderDCTcw *>(renderDC);
twDC *dc=renderDCTcw->internalDC();
if(m_isXTBFont){
static_cast<const XTBFont *>(m_font)->render(dc, m_color, twPoint(pt.x, pt.y), str,
m_size, m_alpha);
if(m_weight>300){
static_cast<const XTBFont *>(m_font)->render(dc, m_color, twPoint(pt.x, pt.y), str,
m_size, m_alpha);
}
if(m_weight>150){
static_cast<const XTBFont *>(m_font)->render(dc, m_color, twPoint(pt.x+1, pt.y), str,
m_size, m_alpha);
}
}else{
m_font->render(dc, m_color, twPoint(pt.x, pt.y), str);
if(m_weight>300){
m_font->render(dc, m_color, twPoint(pt.x, pt.y), str);
}
if(m_weight>150){
m_font->render(dc, m_color, twPoint(pt.x+1, pt.y), str);
}
}
if(m_decoration){
TWSize sz=sizeForString(str);
if(m_decoration&TWHTMLStyleTextDecorationUnderline){
dc->fillRect(m_color, twRect(pt.x, pt.y+sz.h-1, sz.w, 1));
}
if(m_decoration&TWHTMLStyleTextDecorationOverline){
dc->fillRect(m_color, twRect(pt.x, pt.y, sz.w, 1));
}
if(m_decoration&TWHTMLStyleTextDecorationLineThrough){
dc->fillRect(m_color, twRect(pt.x, pt.y+sz.h/2, sz.w, 1));
}
}
}
TWSize TWRenderManagerTcw::sizeForImage(const std::wstring &url){
XTBImageArticle *article=m_imageManager->imageArticleForUrl(url);
if(!article){
return TWSize(16,16);
}
twSize s(16,16);
try{
s=article->dimension();
}catch(const std::wstring& err){
XTBLog("Exception while getting dimension of \"%s\": %s", twW2M(url).c_str(),
twW2M(err).c_str());
}catch(const std::string& err){
XTBLog("Exception while getting dimension of \"%s\": %s", twW2M(url).c_str(),
(err).c_str());
}catch(const std::exception& err){
XTBLog("Exception while getting dimension of \"%s\": %s", twW2M(url).c_str(),
(err.what()));
}catch(...){
}
return TWSize(s.w,s.h);
}
void TWRenderManagerTcw::drawImage(const std::wstring &url,
const TWRect &pt, TWRenderDC *dc){
// TODO: support scaling.
XTBImageArticle *article=m_imageManager->imageArticleForUrl(url);
twDC *imageDC=getImageNotLoadedImage();
try{
if(!article){
imageDC=getImageNotLoadedImage();
}else{
imageDC=article->image()->dc();
}
}catch(const std::wstring& err){
XTBLog("Exception while getting image of \"%s\": %s", twW2M(url).c_str(),
twW2M(err).c_str());
}catch(const std::string& err){
XTBLog("Exception while getting image of \"%s\": %s", twW2M(url).c_str(),
(err).c_str());
}catch(const std::exception& err){
XTBLog("Exception while getting image of \"%s\": %s", twW2M(url).c_str(),
(err.what()));
}catch(...){
}
twSize s=imageDC->getClipRect().size();
TWRenderDCTcw *renderDCTcw=static_cast<TWRenderDCTcw *>(dc);
twDC *idc=renderDCTcw->internalDC();
twRect outRect;
if(imageDC==getImageNotLoadedImage()){
outRect.w=std::min(pt.w, s.w);
outRect.h=std::min(pt.h, s.h);
outRect.x=(pt.w-outRect.w)/2+pt.x;
outRect.y=(pt.h-outRect.h)/2+pt.y;
idc->drawRect(0xb0b0b0, twRect(pt.x, pt.y, pt.w-1, pt.h-1));
}else{
outRect=twRect(pt.x, pt.y, pt.w, pt.h);
}
idc->stretchBlt(imageDC, outRect,
twRect(0,0,s.w,s.h));
//idc->bitBlt(imageDC, twPoint(pt.x, pt.y), twRect(0,0,s.w,s.h));
}
TWSize TWRenderManagerTcw::sizeForListMarker(){
return TWSize(3,3);
}
void TWRenderManagerTcw::drawListMarker(const TWRect &rt, TWRenderDC * renderDC){
TWRenderDCTcw *renderDCTcw=static_cast<TWRenderDCTcw *>(renderDC);
twDC *dc=renderDCTcw->internalDC();
twColor col=twRGB(0,82,140);
dc->fillRect(col, twRect(rt.x, rt.y, rt.w, rt.h));
}