+/* find bbox of a string */
+static void compute_string_bbox(gfx_string string) {
+ unsigned int n;
+ FT_BBox bbox;
+
+ bbox.xMin = bbox.yMin = 32000;
+ bbox.xMax = bbox.yMax = -32000;
+ for ( n = 0; n < string->num_glyphs; n++ ) {
+ FT_BBox glyph_bbox;
+ FT_Glyph_Get_CBox( string->glyphs[n].image, ft_glyph_bbox_gridfit,
+ &glyph_bbox );
+ if (glyph_bbox.xMin < bbox.xMin) {
+ bbox.xMin = glyph_bbox.xMin;
+ }
+ if (glyph_bbox.yMin < bbox.yMin) {
+ bbox.yMin = glyph_bbox.yMin;
+ }
+ if (glyph_bbox.xMax > bbox.xMax) {
+ bbox.xMax = glyph_bbox.xMax;
+ }
+ if (glyph_bbox.yMax > bbox.yMax) {
+ bbox.yMax = glyph_bbox.yMax;
+ }
+ }
+ if ( bbox.xMin > bbox.xMax ) {
+ bbox.xMin = 0;
+ bbox.yMin = 0;
+ bbox.xMax = 0;
+ bbox.yMax = 0;
+ }
+ string->bbox.xMin = bbox.xMin;
+ string->bbox.xMax = bbox.xMax;
+ string->bbox.yMin = bbox.yMin;
+ string->bbox.yMax = bbox.yMax;
+}
+
+/* create a free type glyph string */
+gfx_string gfx_string_create(FT_Face face,const char *text,
+ int rotation, double tabwidth)
+{
+
+ FT_GlyphSlot slot = face->glyph; /* a small shortcut */
+ FT_Bool use_kerning;
+ FT_UInt previous;
+ FT_Vector ft_pen;
+
+ gfx_string string;
+ gfx_char glyph; /* current glyph in table */
+ unsigned int n;
+ int error;
+ int gottab;
+
+ ft_pen.x = 0; /* start at (0,0) !! */
+ ft_pen.y = 0;
+
+ string = (gfx_string) malloc (sizeof(struct gfx_string_s));
+ string->width = 0;
+ string->height = 0;
+ string->count = strlen (text);
+ string->glyphs = (gfx_char) calloc (string->count,sizeof(struct gfx_char_s));
+ string->num_glyphs = 0;
+ string->transform.xx = (FT_Fixed)( cos(M_PI*(rotation)/180.0)*0x10000);
+ string->transform.xy = (FT_Fixed)(-sin(M_PI*(rotation)/180.0)*0x10000);
+ string->transform.yx = (FT_Fixed)( sin(M_PI*(rotation)/180.0)*0x10000);
+ string->transform.yy = (FT_Fixed)( cos(M_PI*(rotation)/180.0)*0x10000);
+
+ use_kerning = FT_HAS_KERNING(face);
+ previous = 0;
+ glyph = string->glyphs;
+ for (n=0; n<string->count; n++, glyph++) {
+ FT_Vector vec;
+ /* handle the tabs ...
+ have a witespace glyph inserted, but set its width such that the distance
+ of the new right edge is x times tabwidth from 0,0 where x is an integer. */
+ char letter = text[n];
+ gottab = 0;
+ if (letter == '\\' && n+1 < string->count && text[n+1] == 't'){
+ /* we have a tab here so skip the backslash and
+ set t to ' ' so that we get a white space */
+ gottab = 1;
+ n++;
+ letter = ' ';
+ }
+ if (letter == '\t'){
+ letter = ' ';
+ gottab = 1 ;
+ }
+ /* initialize each struct gfx_char_s */
+ glyph->index = 0;
+ glyph->pos.x = 0;
+ glyph->pos.y = 0;
+ glyph->image = NULL;
+
+ glyph->index = FT_Get_Char_Index( face, letter );
+
+ /* compute glyph origin */
+ if ( use_kerning && previous && glyph->index ) {
+ FT_Vector kerning;
+ FT_Get_Kerning (face, previous, glyph->index,
+ ft_kerning_default, &kerning);
+ ft_pen.x += kerning.x;
+ ft_pen.y += kerning.y;
+ }
+
+ /* load the glyph image (in its native format) */
+ /* for now, we take a monochrome glyph bitmap */
+ error = FT_Load_Glyph (face, glyph->index, FT_LOAD_DEFAULT);
+ if (error) {
+ fprintf (stderr, "couldn't load glyph: %c\n", letter);
+ continue;
+ }
+ error = FT_Get_Glyph (slot, &glyph->image);
+ if (error) {
+ fprintf (stderr, "couldn't get glyph from slot: %c\n", letter);
+ continue;
+ }
+ /* if we are in tabbing mode, we replace the tab with a space and shift the position
+ of the space so that its left edge is where the tab was supposed to land us */
+ if (gottab){
+ /* we are in gridfitting mode so the calculations happen in 1/64 pixles */
+ ft_pen.x = tabwidth*64.0 * (float)(1 + (long)(ft_pen.x / (tabwidth * 64.0))) - slot->advance.x;
+ }
+ /* store current pen position */
+ glyph->pos.x = ft_pen.x;
+ glyph->pos.y = ft_pen.y;
+
+
+ ft_pen.x += slot->advance.x;
+ ft_pen.y += slot->advance.y;
+
+ /* rotate glyph */
+ vec = glyph->pos;
+ FT_Vector_Transform (&vec, &string->transform);
+ error = FT_Glyph_Transform (glyph->image, &string->transform, &vec);
+ if (error) {
+ fprintf (stderr, "couldn't transform glyph\n");
+ continue;
+ }
+
+ /* convert to a bitmap - destroy native image */
+ error = FT_Glyph_To_Bitmap (&glyph->image, FT_RENDER_MODE_NORMAL, 0, 1);
+ if (error) {
+ fprintf (stderr, "couldn't convert glyph to bitmap\n");
+ continue;
+ }
+
+ /* increment number of glyphs */
+ previous = glyph->index;
+ string->num_glyphs++;
+ }
+/* printf ("number of glyphs = %d\n", string->num_glyphs);*/
+ compute_string_bbox( string );
+ /* the last character was a tab */
+ if (gottab) {
+ string->width = ft_pen.x;
+ } else {
+ string->width = string->bbox.xMax - string->bbox.xMin;
+ }
+ string->height = string->bbox.yMax - string->bbox.yMin;
+ return string;
+}
+
+