Skip to content

Commit

Permalink
Implement MSDF font rendering and change default font
Browse files Browse the repository at this point in the history
We now use Noto Sans, since it has the broadest Unicode support of the open source fonts. I added a README.md in the font folder, so that people can change the font if the need should arise to do so.

With this change, we unified our text rendering to use MSDF rendering. For this we have to generate the whole font map offline in advance.
  • Loading branch information
hasenbanck committed Dec 20, 2024
1 parent 6037aa1 commit e2050f4
Show file tree
Hide file tree
Showing 38 changed files with 952 additions and 521 deletions.
56 changes: 2 additions & 54 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ bumpalo = "3.16"
bytemuck = "1.20"
cgmath = "0.18"
chrono = "0.4"
cosmic-text = "0.12"
cosmic-text = { version = "0.12", default-features = false }
cpal = "0.15"
derive-new = "0.7"
etherparse = "0.16"
fast-srgb8 = "1"
flate2 = "1"
glidesort = "0.1"
hashbrown = "0.15"
image = { version = "0.25", default-features = false }
Expand Down
3 changes: 2 additions & 1 deletion korangar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ bumpalo = { workspace = true, features = ["allocator_api"] }
bytemuck = { workspace = true, features = ["derive", "extern_crate_std", "min_const_generics"] }
cgmath = { workspace = true, features = ["mint", "serde"] }
chrono = { workspace = true }
cosmic-text = { workspace = true }
cosmic-text = { workspace = true, features = ["std", "fontconfig"] }
derive-new = { workspace = true }
flate2 = { workspace = true }
hashbrown = { workspace = true }
glidesort = { workspace = true }
image = { workspace = true, features = ["bmp", "jpeg", "png", "tga", "rayon"] }
Expand Down
Binary file removed korangar/archive/data/WenQuanYiMicroHei.ttf
Binary file not shown.
Binary file added korangar/archive/data/font/NotoSans.csv.gz
Binary file not shown.
Binary file added korangar/archive/data/font/NotoSans.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added korangar/archive/data/font/NotoSans.ttf
Binary file not shown.
93 changes: 93 additions & 0 deletions korangar/archive/data/font/OFL.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
Copyright 2022 The Noto Project Authors (https://github.com/notofonts/latin-greek-cyrillic)

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.
21 changes: 21 additions & 0 deletions korangar/archive/data/font/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# How to use a different font

It is possible to use a different font in Korangar. This may be needed, if you want to support languages that are not
included in the distributed font file. Currently, we use "Noto Sans", which includes all glyphs for the Latin, Cyrillic,
and Greek alphabets. If you need to support a different alphabet, then you need to use a different font (for example
"Noto Sans Japanese"). Since Korangar uses a pre-assembled font map, that includes all glyphs of a font file in
a multichannel signed distance field (MSDF) representation, you need to create a such a font map and also a font map
description file in the CSV format:

1. Use [msdfgen](https://github.com/Chlumsky/msdfgen) to create the font map image and the description file in the
CSV format.
```sh
msdf-atlas-gen -allglyphs -pxrange 6 -size 32 -yorigin top -type mtsdf -format png -font NotoSans.ttf -csv NotoSans.csv -imageout NotoSans.png
```
2. Copy the original font file and also the generated PNG and CSV file into the `archive/data/font` folder.
3. Optionally compress the CSV file with gzip:
```sh
gzip NotoSans.csv
```
4. Update the `FONT_FILE_PATH`, `FONT_MAP_DESCRIPTION_FILE_PATH`, `FONT_MAP_FILE_PATH` and `FONT_FAMILY_NAME` in the
`korangar/src/loaders/font/mod.rs` file.
Binary file removed korangar/archive/data/texture/font.png
Binary file not shown.
Binary file modified korangar/archive/data/texture/marker_effect.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified korangar/archive/data/texture/marker_entity.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified korangar/archive/data/texture/marker_light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified korangar/archive/data/texture/marker_object.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified korangar/archive/data/texture/marker_particle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified korangar/archive/data/texture/marker_shadow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified korangar/archive/data/texture/marker_sound.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 28 additions & 5 deletions korangar/src/graphics/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub struct RenderInstruction<'a> {
pub effects: &'a [EffectInstruction],
pub water: Option<WaterInstruction<'a>>,
pub map_picker_tile_vertex_buffer: &'a Buffer<TileVertex>,
pub font_atlas_texture: &'a Texture,
pub font_map_texture: &'a Texture,
#[cfg(feature = "debug")]
pub render_settings: RenderSettings,
#[cfg(feature = "debug")]
Expand Down Expand Up @@ -121,6 +121,15 @@ pub enum RectangleInstruction {
screen_size: ScreenSize,
color: Color,
},
Sprite {
screen_position: ScreenPosition,
screen_size: ScreenSize,
color: Color,
texture_position: Vector2<f32>,
texture_size: Vector2<f32>,
linear_filtering: bool,
texture: Arc<Texture>,
},
Sdf {
screen_position: ScreenPosition,
screen_size: ScreenSize,
Expand All @@ -129,15 +138,21 @@ pub enum RectangleInstruction {
texture_size: Vector2<f32>,
texture: Arc<Texture>,
},
Sprite {
Msdf {
screen_position: ScreenPosition,
screen_size: ScreenSize,
color: Color,
texture_position: Vector2<f32>,
texture_size: Vector2<f32>,
linear_filtering: bool,
texture: Arc<Texture>,
},
Text {
screen_position: ScreenPosition,
screen_size: ScreenSize,
color: Color,
texture_position: Vector2<f32>,
texture_size: Vector2<f32>,
},
}

#[derive(Clone, Debug)]
Expand All @@ -149,6 +164,15 @@ pub enum InterfaceRectangleInstruction {
color: Color,
corner_radius: CornerRadius,
},
Sprite {
screen_position: ScreenPosition,
screen_size: ScreenSize,
screen_clip: ScreenClip,
color: Color,
corner_radius: CornerRadius,
texture: Arc<Texture>,
smooth: bool,
},
Sdf {
screen_position: ScreenPosition,
screen_size: ScreenSize,
Expand All @@ -157,14 +181,13 @@ pub enum InterfaceRectangleInstruction {
corner_radius: CornerRadius,
texture: Arc<Texture>,
},
Sprite {
Msdf {
screen_position: ScreenPosition,
screen_size: ScreenSize,
screen_clip: ScreenClip,
color: Color,
corner_radius: CornerRadius,
texture: Arc<Texture>,
smooth: bool,
},
Text {
screen_position: ScreenPosition,
Expand Down
4 changes: 2 additions & 2 deletions korangar/src/graphics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub(crate) struct DebugUniforms {
show_directional_shadow_map: u32,
show_point_shadow_map: u32,
show_light_culling_count_buffer: u32,
show_font_atlas: u32,
show_font_map: u32,
}

#[derive(Copy, Clone, Pod, Zeroable)]
Expand Down Expand Up @@ -272,7 +272,7 @@ impl Prepare for GlobalContext {
.map(|value| value.get())
.unwrap_or(0),
show_light_culling_count_buffer: instructions.render_settings.show_light_culling_count_buffer as u32,
show_font_atlas: instructions.render_settings.show_font_atlas as u32,
show_font_map: instructions.render_settings.show_font_map as u32,
};
}
}
Expand Down
Loading

0 comments on commit e2050f4

Please sign in to comment.