文件列表
untitled3.pro
QT -= gui
CONFIG += c++11 console
CONFIG -= app_bundle
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
LIBS += -L$$PWD/freetype2/lib -lfreetype
INCLUDEPATH += $$PWD/freetype2/include
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
# 执行copy命令
copyConfigFiles.commands = cp $$PWD/汉仪魏碑简.ttf $$OUT_PWD/汉仪魏碑简.ttf
QMAKE_EXTRA_TARGETS += copyConfigFiles
POST_TARGETDEPS += copyConfigFiles
main.cpp
/* example1.c */
/* */
/* This small program shows how to print a rotated string with the */
/* FreeType 2 library. */
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <QDebug>
#include "freetype2/ft2build.h"
#include FT_FREETYPE_H
#define WIDTH 80
#define HEIGHT 80
#include <ft2build.h>
#include <arpa/inet.h>
#include FT_FREETYPE_H
#define FONT_SIZE 16
typedef unsigned short WORD;
#define printf(fmt,...) (void)0
int print_font(char* buf, int point_size)
{
unsigned char mask = 0x80;
int i = 0;
QString str;
while(i < point_size)
{
int index = 0;
mask = 0x80;
for(index = 0; index < 8; index++) {
bool flag = mask & buf[i];
if(flag) {
str += "O ";
} else {
str += " ";
}
mask >>= 1;
}
if((i + 1) % (FONT_SIZE/8) == 0) {
qDebug() << str;
str.clear();
}
i++;
}
}
static int g_index = 0;
int set_bit_value(int value, char font_buf[32], int font_size)
{
g_index %= (font_size*font_size);
int byte_index = g_index / 8;
int bit_index = g_index % 8;
bit_index = 7 - bit_index;
font_buf[byte_index] &= ~(1 << bit_index);
font_buf[byte_index] |= (value << bit_index);
g_index++;
}
static FT_Library library;
static FT_Face face;
void print_face_info(FT_Face face)
{
int n ;
FT_CharMap map;
char* p;
for(n = 0; n < face->num_charmaps; ++n)
{
map = face->charmaps[n];
int encode = htonl(map->encoding);
p = (char*)&encode;
printf("%d %d %d\n", map->platform_id, map->encoding_id, map->encoding);
printf("encode : %c %c %c %c\n", p[0], p[1], p[2], p[3]);
}
}
int freeType_init(const char* ttf_path, int font_size)
{
int error;
error = FT_Init_FreeType(&library);
if (error)
{
printf("can not init free type library!\n");
return 0;
}
error = FT_New_Face(library, ttf_path, 0, &face);
if (error)
{
printf("create new face falied!\n");
return 0;
}
print_face_info(face);
printf("faces num : %ld , glyphs num : %ld\n", face->num_faces, face->num_glyphs);
error = FT_Set_Pixel_Sizes(face, 0, font_size);
if (error)
{
printf("set font size error!\n");
return 0;
}
#if 1
error = FT_Select_Charmap(face, ft_encoding_unicode);
if (error)
{
printf("select charmap error!\n");
return 0;
}
#endif
return 0;
}
int freeType_Uninit()
{
int error;
error = FT_Done_FreeType(library);
if (error)
{
printf("can not Uninit free type library!\n");
return 0;
}
return 0;
}
int get_font_by_index(const char* ttf_path, int font_size, int char_index, char font_buf[32])
{
int error;
int i, j, k, counter;
unsigned char temp;
FT_UInt glyph_index;
#if 0
error = FT_Init_FreeType(&library);
if (error)
{
printf("can not init free type library!\n");
return 0;
}
error = FT_New_Face(library, ttf_path, 0, &face);
if (error)
{
printf("create new face falied!\n");
return 0;
}
#endif
//printf("file family name %s\n", face->family_name);
//printf("file style name %s\n", face->style_name);
//printf("number of char %d\n", face->num_glyphs);
//printf("number of fixed bitmap %d\n", face->num_fixed_sizes);
//printf("Char size %d\n", face->size);
/**********************************************************/
/************************做索引转换**********************************/
/**********************************************************/
#if 1
printf("char_index: 0x%x\n", char_index);
glyph_index = FT_Get_Char_Index( face, char_index );
// printf("glyph_index : %d\n", glyph_index);
qDebug() << "glyph_index : " << glyph_index;
if(!glyph_index) return 0;
/**********************************************************/
error = FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
if (error) {
printf("Load char error!\n");
return 0;
}
if (face->glyph->format != FT_GLYPH_FORMAT_BITMAP) {
error = FT_Render_Glyph(face->glyph, FT_RENDER_MODE_MONO);
if (error) {
printf("render char failed!\n");
return 0;
}
}
#else
error = FT_Load_Char( face, char_index, FT_LOAD_RENDER);
if (error)
{
printf("render char failed!\n");
return 0;
}
#endif
// printf("rows %d, width %d\n", face->glyph->bitmap.rows, face->glyph->bitmap.width);
//printf("pitch %d\n", face->glyph->bitmap.pitch);
// printf("bit_map_left %d bit_map_top %d\n", face->glyph->bitmap_left,
// face->glyph->bitmap_top);
// printf("width %d height %d\n", face->glyph->metrics.width, face->glyph->metrics.height);
// printf("%d %d %d\n", face->glyph->metrics.horiBearingX, face->glyph->metrics.horiBearingY,
// face->glyph->metrics.horiAdvance);
FT_Bitmap* bitmap = &face->glyph->bitmap;
printf("r:%d, w:%d, p: %d, s: %d\n",
bitmap->rows,
bitmap->width,
bitmap->pitch,
bitmap->num_grays);
for (j = 0; j < (font_size * 26) / 32 - face->glyph->bitmap_top; j++) {
QString str;
for (i = 0; i < font_size; i++) {
str += "_";
set_bit_value(0, font_buf, font_size);
}
qDebug() << str;
}
for (; j < face->glyph->bitmap.rows + (font_size * 26) / 32 - face->glyph->bitmap_top; j++)
{
if(j > 20000) {
break;
}
QString str;
for (i = 1; i <= face->glyph->bitmap_left; i++)
{
str += "_";
set_bit_value(0, font_buf, font_size);
}
for (k = 0; k < face->glyph->bitmap.pitch; k++)
{
temp = face->glyph->bitmap.buffer[face->glyph->bitmap.pitch*(j + face->glyph->bitmap_top - (font_size * 26) / 32) + k];
for (counter = 0; counter < 8; counter++)
{
if (temp & 0x80) {
str += "*";
set_bit_value(1, font_buf, font_size);
} else {
str += "_";
set_bit_value(0, font_buf, font_size);
}
temp <<= 1;
i++;
if (i > font_size) {
break;
}
}
}
for (; i <= font_size; i++)
{
// printf("|");
set_bit_value(0, font_buf, font_size);
}
qDebug() << str;
}
for (; j < font_size; j++) {
QString str;
for (i = 0; i < font_size; i++) {
str += "_";
set_bit_value(0, font_buf, font_size);
}
qDebug() << str;
}
char tmpfont_buf[32];
memcpy(tmpfont_buf, font_buf, sizeof(tmpfont_buf));
for (; j < font_size; j++) {
for (i = 0; i < font_size; i++) {
printf("3");
set_bit_value(0, font_buf, font_size);
}
printf("\n");
}
return 1;
}
int main(int argc, char* argv[])
{
qDebug() << "int main(int argc, char* argv[])";
char font_buf[FONT_SIZE * FONT_SIZE / 8] = {'\0'};
char* unicode_font_name = "unicode_16x16.bin";
FILE* file = fopen(unicode_font_name, "w+");
freeType_init("./汉仪魏碑简.ttf", FONT_SIZE);
int i;
wchar_t *wszString = L"版";
WORD word;
int wszStringLen = wcslen(wszString);
printf("wszStringLen: %d\n",wszStringLen);
memcpy(&word, wszString, 2);
for(i = 0; i < 1; ++i) {
if(get_font_by_index(argv[1], FONT_SIZE, word, font_buf)) {
print_font(font_buf, sizeof(font_buf));
fwrite(font_buf, 1, sizeof(font_buf), file);
} else {
memset(font_buf, 0, sizeof(font_buf));
fwrite(font_buf, 1, sizeof(font_buf), file);
}
g_index = 0;
}
freeType_Uninit();
return 0;
}
运行效果