/* curve.coord_rect([TRAFO]) * * Return the smallest aligned rectangle that contains all nodes and * control points. With optional argument TRAFO, compute the rectangle * as if the path were transformed by the transformation TRAFO. */ static PyObject * curve_coord_rect(SKCurveObject * self, PyObject * args) { SKRectObject * rect = NULL; CurveSegment * segment; PyObject * trafo = NULL; int i; if (!PyArg_ParseTuple(args, "|O!", &SKTrafoType, &trafo)) return NULL; if (self->len == 0) { Py_INCREF(SKRect_EmptyRect); return (PyObject*)SKRect_EmptyRect; } segment = self->segments; if (!trafo) { rect = (SKRectObject*)SKRect_FromDouble(segment->x, segment->y, segment->x, segment->y); if (!rect) return NULL; segment += 1; for (i = 1; i < self->len; i++, segment++) { SKRect_AddXY(rect, segment->x, segment->y); if (segment->type == CurveBezier) { SKRect_AddXY(rect, segment->x1, segment->y1); SKRect_AddXY(rect, segment->x2, segment->y2); } } } else { SKCoord x, y; SKTrafo_TransformXY(trafo, segment->x, segment->y, &x, &y); rect = (SKRectObject*)SKRect_FromDouble(x, y, x, y); if (!rect) return NULL; segment += 1; for (i = 1; i < self->len; i++, segment++) { SKTrafo_TransformXY(trafo, segment->x, segment->y, &x, &y); SKRect_AddXY(rect, x, y); if (segment->type == CurveBezier) { SKTrafo_TransformXY(trafo, segment->x1, segment->y1, &x, &y); SKRect_AddXY(rect, x, y); SKTrafo_TransformXY(trafo, segment->x2, segment->y2, &x, &y); SKRect_AddXY(rect, x, y); } } } return (PyObject*)rect; }
static PyObject * sktrafo_call(SKTrafoObject * self, PyObject * args, PyObject * kw) { PyObject * arg; double x, y; if (PyTuple_Size(args) == 2) arg = args; else if (!PyArg_ParseTuple(args, "O", &arg)) return NULL; if (skpoint_extract_xy(arg, &x, &y)) { return SKPoint_FromXY(self->m11 * x + self->m12 * y + self->v1, self->m21 * x + self->m22 * y + self->v2); } else if (SKTrafo_Check(arg)) { register SKTrafoObject * t = (SKTrafoObject *) arg; return SKTrafo_FromDouble(self->m11 * t->m11 + self->m12 * t->m21, self->m21 * t->m11 + self->m22 * t->m21, self->m11 * t->m12 + self->m12 * t->m22, self->m21 * t->m12 + self->m22 * t->m22, self->m11*t->v1 + self->m12*t->v2 + self->v1, self->m21*t->v1 + self->m22*t->v2 +self->v2); } else if (SKRect_Check(arg)) { SKRectObject * result; register SKRectObject * r = (SKRectObject*)arg; if (r == SKRect_InfinityRect || r == SKRect_EmptyRect) { Py_INCREF(r); return (PyObject*)r; } result = (SKRectObject*) \ SKRect_FromDouble(self->m11 * r->left + self->m12 * r->top, self->m21 * r->left + self->m22 * r->top, self->m11 * r->right + self->m12 * r->bottom, self->m21 * r->right + self->m22 * r->bottom); if (result) { SKRect_AddXY(result, self->m11 * r->right + self->m12 * r->top, self->m21 * r->right + self->m22 * r->top); SKRect_AddXY(result, self->m11 * r->left + self->m12 * r->bottom, self->m21 * r->left + self->m22 * r->bottom); result->left += self->v1; result->right += self->v1; result->top += self->v2; result->bottom += self->v2; } return (PyObject*) result; } PyErr_SetString(PyExc_TypeError, "SKTrafo must be applied to SKPoints, " "SKRects or SKTrafos"); return NULL; }
void init_sketch(void) { PyObject * d, *m, *r, *pax; m = Py_InitModule("_sketch", curve_functions); d = PyModule_GetDict(m); /* Rect specific initialization */ /* The InfinityRect is initialized with FLT_MAX instead of HUGE_VAL now (Sketch 0.5.4), because of problems with HUGE_VAL on Alpha Linux. */ r = SKRect_FromDouble(-FLT_MAX, -FLT_MAX, FLT_MAX, FLT_MAX); if (r) { PyDict_SetItemString(d, "InfinityRect", r); SKRect_InfinityRect = (SKRectObject*)r; } r = SKRect_FromDouble(0.0, 0.0, 0.0, 0.0); if (r) { PyDict_SetItemString(d, "EmptyRect", r); SKRect_EmptyRect = (SKRectObject*)r; } /* Trafo specific initialization */ SKTrafo_ExcSingular = PyErr_NewException("_sketch.SingularMatrix", PyExc_ArithmeticError, NULL); if (SKTrafo_ExcSingular) { PyDict_SetItemString(d, "SingularMatrix", SKTrafo_ExcSingular); } /* Sketch type objects */ PyDict_SetItemString(d, "RectType", (PyObject*)&SKRectType); PyDict_SetItemString(d, "PointType", (PyObject*)&SKPointType); PyDict_SetItemString(d, "TrafoType", (PyObject*)&SKTrafoType); PyDict_SetItemString(d, "CurveType", (PyObject*)&SKCurveType); PyDict_SetItemString(d, "ColorType", (PyObject*)&SKColorType); /* Curve specific initialization */ #define ADD_INT(name) add_int(d, name, #name) #define ADD_INT2(i, name) add_int(d, i, name) ADD_INT(ContAngle); ADD_INT(ContSmooth); ADD_INT(ContSymmetrical); ADD_INT2(CurveBezier, "Bezier"); ADD_INT2(CurveLine, "Line"); ADD_INT(SelNone); ADD_INT(SelNodes); ADD_INT(SelSegmentFirst); ADD_INT(SelSegmentLast); _SKCurve_InitCurveObject(); /* import some objects from pax */ pax = PyImport_ImportModule("pax"); if (pax) { Pax_GCType = PyObject_GetAttrString(pax, "PaxGCType"); if (!Pax_GCType) return; Pax_ImageType = PyObject_GetAttrString(pax, "PaxImageType"); if (!Pax_ImageType) return; r = PyObject_GetAttrString(pax, "Pax_Functions"); if (!r) return; pax_functions = (Pax_Functions*)PyCObject_AsVoidPtr(r); Py_DECREF(r); } }
init_sk1objs(void) { PyObject * d, *m, *r; SKCurveType.ob_type = &PyType_Type; SKCacheType.ob_type = &PyType_Type; SKColorType.ob_type = &PyType_Type; SKFontMetricType.ob_type = &PyType_Type; SKPointType.ob_type = &PyType_Type; SKRectType.ob_type = &PyType_Type; SKTrafoType.ob_type = &PyType_Type; m = Py_InitModule("_sk1objs", curve_functions); d = PyModule_GetDict(m); /* Rect specific initialization */ /* The InfinityRect is initialized with FLT_MAX instead of HUGE_VAL now (Sketch 0.5.4), because of problems with HUGE_VAL on Alpha Linux. */ r = SKRect_FromDouble(-FLT_MAX, -FLT_MAX, FLT_MAX, FLT_MAX); if (r) { PyDict_SetItemString(d, "InfinityRect", r); SKRect_InfinityRect = (SKRectObject*)r; } r = SKRect_FromDouble(0.0, 0.0, 0.0, 0.0); if (r) { PyDict_SetItemString(d, "EmptyRect", r); SKRect_EmptyRect = (SKRectObject*)r; } /* Trafo specific initialization */ SKTrafo_ExcSingular = PyErr_NewException("_sk1objs.SingularMatrix", PyExc_ArithmeticError, NULL); if (SKTrafo_ExcSingular) { PyDict_SetItemString(d, "SingularMatrix", SKTrafo_ExcSingular); } /* Sketch type objects */ PyDict_SetItemString(d, "RectType", (PyObject*)&SKRectType); PyDict_SetItemString(d, "PointType", (PyObject*)&SKPointType); PyDict_SetItemString(d, "TrafoType", (PyObject*)&SKTrafoType); PyDict_SetItemString(d, "CurveType", (PyObject*)&SKCurveType); /* Curve specific initialization */ #define ADD_INT(name) add_int(d, name, #name) #define ADD_INT2(i, name) add_int(d, i, name) ADD_INT(ContAngle); ADD_INT(ContSmooth); ADD_INT(ContSymmetrical); ADD_INT2(CurveBezier, "Bezier"); ADD_INT2(CurveLine, "Line"); ADD_INT(SelNone); ADD_INT(SelNodes); ADD_INT(SelSegmentFirst); ADD_INT(SelSegmentLast); _SKCurve_InitCurveObject(); }