~ubuntu-branches/ubuntu/lucid/graphviz/lucid-updates

« back to all changes in this revision

Viewing changes to lib/gvc/gvusershape.c

  • Committer: Bazaar Package Importer
  • Author(s): Bryce Harrington
  • Date: 2008-06-19 20:23:23 UTC
  • mfrom: (1.2.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20080619202323-ls23h96ntj9ny94m
Tags: 2.18-1ubuntu1
* Merge from debian unstable, remaining changes:
  - Build depend on liblualib50-dev instead of liblua5.1-0-dev.
  - Drop libttf-dev (libttf-dev is in universe) (LP: #174749).
  - Replace gs-common with ghostscript.
  - Build-depend on python-dev instead of python2.4-dev or python2.5-dev.
  - Mention the correct python version for the python bindings in the
    package description.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: gvusershape.c,v 1.31 2007/09/07 20:28:24 ellson Exp $ $Revision: 1.31 $ */
 
1
/* $Id: gvusershape.c,v 1.34 2007/12/27 22:09:22 ellson Exp $ $Revision: 1.34 $ */
2
2
/* vim:set shiftwidth=4 ts=8: */
3
3
 
4
4
/**********************************************************
22
22
#include <string.h>
23
23
 
24
24
#include "types.h"
 
25
#include "logic.h"
25
26
#include "memory.h"
26
27
#include "graph.h"
27
28
#include "agxbuf.h"
47
48
#define JPEG_MAGIC "\xFF\xD8\xFF\xE0"
48
49
#define PDF_MAGIC  "%PDF-"
49
50
#define EPS_MAGIC  "\xC5\xD0\xD3\xC6"
 
51
#define XML_MAGIC  "<?xml"
 
52
#define SVG_MAGIC  "<svg"
50
53
 
51
54
static knowntype_t knowntypes[] = {
52
55
    { PNG_MAGIC,  sizeof(PNG_MAGIC)-1,  FT_PNG,  "png",  },
56
59
    { JPEG_MAGIC, sizeof(JPEG_MAGIC)-1, FT_JPEG, "jpeg", },
57
60
    { PDF_MAGIC,  sizeof(PDF_MAGIC)-1,  FT_PDF,  "pdf",  },
58
61
    { EPS_MAGIC,  sizeof(EPS_MAGIC)-1,  FT_EPS,  "eps",  },
 
62
/*    { SVG_MAGIC,  sizeof(SVG_MAGIC)-1,  FT_SVG,  "svg",  },  - viewers expect xml preamble */
 
63
    { XML_MAGIC,  sizeof(XML_MAGIC)-1,  FT_XML,  "xml",  },
59
64
};
60
65
 
61
66
static int imagetype (usershape_t *us)
62
67
{
63
68
    char header[HDRLEN];
 
69
    char line[200];
64
70
    int i;
65
71
 
66
72
    if (us->f && fread(header, 1, HDRLEN, us->f) == HDRLEN) {
67
73
        for (i = 0; i < sizeof(knowntypes) / sizeof(knowntype_t); i++) {
68
74
            if (!memcmp (header, knowntypes[i].template, knowntypes[i].size)) {
69
75
                us->stringtype = knowntypes[i].stringtype;
70
 
                return (us->type = knowntypes[i].type);
 
76
                us->type = knowntypes[i].type;
 
77
                if (us->type != FT_XML)
 
78
                    return us->type;
 
79
                /* check for SVG in case of XML */
 
80
                while (fgets(line, sizeof(line), us->f) != NULL) {
 
81
                    if (!memcmp(line, SVG_MAGIC, sizeof(SVG_MAGIC)-1)) {
 
82
                        us->stringtype = "svg";
 
83
                        return (us->type = FT_SVG);
 
84
                    }
 
85
                }
71
86
            }
72
87
        }
73
88
    }
 
89
 
74
90
    us->stringtype = "(lib)";
75
91
    us->type = FT_NULL;
 
92
 
76
93
    return FT_NULL;
77
94
}
78
95
    
105
122
    return TRUE;
106
123
}
107
124
 
 
125
static unsigned int svg_units_convert(double n, char *u)
 
126
{
 
127
    if (strcmp(u, "in") == 0)
 
128
        return ROUND(n * POINTS_PER_INCH);
 
129
    if (strcmp(u, "px") == 0)
 
130
        return ROUND(n * POINTS_PER_INCH / 96);
 
131
    if (strcmp(u, "pc") == 0)
 
132
        return ROUND(n * POINTS_PER_INCH / 6); 
 
133
    if (strcmp(u, "pt") == 0 || strcmp(u, "\"") == 0)   /* ugly!!  - if there are no inits then the %2s get the trailing '"' */
 
134
        return ROUND(n);
 
135
    if (strcmp(u, "cm") == 0)
 
136
        return ROUND(n * POINTS_PER_CM);
 
137
    if (strcmp(u, "mm") == 0)
 
138
        return ROUND(n * POINTS_PER_MM);
 
139
    return 0;
 
140
}
 
141
 
 
142
static void svg_size (usershape_t *us)
 
143
{
 
144
    unsigned int w = 0, h = 0;
 
145
    double n;
 
146
    char u[10];
 
147
    char *token;
 
148
    char line[200];
 
149
    bool wFlag = false, hFlag = false;
 
150
 
 
151
    fseek(us->f, -strlen(line), SEEK_CUR);
 
152
    while (fgets(line, sizeof(line), us->f) != NULL && (!wFlag || !hFlag)) {
 
153
        token = strtok(line, " ");
 
154
        while (token != NULL && token[strlen(token)-1] != '>') {
 
155
            if (sscanf(token, "width=\"%lf%2s\"", &n, u) == 2) {
 
156
                w = svg_units_convert(n, u);
 
157
                wFlag = true;
 
158
                if (hFlag)
 
159
                    break;
 
160
            }
 
161
            if (sscanf(token, "height=\"%lf%2s\"", &n, u) == 2) {
 
162
                h = svg_units_convert(n, u);
 
163
                hFlag = true;
 
164
                if (wFlag)
 
165
                    break;
 
166
            }
 
167
            token =  strtok(NULL, " ");
 
168
        }
 
169
    }
 
170
    us->dpi = 72;
 
171
    us->w = w;
 
172
    us->h = h;
 
173
}
 
174
 
108
175
static void png_size (usershape_t *us)
109
176
{
110
177
    unsigned int w, h;
317
384
            case FT_PS:
318
385
                ps_size(us);
319
386
                break;
 
387
            case FT_SVG:
 
388
                svg_size(us);
 
389
                break;
320
390
            case FT_PDF:   /* no pdf_size code available */
321
391
            case FT_EPS:   /* no eps_size code available */
322
392
            default: