Skip to content

Commit 9d80845

Browse files
committed
font: fixes in gui, cursor
1 parent c41add3 commit 9d80845

File tree

13 files changed

+479
-279
lines changed

13 files changed

+479
-279
lines changed

data/cage/font/font.assets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ font.pack
44

55
[]
66
scheme = font
7-
ubuntu/regular.ttf
87
ubuntu/bold.ttf
98
ubuntu/italic.ttf
109
ubuntu/monospace.ttf
10+
ubuntu/regular.ttf

sources/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ cage_ide_sort_files(cage-core)
7676

7777
file(GLOB_RECURSE cage-engine-sources "libengine/*" "include/cage-engine/*")
7878
add_library(cage-engine SHARED ${cage-engine-sources})
79-
target_link_libraries(cage-engine PRIVATE cubeb glfw openxr_loader freetype harfbuzz SheenBidi uni-algo)
79+
target_link_libraries(cage-engine PRIVATE cubeb glfw openxr_loader freetype harfbuzz SheenBidi)
8080
target_link_libraries(cage-engine PUBLIC cage-core glad)
8181
file(GLOB_RECURSE controller-bindings RELATIVE "${CMAKE_CURRENT_LIST_DIR}" "controller-bindings/*")
8282
set(index 0)

sources/asset-processor/font.cpp

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,11 @@ namespace
9090
CAGE_THROW_ERROR(Exception, "unknown freetype error code");
9191
}
9292

93-
msdfgen::Shape cursorShape()
93+
void setSize(uint32 nominalSize)
9494
{
95-
const msdfgen::Point2 points[4] = { { 0, 0 }, { 0, 10 }, { 1, 10 }, { 1, 0 } };
96-
msdfgen::Shape shape;
97-
msdfgen::Contour &c = shape.addContour();
98-
c.addEdge(msdfgen::EdgeHolder(points[0], points[1]));
99-
c.addEdge(msdfgen::EdgeHolder(points[1], points[2]));
100-
c.addEdge(msdfgen::EdgeHolder(points[2], points[3]));
101-
c.addEdge(msdfgen::EdgeHolder(points[3], points[0]));
102-
return shape;
95+
CALL(FT_Set_Pixel_Sizes, face, nominalSize, nominalSize);
96+
header.nominalSize = nominalSize;
97+
header.nominalScale = 1.0 / nominalSize / 64;
10398
}
10499

105100
void loadGlyphs()
@@ -123,6 +118,45 @@ namespace
123118
CAGE_LOG(SeverityEnum::Info, "assetProcessor", Stringizer() + "non-empty glyphs count: " + header.glyphsCount);
124119
}
125120

121+
void computeLineProperties()
122+
{
123+
CAGE_LOG(SeverityEnum::Info, "assetProcessor", "compute line properties");
124+
Real maxAscender, minDescender;
125+
for (char c : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
126+
{
127+
CALL(FT_Load_Char, face, c, FT_LOAD_DEFAULT);
128+
const FT_Glyph_Metrics &glm = face->glyph->metrics;
129+
maxAscender = max(maxAscender, float(glm.horiBearingY) * header.nominalScale);
130+
minDescender = min(minDescender, (float(glm.horiBearingY) - float(glm.height)) * header.nominalScale);
131+
}
132+
header.lineHeight = maxAscender - minDescender;
133+
header.lineOffset = minDescender * 0.5 - maxAscender;
134+
CAGE_LOG(SeverityEnum::Note, "assetProcessor", Stringizer() + "line offset: " + header.lineOffset);
135+
CAGE_LOG(SeverityEnum::Note, "assetProcessor", Stringizer() + "line height: " + header.lineHeight);
136+
}
137+
138+
msdfgen::Shape cursorShape()
139+
{
140+
const msdfgen::Point2 points[4] = { { 0, 0 }, { 0, 10 }, { 1, 10 }, { 1, 0 } };
141+
msdfgen::Shape shape;
142+
msdfgen::Contour &c = shape.addContour();
143+
c.addEdge(msdfgen::EdgeHolder(points[0], points[1]));
144+
c.addEdge(msdfgen::EdgeHolder(points[1], points[2]));
145+
c.addEdge(msdfgen::EdgeHolder(points[2], points[3]));
146+
c.addEdge(msdfgen::EdgeHolder(points[3], points[0]));
147+
return shape;
148+
}
149+
150+
void addCursorGlyph()
151+
{
152+
CAGE_LOG(SeverityEnum::Info, "assetProcessor", "adding cursor glyph");
153+
Glyph g;
154+
g.data.glyphId = uint32(-2);
155+
g.shape = cursorShape();
156+
glyphs.push_back(std::move(g));
157+
header.glyphsCount++;
158+
}
159+
126160
void createGlyphsImages()
127161
{
128162
CAGE_LOG(SeverityEnum::Info, "assetProcessor", "create glyphs images");
@@ -187,23 +221,6 @@ namespace
187221
images.push_back(std::move(image));
188222
}
189223

190-
void computeLineProperties()
191-
{
192-
CAGE_LOG(SeverityEnum::Info, "assetProcessor", "compute line properties");
193-
Real maxAscender, minDescender;
194-
for (char c : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
195-
{
196-
CALL(FT_Load_Char, face, c, FT_LOAD_DEFAULT);
197-
const FT_Glyph_Metrics &glm = face->glyph->metrics;
198-
maxAscender = max(maxAscender, float(glm.horiBearingY) * header.nominalScale);
199-
minDescender = min(minDescender, (float(glm.horiBearingY) - float(glm.height)) * header.nominalScale);
200-
}
201-
header.lineHeight = maxAscender - minDescender;
202-
header.lineOffset = minDescender * 0.5 - maxAscender;
203-
CAGE_LOG(SeverityEnum::Note, "assetProcessor", Stringizer() + "line offset: " + header.lineOffset);
204-
CAGE_LOG(SeverityEnum::Note, "assetProcessor", Stringizer() + "line height: " + header.lineHeight);
205-
}
206-
207224
void exportData()
208225
{
209226
CAGE_LOG(SeverityEnum::Info, "assetProcessor", "exporting data");
@@ -265,13 +282,6 @@ namespace
265282
glyphs.clear();
266283
images.clear();
267284
}
268-
269-
void setSize(uint32 nominalSize)
270-
{
271-
CALL(FT_Set_Pixel_Sizes, face, nominalSize, nominalSize);
272-
header.nominalSize = nominalSize;
273-
header.nominalScale = 1.0 / nominalSize / 64;
274-
}
275285
}
276286

277287
void processFont()
@@ -287,9 +297,10 @@ void processFont()
287297
CAGE_LOG(SeverityEnum::Info, "assetProcessor", Stringizer() + "units per EM: " + face->units_per_EM);
288298
setSize(40);
289299
loadGlyphs();
300+
computeLineProperties();
301+
addCursorGlyph();
290302
createGlyphsImages();
291303
createImage();
292-
computeLineProperties();
293304
exportData();
294305
printDebugData();
295306
clearAll();

sources/include/cage-engine/font.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ namespace cage
1919
struct CAGE_ENGINE_API FontLayoutGlyph
2020
{
2121
Vec4 wrld;
22-
uint32 index = 0; // glyph info
22+
uint32 index = m; // glyph info
23+
uint32 cluster = m; // index in utf32 encoded input string
2324
};
2425

2526
struct CAGE_ENGINE_API FontLayoutResult : private Noncopyable
2627
{
2728
Holder<PointerRange<FontLayoutGlyph>> glyphs;
2829
Vec2 size;
30+
uint32 cursor = m; // index in utf32 encoded input string
2931
};
3032

3133
class CAGE_ENGINE_API Font : private Immovable
@@ -39,7 +41,10 @@ namespace cage
3941

4042
void importBuffer(PointerRange<const char> buffer);
4143

42-
FontLayoutResult layout(PointerRange<const char> text, const FontFormat &format) const;
44+
FontLayoutResult layout(PointerRange<const char> text, const FontFormat &format, Vec2 cursorPoint) const;
45+
FontLayoutResult layout(PointerRange<const char> text, const FontFormat &format, uint32 cursorIndexUtf32 = m) const;
46+
FontLayoutResult layout(PointerRange<const uint32> text, const FontFormat &format, Vec2 cursorPoint) const;
47+
FontLayoutResult layout(PointerRange<const uint32> text, const FontFormat &format, uint32 cursorIndexUtf32 = m) const;
4348

4449
void render(RenderQueue *queue, const Holder<Model> &model, const FontLayoutResult &layout) const;
4550
};

sources/libcore/unicode/unicode.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@
33
#include <uni_algo/case.h>
44
#include <uni_algo/conv.h>
55
#include <uni_algo/norm.h>
6+
#include <uni_algo/prop.h>
67

78
#include <cage-core/pointerRangeHolder.h>
89
#include <cage-core/unicode.h>
910

1011
namespace cage
1112
{
13+
namespace privat
14+
{
15+
CAGE_API_EXPORT bool unicodeIsWhitespace(uint32 c)
16+
{
17+
return una::codepoint::is_whitespace(c);
18+
}
19+
}
20+
1221
namespace
1322
{
1423
std::string_view view(PointerRange<const char> buffer)

0 commit comments

Comments
 (0)