参考的代码有:
https://blog.csdn.net/quantum7/article/details/79762714
https://blog.csdn.net/quantum7/article/details/107279477
具体代码:
#include <stdio.h> #include <stdlib.h> #include <wchar.h> #include <ft2build.h> #include <freetype/freetype.h> #include FT_FREETYPE_H #define FONT_FILE "/home/uos/font/simsun.ttc" static void dumpBuffer(char* pBuffer, int w, int h) { int i=0; int j=0; printf("w=%d, h=%d\n", w, h); for (i=0; i<h; i++) { printf("|"); for (j=0; j<w; j++) { printf(((pBuffer[i * w + j]) == 0 ) ? " " : "1"); } printf("|\n"); } } static void dumpFontBmp(FT_Bitmap* pBmp) { int w = pBmp->width; int h = pBmp->rows; dumpBuffer(pBmp->buffer, w, h); } static void rotate_90(FT_Bitmap* pBmp) { int w = pBmp->width; int h = pBmp->rows; int i=0; char* pRotated; int size; int offset=0; size = w * h; pRotated = (char*)malloc(size); memcpy(pRotated, pBmp->buffer, size); int startPos = (h - 1)*w; i = 0; for (int x = 0; x < w; x++) { int offset = startPos; for (int y = h - 1; y >= 0; y--) { pRotated[i] = pBmp->buffer[offset + x]; i++; offset -= w; } } dumpBuffer(pRotated, h, w); } static void rotate_270(FT_Bitmap* pBmp) { int w = pBmp->width; int h = pBmp->rows; int i=0; char* pRotated; int size; int offset=0; size = w * h; pRotated = (char*)malloc(size); memcpy(pRotated, pBmp->buffer, size); i = 0; for (int x = w-1; x >= 0; x--) { int offset = 0; for (int y = 0; y < h; y++) { pRotated[i] = pBmp->buffer[offset + x]; i++; offset += w; } } dumpBuffer(pRotated, h, w); } int main() { FT_Library m_pFTLib; FT_Face m_pFTFace; wchar_t chinese_char = L'泰'; FT_Error result = FT_Init_FreeType(&m_pFTLib); if(FT_New_Face(m_pFTLib, FONT_FILE, 0, &m_pFTFace)) { printf("FT_New_Face error!\n"); return -1; } //FT_ENCODING_GB2312, FT_ENCODING_UNICODE FT_Select_Charmap(m_pFTFace, FT_ENCODING_UNICODE); FT_Set_Char_Size(m_pFTFace, 0, 12<<6, 200, 200); result = FT_Load_Glyph(m_pFTFace, FT_Get_Char_Index(m_pFTFace, chinese_char), FT_LOAD_DEFAULT); // 第二个参数为渲染模式 result = FT_Render_Glyph(m_pFTFace->glyph, FT_RENDER_MODE_NORMAL); printf("result=%d\n", result); dumpFontBmp(&(m_pFTFace->glyph->bitmap)); rotate_90( &(m_pFTFace->glyph->bitmap)); rotate_270(&(m_pFTFace->glyph->bitmap)); return 0; }
原始字体:
| 111 | | 1111 | | 111 11 | | 111 1111 | | 111111111111111111111111111 | | 11111 111 | | 111 11 | | 111 1111 | | 11111111111111111111111 | | 1111 111 | | 111 1 | | 111 1111 | |111111111111111111111111111111 | | 1111 1111 11 | | 111 11 111 | | 1111 1111 111 | | 1111 1111 111 | | 1111 111 1111 | | 1111 111 111111 | | 1111111 111 111111111 | | 1111 111111111111111111111 | | 1111 111111111 1111111| | 1111 11 1111 1111 | |111 111111111 11 | |1 11111111111111 | | 111111 111 111111 | | 11111 111 11111 | | 1111 1111111 111 | | 11 111111 11 | | 111 |
旋转90度:
| 11 1 | | 11 11 | | 111 11 11 | | 111 11 11 | | 11 111 11 1 11 | | 1111 111 1 11 11 | | 1111 1111 1 11 11 | | 111 1111 1 11 1 | | 111 11111 1 11 1 | | 1 11 111111111 1 1 | | 11 1111111 111111 1 1 | | 11 111111 11111111 1 | |111 11 11 11111111111 | |1111111111111111 1 11111111111| |1111111111111111 1 111111111| | 11111111111111 1 1 1 111| | 11111 11 1 1 1 1 | | 11 11 1 1 1 | | 111 111 111 1 1 | | 111 111 1111 1 1 | | 111 1111111 1 1 1 | | 1111 111111 1 1 1 | | 1111 11111 1 11 1 | | 1111 1111 1 111 1 | | 1111 1 111 11 | | 111 11 11 111 | | 1111 11 1 111 | | 1111 111 11 | | 1111 11 1 | | 111 1 | | 1 |
旋转-90:
| 1 | | 1 111 | | 1 11 1111 | | 11 111 1111 | | 111 1 11 1111 | | 111 11 11 111 | | 11 111 1 1111 | | 1 111 1 1111 1111 | | 1 11 1 11111 1111 | | 1 1 1 111111 1111 | | 1 1 1 1111111 111 | | 1 1 1111 111 111 | | 1 1 111 111 111 | | 1 1 1 11 11 | | 1 1 1 1 11 11111 | |111 1 1 1 11111111111111 | |111111111 1 1111111111111111| |11111111111 1 1111111111111111| | 11111111111 11 11 111| | 1 11111111 111111 11 | | 1 1 111111 1111111 11 | | 1 1 111111111 11 1 | | 1 11 1 11111 111 | | 1 11 1 1111 111 | | 11 11 1 1111 1111 | | 11 11 1 111 1111 | | 11 1 11 111 11 | | 11 11 111 | | 11 11 111 | | 11 11 | | 1 11 |