Combine UCS2 & UTF-8 logic.

This commit is contained in:
WandererFan 2013-03-20 19:12:53 -04:00
parent 9c5608d8d2
commit 6465e367eb
3 changed files with 50 additions and 48 deletions

View File

@ -118,11 +118,13 @@
#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"
@ -325,19 +327,22 @@ static PyObject * makeWireString(PyObject *self, PyObject *args)
{
const char* dir;
const char* fontfile;
const char* text;
float height;
int track = 0;
std::string sdir,sfontfile;
const char* text;
PyObject *intext;
Py_UNICODE *unichars;
Py_ssize_t pysize;
// std::string sdir,sfontfile;
std::vector <std::vector <TopoDS_Wire> > ret;
std::vector<TopoDS_Wire>::iterator iWire;
std::vector<std::vector<TopoDS_Wire> >:: iterator iChar;
PyObject *WireList, *CharList, *intext;
Py_UNICODE *unichars;
Py_ssize_t pysize;
PyObject *WireList, *CharList;
if (!PyArg_ParseTuple(args, "Ossf|i", &intext,
&dir,
@ -348,46 +353,39 @@ static PyObject * makeWireString(PyObject *self, PyObject *args)
return NULL;
}
sdir = dir; // c string to std::string
sfontfile = fontfile;
// sdir = dir; // c string to std::string
// sfontfile = fontfile;
if (PyString_Check(intext)) {
// Base::Console().Message("** makeWireString obj is pystring.\n");
// handle c type string
try {
text = PyString_AsString(intext);
// Base::Console().Message("** makeWireString pystring => text:<%s>\n", text);
PyObject *p = Base::PyAsUnicodeObject(text);
if (!p) {
Base::Console().Message("** makeWireString Base::PyAsUnicode returns NULL.\n");
return NULL;
PyObject *p = Base::PyAsUnicodeObject(PyString_AsString(intext)); //ascii/utf8 to PyUni
if (!p) {
Base::Console().Message("** makeWireString can't convert PyString.\n");
return NULL;
}
pysize = PyUnicode_GetSize(p);
unichars = PyUnicode_AS_UNICODE(p);
// Base::Console().Message("** makeWireString pystring len: %d\n", pysize);
ret = FT2FCpu(unichars,pysize,sdir,sfontfile,height,track); // get vector of wire chars
pysize = PyUnicode_GetSize(p);
unichars = PyUnicode_AS_UNICODE(p);
}
catch (Standard_DomainError) {
PyErr_SetString(PyExc_Exception, "makeWireString failed 1");
return NULL;
}
}
else if (PyUnicode_Check(intext)) {
// Base::Console().Message("** makeWireString obj is unicode.\n");
// handle ucs-2/4 version (Py_UNICODE object)
try {
Py_ssize_t pysize = PyUnicode_GetSize(intext);
unichars = PyUnicode_AS_UNICODE(intext);
// Base::Console().Message("** makeWireString unicode len: %d\n", pysize);
ret = FT2FCpu(unichars,pysize,sdir,sfontfile,height,track); // get vector of wire chars
}
catch (Standard_DomainError) {
PyErr_SetString(PyExc_Exception, "makeWireString failed 2");
return NULL;
}
// handle ucs-2/4 input (Py_UNICODE object)
pysize = PyUnicode_GetSize(intext);
// Base::Console().Message("** makeWireString intext is Unicode len: '%d'.\n", pysize);
unichars = PyUnicode_AS_UNICODE(intext);
}
else {
Base::Console().Message("** makeWireString bad string.\n");
Base::Console().Message("** makeWireString bad text parameter.\n");
return NULL;
}
try {
ret = FT2FCpu(unichars,pysize,dir,fontfile,height,track); // get vector of wire chars
}
catch (Standard_DomainError) { // Standard_DomainError is OCC error.
PyErr_SetString(PyExc_Exception, "makeWireString failed - OCC");
return NULL;
}
catch (std::runtime_error& e) { // FT2 or FT2FC errors
PyErr_SetString(PyExc_Exception, e.what());
return NULL;
}

View File

@ -187,7 +187,7 @@ void getFTChar(FT_Face FTFont, UNICHAR c) {
}
// get kerning values for this char pair
//TODO: should check FT_HASKERNING flag
//TODO: should check FT_HASKERNING flag?
FT_Vector getKerning(FT_Face FTFont, UNICHAR lc, UNICHAR rc) {
FT_Vector retXY;
FT_Error error;
@ -237,8 +237,8 @@ std::vector<TopoDS_Wire> getGlyphContours(FT_Face FTFont, UNICHAR currchar, int
// get string's wires (contours) in FC/OCC coords
FT2FCRET _FT2FC(const std::vector<UNICHAR> stringvec,
const std::string FontPath,
const std::string FontName,
const char * FontPath,
const char * FontName,
const float stringheight, // in fc coords
const int tracking) { // in fc coords
FT_Library FTLib;
@ -263,7 +263,11 @@ FT2FCRET _FT2FC(const std::vector<UNICHAR> stringvec,
ErrorMsg << "FT_Init_FreeType failed: " << error;
throw std::runtime_error(ErrorMsg.str());
}
FontSpec = FontPath + FontName;
std::string tmpPath = FontPath; // can't concat const char*
std::string tmpName = FontName;
FontSpec = tmpPath + tmpName;
FaceIndex = 0; // some fonts have multiple faces
// NOTE: FT blows up if font file not found. It does not return an error!!!
@ -280,7 +284,7 @@ FT2FCRET _FT2FC(const std::vector<UNICHAR> stringvec,
ErrorMsg << "FT_New_Face failed: " << error;
throw std::runtime_error(ErrorMsg.str());
}
//TODO: check that FTFont is scalable.
//TODO: check that FTFont is scalable?
// FT2 blows up if char size is not set to some non-zero value.
// This sets size to 48 point. Magic.
@ -341,8 +345,8 @@ FT2FCRET FT2FCc(const char *cstring,
FT2FCRET FT2FCpu(const Py_UNICODE *pustring,
const size_t length,
const std::string FontPath,
const std::string FontName,
const char *FontPath,
const char *FontName,
const float stringheight, // in fc coords
const int tracking) { // in fc coords
size_t i;

View File

@ -10,8 +10,8 @@
const int tracking);*/
std::vector <std::vector <TopoDS_Wire> > FT2FCpu(const Py_UNICODE *unichars,
const size_t length,
const std::string FontPath,
const std::string FontName,
const char *FontPath,
const char *FontName,
const float stringheight,
const int tracking);
#endif // FT2FC_H