📄 graphicsmodule.cpp
字号:
}
for (i=0; i<n_coords/2; i++) {
int x1,y1,x2,y2;
if (!PyCoordSeq_GetItem(coordseq_obj,i*2,&x1,&y1)||
!PyCoordSeq_GetItem(coordseq_obj,i*2+1,&x2,&y2))
goto error;
TRect bbox(x1,y1,x2,y2);
TPoint startPoint=TPoint_FromAngleAndBoundingBox(bbox,start_angle);
TPoint endPoint=TPoint_FromAngleAndBoundingBox(bbox,end_angle);
gc->DrawArc(bbox,startPoint,endPoint);
}
Graphics_ResetParams(self);
PY_RETURN_NONE;
error:
Graphics_ResetParams(self);
return NULL;
}
extern "C" PyObject*
Draw_pieslice(Draw_object *self, PyObject *args, PyObject *keywds)
{
CBitmapContext *gc=self->gc;
PyObject *coordseq_obj=NULL;
int n_coords,i;
float start_angle,end_angle;
if (!Graphics_ParseAndSetParams(self,args,keywds,&coordseq_obj,&n_coords,&start_angle,&end_angle))
return NULL;
if (n_coords % 2 != 0) {
PyErr_SetString(PyExc_ValueError, "even number of coordinates expected");
goto error;
}
for (i=0; i<n_coords/2; i++) {
int x1,y1,x2,y2;
if (!PyCoordSeq_GetItem(coordseq_obj,i*2,&x1,&y1)||
!PyCoordSeq_GetItem(coordseq_obj,i*2+1,&x2,&y2))
goto error;
TRect bbox(x1,y1,x2,y2);
TPoint startPoint=TPoint_FromAngleAndBoundingBox(bbox,start_angle);
TPoint endPoint=TPoint_FromAngleAndBoundingBox(bbox,end_angle);
gc->DrawPie(bbox,startPoint,endPoint);
}
Graphics_ResetParams(self);
PY_RETURN_NONE;
error:
Graphics_ResetParams(self);
return NULL;
}
extern "C" PyObject*
Draw_point(Draw_object *self, PyObject *args, PyObject *keywds)
{
CBitmapContext *gc=self->gc;
PyObject *coordseq_obj=NULL;
int n_coords;
if (!Graphics_ParseAndSetParams(self,args,keywds,&coordseq_obj,&n_coords))
return NULL;
TPoint point;
if (!PyCoordSeq_GetItem(coordseq_obj, 0, &point.iX, &point.iY))
goto error;
gc->Plot(point);
Graphics_ResetParams(self);
PY_RETURN_NONE;
error:
Graphics_ResetParams(self);
return NULL;
}
extern "C" PyObject*
Draw_line(Draw_object *self, PyObject *args, PyObject *keywds)
{
CBitmapContext *gc=self->gc;
PyObject *coordseq_obj=NULL;
int n_coords,i;
if (!Graphics_ParseAndSetParams(self,args,keywds,&coordseq_obj,&n_coords))
return NULL;
TPoint point;
if (!PyCoordSeq_GetItem(coordseq_obj, 0, &point.iX, &point.iY))
goto error;
gc->MoveTo(point);
for (i=0; i<n_coords; i++) {
if (!PyCoordSeq_GetItem(coordseq_obj, i, &point.iX, &point.iY))
goto error;
gc->DrawLineTo(point);
}
Graphics_ResetParams(self);
PY_RETURN_NONE;
error:
Graphics_ResetParams(self);
return NULL;
}
extern "C" PyObject*
Draw_polygon(Draw_object *self, PyObject *args, PyObject *keywds)
{
CBitmapContext *gc=self->gc;
PyObject *coordseq_obj=NULL;
int n_coords,i;
if (!Graphics_ParseAndSetParams(self,args,keywds,&coordseq_obj,&n_coords))
return NULL;
TPoint *points=new TPoint[n_coords];
if (!points)
goto error;
for (i=0; i<n_coords; i++)
if (!PyCoordSeq_GetItem(coordseq_obj,i, &points[i].iX, &points[i].iY))
goto error;
gc->DrawPolygon(points,n_coords);
delete[] points;
Graphics_ResetParams(self);
PY_RETURN_NONE;
error:
delete[] points;
Graphics_ResetParams(self);
return NULL;
}
extern "C" PyObject*
Draw_clear(Draw_object *self, PyObject *args, PyObject *keywds)
{
CBitmapContext *gc=self->gc;
PyObject *fill_obj=NULL;
TRgb fill_color(255,255,255);
static const char *const kwlist[] = {"fill", NULL};
if (!PyArg_ParseTupleAndKeywords(args, keywds, "|O", (char**)kwlist,
&fill_obj) ||
(fill_obj && !ColorSpec_AsRgb(fill_obj,&fill_color))) {
return NULL;
}
gc->SetBrushColor(fill_color);
gc->Clear();
PY_RETURN_NONE;
}
#define FAILSYMBIAN(phase) do { ret=SPyErr_SetFromSymbianOSErr(error); goto fail##phase; } while(0)
#define FAILSYMBIAN_IFERROR(phase) do { if (error != KErrNone) FAILSYMBIAN(phase); } while (0)
extern "C" PyObject*
Draw_text(Draw_object *self, PyObject *args, PyObject *keywds)
{
int length,i;
char* buf;
CBitmapContext *gc=self->gc;
PyObject *fill_obj=NULL;
TRgb fill_color(0,0,0);
PyObject *coordseq_object=NULL;
PyObject *new_font_spec_object=Py_None;
int n_coords;
static const char *const kwlist[] = {"coords", "text", "fill", "font", NULL};
if (!PyArg_ParseTupleAndKeywords(args, keywds, "Ou#|OO", (char**)kwlist,
&coordseq_object,
&buf,&length,
&fill_obj,
&new_font_spec_object) ||
!PyCoordSeq_Length(coordseq_object, &n_coords) ||
(fill_obj && !ColorSpec_AsRgb(fill_obj,&fill_color)))
return NULL;
if (self->font_spec_object != new_font_spec_object) {
/* Using a different font than before.
* For efficiency we care only about object identity, not value. */
CFont *font=NULL;
// if (-1 == CFont_from_python_fontspec(new_font_spec_object, font, *device))
if (-1 == self->fontcache->CFont_from_python_fontspec(new_font_spec_object, font))
return NULL;
gc->DiscardFont(); /* This is safe even if font is not set. */
/*if (self->cfont)
device->ReleaseFont(self->cfont);*/
self->cfont=font;
Py_XDECREF(self->font_spec_object);
self->font_spec_object=new_font_spec_object;
Py_INCREF(self->font_spec_object);
gc->UseFont(font);
}
gc->SetPenColor(fill_color);
for (i=0; i<n_coords; i++) {
TPoint loc;
if (!PyCoordSeq_GetItem(coordseq_object,i,&loc.iX,&loc.iY)) {
return NULL;
}
gc->DrawText(TPtrC((TUint16 *)buf,length), loc);
}
PY_RETURN_NONE;
}
extern "C" PyObject*
Draw_measure_text(Draw_object *self, PyObject *args, PyObject *keywds)
{
char* aText_buf;
int aText_length;
int aMaxWidth=-1;
int aMaxAdvance=-1;
PyObject *aPyFontSpec_object=Py_None;
static const char *const kwlist[] = {"text", "font", "maxwidth", "maxadvance", NULL};
if (!PyArg_ParseTupleAndKeywords(args, keywds, "u#|Oii", (char**)kwlist,
&aText_buf,&aText_length,
&aPyFontSpec_object,
&aMaxWidth,
&aMaxAdvance))
return NULL;
CFont *font=NULL;
// if (-1 == CFont_from_python_fontspec(aPyFontSpec_object, font, *device))
if (-1 == self->fontcache->CFont_from_python_fontspec(aPyFontSpec_object, font))
return NULL;
TPtrC textDes((const unsigned short *)aText_buf, aText_length);
CFont::TMeasureTextInput mti;
CFont::TMeasureTextOutput mto;
if (aMaxWidth != -1)
mti.iMaxBounds = aMaxWidth;
if (aMaxAdvance != -1)
mti.iMaxAdvance = aMaxAdvance;
int advance=font->MeasureText(textDes, &mti, &mto);
return Py_BuildValue("((iiii)ii)",
mto.iBounds.iTl.iX, mto.iBounds.iTl.iY,
mto.iBounds.iBr.iX, mto.iBounds.iBr.iY,
advance,
mto.iChars);
}
extern "C" PyObject *
graphics_screenshot(PyObject* /*self*/)
{
TInt error;
Image_object *obj;
if (!(obj = PyObject_New(Image_object, Image_type)))
return PyErr_NoMemory();
TRAP(error, {
obj->data = new (ELeave) ImageObject();
CleanupStack::PushL(obj->data);
obj->data->ConstructL(new (ELeave) CFbsBitmap());
CleanupStack::Pop();
});
if(error != KErrNone){
PyObject_Del(obj);
return SPyErr_SetFromSymbianOSErr(error);
}
error = (obj->data->GetBitmap())->Create(CCoeEnv::Static()->ScreenDevice()->SizeInPixels(),
CCoeEnv::Static()->ScreenDevice()->DisplayMode());
if (error != KErrNone) {
PyObject_Del(obj);
return SPyErr_SetFromSymbianOSErr(error);
}
error = CCoeEnv::Static()->ScreenDevice()->CopyScreenToBitmap(obj->data->GetBitmap());
if (error != KErrNone) {
PyObject_Del(obj);
return SPyErr_SetFromSymbianOSErr(error);
}
return (PyObject*)obj;
}
static void Draw_dealloc(Draw_object *obj)
{
obj->gc->DiscardFont();
/*if (obj->cfont) {
static_cast<CBitmapDevice *>(obj->gc->Device())->ReleaseFont(obj->cfont);
obj->cfont=NULL;
}*/
delete obj->fontcache;
Py_DECREF(obj->drawapi_cobject);
Py_XDECREF(obj->font_spec_object);
PyObject_Del(obj);
}
const static PyMethodDef Draw_methods[] = {
{"line", (PyCFunction)Draw_line, METH_VARARGS|METH_KEYWORDS},
{"blit", (PyCFunction)Draw_blit, METH_VARARGS|METH_KEYWORDS},
{"rectangle", (PyCFunction)Draw_rectangle, METH_VARARGS|METH_KEYWORDS},
{"ellipse", (PyCFunction)Draw_ellipse, METH_VARARGS|METH_KEYWORDS},
{"arc", (PyCFunction)Draw_arc, METH_VARARGS|METH_KEYWORDS},
{"pieslice", (PyCFunction)Draw_pieslice, METH_VARARGS|METH_KEYWORDS},
{"point", (PyCFunction)Draw_point, METH_VARARGS|METH_KEYWORDS},
{"polygon", (PyCFunction)Draw_polygon, METH_VARARGS|METH_KEYWORDS},
{"clear", (PyCFunction)Draw_clear, METH_VARARGS|METH_KEYWORDS},
{"text", (PyCFunction)Draw_text, METH_VARARGS|METH_KEYWORDS},
{"measure_text", (PyCFunction)Draw_measure_text, METH_VARARGS|METH_KEYWORDS},
{NULL, NULL} // sentinel
};
static PyObject *
Draw_getattr(Draw_object *op, char *name)
{
return Py_FindMethod((PyMethodDef*)Draw_methods,
(PyObject *)op, name);
}
static const PyTypeObject c_Draw_type = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"graphics.Draw", /*tp_name*/
sizeof(Draw_object), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
(destructor)Draw_dealloc, /*tp_dealloc*/
0, /*tp_print*/
(getattrfunc)Draw_getattr, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash*/
};
static void Image_dealloc(Image_object *obj)
{
delete obj->data;
PyObject_Del(obj);
}
const static PyMethodDef Image_methods[] = {
{"_drawapi", (PyCFunction)Image__drawapi, METH_VARARGS},
{"_bitmapapi", (PyCFunction)Image__bitmapapi, METH_VARARGS},
{"getpixel", (PyCFunction)Image_getpixel, METH_VARARGS},
#ifdef ICL_SUPPORT
{"load", (PyCFunction)Image_load, METH_VARARGS},
{"save", (PyCFunction)Image_save, METH_VARARGS},
{"resize", (PyCFunction)Image_resize, METH_VARARGS},
{"transpose", (PyCFunction)Image_transpose, METH_VARARGS},
{"stop", (PyCFunction)Image_stop, METH_NOARGS},
#endif /* ICL_SUPPORT */
{NULL, NULL} // sentinel
};
static PyObject *
Image_getattr(Image_object *op, char *name)
{
if (!strcmp(name,"size")) {
TSize size=op->data->Size();
return Py_BuildValue("(ii)",size.iWidth,size.iHeight);
}
if (!strcmp(name,"twipsize")) {
TSize twipSize=op->data->TwipSize();
return Py_BuildValue("(ii)",twipSize.iWidth,twipSize.iHeight);
}
if (!strcmp(name,"mode")) {
return Py_BuildValue("i",op->data->DisplayMode());
}
return Py_FindMethod((PyMethodDef*)Image_methods,
(PyObject *)op, name);
}
static int Image_setattr(Image_object *op, char *name, PyObject *v)
{
TInt aWidthInTwip;
TInt aHeightInTwip;
if (!strcmp(name,"twipsize")) {
if (!PyArg_ParseTuple(v, "ii", &aWidthInTwip, &aHeightInTwip))
return -1;
else
op->data->SetTwipSize(aWidthInTwip, aHeightInTwip);
}
else {
PyErr_SetString(PyExc_AttributeError, "no such attribute");
return -1;
}
return 0;
}
static const PyTypeObject c_Image_type = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"graphics.Image", /*tp_name*/
sizeof(Image_object), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
(destructor)Image_dealloc, /*tp_dealloc*/
0, /*tp_print*/
(getattrfunc)Image_getattr, /*tp_getattr*/
(setattrfunc)Image_setattr, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash*/
};
static const PyMethodDef graphics_methods[] = {
{"ImageNew", (PyCFunction)graphics_ImageNew, METH_VARARGS, NULL},
{"ImageFromCFbsBitmap", (PyCFunction)graphics_ImageFromCFbsBitmap, METH_VARARGS, NULL},
#ifdef EKA2
{"ImageFromIcon", (PyCFunction)graphics_ImageFromIcon, METH_VARARGS, NULL},
#endif
#ifdef ICL_SUPPORT
{"ImageOpen", (PyCFunction)graphics_ImageOpen, METH_VARARGS, NULL},
{"ImageInspect", (PyCFunction)graphics_ImageInspect, METH_VARARGS, NULL},
#endif // ICL_SUPPORT
{"Draw", (PyCFunction)graphics_Draw, METH_VARARGS, NULL},
// {"rawscreen", (PyCFunction)graphics_rawscreen, METH_VARARGS, NULL},
{"screenshot", (PyCFunction)graphics_screenshot, METH_NOARGS, NULL},
{NULL, NULL} /* sentinel */
};
#define DEFTYPE(name,type_template) do { \
PyTypeObject* tmp = PyObject_New(PyTypeObject, &PyType_Type); \
*tmp = (type_template); \
tmp->ob_type = &PyType_Type; \
SPyAddGlobalString((name), (PyObject*)tmp); \
} while (0)
extern "C" {
DL_EXPORT(void) initgraphics(void)
{
PyObject *m;
DEFTYPE("DrawType",c_Draw_type);
DEFTYPE("ImageType",c_Image_type);
//RFbsSession::Connect();
m = Py_InitModule("_graphics", (PyMethodDef*)graphics_methods);
PyObject *d = PyModule_GetDict(m);
#define DEFLONG(pyname,value) PyDict_SetItemString(d,pyname, PyInt_FromLong(value))
#define DEFLONGX(name) DEFLONG(#name,name)
DEFLONGX(EGray2);
DEFLONGX(EGray256);
DEFLONGX(EColor4K);
DEFLONGX(EColor64K);
DEFLONGX(EColor16M);
PyDict_SetItemString(d, "_draw_methods",
Py_BuildValue("[sssssssssss]",
"line",
"blit",
"rectangle",
"ellipse",
"arc",
"pieslice",
"point",
"polygon",
"clear",
"text",
"measure_text"));
#ifdef ICL_SUPPORT
DEFLONGX(FLIP_LEFT_RIGHT);
DEFLONGX(FLIP_TOP_BOTTOM);
DEFLONGX(ROTATE_90);
DEFLONGX(ROTATE_180);
DEFLONGX(ROTATE_270);
DEFLONG("ICL_SUPPORT",1);
#else
DEFLONG("ICL_SUPPORT",0);
#endif //ICL_SUPPORT
}
DL_EXPORT(void) zfinalizegraphics(void)
{
//RFbsSession::Disconnect();
}
} /* extern "C" */
#ifndef EKA2
GLDEF_C TInt E32Dll(TDllReason)
{
return KErrNone;
}
#endif /*EKA2*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -