gwenhywfar  4.3.3
g_box.c
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Sat Feb 20 2010
3  copyright : (C) 2010 by Martin Preuss
4  email : martin@libchipcard.de
5 
6  ***************************************************************************
7  * Please see toplevel file COPYING for license details *
8  ***************************************************************************/
9 
10 
11 #ifdef HAVE_CONFIG_H
12 # include <config.h>
13 #endif
14 
15 #define DISABLE_DEBUGLOG
16 
17 
18 #include "g_box_p.h"
19 #include "g_generic_l.h"
20 #include "g_table_l.h"
21 #include "g_unorderedlist_l.h"
22 #include "htmlctx_l.h"
23 #include "o_box_l.h"
24 #include "o_word_l.h"
25 #include "o_grid_l.h"
26 #include "o_image_be.h"
27 
28 #include <gwenhywfar/misc.h>
29 #include <gwenhywfar/debug.h>
30 
31 #include <ctype.h>
32 
33 
34 
35 
36 HTML_GROUP *HtmlGroup_Box_new(const char *groupName,
37  HTML_GROUP *parent,
38  GWEN_XML_CONTEXT *ctx) {
39  HTML_GROUP *g;
40 
41  /* create base group */
42  g=HtmlGroup_Generic_new(groupName, parent, ctx);
43  assert(g);
44 
45  /* set virtual functions */
48 
49  return g;
50 }
51 
52 
53 
54 int HtmlGroup_Box_StartTag(HTML_GROUP *g, const char *tagName) {
55  HTML_GROUP *gNew=NULL;
56  GWEN_XML_CONTEXT *ctx;
57  GWEN_DB_NODE *dbAttribs;
58 
59  assert(g);
60 
62  dbAttribs=HtmlCtx_GetCurrentAttributes(ctx);
63 
64  if (strcasecmp(tagName, "b")==0) {
65  /* Create new parser group with new properties but use the same object */
66  HTML_PROPS *pr;
67  HTML_FONT *fnt;
68 
69  gNew=HtmlGroup_Box_new(tagName, g, ctx);
71  fnt=HtmlProps_GetFont(pr);
72  fnt=HtmlCtx_GetFont(ctx,
76  if (fnt) {
77  HtmlProps_SetFont(pr, fnt);
78  //HtmlFont_free(fnt);
79  }
80  HtmlGroup_SetProperties(gNew, pr);
81  HtmlProps_free(pr);
83  }
84  else if (strcasecmp(tagName, "i")==0) {
85  /* Create new parser group with new properties but use the same object */
86  HTML_PROPS *pr;
87  HTML_FONT *fnt;
88 
89  gNew=HtmlGroup_Box_new(tagName, g, ctx);
91  fnt=HtmlProps_GetFont(pr);
92  fnt=HtmlCtx_GetFont(ctx,
96  if (fnt) {
97  HtmlProps_SetFont(pr, fnt);
98  //HtmlFont_free(fnt);
99  }
100  HtmlGroup_SetProperties(gNew, pr);
101  HtmlProps_free(pr);
103  }
104  else if (strcasecmp(tagName, "u")==0) {
105  /* Create new parser group with new properties but use the same object */
106  HTML_PROPS *pr;
107  HTML_FONT *fnt;
108 
109  gNew=HtmlGroup_Box_new(tagName, g, ctx);
111  fnt=HtmlProps_GetFont(pr);
112  fnt=HtmlCtx_GetFont(ctx,
116  if (fnt) {
117  HtmlProps_SetFont(pr, fnt);
118  //HtmlFont_free(fnt);
119  }
120  HtmlGroup_SetProperties(gNew, pr);
121  HtmlProps_free(pr);
123  }
124  else if (strcasecmp(tagName, "p")==0) {
125  HTML_OBJECT *o;
126 
127  gNew=HtmlGroup_Box_new(tagName, g, ctx);
129  o=HtmlObject_Box_new(ctx);
133  if (dbAttribs) {
134  const char *s;
135 
136  s=GWEN_DB_GetCharValue(dbAttribs, "align", 0, "left");
137  if (s) {
138  if (strcasecmp(s, "right")==0)
140  else if (strcasecmp(s, "center")==0)
142  }
143  }
144  HtmlObject_Tree_AddChild(HtmlGroup_GetObject(g), o);
146  HtmlGroup_SetObject(gNew, o);
147  }
148  else if (strcasecmp(tagName, "right")==0) {
149  HTML_OBJECT *o;
150 
151  gNew=HtmlGroup_Box_new(tagName, g, ctx);
153  o=HtmlObject_Box_new(ctx);
158  HtmlObject_Tree_AddChild(HtmlGroup_GetObject(g), o);
160  HtmlGroup_SetObject(gNew, o);
161  }
162  else if (strcasecmp(tagName, "br")==0) {
163  HTML_OBJECT *o;
164 
165  /* just create and add a control object */
168  HtmlObject_Tree_AddChild(HtmlGroup_GetObject(g), o);
170  }
171  else if (strcasecmp(tagName, "img")==0) {
172  HTML_OBJECT *o;
173  GWEN_DB_NODE *dbAttribs;
174 
175  o=HtmlObject_Image_new(ctx);
179  dbAttribs=HtmlCtx_GetCurrentAttributes(ctx);
180  if (dbAttribs) {
181  const char *s;
182  int w;
183  int h;
184 
185  w=GWEN_DB_GetIntValue(dbAttribs, "width", 0, -1);
186  h=GWEN_DB_GetIntValue(dbAttribs, "height", 0, -1);
187 
188  /* preset */
189  if (w!=-1)
191  if (h!=-1)
193 
194  s=GWEN_DB_GetCharValue(dbAttribs, "src", 0, NULL);
195  if (s && *s) {
196  HTML_IMAGE *img;
197 
198  img=HtmlCtx_GetImage(ctx, s);
199  if (img) {
201  /* adjust scaled width and height if not set by attributes */
202  if (w==-1)
204  if (h==-1)
206  }
207  else {
208  DBG_ERROR(GWEN_LOGDOMAIN, "Image [%s] not found", s);
209  }
210  }
211  else {
212  DBG_ERROR(GWEN_LOGDOMAIN, "Missing image name in IMG element");
213  }
214  }
215 
216  HtmlObject_Tree_AddChild(HtmlGroup_GetObject(g), o);
218  }
219  else if (strcasecmp(tagName, "table")==0) {
220  HTML_OBJECT *o;
221 
222  gNew=HtmlGroup_Table_new(tagName, g, ctx);
224  o=HtmlObject_Grid_new(ctx);
226  HtmlGroup_SetObject(gNew, o);
227  HtmlObject_Tree_AddChild(HtmlGroup_GetObject(g), o);
228  }
229  else if (strcasecmp(tagName, "ul")==0) {
230  HTML_OBJECT *o;
231 
232  gNew=HtmlGroup_UnorderedList_new(tagName, g, ctx);
234  o=HtmlObject_Grid_new(ctx);
237  HtmlGroup_SetObject(gNew, o);
238  HtmlObject_Tree_AddChild(HtmlGroup_GetObject(g), o);
239  }
240  else if (strcasecmp(tagName, "font")==0) {
241  /* Create new parser group with new properties but use the same object */
242  HTML_PROPS *pr;
243  GWEN_DB_NODE *dbAttribs;
244 
245  gNew=HtmlGroup_Box_new(tagName, g, ctx);
247 
248  dbAttribs=HtmlCtx_GetCurrentAttributes(ctx);
249  if (dbAttribs) {
250  HTML_FONT *fnt;
251  const char *s;
252  const char *fontName;
253  int fontSize;
254  uint32_t fontFlags;
255 
256  fnt=HtmlProps_GetFont(pr);
257  fontName=GWEN_DB_GetCharValue(dbAttribs, "face", 0, NULL);
258  if (fontName==NULL)
259  fontName=HtmlFont_GetFontName(fnt);
260  fontFlags=HtmlFont_GetFontFlags(fnt);
261  fontSize=HtmlFont_GetFontSize(fnt);
262  s=GWEN_DB_GetCharValue(dbAttribs, "size", 0, NULL);
263  if (s && *s) {
264  if (*s=='+') {
265  int i;
266 
267  sscanf(s, "%d", &i);
268  fontSize+=i*4;
269  }
270  else if (*s=='-') {
271  int i;
272 
273  sscanf(s, "%d", &i);
274  fontSize+=i*4;
275  }
276  else
277  sscanf(s, "%d", &fontSize);
278  }
279 
280  s=GWEN_DB_GetCharValue(dbAttribs, "color", 0, NULL);
281  if (s && *s) {
282  uint32_t color;
283 
284  color=HtmlCtx_GetColorFromName(ctx, s);
285  HtmlProps_SetForegroundColor(pr, color);
286  }
287 
288  fnt=HtmlCtx_GetFont(ctx, fontName, fontSize, fontFlags);
289  if (fnt) {
290  HtmlProps_SetFont(pr, fnt);
291  //HtmlFont_free(fnt);
292  }
293  }
294 
295  HtmlGroup_SetProperties(gNew, pr);
296  HtmlProps_free(pr);
298  }
299  else if (strcasecmp(tagName, "h1")==0) {
300  /* Create new parser group with new properties but use the same object */
301  HTML_PROPS *pr;
302  HTML_FONT *fnt;
303  HTML_OBJECT *o;
304 
305  gNew=HtmlGroup_Box_new(tagName, g, ctx);
307  fnt=HtmlProps_GetFont(pr);
308  fnt=HtmlCtx_GetFont(ctx,
310  HtmlFont_GetFontSize(fnt)*1.8,
312  if (fnt) {
313  HtmlProps_SetFont(pr, fnt);
314  //HtmlFont_free(fnt);
315  }
316  HtmlGroup_SetProperties(gNew, pr);
317 
318  o=HtmlObject_Box_new(ctx);
319  HtmlObject_Tree_AddChild(HtmlGroup_GetObject(g), o);
324  HtmlGroup_SetObject(gNew, o);
325  HtmlProps_free(pr);
326  }
327  else if (strcasecmp(tagName, "h2")==0) {
328  /* Create new parser group with new properties but use the same object */
329  HTML_PROPS *pr;
330  HTML_FONT *fnt;
331  HTML_OBJECT *o;
332 
333  gNew=HtmlGroup_Box_new(tagName, g, ctx);
335  fnt=HtmlProps_GetFont(pr);
336  fnt=HtmlCtx_GetFont(ctx,
338  HtmlFont_GetFontSize(fnt)*1.5,
340  if (fnt) {
341  HtmlProps_SetFont(pr, fnt);
342  //HtmlFont_free(fnt);
343  }
344  HtmlGroup_SetProperties(gNew, pr);
345 
346  o=HtmlObject_Box_new(ctx);
347  HtmlObject_Tree_AddChild(HtmlGroup_GetObject(g), o);
352  HtmlGroup_SetObject(gNew, o);
353  HtmlProps_free(pr);
354  }
355  else if (strcasecmp(tagName, "h3")==0) {
356  /* Create new parser group with new properties but use the same object */
357  HTML_PROPS *pr;
358  HTML_FONT *fnt;
359  HTML_OBJECT *o;
360 
361  gNew=HtmlGroup_Box_new(tagName, g, ctx);
363  fnt=HtmlProps_GetFont(pr);
364  fnt=HtmlCtx_GetFont(ctx,
368  if (fnt) {
369  HtmlProps_SetFont(pr, fnt);
370  //HtmlFont_free(fnt);
371  }
372  HtmlGroup_SetProperties(gNew, pr);
373 
374  o=HtmlObject_Box_new(ctx);
375  HtmlObject_Tree_AddChild(HtmlGroup_GetObject(g), o);
380  HtmlGroup_SetObject(gNew, o);
381  HtmlProps_free(pr);
382  }
383  else if (strcasecmp(tagName, "html")==0 ||
384  strcasecmp(tagName, "body")==0) {
385  }
386  else {
388  "Unknown group [%s], handling as normal box", tagName);
389  gNew=HtmlGroup_Box_new(tagName, g, ctx);
392  }
393 
394  if (gNew) {
395  HtmlCtx_SetCurrentGroup(ctx, gNew);
397  }
398 
399  return 0;
400 }
401 
402 
403 
404 int HtmlGroup_Box_AddData(HTML_GROUP *g, const char *data) {
405  GWEN_XML_CONTEXT *ctx;
406  GWEN_BUFFER *buf;
407  int rv;
408  uint8_t *s;
409  HTML_OBJECT *o;
410 
411  assert(g);
412 
414  if (data && *data) {
415  buf=GWEN_Buffer_new(0, strlen(data), 0, 1);
416  rv=HtmlCtx_SanitizeData(ctx, data, buf);
417  if (rv<0) {
418  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
419  GWEN_Buffer_free(buf);
420  return rv;
421  }
422  if (GWEN_Buffer_GetUsedBytes(buf)) {
423  s=(uint8_t*)GWEN_Buffer_GetStart(buf);
424 
425  while(*s) {
426  uint8_t *t;
427  uint8_t c;
428 
429  /* find begin of word */
430  while(*s && isspace(*s))
431  s++;
432 
433  /* find end of word */
434  t=s;
435  while(*t && !isspace(*t))
436  t++;
437  c=*t;
438  *t=0;
439  o=HtmlObject_Word_new(ctx, (const char*) s);
441  HtmlObject_Tree_AddChild(HtmlGroup_GetObject(g), o);
442  *t=c;
443  s=t;
444  }
445  }
446  GWEN_Buffer_free(buf);
447  }
448 
449  return 0;
450 }
451 
452 
453