home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 10 / AU_CD10.iso / Archived / Updates / Flash / writeflash / !MakeFlash / c / shape < prev    next >
Text File  |  2000-04-19  |  10KB  |  336 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <ctype.h>
  6. //
  7. #include "proto.h"
  8. #include "flash.h"
  9. #include "main.h"
  10. #include "bucket.h"
  11. #include "bitcount.h"
  12. #include "matrix.h"
  13. #include "rectangle.h"
  14. #include "bbox.h"
  15. #include "gradient.h"
  16.  
  17.  
  18. static int fillstyle_read(FILLSTYLE *style);
  19. static int linestyle_read(LINESTYLE *style);
  20. static int fillstyle_write(FILLSTYLE *style);
  21. static int linestyle_write(LINESTYLE *style);
  22. static int fillstylearray_write(FILLSTYLEARRAY *styles);
  23. static int linestylearray_write(LINESTYLEARRAY *styles);
  24. static int calculate_bbox(SHAPE *shape);
  25.  
  26.  
  27. int shape_add_fillstyle(SHAPE *shp, FILLSTYLE *style) {
  28.  
  29.   FILLSTYLEARRAY *array;
  30.  
  31.   array = &shp->fillstyles;
  32.   if (!array->styles) {
  33.     array->styles = malloc(sizeof(FILLSTYLE));
  34.     if (!array->styles)                 return 1;
  35.     array->n = 0;
  36.   } else {
  37.     FILLSTYLE *newstyles;
  38.     newstyles = realloc(array->styles, (array->n+1)*sizeof(FILLSTYLE));
  39.     if (!newstyles)                     return 1;
  40.     array->styles = newstyles;
  41.   }
  42.  
  43.   memcpy(array->styles+array->n, style, sizeof(FILLSTYLE));
  44.   array->n++;
  45.  
  46.   return 0;
  47. }
  48.  
  49.  
  50. int shape_add_linestyle(SHAPE *shp, LINESTYLE *style) {
  51.  
  52.   LINESTYLEARRAY *array;
  53.  
  54.   array = &shp->linestyles;
  55.   if (!array->styles) {
  56.     array->styles = malloc(sizeof(LINESTYLE));
  57.     if (!array->styles)                 return 1;
  58.     array->n = 0;
  59.   } else {
  60.     LINESTYLE *newstyles;
  61.     newstyles = realloc(array->styles, (array->n+1)*sizeof(LINESTYLE));
  62.     if (!newstyles)                     return 1;
  63.     array->styles = newstyles;
  64.   }
  65.  
  66.   memcpy(array->styles+array->n, style, sizeof(LINESTYLE));
  67.   array->n++;
  68.  
  69.   return 0;
  70. }
  71.  
  72.  
  73. int shape_add_record(SHAPE *shape, SHAPERECORD *rec) {
  74.  
  75.   if (!shape->records) {
  76.     shape->records = malloc(sizeof(SHAPERECORD));
  77.     if (!shape->records)                return 1;
  78.     shape->n = 0;
  79.   } else {
  80.     SHAPERECORD *newrecords;
  81.     newrecords = realloc(shape->records, (shape->n+1)*sizeof(SHAPERECORD));
  82.     if (!newrecords)                    return 1;
  83.     shape->records = newrecords;
  84.   }
  85.  
  86.   memcpy(shape->records+shape->n, rec, sizeof(SHAPERECORD));
  87.   shape->n++;
  88.  
  89.   return 0;
  90. }
  91.  
  92.  
  93. int shape_create(SHAPE **shp) {
  94.  
  95.   SHAPE *shape;
  96.  
  97.   shape = malloc(sizeof(SHAPE));
  98.   if (!shape)                           return 1;
  99.  
  100.   *shp = shape;
  101.   memset(shape, 0, sizeof(SHAPE));
  102.  
  103.   return 0;
  104. }
  105.  
  106.  
  107. int shape_write(SHAPE *shape) {
  108.  
  109.   int i;
  110.   U32 fillbits, linebits, ptr;
  111.  
  112.   if (calculate_bbox(shape))                                return 1;
  113.  
  114.   if (flush_bucket())                                       return 1;
  115.   ptr = read_position(NULL);
  116.   if (write_ushort(0))                                      return 1;
  117.   if (write_ushort(shape->id))                              return 1;
  118.  
  119.   if (rect_write(&shape->bbox))                             return 1;
  120.   if (fillstylearray_write(&shape->fillstyles))             return 1;
  121.   if (linestylearray_write(&shape->linestyles))             return 1;
  122.   fillbits = bitcount(shape->fillstyles.n, 0, 0, 0);
  123.   linebits = bitcount(shape->linestyles.n, 0, 0, 0);
  124.   if (write_ubits(4, fillbits))                             return 1;
  125.   if (write_ubits(4, linebits))                             return 1;
  126.  
  127.   for (i = 0; i < shape->n; i++) {
  128.     SHAPERECORD *rec;
  129.     rec = shape->records+i;
  130.  
  131.     switch (rec->type) {
  132.     case SHAPERECORD_END:
  133.       if (write_ubits(1, 0))                                return 1;
  134.       if (write_ubits(5, 0))                                return 1;
  135.       break;
  136.     case SHAPERECORD_STYLE:
  137.       {
  138.         if (write_ubits(1, 0))                              return 1;
  139.         if (write_ubits(5, rec->flags))                     return 1;
  140.         if (rec->flags & SHAPERECORD_STYLE_MOVE) {
  141.           U32 bits;
  142.           bits = bitcount_signed(rec->x, rec->y, 0, 0);
  143.           if (write_ubits(5, bits))                         return 1;
  144.           if (write_bits(bits, rec->x))                     return 1;
  145.           if (write_bits(bits, rec->y))                     return 1;
  146.         }
  147.         if (rec->flags & SHAPERECORD_STYLE_FILL0)
  148.           if (write_ubits(fillbits, rec->fillstyle0))       return 1;
  149.         if (rec->flags & SHAPERECORD_STYLE_FILL1)
  150.           if (write_ubits(fillbits, rec->fillstyle1))       return 1;
  151.         if (rec->flags & SHAPERECORD_STYLE_LINE)
  152.           if (write_ubits(linebits, rec->linestyle))        return 1;
  153.       }
  154.       break;
  155.     case SHAPERECORD_STRAIGHT:
  156.       {
  157.         U32 bits;
  158.  
  159.         if (write_ubits(1, 1))                              return 1;
  160.         if (write_ubits(1, 1))                              return 1;
  161.         bits = bitcount_signed(rec->x, rec->y, 0, 0);
  162.         if (bits < 2)   bits = 2;
  163.         if (write_ubits(4, bits-2))                         return 1;
  164.         if ((rec->x == 0) || (rec->y == 0)) {
  165.           if (write_ubits(1, 0))                            return 1;
  166.           if (rec->y == 0) {
  167.             if (write_ubits(1, 0))                          return 1;
  168.             if (write_bits(bits, rec->x))                   return 1;
  169.           } else {
  170.             if (write_ubits(1, 1))                          return 1;
  171.             if (write_bits(bits, rec->y))                   return 1;
  172.           }
  173.         } else {
  174.           if (write_ubits(1, 1))                            return 1;
  175.           if (write_bits(bits, rec->x))                     return 1;
  176.           if (write_bits(bits, rec->y))                     return 1;
  177.         }
  178.       }
  179.       break;
  180.     case SHAPERECORD_CURVE:
  181.       {
  182.         U32 bits;
  183.  
  184.         if (write_ubits(1, 1))                              return 1;
  185.         if (write_ubits(1, 0))                              return 1;
  186.         bits = bitcount_signed(rec->x, rec->y, rec->ctrlx, rec->ctrly);
  187.         if (bits < 2)   bits = 2;
  188.         if (write_ubits(4, bits-2))                         return 1;
  189.         if (write_bits(bits, rec->ctrlx))                   return 1;
  190.         if (write_bits(bits, rec->ctrly))                   return 1;
  191.         if (write_bits(bits, rec->x))                       return 1;
  192.         if (write_bits(bits, rec->y))                       return 1;
  193.       }
  194.       break;
  195.     }
  196.   }
  197.  
  198.   return update_record_header(stagDefineShape3, ptr);
  199. }
  200.  
  201.  
  202. int shape_read(SHAPE *shape) {
  203.  
  204.   return 0;
  205. }
  206.  
  207.  
  208. int fillstylearray_write(FILLSTYLEARRAY *array) {
  209.  
  210.   int i;
  211.  
  212.   if (array->n > 254) {
  213.     if (write_ubyte(255))                                   return 1;
  214.     if (write_ushort(array->n))                             return 1;
  215.   } else
  216.     if (write_ubyte(array->n))                              return 1;
  217.  
  218.   for (i = 0; i < array->n; i++)
  219.     if (fillstyle_write(array->styles+i))                   return 1;
  220.  
  221.   return 0;
  222. }
  223.  
  224.  
  225. int linestylearray_write(LINESTYLEARRAY *array) {
  226.  
  227.   int i;
  228.  
  229.   if (array->n > 254) {
  230.     if (write_ubyte(255))                                   return 1;
  231.     if (write_ushort(array->n))                             return 1;
  232.   } else
  233.     if (write_ubyte(array->n))                              return 1;
  234.  
  235.   for (i = 0; i < array->n; i++)
  236.     if (linestyle_write(array->styles+i))                   return 1;
  237.  
  238.   return 0;
  239. }
  240.  
  241.  
  242. int fillstyle_write(FILLSTYLE *style) {
  243.  
  244.   if (write_ubyte(style->type))                             return 1;
  245.   switch (style->type) {
  246.   case FILLSTYLE_SOLID:
  247.     if (write_ubyte( style->fill.solid      & 0xff))        return 1;
  248.     if (write_ubyte((style->fill.solid>> 8) & 0xff))        return 1;
  249.     if (write_ubyte((style->fill.solid>>16) & 0xff))        return 1;
  250.     if (write_ubyte((style->fill.solid>>24) & 0xff))        return 1;
  251.     break;
  252.   case FILLSTYLE_LINEAR:
  253.   case FILLSTYLE_RADIAL:
  254.     if (matrix_write(&style->fill.gradient.matrix))         return 1;
  255.     if (gradient_write(&style->fill.gradient.gradient))     return 1;
  256.     break;
  257.   case FILLSTYLE_TILED:
  258.   case FILLSTYLE_CLIPPED:
  259.     if (write_ushort(style->fill.bitmap.id))                return 1;
  260.     if (matrix_write(&style->fill.bitmap.matrix))           return 1;
  261.     break;
  262.   }
  263.   return 0;
  264. }
  265.  
  266.  
  267. int linestyle_write(LINESTYLE *style) {
  268.  
  269.   if (write_ushort(style->width))                           return 1;
  270.   if (write_ubyte( style->rgba      & 0xff))                return 1;
  271.   if (write_ubyte((style->rgba>> 8) & 0xff))                return 1;
  272.   if (write_ubyte((style->rgba>>16) & 0xff))                return 1;
  273.   if (write_ubyte((style->rgba>>24) & 0xff))                return 1;
  274.  
  275.   return 0;
  276. }
  277.  
  278.  
  279. int fillstyle_read(FILLSTYLE *style) {
  280.  
  281.   return 0;
  282. }
  283.  
  284.  
  285. int linestyle_read(LINESTYLE *style) {
  286.  
  287.   return 0;
  288. }
  289.  
  290.  
  291. // -------------------------------------------------------------
  292.  
  293. int calculate_bbox(SHAPE *shape) {
  294.  
  295.   U16 width;
  296.   int i, context;
  297.   S32 x, y;
  298.  
  299.   width = 20;
  300.  
  301.   bbox_init(&shape->bbox, &context);
  302.  
  303.   x = y = 0;
  304.   for (i = 0; i < shape->n; i++) {
  305.     SHAPERECORD *rec;
  306.     rec = shape->records + i;
  307.     switch (rec->type) {
  308.     case SHAPERECORD_STYLE:
  309.       if (rec->flags == SHAPERECORD_STYLE_MOVE) {
  310.         x += rec->x;
  311.         y += rec->y;
  312.         bbox_add_point(&shape->bbox, &context, x, y, width);
  313.       }
  314.       break;
  315.     case SHAPERECORD_CURVE:
  316.       x += rec->x;
  317.       y += rec->y;
  318.       bbox_add_point(&shape->bbox, &context, x, y, width);
  319.       x += rec->ctrlx;
  320.       y += rec->ctrly;
  321.       bbox_add_point(&shape->bbox, &context, x, y, width);
  322.       break;
  323.     case SHAPERECORD_STRAIGHT:
  324.       x += rec->x;
  325.       y += rec->y;
  326.       bbox_add_point(&shape->bbox, &context, x, y, width);
  327.       break;
  328.     case SHAPERECORD_STYLE_LINE:
  329.       width = 20+shape->linestyles.styles[rec->linestyle].width;
  330.       break;
  331.     }
  332.   }
  333.  
  334.   return 0;
  335. }
  336.