bezctx_libart.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. ppedit - A pattern plate editor for Spiro splines.
  3. Copyright (C) 2007 Raph Levien
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301, USA.
  16. */
  17. #include <libart_lgpl/libart.h>
  18. #include "zmisc.h"
  19. #include "bezctx.h"
  20. #include "bezctx_libart.h"
  21. typedef struct {
  22. bezctx base;
  23. int n_bez;
  24. int n_bez_max;
  25. ArtBpath *bez;
  26. } bezctx_libart;
  27. static void
  28. bezctx_libart_moveto(bezctx *z, double x, double y, int is_open) {
  29. bezctx_libart *bc = (bezctx_libart *)z;
  30. ArtBpath *bp;
  31. if (bc->n_bez == bc->n_bez_max) {
  32. bc->bez = zrenew(ArtBpath, bc->bez, bc->n_bez_max <<= 1);
  33. }
  34. bp = &bc->bez[bc->n_bez++];
  35. bp->code = is_open ? ART_MOVETO_OPEN : ART_MOVETO;
  36. bp->x3 = x;
  37. bp->y3 = y;
  38. }
  39. void
  40. bezctx_libart_lineto(bezctx *z, double x, double y) {
  41. bezctx_libart *bc = (bezctx_libart *)z;
  42. ArtBpath *bp;
  43. if (bc->n_bez == bc->n_bez_max) {
  44. bc->bez = zrenew(ArtBpath, bc->bez, bc->n_bez_max <<= 1);
  45. }
  46. bp = &bc->bez[bc->n_bez++];
  47. bp->code = ART_LINETO;
  48. bp->x3 = x;
  49. bp->y3 = y;
  50. }
  51. void
  52. bezctx_libart_quadto(bezctx *z, double x1, double y1, double x2, double y2)
  53. {
  54. bezctx_libart *bc = (bezctx_libart *)z;
  55. ArtBpath *bp;
  56. double x0, y0;
  57. if (bc->n_bez == bc->n_bez_max) {
  58. bc->bez = zrenew(ArtBpath, bc->bez, bc->n_bez_max <<= 1);
  59. }
  60. bp = &bc->bez[bc->n_bez++];
  61. x0 = bp[-1].x3;
  62. y0 = bp[-1].y3;
  63. bp->code = ART_CURVETO;
  64. bp->x1 = x1 + (1./3) * (x0 - x1);
  65. bp->y1 = y1 + (1./3) * (y0 - y1);
  66. bp->x2 = x1 + (1./3) * (x2 - x1);
  67. bp->y2 = y1 + (1./3) * (y2 - y1);
  68. bp->x3 = x2;
  69. bp->y3 = y2;
  70. }
  71. void
  72. bezctx_libart_curveto(bezctx *z, double x1, double y1, double x2, double y2,
  73. double x3, double y3)
  74. {
  75. bezctx_libart *bc = (bezctx_libart *)z;
  76. ArtBpath *bp;
  77. if (bc->n_bez == bc->n_bez_max) {
  78. bc->bez = zrenew(ArtBpath, bc->bez, bc->n_bez_max <<= 1);
  79. }
  80. bp = &bc->bez[bc->n_bez++];
  81. bp->code = ART_CURVETO;
  82. bp->x1 = x1;
  83. bp->y1 = y1;
  84. bp->x2 = x2;
  85. bp->y2 = y2;
  86. bp->x3 = x3;
  87. bp->y3 = y3;
  88. }
  89. ArtBpath *
  90. bezctx_to_bpath(bezctx *z) {
  91. bezctx_libart *bc = (bezctx_libart *)z;
  92. ArtBpath *result;
  93. if (bc->n_bez == bc->n_bez_max) {
  94. bc->bez = zrenew(ArtBpath, bc->bez, bc->n_bez_max <<= 1);
  95. }
  96. bc->bez[bc->n_bez].code = ART_END;
  97. result = bc->bez;
  98. zfree(bc);
  99. return result;
  100. }
  101. bezctx *
  102. new_bezctx_libart(void) {
  103. bezctx_libart *result = znew(bezctx_libart, 1);
  104. result->base.moveto = bezctx_libart_moveto;
  105. result->base.lineto = bezctx_libart_lineto;
  106. result->base.quadto = bezctx_libart_quadto;
  107. result->base.curveto = bezctx_libart_curveto;
  108. result->base.mark_knot = NULL;
  109. result->n_bez = 0;
  110. result->n_bez_max = 4;
  111. result->bez = znew(ArtBpath, result->n_bez_max);
  112. return &result->base;
  113. }