Refactor to move FC object creation from AppPartPy to FT2FC.

This commit is contained in:
WandererFan 2013-03-21 09:14:25 -04:00
parent da8b6d4a81
commit b507f618bb
3 changed files with 88 additions and 102 deletions

View File

@ -118,13 +118,13 @@
#include "ImportStep.h" #include "ImportStep.h"
#include "edgecluster.h" #include "edgecluster.h"
//needed in AppPartPy??? /*//needed in AppPartPy???
#include <ft2build.h> #include <ft2build.h>
#include FT_FREETYPE_H #include FT_FREETYPE_H
#include FT_OUTLINE_H #include FT_OUTLINE_H
#include FT_GLYPH_H #include FT_GLYPH_H
#include FT_TYPES_H #include FT_TYPES_H
//?? //??*/
#include "FT2FC.h" #include "FT2FC.h"
@ -325,20 +325,19 @@ show(PyObject *self, PyObject *args)
static PyObject * makeWireString(PyObject *self, PyObject *args) static PyObject * makeWireString(PyObject *self, PyObject *args)
{ {
PyObject *intext;
const char* dir; const char* dir;
const char* fontfile; const char* fontfile;
float height; float height;
int track = 0; int track = 0;
const char* text; const char* text;
PyObject *intext;
Py_UNICODE *unichars; Py_UNICODE *unichars;
Py_ssize_t pysize; Py_ssize_t pysize;
std::vector <std::vector <TopoDS_Wire> > ret; std::vector <std::vector <TopoShapeWirePy*> > ret;
std::vector<TopoDS_Wire>::iterator iWire; std::vector<TopoShapeWirePy*>::iterator iWire;
std::vector<std::vector<TopoDS_Wire> >:: iterator iChar; std::vector<std::vector<TopoShapeWirePy*> >:: iterator iChar;
PyObject *WireList, *CharList; PyObject *WireList, *CharList;
@ -352,8 +351,7 @@ static PyObject * makeWireString(PyObject *self, PyObject *args)
} }
if (PyString_Check(intext)) { if (PyString_Check(intext)) {
// handle c type string PyObject *p = Base::PyAsUnicodeObject(PyString_AsString(intext));
PyObject *p = Base::PyAsUnicodeObject(PyString_AsString(intext)); //ascii/utf8 to PyUni
if (!p) { if (!p) {
Base::Console().Message("** makeWireString can't convert PyString.\n"); Base::Console().Message("** makeWireString can't convert PyString.\n");
return NULL; return NULL;
@ -362,7 +360,6 @@ static PyObject * makeWireString(PyObject *self, PyObject *args)
unichars = PyUnicode_AS_UNICODE(p); unichars = PyUnicode_AS_UNICODE(p);
} }
else if (PyUnicode_Check(intext)) { else if (PyUnicode_Check(intext)) {
// handle ucs-2/4 input (Py_UNICODE object)
pysize = PyUnicode_GetSize(intext); pysize = PyUnicode_GetSize(intext);
unichars = PyUnicode_AS_UNICODE(intext); unichars = PyUnicode_AS_UNICODE(intext);
} }
@ -375,21 +372,20 @@ static PyObject * makeWireString(PyObject *self, PyObject *args)
ret = FT2FC(unichars,pysize,dir,fontfile,height,track); // get vector of wire chars ret = FT2FC(unichars,pysize,dir,fontfile,height,track); // get vector of wire chars
} }
catch (Standard_DomainError) { // Standard_DomainError is OCC error. catch (Standard_DomainError) { // Standard_DomainError is OCC error.
PyErr_SetString(PyExc_Exception, "makeWireString failed - OCC"); PyErr_SetString(PyExc_Exception, "makeWireString failed - Standard_DomainError");
return NULL; return NULL;
} }
catch (std::runtime_error& e) { // FT2 or FT2FC errors catch (std::runtime_error& e) { // FT2 or FT2FC errors
PyErr_SetString(PyExc_Exception, e.what()); PyErr_SetString(PyExc_Exception, e.what());
return NULL; return NULL;
} }
// if (ret not empty) // if (ret not empty)
CharList = PyList_New(0); CharList = PyList_New(0);
for (iChar = ret.begin(); iChar !=ret.end(); ++iChar) { for (iChar = ret.begin(); iChar !=ret.end(); ++iChar) {
WireList = PyList_New(0); WireList = PyList_New(0);
for (iWire = iChar->begin(); iWire != iChar->end(); ++iWire){ for (iWire = iChar->begin(); iWire != iChar->end(); ++iWire){
PyObject* newobj = new TopoShapeWirePy(new TopoShape (*iWire)); PyList_Append(WireList,*iWire);
PyList_Append(WireList,newobj);
} }
// if (list not empty) // if (list not empty)
PyList_Append(CharList,WireList); PyList_Append(CharList,WireList);

View File

@ -1,11 +1,5 @@
//$$INSERT legal.txt //$$INSERT legal.txt
#include <Python.h> #include "PreCompiled.h"
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_OUTLINE_H
#include FT_GLYPH_H
#include FT_TYPES_H
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
@ -26,19 +20,27 @@
#include <TColStd_Array1OfInteger.hxx> #include <TColStd_Array1OfInteger.hxx>
#include <TColgp_Array1OfPnt.hxx> #include <TColgp_Array1OfPnt.hxx>
#include "TopoShape.h"
#include "TopoShapePy.h"
#include "TopoShapeEdgePy.h"
#include "TopoShapeWirePy.h"
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_OUTLINE_H
#include FT_GLYPH_H
#include FT_TYPES_H
#include "FT2FC.h" #include "FT2FC.h"
typedef unsigned long UNICHAR; // should be = Py_UNICODE?? ul is FT2's codepoint type using namespace Part;
typedef std::vector <std::vector <TopoDS_Wire> > FT2FCRET;
// Private function prototypes typedef unsigned long UNICHAR; // ul is FT2's codepoint type <=> Py_UNICODE2/4
void getFTChar(UNICHAR c); typedef std::vector <std::vector <TopoShapeWirePy*> > FT2FCRET;
std::vector<TopoDS_Wire> getGlyphContours();
FT_Vector getKerning(UNICHAR lc, UNICHAR rc);
TopoDS_Wire edgesToWire(std::vector<TopoDS_Edge> Edges);
struct FTDC_Ctx { // FT Decomp Context for 1 char // FT Decomp Context for 1 char
std::vector<TopoDS_Wire> TWires; struct FTDC_Ctx {
std::vector<TopoShapeWirePy*> TWires;
std::vector<TopoDS_Edge> Edges; std::vector<TopoDS_Edge> Edges;
UNICHAR currchar; UNICHAR currchar;
int penpos; int penpos;
@ -46,9 +48,15 @@ struct FTDC_Ctx { // FT Decomp Context for 1 char
FT_Vector LastVert; FT_Vector LastVert;
}; };
// Made a TopoDS_Wire from a list of TopoDS_Edges // Private function prototypes
TopoDS_Wire edgesToWire(std::vector<TopoDS_Edge> Edges) { //void getFTChar(UNICHAR c);
TopoDS_Wire result; std::vector<TopoShapeWirePy*> getGlyphContours();
FT_Vector getKerning(UNICHAR lc, UNICHAR rc);
TopoShapeWirePy* edgesToWire(std::vector<TopoDS_Edge> Edges);
// Make a TopoDS_Wire from a list of TopoDS_Edges
TopoShapeWirePy* edgesToWire(std::vector<TopoDS_Edge> Edges) {
TopoDS_Wire occwire;
std::vector<TopoDS_Edge>::iterator iEdge; std::vector<TopoDS_Edge>::iterator iEdge;
// if Edges.empty() ???? // if Edges.empty() ????
BRepBuilderAPI_MakeWire mkWire; BRepBuilderAPI_MakeWire mkWire;
@ -56,8 +64,10 @@ TopoDS_Wire edgesToWire(std::vector<TopoDS_Edge> Edges) {
mkWire.Add(*iEdge); mkWire.Add(*iEdge);
} }
// if(mkWire.Done()) ??? // if(mkWire.Done()) ???
result = mkWire.Wire(); occwire = mkWire.Wire();
return(result); TopoShapeWirePy* newwire = new TopoShapeWirePy(new TopoShape (occwire));
return(newwire);
} }
// FT Decompose callbacks and data defns // FT Decompose callbacks and data defns
@ -65,13 +75,13 @@ TopoDS_Wire edgesToWire(std::vector<TopoDS_Edge> Edges) {
// p points to the context where we remember what happened previously (last point, etc) // p points to the context where we remember what happened previously (last point, etc)
static int move_cb(const FT_Vector* pt, void* p) { static int move_cb(const FT_Vector* pt, void* p) {
FTDC_Ctx* dc = (FTDC_Ctx*) p; FTDC_Ctx* dc = (FTDC_Ctx*) p;
if (!dc->Edges.empty()){ // empty on first contour. (or messed up font) if (!dc->Edges.empty()){
TopoDS_Wire newwire; TopoShapeWirePy* newwire;
newwire = edgesToWire(dc->Edges); newwire = edgesToWire(dc->Edges);
dc->TWires.push_back(newwire); dc->TWires.push_back(newwire);
dc->Edges.clear(); dc->Edges.clear();
} }
dc->LastVert.x = pt->x + dc->penpos; // move along baseline dc->LastVert.x = pt->x + dc->penpos;
dc->LastVert.y = pt->y; dc->LastVert.y = pt->y;
return 0; return 0;
} }
@ -80,7 +90,7 @@ static int move_cb(const FT_Vector* pt, void* p) {
static int line_cb(const FT_Vector* pt, void* p) { static int line_cb(const FT_Vector* pt, void* p) {
FTDC_Ctx* dc = (FTDC_Ctx*) p; FTDC_Ctx* dc = (FTDC_Ctx*) p;
// convert font coords to FC/OCC coords // convert font coords to FC/OCC coords
float v1x = dc->scalefactor * dc->LastVert.x; // LastVert already moved along baseline float v1x = dc->scalefactor * dc->LastVert.x;
float v1y = dc->scalefactor * dc->LastVert.y; float v1y = dc->scalefactor * dc->LastVert.y;
float v2x = dc->scalefactor * (pt->x + dc->penpos); float v2x = dc->scalefactor * (pt->x + dc->penpos);
float v2y = dc->scalefactor * pt->y; float v2y = dc->scalefactor * pt->y;
@ -111,8 +121,8 @@ static int quad_cb(const FT_Vector* pt0, const FT_Vector* pt1, void* p) {
Poles.SetValue(1, v1); Poles.SetValue(1, v1);
Poles.SetValue(2, c1); Poles.SetValue(2, c1);
Poles.SetValue(3, v2); 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, but don't know when. does makeedge need it forever? or just for creation?
// how to delete a "handle"? // how to delete a "handle"? memory leak?
Handle(Geom_BezierCurve) bcseg = new Geom_BezierCurve(Poles); Handle(Geom_BezierCurve) bcseg = new Geom_BezierCurve(Poles);
BRepBuilderAPI_MakeEdge makeEdge(bcseg, v1, v2); BRepBuilderAPI_MakeEdge makeEdge(bcseg, v1, v2);
TopoDS_Edge edge = makeEdge.Edge(); TopoDS_Edge edge = makeEdge.Edge();
@ -154,29 +164,29 @@ static int cubic_cb(const FT_Vector* pt0, const FT_Vector* pt1, const FT_Vector*
} }
// FT Callbacks structure // FT Callbacks structure
static FT_Outline_Funcs outline_funcs = { static FT_Outline_Funcs FTcbFuncs = {
(FT_Outline_MoveToFunc)move_cb, (FT_Outline_MoveToFunc)move_cb,
(FT_Outline_LineToFunc)line_cb, (FT_Outline_LineToFunc)line_cb,
(FT_Outline_ConicToFunc)quad_cb, (FT_Outline_ConicToFunc)quad_cb,
(FT_Outline_CubicToFunc)cubic_cb, (FT_Outline_CubicToFunc)cubic_cb,
0, 0 // not needed for FC 0, 0 // not needed for FC
}; };
// load glyph outline into FTFont. /*// load glyph outline into FTFont.
void getFTChar(FT_Face FTFont, UNICHAR c) { void getFTChar(FT_Face FTFont, UNICHAR c) {
FT_Error error; FT_Error error;
FT_UInt flags = FT_LOAD_DEFAULT | FT_LOAD_NO_BITMAP; FT_UInt FTLoadFlags = FT_LOAD_DEFAULT | FT_LOAD_NO_BITMAP;
std::stringstream ErrorMsg; std::stringstream ErrorMsg;
error = FT_Load_Char(FTFont, error = FT_Load_Char(FTFont,
c, c,
flags); FTLoadFlags);
if(error) { if(error) {
ErrorMsg << "FT_Load_Char failed: " << error; ErrorMsg << "FT_Load_Char failed: " << error;
throw std::runtime_error(ErrorMsg.str()); throw std::runtime_error(ErrorMsg.str());
} }
return; return;
} }*/
// get kerning values for this char pair // get kerning values for this char pair
//TODO: should check FT_HASKERNING flag? //TODO: should check FT_HASKERNING flag?
@ -197,62 +207,56 @@ FT_Vector getKerning(FT_Face FTFont, UNICHAR lc, UNICHAR rc) {
return(retXY); return(retXY);
} }
// get glyph outline for current char // get glyph outline in wires
std::vector<TopoDS_Wire> getGlyphContours(FT_Face FTFont, UNICHAR currchar, int PenPos, float Scale) { std::vector<TopoShapeWirePy*> getGlyphContours(FT_Face FTFont, UNICHAR currchar, int PenPos, float Scale) {
FT_Error error = 0; FT_Error error = 0;
std::stringstream ErrorMsg; std::stringstream ErrorMsg;
FT_Outline* FTOPointer; // FT_Outline* pFTO = &FTFont->glyph->outline;
FTDC_Ctx ctx; FTDC_Ctx ctx;
FTOPointer = &FTFont->glyph->outline;
// ctx.ConCnt = 0;
// ctx.SegCnt = 0;
ctx.currchar = currchar; ctx.currchar = currchar;
ctx.penpos = PenPos; ctx.penpos = PenPos;
ctx.scalefactor = Scale; ctx.scalefactor = Scale;
ctx.Edges.clear(); // ctx.Edges.clear();
ctx.TWires.clear(); // ctx.TWires.clear();
error = FT_Outline_Decompose(FTOPointer, &outline_funcs, &ctx); error = FT_Outline_Decompose(&FTFont->glyph->outline,
&FTcbFuncs,
&ctx);
if(error) { if(error) {
ErrorMsg << "FT_Decompose failed: " << error; ErrorMsg << "FT_Decompose failed: " << error;
throw std::runtime_error(ErrorMsg.str()); throw std::runtime_error(ErrorMsg.str());
} }
if (!ctx.Edges.empty()){ // make the last twire // make the last twire
TopoDS_Wire newwire; if (!ctx.Edges.empty()){
newwire = edgesToWire(ctx.Edges); ctx.TWires.push_back(edgesToWire(ctx.Edges));
ctx.TWires.push_back(newwire);
} }
return(ctx.TWires); return(ctx.TWires);
} }
// get string's wires (contours) in FC/OCC coords // get string's wires (contours) in FC/OCC coords
FT2FCRET FT2FC(const Py_UNICODE *pustring, FT2FCRET FT2FC(const Py_UNICODE *pustring,
const size_t length, const size_t length,
const char *FontPath, const char *FontPath,
const char *FontName, const char *FontName,
const float stringheight, // in fc coords const float stringheight, // fc coords
const int tracking) { // in fc coords const int tracking) { // fc coords
/*FT2FCRET _FT2FC(const std::vector<UNICHAR> stringvec,
const char * FontPath,
const char * FontName,
const float stringheight, // in fc coords
const int tracking) { // in fc coords*/
FT_Library FTLib; FT_Library FTLib;
FT_Face FTFont; FT_Face FTFont;
FT_Error error; FT_Error error;
FT_Long FaceIndex; FT_Long FaceIndex = 0; // some fonts have multiple faces
FT_Vector kern; FT_Vector kern;
FT_UInt FTLoadFlags = FT_LOAD_DEFAULT | FT_LOAD_NO_BITMAP;
std::string FontSpec; std::string FontSpec;
std::stringstream ErrorMsg; std::stringstream ErrorMsg;
float scalefactor; float scalefactor;
UNICHAR prevchar,currchar; UNICHAR prevchar = 0, currchar = 0;
int cadv,PenPos; int cadv,PenPos = 0;
// size_t i, length;
size_t i; size_t i;
std::vector<TopoDS_Wire> CharWires; std::vector<TopoShapeWirePy*> CharWires;
FT2FCRET Ret; FT2FCRET Ret;
error = FT_Init_FreeType(&FTLib); error = FT_Init_FreeType(&FTLib);
@ -265,9 +269,7 @@ FT2FCRET FT2FC(const Py_UNICODE *pustring,
std::string tmpName = FontName; std::string tmpName = FontName;
FontSpec = tmpPath + tmpName; FontSpec = tmpPath + tmpName;
FaceIndex = 0; // some fonts have multiple faces // FT does not return an error if font file not found?
// NOTE: FT does not return an error if font file not found.
std::ifstream is; std::ifstream is;
is.open (FontSpec.c_str()); is.open (FontSpec.c_str());
if (!is) { if (!is) {
@ -296,15 +298,18 @@ FT2FCRET FT2FC(const Py_UNICODE *pustring,
throw std::runtime_error(ErrorMsg.str()); throw std::runtime_error(ErrorMsg.str());
} }
prevchar = 0;
PenPos = 0;
scalefactor = float(stringheight/FTFont->height); scalefactor = float(stringheight/FTFont->height);
// length = stringvec.size(); for (i=0; i<length; i++) {
// for (i=0;i<length;i++) {
for (i=0;i<length;i++) {
currchar = pustring[i]; currchar = pustring[i];
// currchar = stringvec[i]; // getFTChar(FTFont,currchar);
getFTChar(FTFont,currchar); error = FT_Load_Char(FTFont,
currchar,
FTLoadFlags);
if(error) {
ErrorMsg << "FT_Load_Char failed: " << error;
throw std::runtime_error(ErrorMsg.str());
}
cadv = FTFont->glyph->advance.x; cadv = FTFont->glyph->advance.x;
kern = getKerning(FTFont,prevchar,currchar); kern = getKerning(FTFont,prevchar,currchar);
PenPos += kern.x; PenPos += kern.x;
@ -316,7 +321,7 @@ FT2FCRET FT2FC(const Py_UNICODE *pustring,
// not entirely happy with tracking solution. It's specified in FC units, // 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 // so we have to convert back to font units to use it here. We could
// lose some accuracy. Hard to put it into FT callbacks since tracking // lose some accuracy. Hard to put it into FT callbacks since tracking
// only affects position of chars 2 - n. // only affects position of chars 2 - n. Don't want to loop 2x through wires.
PenPos += cadv + int(tracking/scalefactor); PenPos += cadv + int(tracking/scalefactor);
prevchar = currchar; prevchar = currchar;
} }
@ -330,17 +335,3 @@ FT2FCRET FT2FC(const Py_UNICODE *pustring,
return(Ret); return(Ret);
} }
/*FT2FCRET FT2FCpu(const Py_UNICODE *pustring,
const size_t length,
const char *FontPath,
const char *FontName,
const float stringheight, // in fc coords
const int tracking) { // in fc coords
size_t i;
std::vector<UNICHAR> stringvec(length, 0);
for (i=0;i<length;i++)
stringvec[i] = pustring[i];
return (_FT2FC(stringvec,FontPath,FontName,stringheight,tracking));
}
*/

View File

@ -1,9 +1,8 @@
// Public header for FT2FC.cpp // Public header for FT2FC.cpp
#ifndef FT2FC_H #ifndef FT2FC_H
#define FT2FC_H #define FT2FC_H
// public function // public function
std::vector <std::vector <TopoDS_Wire> > FT2FC(const Py_UNICODE *unichars, std::vector <std::vector <Part::TopoShapeWirePy*> > FT2FC(const Py_UNICODE *unichars,
const size_t length, const size_t length,
const char *FontPath, const char *FontPath,
const char *FontName, const char *FontName,