Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * This file is part of Libsvgtiny
  3.  * Licensed under the MIT License,
  4.  *                http://opensource.org/licenses/mit-license.php
  5.  * Copyright 2008 James Bursa <james@semichrome.net>
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <unistd.h>
  12. #include <stdbool.h>
  13. #include <stdlib.h>
  14.  
  15. #include <svgtiny.h>
  16.  
  17. #include "libnsfb.h"
  18. #include "libnsfb_plot.h"
  19. #include "libnsfb_event.h"
  20.  
  21.     nsfb_t *nsfb;
  22.     nsfb_bbox_t screen_box;
  23.     uint8_t *fbptr;
  24.     int fbstride;
  25.     nsfb_event_t event;
  26.  
  27. static int setup_fb(void)
  28. {
  29.     nsfb = nsfb_init(NSFB_FRONTEND_SDL);
  30.     if (nsfb == NULL) {
  31.         fprintf(stderr, "Unable to initialise nsfb with SDL frontend\n");
  32.         return 1;
  33.     }
  34.  
  35.     if (nsfb_init_frontend(nsfb) == -1) {
  36.         fprintf(stderr, "Unable to initialise nsfb frontend\n");
  37.         return 2;
  38.     }
  39.  
  40.     /* get the geometry of the whole screen */
  41.     screen_box.x0 = screen_box.y0 = 0;
  42.     nsfb_get_geometry(nsfb, &screen_box.x1, &screen_box.y1, NULL);
  43.  
  44.     nsfb_get_framebuffer(nsfb, &fbptr, &fbstride);
  45.  
  46.     /* claim the whole screen for update */
  47.     nsfb_claim(nsfb, &screen_box);
  48.  
  49.     nsfb_plot_clg(nsfb, 0xffffffff);
  50.  
  51.     return 0;
  52. }
  53.  
  54. int main(int argc, char *argv[])
  55. {
  56.     FILE *fd;
  57.     float scale = 1.0;
  58.     struct stat sb;
  59.     char *buffer;
  60.     size_t size;
  61.     size_t n;
  62.     struct svgtiny_diagram *diagram;
  63.     svgtiny_code code;
  64.  
  65.     if (argc != 2 && argc != 3) {
  66.         fprintf(stderr, "Usage: %s FILE [SCALE]\n", argv[0]);
  67.         return 1;
  68.     }
  69.  
  70.     /* load file into memory buffer */
  71.     fd = fopen(argv[1], "rb");
  72.     if (!fd) {
  73.         perror(argv[1]);
  74.         return 1;
  75.     }
  76.  
  77.     if (stat(argv[1], &sb)) {
  78.         perror(argv[1]);
  79.         return 1;
  80.     }
  81.     size = sb.st_size;
  82.  
  83.     buffer = malloc(size);
  84.     if (!buffer) {
  85.         fprintf(stderr, "Unable to allocate %lld bytes\n",
  86.                 (long long) size);
  87.         return 1;
  88.     }
  89.  
  90.     n = fread(buffer, 1, size, fd);
  91.     if (n != size) {
  92.         perror(argv[1]);
  93.         return 1;
  94.     }
  95.  
  96.     fclose(fd);
  97.  
  98.     /* read scale argument */
  99.     if (argc == 3) {
  100.         scale = atof(argv[2]);
  101.         if (scale == 0)
  102.             scale = 1.0;
  103.     }
  104.  
  105.     /* create svgtiny object */
  106.     diagram = svgtiny_create();
  107.     if (!diagram) {
  108.         fprintf(stderr, "svgtiny_create failed\n");
  109.         return 1;
  110.     }
  111.  
  112.     /* parse */
  113.     code = svgtiny_parse(diagram, buffer, size, argv[1], 1000, 1000);
  114.     if (code != svgtiny_OK) {
  115.         fprintf(stderr, "svgtiny_parse failed: ");
  116.         switch (code) {
  117.         case svgtiny_OUT_OF_MEMORY:
  118.             fprintf(stderr, "svgtiny_OUT_OF_MEMORY");
  119.             break;
  120.         case svgtiny_LIBXML_ERROR:
  121.             fprintf(stderr, "svgtiny_LIBXML_ERROR");
  122.             break;
  123.         case svgtiny_NOT_SVG:
  124.             fprintf(stderr, "svgtiny_NOT_SVG");
  125.             break;
  126.         case svgtiny_SVG_ERROR:
  127.             fprintf(stderr, "svgtiny_SVG_ERROR: line %i: %s",
  128.                     diagram->error_line,
  129.                     diagram->error_message);
  130.             break;
  131.         default:
  132.             fprintf(stderr, "unknown svgtiny_code %i", code);
  133.             break;
  134.         }
  135.         fprintf(stderr, "\n");
  136.     }
  137.  
  138.     free(buffer);
  139.  
  140.     if (setup_fb() != 0)
  141.         return 1;
  142.  
  143.     for (unsigned int i = 0; i != diagram->shape_count; i++) {
  144.         nsfb_plot_pen_t pen;
  145.         pen.stroke_colour = svgtiny_RED(diagram->shape[i].stroke) |
  146.                             svgtiny_GREEN(diagram->shape[i].stroke) << 8|
  147.                             svgtiny_BLUE(diagram->shape[i].stroke) << 16;
  148.         pen.fill_colour = svgtiny_RED(diagram->shape[i].fill) |
  149.                             svgtiny_GREEN(diagram->shape[i].fill) << 8|
  150.                             svgtiny_BLUE(diagram->shape[i].fill) << 16;
  151.  
  152.         if (diagram->shape[i].fill == svgtiny_TRANSPARENT)
  153.             pen.fill_type = NFSB_PLOT_OPTYPE_NONE;
  154.         else
  155.             pen.fill_type = NFSB_PLOT_OPTYPE_SOLID;
  156.  
  157.         if (diagram->shape[i].stroke == svgtiny_TRANSPARENT)
  158.             pen.stroke_type = NFSB_PLOT_OPTYPE_NONE;
  159.         else
  160.             pen.stroke_type = NFSB_PLOT_OPTYPE_SOLID;
  161.  
  162.         pen.stroke_width = scale * diagram->shape[i].stroke_width;
  163.  
  164.         if (diagram->shape[i].path) {
  165.             nsfb_plot_pathop_t *fb_path;
  166.             int fb_path_c;
  167.             unsigned int j;
  168.             fb_path = malloc(diagram->shape[i].path_length * 3 * sizeof(nsfb_plot_pathop_t));
  169.             fb_path_c = 0;
  170.  
  171.             for (j = 0;
  172.                  j != diagram->shape[i].path_length; ) {
  173.                 switch ((int) diagram->shape[i].path[j]) {
  174.                 case svgtiny_PATH_MOVE:
  175.                     fb_path[fb_path_c].operation = NFSB_PLOT_PATHOP_MOVE;
  176.                     fb_path[fb_path_c].point.x = scale * diagram->shape[i].path[j + 1];
  177.                     fb_path[fb_path_c].point.y = scale * diagram->shape[i].path[j + 2];
  178.                     fb_path_c++;
  179.                     j += 3;
  180.                     break;
  181.  
  182.                 case svgtiny_PATH_CLOSE:       
  183.                     fb_path[fb_path_c].operation = NFSB_PLOT_PATHOP_LINE;
  184.                     fb_path[fb_path_c].point.x = fb_path[0].point.x;
  185.                     fb_path[fb_path_c].point.y = fb_path[0].point.y;
  186.                     fb_path_c++;
  187.                     j += 1;
  188.                     break;
  189.  
  190.                 case svgtiny_PATH_LINE:
  191.                     fb_path[fb_path_c].operation = NFSB_PLOT_PATHOP_LINE;
  192.                     fb_path[fb_path_c].point.x = scale * diagram->shape[i].path[j + 1];
  193.                     fb_path[fb_path_c].point.y = scale * diagram->shape[i].path[j + 2];
  194.                     fb_path_c++;
  195.  
  196.                     j += 3;
  197.                     break;
  198.  
  199.                 case svgtiny_PATH_BEZIER:
  200.                     fb_path[fb_path_c].operation = NFSB_PLOT_PATHOP_MOVE;
  201.                     fb_path[fb_path_c].point.x = scale * diagram->shape[i].path[j + 1];
  202.                     fb_path[fb_path_c].point.y = scale * diagram->shape[i].path[j + 2];
  203.                     fb_path_c++;
  204.                     fb_path[fb_path_c].operation = NFSB_PLOT_PATHOP_MOVE;
  205.                     fb_path[fb_path_c].point.x = scale * diagram->shape[i].path[j + 3];
  206.                     fb_path[fb_path_c].point.y = scale * diagram->shape[i].path[j + 4];
  207.                     fb_path_c++;
  208.                     fb_path[fb_path_c].operation = NFSB_PLOT_PATHOP_CUBIC;
  209.                     fb_path[fb_path_c].point.x = scale * diagram->shape[i].path[j + 5];
  210.                     fb_path[fb_path_c].point.y = scale * diagram->shape[i].path[j + 6];
  211.                     fb_path_c++;
  212.  
  213.                     j += 7;
  214.                     break;
  215.  
  216.                 default:
  217.                     printf("error ");
  218.                     j += 1;
  219.                 }
  220.             }
  221.  
  222.             nsfb_plot_path(nsfb, fb_path_c, fb_path, &pen);
  223.         } else if (diagram->shape[i].text) {
  224.             /* printf("text %g %g '%s' ",
  225.                scale * diagram->shape[i].text_x,
  226.                scale * diagram->shape[i].text_y,
  227.                diagram->shape[i].text);*/
  228.         }
  229.     }
  230.  
  231.     svgtiny_free(diagram);
  232.  
  233.     nsfb_update(nsfb, &screen_box);
  234.    
  235.     while (event.type != NSFB_EVENT_CONTROL)
  236.         nsfb_event(nsfb, &event, -1);
  237.  
  238.     return 0;
  239. }
  240.  
  241. /*
  242.  
  243. cc -g -std=c99 -D_BSD_SOURCE -I/home/vince/netsurf/libnsfb/include/ -I/home/vince/netsurf/libnsfb/src -I/usr/local/include -I/usr/include/libxml2 -Wall -Wextra -Wundef -Wpointer-arith -Wcast-align -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wnested-externs -Werror -pedantic -Wno-overlength-strings   -DNDEBUG -O2 -DBUILD_TARGET_Linux -DBUILD_HOST_Linux -o build-Linux-Linux-release-lib-static/test_svgtiny.o -c test/svgtiny.c
  244.  
  245. cc -o build-Linux-Linux-release-lib-static/test_svgtiny build-Linux-Linux-release-lib-static/test_svgtiny.o -Wl,--whole-archive -lnsfb -Wl,--no-whole-archive -lSDL -Lbuild-Linux-Linux-release-lib-static/ -lnsfb -lsvgtiny -lxml2
  246.  
  247.  */
  248.