// ---------------------------------------------------------------------- // System Headers // ---------------------------------------------------------------------- #include #include #include // ---------------------------------------------------------------------- // Local Headers // ---------------------------------------------------------------------- #include "util.h" #include "svg.h" // ---------------------------------------------------------------------- // Local Functions // ---------------------------------------------------------------------- static Colors getColors() { static bool first = true; static Colors colors = { 0 }; // ---------------------------------------------------------------------- // Because each field in 'Colors' has a name // that's identical to the string it will hold // (so "red" will be stored in colors.red) we // can use a macro to avoid typing the name twice // ---------------------------------------------------------------------- #define CINIT(structName, colorName) structName.colorName = #colorName if(first) { first = false; CINIT(colors, none); CINIT(colors, red); CINIT(colors, green); CINIT(colors, blue); CINIT(colors, black); CINIT(colors, cyan); CINIT(colors, magenta); CINIT(colors, yellow); } return(colors); } static void svgHeader(SVG *svg, const char *title) { fprintf( svg->mPtr, "\n" " %s\n" " \n" " \n" "\n", title, svg->mWidth, svg->mHeight ); } static void svgFooter(SVG *svg) { fprintf( svg->mPtr, "\n" " \n" " \n" "\n" ); } // ---------------------------------------------------------------------- // SVG Functions // ---------------------------------------------------------------------- void svgRect( SVG *svg, int x, int y, int width, int height, const char *stroke, const char *fill ) { fprintf( svg->mPtr, " \n", x, y, width, height, stroke, fill ); } void svgCircle( SVG *svg, int x, int y, int radius, const char *stroke, const char *fill ) { fprintf( svg->mPtr, " \n", x, y, radius, stroke, fill ); } void svgLine( SVG *svg, int x1, int y1, int x2, int y2, const char *color ) { fprintf( svg->mPtr, " \n", x1, y1, x2, y2, color ); } void svgLineEx( SVG *svg, int x1, int y1, int x2, int y2, int strokeWidth, const char *color ) { fprintf( svg->mPtr, " \n", x1, y1, x2, y2, strokeWidth, color ); } void svgText( SVG *svg, int x, int y, const char *txt, int size ) { fprintf( svg->mPtr, " %s\n", x, y, size, txt ); } void svgBorder(SVG *svg, const char *stroke) { svgRect( svg, 0, 0, svg->mWidth - 1, svg->mHeight - 1, stroke, svg->mColors.none ); } SVG *svgOpen( const char *fileName, const char *title, int width, int height ) { FILE *f = utilOpenFile(fileName, "w"); if(!f) return(NULL); SVG *svg = malloc(sizeof(SVG)); if(!svg) { fclose(f); return(NULL); } svg->mColors = getColors(); svg->mPtr = f; svg->mWidth = width; svg->mHeight = height; svgHeader(svg, title); return(svg); } void svgClose(SVG *svg) { svgFooter(svg); fclose(svg->mPtr); svg->mPtr = NULL; free(svg); }