簡體   English   中英

無法在頭文件中包含頭文件,但可以包含在 cpp 中

[英]Unable to include header in header file, but can include in cpp

如果我將 FreeType 頭文件移動到FontRasterization.hpp頭文件中,我會收到fatal error C1083: Cannot open include file: 'ft2build.h': No such file or directory為什么fatal error C1083: Cannot open include file: 'ft2build.h': No such file or directory

我必須做一個涉及void*的奇怪解決方法來將 FreeType 對象存儲在我的頭文件中並將它們投射到我的源文件中。

我只是想正常使用FreeType,FreeType的頭文件位置,lib位置,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"

根據C1083 錯誤,請檢查 Project Properties\\C/C++\\General 中的“其他包含目錄”是否正確。 如果目錄是相對路徑,請嘗試將其切換為絕對路徑。

此外,您也可以嘗試使用雙引號代替尖括號來包含它。

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

如果它們不起作用,您可以嘗試使用vcpkg tool ,它可以幫助您自動管理 Visual Studio 的 C++ 庫。 我已經在我這邊進行了測試,“ft2build.h”通常會包含在頭文件中。

步驟:

  1. 打開 Git CMD
  2. 下載 vcpkg 工具:> git clone https://github.com/microsoft/vcpkg
  3. 安裝工具:> .\\vcpkg\\bootstrap-vcpkg.bat
  4. 在 Visual Studio 中使用 vcpkg:> .\\vcpkg\\vcpkg 集成安裝
  5. 安裝 freetype 庫:> .\\vcpkg\\vcpkg install freetype:x86-windows
  6. 卸載您的項目,然后重新加載您的項目

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM