Logo ROOT   6.08/07
Reference Guide
TGHtmlImage.cxx
Go to the documentation of this file.
1 // $Id: TGHtmlImage.cxx,v 1.2 2007/05/07 15:28:48 brun Exp $
2 // Author: Valeriy Onuchin 03/05/2007
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2001, Rene Brun, Fons Rademakers and Reiner Rohlfs *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 /**************************************************************************
13 
14  HTML widget for xclass. Based on tkhtml 1.28
15  Copyright (C) 1997-2000 D. Richard Hipp <drh@acm.org>
16  Copyright (C) 2002-2003 Hector Peraza.
17 
18  This library is free software; you can redistribute it and/or
19  modify it under the terms of the GNU Library General Public
20  License as published by the Free Software Foundation; either
21  version 2 of the License, or (at your option) any later version.
22 
23  This library is distributed in the hope that it will be useful,
24  but WITHOUT ANY WARRANTY; without even the implied warranty of
25  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26  Library General Public License for more details.
27 
28  You should have received a copy of the GNU Library General Public
29  License along with this library; if not, write to the Free
30  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 
32 **************************************************************************/
33 
34 // Routines used for processing <IMG> markup
35 
36 #include <string.h>
37 #include <stdlib.h>
38 
39 #include "TGHtml.h"
40 //#include <TGHtmlUri.h>
41 #include "TImage.h"
42 #include "TUrl.h"
43 #include "TSocket.h"
44 #include "TSystem.h"
45 
46 ////////////////////////////////////////////////////////////////////////////////
47 /// ctor.
48 
49 TGHtmlImage::TGHtmlImage(TGHtml *htm, const char *url, const char *width,
50  const char *height)
51 {
52  fHtml = htm;
53  fZUrl = StrDup(url);
54  fZWidth = StrDup(width);
55  fZHeight = StrDup(height);
56  fImage = NULL;
57  fPNext = NULL;
58  fPList = NULL;
59  fW = 0;
60  fH = 0;
61  fTimer = NULL;
62 }
63 
64 ////////////////////////////////////////////////////////////////////////////////
65 /// dtor.
66 
68 {
69  delete [] fZUrl;
70  delete [] fZWidth;
71  delete [] fZHeight;
72 
73  if (fImage) delete fImage;
74  if (fTimer) delete fTimer;
75 }
76 
77 ////////////////////////////////////////////////////////////////////////////////
78 /// Find the alignment for an image
79 
81 {
82  const char *z;
83  int i;
84  int result;
85 
86  static struct {
87  const char *zName;
88  int iValue;
89  } aligns[] = {
90  { "bottom", IMAGE_ALIGN_Bottom },
91  { "baseline", IMAGE_ALIGN_Bottom },
92  { "middle", IMAGE_ALIGN_Middle },
93  { "top", IMAGE_ALIGN_Top },
94  { "absbottom", IMAGE_ALIGN_AbsBottom },
95  { "absmiddle", IMAGE_ALIGN_AbsMiddle },
96  { "texttop", IMAGE_ALIGN_TextTop },
97  { "left", IMAGE_ALIGN_Left },
98  { "right", IMAGE_ALIGN_Right },
99  };
100 
101  z = p->MarkupArg("align", 0);
102  result = IMAGE_ALIGN_Bottom;
103  if (z) {
104  for (i = 0; i < int(sizeof(aligns) / sizeof(aligns[0])); i++) {
105  if (strcasecmp(aligns[i].zName, z) == 0) {
106  result = aligns[i].iValue;
107  break;
108  }
109  }
110  }
111  return result;
112 }
113 
114 ////////////////////////////////////////////////////////////////////////////////
115 /// This routine is called when an image changes. If the size of the
116 /// images changes, then we need to completely redo the layout. If
117 /// only the appearance changes, then this works like an expose event.
118 ///
119 /// pImage - Pointer to an TGHtmlImage object
120 /// newWidth - New width of the image
121 /// newHeight - New height of the image
122 
123 void TGHtml::ImageChanged(TGHtmlImage *pImage, int newWidth, int newHeight)
124 {
125  TGHtmlImageMarkup *pElem;
126 
127  if (pImage->fW != newWidth || pImage->fH != newHeight) {
128  // We have to completely redo the layout after adjusting the size
129  // of the images
130  for (pElem = pImage->fPList; pElem; pElem = pElem->fINext) {
131  pElem->fW = newWidth;
132  pElem->fH = newHeight;
133  }
134  fFlags |= RELAYOUT;
135  pImage->fW = newWidth;
136  pImage->fH = newHeight;
137  RedrawEverything();
138  } else {
139 #if 0
140  for (pElem = pImage->fPList; pElem; pElem = pElem->fINext) {
141  pElem->fRedrawNeeded = 1;
142  }
143  fFlags |= REDRAW_IMAGES;
144  ScheduleRedraw();
145 #else
146  for (pElem = pImage->fPList; pElem; pElem = pElem->fINext) {
147  pElem->fRedrawNeeded = 1;
148  DrawRegion(pElem->fX, pElem->fY - pElem->fAscent, pElem->fW, pElem->fH);
149  }
150 #endif
151  }
152 }
153 
154 ////////////////////////////////////////////////////////////////////////////////
155 /// Given an <IMG> markup, find or create an appropriate TGHtmlImage
156 /// object and return a pointer to that object. NULL might be returned.
157 
159 {
160  const char *zWidth;
161  const char *zHeight;
162  const char *zSrc;
163  TGHtmlImage *pImage;
164 
165  if (p->fType != Html_IMG) { CANT_HAPPEN; return 0; }
166 
167  zSrc = p->MarkupArg("src", 0);
168  if (zSrc == 0) return 0;
169 
170  zSrc = ResolveUri(zSrc);
171  if (zSrc == 0) return 0;
172 
173  zWidth = p->MarkupArg("width", "");
174  zHeight = p->MarkupArg("height", "");
175 
176  //p->w = atoi(fZWidth);
177  //p->h = atoi(zHeight);
178 
179  for (pImage = fImageList; pImage; pImage = pImage->fPNext) {
180  if (strcmp(pImage->fZUrl, zSrc) == 0
181  && strcmp(pImage->fZWidth, zWidth) == 0
182  && strcmp(pImage->fZHeight, zHeight) == 0) {
183  delete [] zSrc;
184  return pImage;
185  }
186  }
187 
188  TImage *img = LoadImage(zSrc, atoi(zWidth), atoi(zHeight));
189 
190  if (img) {
191  pImage = new TGHtmlImage(this, zSrc, zWidth, zHeight);
192  pImage->fImage = img;
193  //if (img->IsAnimated()) {
194  // pImage->timer = new TTimer(this, img->GetAnimDelay());
195  //}
196  ImageChanged(pImage, img->GetWidth(), img->GetHeight());
197  pImage->fPNext = fImageList;
198  fImageList = pImage;
199  } else {
200  pImage = 0;
201  }
202 
203  delete [] zSrc;
204 
205  return pImage;
206 }
207 
208 ////////////////////////////////////////////////////////////////////////////////
209 /// Temporary function to read remote pictures
210 
211 static TImage *ReadRemoteImage(const char *url)
212 {
213  TImage *image = 0;
214  FILE *tmp;
215  char *buf;
216  TUrl fUrl(url);
217 
218  TString msg = "GET ";
219  msg += fUrl.GetProtocol();
220  msg += "://";
221  msg += fUrl.GetHost();
222  msg += ":";
223  msg += fUrl.GetPort();
224  msg += "/";
225  msg += fUrl.GetFile();
226  msg += "\r\n";
227 
228  TString uri(url);
229  if (!uri.BeginsWith("http://") || uri.EndsWith(".html"))
230  return 0;
231  TSocket s(fUrl.GetHost(), fUrl.GetPort());
232  if (!s.IsValid())
233  return 0;
234  if (s.SendRaw(msg.Data(), msg.Length()) == -1)
235  return 0;
236  Int_t size = 1024*1024;
237  buf = (char *)calloc(size, sizeof(char));
238  if (!buf) return 0;
239  if (s.RecvRaw(buf, size) == -1) {
240  free(buf);
241  return 0;
242  }
243  TString pathtmp = TString::Format("%s/%s", gSystem->TempDirectory(),
244  gSystem->BaseName(url));
245  tmp = fopen(pathtmp.Data(), "wb");
246  if (!tmp) {
247  free(buf);
248  return 0;
249  }
250  fwrite(buf, sizeof(char), size, tmp);
251  fclose(tmp);
252  free(buf);
253  image = TImage::Open(pathtmp.Data());
254  if (image && !image->IsValid()) {
255  delete image;
256  image = 0;
257  }
258  gSystem->Unlink(pathtmp.Data());
259  return image;
260 }
261 
262 ////////////////////////////////////////////////////////////////////////////////
263 /// This is the default LoadImage() procedure. It just tries to load the
264 /// image from a file in the local filesystem.
265 
266 TImage *TGHtml::LoadImage(const char *url, int w, int h)
267 {
268  TImage *image = 0;
269 
270  //TGHtmlUri uri(url);
271 
272  TString uri(url);
273  if (uri.BeginsWith("http://") && !uri.EndsWith(".html"))
274  image = ReadRemoteImage(url);
275  else
276  image = TImage::Open(url);
277  if (image) {
278  if (!image->IsValid()) {
279  delete image;
280  image = 0;
281  return 0;
282  }
283  if ((w > 0 && h > 0) && ((w != (int)image->GetWidth()) ||
284  (h != (int)image->GetHeight()))) {
285  image->Scale(w, h);
286  }
287  }
288  return image;
289 }
290 
291 ////////////////////////////////////////////////////////////////////////////////
292 /// Return the height and width, converting to percent if required
293 /// ret must be at least 16 characters long
294 
295 const char *TGHtml::GetPctWidth(TGHtmlElement *p, char *opt, char *ret)
296 {
297  int n, m, val;
298  const char *tz, *z;
299  TGHtmlElement *pElem = p;
300 
301  z = pElem->MarkupArg(opt, "");
302  if (!z) return z;
303  if (!strchr(z, '%')) return z;
304  // coverity[secure_coding]
305  if (!sscanf(z, "%d", &n)) return z;
306  if (n < 0 || n > 100) return z;
307  if (opt[0] == 'h') {
308  val = fCanvas->GetHeight() * 100;
309  } else {
310  val = fCanvas->GetWidth() * 100;
311  }
312  if (!fInTd) {
313  snprintf(ret, 15, "%d", val / n);
314  } else {
315  while (pElem && pElem->fType != Html_TD) pElem = pElem->fPPrev;
316  if (!pElem) return z;
317  tz = pElem->MarkupArg(opt, 0);
318  // coverity[secure_coding]
319  if (tz && !strchr(tz, '%') && sscanf(tz, "%d", &m)) {
320  snprintf(ret, 15, "%d", m * 100 / n);
321  return ret;
322  }
323  pElem = ((TGHtmlCell *)pElem)->fPTable;
324  if (!pElem) return z;
325  tz = pElem->MarkupArg(opt, 0);
326  // coverity[secure_coding]
327  if (tz && !strchr(tz, '%') && sscanf(tz, "%d", &m)) {
328  snprintf(ret, 15, "%d", m * 100 / n);
329  return ret;
330  }
331  return z;
332  }
333  return ret;
334 }
335 
336 ////////////////////////////////////////////////////////////////////////////////
337 /// This routine searchs for an image beneath the coordinates x,y
338 /// and returns the token number of the the image, or -1 if no
339 /// image found.
340 
341 int TGHtml::GetImageAt(int x, int y)
342 {
343  TGHtmlBlock *pBlock;
344  TGHtmlElement *pElem;
345  //int n;
346 
347  for (pBlock = fFirstBlock; pBlock; pBlock = pBlock->fBNext) {
348  if (pBlock->fTop > y || pBlock->fBottom < y ||
349  pBlock->fLeft > x || pBlock->fRight < x) {
350  continue;
351  }
352  for (pElem = pBlock->fPNext; pElem; pElem = pElem->fPNext) {
353  if (pBlock->fBNext && pElem == pBlock->fBNext->fPNext) break;
354  if (pElem->fType == Html_IMG) {
355  return TokenNumber(pElem);
356  }
357  }
358  }
359 
360  return -1;
361 }
virtual const char * BaseName(const char *pathname)
Base name of a file name. Base name of /user/root is root.
Definition: TSystem.cxx:929
char * fZHeight
Definition: TGHtml.h:526
#define IMAGE_ALIGN_Left
Definition: TGHtml.h:570
#define IMAGE_ALIGN_TextTop
Definition: TGHtml.h:567
int fTop
Definition: TGHtml.h:721
virtual UInt_t GetHeight() const
Definition: TImage.h:237
TGHtmlImageMarkup * fINext
Definition: TGHtml.h:557
Html_16_t fAscent
Definition: TGHtml.h:550
char * fZUrl
Definition: TGHtml.h:525
#define REDRAW_IMAGES
Definition: TGHtml.h:1335
Html_u16_t fRight
Definition: TGHtml.h:722
virtual UInt_t GetWidth() const
Definition: TImage.h:236
virtual Bool_t IsValid() const
Definition: TImage.h:238
Html_16_t fX
Definition: TGHtml.h:552
This class represents a WWW compatible URL.
Definition: TUrl.h:41
const char * GetProtocol() const
Definition: TUrl.h:73
TH1 * h
Definition: legend2.C:5
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
An abstract interface to image processing library.
Definition: TImage.h:37
Html_16_t fW
Definition: TGHtml.h:549
Definition: TGHtml.h:876
TGHtmlElement * fPNext
Definition: TGHtml.h:265
const char * GetPctWidth(TGHtmlElement *p, char *opt, char *ret)
Return the height and width, converting to percent if required ret must be at least 16 characters lon...
virtual void Scale(UInt_t, UInt_t)
Definition: TImage.h:149
const char * GetFile() const
Definition: TUrl.h:78
TTimer * fTimer
Definition: TGHtml.h:530
const char * GetHost() const
Definition: TUrl.h:76
virtual int Unlink(const char *name)
Unlink, i.e. remove, a file.
Definition: TSystem.cxx:1347
Double_t x[n]
Definition: legend1.C:17
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString...
Definition: TString.cxx:2335
virtual const char * MarkupArg(const char *, const char *)
Definition: TGHtml.h:258
char * fZWidth
Definition: TGHtml.h:526
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
Definition: TString.cxx:2221
TGHtmlElement * fPPrev
Definition: TGHtml.h:266
virtual const char * TempDirectory() const
Return a user configured or systemwide directory to create temporary files in.
Definition: TSystem.cxx:1448
TGHtmlBlock * fBNext
Definition: TGHtml.h:724
Html_u8_t fRedrawNeeded
Definition: TGHtml.h:546
Html_32_t fY
Definition: TGHtml.h:553
void ImageChanged(TGHtmlImage *image, int newWidth, int newHeight)
This routine is called when an image changes.
#define IMAGE_ALIGN_AbsBottom
Definition: TGHtml.h:569
#define IMAGE_ALIGN_Top
Definition: TGHtml.h:566
R__EXTERN TSystem * gSystem
Definition: TSystem.h:549
TImage * fImage
Definition: TGHtml.h:522
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition: TString.h:558
int GetImageAt(int x, int y)
This routine searchs for an image beneath the coordinates x,y and returns the token number of the the...
TMarker * m
Definition: textangle.C:8
TGHtmlImage * fPNext
Definition: TGHtml.h:527
Ssiz_t Length() const
Definition: TString.h:390
#define calloc
Definition: civetweb.c:819
Html_u8_t fType
Definition: TGHtml.h:268
char * StrDup(const char *str)
Duplicate the string str.
Definition: TString.cxx:2514
virtual ~TGHtmlImage()
dtor.
Definition: TGHtmlImage.cxx:67
static TImage * ReadRemoteImage(const char *url)
Temporary function to read remote pictures.
TGHtmlImage * GetImage(TGHtmlImageMarkup *p)
Given an.
Html_32_t fH
Definition: TGHtml.h:524
TGHtmlImage(const TGHtmlImage &)
#define IMAGE_ALIGN_Middle
Definition: TGHtml.h:565
Html_16_t fH
Definition: TGHtml.h:548
#define free
Definition: civetweb.c:821
Double_t y[n]
Definition: legend1.C:17
Int_t GetPort() const
Definition: TUrl.h:87
#define IMAGE_ALIGN_AbsMiddle
Definition: TGHtml.h:568
you should not use this method at all Int_t Int_t z
Definition: TRolke.cxx:630
Html_u16_t fLeft
Definition: TGHtml.h:722
#define CANT_HAPPEN
Definition: TGHtml.h:63
#define IMAGE_ALIGN_Bottom
Definition: TGHtml.h:564
TGHtml * fHtml
Definition: TGHtml.h:521
#define NULL
Definition: Rtypes.h:82
#define snprintf
Definition: civetweb.c:822
int fBottom
Definition: TGHtml.h:721
Html_32_t fW
Definition: TGHtml.h:523
static TImage * Open(const char *file, EImageFileTypes type=kUnknown)
Open a specified image file.
Definition: TImage.cxx:110
int GetImageAlignment(TGHtmlElement *p)
Find the alignment for an image.
Definition: TGHtmlImage.cxx:80
double result[121]
#define IMAGE_ALIGN_Right
Definition: TGHtml.h:571
virtual TImage * LoadImage(const char *uri, int w=0, int h=0)
This is the default LoadImage() procedure.
virtual const char * MarkupArg(const char *tag, const char *zDefault)
Lookup an argument in the given markup with the name given.
TGHtmlImageMarkup * fPList
Definition: TGHtml.h:528
const Int_t n
Definition: legend1.C:16
#define RELAYOUT
Definition: TGHtml.h:1328
const char * Data() const
Definition: TString.h:349