From c723cc99d1451793cf0dd396993bfc477681d69c Mon Sep 17 00:00:00 2001 From: WandererFan Date: Fri, 22 Mar 2013 10:35:34 -0400 Subject: [PATCH] Tidy PyList creation code. Add test driver. --- src/Mod/Part/App/AppPartPy.cpp | 25 +------------- src/Mod/Part/App/FT2FC.cpp | 21 +++--------- src/Mod/Part/App/FT2FC.h | 1 - src/Mod/Test/testmakeWireString.py | 52 ++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 41 deletions(-) create mode 100644 src/Mod/Test/testmakeWireString.py diff --git a/src/Mod/Part/App/AppPartPy.cpp b/src/Mod/Part/App/AppPartPy.cpp index 3f387fcbf..6d103281a 100644 --- a/src/Mod/Part/App/AppPartPy.cpp +++ b/src/Mod/Part/App/AppPartPy.cpp @@ -118,14 +118,6 @@ #include "ImportStep.h" #include "edgecluster.h" -/*//needed in AppPartPy??? -#include -#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 > ret; -// std::vector::iterator iWire; -// std::vector >:: 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"}, diff --git a/src/Mod/Part/App/FT2FC.cpp b/src/Mod/Part/App/FT2FC.cpp index 3fb22ee72..014ac98a0 100644 --- a/src/Mod/Part/App/FT2FC.cpp +++ b/src/Mod/Part/App/FT2FC.cpp @@ -36,18 +36,14 @@ using namespace Part; typedef unsigned long UNICHAR; // ul is FT2's codepoint type <=> Py_UNICODE2/4 -//typedef std::vector > FT2FCRET; -typedef PyObject* FT2FCRET; // Private function prototypes -//std::vector 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 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, @@ -64,10 +60,8 @@ PyObject* FT2FC(const Py_UNICODE *pustring, std::stringstream ErrorMsg; float scalefactor; UNICHAR prevchar = 0, currchar = 0; - int cadv,PenPos = 0, PyErr; + int cadv, PenPos = 0, PyErr; size_t i; -// std::vector 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; iglyph->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 getGlyphContours(FT_Face FTFont, UNICHAR currchar, int PenPos, float Scale) { diff --git a/src/Mod/Part/App/FT2FC.h b/src/Mod/Part/App/FT2FC.h index 2d2eadac0..cff39e9c0 100644 --- a/src/Mod/Part/App/FT2FC.h +++ b/src/Mod/Part/App/FT2FC.h @@ -2,7 +2,6 @@ #ifndef FT2FC_H #define FT2FC_H // public function -//std::vector > FT2FC(const Py_UNICODE *unichars, PyObject* FT2FC(const Py_UNICODE *unichars, const size_t length, const char *FontPath, diff --git a/src/Mod/Test/testmakeWireString.py b/src/Mod/Test/testmakeWireString.py new file mode 100644 index 000000000..a75a58357 --- /dev/null +++ b/src/Mod/Test/testmakeWireString.py @@ -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."