├── README.md ├── main.c ├── svg.c └── svg.h /README.md: -------------------------------------------------------------------------------- 1 | For full details of this project go to 2 | http://www.codedrome.com/svg-library-in-c/ 3 | -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include"svg.h" 8 | 9 | //-------------------------------------------------------- 10 | // FUNCTION PROTOTYPES 11 | //-------------------------------------------------------- 12 | void drawrectangles(void); 13 | void drawallshapes(void); 14 | void iwanttobelieve(void); 15 | void mondrian(void); 16 | void stochasticpi(void); 17 | 18 | //-------------------------------------------------------- 19 | // FUNCTION main 20 | //-------------------------------------------------------- 21 | int main(void) 22 | { 23 | puts("-----------------"); 24 | puts("| codedrome.com |"); 25 | puts("| SVG Library |"); 26 | puts("-----------------\n"); 27 | 28 | //drawrectangles(); 29 | 30 | //drawallshapes(); 31 | 32 | //iwanttobelieve(); 33 | 34 | //mondrian(); 35 | 36 | return EXIT_SUCCESS; 37 | } 38 | 39 | // -------------------------------------------------------- 40 | // FUNCTION drawrectangles 41 | // -------------------------------------------------------- 42 | void drawrectangles(void) 43 | { 44 | svg* psvg; 45 | psvg = svg_create(512, 512); 46 | 47 | if(psvg == NULL) 48 | { 49 | puts("psvg is NULL"); 50 | } 51 | else 52 | { 53 | svg_rectangle(psvg, 512, 512, 0, 0, "#C0C0FF", "black", 1, 0, 0); 54 | 55 | svg_rectangle(psvg, 384, 384, 64, 64, "#00FF00", "#000000", 4, 0, 0); 56 | svg_rectangle(psvg, 320, 320, 96, 96, "#FFFF00", "#000000", 2, 8, 8); 57 | svg_rectangle(psvg, 256, 256, 128, 128, "#00FFFF", "#000000", 2, 8, 8); 58 | svg_rectangle(psvg, 192, 192, 160, 160, "#FF80FF", "#000000", 2, 8, 8); 59 | 60 | svg_finalize(psvg); 61 | svg_save(psvg, "rectangles.svg"); 62 | svg_free(psvg); 63 | } 64 | } 65 | 66 | // -------------------------------------------------------- 67 | // FUNCTION drawallshapes 68 | // -------------------------------------------------------- 69 | void drawallshapes(void) 70 | { 71 | svg* psvg; 72 | psvg = svg_create(192, 320); 73 | 74 | if(psvg == NULL) 75 | { 76 | puts("psvg is NULL"); 77 | } 78 | else 79 | { 80 | svg_fill(psvg, "#DADAFF"); 81 | 82 | svg_text(psvg, 32, 32, "sans-serif", 16, "#000000", "#000000", "drawallshapes"); 83 | svg_circle(psvg, "#000080", 4, "#0000FF", 32, 64, 96); 84 | svg_ellipse(psvg, 64, 160, 32, 16, "#FF0000", "#800000", 4); 85 | 86 | svg_line(psvg, "#000000", 2, 32, 192, 160, 192); 87 | 88 | svg_rectangle(psvg, 64, 64, 32, 224, "#00FF00", "#008000", 4, 4, 4); 89 | 90 | svg_finalize(psvg); 91 | svg_print(psvg); 92 | svg_save(psvg, "allshapes.svg"); 93 | svg_free(psvg); 94 | } 95 | } 96 | 97 | // -------------------------------------------------------- 98 | // FUNCTION iwanttobelieve 99 | // -------------------------------------------------------- 100 | void iwanttobelieve(void) 101 | { 102 | svg* psvg; 103 | psvg = svg_create(512, 768); 104 | 105 | if(psvg == NULL) 106 | { 107 | puts("psvg is NULL"); 108 | } 109 | else 110 | { 111 | svg_fill(psvg, "#000010"); 112 | 113 | srand(time(NULL)); 114 | int x, y; 115 | for(int s = 0; s <= 512; s++) 116 | { 117 | x = (rand() % 512); 118 | y = (rand() % 768); 119 | 120 | svg_rectangle(psvg, 1, 1, x, y, "white", "white", 0, 0, 0); 121 | } 122 | 123 | svg_text(psvg, 96, 712, "sans-serif", 32, "#FFFFFF", "#FFFFFF", "I WANT TO BELIEVE"); 124 | 125 | svg_circle(psvg, "silver", 1, "rgba(0,0,0,0)", 28, 256, 384); 126 | 127 | svg_ellipse(psvg, 256, 374, 8, 14, "#808080", "#808080", 0); 128 | svg_ellipse(psvg, 252, 372, 3, 2, "#000000", "#000000", 0); 129 | svg_ellipse(psvg, 260, 372, 3, 2, "#000000", "#000000", 0); 130 | svg_rectangle(psvg, 1, 1, 251, 371, "white", "white", 0, 0, 0); 131 | svg_rectangle(psvg, 1, 1, 259, 371, "white", "white", 0, 0, 0); 132 | svg_line(psvg, "black", 2, 254, 378, 258, 378); 133 | 134 | svg_line(psvg, "silver", 2, 234, 416, 226, 432); 135 | svg_line(psvg, "silver", 2, 278, 416, 286, 432); 136 | svg_ellipse(psvg, 256, 400, 64, 16, "silver", "silver", 4); 137 | 138 | svg_finalize(psvg); 139 | svg_save(psvg, "iwanttobelieve.svg"); 140 | svg_free(psvg); 141 | } 142 | } 143 | 144 | // -------------------------------------------------------- 145 | // FUNCTION mondrian 146 | // -------------------------------------------------------- 147 | void mondrian(void) 148 | { 149 | svg* psvg; 150 | psvg = svg_create(512, 512); 151 | 152 | if(psvg == NULL) 153 | { 154 | puts("psvg is NULL"); 155 | } 156 | else 157 | { 158 | svg_fill(psvg, "white"); 159 | 160 | svg_rectangle(psvg, 512, 512, 0, 0, "white", "black", 1, 0, 0); 161 | 162 | svg_rectangle(psvg, 256, 256, 64, 64, "red", "red", 0, 0, 0); 163 | svg_rectangle(psvg, 128, 128, 64, 320, "black", "black", 0, 0, 0); 164 | svg_rectangle(psvg, 64, 128, 0, 384, "orange", "orange", 0, 0, 0); 165 | svg_rectangle(psvg, 128, 192, 320, 0, "orange", "orange", 0, 0, 0); 166 | svg_rectangle(psvg, 128, 64, 320, 384, "navy", "navy", 0, 0, 0); 167 | svg_rectangle(psvg, 64, 128, 448, 384, "red", "red", 0, 0, 0); 168 | 169 | svg_line(psvg, "black", 8, 0, 64, 448, 64); 170 | svg_line(psvg, "black", 8, 64, 64, 64, 512); 171 | svg_line(psvg, "black", 8, 0, 192, 64, 192); 172 | svg_line(psvg, "black", 8, 0, 384, 512, 384); 173 | svg_line(psvg, "black", 8, 128, 0, 128, 64); 174 | svg_line(psvg, "black", 8, 320, 0, 320, 448); 175 | svg_line(psvg, "black", 8, 64, 320, 448, 320); 176 | svg_line(psvg, "black", 8, 320, 192, 448, 192); 177 | svg_line(psvg, "black", 8, 64, 448, 448, 448); 178 | svg_line(psvg, "black", 8, 448, 0, 448, 512); 179 | svg_line(psvg, "black", 8, 192, 320, 192, 512); 180 | svg_line(psvg, "black", 8, 384, 192, 384, 320); 181 | 182 | svg_finalize(psvg); 183 | svg_save(psvg, "mondrian.svg"); 184 | svg_free(psvg); 185 | } 186 | } 187 | -------------------------------------------------------------------------------- /svg.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include"svg.h" 9 | 10 | // -------------------------------------------------------- 11 | // STATIC FUNCTION appendstringtosvg 12 | // -------------------------------------------------------- 13 | static void appendstringtosvg(svg* psvg, char* text) 14 | { 15 | int l = strlen(psvg->svg) + strlen(text) + 1; 16 | 17 | char* p = realloc(psvg->svg, l); 18 | 19 | if(p) 20 | { 21 | psvg->svg = p; 22 | } 23 | 24 | strcat(psvg->svg, text); 25 | } 26 | 27 | // -------------------------------------------------------- 28 | // STATIC FUNCTION appendnumbertosvg 29 | // -------------------------------------------------------- 30 | static void appendnumbertosvg(svg* psvg, int n) 31 | { 32 | char sn[16]; 33 | 34 | sprintf(sn, "%d", n); 35 | 36 | appendstringtosvg(psvg, sn); 37 | } 38 | 39 | // -------------------------------------------------------- 40 | // FUNCTION svg_create 41 | // -------------------------------------------------------- 42 | svg* svg_create(int width, int height) 43 | { 44 | svg* psvg = malloc(sizeof(svg)); 45 | 46 | if(psvg != NULL) 47 | { 48 | *psvg = (svg){.svg = NULL, .width = width, .height = height, .finalized = false}; 49 | 50 | psvg->svg = malloc(1); 51 | 52 | sprintf(psvg->svg, "%s", "\0"); 53 | 54 | appendstringtosvg(psvg, "\n"); 59 | 60 | return psvg; 61 | } 62 | else 63 | { 64 | return NULL; 65 | } 66 | } 67 | 68 | // -------------------------------------------------------- 69 | // FUNCTION svg_finalize 70 | // -------------------------------------------------------- 71 | void svg_finalize(svg* psvg) 72 | { 73 | appendstringtosvg(psvg, ""); 74 | 75 | psvg->finalized = true; 76 | } 77 | 78 | // -------------------------------------------------------- 79 | // FUNCTION svg_print 80 | // -------------------------------------------------------- 81 | void svg_print(svg* psvg) 82 | { 83 | printf("%s\n", psvg->svg); 84 | } 85 | 86 | // -------------------------------------------------------- 87 | // FUNCTION svg_save 88 | // -------------------------------------------------------- 89 | void svg_save(svg* psvg, char* filepath) 90 | { 91 | if(!psvg->finalized) 92 | { 93 | svg_finalize(psvg); 94 | } 95 | 96 | FILE* fp; 97 | 98 | fp = fopen(filepath, "w"); 99 | 100 | if(fp != NULL) 101 | { 102 | fwrite(psvg->svg, 1, strlen(psvg->svg), fp); 103 | 104 | fclose(fp); 105 | } 106 | } 107 | 108 | //---------------------------------------------------------------- 109 | // FUNCTION svg_free 110 | //---------------------------------------------------------------- 111 | void svg_free(svg* psvg) 112 | { 113 | free(psvg->svg); 114 | 115 | free(psvg); 116 | } 117 | 118 | //---------------------------------------------------------------- 119 | // FUNCTION svg_circle 120 | //---------------------------------------------------------------- 121 | void svg_circle(svg* psvg, char* stroke, int strokewidth, char* fill, int r, int cx, int cy) 122 | { 123 | appendstringtosvg(psvg, " \n"); 136 | } 137 | 138 | //---------------------------------------------------------------- 139 | // FUNCTION svg_line 140 | //---------------------------------------------------------------- 141 | void svg_line(svg* psvg, char* stroke, int strokewidth, int x1, int y1, int x2, int y2) 142 | { 143 | appendstringtosvg(psvg, " \n"); 156 | } 157 | 158 | //---------------------------------------------------------------- 159 | // FUNCTION svg_rectangle 160 | //---------------------------------------------------------------- 161 | void svg_rectangle(svg* psvg, int width, int height, int x, int y, char* fill, char* stroke, int strokewidth, int radiusx, int radiusy) 162 | { 163 | appendstringtosvg(psvg, " \n"); 182 | } 183 | 184 | // -------------------------------------------------------- 185 | // FUNCTION svg_fill 186 | // -------------------------------------------------------- 187 | void svg_fill(svg* psvg, char* Fill) 188 | { 189 | svg_rectangle(psvg, psvg->width, psvg->height, 0, 0, Fill, Fill, 0, 0, 0); 190 | } 191 | 192 | //---------------------------------------------------------------- 193 | // FUNCTION svg_text 194 | //---------------------------------------------------------------- 195 | void svg_text(svg* psvg, int x, int y, char* fontfamily, int fontsize, char* fill, char* stroke, char* text) 196 | { 197 | appendstringtosvg(psvg, " "); 210 | appendstringtosvg(psvg, text); 211 | appendstringtosvg(psvg, "\n"); 212 | } 213 | 214 | //---------------------------------------------------------------- 215 | // FUNCTION svg_ellipse 216 | //---------------------------------------------------------------- 217 | void svg_ellipse(svg* psvg, int cx, int cy, int rx, int ry, char* fill, char* stroke, int strokewidth) 218 | { 219 | appendstringtosvg(psvg, " \n"); 234 | } 235 | -------------------------------------------------------------------------------- /svg.h: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | // -------------------------------------------------------- 8 | // STRUCT svg 9 | // -------------------------------------------------------- 10 | typedef struct svg 11 | { 12 | char* svg; 13 | int height; 14 | int width; 15 | bool finalized; 16 | } svg; 17 | 18 | // -------------------------------------------------------- 19 | // FUNCTION PROTOTYPES 20 | // -------------------------------------------------------- 21 | svg* svg_create(int width, int height); 22 | void svg_finalize(svg* psvg); 23 | void svg_print(svg* psvg); 24 | void svg_save(svg* psvg, char* filepath); 25 | void svg_free(svg* psvg); 26 | 27 | void svg_circle(svg* psvg, char* stroke, int strokewidth, char* fill, int r, int cx, int cy); 28 | void svg_line(svg* psvg, char* stroke, int strokewidth, int x1, int y1, int x2, int y2); 29 | void svg_rectangle(svg* psvg, int width, int height, int x, int y, char* fill, char* stroke, int strokewidth, int radiusx, int radiusy); 30 | void svg_fill(svg* psvg, char* fill); 31 | void svg_text(svg* psvg, int x, int y, char* fontfamily, int fontsize, char* fill, char* stroke, char* text); 32 | void svg_ellipse(svg* psvg, int cx, int cy, int rx, int ry, char* fill, char* stroke, int strokewidth); 33 | --------------------------------------------------------------------------------