Skip to content

Commit

Permalink
Merge pull request #1 from R-YaTian/main
Browse files Browse the repository at this point in the history
Port new font render@ thanks for your work @R-YaTian
  • Loading branch information
dezem committed Jan 29, 2022
2 parents 53bc5c3 + 234265f commit 7874dea
Show file tree
Hide file tree
Showing 50 changed files with 3,860 additions and 3,177 deletions.
4 changes: 2 additions & 2 deletions Plutonium/Include/pu/Plutonium
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/

#pragma once
#include <pu/pu_CFW.hpp>
#include <pu/pu_String.hpp>

#include <pu/audio/audio_Music.hpp>
#include <pu/audio/audio_Sfx.hpp>
Expand All @@ -36,4 +36,4 @@
#include <pu/ui/extras/extras_Toast.hpp>

#include <pu/ui/render/render_Renderer.hpp>
#include <pu/ui/render/render_SDL2.hpp>
#include <pu/ui/render/render_SDL2.hpp>
20 changes: 0 additions & 20 deletions Plutonium/Include/pu/pu_CFW.hpp

This file was deleted.

4 changes: 4 additions & 0 deletions Plutonium/Include/pu/pu_Macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#pragma once

#include <memory>
#include <switch.h>

// Defines a static function (::New(...)) as a constructor for smart ptrs, also defines a custom type (::Ref) to simplify it
#define PU_SMART_CTOR(type) \
Expand All @@ -11,3 +12,6 @@ static Ref New(Args &&...CtorArgs) \
{ \
return std::move(std::make_shared<type>(std::forward<Args>(CtorArgs)...)); \
}

// For convenience
using i32 = s32;
113 changes: 113 additions & 0 deletions Plutonium/Include/pu/pu_String.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@

/*
Plutonium library
@file pu_String.hpp
@brief A custom string class to support UTF16 and UTF8 -> UTF16 conversion
@author XorTroll
@copyright Plutonium project - an easy-to-use UI framework for Nintendo Switch homebrew
*/

#pragma once
#include <string>
#include <switch.h>
#include <pu/pu_Macros.hpp>

namespace pu
{
class String
{
using iterator = std::u16string::iterator;

public:
String();
static const size_t npos;

String(const char *C_UTF8);
bool operator==(const char *C_UTF8);
bool operator!=(const char *C_UTF8);
String operator+=(const char *C_UTF8);

String(const char16_t *C_UTF16);
bool operator==(const char16_t *C_UTF16);
bool operator!=(const char16_t *C_UTF16);
String operator+=(const char16_t *C_UTF16);

String(std::string UTF8);
bool operator==(std::string UTF8);
bool operator!=(std::string UTF8);
String operator+=(std::string UTF8);
std::string AsUTF8();

String(std::u16string UTF16);
bool operator==(std::u16string UTF16);
bool operator!=(std::u16string UTF16);
String operator+=(std::u16string UTF16);
std::u16string AsUTF16();

bool operator==(String Str);
bool operator!=(String Str);
String operator+=(String Str);

bool StartsWith(String Str);
bool IsEmpty();
bool HasAny();
size_t GetLength();
String Substring(size_t Index, size_t Length);

// C++ string compatibility

bool empty();
size_t length();
String substr(size_t Offset, size_t Length = npos);
size_t find_first_of(String Str, size_t Position = 0);
size_t find_last_of(String Str, size_t Position = npos);
void erase(size_t Offset, size_t Length = npos);
iterator begin();
iterator end();
size_t find(String Str, size_t Position = 0);
String replace(size_t Position, size_t Length, String Str);
void reserve(size_t Length);

friend String operator+(const String &L, char R);
friend String operator+(const String &L, const char *R);
friend String operator+(const String &L, const char16_t *R);
friend String operator+(const String &L, const std::string R);
friend String operator+(const String &L, const std::u16string R);
friend String operator+(const String &L, const String &R);

friend String operator+(const String &L, u8 U);
friend String operator+(const String &L, u16 U);
friend String operator+(const String &L, u32 U);
friend String operator+(const String &L, u64 U);
friend String operator+(const String &L, s8 I);
friend String operator+(const String &L, s16 I);
friend String operator+(const String &L, i32 I);
friend String operator+(const String &L, s64 I);
friend String operator+(const String &L, float D);
friend String operator+(const String &L, double D);

String operator+=(char C);
String operator+=(u8 U);
String operator+=(u16 U);
String operator+=(u32 U);
String operator+=(u64 U);
String operator+=(s8 I);
String operator+=(s16 I);
String operator+=(i32 I);
String operator+=(s64 I);
String operator+=(float D);
String operator+=(double D);
private:
std::u16string base;
};
}

namespace std
{
// More compatibility
int stoi(pu::String Str, size_t *Index = 0, int Base = 10);
}
16 changes: 16 additions & 0 deletions Plutonium/Include/pu/sdl2/sdl2_Types.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

#pragma once
#include <SDL2/SDL.h>
#include <SDL2/SDL2_gfxPrimitives.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
#include <pu/sdl2/ttf_SDL_ttf.h>

namespace pu::sdl2
{
using Texture = SDL_Texture*;
using Window = SDL_Window*;
using Renderer = SDL_Renderer*;
using Font = TTF_Font*;
using Surface = SDL_Surface*;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
SDL_ttf: A companion library to SDL for working with TrueType (tm) fonts
Copyright (C) 2001-2019 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 2001-2013 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
Expand All @@ -24,16 +24,14 @@
http://www.freetype.org/
*/

/* Note: In many places, SDL_ttf will say "glyph" when it means "code point."
Unicode is hard, we learn as we go, and we apologize for adding to the
confusion. */

#ifndef SDL_TTF_H_
#define SDL_TTF_H_
#ifndef _SDL_TTF_H
#define _SDL_TTF_H

#include <SDL2/SDL.h>
#include <SDL2/begin_code.h>

#include <switch.h>

/* Set up for C function definitions, even when using C++ */
#ifdef __cplusplus
extern "C" {
Expand All @@ -43,7 +41,7 @@ extern "C" {
*/
#define SDL_TTF_MAJOR_VERSION 2
#define SDL_TTF_MINOR_VERSION 0
#define SDL_TTF_PATCHLEVEL 15
#define SDL_TTF_PATCHLEVEL 12

/* This macro can be used to fill a version structure with the compile-time
* version of the SDL_ttf library.
Expand All @@ -61,23 +59,6 @@ extern "C" {
#define TTF_PATCHLEVEL SDL_TTF_PATCHLEVEL
#define TTF_VERSION(X) SDL_TTF_VERSION(X)

/**
* This is the version number macro for the current SDL_ttf version.
*/
#define SDL_TTF_COMPILEDVERSION \
SDL_VERSIONNUM(SDL_TTF_MAJOR_VERSION, SDL_TTF_MINOR_VERSION, SDL_TTF_PATCHLEVEL)

/**
* This macro will evaluate to true if compiled with SDL_ttf at least X.Y.Z.
*/
#define SDL_TTF_VERSION_ATLEAST(X, Y, Z) \
(SDL_TTF_COMPILEDVERSION >= SDL_VERSIONNUM(X, Y, Z))

/* Make sure this is defined (only available in newer SDL versions) */
#ifndef SDL_DEPRECATED
#define SDL_DEPRECATED
#endif

/* This function gets the version of the dynamically linked SDL_ttf library.
it should NOT be used to fill a version structure, instead you should
use the SDL_TTF_VERSION() macro.
Expand Down Expand Up @@ -168,21 +149,21 @@ extern DECLSPEC int SDLCALL TTF_GlyphMetrics(TTF_Font *font, Uint16 ch,
int *miny, int *maxy, int *advance);

/* Get the dimensions of a rendered string of text */
extern DECLSPEC int SDLCALL TTF_SizeText(TTF_Font *font, TTF_Font *meme, const char *text, int *w, int *h);
extern DECLSPEC int SDLCALL TTF_SizeUTF8(TTF_Font *font, TTF_Font *meme, const char *text, int *w, int *h);
extern DECLSPEC int SDLCALL TTF_SizeUNICODE(TTF_Font *font, TTF_Font *meme, const Uint16 *text, int *w, int *h);
extern DECLSPEC int SDLCALL TTF_SizeText(TTF_Font *font, const char *text, int *w, int *h);
extern DECLSPEC int SDLCALL TTF_SizeUTF8(TTF_Font *font, const char *text, int *w, int *h);
extern DECLSPEC int SDLCALL TTF_SizeUNICODE(TTF_Font *font, const Uint16 *text, int *w, int *h);

/* Create an 8-bit palettized surface and render the given text at
fast quality with the given font and color. The 0 pixel is the
colorkey, giving a transparent background, and the 1 pixel is set
to the text color.
This function returns the new surface, or NULL if there was an error.
*/
extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderText_Solid(TTF_Font *font, TTF_Font *meme,
extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderText_Solid(TTF_Font *font,
const char *text, SDL_Color fg);
extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUTF8_Solid(TTF_Font *font, TTF_Font *meme,
extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUTF8_Solid(TTF_Font *font,
const char *text, SDL_Color fg);
extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUNICODE_Solid(TTF_Font *font, TTF_Font *meme,
extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUNICODE_Solid(TTF_Font *font,
const Uint16 *text, SDL_Color fg);

/* Create an 8-bit palettized surface and render the given glyph at
Expand All @@ -192,19 +173,19 @@ extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUNICODE_Solid(TTF_Font *font, TT
centering in the X direction, and aligned normally in the Y direction.
This function returns the new surface, or NULL if there was an error.
*/
extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderGlyph_Solid(TTF_Font *font, TTF_Font *meme,
extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderGlyph_Solid(TTF_Font *font,
Uint16 ch, SDL_Color fg);

/* Create an 8-bit palettized surface and render the given text at
high quality with the given font and colors. The 0 pixel is background,
while other pixels have varying degrees of the foreground color.
This function returns the new surface, or NULL if there was an error.
*/
extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderText_Shaded(TTF_Font *font, TTF_Font *meme,
extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderText_Shaded(TTF_Font *font,
const char *text, SDL_Color fg, SDL_Color bg);
extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUTF8_Shaded(TTF_Font *font, TTF_Font *meme,
extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUTF8_Shaded(TTF_Font *font,
const char *text, SDL_Color fg, SDL_Color bg);
extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUNICODE_Shaded(TTF_Font *font, TTF_Font *meme,
extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUNICODE_Shaded(TTF_Font *font,
const Uint16 *text, SDL_Color fg, SDL_Color bg);

/* Create an 8-bit palettized surface and render the given glyph at
Expand All @@ -214,18 +195,18 @@ extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUNICODE_Shaded(TTF_Font *font, T
direction, and aligned normally in the Y direction.
This function returns the new surface, or NULL if there was an error.
*/
extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderGlyph_Shaded(TTF_Font *font, TTF_Font *meme,
extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderGlyph_Shaded(TTF_Font *font,
Uint16 ch, SDL_Color fg, SDL_Color bg);

/* Create a 32-bit ARGB surface and render the given text at high quality,
using alpha blending to dither the font with the given color.
This function returns the new surface, or NULL if there was an error.
*/
extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderText_Blended(TTF_Font *font, TTF_Font *meme,
extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderText_Blended(TTF_Font *font,
const char *text, SDL_Color fg);
extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUTF8_Blended(TTF_Font *font, TTF_Font *meme,
extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUTF8_Blended(TTF_Font *font,
const char *text, SDL_Color fg);
extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUNICODE_Blended(TTF_Font *font, TTF_Font *meme,
extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUNICODE_Blended(TTF_Font *font,
const Uint16 *text, SDL_Color fg);


Expand All @@ -235,11 +216,11 @@ extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUNICODE_Blended(TTF_Font *font,
if it extends beyond wrapLength in pixels.
This function returns the new surface, or NULL if there was an error.
*/
extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderText_Blended_Wrapped(TTF_Font *font, TTF_Font *meme,
extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderText_Blended_Wrapped(TTF_Font *font,
const char *text, SDL_Color fg, Uint32 wrapLength);
extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUTF8_Blended_Wrapped(TTF_Font *font, TTF_Font *meme,
extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUTF8_Blended_Wrapped(TTF_Font *font,
const char *text, SDL_Color fg, Uint32 wrapLength);
extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUNICODE_Blended_Wrapped(TTF_Font *font, TTF_Font *meme,
extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUNICODE_Blended_Wrapped(TTF_Font *font,
const Uint16 *text, SDL_Color fg, Uint32 wrapLength);

/* Create a 32-bit ARGB surface and render the given glyph at high quality,
Expand All @@ -248,7 +229,7 @@ extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUNICODE_Blended_Wrapped(TTF_Font
direction, and aligned normally in the Y direction.
This function returns the new surface, or NULL if there was an error.
*/
extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderGlyph_Blended(TTF_Font *font, TTF_Font *meme,
extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderGlyph_Blended(TTF_Font *font,
Uint16 ch, SDL_Color fg);

/* For compatibility with previous versions, here are the old functions */
Expand All @@ -268,27 +249,28 @@ extern DECLSPEC void SDLCALL TTF_Quit(void);
/* Check if the TTF engine is initialized */
extern DECLSPEC int SDLCALL TTF_WasInit(void);

/* Get the kerning size of two glyphs indices */
/* DEPRECATED: this function requires FreeType font indexes, not glyphs,
by accident, which we don't expose through this API, so it could give
wildly incorrect results, especially with non-ASCII values.
Going forward, please use TTF_GetFontKerningSizeGlyphs() instead, which
does what you probably expected this function to do. */
extern DECLSPEC int TTF_GetFontKerningSize(TTF_Font *font, int prev_index, int index) SDL_DEPRECATED;

/* Get the kerning size of two glyphs */
extern DECLSPEC int TTF_GetFontKerningSizeGlyphs(TTF_Font *font, Uint16 previous_ch, Uint16 ch);
extern DECLSPEC int TTF_GetFontKerningSize(TTF_Font *font, int prev_index, int index);

/* Code present in C++ code */
TTF_Font *TTF_CppWrap_FindValidFont(TTF_Font *font, Uint16 ch);

/* Get the pointer to the C++ data */
void *TTF_CppWrap_GetCppPtrRef(TTF_Font *font);

/* Set the pointer to the C++ data */
void TTF_CppWrap_SetCppPtrRef(TTF_Font *font, void *cpp_ptr_ref);

/* We'll use SDL for reporting errors */
#define TTF_SetError SDL_SetError
#define TTF_GetError SDL_GetError

#define TMP_LOG(str) { const char *cstr = str; svcOutputDebugString(cstr, strlen(cstr)); }

/* Ends C function definitions when using C++ */
#ifdef __cplusplus
}
#endif
#include <SDL2/close_code.h>

#endif /* SDL_TTF_H_ */

/* vi: set ts=4 sw=4 expandtab: */
#endif /* _SDL_TTF_H */

0 comments on commit 7874dea

Please sign in to comment.