第一篇用QNX SCREEN 渲染文字使用基础字库加上点阵放大实现的,放大后字体会很难看,于是考虑使用FreeTpye矢量字体实现。代码实现如下
1、英文矢量字体渲染
#pragma once
#include "libmessage.h"
#include "bagad_module.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <screen/screen.h>
#include <iostream>
#include <ctype.h>
#include <fcntl.h>
#include <string.h>
#include <img/img.h>
#include <math.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#define WIDTH 1920
#define HEIGHT 720
unsigned char image[HEIGHT][WIDTH];
//渲染
void draw_pix(int x, int y,char *ptr,int stride,int color){
ptr += (stride*y);
ptr[x*4] = ((0xff000000&color)>>24);
ptr[x*4+1] = ((0x00ff0000&color)>>16);
ptr[x*4+2] = ((0x0000ff00&color)>>8);
ptr[x*4+3] = (0x000000ff&color);
}
//获取二维坐标数组
void draw_bitmap( FT_Bitmap* bitmap, FT_Int x, FT_Int y)
{
printf("start draw_bitmap");
FT_Int i, j, p, q;
FT_Int x_max = x + bitmap->width;
FT_Int y_max = y + bitmap->rows;
for ( i = x, p = 0; i < x_max; i++, p++ )
{
for ( j = y, q = 0; j < y_max; j++, q++ )
{
if ( i < 0 || j < 0 ||
i >= WIDTH || j >= HEIGHT )
continue;
image[j][i] |= bitmap->buffer[q * bitmap->width + p];
}
}
}
//filename 字体文件 str 字符串文字 text_size 文字大小
int en_draw(const char * filename ,const char * str, int text_size){
printf("start freetype_draw\n");
printf("filename=%s str=%s text_size=%d \n",filename,str,text_size);
FT_Library library;
FT_Face face;
FT_GlyphSlot slot;
FT_Matrix matrix;
FT_Vector pen;
FT_Error error;
double angle;
int target_height;
int n, num_chars;
num_chars = strlen( str );
angle = ( 0.0 / 360 ) * 3.14159 * 2;
target_height = HEIGHT;
printf("start freetype_draw 1\n");
error = FT_Init_FreeType( &library );
printf("start freetype_draw 2 error=%d \n",error);
error = FT_New_Face( library, filename, 0, &face );
printf("start freetype_draw 3 error=%d \n",error);
FT_Set_Pixel_Sizes(face, text_size, text_size);
slot = face->glyph;
matrix.xx = (FT_Fixed)( cos( angle ) * 0x10000L );
matrix.xy = (FT_Fixed)(-sin( angle ) * 0x10000L );
matrix.yx = (FT_Fixed)( sin( angle ) * 0x10000L );
matrix.yy = (FT_Fixed)( cos( angle ) * 0x10000L