Added bounding box functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
diff --git a/src/FTFont.cpp b/src/FTFont.cpp
index ad1fb1d..d7daf51 100755
--- a/src/FTFont.cpp
+++ b/src/FTFont.cpp
@@ -1,6 +1,7 @@
#include "FTFace.h"
#include "FTFont.h"
#include "FTGlyphContainer.h"
+#include "FTGlyph.h" // for FTBbox
#include "FTGL.h"
@@ -104,6 +105,79 @@ int FTFont::Descender() const
}
+void FTFont::BBox( const char* string,
+ int& llx, int& lly, int& llz, int& urx, int& ury, int& urz)
+{
+ const unsigned char* c = (unsigned char*)string; // This is ugly, what is the c++ way?
+ llx = lly = llz = urx = ury = urz = 0;
+ FTBBox bbox;
+
+ while( *c)
+ {
+ if( !glyphList->Glyph( static_cast<unsigned int>(*c)))
+ {
+ unsigned int g = face.CharIndex( static_cast<unsigned int>(*c));
+ glyphList->Add( MakeGlyph( g), g);
+ }
+
+ bbox = glyphList->BBox( *c);
+
+ // Lower extent
+ lly = lly < bbox.y1 ? lly: bbox.y1;
+ // Upper extent
+ ury = ury > bbox.y2 ? ury: bbox.y2;
+ // Depth
+ urz = urz < bbox.z2 ? urz: bbox.z2;
+
+ // Width
+ urx += glyphList->Advance( *c, *(c + 1));
+ ++c;
+ }
+
+ //Final adjustments
+ llx = glyphList->BBox( *string).x1;
+ urx -= glyphList->Advance( *(c - 1), 0);
+ urx += bbox.x2;
+
+}
+
+void FTFont::BBox( const wchar_t* string,
+ int& llx, int& lly, int& llz, int& urx, int& ury, int& urz)
+{
+ const wchar_t* c = string; // wchar_t IS unsigned?
+ llx = lly = llz = urx = ury = urz = 0;
+ FTBBox bbox;
+
+ while( *c)
+ {
+ if( !glyphList->Glyph( static_cast<unsigned int>(*c)))
+ {
+ unsigned int g = face.CharIndex( static_cast<unsigned int>(*c));
+ glyphList->Add( MakeGlyph( g), g);
+ }
+
+ bbox = glyphList->BBox( *c);
+
+ // Lower extent
+ lly = lly < bbox.y1 ? lly: bbox.y1;
+ // Upper extent
+ ury = ury > bbox.y2 ? ury: bbox.y2;
+ // Depth
+ urz = urz < bbox.z2 ? urz: bbox.z2;
+
+ // Width
+ urx += glyphList->Advance( *c, *(c + 1));
+ ++c;
+ }
+
+ //Final adjustments
+ llx = glyphList->BBox( *string).x1;
+ urx -= glyphList->Advance( *(c - 1), 0);
+ urx += bbox.x2;
+
+}
+
+
float FTFont::Advance( const wchar_t* string)
{
const wchar_t* c = string; // wchar_t IS unsigned?