Hi developers,
I am turning to you with my problem to use colors as described here.
https://gitlab.freedesktop.org/freetype/freetype/-/issues/1099
To invoke this issue I use a function* FT_Palette_Set_Foreground_Color* in
my source code (in the attachment). The tail of make log file is also in
the attachment.
The aim of my work is to show a text on an OLED display. This works but it
looks ugly. Half of a text is not visible and there is a black background
that I can't change.
https://stackoverflow.com/questions/69222145/freetype-set-size-and-color-of-bounding-box
Is there something I have missed?
I will appreciate help
Lenka Polaskova
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_COLOR_H
#include "log.h"
#include <fcntl.h>
#include <string.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <linux/kd.h>
#include <assert.h>
#define WIDTH 640
#define HEIGHT 480
FT_Library ft;
FT_Face face;
unsigned char image[WIDTH][HEIGHT];
void show_image( char *fb );
void show_image( char *fb )
{
int i, j;
for ( i = 0; i < WIDTH; i++ )
{
for ( j = 0; j < HEIGHT; j++ )
fb[i+2432*j] = image[i][j];
}
}
static void draw_bitmap(char *fb, FT_Bitmap* bitmap,
FT_Int x,
FT_Int y)
{
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[i][j] |= bitmap->buffer[q * bitmap->width + p];
}
}
}
int TypewriterSetForeground(uint32_t color) {
FT_Color fc;
fc.alpha = color & 0xFF000000 >> 24;
fc.red = color & 0xFF0000 >> 16;
fc.green = color & 0xFF00 >> 8;
fc.blue = color & 0xFF;
FT_Palette_Set_Foreground_Color(face, fc);
}
int TypewriterInit(void) {
if (FT_Init_FreeType(&ft))
{
LOG_E("Typewriter init failed");
return -1;
}
if (FT_New_Face(ft, "/root/projects/GPU/fonts/arial.ttf", 0, &face))
{
LOG_E("Typewriter failed to load font");
return -1;
}
}
int TypewriterRenderText(char *fb, char *text) {
int n = 0;
int target_height = 600;
int pen_x = 300;
int pen_y = 200;
assert(text!=NULL);
FT_ULong character = 'x';
FT_UInt glyph_index;
FT_Vector pen;
FT_UInt gi = FT_Get_Char_Index (face, character);
/* char width, height, horz_resolution, vert_resolution, 50pt at 100dpi */
if(FT_Set_Char_Size(face, 100 * 64, 0,
100, 100 )) {
LOG_E("Set char size failed");
return -1;
}
for (n = 0; n < strlen(text); n++) {
if (text[n] == 'g') {
FT_UInt gi = FT_Get_Char_Index (face, text[n]);
FT_Load_Glyph (face, gi, FT_LOAD_DEFAULT);
face->glyph->metrics.horiBearingY =
face->glyph->metrics.horiBearingY + 5;
}
FT_Load_Char( face, text[n], FT_LOAD_RENDER );
draw_bitmap(fb, &face->glyph->bitmap,
pen_x + face->glyph->bitmap_left,
pen_y + face->glyph->bitmap_top );
/* increment pen position */
pen_x += face->glyph->advance.x >> 6;
printf("Char %c is printed \n", text[n]);
}
show_image(fb);
}
cc -Wall -g -DCO_SINGLE_THREAD -D_LARGE_FILE_SOURCE=1 -ICANopenLinux
-ICANopenLinux/CANopenNode -IGPU -ISrc -I/../../usr/local/include/freetype2
-Wl,--stack,67108864 -c Src/main.c -o Src/main.o
cc -g -lrt -lpng -pthread -lfreetype CANopenLinux/CO_driver.o
CANopenLinux/CO_error.o CANopenLinux/CO_epoll_interface.o
CANopenLinux/CO_storageLinux.o CANopenLinux/CANopenNode/301/CO_ODinterface.o
CANopenLinux/CANopenNode/301/CO_NMT_Heartbeat.o
CANopenLinux/CANopenNode/301/CO_HBconsumer.o
CANopenLinux/CANopenNode/301/CO_Emergency.o
CANopenLinux/CANopenNode/301/CO_SDOserver.o
CANopenLinux/CANopenNode/301/CO_SDOclient.o
CANopenLinux/CANopenNode/301/CO_TIME.o CANopenLinux/CANopenNode/301/CO_SYNC.o
CANopenLinux/CANopenNode/301/CO_PDO.o
CANopenLinux/CANopenNode/301/crc16-ccitt.o
CANopenLinux/CANopenNode/301/CO_fifo.o CANopenLinux/CANopenNode/303/CO_LEDs.o
CANopenLinux/CANopenNode/304/CO_GFC.o CANopenLinux/CANopenNode/304/CO_SRDO.o
CANopenLinux/CANopenNode/305/CO_LSSslave.o
CANopenLinux/CANopenNode/305/CO_LSSmaster.o
CANopenLinux/CANopenNode/309/CO_gateway_ascii.o
CANopenLinux/CANopenNode/storage/CO_storage.o
CANopenLinux/CANopenNode/extra/CO_trace.o CANopenLinux/CANopenNode/OD/OD.o
CANopenLinux/CANopenNode/CANopen.o Src/task_default.o Src/task_can.o
Src/task_gpu.o GPU/png_decoder.o GPU/drawer.o GPU/typewriter.o Src/main.o -o
canopend
/usr/bin/ld: GPU/typewriter.o: in function `TypewriterSetForeground':
/root/projects/GPU/typewriter.c:69: undefined reference to
`FT_Palette_Set_Foreground_Color'
collect2: error: ld returned 1 exit status
make: *** [Makefile:93: canopend] Error 1