53 TGHtmlImage::TGHtmlImage(TGHtml *htm,
const char *url,
const char *width,
58 fZWidth = StrDup(width);
59 fZHeight = StrDup(height);
71 TGHtmlImage::~TGHtmlImage()
77 if (fImage)
delete fImage;
78 if (fTimer)
delete fTimer;
84 int TGHtml::GetImageAlignment(TGHtmlElement *p)
94 {
"bottom", IMAGE_ALIGN_Bottom },
95 {
"baseline", IMAGE_ALIGN_Bottom },
96 {
"middle", IMAGE_ALIGN_Middle },
97 {
"top", IMAGE_ALIGN_Top },
98 {
"absbottom", IMAGE_ALIGN_AbsBottom },
99 {
"absmiddle", IMAGE_ALIGN_AbsMiddle },
100 {
"texttop", IMAGE_ALIGN_TextTop },
101 {
"left", IMAGE_ALIGN_Left },
102 {
"right", IMAGE_ALIGN_Right },
105 z = p->MarkupArg(
"align", 0);
106 result = IMAGE_ALIGN_Bottom;
108 for (i = 0; i < int(
sizeof(aligns) /
sizeof(aligns[0])); i++) {
109 if (strcasecmp(aligns[i].zName, z) == 0) {
110 result = aligns[i].iValue;
127 void TGHtml::ImageChanged(TGHtmlImage *pImage,
int newWidth,
int newHeight)
129 TGHtmlImageMarkup *pElem;
131 if (pImage->fW != newWidth || pImage->fH != newHeight) {
134 for (pElem = pImage->fPList; pElem; pElem = pElem->fINext) {
135 pElem->fW = newWidth;
136 pElem->fH = newHeight;
139 pImage->fW = newWidth;
140 pImage->fH = newHeight;
144 for (pElem = pImage->fPList; pElem; pElem = pElem->fINext) {
145 pElem->fRedrawNeeded = 1;
147 fFlags |= REDRAW_IMAGES;
150 for (pElem = pImage->fPList; pElem; pElem = pElem->fINext) {
151 pElem->fRedrawNeeded = 1;
152 DrawRegion(pElem->fX, pElem->fY - pElem->fAscent, pElem->fW, pElem->fH);
162 TGHtmlImage *TGHtml::GetImage(TGHtmlImageMarkup *p)
169 if (p->fType != Html_IMG) { CANT_HAPPEN;
return 0; }
171 zSrc = p->MarkupArg(
"src", 0);
172 if (zSrc == 0)
return 0;
174 zSrc = ResolveUri(zSrc);
175 if (zSrc == 0)
return 0;
177 zWidth = p->MarkupArg(
"width",
"");
178 zHeight = p->MarkupArg(
"height",
"");
183 for (pImage = fImageList; pImage; pImage = pImage->fPNext) {
184 if (strcmp(pImage->fZUrl, zSrc) == 0
185 && strcmp(pImage->fZWidth, zWidth) == 0
186 && strcmp(pImage->fZHeight, zHeight) == 0) {
192 TImage *img = LoadImage(zSrc, atoi(zWidth), atoi(zHeight));
195 pImage =
new TGHtmlImage(
this, zSrc, zWidth, zHeight);
196 pImage->fImage = img;
200 ImageChanged(pImage, img->GetWidth(), img->GetHeight());
201 pImage->fPNext = fImageList;
215 static TImage *ReadRemoteImage(
const char *url)
222 TString msg =
"GET ";
223 msg += fUrl.GetProtocol();
225 msg += fUrl.GetHost();
227 msg += fUrl.GetPort();
229 msg += fUrl.GetFile();
233 if ((!uri.BeginsWith(
"http://") && !uri.BeginsWith(
"https://")) ||
234 uri.EndsWith(
".html"))
237 if (uri.BeginsWith(
"https://")) {
239 s =
new TSSLSocket(fUrl.GetHost(), fUrl.GetPort());
241 ::Error(
"ReadRemoteImage",
"library compiled without SSL, https not supported");
246 s =
new TSocket(fUrl.GetHost(), fUrl.GetPort());
252 if (s->SendRaw(msg.Data(), msg.Length()) == -1) {
256 Int_t size = 1024*1024;
257 buf = (
char *)calloc(size,
sizeof(
char));
262 if (s->RecvRaw(buf, size) == -1) {
267 TString pathtmp = TString::Format(
"%s/%s", gSystem->TempDirectory(),
268 gSystem->BaseName(url));
269 tmp = fopen(pathtmp.Data(),
"wb");
275 fwrite(buf,
sizeof(
char), size, tmp);
278 image = TImage::Open(pathtmp.Data());
279 if (image && !image->IsValid()) {
283 gSystem->Unlink(pathtmp.Data());
292 TImage *TGHtml::LoadImage(
const char *url,
int w,
int h)
299 if ((uri.BeginsWith(
"http://") || uri.BeginsWith(
"https://")) &&
300 !uri.EndsWith(
".html"))
301 image = ReadRemoteImage(url);
303 image = TImage::Open(url);
305 if (!image->IsValid()) {
310 if ((w > 0 && h > 0) && ((w != (int)image->GetWidth()) ||
311 (h != (
int)image->GetHeight()))) {
322 const char *TGHtml::GetPctWidth(TGHtmlElement *p,
char *opt,
char *ret)
326 TGHtmlElement *pElem = p;
328 z = pElem->MarkupArg(opt,
"");
330 if (!strchr(z,
'%'))
return z;
332 if (!sscanf(z,
"%d", &n))
return z;
333 if (n <= 0 || n > 100)
return z;
335 val = fCanvas->GetHeight() * 100;
337 val = fCanvas->GetWidth() * 100;
340 snprintf(ret, 15,
"%d", val / n);
342 while (pElem && pElem->fType != Html_TD) pElem = pElem->fPPrev;
343 if (!pElem)
return z;
344 tz = pElem->MarkupArg(opt, 0);
346 if (tz && !strchr(tz,
'%') && sscanf(tz,
"%d", &m)) {
347 snprintf(ret, 15,
"%d", m * 100 / n);
350 pElem = ((TGHtmlCell *)pElem)->fPTable;
351 if (!pElem)
return z;
352 tz = pElem->MarkupArg(opt, 0);
354 if (tz && !strchr(tz,
'%') && sscanf(tz,
"%d", &m)) {
355 snprintf(ret, 15,
"%d", m * 100 / n);
368 int TGHtml::GetImageAt(
int x,
int y)
371 TGHtmlElement *pElem;
374 for (pBlock = fFirstBlock; pBlock; pBlock = pBlock->fBNext) {
375 if (pBlock->fTop > y || pBlock->fBottom < y ||
376 pBlock->fLeft > x || pBlock->fRight < x) {
379 for (pElem = pBlock->fPNext; pElem; pElem = pElem->fPNext) {
380 if (pBlock->fBNext && pElem == pBlock->fBNext->fPNext)
break;
381 if (pElem->fType == Html_IMG) {
382 return TokenNumber(pElem);