Hello !
How can I draw text with a cmap font ?
I am sending you my current source code :-)
Regards, Łukasz Płuska
#pragma once
#include <math.h>
void
draw_bitmap( FT_Bitmap* bitmap,
FT_Int x,
FT_Int y,
RGBQUAD *image,
int image_cx,
int image_cy,
DRGBQUAD *TextColor, RGBQUAD *ClipImage )
{
FT_Int i, j, p, q;
FT_Int x_max = x + bitmap->width;
FT_Int y_max = y + bitmap->rows;
if ( TextColor->Red < 0 ) TextColor->Red = 0;
else if ( TextColor->Red > 255.0f ) TextColor->Red = 255.0f;
if ( TextColor->Green < 0 ) TextColor->Green = 0;
else if ( TextColor->Green > 255.0f ) TextColor->Green = 255.0f;
if ( TextColor->Red < 0 ) TextColor->Blue = 0;
else if ( TextColor->Blue > 255.0f ) TextColor->Blue = 255.0f;
RGBQUAD Jasne;
Jasne.rgbRed = ( TextColor->Red * 255.0f );
Jasne.rgbGreen = ( TextColor->Green * 255.0f );
Jasne.rgbBlue = ( TextColor->Blue * 255.0f );
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 >= image_cx || j >= image_cy )
continue;
BYTE src = bitmap->buffer[q * bitmap->width + p];
DWORD dest = (image_cy-j-1)*image_cx+i;
if ( ClipImage && ClipImage[dest].rgbRed != 0xFF ) continue;
if ( src == 0xFF ) {
image[dest].rgbRed = Jasne.rgbRed ;
} else if ( src == 0x00 ) {
image[dest].rgbRed = image[dest].rgbRed;
} else {
image[dest].rgbRed = ( ( ( src ) * ( Jasne.rgbRed ) ) +
( ( image[dest].rgbRed ) * ( 255 - src ) ) ) / 255;
}
if ( src == 0xFF ) {
image[dest].rgbGreen = Jasne.rgbGreen ;
} else if ( src == 0x00 ) {
image[dest].rgbGreen = image[dest].rgbGreen;
} else {
image[dest].rgbGreen = ( ( ( src ) * ( Jasne.rgbGreen )
) + ( ( image[dest].rgbGreen ) * ( 255 - src ) ) ) / 255;
}
if ( src == 0xFF ) {
image[dest].rgbBlue = Jasne.rgbBlue ;
} else if ( src == 0x00 ) {
image[dest].rgbBlue = image[dest].rgbBlue;
} else {
image[dest].rgbBlue = ( ( ( src ) * ( Jasne.rgbBlue ) )
+ ( ( image[dest].rgbBlue ) * ( 255 - src ) ) ) / 255;
}
}
}
}
bool PDF_DrawText2( PDF_FILE *pdf, FT_Library library, PDF_TEXT *text, DWORD
TextLenght, double *text_matrix, double ratio, char *FontName,
FT_UInt pixel_width, FT_UInt pixel_height,
BYTE RenderingMode, RGBQUAD *ClipImage ) {
long n;
for ( n = 0; n < g_FontsNR; n++ ) {
if ( lstrcmpiA( FontName, g_Fonts[n].FontName ) == 0 ) break;
}
if ( n >= g_FontsNR ) n = 4;
FT_Face face = g_Fonts[n].face;
if ( face ) {
FT_Error error;
error = FT_Set_Char_Size(
face, /* handle to face object */
pixel_width*64.0f, /* char_width in 1/64th of points */
pixel_height*64.0f, /* char_height in 1/64th of points */
72, /* horizontal device resolution */
72 ); /* vertical device resolution */
//WCHAR *example_text = text;
int n,dest_x = 0;
FT_Vector pen;
FT_Matrix matrix;
FT_GlyphSlot slot;
slot = face->glyph;
//double angle = 0;//( 25.0 / 360 ) * 3.14159 * 2;
// use 25 degrees
int target_height =
pdf->Pages[pdf->Page.PageNR].Height;//pdf->Pages.Height;
DRGBQUAD TextColorDef;
TextColorDef.Red = 0;
TextColorDef.Blue = 0;
TextColorDef.Green = 0;
matrix.xx = (FT_Fixed)( text_matrix[0] * ((double)0x10000L) );
matrix.xy = (FT_Fixed)( text_matrix[3] * ((double)0x10000L) );
matrix.yx = (FT_Fixed)( text_matrix[1] * ((double)0x10000L) );
matrix.yy = (FT_Fixed)( text_matrix[4] * ((double)0x10000L) );
//FT_Set_Transform( face, &matrix, &pen );
pen.x = (FT_Pos)( text_matrix[6] * ratio * 64.0f );
pen.y = (FT_Pos)( text_matrix[7] * ratio * 64.0f );
FT_Int major, minor, patch;
FT_Library_Version( library, &major, &minor, &patch);
FT_ULong charcode;
FT_UInt gindex;
if ( error == 0 ) for ( n = 0; n < TextLenght; n++ ) {
FT_UInt glyph_index;
FT_Set_Transform( face, &matrix, &pen );
if ( text[n].Index ) {
// text[n].Index = cmap index
// text[n].Letter = unicode code
glyph_index =
text[n].Index;//FT_Face_GetCharVariantIndex;
} else {
glyph_index = FT_Get_Char_Index( face,
text[n].Letter );
}
// load glyph image into the slot (erase previous one)
error = FT_Load_Glyph( face, glyph_index,
FT_LOAD_RENDER );
if ( error ) continue; // ignore errors
// convert to an anti-aliased bitmap
error = FT_Render_Glyph( face->glyph,
FT_RENDER_MODE_LIGHT );
if ( error ) continue;
//error = FT_Load_Char( face, example_text[n],
FT_LOAD_RENDER );
draw_bitmap( &slot->bitmap, slot->bitmap_left,
target_height - slot->bitmap_top,
pdf->Pages[pdf->Page.PageNR].Image,
pdf->Pages[pdf->Page.PageNR].Width,
pdf->Pages[pdf->Page.PageNR].Height,
&text[n].color, ClipImage );
pen.x += slot->advance.x;
pen.y += slot->advance.y;
}
//FT_Done_Face( face );
return true;
}
return false;
}
_______________________________________________
Freetype-devel mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/freetype-devel