diff --git a/cmSvgWriter.c b/cmSvgWriter.c index 5533bfb..cfb630e 100644 --- a/cmSvgWriter.c +++ b/cmSvgWriter.c @@ -186,6 +186,7 @@ void _cmSvgWriterFlipY( cmSvg_t* p, unsigned height ) } + cmSvgRC_t cmSvgWriterWrite( cmSvgH_t h, const cmChar_t* cssFn, const cmChar_t* outFn ) { cmSvgRC_t rc = kOkSvgRC; @@ -194,17 +195,34 @@ cmSvgRC_t cmSvgWriterWrite( cmSvgH_t h, const cmChar_t* cssFn, const cmChar_t* double svgHeight = 0; cmSvgEle_t* e = p->elist; cmFileH_t fH = cmFileNullHandle; + cmChar_t* s0 = NULL; + cmChar_t* s1 = NULL; + + cmChar_t hdr[] = + "" + "" + "" + "" + "" + "" + "" + "" + "" + ""; + + _cmSvgSize(p, &svgWidth, &svgHeight ); _cmSvgWriterFlipY( p, svgHeight ); - - if( cmFileOpen(&fH,outFn,kWriteFileFl,p->err.rpt) != kOkFileRC ) - return cmErrMsg(&p->err,kFileFailSvgRC,"SVG file create failed for '%s'.",cmStringNullGuard(outFn)); - if( cmFilePrintf(fH,"\n\n\n\n",svgWidth,svgHeight,cssFn) != kOkFileRC ) + // print the file header + if( (s0 = cmTsPrintfP(s0,hdr,cssFn,svgWidth,svgHeight)) == NULL ) { - rc = cmErrMsg(&p->err,kFileFailSvgRC,"File prefix write failed."); + rc = cmErrMsg(&p->err,kPrintFailSvgRC,"File prefix write failed."); goto errLabel; } @@ -213,18 +231,18 @@ cmSvgRC_t cmSvgWriterWrite( cmSvgH_t h, const cmChar_t* cssFn, const cmChar_t* switch( e->id ) { case kRectSvgId: - if( cmFilePrintf(fH,"\n",e->x0,e->y0,e->x1-e->x0,e->y1-e->y0,e->cssClass) != kOkFileRC ) - rc = kFileFailSvgRC; + if( (s1 = cmTsPrintfP(s1,"\n",e->x0,e->y0,e->x1-e->x0,e->y1-e->y0,e->cssClass)) == NULL ) + rc = kPrintFailSvgRC; break; case kLineSvgId: - if( cmFilePrintf(fH,"\n",e->x0,e->y0,e->x1,e->y1,e->cssClass) != kOkFileRC ) - rc = kFileFailSvgRC; + if( (s1 = cmTsPrintfP(s1,"\n",e->x0,e->y0,e->x1,e->y1,e->cssClass)) == NULL ) + rc = kPrintFailSvgRC; break; case kTextSvgId: - if( cmFilePrintf(fH,"%s\n",e->x0,e->y0,e->cssClass,e->text) != kOkFileRC ) - rc = kFileFailSvgRC; + if( (s1 = cmTsPrintfP(s1,"%s\n",e->x0,e->y0,e->cssClass,e->text)) == NULL ) + rc = kPrintFailSvgRC; break; default: @@ -234,21 +252,38 @@ cmSvgRC_t cmSvgWriterWrite( cmSvgH_t h, const cmChar_t* cssFn, const cmChar_t* if( rc != kOkSvgRC ) { - rc = cmErrMsg(&p->err,kFileFailSvgRC,"Element write failed."); + rc = cmErrMsg(&p->err,kPrintFailSvgRC,"Element write failed."); break; } + + s0 = cmTextAppendSS(s0,s1); + } - if( cmFilePrint(fH,"\n\n\n") != kOkFileRC ) + if( (s1 = cmTsPrintfP(s1,"\n\n\n")) == NULL ) { - rc = cmErrMsg(&p->err,kFileFailSvgRC,"File suffix write failed."); + rc = cmErrMsg(&p->err,kPrintFailSvgRC,"File suffix write failed."); goto errLabel; } + if( cmFileOpen(&fH,outFn,kWriteFileFl,p->err.rpt) != kOkFileRC ) + { + rc = cmErrMsg(&p->err,kFileFailSvgRC,"SVG file create failed for '%s'.",cmStringNullGuard(outFn)); + goto errLabel; + } + + if( cmFilePrint(fH,s0 = cmTextAppendSS(s0,s1)) != kOkFileRC ) + { + rc = cmErrMsg(&p->err,kFileFailSvgRC,"File write failed."); + goto errLabel; + } errLabel: cmFileClose(&fH); + cmMemFree(s0); + cmMemFree(s1); + return rc; } diff --git a/cmSvgWriter.h b/cmSvgWriter.h index 83470d0..01214cf 100644 --- a/cmSvgWriter.h +++ b/cmSvgWriter.h @@ -9,6 +9,7 @@ enum { kOkSvgRC = cmOkRC, kFileFailSvgRC, + kPrintFailSvgRC, kLHeapFailSvgRC }; @@ -25,6 +26,10 @@ enum cmSvgRC_t cmSvgWriterLine( cmSvgH_t h, double x0, double y0, double x1, double y1, const cmChar_t* cssClassLabel ); cmSvgRC_t cmSvgWriterText( cmSvgH_t h, double x, double y, const cmChar_t* text, const cmChar_t* cssClassLabel ); + // Write the SVG file. Note that header on this file references the CSS file 'cssFn' + // and the Javascript file svg-pan-zoom.min.js from https://github.com/ariutta/svg-pan-zoom. + // Both the CSS file and svg-pan-zoom.min.js should therefore be in the same directory + // as the output HTML file. cmSvgRC_t cmSvgWriterWrite( cmSvgH_t h, const cmChar_t* cssFn, const cmChar_t* outFn ); #ifdef __cplusplus