我正在尝试将 FreeType 库与 libpng 一起使用来输出字形的 PNG 图像。我可以创建字形的光栅位图,也可以创建有效的 PNG 文件,但我似乎无法将两者放在一起。问题来自接近尾声的这条线:
png_bytep image = (png_bytep) slot->bitmap;
看来我不能简单地将 FreeType 位图转换为 apng_bytep
并将其传递给 pnglib(这是一厢情愿的想法)。我收到以下错误:
/home/david/Desktop/png.c: In function ‘main’:
/home/david/Desktop/png.c:47:2: error: cannot convert to a pointer type
但是,我不确定如何从这里开始。但这是完整的代码块:
#include <stdlib.h>
#include <stdio.h>
#include <png.h>
#include <ft2build.h>
#include FT_FREETYPE_H
main() {
// Declare FreeType variables
FT_Library library;
FT_Face face;
FT_GlyphSlot slot = face->glyph;
FT_UInt glyph_index;
int pen_x, pen_y, n;
char* file = "/usr/share/fonts/truetype/freefont/FreeMono.ttf";
// Declare PNG variables
png_uint_32 width = 100;
png_uint_32 height = 100;
int bit_depth = 16;
int color_type = PNG_COLOR_TYPE_GRAY;
char* file_name = "/home/david/Desktop/out.png";
png_structp png_ptr;
png_infop info_ptr;
// Render font
FT_New_Face(library, file, 0, &face);
FT_Set_Pixel_Sizes(face, 0, 16);
glyph_index = 30;
FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL);
// Create a PNG file
FILE *fp = fopen(file_name, "wb");
// Create the PNG in memory
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
info_ptr = png_create_info_struct(png_ptr);
png_init_io(png_ptr, fp);
// Write the header
png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, color_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_write_info(png_ptr, info_ptr);
// Write image data
png_bytep image = (png_bytep) slot->bitmap;
png_write_image(png_ptr, &image);
// End write
png_write_end(png_ptr, NULL);
fclose(fp);
}