无法在头文件中包含标头,但可以包含在 cpp 中

Unable to include header in header file, but can include in cpp

本文关键字:但可以 包含 cpp 包含标 文件      更新时间:2024-04-29

如果我将FreeType头文件移动到FontRasterization.hpp头文件中,我会得到fatal error C1083: Cannot open include file: 'ft2build.h': No such file or directory为什么?

我必须做一个奇怪的解决方法,涉及void*将 FreeType 对象存储在我的头文件中并将它们强制转换为我的源文件中。

我只想正常使用 FreeType,FreeType 的标头位置、lib 位置、freetype.lib 和 freetype.dll 都已指定。

顺便说一下,所有这些都是一个带有预编译标头的静态库。

按照注释中的要求,完整源代码: 字体光栅化.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);
};
}

字体光栅化.cpp

#include "pch.h"
// Header
#include "FontRasterization.hpp"
// FreeType
#include <ft2build.h>
#include <freetypefreetype.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();
}

代码库中尚未使用方法ErrStack Font::addSize(uint32_t size)ErrStack CharacterAtlas::addFont(FilePath& path, Font*& r_font)

预编译头文件

#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 <glmvec2.hpp>
#include <glmvec4.hpp>
// Mine
#include "ErrorStack.hpp"

根据 C1083 错误,请检查项目属性\C/C++\常规中的"其他包含目录"是否正确。如果目录是相对路径,请尝试将其切换为绝对路径。

此外,您还可以尝试使用双引号而不是尖括号来包含它。

#include "ft2build.h"
#include "freetypefreetype.h"

如果它们不起作用,您可以尝试使用 vcpkg 工具,它可以帮助您自动管理C++ Visual Studio 的库。我已经在我这边测试过它,"ft2build.h"通常会包含在头文件中。

步骤:

  1. 打开 Git CMD
  2. 下载 vcpkg 工具:> git 克隆 https://github.com/microsoft/vcpkg
  3. 安装工具:> .\vcpkg\bootstrap-vcpkg.bat
  4. 将 vcpkg 与 Visual Studio 一起使用:> .\vcpkg\vcpkg 集成安装
  5. 安装 freetype
  6. library:> .\vcpkg\vcpkg install freetype:x86-windows
  7. 卸载项目,然后重新加载项目
相关文章: