Browse Source

cmSvgWriter.h/c : Added calls to use svg-pan-zoom.js to HTML header.

master
kevin 7 years ago
parent
commit
ca40492412
2 changed files with 54 additions and 14 deletions
  1. 49
    14
      cmSvgWriter.c
  2. 5
    0
      cmSvgWriter.h

+ 49
- 14
cmSvgWriter.c View File

@@ -186,6 +186,7 @@ void _cmSvgWriterFlipY( cmSvg_t* p, unsigned height )
186 186
 }
187 187
 
188 188
 
189
+
189 190
 cmSvgRC_t cmSvgWriterWrite( cmSvgH_t h,  const cmChar_t* cssFn, const cmChar_t* outFn )
190 191
 {
191 192
   cmSvgRC_t   rc        = kOkSvgRC;
@@ -194,17 +195,34 @@ cmSvgRC_t cmSvgWriterWrite( cmSvgH_t h,  const cmChar_t* cssFn, const cmChar_t*
194 195
   double      svgHeight = 0;
195 196
   cmSvgEle_t* e         = p->elist;
196 197
   cmFileH_t   fH        = cmFileNullHandle;
198
+  cmChar_t*   s0        = NULL;
199
+  cmChar_t*   s1        = NULL;
200
+
201
+  cmChar_t hdr[] =
202
+    "<!DOCTYPE html>"
203
+    "<html>"
204
+    "<head>"
205
+    "<meta charset=\"utf-8\">"    
206
+    "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\">"
207
+    "<script type=\"text/javascript\" src=\"svg-pan-zoom.min.js\"></script>"
208
+    "<script>"
209
+    " var panZoom = null;"
210
+    "  function doOnLoad() { panZoom = svgPanZoom(document.querySelector('#mysvg'), { controlIconsEnabled:true } ) }"
211
+    "</script>"
212
+    "</head>"
213
+    "<body onload=\"doOnLoad()\">"
214
+    "<svg id=\"mysvg\" width=\"%f\" height=\"%f\">";
215
+
216
+ 
197 217
   
198 218
   _cmSvgSize(p, &svgWidth, &svgHeight );
199 219
 
200 220
   _cmSvgWriterFlipY( p, svgHeight );
201
-  
202
-  if( cmFileOpen(&fH,outFn,kWriteFileFl,p->err.rpt) != kOkFileRC )
203
-    return cmErrMsg(&p->err,kFileFailSvgRC,"SVG file create failed for '%s'.",cmStringNullGuard(outFn));
204 221
 
205
-  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 )
222
+  // print the file header
223
+  if( (s0 = cmTsPrintfP(s0,hdr,cssFn,svgWidth,svgHeight)) == NULL )
206 224
   {
207
-    rc = cmErrMsg(&p->err,kFileFailSvgRC,"File prefix write failed.");
225
+    rc = cmErrMsg(&p->err,kPrintFailSvgRC,"File prefix write failed.");
208 226
     goto errLabel;
209 227
   }
210 228
 
@@ -213,18 +231,18 @@ cmSvgRC_t cmSvgWriterWrite( cmSvgH_t h,  const cmChar_t* cssFn, const cmChar_t*
213 231
     switch( e->id )
214 232
     {
215 233
       case kRectSvgId:
216
-        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 )
217
-          rc = kFileFailSvgRC;
234
+        if( (s1 = cmTsPrintfP(s1,"<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)) == NULL )
235
+          rc = kPrintFailSvgRC;
218 236
         break;
219 237
         
220 238
       case kLineSvgId:
221
-        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 )
222
-          rc = kFileFailSvgRC;
239
+        if( (s1 = cmTsPrintfP(s1,"<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" class=\"%s\"/>\n",e->x0,e->y0,e->x1,e->y1,e->cssClass)) == NULL )
240
+          rc = kPrintFailSvgRC;
223 241
         break;
224 242
         
225 243
       case kTextSvgId:
226
-        if( cmFilePrintf(fH,"<text x=\"%f\" y=\"%f\" class=\"%s\">%s</text>\n",e->x0,e->y0,e->cssClass,e->text) != kOkFileRC )
227
-          rc = kFileFailSvgRC;        
244
+        if( (s1 = cmTsPrintfP(s1,"<text x=\"%f\" y=\"%f\" class=\"%s\">%s</text>\n",e->x0,e->y0,e->cssClass,e->text)) == NULL )
245
+          rc = kPrintFailSvgRC;        
228 246
         break;
229 247
         
230 248
       default:
@@ -234,21 +252,38 @@ cmSvgRC_t cmSvgWriterWrite( cmSvgH_t h,  const cmChar_t* cssFn, const cmChar_t*
234 252
 
235 253
     if( rc != kOkSvgRC )
236 254
     {
237
-      rc = cmErrMsg(&p->err,kFileFailSvgRC,"Element write failed.");
255
+      rc = cmErrMsg(&p->err,kPrintFailSvgRC,"Element write failed.");
238 256
       break;
239 257
     }
258
+
259
+    s0 = cmTextAppendSS(s0,s1);
260
+    
240 261
   }
241 262
   
242
-  if( cmFilePrint(fH,"</svg>\n</body>\n</html>\n") != kOkFileRC )
263
+  if( (s1 = cmTsPrintfP(s1,"</svg>\n</body>\n</html>\n")) == NULL )
243 264
   {
244
-    rc = cmErrMsg(&p->err,kFileFailSvgRC,"File suffix write failed.");
265
+    rc = cmErrMsg(&p->err,kPrintFailSvgRC,"File suffix write failed.");
245 266
     goto errLabel;
246 267
   }
247 268
 
269
+  if( cmFileOpen(&fH,outFn,kWriteFileFl,p->err.rpt) != kOkFileRC )
270
+  {
271
+    rc = cmErrMsg(&p->err,kFileFailSvgRC,"SVG file create failed for '%s'.",cmStringNullGuard(outFn));
272
+    goto errLabel;
273
+  }
274
+  
275
+  if( cmFilePrint(fH,s0 = cmTextAppendSS(s0,s1)) != kOkFileRC )
276
+  {    
277
+    rc = cmErrMsg(&p->err,kFileFailSvgRC,"File write failed.");
278
+    goto errLabel;
279
+  }
248 280
 
249 281
  errLabel:
250 282
   cmFileClose(&fH);
251 283
 
284
+  cmMemFree(s0);
285
+  cmMemFree(s1);
286
+  
252 287
   return rc;
253 288
 }
254 289
 

+ 5
- 0
cmSvgWriter.h View File

@@ -9,6 +9,7 @@ enum
9 9
 {
10 10
   kOkSvgRC = cmOkRC,
11 11
   kFileFailSvgRC,
12
+  kPrintFailSvgRC,
12 13
   kLHeapFailSvgRC
13 14
 };
14 15
 
@@ -25,6 +26,10 @@ enum
25 26
   cmSvgRC_t cmSvgWriterLine( cmSvgH_t h, double x0, double y0, double x1, double y1, const cmChar_t* cssClassLabel );
26 27
   cmSvgRC_t cmSvgWriterText( cmSvgH_t h, double  x, double y,  const cmChar_t* text, const cmChar_t* cssClassLabel );
27 28
 
29
+  // Write the SVG file.  Note that header on this file references the CSS file 'cssFn'
30
+  // and the Javascript file svg-pan-zoom.min.js from https://github.com/ariutta/svg-pan-zoom.
31
+  // Both the CSS file and svg-pan-zoom.min.js should therefore be in the same directory
32
+  // as the output HTML file.
28 33
   cmSvgRC_t cmSvgWriterWrite( cmSvgH_t h, const cmChar_t* cssFn, const cmChar_t* outFn );
29 34
   
30 35
 #ifdef __cplusplus

Loading…
Cancel
Save