freetype
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Freetype] using a Stroker to make a glyph border pixmap


From: George Ogata
Subject: [Freetype] using a Stroker to make a glyph border pixmap
Date: Sat, 10 Jul 2004 06:50:35 +1000
User-agent: Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Reasonable Discussion, linux)

Hi,

if my understanding is correct, the Stroker functions should enable me
to obtain a border of a glyph.  I've only found a couple of stroker
examples, and documentation seems rather scant.  It seems using
FT_Glyph_Stroke is the simplest way, but I can't understand why this
simple test program doesn't print out the border; it just prints the
normal glyph instead.  Can someone tell me what I'm doing wrong?

Thanks.

//////////////////////////////////////////////////

#include <stdlib.h>
#include <stdio.h>

#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_STROKER_H

/* Error checking facility */
const char *error_string(FT_Error error) {
#undef __FTERRORS_H__
#define FT_ERRORDEF( e, v, s )  { e, s },
#define FT_ERROR_START_LIST     {
#define FT_ERROR_END_LIST       { 0, 0 } };
  static const struct ErrorRecord {
    int          err_code;
    const char*  err_msg;
  } ft_errors[] = 
#include FT_ERRORS_H

  const struct ErrorRecord *p;
  for (p = ft_errors; p->err_msg; ++p)
    if (p->err_code == error)
      return p->err_msg;
  return "??";
}
#define CHECK(func, arglist)                                            \
  do {                                                                  \
    if ((error = func arglist)) {                                       \
      fprintf(stderr, "%d: %s failed (%d): %s\n", __LINE__, #func, error, 
error_string(error)); \
      done(EXIT_FAILURE);                                               \
    }                                                                   \
  } while (0)

/* FT resources */
FT_Library lib;
FT_Face face;
FT_Glyph glyph;
FT_Stroker stroker;

/* Free everything and exit. */
void done(int status) {
  if (stroker)
    FT_Stroker_Done(stroker);
  if (glyph)
    FT_Done_Glyph(glyph);
  if (face)
    FT_Done_Face(face);
  if (lib)
    FT_Done_FreeType(lib);
  exit(status);
}

void print_bitmap(FT_Bitmap *map) {
  int s, r, c;
  s = map->pitch > 0 ? 0 : (map->rows - 1) * -map->pitch;
  printf("pitch=%d size=%dx%d\n", map->pitch, map->rows, map->width);
  for (r = 0; r < map->rows; ++r, s += map->pitch) {
    for (c = 0; c < map->width; ++c) {
      if (map->buffer[s+c])
        printf("#");
      else
        printf(" ");
    }
    printf("\n");
  }
}

int main(int argc, char **argv) {
  FT_Error error;
  FT_Vector origin = {0, 0};

  if (argc != 2) {
    printf("usage: %s FONTFILE", argv[0]);
    return EXIT_FAILURE;
  }

  // init library and load char
  CHECK( FT_Init_FreeType, (&lib) );
  CHECK( FT_New_Face, (lib, argv[1], 0, &face) );
  CHECK( FT_Set_Pixel_Sizes, (face, 40, 0) );
  CHECK( FT_Load_Char, (face, 'A', FT_LOAD_DEFAULT) );
  CHECK( FT_Get_Glyph, (face->glyph, &glyph) );

  // make stroker
  CHECK( FT_Stroker_New, (face->memory, &stroker) );
  FT_Stroker_Set, (stroker,
                   32,
                   FT_STROKER_LINECAP_ROUND,
                   FT_STROKER_LINEJOIN_ROUND,
                   0);

  // make bitmap
  CHECK( FT_Glyph_Stroke, (&glyph, stroker, 1) );
  CHECK( FT_Glyph_To_Bitmap, (&glyph, FT_RENDER_MODE_NORMAL, &origin, 1) );

  print_bitmap(&((FT_BitmapGlyph)glyph)->bitmap);
  done(EXIT_SUCCESS);
  return 0;  // not reached
}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]