Ubuntu 18.04.2 LTS (bionic)
clang version 6.0.0-1ubuntu2
$ sudo apt install libfreetype6 libfreetype6-dev $ sudo apt install libpng16-16 libpng-dev
2.8.1-2ubuntu2
, although at the time of writing, the latest version of FreeType-2.10.1
, it also works.(1.6.34-1ubuntu0.18.04.2)
main.c
in my case) #include <stdio.h> int main() { printf("Hello, world\n"); return 0; }
$ clang -Wall -Werror -o main main.c $ ./main Hello, world
#include
) for FreeType, run: $ pkg-config --cflags freetype2 -I/usr/include/freetype2 -I/usr/include/libpng16
-I/usr/include/freetype2 -I/usr/include/libpng16
contains the compilation flags necessary to enable FreeType in the C program. #include <stdio.h> #include <freetype2/ft2build.h> #include FT_FREETYPE_H int main() { printf("Hello, world\n"); return 0; }
$ clang -I/usr/include/freetype2 \ -I/usr/include/libpng16 \ -Wall -Werror \ -o main \ main.c $ ./main Hello, world
main()
initialize FreeType with FT_Init_FreeType(&ft)
and check for errors (FreeType functions return 0 if successful). FT_Library ft; FT_Error err = FT_Init_FreeType(&ft); if (err != 0) { printf("Failed to initialize FreeType\n"); exit(EXIT_FAILURE); }
FT_Int major, minor, patch; FT_Library_Version(ft, &major, &minor, &patch); printf("FreeType's version is %d.%d.%d\n", major, minor, patch);
/tmp/main-d41304.o: In function `main': main.c:(.text+0x14): undefined reference to `FT_Init_FreeType' main.c:(.text+0x54): undefined reference to `FT_Library_Version' clang: error: linker command failed with exit code 1 (use -v to see invocation)
-lfreetype
. $ clang -I/usr/include/freetype2 \ -I/usr/include/libpng16 \ -Wall -Werror \ -o main \ -lfreetype \ main.c $ ./main FreeType's version is 2.8.1
FT_Face face; err = FT_New_Face(ft, "./UbuntuMono.ttf", 0, &face); if (err != 0) { printf("Failed to load face\n"); exit(EXIT_FAILURE); }
err = FT_Set_Pixel_Sizes(face, 0, 32); if (err != 0) { printf("Failed to set pixel size\n"); exit(EXIT_FAILURE); }
char
says, and a glyph is an image that is somehow associated with that character. This relationship is rather complicated because char can correspond to several glyphs: i.e. accents. A glyph can correspond to many characters: that is, ligatures, where -> is represented as a single image.FT_Get_Char_Index
. As you can understand, this involves matching characters and glyphs only one to one. In a future article in this series, we will solve the problem using the HarfBuzz library. FT_UInt glyph_index = FT_Get_Char_Index(face, 'a');
FT_Int32 load_flags = FT_LOAD_DEFAULT; err = FT_Load_Glyph(face, glyph_index, load_flags); if (err != 0) { printf("Failed to load glyph\n"); exit(EXIT_FAILURE); }
face->glyph
. FT_Int32 render_flags = FT_RENDER_MODE_NORMAL; err = FT_Render_Glyph(face->glyph, render_flags); if (err != 0) { printf("Failed to render the glyph\n"); exit(EXIT_FAILURE); }
face->glyph->bitmap.buffer
, where it is presented as an array of unsigned char values, so its values range from 0 to 255.column * row_width + row
, as in bitmap.buffer[i * face->glyph->bitmap.pitch + j]
.bitmap.width
in a loop and bitmap.pitch
, because the length of each line of pixels is equal to bitmap.width
, but the “width” of the buffer is bitmap.pitch
. for (size_t i = 0; i < face->glyph->bitmap.rows; i++) { for (size_t j = 0; j < face->glyph->bitmap.width; j++) { unsigned char pixel_brightness = face->glyph->bitmap.buffer[i * face->glyph->bitmap.pitch + j]; if (pixel_brightness > 169) { printf("*"); } else if (pixel_brightness > 84) { printf("."); } else { printf(" "); } } printf("\n"); }
$ clang -I/usr/include/freetype2 \ -I/usr/include/libpng16 \ -Wall -Werror \ -o main \ -lfreetype \ main.c && ./main FreeType's version is 2.8.1 .*****. .********. .********* . ***. *** *** .******** *********** .**. *** *** *** *** *** ***. *** .*********** *********** .*******..
Source: https://habr.com/ru/post/461497/
All Articles