clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name chart.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/var/lib/jenkins/workspace/scan-build-netsurf -fcoverage-compilation-dir=/var/lib/jenkins/workspace/scan-build-netsurf -resource-dir /usr/lib/llvm-19/lib/clang/19 -isystem /usr/include/mit-krb5 -I . -I include -I build/Linux-gtk2 -I frontends -I content/handlers -D WITH_JPEG -U WITH_PDF_EXPORT -D LIBICONV_PLUG -I /var/lib/jenkins/artifacts-x86_64-linux-gnu/include -I /var/lib/jenkins/artifacts-x86_64-linux-gnu/include -I /var/lib/jenkins/artifacts-x86_64-linux-gnu/include -I /usr/include/x86_64-linux-gnu -I /usr/include/p11-kit-1 -D WITH_CURL -D WITH_OPENSSL -I /var/lib/jenkins/artifacts-x86_64-linux-gnu/include -D UTF8PROC_EXPORTS -D WITH_UTF8PROC -I /usr/include/webp -D WITH_WEBP -I /usr/include/libpng16 -D WITH_PNG -I /var/lib/jenkins/artifacts-x86_64-linux-gnu/include/ -D WITH_BMP -I /var/lib/jenkins/artifacts-x86_64-linux-gnu/include -D WITH_GIF -I /var/lib/jenkins/artifacts-x86_64-linux-gnu/include -D WITH_NS_SVG -I /var/lib/jenkins/artifacts-x86_64-linux-gnu/include -D WITH_NSSPRITE -I /var/lib/jenkins/artifacts-x86_64-linux-gnu/include -D WITH_NSPSL -I /var/lib/jenkins/artifacts-x86_64-linux-gnu/include -D WITH_NSLOG -D NETSURF_UA_FORMAT_STRING="Mozilla/5.0 (%s) NetSurf/%d.%d" -D NETSURF_HOMEPAGE="about:welcome" -D NETSURF_LOG_LEVEL=VERBOSE -D NETSURF_BUILTIN_LOG_FILTER="(level:WARNING || cat:jserrors)" -D NETSURF_BUILTIN_VERBOSE_FILTER="(level:VERBOSE || cat:jserrors)" -D STMTEXPR=1 -I /usr/include/librsvg-2.0 -I /usr/include/glib-2.0 -I /usr/lib/x86_64-linux-gnu/glib-2.0/include -I /usr/include/sysprof-6 -I /usr/include/libmount -I /usr/include/blkid -I /usr/include/gdk-pixbuf-2.0 -I /usr/include/libpng16 -I /usr/include/x86_64-linux-gnu -I /usr/include/webp -I /usr/include/cairo -I /usr/include/freetype2 -I /usr/include/pixman-1 -I /usr/include/harfbuzz -I /usr/include/libxml2 -I /usr/include/pango-1.0 -I /usr/include/fribidi -D WITH_RSVG -I /usr/include/gtk-2.0 -I /usr/lib/x86_64-linux-gnu/gtk-2.0/include -I /usr/include/pango-1.0 -I /usr/include/glib-2.0 -I /usr/lib/x86_64-linux-gnu/glib-2.0/include -I /usr/include/sysprof-6 -I /usr/include/harfbuzz -I /usr/include/freetype2 -I /usr/include/libpng16 -I /usr/include/libmount -I /usr/include/blkid -I /usr/include/fribidi -I /usr/include/cairo -I /usr/include/pixman-1 -I /usr/include/gdk-pixbuf-2.0 -I /usr/include/x86_64-linux-gnu -I /usr/include/webp -I /usr/include/atk-1.0 -I /usr/include/glib-2.0 -I /usr/lib/x86_64-linux-gnu/glib-2.0/include -I /usr/include/sysprof-6 -I /usr/include/glib-2.0 -I /usr/lib/x86_64-linux-gnu/glib-2.0/include -I /usr/include/sysprof-6 -D gtk -D nsgtk -D G_DISABLE_SINGLE_INCLUDES -D G_DISABLE_DEPRECATED -D GTK_DISABLE_SINGLE_INCLUDES -D GTK_MULTIHEAD_SAFE -D PANGO_DISABLE_DEPRECATED -D GTK_DISABLE_DEPRECATED -D _XOPEN_SOURCE=700 -D _POSIX_C_SOURCE=200809L -D _BSD_SOURCE -D _DEFAULT_SOURCE -D _NETBSD_SOURCE -D GTK_RESPATH="/var/lib/jenkins/artifacts-x86_64-linux-gnu/share/netsurf/:./frontends/gtk/res/" -D WITH_GRESOURCE -D DUK_OPT_HAVE_CUSTOM_H -internal-isystem /usr/lib/llvm-19/lib/clang/19/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wwrite-strings -Wno-unused-parameter -Wno-unused-but-set-variable -std=c99 -fconst-strings -ferror-limit 19 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -vectorize-loops -vectorize-slp -analyzer-display-progress -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /var/lib/jenkins/workspace/scan-build-netsurf/clangScanBuildReports/2025-11-26-172431-2397091-1 -x c content/fetchers/about/chart.c
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | #include <stdbool.h> |
| 30 | #include <stddef.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | #include <math.h> |
| 34 | #include <stdio.h> |
| 35 | |
| 36 | #include "utils/config.h" |
| 37 | #include "netsurf/inttypes.h" |
| 38 | #include "utils/config.h" |
| 39 | #include "utils/utils.h" |
| 40 | #include "utils/errors.h" |
| 41 | #include "utils/nsurl.h" |
| 42 | |
| 43 | #include "private.h" |
| 44 | #include "chart.h" |
| 45 | |
| 46 | |
| 47 | #define FIGURE_MIN_WIDTH 150 |
| 48 | #define FIGURE_MIN_HEIGHT 100 |
| 49 | |
| 50 | enum chart_type { |
| 51 | CHART_TYPE_UNKNOWN, |
| 52 | CHART_TYPE_PIE, |
| 53 | }; |
| 54 | |
| 55 | |
| 56 | enum key_type { |
| 57 | CHART_KEY_UNSET, |
| 58 | CHART_KEY_NONE, |
| 59 | CHART_KEY_LEFT, |
| 60 | CHART_KEY_RIGHT, |
| 61 | CHART_KEY_TOP, |
| 62 | CHART_KEY_BOT, |
| 63 | CHART_KEY_END |
| 64 | }; |
| 65 | |
| 66 | |
| 67 | struct chart_label { |
| 68 | char *title; |
| 69 | unsigned int colour; |
| 70 | }; |
| 71 | |
| 72 | struct chart_series { |
| 73 | unsigned int len; |
| 74 | float *value; |
| 75 | }; |
| 76 | |
| 77 | #define MAX_SERIES 4 |
| 78 | |
| 79 | struct chart_data { |
| 80 | unsigned int series_len; |
| 81 | struct chart_series series[MAX_SERIES]; |
| 82 | |
| 83 | unsigned int label_len; |
| 84 | struct chart_label *label; |
| 85 | |
| 86 | }; |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | struct chart_param { |
| 92 | enum chart_type type; |
| 93 | enum key_type key; |
| 94 | unsigned int width; |
| 95 | unsigned int height; |
| 96 | char *title; |
| 97 | struct { |
| 98 | unsigned int x; |
| 99 | unsigned int y; |
| 100 | unsigned int width; |
| 101 | unsigned int height; |
| 102 | } area; |
| 103 | struct chart_data data; |
| 104 | }; |
| 105 | |
| 106 | #define DEF_COLOUR_NUM 8 |
| 107 | |
| 108 | static unsigned int colour_series[DEF_COLOUR_NUM] = |
| 109 | { |
| 110 | 0x00ff00, |
| 111 | 0x0000ff, |
| 112 | 0xff0000, |
| 113 | 0xffff00, |
| 114 | 0x00ffff, |
| 115 | 0xff00ff, |
| 116 | 0x777777, |
| 117 | 0x000000, |
| 118 | }; |
| 119 | |
| 120 | |
| 121 | |
| 122 | static nserror ensure_label_count(struct chart_param *chart, unsigned int count) |
| 123 | { |
| 124 | unsigned int lidx; |
| 125 | int deltac; |
| 126 | struct chart_label *nlabels; |
| 127 | |
| 128 | deltac = count - chart->data.label_len; |
| 129 | if (deltac <= 0) { |
| 130 | |
| 131 | return NSERROR_OK; |
| 132 | } |
| 133 | |
| 134 | nlabels = realloc(chart->data.label, |
| 135 | count * sizeof(struct chart_label)); |
| 136 | if (nlabels == NULL) { |
| 137 | return NSERROR_NOMEM; |
| 138 | } |
| 139 | chart->data.label = nlabels; |
| 140 | |
| 141 | for (lidx = chart->data.label_len; lidx < count; lidx++) { |
| 142 | chart->data.label[lidx].title = calloc(1, 20); |
| 143 | snprintf(chart->data.label[lidx].title, 19, "item %d", lidx + 1); |
| 144 | chart->data.label[lidx].colour = colour_series[lidx % DEF_COLOUR_NUM]; |
| 145 | } |
| 146 | |
| 147 | chart->data.label_len = count; |
| 148 | |
| 149 | return NSERROR_OK; |
| 150 | } |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | static nserror |
| 156 | extract_series_values(struct chart_param *chart, |
| 157 | unsigned int series_num, |
| 158 | const char *valstr, |
| 159 | size_t valstrlen) |
| 160 | { |
| 161 | nserror res; |
| 162 | unsigned int valcur; |
| 163 | size_t valstart; |
| 164 | size_t vallen; |
| 165 | struct chart_series *series; |
| 166 | |
| 167 | series = chart->data.series + series_num; |
| 168 | |
| 169 | |
| 170 | if (series->value != NULL) { |
| 171 | free(series->value); |
| 172 | } |
| 173 | |
| 174 | |
| 175 | for (series->len = 1, valstart=0; valstart < valstrlen; valstart++) { |
| 176 | if (valstr[valstart] == ',') { |
| 177 | series->len++; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | |
| 182 | series->value = calloc(series->len, sizeof(float)); |
| 183 | if (series->value == NULL) { |
| 184 | return NSERROR_NOMEM; |
| 185 | } |
| 186 | |
| 187 | |
| 188 | for (valcur = 0, vallen = 0, valstart = 0; |
| 189 | (valstart < valstrlen) && (valcur < series->len); |
| 190 | valstart += vallen, valcur++) { |
| 191 | |
| 192 | vallen = 0; |
| 193 | while (((valstart + vallen) < valstrlen) && |
| 194 | (valstr[valstart + vallen] != ',')) { |
| 195 | vallen++; |
| 196 | } |
| 197 | |
| 198 | series->value[valcur] = strtof(valstr + valstart, NULL); |
| 199 | vallen++; |
| 200 | } |
| 201 | |
| 202 | res = ensure_label_count(chart, series->len); |
| 203 | |
| 204 | return res; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | static nserror |
| 212 | extract_next_series_values(struct chart_param *chart, |
| 213 | const char *valstr, |
| 214 | size_t valstrlen) |
| 215 | { |
| 216 | nserror res; |
| 217 | |
| 218 | if (chart->data.series_len >= MAX_SERIES) { |
| 219 | return NSERROR_NOSPACE; |
| 220 | } |
| 221 | |
| 222 | res = extract_series_values(chart, |
| 223 | chart->data.series_len, |
| 224 | valstr, |
| 225 | valstrlen); |
| 226 | if (res == NSERROR_OK) { |
| 227 | chart->data.series_len++; |
| 228 | } |
| 229 | |
| 230 | return res; |
| 231 | } |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | static nserror |
| 238 | extract_series_labels(struct chart_param *chart, |
| 1 | [debug] analyzing from extract_series_labels | |
|
| 239 | const char *valstr, |
| 240 | size_t valstrlen) |
| 241 | { |
| 242 | nserror res; |
| 243 | unsigned int valcount; |
| 244 | unsigned int valcur; |
| 245 | size_t valstart; |
| 246 | size_t vallen; |
| 247 | |
| 248 | for (valcount = 1, valstart=0; valstart < valstrlen; valstart++) { |
| 249 | if (valstr[valstart] == ',') { |
| 250 | valcount++; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | res = ensure_label_count(chart, valcount); |
| 255 | if (res != NSERROR_OK) { |
| 256 | return res; |
| 257 | } |
| 258 | |
| 259 | |
| 260 | for (valcur = 0, vallen = 0, valstart = 0; |
| 2 | | Although the value stored to 'vallen' is used in the enclosing expression, the value is never actually read from 'vallen' |
|
| 261 | (valstart < valstrlen) && (valcur < chart->data.label_len); |
| 262 | valstart += vallen, valcur++) { |
| 263 | |
| 264 | vallen = 0; |
| 265 | while (((valstart + vallen) < valstrlen) && |
| 266 | (valstr[valstart + vallen] != ',')) { |
| 267 | vallen++; |
| 268 | } |
| 269 | |
| 270 | chart->data.label[valcur].title = strndup(valstr + valstart, vallen); |
| 271 | vallen++; |
| 272 | } |
| 273 | return NSERROR_OK; |
| 274 | } |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | static nserror |
| 281 | extract_series_colours(struct chart_param *chart, |
| 282 | const char *valstr, |
| 283 | size_t valstrlen) |
| 284 | { |
| 285 | return NSERROR_OK; |
| 286 | } |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | static nserror |
| 292 | process_query_section(const char *str, size_t len, struct chart_param *chart) |
| 293 | { |
| 294 | nserror res = NSERROR_OK; |
| 295 | |
| 296 | if ((len > 6) && |
| 297 | (strncmp(str, "width=", 6) == 0)) { |
| 298 | |
| 299 | chart->width = strtoul(str + 6, NULL, 10); |
| 300 | } else if ((len > 7) && |
| 301 | (strncmp(str, "height=", 7) == 0)) { |
| 302 | |
| 303 | chart->height = strtoul(str + 7, NULL, 10); |
| 304 | } else if ((len > 8) && |
| 305 | (strncmp(str, "cawidth=", 8) == 0)) { |
| 306 | |
| 307 | chart->area.width = strtoul(str + 8, NULL, 10); |
| 308 | } else if ((len > 9) && |
| 309 | (strncmp(str, "caheight=", 9) == 0)) { |
| 310 | |
| 311 | chart->area.height = strtoul(str + 9, NULL, 10); |
| 312 | } else if ((len > 4) && |
| 313 | (strncmp(str, "key=", 4) == 0)) { |
| 314 | |
| 315 | chart->key = strtoul(str + 4, NULL, 10); |
| 316 | } else if ((len > 6) && |
| 317 | (strncmp(str, "title=", 6) == 0)) { |
| 318 | chart->title = strndup(str + 6, len - 6); |
| 319 | } else if ((len > 5) && |
| 320 | (strncmp(str, "type=", 5) == 0)) { |
| 321 | if (strncmp(str + 5, "pie", len - 5) == 0) { |
| 322 | chart->type = CHART_TYPE_PIE; |
| 323 | } else { |
| 324 | chart->type = CHART_TYPE_UNKNOWN; |
| 325 | } |
| 326 | } else if ((len > 7) && |
| 327 | (strncmp(str, "values=", 7) == 0)) { |
| 328 | res = extract_next_series_values(chart, str + 7, len - 7); |
| 329 | } else if ((len > 7) && |
| 330 | (strncmp(str, "labels=", 7) == 0)) { |
| 331 | res = extract_series_labels(chart, str + 7, len - 7); |
| 332 | } else if ((len > 8) && |
| 333 | (strncmp(str, "colours=", 8) == 0)) { |
| 334 | res = extract_series_colours(chart, str + 8, len - 8); |
| 335 | } |
| 336 | |
| 337 | return res; |
| 338 | } |
| 339 | |
| 340 | |
| 341 | |
| 342 | static nserror |
| 343 | chart_from_query(struct nsurl *url, struct chart_param *chart) |
| 344 | { |
| 345 | nserror res; |
| 346 | char *querystr; |
| 347 | size_t querylen; |
| 348 | size_t kvstart; |
| 349 | size_t kvlen; |
| 350 | |
| 351 | res = nsurl_get(url, NSURL_QUERY, &querystr, &querylen); |
| 352 | if (res != NSERROR_OK) { |
| 353 | return res; |
| 354 | } |
| 355 | |
| 356 | for (kvlen = 0, kvstart = 0; kvstart < querylen; kvstart += kvlen) { |
| 357 | |
| 358 | kvlen = 0; |
| 359 | while (((kvstart + kvlen) < querylen) && |
| 360 | (querystr[kvstart + kvlen] != '&')) { |
| 361 | kvlen++; |
| 362 | } |
| 363 | |
| 364 | res = process_query_section(querystr + kvstart, kvlen, chart); |
| 365 | if (res != NSERROR_OK) { |
| 366 | break; |
| 367 | } |
| 368 | kvlen++; |
| 369 | } |
| 370 | free(querystr); |
| 371 | |
| 372 | |
| 373 | if (chart->width < FIGURE_MIN_WIDTH) { |
| 374 | |
| 375 | if (chart->height < FIGURE_MIN_HEIGHT) { |
| 376 | |
| 377 | chart->width = FIGURE_MIN_WIDTH; |
| 378 | chart->height = FIGURE_MIN_HEIGHT; |
| 379 | } else { |
| 380 | |
| 381 | chart->width = (chart->height * 3) / 2; |
| 382 | } |
| 383 | } else { |
| 384 | |
| 385 | if (chart->height < FIGURE_MIN_HEIGHT) { |
| 386 | |
| 387 | chart->height = (chart->width * 2) / 3; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | |
| 392 | if ((chart->key == CHART_KEY_UNSET) || |
| 393 | (chart->key >= CHART_KEY_END )) { |
| 394 | |
| 395 | chart->key = CHART_KEY_RIGHT; |
| 396 | } |
| 397 | |
| 398 | return NSERROR_OK; |
| 399 | } |
| 400 | |
| 401 | |
| 402 | static nserror |
| 403 | output_pie_legend(struct fetch_about_context *ctx, struct chart_param *chart) |
| 404 | { |
| 405 | nserror res; |
| 406 | unsigned int lblidx; |
| 407 | unsigned int legend_width; |
| 408 | unsigned int legend_height; |
| 409 | unsigned int vertical_spacing; |
| 410 | |
| 411 | switch (chart->key) { |
| 412 | |
| 413 | case CHART_KEY_NONE: |
| 414 | break; |
| 415 | case CHART_KEY_RIGHT: |
| 416 | legend_width = chart->width - chart->area.width - chart->area.x; |
| 417 | legend_width -= 10; |
| 418 | legend_height = chart->height; |
| 419 | vertical_spacing = legend_height / (chart->data.label_len + 1); |
| 420 | |
| 421 | for(lblidx = 0; lblidx < chart->data.label_len ; lblidx++) { |
| 422 | res = fetch_about_ssenddataf(ctx, |
| 423 | "<rect x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\" fill=\"#%06x\" />", |
| 424 | chart->width - legend_width, |
| 425 | (vertical_spacing * lblidx) + (vertical_spacing/2), |
| 426 | vertical_spacing * 2 / 3, |
| 427 | vertical_spacing * 2 / 3, |
| 428 | chart->data.label[lblidx].colour); |
| 429 | if (res != NSERROR_OK) { |
| 430 | return res; |
| 431 | } |
| 432 | res = fetch_about_ssenddataf(ctx, |
| 433 | "<text x=\"%d\" y=\"%d\" fill=\"#%06x\" >%s</text>", |
| 434 | chart->width - legend_width + vertical_spacing, |
| 435 | vertical_spacing * (lblidx+1), |
| 436 | chart->data.label[lblidx].colour, |
| 437 | chart->data.label[lblidx].title); |
| 438 | if (res != NSERROR_OK) { |
| 439 | return res; |
| 440 | } |
| 441 | } |
| 442 | break; |
| 443 | default: |
| 444 | break; |
| 445 | } |
| 446 | |
| 447 | return NSERROR_OK; |
| 448 | } |
| 449 | |
| 450 | static float |
| 451 | compute_series_total(struct chart_param *chart, unsigned int series) |
| 452 | { |
| 453 | float total; |
| 454 | unsigned int curdata; |
| 455 | |
| 456 | for (total = 0, curdata = 0; |
| 457 | curdata < chart->data.series[series].len; |
| 458 | curdata++) { |
| 459 | total += chart->data.series[series].value[curdata]; |
| 460 | } |
| 461 | return total; |
| 462 | } |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | static bool |
| 468 | pie_chart(struct fetch_about_context *ctx, struct chart_param *chart) |
| 469 | { |
| 470 | nserror res; |
| 471 | float ra; |
| 472 | float rb; |
| 473 | float series_total; |
| 474 | unsigned int curdata; |
| 475 | float last_x, last_y; |
| 476 | float end_x, end_y; |
| 477 | float start; |
| 478 | float extent; |
| 479 | bool large; |
| 480 | float circle_centre_x, circle_centre_y; |
| 481 | |
| 482 | |
| 483 | if ((chart->data.series_len < 1) || (chart->data.series[0].len < 2)) { |
| 484 | return NSERROR_BAD_PARAMETER; |
| 485 | } |
| 486 | |
| 487 | |
| 488 | series_total = compute_series_total(chart, 0); |
| 489 | if (series_total == 0) { |
| 490 | |
| 491 | return NSERROR_BAD_PARAMETER; |
| 492 | } |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | if ((chart->area.width == 0) || (chart->area.height == 0)) { |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | if (chart->width > chart->height) { |
| 506 | chart->area.width = chart->area.height = (chart->height - chart->area.x); |
| 507 | } else { |
| 508 | chart->area.width = chart->area.height = (chart->width - chart->area.y); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | |
| 513 | fetch_about_set_http_code(ctx, 200); |
| 514 | |
| 515 | |
| 516 | if (fetch_about_send_header(ctx, |
| 517 | "Content-Type: image/svg; charset=utf-8")) { |
| 518 | goto aborted; |
| 519 | } |
| 520 | |
| 521 | |
| 522 | ra = chart->area.width / 2; |
| 523 | rb = chart->area.height / 2; |
| 524 | |
| 525 | |
| 526 | circle_centre_x = chart->area.x + ra; |
| 527 | circle_centre_y = chart->area.y + rb; |
| 528 | |
| 529 | |
| 530 | |
| 531 | res = fetch_about_ssenddataf(ctx, |
| 532 | "<svg width=\"%u\" height=\"%u\" " |
| 533 | "xmlns=\"http://www.w3.org/2000/svg\">\n", |
| 534 | chart->width, chart->height); |
| 535 | if (res != NSERROR_OK) { |
| 536 | goto aborted; |
| 537 | } |
| 538 | |
| 539 | |
| 540 | res = output_pie_legend(ctx, chart); |
| 541 | if (res != NSERROR_OK) { |
| 542 | goto aborted; |
| 543 | } |
| 544 | |
| 545 | |
| 546 | start = -M_PI_2; |
| 547 | last_x = (ra * cos(start)); |
| 548 | last_y = (rb * sin(start)); |
| 549 | |
| 550 | |
| 551 | for (curdata=0; curdata < chart->data.series[0].len; curdata++) { |
| 552 | extent = ((chart->data.series[0].value[curdata] / series_total) * 2 * M_PI); |
| 553 | end_x = (ra * cos(start + extent)); |
| 554 | end_y = (rb * sin(start + extent)); |
| 555 | |
| 556 | if (extent > M_PI) { |
| 557 | large = true; |
| 558 | } else { |
| 559 | large = false; |
| 560 | } |
| 561 | |
| 562 | res = fetch_about_ssenddataf( |
| 563 | ctx, |
| 564 | "<path d=\"M %g %g\n" |
| 565 | "A %g %g 0 %d 1 %g %g\n" |
| 566 | "L %g %g Z\" fill=\"#%06x\" />\n", |
| 567 | circle_centre_x + last_x, |
| 568 | circle_centre_y + last_y, |
| 569 | ra, rb, large?1:0, |
| 570 | circle_centre_x + end_x, |
| 571 | circle_centre_y + end_y, |
| 572 | circle_centre_x, |
| 573 | circle_centre_y, |
| 574 | chart->data.label[curdata].colour); |
| 575 | if (res != NSERROR_OK) { |
| 576 | goto aborted; |
| 577 | } |
| 578 | last_x = end_x; |
| 579 | last_y = end_y; |
| 580 | start +=extent; |
| 581 | } |
| 582 | |
| 583 | res = fetch_about_ssenddataf(ctx, "</svg>\n"); |
| 584 | if (res != NSERROR_OK) { |
| 585 | goto aborted; |
| 586 | } |
| 587 | |
| 588 | fetch_about_send_finished(ctx); |
| 589 | |
| 590 | return true; |
| 591 | |
| 592 | aborted: |
| 593 | |
| 594 | return false; |
| 595 | |
| 596 | } |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | bool fetch_about_chart_handler(struct fetch_about_context *ctx) |
| 607 | { |
| 608 | nserror res; |
| 609 | struct chart_param chart; |
| 610 | memset(&chart, 0, sizeof(struct chart_param)); |
| 611 | |
| 612 | res = chart_from_query(fetch_about_get_url(ctx), &chart); |
| 613 | if (res != NSERROR_OK) { |
| 614 | goto aborted; |
| 615 | } |
| 616 | |
| 617 | switch (chart.type) { |
| 618 | case CHART_TYPE_PIE: |
| 619 | return pie_chart(ctx, &chart); |
| 620 | |
| 621 | |
| 622 | default: |
| 623 | break; |
| 624 | } |
| 625 | |
| 626 | aborted: |
| 627 | |
| 628 | return false; |
| 629 | |
| 630 | } |