Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C/C++: Программирование под Unix/Linux > Создать изображение с нужным шрифтом


Автор: killobyte 1.9.2010, 22:30
Доброго времени суток. Каким образом можно создать изображение с заданным текстом, написанным заданным шрифтом?
желательно на голом x11

Автор: Amp 2.9.2010, 10:49
FreeType

Автор: proc_maker 14.9.2010, 11:19
10 минут гугления дали примерно следующий пример. В результате имеем массив пикселов каждой буквы - далее пакуйте в нужный формат и все:

Код

#include <stdio.h>
#include <freetype/freetype.h>
#include <freetype/ftglyph.h>

inline int next_p2(const int a)
{
   int rval = 1;
   while (rval < a)
   {
      rval <<= 1;
   }

   return rval;
}

int main(int, char*[])
{
   const char* const pString = "Some text";
   const char* const pFontFileName = "/usr/share/fonts/TTF/DejaVuSans.ttf";
   const int h = 14;

   FT_Library freeTypeLibrary;

   do
   {
      // Init the library
      if (0 != FT_Init_FreeType(&freeTypeLibrary))
      {
         printf("Unable to initialize FreeType library\n");
         break;
      }

      do
      {
         // Create a font
         FT_Face font;
         if (0 != FT_New_Face(freeTypeLibrary, pFontFileName, 0, &font))
         {
            printf("Unable to load font\n");
            break;
         }

         FT_Set_Char_Size(font, h << 6, h << 6, 96, 96);

         for (const char* c = pString; 0 != c and '\0' != *c; ++c)
         {
            if (0 != FT_Load_Glyph(font, FT_Get_Char_Index(font, *c), FT_LOAD_DEFAULT ))
            {
               printf("Unable to load glyph\n");
               break;
            }

            FT_Glyph glyph;
            if (0 != FT_Get_Glyph(font->glyph, &glyph))
            {
               printf("Unable to get glyph\n");
               break;
            }

            // Convert the glyph to raster
            FT_Glyph_To_Bitmap(&glyph, FT_RENDER_MODE_NORMAL, 0, 1);

            const FT_BitmapGlyph bitmapGlyph = (FT_BitmapGlyph)glyph;
            const FT_Bitmap& bitmap = bitmapGlyph->bitmap;

            const int width = next_p2(bitmap.width);
            const int height = next_p2(bitmap.rows);

            const unsigned int buffSize = 2 * width * height;
            unsigned char* pBuffer = new unsigned char[buffSize];
            memset(pBuffer, 0, buffSize);

            for (int j = 0; j <height; ++j)
            {
               for(int i = 0; i < width; ++i)
               {
                  pBuffer[2 * (i + j * width)] = pBuffer[2 * (i + j * width) + 1] =
                     (i >= bitmap.width || j >= bitmap.rows) ? 0 : bitmap.buffer[i + bitmap.width * j];
               }
            }

            // TODO: Pack the image to somewhere

            // Release the buffer
            delete [] pBuffer;

            // Destroy glyph
            FT_Done_Glyph(glyph);
         }

         // Destroy font
         FT_Done_Face(font);
      } while (0);

      // Release the library
      if (0 != FT_Done_FreeType(freeTypeLibrary))
      {
         printf("Unable to release FreeType library\n");
         break;
      }
   } while(0);

   return 0;
}


Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)