首页 > 解决方案 > 无法在头文件中包含标头,但可以包含在 cpp 中

问题描述

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

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

我只是想正常使用FreeType,FreeType的headers location、lib location、freetype.lib和freetype.dll都已经指定了。

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

根据评论完整源代码中的要求: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);
    };
}

字体光栅化.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();
}

方法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 <glm\vec2.hpp>
#include <glm\vec4.hpp>

// Mine
#include "ErrorStack.hpp"

标签: c++visual-studiostatic-libraries

解决方案


根据C1083 错误,请在 Project Properties\C/C++\General 中检查您的 'Additional Include Directories' 是否正确。如果目录是相对路径,请尝试切换为绝对路径。

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

#include "ft2build.h"
#include "freetype\freetype.h"

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

步骤:

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

推荐阅读