From 6465e367ebd13adfe2013ab47e0656b8a624d07e Mon Sep 17 00:00:00 2001 From: WandererFan Date: Wed, 20 Mar 2013 19:12:53 -0400 Subject: [PATCH] Combine UCS2 & UTF-8 logic. --- src/Mod/Part/App/AppPartPy.cpp | 76 +++++++++++++++++----------------- src/Mod/Part/App/FT2FC.cpp | 18 ++++---- src/Mod/Part/App/FT2FC.h | 4 +- 3 files changed, 50 insertions(+), 48 deletions(-) diff --git a/src/Mod/Part/App/AppPartPy.cpp b/src/Mod/Part/App/AppPartPy.cpp index 3ae4dc95e..6fafa1937 100644 --- a/src/Mod/Part/App/AppPartPy.cpp +++ b/src/Mod/Part/App/AppPartPy.cpp @@ -118,11 +118,13 @@ #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" @@ -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 > ret; std::vector::iterator iWire; std::vector >:: 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; } diff --git a/src/Mod/Part/App/FT2FC.cpp b/src/Mod/Part/App/FT2FC.cpp index 632d8c4e0..ab0bdbb19 100644 --- a/src/Mod/Part/App/FT2FC.cpp +++ b/src/Mod/Part/App/FT2FC.cpp @@ -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 getGlyphContours(FT_Face FTFont, UNICHAR currchar, int // get string's wires (contours) in FC/OCC coords FT2FCRET _FT2FC(const std::vector 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 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 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; diff --git a/src/Mod/Part/App/FT2FC.h b/src/Mod/Part/App/FT2FC.h index 2d5a325f8..3475d2074 100644 --- a/src/Mod/Part/App/FT2FC.h +++ b/src/Mod/Part/App/FT2FC.h @@ -10,8 +10,8 @@ const int tracking);*/ std::vector > 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