Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/text #62

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
3837554
Adding msdf gen lib
gustavo-tomas Sep 20, 2024
c9cdff4
Adding font class
gustavo-tomas Sep 20, 2024
49d669b
Adding comments
gustavo-tomas Sep 20, 2024
d2188ed
Adding msdf atlas gen lib
gustavo-tomas Sep 20, 2024
b5dfc5b
Fixing msdf_atlas_gen linking issues
gustavo-tomas Sep 20, 2024
a8ee85c
Creating font atlas
gustavo-tomas Sep 21, 2024
f95e0eb
Moving InternalFontData to header file (temporary)
gustavo-tomas Sep 21, 2024
b9775e2
Adding text shaders
gustavo-tomas Sep 22, 2024
a04769b
Adding ImageFormat enum to image resource
gustavo-tomas Sep 22, 2024
1364c3b
wip: adding text rendering
gustavo-tomas Sep 22, 2024
c618cfa
Adding open sans font
gustavo-tomas Sep 23, 2024
c230584
Adding font loader and manager
gustavo-tomas Sep 23, 2024
adf6c87
Adding const qualifier
gustavo-tomas Sep 23, 2024
5a430e3
Adding text component
gustavo-tomas Sep 23, 2024
5d3febf
Adding is extension supported method
gustavo-tomas Sep 23, 2024
85060cf
Adding text on drag n drop
gustavo-tomas Sep 23, 2024
aba9c17
Adding editable field function to handle multiple input types
gustavo-tomas Sep 24, 2024
850089c
Adding text serialization
gustavo-tomas Sep 24, 2024
e27b338
Adding text deserialization
gustavo-tomas Sep 24, 2024
93c72c2
Adding TextComponent to properties panel
gustavo-tomas Sep 24, 2024
0488dad
Adding some parameters to draw string
gustavo-tomas Sep 24, 2024
1d788dc
Merge pull request #64 from gustavo-tomas/main
gustavo-tomas Sep 26, 2024
c2bd23a
Using quad vertices to render text
gustavo-tomas Sep 26, 2024
cf61456
Adding little hack to render text
gustavo-tomas Sep 26, 2024
603e242
Changing msdf-atlas-gen branch to master
gustavo-tomas Sep 26, 2024
30b365f
Merge branch 'main' into feature/text
gustavo-tomas Oct 5, 2024
f755c2e
Adding missing include
gustavo-tomas Oct 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
path = libs/sol2
url = https://github.com/ThePhD/sol2.git
branch = main
[submodule "libs/msdf_atlas_gen"]
path = libs/msdf_atlas_gen
url = https://github.com/gustavo-tomas/msdf-atlas-gen.git
[submodule "libs/implot"]
path = libs/implot
url = https://github.com/epezent/implot.git
Expand Down
1 change: 1 addition & 0 deletions libs/msdf_atlas_gen
Submodule msdf_atlas_gen added at ae0fdd
93 changes: 93 additions & 0 deletions magnolia/assets/fonts/OFL.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
Copyright 2020 The Open Sans Project Authors (https://github.com/googlefonts/opensans)

This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
https://openfontlicense.org


-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------

PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.

The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.

DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.

"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).

"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).

"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.

"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.

PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:

1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.

2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.

3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.

4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.

5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.

TERMINATION
This license becomes null and void if any of the above conditions are
not met.

DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.
Binary file not shown.
Binary file not shown.
100 changes: 100 additions & 0 deletions magnolia/assets/fonts/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
Open Sans Variable Font
=======================

This download contains Open Sans as both variable fonts and static fonts.

Open Sans is a variable font with these axes:
wdth
wght

This means all the styles are contained in these files:
OpenSans-VariableFont_wdth,wght.ttf
OpenSans-Italic-VariableFont_wdth,wght.ttf

If your app fully supports variable fonts, you can now pick intermediate styles
that aren’t available as static fonts. Not all apps support variable fonts, and
in those cases you can use the static font files for Open Sans:
static/OpenSans_Condensed-Light.ttf
static/OpenSans_Condensed-Regular.ttf
static/OpenSans_Condensed-Medium.ttf
static/OpenSans_Condensed-SemiBold.ttf
static/OpenSans_Condensed-Bold.ttf
static/OpenSans_Condensed-ExtraBold.ttf
static/OpenSans_SemiCondensed-Light.ttf
static/OpenSans_SemiCondensed-Regular.ttf
static/OpenSans_SemiCondensed-Medium.ttf
static/OpenSans_SemiCondensed-SemiBold.ttf
static/OpenSans_SemiCondensed-Bold.ttf
static/OpenSans_SemiCondensed-ExtraBold.ttf
static/OpenSans-Light.ttf
static/OpenSans-Regular.ttf
static/OpenSans-Medium.ttf
static/OpenSans-SemiBold.ttf
static/OpenSans-Bold.ttf
static/OpenSans-ExtraBold.ttf
static/OpenSans_Condensed-LightItalic.ttf
static/OpenSans_Condensed-Italic.ttf
static/OpenSans_Condensed-MediumItalic.ttf
static/OpenSans_Condensed-SemiBoldItalic.ttf
static/OpenSans_Condensed-BoldItalic.ttf
static/OpenSans_Condensed-ExtraBoldItalic.ttf
static/OpenSans_SemiCondensed-LightItalic.ttf
static/OpenSans_SemiCondensed-Italic.ttf
static/OpenSans_SemiCondensed-MediumItalic.ttf
static/OpenSans_SemiCondensed-SemiBoldItalic.ttf
static/OpenSans_SemiCondensed-BoldItalic.ttf
static/OpenSans_SemiCondensed-ExtraBoldItalic.ttf
static/OpenSans-LightItalic.ttf
static/OpenSans-Italic.ttf
static/OpenSans-MediumItalic.ttf
static/OpenSans-SemiBoldItalic.ttf
static/OpenSans-BoldItalic.ttf
static/OpenSans-ExtraBoldItalic.ttf

Get started
-----------

1. Install the font files you want to use

2. Use your app's font picker to view the font family and all the
available styles

Learn more about variable fonts
-------------------------------

https://developers.google.com/web/fundamentals/design-and-ux/typography/variable-fonts
https://variablefonts.typenetwork.com
https://medium.com/variable-fonts

In desktop apps

https://theblog.adobe.com/can-variable-fonts-illustrator-cc
https://helpx.adobe.com/nz/photoshop/using/fonts.html#variable_fonts

Online

https://developers.google.com/fonts/docs/getting_started
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Fonts/Variable_Fonts_Guide
https://developer.microsoft.com/en-us/microsoft-edge/testdrive/demos/variable-fonts

Installing fonts

MacOS: https://support.apple.com/en-us/HT201749
Linux: https://www.google.com/search?q=how+to+install+a+font+on+gnu%2Blinux
Windows: https://support.microsoft.com/en-us/help/314960/how-to-install-or-remove-a-font-in-windows

Android Apps

https://developers.google.com/fonts/docs/android
https://developer.android.com/guide/topics/ui/look-and-feel/downloadable-fonts

License
-------
Please read the full license text (OFL.txt) to understand the permissions,
restrictions and requirements for usage, redistribution, and modification.

You can use them in your products & projects – print or digital,
commercial or otherwise.

This isn't legal advice, please consider consulting a lawyer and see the full
license for all details.
Binary file added magnolia/assets/fonts/static/OpenSans-Bold.ttf
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added magnolia/assets/fonts/static/OpenSans-Italic.ttf
Binary file not shown.
Binary file added magnolia/assets/fonts/static/OpenSans-Light.ttf
Binary file not shown.
Binary file not shown.
Binary file added magnolia/assets/fonts/static/OpenSans-Medium.ttf
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
10 changes: 9 additions & 1 deletion magnolia/src/core/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ namespace mag
shader_loader = create_unique<ShaderLoader>();
LOG_SUCCESS("ShaderLoader initialized");

// Create the font loader
font_loader = create_unique<FontLoader>();
LOG_SUCCESS("FontLoader initialized");

// Create the texture manager
texture_loader = create_unique<TextureManager>();
texture_manager = create_unique<TextureManager>();
LOG_SUCCESS("TextureManager initialized");

// Create the material manager
Expand All @@ -71,6 +75,10 @@ namespace mag
shader_manager = create_unique<ShaderManager>();
LOG_SUCCESS("ShaderManager initialized");

// Create the font manager
font_manager = create_unique<FontManager>();
LOG_SUCCESS("FontManager initialized");

// Create the physics engine
physics_engine = create_unique<PhysicsEngine>();
LOG_SUCCESS("Physics initialized");
Expand Down
12 changes: 9 additions & 3 deletions magnolia/src/core/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "platform/file_dialog.hpp"
#include "renderer/renderer.hpp"
#include "renderer/shader.hpp"
#include "resources/font.hpp"
#include "resources/font_loader.hpp"
#include "resources/image_loader.hpp"
#include "resources/material.hpp"
#include "resources/material_loader.hpp"
Expand Down Expand Up @@ -46,11 +48,13 @@ namespace mag
ImageLoader& get_image_loader() { return *image_loader; };
MaterialLoader& get_material_loader() { return *material_loader; };
ModelLoader& get_model_loader() { return *model_loader; };
TextureManager& get_texture_manager() { return *texture_loader; };
ShaderLoader& get_shader_loader() { return *shader_loader; };
FontLoader& get_font_loader() { return *font_loader; };
TextureManager& get_texture_manager() { return *texture_manager; };
MaterialManager& get_material_manager() { return *material_manager; };
ModelManager& get_model_manager() { return *model_manager; };
ShaderLoader& get_shader_loader() { return *shader_loader; };
ShaderManager& get_shader_manager() { return *shader_manager; };
FontManager& get_font_manager() { return *font_manager; };
PhysicsEngine& get_physics_engine() { return *physics_engine; };
FileDialog& get_file_dialog() { return *file_dialog; };

Expand All @@ -66,10 +70,12 @@ namespace mag
unique<MaterialLoader> material_loader;
unique<ModelLoader> model_loader;
unique<ShaderLoader> shader_loader;
unique<TextureManager> texture_loader;
unique<FontLoader> font_loader;
unique<TextureManager> texture_manager;
unique<MaterialManager> material_manager;
unique<ModelManager> model_manager;
unique<ShaderManager> shader_manager;
unique<FontManager> font_manager;
unique<PhysicsEngine> physics_engine;
unique<FileDialog> file_dialog;

Expand Down
18 changes: 18 additions & 0 deletions magnolia/src/ecs/components.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,24 @@ namespace mag
Camera camera;
};

class Font;
struct TextComponent : public Component
{
TextComponent(const str& text, const ref<Font>& font, const vec4& color = vec4(1), const f32 kerning = 0,
const f32 line_spacing = 0)
: text(text), color(color), kerning(kerning), line_spacing(line_spacing), font(font)
{
}

CLONE(TextComponent);

str text;
vec4 color;
f32 kerning;
f32 line_spacing;
ref<Font> font;
};

class Script;
struct ScriptComponent : public Component
{
Expand Down
69 changes: 68 additions & 1 deletion magnolia/src/renderer/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

#include "core/assert.hpp"
#include "core/logger.hpp"
#include "ecs/components.hpp"
#include "renderer/buffers.hpp"
#include "renderer/test_model.hpp"
#include "resources/model.hpp"

namespace mag
Expand Down Expand Up @@ -65,6 +67,24 @@ namespace mag
command_buffer.bind_index_buffer(ibo_it->second->get_buffer());
}

void Renderer::bind_buffers(TextComponent* text_c)
{
auto vbo_it = vertex_buffers.find(text_c);
auto ibo_it = index_buffers.find(text_c);

if (vbo_it == vertex_buffers.end() || ibo_it == index_buffers.end())
{
LOG_ERROR("Text '{0}' was not uploaded to the GPU", static_cast<void*>(text_c));
return;
}

auto& command_buffer = context->get_curr_frame().command_buffer;

// Bind buffers
command_buffer.bind_vertex_buffer(vbo_it->second->get_buffer());
command_buffer.bind_index_buffer(ibo_it->second->get_buffer());
}

void Renderer::update_model(Model* model)
{
auto vbo_it = vertex_buffers.find(model);
Expand Down Expand Up @@ -149,7 +169,22 @@ namespace mag
const vk::Extent3D extent(image->width, image->height, 1);

// @TODO: check for supported formats
const vk::Format format = vk::Format::eR8G8B8A8Srgb;
vk::Format format = vk::Format::eR8G8B8A8Srgb;

if (image->format == ImageFormat::Srgb)
{
format = vk::Format::eR8G8B8A8Srgb;
}

else if (image->format == ImageFormat::Unorm)
{
format = vk::Format::eR8G8B8A8Unorm;
}

else
{
LOG_ERROR("Invalid image format: '{0}'", static_cast<u32>(image->format));
}

images[image] =
create_ref<RendererImage>(extent, image->pixels, format,
Expand All @@ -173,6 +208,38 @@ namespace mag
images.erase(it);
}

void Renderer::upload_text(TextComponent* text_c, const std::vector<QuadVertex>& vertices,
const std::vector<u32>& indices)
{
auto vbo_it = vertex_buffers.find(text_c);
auto ibo_it = index_buffers.find(text_c);

if (vbo_it != vertex_buffers.end() || ibo_it != index_buffers.end())
{
vertex_buffers[text_c]->resize(vertices.data(), VEC_SIZE_BYTES(vertices));
index_buffers[text_c]->resize(indices.data(), VEC_SIZE_BYTES(indices));
return;
}

vertex_buffers[text_c] = create_ref<VertexBuffer>(vertices.data(), VEC_SIZE_BYTES(vertices));
index_buffers[text_c] = create_ref<IndexBuffer>(indices.data(), VEC_SIZE_BYTES(indices));
}

void Renderer::remove_text(TextComponent* text_c)
{
auto vbo_it = vertex_buffers.find(text_c);
auto ibo_it = index_buffers.find(text_c);

if (vbo_it == vertex_buffers.end() || ibo_it == index_buffers.end())
{
LOG_ERROR("Tried to remove invalid text '{0}'", static_cast<void*>(text_c));
return;
}

vertex_buffers.erase(vbo_it);
index_buffers.erase(ibo_it);
}

void Renderer::on_event(Event& e)
{
EventDispatcher dispatcher(e);
Expand Down
Loading