Advertisement
Guest User

FreeType subpixel hinting attempt

a guest
Jun 7th, 2015
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #include <ft2build.h>
  4. #include FT_FREETYPE_H
  5. #include FT_LCD_FILTER_H
  6. #include FT_MODULE_H
  7. #include FT_TRUETYPE_DRIVER_H
  8.  
  9. #include "FileUtility.hpp"
  10.  
  11. int main(int argc, char** argv) {
  12.  
  13.     FT_Library library;
  14.     FT_Error error = FT_Init_FreeType(&library);
  15.     if (error) std::cout << "Freetype init failed!" << std::endl;
  16.  
  17.     FT_UInt interpreter_version = TT_INTERPRETER_VERSION_38;
  18.     error = FT_Property_Set(library, "truetype", "interpreter-version", &interpreter_version);
  19.     if (error) std::cout << "Property setting failed!" << std::endl;
  20.  
  21.     FT_Library_SetLcdFilter(library, FT_LCD_FILTER_DEFAULT);
  22.     if (error) std::cout << "Failed to set LCD filter!" << std::endl;
  23.  
  24.     FT_Face face;
  25.     error = FT_New_Face(library, "NotoSans-Regular.ttf", 0, &face);
  26.     if (error) std::cout << "Font loading failed!" << std::endl;
  27.  
  28.     error = FT_Set_Pixel_Sizes(face, 0, 32);
  29.     if (error) std::cout << "Failed to change font size!" << std::endl;
  30.  
  31.     FT_UInt glyph_index = FT_Get_Char_Index(face, 'T');
  32.  
  33.     error = FT_Load_Glyph(face, glyph_index, FT_LOAD_TARGET_LCD);
  34.     if (error) std::cout << "Glyph load failed!" << std::endl;
  35.  
  36.     error = FT_Render_Glyph(face->glyph, FT_RENDER_MODE_LCD);
  37.     if (error) std::cout << "Glyph render failed!" << std::endl;
  38.  
  39.     // Bitmap exporting
  40.     FT_Bitmap *result = &face->glyph->bitmap;
  41.     char *pixelData = new char[result->width * result->rows];
  42.     for (int i = 0; i < result->width / 3 * result->rows; i++) {
  43.         // Reversing channel order
  44.         pixelData[i * 3    ] = result->buffer[i * 3 + 2];
  45.         pixelData[i * 3 + 1] = result->buffer[i * 3 + 1];
  46.         pixelData[i * 3 + 2] = result->buffer[i * 3];
  47.     }
  48.     FileUtility::createBitmapFile("out.bmp", result->width / 3, result->rows, pixelData);
  49.  
  50.     delete[] pixelData;
  51.     FT_Done_Face(face);
  52.     FT_Done_FreeType(library);
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement