Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.95 KB | None | 0 0
  1. #include <jansson.h>
  2. #include <cairo/cairo.h>
  3. #include <math.h>
  4. #include <string.h>
  5. #include "projeto.h"
  6.  
  7.  
  8. int main(){
  9.  
  10.    json_t *root;
  11.    json_error_t error;
  12.    root = json_load_file("test.json", 0, &error);
  13.    if(!root){
  14.    fprintf(stderr, "error: on line %d: %s\n", error.line, error.text);
  15.    return 1;
  16.    }
  17.  
  18.    json_t * jfilename = json_object_get(root, "file_name"); //Obtendo o nome do arquivo
  19.    const char * filename = json_string_value(jfilename);
  20.  
  21.    json_t * jtype = json_object_get(root, "file_type"); //Obtendo o tipo de arquivo
  22.    const char * filetype = json_string_value(jtype);
  23.  
  24.    char * fn = (char *)malloc (strlen (filename) + strlen (filetype) + 2);
  25.    sprintf(fn, "%s.%s", filename, filetype);
  26.    printf("Filename: %s\n", fn);
  27.  
  28.    json_t * jwidth = json_object_get(root, "width"); //Obtendo a largura
  29.    int width = json_number_value(jwidth);
  30.    printf("Width: %d\n", width);
  31.  
  32.    json_t * jheight = json_object_get(root, "height"); //Obtendo a altura
  33.    int height = json_number_value(jheight);
  34.    printf("Height: %d\n", height);
  35.  
  36.  
  37.    cairo_surface_t *surface;
  38.    cairo_t *cr;
  39.    
  40.    surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
  41.    cr = cairo_create(surface);
  42.    
  43.  
  44.    json_t * jcontent = json_object_get(root, "content"); //Obtendo as definições
  45.  
  46.    for(int i = 0; i < json_array_size(jcontent); ++i){
  47.         json_t * jdados = json_array_get(jcontent, i);
  48.         int type =  json_number_value(json_object_get(jdados, "type")); //Identificando a saida
  49.         switch(type){
  50.             case 0:{
  51.                 int x = json_number_value(json_object_get(jdados, "x"));
  52.                 int y = json_number_value(json_object_get(jdados, "y"));
  53.                 int width = json_number_value(json_object_get(jdados, "width"));
  54.                 int height =  json_number_value(json_object_get(jdados, "height"));
  55.         int linewidth = json_number_value(json_object_get(jdados, "line_width"));      
  56.         json_t * bgcolor = json_object_get(jdados, "background_color");
  57.         float red =  json_number_value(json_object_get(bgcolor, "r"));
  58.         float green =  json_number_value(json_object_get(bgcolor, "g"));
  59.         float blue =  json_number_value(json_object_get(bgcolor, "b"));
  60.         //float alpha =  json_number_value(json_object_get(bgcolor, "a"));
  61.  
  62.         json_t * stcolor = json_object_get(jdados, "stroke_color");
  63.         float sred =  json_number_value(json_object_get(stcolor, "r"));
  64.         float sgreen =  json_number_value(json_object_get(stcolor, "g"));
  65.         float sblue =  json_number_value(json_object_get(stcolor, "b"));
  66.         //float salpha =  json_number_value(json_object_get(stcolor, "a"));
  67.                 makeRectangle(cr, x, y, width, height, linewidth, red, green, blue, sred, sgreen, sblue);
  68.                 break;
  69.             }
  70.             default:
  71.                 break;
  72.         }
  73.    }
  74.  
  75.    cairo_surface_write_to_png(surface, fn);
  76.    cairo_destroy(cr);
  77.    cairo_surface_destroy(surface);
  78.    free (fn);
  79.  
  80.  
  81. return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement