Tidy PyList creation code. Add test driver.

This commit is contained in:
WandererFan 2013-03-22 10:35:34 -04:00
parent 830743d2e6
commit c723cc99d1
4 changed files with 58 additions and 41 deletions

View File

@ -118,14 +118,6 @@
#include "ImportStep.h"
#include "edgecluster.h"
/*//needed in AppPartPy???
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_OUTLINE_H
#include FT_GLYPH_H
#include FT_TYPES_H
//??*/
#include "FT2FC.h"
using Base::Console;
@ -335,11 +327,6 @@ static PyObject * makeWireString(PyObject *self, PyObject *args)
Py_UNICODE *unichars;
Py_ssize_t pysize;
// std::vector <std::vector <TopoShapeWirePy*> > ret;
// std::vector<TopoShapeWirePy*>::iterator iWire;
// std::vector<std::vector<TopoShapeWirePy*> >:: iterator iChar;
// PyObject *WireList, *CharList;
PyObject *CharList;
if (!PyArg_ParseTuple(args, "Ossf|i", &intext,
@ -381,16 +368,6 @@ static PyObject * makeWireString(PyObject *self, PyObject *args)
return NULL;
}
/* // if (ret not empty)
CharList = PyList_New(0);
for (iChar = ret.begin(); iChar !=ret.end(); ++iChar) {
WireList = PyList_New(0);
for (iWire = iChar->begin(); iWire != iChar->end(); ++iWire){
PyList_Append(WireList,*iWire);
}
// if (list not empty)
PyList_Append(CharList,WireList);
}*/
return (CharList);
}
@ -1599,7 +1576,7 @@ struct PyMethodDef Part_methods[] = {
"makeLoft(list of wires) -- Create a loft shape."},
{"makeWireString" ,makeWireString ,METH_VARARGS,
"makeWireString(fontdir,fontfile,string,height,[track]) -- Make wires in the form of a string."},
"makeWireString(fontdir,fontfile,string,height,[track]) -- Make list of wires in the form of a string's characters."},
{"cast_to_shape" ,cast_to_shape,METH_VARARGS,
"cast_to_shape(shape) -- Cast to the actual shape type"},

View File

@ -36,18 +36,14 @@
using namespace Part;
typedef unsigned long UNICHAR; // ul is FT2's codepoint type <=> Py_UNICODE2/4
//typedef std::vector <std::vector <TopoShapeWirePy*> > FT2FCRET;
typedef PyObject* FT2FCRET;
// Private function prototypes
//std::vector<TopoShapeWirePy*> getGlyphContours();
PyObject* getGlyphContours(FT_Face FTFont, UNICHAR currchar, int PenPos, float Scale);
FT_Vector getKerning(FT_Face FTFont, UNICHAR lc, UNICHAR rc);
TopoShapeWirePy* edgesToWire(std::vector<TopoDS_Edge> Edges);
// get string's wires (contours) in FC/OCC coords
//FT2FCRET FT2FC(const Py_UNICODE *pustring,
PyObject* FT2FC(const Py_UNICODE *pustring,
PyObject* FT2FC(const Py_UNICODE *PyUString,
const size_t length,
const char *FontPath,
const char *FontName,
@ -66,8 +62,6 @@ PyObject* FT2FC(const Py_UNICODE *pustring,
UNICHAR prevchar = 0, currchar = 0;
int cadv, PenPos = 0, PyErr;
size_t i;
// std::vector<TopoShapeWirePy*> CharWires;
// FT2FCRET Ret;
PyObject *WireList, *CharList;
@ -113,7 +107,7 @@ PyObject* FT2FC(const Py_UNICODE *pustring,
scalefactor = float(stringheight/FTFont->height);
CharList = PyList_New(0);
for (i=0; i<length; i++) {
currchar = pustring[i];
currchar = PyUString[i];
error = FT_Load_Char(FTFont,
currchar,
FTLoadFlags);
@ -125,13 +119,10 @@ PyObject* FT2FC(const Py_UNICODE *pustring,
cadv = FTFont->glyph->advance.x;
kern = getKerning(FTFont,prevchar,currchar);
PenPos += kern.x;
// CharWires = getGlyphContours(FTFont,currchar,PenPos, scalefactor);
WireList = getGlyphContours(FTFont,currchar,PenPos, scalefactor);
// if (CharWires.empty()) // whitespace char
if (!PyList_Size(WireList)) // empty ==> whitespace
std::cout << "char " << i << " = " << hex << std::showbase << currchar << " has no wires! " << std::endl;
else
// Ret.push_back(CharWires);
PyErr = PyList_Append(CharList, WireList); //add error check
// not entirely happy with tracking solution. It's specified in FC units,
// so we have to convert back to font units to use it here. We could
@ -170,7 +161,6 @@ static int move_cb(const FT_Vector* pt, void* p) {
if (!dc->Edges.empty()){
TopoShapeWirePy* newwire;
newwire = edgesToWire(dc->Edges);
// dc->TWires.push_back(newwire);
PyErr = PyList_Append(dc->WireList, newwire); // add error check
dc->Edges.clear();
}
@ -196,6 +186,7 @@ static int line_cb(const FT_Vector* pt, void* p) {
dc->LastVert.y = pt->y;
return 0;
}
// quad_cb called for quadratic (conic) BCurve segment in the current contour
// (ie V-C-V in TTF fonts). BCurve(LastVert -- pt0 -- pt1)
static int quad_cb(const FT_Vector* pt0, const FT_Vector* pt1, void* p) {
@ -214,7 +205,7 @@ static int quad_cb(const FT_Vector* pt0, const FT_Vector* pt1, void* p) {
Poles.SetValue(1, v1);
Poles.SetValue(2, c1);
Poles.SetValue(3, v2);
// "new" bcseg? need to free this, but don't know when. does makeedge need it forever? or just for creation?
// "new" bcseg? need to free this? don't know when. does makeedge need it forever? or just for creation?
// how to delete a "handle"? memory leak?
Handle(Geom_BezierCurve) bcseg = new Geom_BezierCurve(Poles);
BRepBuilderAPI_MakeEdge makeEdge(bcseg, v1, v2);
@ -265,8 +256,6 @@ static FT_Outline_Funcs FTcbFuncs = {
0, 0 // not needed for FC
};
//
//********** FT2FC Helpers
// get glyph outline in wires
//std::vector<TopoShapeWirePy*> getGlyphContours(FT_Face FTFont, UNICHAR currchar, int PenPos, float Scale) {

View File

@ -2,7 +2,6 @@
#ifndef FT2FC_H
#define FT2FC_H
// public function
//std::vector <std::vector <Part::TopoShapeWirePy*> > FT2FC(const Py_UNICODE *unichars,
PyObject* FT2FC(const Py_UNICODE *unichars,
const size_t length,
const char *FontPath,

View File

@ -0,0 +1,52 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# tester for Part.makeWireString
import FreeCAD
import Part
import PartDesign
print "testWire started"
# test strings
# if string contains funky characters, it has to be declared as Unicode or it
# turns into the default encoding (usually utf8). FT2 doesn't do utf8.
#String = 'Wide WMA_' # wide glyphs for tracking
#String = 'Big'
#String = u'ecAnO' # UCS-2 w/ only ASCII
#String = u'ucs2uéçÄñØ' # UCS-2
#String = 'utf8!uéçÄñØ' # UTF-8
#String = 'abcdefghijklmnopqrstuvwxyz0123456789'
#String = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
#String = 'Big Daddy' # white space
#String = 'AVWAIXA.V' # kerning
String = 'FreeCAD' # ASCII
#FontPath = '/usr/share/fonts/truetype/msttcorefonts/'
#FontName = 'Times_New_Roman_Italic.ttf'
FontPath = '/usr/share/fonts/truetype/msttcorefonts/'
#FontName = 'Arial.ttf'
FontName = 'NOTArial.ttf' # font file not found error
#FontPath = '/usr/share/fonts/truetype/msttcorefonts/'
#FontName = 'ariali.ttf' #symlink to ttf
#FontPath = '/usr/share/fonts/truetype/'
#FontName = 'Peterbuilt.ttf' # overlapping script font
#FontPath = '/usr/share/fonts/truetype/'
#FontName = 'dyspepsia.ttf' # overlapping script font # :)
Height = 2000 # out string height FCunits
Track = 0 # intercharacter spacing
print "testWire.py input String contains ", len(String), " characters."
s = Part.makeWireString(String,FontPath,FontName,Height,Track)
print "returned from makeWireString"
print "testWire.py output contains ", len(s), " WireChars."
for char in s:
for contour in char:
Part.show(contour)
print "testWire ended."