Reputation: 345
If I move the FreeType header files into the FontRasterization.hpp
header file I get fatal error C1083: Cannot open include file: 'ft2build.h': No such file or directory
why ?
I have to do a weird workaround involving void*
to store FreeType objects in my header file and cast them in my source file.
I just want to use FreeType normally, FreeType's headers location, lib location, freetype.lib and freetype.dll have been specified.
By the way all of this is a static library with precompiled headers.
As requested in the comments full source code: FontRasterization.hpp
#pragma once
#include "FileIO.hpp"
#include "TextureAtlas.hpp"
namespace nui {
class CharacterAtlas;
struct Character {
uint32_t unicode;
int32_t bitmap_left;
int32_t bitmap_top;
int32_t hori_bearing_X;
int32_t hori_bearing_Y;
int32_t advance_X;
int32_t advance_Y;
AtlasRegion* zone;
uint32_t vertex_start_idx; // location in the vertex buffer where to find vertices
uint32_t index_start_idx; // location in the index buffer where to find indexes
};
struct FontSize {
uint32_t size;
uint32_t ascender;
uint32_t descender;
uint32_t line_spacing;
std::vector<Character> chars;
};
class Font {
public:
CharacterAtlas* atlas;
std::vector<uint8_t> ttf_file;
void* face_ft;
// cache
std::vector<uint8_t> bitmap;
// props
std::string family_name;
std::string style_name;
std::vector<FontSize> sizes;
public:
ErrStack addSize(uint32_t size);
};
class CharacterAtlas {
public:
TextureAtlas atlas;
void* free_type_ft = nullptr;
std::vector<Font> fonts;
public:
ErrStack addFont(FilePath& path, Font*& r_font);
ErrStack addFont(FilePath& path, std::vector<uint32_t>& sizes, Font*& r_font);
};
}
FontRasterization.cpp
#include "pch.h"
// Header
#include "FontRasterization.hpp"
// FreeType
#include <ft2build.h>
#include <freetype\freetype.h>
using namespace nui;
ErrStack Font::addSize(uint32_t size)
{
ErrStack err_stack;
FT_Error err;
uint32_t first_unicode = '!';
uint32_t last_unicode = '~';
uint32_t unicode_count = last_unicode - first_unicode + 1;
FT_Face face = (FT_Face)face_ft;
err = FT_Set_Pixel_Sizes(face, 0, size);
if (err) {
return ErrStack(code_location, "failed to set font face size");
}
FontSize& font_size = sizes.emplace_back();
font_size.size = size;
FT_Size_Metrics& metrics = face->size->metrics;
font_size.ascender = metrics.ascender / 64;
font_size.descender = (-metrics.descender) / 64;
font_size.line_spacing = metrics.height / 64;
font_size.chars.resize(unicode_count + 1);
uint32_t i = 0;
for (uint32_t unicode = first_unicode; unicode <= last_unicode; unicode++) {
uint32_t glyph_idx = FT_Get_Char_Index(face, unicode);
err = FT_Load_Glyph(face, glyph_idx, FT_LOAD_RENDER);
if (err) {
return ErrStack(code_location, "failed to load and render glyph");
}
auto& glyph = face->glyph;
Character& chara = font_size.chars[i++];
chara.unicode = unicode;
chara.bitmap_left = glyph->bitmap_left;
chara.bitmap_top = glyph->bitmap_top;
chara.hori_bearing_X = glyph->metrics.horiBearingX / 64;
chara.hori_bearing_Y = glyph->metrics.horiBearingY / 64;
chara.advance_X = glyph->advance.x / 64;
chara.advance_Y = glyph->advance.y / 64;
bitmap.resize(glyph->bitmap.width * glyph->bitmap.rows);
std::memcpy(bitmap.data(), glyph->bitmap.buffer, bitmap.size());
TextureAtlas& tex_atlas = atlas->atlas;
if (!tex_atlas.addBitmap(bitmap, glyph->bitmap.width, glyph->bitmap.rows, chara.zone)) {
return ErrStack(code_location, "failed to find space to store character in atlas");
}
}
// White Space
{
uint32_t space_unicode = 0x0020;
uint32_t glyph_idx = FT_Get_Char_Index(face, space_unicode);
err = FT_Load_Glyph(face, glyph_idx, FT_LOAD_RENDER);
if (err) {
return ErrStack(code_location, "failed to load and render glyph");
}
auto& glyph = face->glyph;
Character& chara = font_size.chars[i];
chara.unicode = space_unicode;
chara.bitmap_left = glyph->bitmap_left;
chara.bitmap_top = glyph->bitmap_top;
chara.hori_bearing_X = glyph->metrics.horiBearingX / 64;
chara.hori_bearing_Y = glyph->metrics.horiBearingY / 64;
chara.advance_X = glyph->advance.x / 64;
chara.advance_Y = glyph->advance.y / 64;
chara.zone = nullptr;
}
return err_stack;
}
ErrStack CharacterAtlas::addFont(FilePath& path, Font*& r_font)
{
ErrStack err_stack;
FT_Error err;
FT_Library free_type = (FT_Library)free_type_ft;
if (free_type == nullptr) {
err = FT_Init_FreeType(&free_type);
if (err) {
return ErrStack(code_location, "failed to initialize FreeType library");
}
}
Font& font = this->fonts.emplace_back();
font.atlas = this;
checkErrStack(path.read(font.ttf_file), "failed to read font file");
FT_Face face = (FT_Face)font.face_ft;
err = FT_New_Memory_Face(free_type, font.ttf_file.data(), (uint32_t)font.ttf_file.size(), 0, &face);
if (err) {
return ErrStack(code_location, "failed to create font face");
}
font.family_name = face->family_name;
font.style_name = face->style_name;
r_font = &font;
return err_stack;
}
ErrStack CharacterAtlas::addFont(FilePath& path, std::vector<uint32_t>& sizes, Font*& r_font)
{
ErrStack err_stack;
FT_Library free_type = (FT_Library)free_type_ft;
FT_Error err = FT_Init_FreeType(&free_type);
if (err) {
return ErrStack(code_location, "failed to initialize FreeType library");
}
FT_Face face;
std::vector<uint8_t> ttf_file;
checkErrStack(path.read(ttf_file), "failed to read font file");
err = FT_New_Memory_Face(free_type, ttf_file.data(), (uint32_t)ttf_file.size(), 0, &face);
if (err) {
return ErrStack(code_location, "failed to create font face");
}
Font& font = this->fonts.emplace_back();
font.family_name = face->family_name;
font.style_name = face->style_name;
uint32_t first_unicode = '!';
uint32_t last_unicode = '~';
uint32_t unicode_count = last_unicode - first_unicode + 1;
std::vector<uint8_t> bitmap;
for (auto size : sizes) {
err = FT_Set_Pixel_Sizes(face, 0, size);
if (err) {
return ErrStack(code_location, "failed to set font face size");
}
FontSize& font_size = font.sizes.emplace_back();
font_size.size = size;
font_size.ascender = face->size->metrics.ascender / 64;
font_size.descender = (-face->size->metrics.descender) / 64;
font_size.line_spacing = face->size->metrics.height / 64;
font_size.chars.resize(unicode_count + 1);
if (!atlas.colors.size()) {
atlas.create(2048);
}
uint32_t i = 0;
for (uint32_t unicode = first_unicode; unicode <= last_unicode; unicode++) {
uint32_t glyph_idx = FT_Get_Char_Index(face, unicode);
err = FT_Load_Glyph(face, glyph_idx, FT_LOAD_RENDER);
if (err) {
return ErrStack(code_location, "failed to load and render glyph");
}
auto& glyph = face->glyph;
Character& chara = font_size.chars[i++];
chara.unicode = unicode;
chara.bitmap_left = glyph->bitmap_left;
chara.bitmap_top = glyph->bitmap_top;
chara.hori_bearing_X = glyph->metrics.horiBearingX / 64;
chara.hori_bearing_Y = glyph->metrics.horiBearingY / 64;
chara.advance_X = glyph->advance.x / 64;
chara.advance_Y = glyph->advance.y / 64;
bitmap.resize(glyph->bitmap.width * glyph->bitmap.rows);
std::memcpy(bitmap.data(), glyph->bitmap.buffer, bitmap.size());
if (!atlas.addBitmap(bitmap, glyph->bitmap.width, glyph->bitmap.rows, chara.zone)) {
return ErrStack(code_location, "failed to find space to store character in atlas");
}
}
// White Space
uint32_t space_unicode = 0x0020;
uint32_t glyph_idx = FT_Get_Char_Index(face, space_unicode);
err = FT_Load_Glyph(face, glyph_idx, FT_LOAD_RENDER);
if (err) {
return ErrStack(code_location, "failed to load and render glyph");
}
auto& glyph = face->glyph;
Character& chara = font_size.chars[i];
chara.unicode = space_unicode;
chara.bitmap_left = glyph->bitmap_left;
chara.bitmap_top = glyph->bitmap_top;
chara.hori_bearing_X = glyph->metrics.horiBearingX / 64;
chara.hori_bearing_Y = glyph->metrics.horiBearingY / 64;
chara.advance_X = glyph->advance.x / 64;
chara.advance_Y = glyph->advance.y / 64;
chara.zone = nullptr;
}
r_font = &font;
return ErrStack();
}
The methods ErrStack Font::addSize(uint32_t size)
and ErrStack CharacterAtlas::addFont(FilePath& path, Font*& r_font)
are not used in the code base yet.
The Precompiled Header File
#pragma once
// Windows
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
// Windows undefs
#undef min
#undef max
// DirectX 11
#include <d3d11_4.h>
#include <dxgi1_6.h>
#pragma comment(lib, "D3D11.lib")
#pragma comment(lib, "DXGI.lib")
// Standard
#include <string>
#include <vector>
#include <array>
#include <list>
#include <variant>
#include <cmath>
// GLM
#include <glm\vec2.hpp>
#include <glm\vec4.hpp>
// Mine
#include "ErrorStack.hpp"
Upvotes: 0
Views: 973
Reputation: 582
According to C1083 error, please check if your ‘Additional Include Directories’ is correct in Project Properties\C/C++\General. And if the directory is a relative path, please try to switch it to an absolute path.
Besides, you could also try to use double quotes instead of angle brackets to include it.
#include "ft2build.h"
#include "freetype\freetype.h"
If they don’t work, you could try to use vcpkg tool, which could help you manage C++ libraries for visual studio automatically. And I have tested it on my side, the ‘ft2build.h’ would be included into header file normally.
The steps:
Upvotes: 2