File: html2png.c

package info (click to toggle)
qemacs 0.3.1.cvs.20050713-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,168 kB
  • ctags: 3,413
  • sloc: ansic: 31,131; sh: 582; makefile: 369
file content (495 lines) | stat: -rw-r--r-- 11,391 bytes parent folder | download | duplicates (2)
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/* 
 * HTML to PPM converter using the qHTML library 
 * Copyright (c) 2002 Fabrice Bellard.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include "qe.h"
#include "css.h"
#include "cfb.h"

#ifdef CONFIG_PNG_OUTPUT
#include <png.h>
#endif

#define DEFAULT_WIDTH 640
#ifdef CONFIG_PNG_OUTPUT
#define DEFAULT_OUTFILENAME "a.png"
#else
#define DEFAULT_OUTFILENAME "a.ppm"
#endif

/* file I/O for the qHTML library */

CSSFile *css_open(CSSContext *s, const char *filename)
{
    FILE *f;
    f = fopen(filename, "rb");
    return (CSSFile *)f;
}

int css_filesize(CSSFile *f1)
{
    FILE *f = (FILE *)f1;
    int pos, size;

    pos = ftell(f);
    fseek(f, 0, SEEK_END);
    size = ftell(f);
    fseek(f, pos, SEEK_SET);
    return size;
}

int css_read(CSSFile *f1, char *buf, int size)
{
    FILE *f = (FILE *)f1;

    return fread(buf, 1, size, f);
}

void css_close(CSSFile *f1)
{
    FILE *f = (FILE *)f1;
    fclose(f);
}

/* error display */

void css_error(const char *filename, int line_num, 
               const char *msg)
{
    fprintf(stderr, "%s:%d: %s\n", filename, line_num, msg);
}

void put_error(EditState *s, const char *fmt, ...)
{
    va_list ap;

    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    va_end(ap);
    putc('\n', stderr);
}


/* dummy functions */
int eb_nextc(EditBuffer *b, int offset, int *next_ptr)
{
    return 0;
}

/* find a resource file */
/* XXX: suppress that */
int find_resource_file(char *path, int path_size, const char *pattern)
{
    return -1;
}

/* display driver based on cfb driver */

static int ppm_init(QEditScreen *s, int w, int h);
static void ppm_close(QEditScreen *s);

static QEDisplay ppm_dpy = {
    "ppm",
    NULL,
    ppm_init,
    ppm_close,
};

/* realloc ppm bitmap */
int ppm_resize(QEditScreen *s, int w, int h)
{
    CFBContext *cfb = s->private;
    unsigned char *data;
    
    /* alloc bitmap */
    data = realloc(cfb->base, w * h * sizeof(int));
    if (!data) {
        return -1;
    }
    cfb->base = data;
    cfb->wrap = w * sizeof(int);
    s->width = w;
    s->height = h;

    s->clip_x1 = 0;
    s->clip_y1 = 0;
    s->clip_x2 = s->width;
    s->clip_y2 = s->height;
    return 0;
}

static int ppm_init(QEditScreen *s, int w, int h)
{
    CFBContext *cfb;

    memcpy(&s->dpy, &ppm_dpy, sizeof(QEDisplay));

    cfb = malloc(sizeof(CFBContext));
    if (!cfb)
        return -1;

    s->private = cfb;
    s->media = CSS_MEDIA_SCREEN;

    if (cfb_init(s, NULL, w * sizeof(int), 32, ".") < 0)
        goto fail;

    if (ppm_resize(s, w, h) < 0) {
    fail:
        free(cfb);
        return -1;
    }
    return 0;
}

static void ppm_close(QEditScreen *s)
{
    CFBContext *cfb = s->private;
    
    free(cfb->base);
    free(cfb);
}

int ppm_save(QEditScreen *s, const char *filename)
{
    CFBContext *cfb = s->private;
    int w, h, x, y;
    unsigned int r, g, b, v;
    unsigned int *data;
    FILE *f;

    f = fopen(filename, "w");
    if (!f)
        return -1;
    data = (unsigned int *)cfb->base;
    w = s->width;
    h = s->height;

    fprintf(f, "P6\n%d %d\n%d\n", w, h, 255);
    for (y = 0; y < h; y++) {
        for (x = 0; x < w; x++) {
            v = data[x];
            r = (v >> 16) & 0xff;
            g = (v >> 8) & 0xff;
            b = (v) & 0xff;
            fputc(r, f);
            fputc(g, f);
            fputc(b, f);
        }
        data = (void *)((char *)data + cfb->wrap);
    }
    fclose(f);
    return 0;
}

#ifdef CONFIG_PNG_OUTPUT
extern void png_write_init();

int png_save(QEditScreen *s, const char *filename)
{
    CFBContext *cfb = s->private;
    png_struct * volatile png_ptr = NULL;
    png_info * volatile info_ptr = NULL;
    png_byte *row_ptr, *row_pointers[1], *row = NULL;
    int w, h, x, y;
    unsigned int r, g, b, v;
    unsigned int *data;
    FILE * volatile f = NULL;

    row = malloc(3 * s->width);
    if (!row)
        goto fail;
    png_ptr = malloc(sizeof (png_struct));
    if (!png_ptr)
        goto fail;
    info_ptr = malloc(sizeof (png_info));
    if (!info_ptr)
        goto fail;
    
    f = fopen(filename, "w");
    if (!f) 
        goto fail;

    if (setjmp(png_ptr->jmpbuf)) {
        png_write_destroy(png_ptr);
    fail:
        /* free pointers before returning.  Make sure you clean up
           anything else you've done. */
        free(png_ptr);
        free(info_ptr);
        free(row);
        if (f)
            fclose(f);
        return -1;
    }

    png_info_init(info_ptr);
    png_write_init(png_ptr);
    png_init_io(png_ptr, f);
    
    data = (unsigned int *)cfb->base;
    w = s->width;
    h = s->height;

    info_ptr->width = w;
    info_ptr->height = h;
    info_ptr->bit_depth = 8;
    info_ptr->color_type = PNG_COLOR_TYPE_RGB;

    png_write_info(png_ptr, info_ptr);

    row_pointers[0] = row;

    for (y = 0; y < h; y++) {
        row_ptr = row;
        for (x = 0; x < w; x++) {
            v = data[x];
            r = (v >> 16) & 0xff;
            g = (v >> 8) & 0xff;
            b = (v) & 0xff;
            row_ptr[0] = r;
            row_ptr[1] = g;
            row_ptr[2] = b;
            row_ptr += 3;
        }
        png_write_rows(png_ptr, row_pointers, 1);
        data = (void *)((char *)data + cfb->wrap);
    }
    png_write_end(png_ptr, info_ptr);
    png_write_destroy(png_ptr);

    free(png_ptr);
    free(info_ptr);
    free(row);
    fclose(f);
    return 0;
}

#endif

#if 0
void test_display(QEditScreen *screen)
{
    QEFont *font;
    unsigned int buf[256];
    int len;
    
    fill_rectangle(screen, 0, 0, screen->width, screen->height, 
                   QERGB(0xff, 0x00, 0x00));
    len = utf8_to_unicode(buf, sizeof(buf), "Hello World !");
    
    font = select_font(screen, QE_FAMILY_FIXED | QE_STYLE_NORM, 12);
    
    draw_text(screen, font, screen->width / 2, screen->height / 2, 
              buf, len, QERGB(0x00, 0x00, 0x00));

    release_font(screen, font);
}
#endif

extern const char html_style[];

static int html_test_abort(void *opaque)
{
    return 0;
}

#define IO_BUF_SIZE 4096

int draw_html(QEditScreen *scr, 
              const char *filename, QECharset *charset, int flags)
{
    CSSContext *s = NULL;
    CSSBox *top_box = NULL;
    XMLState *xml;
    CSSFile *f = NULL;
    int len;
    char buf[IO_BUF_SIZE];
    CSSRect rect;
    int page_height;

    s = css_new_document(scr, NULL);
    if (!s)
        return -1;

    /* prepare default style sheet */
    s->style_sheet = css_new_style_sheet();

    css_parse_style_sheet_str(s->style_sheet, html_style, 
                              flags);
    
    /* default colors */
    s->selection_bgcolor = QERGB(0x00, 0x00, 0xff);
    s->selection_fgcolor = QERGB(0x00, 0x00, 0x00);
    s->default_bgcolor = QERGB(0xbb, 0xbb, 0xbb);

    /* parse HTML file */

    f = css_open(s, filename);
    if (!f)
        goto fail;

    xml = xml_begin(s->style_sheet, flags, html_test_abort, NULL, filename, charset);
    
    for (;;) {
        len = css_read(f, buf, IO_BUF_SIZE);
        if (len <= 0)
            break;
        xml_parse(xml, buf, len);
    }
    
    css_close(f);

    top_box = xml_end(xml);
    
    /* CSS computation */
    css_compute(s, top_box);

    /* CSS layout */
    css_layout(s, top_box, scr->width, html_test_abort, NULL);
    
    /* now we know the total size, so we allocate the ppm */
    page_height = top_box->bbox.y2;
    
    if (ppm_resize(scr, scr->width, page_height) < 0)
        goto fail;

    /* CSS display */
    rect.x1 = 0;
    rect.y1 = 0;
    rect.x2 = scr->width;
    rect.y2 = scr->height;

    css_display(s, top_box, &rect, 0, 0);
    
    css_delete_box(top_box);
    css_delete_document(s);
    return 0;
 fail:
    if (f)
        css_close(f);
    if (top_box)
        css_delete_box(top_box);
    if (s)
        css_delete_document(s);
    return -1;
}

void help(void)
{
    printf("html2png version %s (c) 2002 Fabrice Bellard\n"
           "\n"
           "usage: html2png [-h] [-x] [-w width] [-o outfile] [-f charset] infile\n"
           "Convert the HTML page 'infile' into the png/ppm image file 'outfile'\n"
           "\n"
           "-h         : display this help\n"
           "-x         : use strict XML parser (xhtml type parsing)\n"
           "-w width   : set the image width (default=%d)\n"
           "-f charset : set the default charset (default='%s')\n"
           "             use -f ? to list supported charsets\n"
           "-o outfile : set the output filename (default='%s')\n",
           QE_VERSION, 
           DEFAULT_WIDTH,
           "8859-1",
           DEFAULT_OUTFILENAME);
}

/* XXX: use module system */
extern int charset_more_init(void);

int main(int argc, char **argv)
{
    QEDisplay *dpy;
    QEditScreen screen1, *screen = &screen1;
    int page_width, c, strict_xml, flags;
    char *outfilename, *infilename;
    QECharset *charset;

    charset_init();
    charset_more_init();
    css_init();

    page_width = DEFAULT_WIDTH; 
    outfilename = DEFAULT_OUTFILENAME;
    charset = &charset_8859_1;
    strict_xml = 0;
    
    for (;;) {
        c = getopt(argc, argv, "h?w:o:f:x");
        if (c == -1)
            break;
        switch (c) {
        case 'h':
        case '?':
            help();
            exit(1);
        case 'w':
            page_width = atoi(optarg);
            break;
        case 'o':
            outfilename = optarg;
            break;
        case 'f':
            charset = find_charset(optarg);
            if (!charset) {
                QECharset *p;
                fprintf(stderr, "Unknown charset '%s'\n", optarg);
                fprintf(stderr, "Supported charsets are:");
                for (p = first_charset; p != NULL; p = p->next)
                    fprintf(stderr, " %s", p->name);
                fprintf(stderr, "\n");
                exit(1);
            }
            break;
        case 'x':
            strict_xml = 1;
            break;
        }
    }
    if (optind >= argc) {
        help();
        exit(1);
    }
    infilename = argv[optind];

    /* init display driver with dummy height */
    dpy = &ppm_dpy;
    if (dpy->dpy_init(screen, page_width, 1) < 0) {
        fprintf(stderr, "Could not init display driver\n");
        exit(1);
    }

    flags = XML_HTML;
    if (!strict_xml)
        flags |= XML_IGNORE_CASE | XML_HTML_SYNTAX;

    draw_html(screen, infilename, charset, flags);

    /* save ppm file */
#ifdef CONFIG_PNG_OUTPUT
    if (strstr(outfilename, ".ppm"))
        ppm_save(screen, outfilename);
    else
        png_save(screen, outfilename);
#else    
    ppm_save(screen, outfilename);
#endif

    /* close screen */
    screen->dpy.dpy_close(screen);
    return 0;
}