added gfxmatrix_transform()
[swftools.git] / lib / gfxtools.c
index 77088ee..239368a 100644 (file)
@@ -56,9 +56,17 @@ static void linedraw_lineTo(gfxdrawer_t*d, gfxcoord_t x, gfxcoord_t y)
 {
     linedraw_internal_t*i = (linedraw_internal_t*)d->internal;
     gfxline_t*l = rfx_alloc(sizeof(gfxline_t));
+
+    if(!i->start) {
+       /* starts with a line, not with a moveto. That needs we first
+          need an explicit moveto to (0,0) */
+       linedraw_moveTo(d, 0, 0);
+    }
+
     l->type = gfx_lineTo;
     d->x = l->x = x;
     d->y = l->y = y;
+
     l->next = 0;
     if(i->next)
        i->next->next = l;
@@ -70,6 +78,13 @@ static void linedraw_splineTo(gfxdrawer_t*d, gfxcoord_t sx, gfxcoord_t sy, gfxco
 {
     linedraw_internal_t*i = (linedraw_internal_t*)d->internal;
     gfxline_t*l = rfx_alloc(sizeof(gfxline_t));
+
+    if(!i->start) {
+       /* starts with a line, not with a moveto. That needs we first
+          need an explicit moveto to (0,0) */
+       linedraw_moveTo(d, 0, 0);
+    }
+
     l->type = gfx_splineTo;
     d->x = l->x = x; 
     d->y = l->y = y;
@@ -94,6 +109,8 @@ static void* linedraw_result(gfxdrawer_t*d)
 void gfxdrawer_target_gfxline(gfxdrawer_t*d)
 {
     linedraw_internal_t*i = (linedraw_internal_t*)rfx_calloc(sizeof(linedraw_internal_t));
+    d->x = 0x7fffffff;
+    d->y = 0x7fffffff;
     d->internal = i;
     d->moveTo = linedraw_moveTo;
     d->lineTo = linedraw_lineTo;
@@ -611,3 +628,24 @@ void gfxmatrix_dump(gfxmatrix_t*m, FILE*fi, char*prefix)
     fprintf(fi, "%f %f | %f\n", m->m00, m->m10, m->tx);
     fprintf(fi, "%f %f | %f\n", m->m01, m->m11, m->ty);
 }
+
+void gfxmatrix_transform(gfxmatrix_t*m, double* v, double*dest)
+{
+    dest[0] = m->m00*v[0] + m->m10*v[1] + m->tx;
+    dest[1] = m->m01*v[0] + m->m11*v[1] + m->ty;
+}
+void gfxmatrix_invert(gfxmatrix_t*m, gfxmatrix_t*dest)
+{
+    double det = m->m00 * m->m11 - m->m10 * m->m01;
+    if(!det) {
+       memset(dest, 0, sizeof(gfxmatrix_t));
+       return;
+    }
+    det = 1/det;
+    dest->m00 = m->m11 * det;
+    dest->m01 = -m->m01 * det;
+    dest->m10 = -m->m10 * det;
+    dest->m11 = m->m00 * det;
+    dest->tx = -(dest->m00 * m->tx + dest->m10 * m->ty);
+    dest->ty = -(dest->m01 * m->tx + dest->m11 * m->ty);
+}