-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFontManager.h
More file actions
100 lines (73 loc) · 2.16 KB
/
FontManager.h
File metadata and controls
100 lines (73 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#pragma once
#include<iostream>
#include<unordered_map>
#include<fstream>
#include<vulkan/vulkan.h>
#include"ft2build.h"
#include FT_FREETYPE_H
#include"utf8.h"
#include"MaxRectPacking.h"
#define PADDING_PIXEL 2
struct CharFont
{
glm::vec2 size;//ビットマップのサイズ
glm::vec2 bearing;//ベースラインからのオフセット
float advance;//次の文字までの進み幅
glm::vec2 uvMin;//アトラス内のuv座標
glm::vec2 uvMax;//アトラス内のuv座標
float fontHeight;//改行時に下げるべきピクセル値
Rect rect;//アトラステクスチャ内のビットマップがある領域
void uvSet(float width, float height)
{
uvMin = glm::vec2(rect.x / width, rect.y / height);
uvMax = glm::vec2((rect.x + rect.width) / width, (rect.y + rect.height) / height);
}
};
class FontManager
{
private:
static FontManager* instance;
std::unique_ptr<MaxRectPacking> texturePack;
FontManager();
FT_Library library;
FT_Face face;
FT_GlyphSlot slot;
const std::string fontPath = "Font/NotoSansJP-Regular.ttf";
const std::string characterPath = "Font/japaneseList.txt";
std::shared_ptr<ImageData> atlasTexture;
std::unordered_map<uint32_t, CharFont> fontMap;
std::unordered_map<uint32_t, std::shared_ptr<ImageData>> bitmaps;
//アトラステクスチャのバッファを示す
VkDescriptorSet atlasTexDescriptorSet;
//アトラステクスチャの作成
void createAtlasTexture();
//日本語の一覧を文字列として読み込む
void loadJapaneseFile();
//ビットマップの作成
void createBitmap(const uint32_t c);
//すべての文字のビットマップをアトラステクスチャにまとめる
void packBitmapsToAtlas(int atlasWidth,int atlasHeight);
//utf-8からshif-jisに変換
std::string convUtf8ToShiftJis(std::string& utf);
~FontManager();
public:
static FontManager* GetInstance()
{
if (!instance)
{
instance = new FontManager();
}
return instance;
}
static void FinishInstance()
{
delete instance;
instance = nullptr;
}
//フォント全体の高さを取得
const float getFontHeight();
std::shared_ptr<ImageData> getTexture() { return atlasTexture; }
VkDescriptorSet& getDescriptorSet() { return atlasTexDescriptorSet; }
//一つの文字のフォント画像を取得
void getCharFont(const std::vector<uint32_t>& utf8Codes, std::vector<CharFont>& charFonts);
};