Skip to content

Commit

Permalink
builtin font, dom text wip
Browse files Browse the repository at this point in the history
  • Loading branch information
afwbkbc committed Dec 21, 2024
1 parent d83ffaf commit 7e9c340
Show file tree
Hide file tree
Showing 29 changed files with 439 additions and 88 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ compile_commands.json
CTestTestfile.cmake
_deps
/bin
/tools/bin
/nbproject
/src/tmp/last_commit.h
/src/tmp
/debug.log
/build
/build_release
Expand Down
23 changes: 18 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ IF ( NOT WIN32 )
)
ENDIF ()

IF ( NOT WIN32 )
SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread" )
ENDIF ()
SET( CMAKE_CXX_FLAGS " -std=c++17 ${CMAKE_CXX_FLAGS} -Wno-pointer-arith -Wno-vla-cxx-extension" )

ADD_SUBDIRECTORY( tools )
SET( EMBED_CPP "src/tmp/embed.cpp" )
SET( EMBED_FILES
"./resource/font/terminus/TerminusTTF-4.49.3-minified.ttf"
)
ADD_CUSTOM_COMMAND(
OUTPUT ${EMBED_CPP}
DEPENDS ${EMBED_FILES}
COMMAND ./tools/bin/embedder "${EMBED_CPP}" "${EMBED_FILES}"
)
ADD_CUSTOM_TARGET( embed ALL DEPENDS ${EMBED_CPP} )

FUNCTION( SUBDIR DIR )
SET( PWD "${PWD}/${DIR}" )
ADD_SUBDIRECTORY( ${DIR} )
Expand All @@ -29,6 +46,7 @@ ENDFUNCTION( SUBDIR )
SUBDIR( src )
ADD_EXECUTABLE( ${PROJECT_NAME}
${SRC}
${EMBED_CPP}
)

IF ( NOT WIN32 )
Expand Down Expand Up @@ -128,9 +146,6 @@ ENDIF ()

IF ( NOT WIN32 )
ADD_DEPENDENCIES( ${PROJECT_NAME} glsmac_version )

SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread" )

TARGET_LINK_OPTIONS( ${PROJECT_NAME} PRIVATE ${FT2_LDFLAGS} ${GLEW_LDFLAGS} ${SDL2_LDFLAGS} ${SDL2IMAGE_LDFLAGS} )
ENDIF ()

Expand All @@ -142,8 +157,6 @@ IF ( WIN32 )
TARGET_LINK_OPTIONS( ${PROJECT_NAME} PRIVATE -lws2_32 )
ENDIF ()

SET( CMAKE_CXX_FLAGS " -std=c++17 ${CMAKE_CXX_FLAGS} -Wno-pointer-arith -Wno-vla-cxx-extension" )

IF (
CMAKE_BUILD_TYPE STREQUAL "Release" OR
CMAKE_BUILD_TYPE STREQUAL "Portable64" OR
Expand Down
9 changes: 7 additions & 2 deletions GLSMAC_data/default/uidemo.gls.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@
imgs []= root.img({
align: a[0],
background: a[1],
width: 50,
height: 50,
width: 20,
height: 20,
});
}
#print('images', imgs);
imgs[4].background = '#ffffff'; // make center visible

let text = root.text({
color: '#ff0000',
text: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz 0123456789 !@#$%^&*()-=_+.,<>{}[];:\'"\|`~',
});

});
97 changes: 97 additions & 0 deletions resource/font/terminus/COPYING
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
Copyright (c) 2010 Dimitar Toshkov Zhekov,
with Reserved Font Name "Terminus Font".

Copyright (c) 2011-2023 Tilman Blumenbach,
with Reserved Font Name "Terminus (TTF)".

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:
http://scripts.sil.org/OFL


-----------------------------------------------------------
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.
2 changes: 1 addition & 1 deletion src/game/backend/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1304,7 +1304,7 @@ void Game::InitGame( MT_Response& response, MT_CANCELABLE ) {
ASSERT( util::FS::FileExists( filename ), "map dump file \"" + filename + "\" not found" );
Log( (std::string)"Loading map dump from " + filename );
ui->SetLoaderText( "Loading dump", false );
m_map->Unserialize( types::Buffer( util::FS::ReadFile( filename ) ) );
m_map->Unserialize( types::Buffer( util::FS::ReadTextFile( filename ) ) );
ec = map::Map::EC_NONE;
}
else
Expand Down
2 changes: 1 addition & 1 deletion src/game/backend/map/Map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ const Map::error_code_t Map::LoadFromFile( const std::string& path ) {
ASSERT( util::FS::FileExists( path ), "map file \"" + path + "\" not found" );

Log( "Loading map from " + path );
auto b = types::Buffer( util::FS::ReadFile( path ) );
auto b = types::Buffer( util::FS::ReadTextFile( path ) );
return LoadFromBuffer( b );
}

Expand Down
7 changes: 7 additions & 0 deletions src/graphics/opengl/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

#include "actor/Sprite.h"
#include "actor/Mesh.h"
#include "actor/Text.h"
#include "common/ObjectLink.h"
#include "scene/Scene.h"
#include "scene/actor/Actor.h"
#include "scene/actor/Mesh.h"
#include "scene/actor/Sprite.h"
#include "scene/actor/Instanced.h"
#include "scene/actor/Text.h"
#include "routine/Routine.h"

namespace graphics {
Expand Down Expand Up @@ -136,6 +138,11 @@ void Scene::Update() {
NEW( gl_actor, Mesh, m_opengl, (scene::actor::Mesh*)*it );
break;
}
case scene::actor::Actor::TYPE_TEXT: {
auto* text_actor = (scene::actor::Text*)*it;
NEW( gl_actor, Text, m_opengl, text_actor, text_actor->GetFont() );
break;
}
default: {
gl_actor = m_routine->AddCustomActor( *it );
}
Expand Down
2 changes: 1 addition & 1 deletion src/gse/GSE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ const Value GSE::RunScript( context::Context* ctx, const si_t& si, const std::st
if ( full_path.empty() ) {
throw Exception( EC.LOADER_ERROR, "Could not find script for include '" + path + "'", ctx, si );
}
const auto source = util::FS::ReadFile( full_path, PATH_SEPARATOR );
const auto source = util::FS::ReadTextFile( full_path, PATH_SEPARATOR );
include_cache_t cache = {
VALUE( type::Undefined ),
nullptr,
Expand Down
2 changes: 1 addition & 1 deletion src/gse/context/GlobalContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ GlobalContext::GlobalContext( GSE* gse, const std::string& source_path )
, m_source_lines(
source_path.empty()
? source_lines_t{}
: util::String::Split( util::FS::ReadFile( source_path, GSE::PATH_SEPARATOR ), '\n' )
: util::String::Split( util::FS::ReadTextFile( source_path, GSE::PATH_SEPARATOR ), '\n' )
) {}

Context* GlobalContext::GetParentContext() const {
Expand Down
2 changes: 1 addition & 1 deletion src/gse/tests/Scripts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void AddScriptsTests( task::gsetests::GSETests* task ) {

std::string last_error = "";
try {
const auto source = util::FS::ReadFile( script, GSE::PATH_SEPARATOR );
const auto source = util::FS::ReadTextFile( script, GSE::PATH_SEPARATOR );
parser = gse->GetParser( script, source );
context = gse->CreateGlobalContext( script );
context->IncRefs();
Expand Down
41 changes: 40 additions & 1 deletion src/loader/font/FontLoader.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,49 @@
#include "FontLoader.h"

#include "types/Font.h"
#include "util/FS.h"

namespace loader {
namespace font {

FontLoader::~FontLoader() {
for ( auto& it : m_fonts ) {
DELETE( it.second );
}
for ( const auto& f : m_builtin_fonts ) {
DELETE( f.second );
}
}

types::Font* FontLoader::LoadFont( const resource::resource_t res, const unsigned char size ) {
return LoadFontImpl( GetFilename( res ), GetPath( res ), size );
const auto& name = GetFilename( res );
const auto key = name + ":" + std::to_string( size );
auto it = m_fonts.find( key );
if ( it == m_fonts.end() ) {
std::vector< unsigned char > data = {};
util::FS::ReadFile( data, GetPath( res ) );
it = m_fonts.insert(
{
key,
LoadFontImpl( GetFilename( res ), data, size )
}
).first;
}
return it->second;
}

types::Font* FontLoader::GetBuiltinFont( const unsigned char size ) {
auto it = m_builtin_fonts.find( size );
if ( it == m_builtin_fonts.end() ) {
const auto font_key = "<BUILTIN>:" + std::to_string( size );
it = m_builtin_fonts.insert(
{
size,
LoadFontImpl( "<BUILTIN>", util::FS::GetEmbeddedFile( "_resource_font_terminus_TerminusTTF_4_49_3_minified_ttf" ), size )
}
).first;
}
return it->second;
}

}
Expand Down
13 changes: 12 additions & 1 deletion src/loader/font/FontLoader.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#pragma once

#include <string>
#include <unordered_map>

#include "loader/Loader.h"

#include "resource/Types.h"
#include "types/Vec2.h"

namespace types {
class Font;
Expand All @@ -14,9 +16,18 @@ namespace loader {
namespace font {

CLASS( FontLoader, Loader )
virtual ~FontLoader();

types::Font* LoadFont( const resource::resource_t res, const unsigned char size );
types::Font* GetBuiltinFont( const unsigned char size );

protected:
virtual types::Font* LoadFontImpl( const std::string& name, const std::string& path, const unsigned char size ) = 0;
virtual types::Font* LoadFontImpl( const std::string& name, const std::vector< unsigned char >& data, const unsigned char size ) = 0;

private:
// cache all fonts for future use
std::unordered_map< std::string, types::Font* > m_fonts;
std::unordered_map< unsigned char, types::Font* > m_builtin_fonts;
};

}
Expand Down
Loading

0 comments on commit 7e9c340

Please sign in to comment.