Skip to content

Commit

Permalink
Overhaul fonts
Browse files Browse the repository at this point in the history
1. Adds support for multiple font rendering
2. Replaces the default wy_scorpio font with 3 fonts:
   1. Fiery_Turk.ttf for Latin (as in the OG DinguxCommander).
   2. FreeSans.ttf for Cyrillic, Greek, etc.
   3. DroidSansFallbackFull.ttf for CJK.

Because the last 2 fonts are less pixel-perfect, we render them
at a larger font size.

Font sizes:
3.9M	res/DroidSansFallbackFull.ttf
64K	res/Fiery_Turk.ttf
1.5M	res/FreeSans.ttf

These fonts also compress very well.
RG-350 OPK size: 3.0M
  • Loading branch information
glebm committed Mar 31, 2020
1 parent 5400d94 commit 69b5b55
Show file tree
Hide file tree
Showing 20 changed files with 218 additions and 59 deletions.
1 change: 0 additions & 1 deletion CMake/rs90_defs.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ set(AUTOSCALE 1)
set(FILE_SYSTEM \"/dev/mmcblk1p1\")
set(PATH_DEFAULT \"/media/sdcard\")
set(RES_DIR "")
set(FONT_PATH \"/usr/share/fonts/truetype/dejavu/DejaVuSansCondensed.ttf\")

set(MYKEY_UP SDLK_UP) # Up
set(MYKEY_RIGHT SDLK_RIGHT) # Right
Expand Down
7 changes: 4 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ set(DinguxCommander_SRCS
panel.cpp
resourceManager.cpp
screen.cpp
sdl_ttf_multifont.cpp
sdlutils.cpp
viewer.cpp
window.cpp
Expand Down Expand Up @@ -101,8 +102,8 @@ target_link_libraries(${BIN_TARGET} PRIVATE
m
)

set(TARGET_PLATFORM retrofw CACHE STRING "Target platform" FORCE)
set_property(CACHE TARGET_PLATFORM PROPERTY STRINGS retrofw gcw0 rg350 rs90)
set(TARGET_PLATFORM host CACHE STRING "Target platform" FORCE)
set_property(CACHE TARGET_PLATFORM PROPERTY STRINGS host retrofw gcw0 rg350 rs90)

if(TARGET_PLATFORM STREQUAL "retrofw")
include(CMake/retrofw_defs.cmake)
Expand All @@ -122,11 +123,11 @@ foreach(
PPU_Y
AUTOSCALE
RES_DIR
FONTS
PATH_DEFAULT
PATH_DEFAULT_RIGHT
PATH_DEFAULT_RIGHT_FALLBACK
FILE_SYSTEM
FONT_PATH
MYKEY_UP
MYKEY_RIGHT
MYKEY_DOWN
Expand Down
4 changes: 4 additions & 0 deletions def.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
#define RES_DIR "res/"
#endif

#ifndef FONTS
#define FONTS {RES_DIR"Fiery_Turk.ttf",8},{RES_DIR"FreeSans.ttf",10},{RES_DIR"DroidSansFallbackFull.ttf",9}
#endif

#ifndef SCREEN_WIDTH
#define SCREEN_WIDTH 320
#endif
Expand Down
10 changes: 5 additions & 5 deletions dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ CDialog::CDialog(const std::string &p_title, const Sint16 p_x, const Sint16 p_y)
m_y(p_y),
m_cursorX(0),
m_cursorY(0),
m_font(CResourceManager::instance().getFont())
m_fonts(CResourceManager::instance().getFonts())
{
// Title
if (!p_title.empty())
Expand Down Expand Up @@ -82,7 +82,7 @@ void CDialog::init(void)
// Title
auto l_it = m_lines.begin();
if (m_nbTitle) {
m_titleImg = SDL_utils::renderText(m_font, *l_it, Globals::g_colorTextTitle, {COLOR_BORDER});
m_titleImg = SDL_utils::renderText(m_fonts, *l_it, Globals::g_colorTextTitle, {COLOR_BORDER});
l_width = m_titleImg->w;
++l_it;
}
Expand All @@ -93,9 +93,9 @@ void CDialog::init(void)
m_linesImgCursor1.reserve(num_non_title_lines);
m_linesImgCursor2.reserve(num_non_title_lines);
for ( ; l_it != m_lines.end(); ++l_it) {
m_linesImg.push_back(SDL_utils::renderText(m_font, *l_it, Globals::g_colorTextNormal, {COLOR_BG_1}));
m_linesImgCursor1.push_back(SDL_utils::renderText(m_font, *l_it, Globals::g_colorTextNormal, {COLOR_CURSOR_1}));
m_linesImgCursor2.push_back(SDL_utils::renderText(m_font, *l_it, Globals::g_colorTextNormal, {COLOR_CURSOR_2}));
m_linesImg.push_back(SDL_utils::renderText(m_fonts, *l_it, Globals::g_colorTextNormal, {COLOR_BG_1}));
m_linesImgCursor1.push_back(SDL_utils::renderText(m_fonts, *l_it, Globals::g_colorTextNormal, {COLOR_CURSOR_1}));
m_linesImgCursor2.push_back(SDL_utils::renderText(m_fonts, *l_it, Globals::g_colorTextNormal, {COLOR_CURSOR_2}));
if (m_linesImg.back()->w > l_width)
l_width = m_linesImg.back()->w;
}
Expand Down
2 changes: 1 addition & 1 deletion dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class CDialog : public CWindow
mutable SDL_Rect m_clip;

// Pointers to resources
TTF_Font *m_font;
const std::vector<TTF_Font *> &m_fonts;
};

#endif
12 changes: 6 additions & 6 deletions keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ CKeyboard::CKeyboard(const std::string &p_inputText):
m_selected(0),
m_footer(NULL),
m_keySet(0),
m_font(CResourceManager::instance().getFont())
m_fonts(CResourceManager::instance().getFonts())
{
// Key sets
m_keySets[0] = "abcdefghijklmnopqrstuvwxyz0123456789., ";
Expand Down Expand Up @@ -78,7 +78,7 @@ CKeyboard::CKeyboard(const std::string &p_inputText):
}
// Create footer
m_footer = SDL_utils::createImage(screen.w * screen.ppu_x, FOOTER_H * screen.ppu_y, SDL_MapRGB(Globals::g_screen->format, COLOR_BORDER));
SDL_utils::applyText(screen.w >> 1, 1, m_footer, m_font, "A-Input B-Cancel START-OK L/R-Change Y-Backspace X-Space", Globals::g_colorTextTitle, {COLOR_TITLE_BG}, SDL_utils::T_TEXT_ALIGN_CENTER);
SDL_utils::applyText(screen.w >> 1, 1, m_footer, m_fonts, "A-Input B-Cancel START-OK L/R-Change Y-Backspace X-Space", Globals::g_colorTextTitle, {COLOR_TITLE_BG}, SDL_utils::T_TEXT_ALIGN_CENTER);
}

CKeyboard::~CKeyboard(void)
Expand Down Expand Up @@ -109,7 +109,7 @@ void CKeyboard::render(const bool p_focus) const
// Input text
if (!m_inputText.empty())
{
SDL_Surface *l_surfaceTmp = SDL_utils::renderText(m_font, m_inputText, Globals::g_colorTextNormal, {COLOR_BG_1});
SDL_Surface *l_surfaceTmp = SDL_utils::renderText(m_fonts, m_inputText, Globals::g_colorTextNormal, {COLOR_BG_1});
if (l_surfaceTmp->w > FIELD_W)
{
// Text is too big => clip it
Expand Down Expand Up @@ -186,15 +186,15 @@ void CKeyboard::render(const bool p_focus) const
l_text = m_keySets[m_keySet].substr(l_i, 1);
l_i += 1;
}
SDL_utils::applyText(KB_X + 20 * l_x + 13, KB_Y + 7 + 20 * l_y, Globals::g_screen, m_font, l_text, Globals::g_colorTextNormal,
SDL_utils::applyText(KB_X + 20 * l_x + 13, KB_Y + 7 + 20 * l_y, Globals::g_screen, m_fonts, l_text, Globals::g_colorTextNormal,
selected_letter_x == l_x && selected_letter_y == l_y ? SDL_Color{COLOR_CURSOR_1} : SDL_Color{COLOR_BG_1}, SDL_utils::T_TEXT_ALIGN_CENTER);
}
}
}
// Buttons text
SDL_utils::applyText(KB_X + 67, KB_Y + 67, Globals::g_screen, m_font, "Cancel", Globals::g_colorTextNormal,
SDL_utils::applyText(KB_X + 67, KB_Y + 67, Globals::g_screen, m_fonts, "Cancel", Globals::g_colorTextNormal,
m_selected == 39 ? SDL_Color{COLOR_CURSOR_1} : SDL_Color{COLOR_BG_1}, SDL_utils::T_TEXT_ALIGN_CENTER);
SDL_utils::applyText(KB_X + 197, KB_Y + 67, Globals::g_screen, m_font, "OK", Globals::g_colorTextNormal,
SDL_utils::applyText(KB_X + 197, KB_Y + 67, Globals::g_screen, m_fonts, "OK", Globals::g_colorTextNormal,
m_selected == 40 ? SDL_Color{COLOR_CURSOR_1} : SDL_Color{COLOR_BG_1}, SDL_utils::T_TEXT_ALIGN_CENTER);
// Draw footer
SDL_utils::applySurface(0, 227, m_footer, Globals::g_screen);
Expand Down
4 changes: 3 additions & 1 deletion keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define _KEYBOARD_H_

#include <string>
#include <vector>

#include <SDL.h>
#include <SDL_ttf.h>
#include "window.h"
Expand Down Expand Up @@ -72,7 +74,7 @@ class CKeyboard : public CWindow
unsigned char m_keySet;

// Pointers to resources
TTF_Font *m_font;
const std::vector<TTF_Font *> &m_fonts;
};

#endif
6 changes: 1 addition & 5 deletions package-opk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,11 @@ main() {
"opkg/readme.$ext.txt"
opkg/commander.png
res/*.png
res/*.ttf
"$BUILD_DIR/commander"
"$OUT"
)

# RS90 package uses a system font.
if [[ $TARGET != rs90 ]]; then
files+=(res/wy_scorpio.ttf)
fi

set -x
mksquashfs "${files[@]}" \
-all-root -no-xattrs -noappend -no-exports
Expand Down
10 changes: 5 additions & 5 deletions panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ CPanel::CPanel(const std::string &p_path, const Sint16 p_x):
m_iconUp(CResourceManager::instance().getSurface(CResourceManager::T_SURFACE_UP)),
m_cursor1(CResourceManager::instance().getSurface(CResourceManager::T_SURFACE_CURSOR1)),
m_cursor2(CResourceManager::instance().getSurface(CResourceManager::T_SURFACE_CURSOR2)),
m_font(CResourceManager::instance().getFont())
m_fonts(CResourceManager::instance().getFonts())
{
// List the given path
if (m_fileLister.list(p_path))
Expand Down Expand Up @@ -55,7 +55,7 @@ void CPanel::render(const bool p_active) const
const SDL_Color *l_color = NULL;
SDL_Rect l_rect;
// Current dir
l_surfaceTmp = SDL_utils::renderText(m_font, m_currentPath, Globals::g_colorTextTitle, {COLOR_TITLE_BG});
l_surfaceTmp = SDL_utils::renderText(m_fonts, m_currentPath, Globals::g_colorTextTitle, {COLOR_TITLE_BG});
if (l_surfaceTmp->w > PANEL_SIZE * screen.ppu_x)
{
l_rect.x = l_surfaceTmp->w - PANEL_SIZE * screen.ppu_x;
Expand Down Expand Up @@ -120,7 +120,7 @@ void CPanel::render(const bool p_active) const
static const SDL_Color kLineBg[2] = {{COLOR_BG_1}, {COLOR_BG_2}};
l_bg = kLineBg[(l_i - m_camera) % 2];
}
l_surfaceTmp = SDL_utils::renderText(m_font, m_fileLister[l_i].m_name, *l_color, l_bg);
l_surfaceTmp = SDL_utils::renderText(m_fonts, m_fileLister[l_i].m_name, *l_color, l_bg);
if (l_surfaceTmp->w > NAME_SIZE * screen.ppu_x)
{
l_rect.x = 0;
Expand Down Expand Up @@ -148,8 +148,8 @@ void CPanel::render(const bool p_active) const
l_footer = l_s.str();
File_utils::formatSize(l_footer);
}
SDL_utils::applyText(m_x + 2, FOOTER_Y + FOOTER_PADDING_TOP, Globals::g_screen, m_font, "Size:", Globals::g_colorTextTitle, {COLOR_TITLE_BG});
SDL_utils::applyText(m_x + PANEL_SIZE - 2, FOOTER_Y + FOOTER_PADDING_TOP, Globals::g_screen, m_font, l_footer, Globals::g_colorTextTitle, {COLOR_TITLE_BG}, SDL_utils::T_TEXT_ALIGN_RIGHT);
SDL_utils::applyText(m_x + 2, FOOTER_Y + FOOTER_PADDING_TOP, Globals::g_screen, m_fonts, "Size:", Globals::g_colorTextTitle, {COLOR_TITLE_BG});
SDL_utils::applyText(m_x + PANEL_SIZE - 2, FOOTER_Y + FOOTER_PADDING_TOP, Globals::g_screen, m_fonts, l_footer, Globals::g_colorTextTitle, {COLOR_TITLE_BG}, SDL_utils::T_TEXT_ALIGN_RIGHT);
}

const bool CPanel::moveCursorUp(unsigned char p_step)
Expand Down
2 changes: 1 addition & 1 deletion panel.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class CPanel
SDL_Surface *m_iconUp;
SDL_Surface *m_cursor1;
SDL_Surface *m_cursor2;
TTF_Font *m_font;
const std::vector<TTF_Font *> &m_fonts;
};

#endif
Binary file added res/DroidSansFallbackFull.ttf
Binary file not shown.
Binary file added res/FreeSans.ttf
Binary file not shown.
38 changes: 23 additions & 15 deletions resourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
#include "screen.h"
#include "sdlutils.h"

#ifndef FONT_PATH
#define FONT_PATH RES_DIR "wy_scorpio.ttf"
#endif

namespace {

SDL_Surface *LoadIcon(const char *path) {
Expand Down Expand Up @@ -40,8 +36,7 @@ CResourceManager& CResourceManager::instance(void)
return l_singleton;
}

CResourceManager::CResourceManager(void) :
m_font(NULL)
CResourceManager::CResourceManager(void)
{
// Load images
m_surfaces[T_SURFACE_FOLDER] = LoadIcon(RES_DIR "folder.png");
Expand All @@ -52,8 +47,23 @@ CResourceManager::CResourceManager(void) :
m_surfaces[T_SURFACE_UP] = LoadIcon(RES_DIR "up.png");
m_surfaces[T_SURFACE_CURSOR1] = SDL_utils::createImage(screen.w / 2 * screen.ppu_x, LINE_HEIGHT * screen.ppu_y, SDL_MapRGB(Globals::g_screen->format, COLOR_CURSOR_1));
m_surfaces[T_SURFACE_CURSOR2] = SDL_utils::createImage(screen.w / 2 * screen.ppu_x, LINE_HEIGHT * screen.ppu_y, SDL_MapRGB(Globals::g_screen->format, COLOR_CURSOR_2));
// Load font
m_font = SDL_utils::loadFont(FONT_PATH, 8);

// Load fonts
struct FontSpec {
const char *const path;
int size;
};
constexpr FontSpec kFonts[] = {FONTS};
constexpr std::size_t kFontsLen = sizeof(kFonts) / sizeof(kFonts[0]);
m_fonts.reserve(kFontsLen);
for (std::size_t i = 0; i < kFontsLen; ++i) {
auto *font = SDL_utils::loadFont(kFonts[i].path, kFonts[i].size);
if (font != nullptr) m_fonts.push_back(font);
}
if (m_fonts.empty()) {
std::cerr << "No fonts found!" << std::endl;
exit(1);
}
}

void CResourceManager::sdlCleanup(void)
Expand All @@ -70,19 +80,17 @@ void CResourceManager::sdlCleanup(void)
}
}
// Free font
if (m_font != NULL)
{
TTF_CloseFont(m_font);
m_font = NULL;
}
for (auto *font : m_fonts)
TTF_CloseFont(font);
m_fonts.clear();
}

SDL_Surface *CResourceManager::getSurface(const T_SURFACE p_surface) const
{
return m_surfaces[p_surface];
}

TTF_Font *CResourceManager::getFont(void) const
const std::vector<TTF_Font *> &CResourceManager::getFonts(void) const
{
return m_font;
return m_fonts;
}
8 changes: 5 additions & 3 deletions resourceManager.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef _RESOURCEMANAGER_H_
#define _RESOURCEMANAGER_H_

#include <vector>

#include <SDL.h>
#include <SDL_ttf.h>

Expand Down Expand Up @@ -32,8 +34,8 @@ class CResourceManager
// Get a loaded surface
SDL_Surface *getSurface(const T_SURFACE p_surface) const;

// Get the loaded font
TTF_Font *getFont(void) const;
// Get the loaded fonts
const std::vector<TTF_Font *> &getFonts(void) const;

private:

Expand All @@ -46,7 +48,7 @@ class CResourceManager
SDL_Surface *m_surfaces[NB_SURFACES];

// Font
TTF_Font *m_font;
std::vector<TTF_Font *> m_fonts;
};

#endif
Loading

0 comments on commit 69b5b55

Please sign in to comment.