gwenhywfar  4.7.0beta
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, "h4")==0) {
384  /* Create new parser group with new properties but use the same object */
385  HTML_PROPS *pr;
386  HTML_FONT *fnt;
387  HTML_OBJECT *o;
388 
389  gNew=HtmlGroup_Box_new(tagName, g, ctx);
391  fnt=HtmlProps_GetFont(pr);
392  fnt=HtmlCtx_GetFont(ctx,
396  if (fnt) {
397  HtmlProps_SetFont(pr, fnt);
398  //HtmlFont_free(fnt);
399  }
400  HtmlGroup_SetProperties(gNew, pr);
401 
402  o=HtmlObject_Box_new(ctx);
403  HtmlObject_Tree_AddChild(HtmlGroup_GetObject(g), o);
408  HtmlGroup_SetObject(gNew, o);
409  HtmlProps_free(pr);
410  }
411  else if (strcasecmp(tagName, "html")==0 ||
412  strcasecmp(tagName, "body")==0) {
413  }
414  else {
416  "Unknown group [%s], handling as normal box", tagName);
417  gNew=HtmlGroup_Box_new(tagName, g, ctx);
420  }
421 
422  if (gNew) {
423  HtmlCtx_SetCurrentGroup(ctx, gNew);
425  }
426 
427  return 0;
428 }
429 
430 
431 
432 int HtmlGroup_Box_AddData(HTML_GROUP *g, const char *data) {
433  GWEN_XML_CONTEXT *ctx;
434  GWEN_BUFFER *buf;
435  int rv;
436  uint8_t *s;
437  HTML_OBJECT *o;
438 
439  assert(g);
440 
442  if (data && *data) {
443  buf=GWEN_Buffer_new(0, strlen(data), 0, 1);
444  rv=HtmlCtx_SanitizeData(ctx, data, buf);
445  if (rv<0) {
446  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
447  GWEN_Buffer_free(buf);
448  return rv;
449  }
450  if (GWEN_Buffer_GetUsedBytes(buf)) {
451  s=(uint8_t*)GWEN_Buffer_GetStart(buf);
452 
453  while(*s) {
454  uint8_t *t;
455  uint8_t c;
456 
457  /* find begin of word */
458  while(*s && isspace(*s))
459  s++;
460 
461  /* find end of word */
462  t=s;
463  while(*t && !isspace(*t))
464  t++;
465  c=*t;
466  *t=0;
467  o=HtmlObject_Word_new(ctx, (const char*) s);
469  HtmlObject_Tree_AddChild(HtmlGroup_GetObject(g), o);
470  *t=c;
471  s=t;
472  }
473  }
474  GWEN_Buffer_free(buf);
475  }
476 
477  return 0;
478 }
479 
480 
481