|
@@ -0,0 +1,234 @@
|
|
1
|
+#include "cmPrefix.h"
|
|
2
|
+#include "cmGlobal.h"
|
|
3
|
+#include "cmFloatTypes.h"
|
|
4
|
+#include "cmRpt.h"
|
|
5
|
+#include "cmErr.h"
|
|
6
|
+#include "cmCtx.h"
|
|
7
|
+#include "cmMem.h"
|
|
8
|
+#include "cmMallocDebug.h"
|
|
9
|
+#include "cmLinkedHeap.h"
|
|
10
|
+#include "cmTime.h"
|
|
11
|
+#include "cmText.h"
|
|
12
|
+#include "cmFile.h"
|
|
13
|
+#include "cmSvgWriter.h"
|
|
14
|
+
|
|
15
|
+enum
|
|
16
|
+{
|
|
17
|
+ kRectSvgId,
|
|
18
|
+ kLineSvgId,
|
|
19
|
+ kTextSvgId
|
|
20
|
+};
|
|
21
|
+
|
|
22
|
+typedef struct cmSvgEle_str
|
|
23
|
+{
|
|
24
|
+ unsigned id;
|
|
25
|
+ double x0;
|
|
26
|
+ double y0;
|
|
27
|
+ double x1;
|
|
28
|
+ double y1;
|
|
29
|
+ cmChar_t* text;
|
|
30
|
+ cmChar_t* cssClass;
|
|
31
|
+ struct cmSvgEle_str* link;
|
|
32
|
+} cmSvgEle_t;
|
|
33
|
+
|
|
34
|
+typedef struct cmSvg_str
|
|
35
|
+{
|
|
36
|
+ cmErr_t err;
|
|
37
|
+ cmLHeapH_t lhH;
|
|
38
|
+ cmSvgEle_t* elist;
|
|
39
|
+ cmSvgEle_t* eol;
|
|
40
|
+} cmSvg_t;
|
|
41
|
+
|
|
42
|
+cmSvgH_t cmSvgNullHandle = cmSTATIC_NULL_HANDLE;
|
|
43
|
+
|
|
44
|
+cmSvg_t* _cmSvgHandleToPtr( cmSvgH_t h )
|
|
45
|
+{
|
|
46
|
+ cmSvg_t* p = (cmSvg_t*)h.h;
|
|
47
|
+ assert(p != NULL );
|
|
48
|
+ return p;
|
|
49
|
+}
|
|
50
|
+
|
|
51
|
+cmSvgRC_t _cmSvgInsertEle( cmSvg_t* p, unsigned id, double x0, double y0, double x1, double y1, const cmChar_t* text, const cmChar_t* class )
|
|
52
|
+{
|
|
53
|
+ cmSvgEle_t* e = cmLhAllocZ(p->lhH,cmSvgEle_t,1);
|
|
54
|
+
|
|
55
|
+ e->id = id;
|
|
56
|
+ e->x0 = x0;
|
|
57
|
+ e->y0 = y0;
|
|
58
|
+ e->x1 = x1;
|
|
59
|
+ e->y1 = y1;
|
|
60
|
+ e->text = text==NULL ? "" : cmLhAllocStr(p->lhH,text);
|
|
61
|
+ e->cssClass = cmLhAllocStr(p->lhH,class);
|
|
62
|
+
|
|
63
|
+ if( p->eol == NULL )
|
|
64
|
+ p->elist = p->eol = e;
|
|
65
|
+ else
|
|
66
|
+ p->eol->link = e;
|
|
67
|
+
|
|
68
|
+ p->eol = e;
|
|
69
|
+
|
|
70
|
+ return kOkSvgRC;
|
|
71
|
+}
|
|
72
|
+
|
|
73
|
+cmSvgRC_t _cmSvgWriterFree( cmSvg_t* p )
|
|
74
|
+{
|
|
75
|
+ cmLHeapDestroy(&p->lhH);
|
|
76
|
+ cmMemFree(p);
|
|
77
|
+ return kOkSvgRC;
|
|
78
|
+}
|
|
79
|
+
|
|
80
|
+cmSvgRC_t cmSvgWriterAlloc( cmCtx_t* ctx, cmSvgH_t* hp )
|
|
81
|
+{
|
|
82
|
+ cmSvgRC_t rc;
|
|
83
|
+ if((rc = cmSvgWriterFree(hp)) != kOkSvgRC )
|
|
84
|
+ return rc;
|
|
85
|
+
|
|
86
|
+ cmSvg_t* p = cmMemAllocZ(cmSvg_t,1);
|
|
87
|
+
|
|
88
|
+ cmErrSetup(&p->err,&ctx->rpt,"SVG Writer");
|
|
89
|
+
|
|
90
|
+ // create a local linked heap
|
|
91
|
+ if( cmLHeapIsValid( p->lhH = cmLHeapCreate(8196,ctx)) == false )
|
|
92
|
+ {
|
|
93
|
+ rc = cmErrMsg(&p->err,kLHeapFailSvgRC,"Lheap create failed.");
|
|
94
|
+ goto errLabel;
|
|
95
|
+ }
|
|
96
|
+
|
|
97
|
+ hp->h = p;
|
|
98
|
+
|
|
99
|
+ errLabel:
|
|
100
|
+ if( rc != kOkSvgRC )
|
|
101
|
+ _cmSvgWriterFree(p);
|
|
102
|
+
|
|
103
|
+ return rc;
|
|
104
|
+}
|
|
105
|
+
|
|
106
|
+cmSvgRC_t cmSvgWriterFree( cmSvgH_t* hp )
|
|
107
|
+{
|
|
108
|
+ cmSvgRC_t rc = kOkSvgRC;
|
|
109
|
+
|
|
110
|
+ if( hp==NULL || cmSvgWriterIsValid(*hp)==false )
|
|
111
|
+ return kOkSvgRC;
|
|
112
|
+
|
|
113
|
+ cmSvg_t* p = _cmSvgHandleToPtr(*hp);
|
|
114
|
+
|
|
115
|
+ if((rc = _cmSvgWriterFree(p)) != kOkSvgRC )
|
|
116
|
+ return rc;
|
|
117
|
+
|
|
118
|
+ hp->h = NULL;
|
|
119
|
+
|
|
120
|
+ return rc;
|
|
121
|
+}
|
|
122
|
+
|
|
123
|
+bool cmSvgWriterIsValid( cmSvgH_t h )
|
|
124
|
+{ return h.h != NULL; }
|
|
125
|
+
|
|
126
|
+cmSvgRC_t cmSvgWriterRect( cmSvgH_t h, double x, double y, double ww, double hh, const cmChar_t* cssClassLabel )
|
|
127
|
+{
|
|
128
|
+ cmSvg_t* p = _cmSvgHandleToPtr(h);
|
|
129
|
+ return _cmSvgInsertEle( p, kRectSvgId, x, y, x+ww, y+hh, NULL, cssClassLabel==NULL?"rectclass":cssClassLabel );
|
|
130
|
+}
|
|
131
|
+
|
|
132
|
+cmSvgRC_t cmSvgWriterLine( cmSvgH_t h, double x0, double y0, double x1, double y1, const cmChar_t* cssClassLabel )
|
|
133
|
+{
|
|
134
|
+ cmSvg_t* p = _cmSvgHandleToPtr(h);
|
|
135
|
+ return _cmSvgInsertEle( p, kLineSvgId, x0, y0, x1, y1, NULL, cssClassLabel==NULL?"lineclass":cssClassLabel );
|
|
136
|
+}
|
|
137
|
+
|
|
138
|
+cmSvgRC_t cmSvgWriterText( cmSvgH_t h, double x, double y, const cmChar_t* text, const cmChar_t* cssClassLabel )
|
|
139
|
+{
|
|
140
|
+ cmSvg_t* p = _cmSvgHandleToPtr(h);
|
|
141
|
+ return _cmSvgInsertEle( p, kTextSvgId, x, y, 0, 0, text==NULL?"":text, cssClassLabel==NULL?"textclass":cssClassLabel );
|
|
142
|
+}
|
|
143
|
+
|
|
144
|
+void _cmSvgSize( cmSvg_t* p, double* widthRef, double* heightRef )
|
|
145
|
+{
|
|
146
|
+ *widthRef = 0;
|
|
147
|
+ *heightRef = 0;
|
|
148
|
+
|
|
149
|
+ if( p->elist == NULL )
|
|
150
|
+ return;
|
|
151
|
+
|
|
152
|
+ cmSvgEle_t* e = p->elist;
|
|
153
|
+ double min_x = cmMin(e->x0,e->x1);
|
|
154
|
+ double max_x = cmMax(e->x0,e->x1);
|
|
155
|
+ double min_y = cmMin(e->y0,e->y1);
|
|
156
|
+ double max_y = cmMax(e->y0,e->y1);
|
|
157
|
+
|
|
158
|
+ for(e=e->link; e!=NULL; e=e->link)
|
|
159
|
+ {
|
|
160
|
+ min_x = cmMin(cmMin(min_x,e->x0),e->x1);
|
|
161
|
+ max_x = cmMax(cmMax(max_x,e->x0),e->x1);
|
|
162
|
+ min_y = cmMin(cmMin(min_y,e->y0),e->y1);
|
|
163
|
+ max_y = cmMax(cmMax(max_y,e->y0),e->y1);
|
|
164
|
+ }
|
|
165
|
+
|
|
166
|
+ *widthRef = max_x - min_x;
|
|
167
|
+ *heightRef = max_y - min_y;
|
|
168
|
+}
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+cmSvgRC_t cmSvgWriterWrite( cmSvgH_t h, const cmChar_t* cssFn, const cmChar_t* outFn )
|
|
172
|
+{
|
|
173
|
+ cmSvgRC_t rc = kOkSvgRC;
|
|
174
|
+ cmSvg_t* p = _cmSvgHandleToPtr(h);
|
|
175
|
+ double svgWidth = 0;
|
|
176
|
+ double svgHeight = 0;
|
|
177
|
+ cmSvgEle_t* e = p->elist;
|
|
178
|
+ cmFileH_t fH = cmFileNullHandle;
|
|
179
|
+
|
|
180
|
+ _cmSvgSize(p, &svgWidth, &svgHeight );
|
|
181
|
+
|
|
182
|
+ if( cmFileOpen(&fH,outFn,kWriteFileFl,p->err.rpt) != kOkFileRC )
|
|
183
|
+ return cmErrMsg(&p->err,kFileFailSvgRC,"SVG file create failed for '%s'.",cmStringNullGuard(outFn));
|
|
184
|
+
|
|
185
|
+ if( cmFilePrintf(fH,"<!DOCTYPE html>\n<html>\n<head><link rel=\"stylesheet\" type=\"text/css\" href=\"%s\"></head><body>\n<svg width=\"%f\" height=\"%f\">\n",svgWidth,svgHeight,cssFn) != kOkFileRC )
|
|
186
|
+ {
|
|
187
|
+ rc = cmErrMsg(&p->err,kFileFailSvgRC,"File prefix write failed.");
|
|
188
|
+ goto errLabel;
|
|
189
|
+ }
|
|
190
|
+
|
|
191
|
+ for(; e!=NULL; e=e->link)
|
|
192
|
+ {
|
|
193
|
+ switch( e->id )
|
|
194
|
+ {
|
|
195
|
+ case kRectSvgId:
|
|
196
|
+ if( cmFilePrintf(fH,"<rect x=\"%f\" y=\"%f\" width=\"%f\" height=\"%f\" class=\"%s\"/>\n",e->x0,e->y0,e->x1-e->x0,e->y1-e->y0,e->cssClass) != kOkFileRC )
|
|
197
|
+ rc = kFileFailSvgRC;
|
|
198
|
+ break;
|
|
199
|
+
|
|
200
|
+ case kLineSvgId:
|
|
201
|
+ if( cmFilePrintf(fH,"<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" class=\"%s\"/>\n",e->x0,e->y0,e->x1,e->y1,e->cssClass) != kOkFileRC )
|
|
202
|
+ rc = kFileFailSvgRC;
|
|
203
|
+ break;
|
|
204
|
+
|
|
205
|
+ case kTextSvgId:
|
|
206
|
+ if( cmFilePrintf(fH,"<text x=\"%f\" y=\"%f\" class=\"%s\">%s</text>\n",e->x0,e->y0,e->cssClass,e->text) != kOkFileRC )
|
|
207
|
+ rc = kFileFailSvgRC;
|
|
208
|
+ break;
|
|
209
|
+
|
|
210
|
+ default:
|
|
211
|
+ { assert(0); }
|
|
212
|
+
|
|
213
|
+ }
|
|
214
|
+
|
|
215
|
+ if( rc != kOkSvgRC )
|
|
216
|
+ {
|
|
217
|
+ rc = cmErrMsg(&p->err,kFileFailSvgRC,"Element write failed.");
|
|
218
|
+ break;
|
|
219
|
+ }
|
|
220
|
+ }
|
|
221
|
+
|
|
222
|
+ if( cmFilePrint(fH,"</svg>\n</body>\n</html>\n") != kOkFileRC )
|
|
223
|
+ {
|
|
224
|
+ rc = cmErrMsg(&p->err,kFileFailSvgRC,"File suffix write failed.");
|
|
225
|
+ goto errLabel;
|
|
226
|
+ }
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+ errLabel:
|
|
230
|
+ cmFileClose(&fH);
|
|
231
|
+
|
|
232
|
+ return rc;
|
|
233
|
+}
|
|
234
|
+
|