From 602bc380f810c78334cfee3576c0d50c248f3c46 Mon Sep 17 00:00:00 2001 From: Julius Ikkala Date: Sat, 7 Jan 2023 18:14:32 +0200 Subject: [PATCH] Enable full Unicode range in SDL2 port (#270) * Enabled full Unicode range in SDL2 * Adjust dependencies and allow older SDL2-ttf versions * Try to fix Visual Studio builds with new SDL_ttf * Update SDL2 port README * Clean up SDL2 glyph rendering code --- cmake/build_dependencies.cmake | 23 +++++---- cmake/sdl2_ttf/CMakeLists.txt | 12 +++++ sdl2/README.md | 16 ++++--- sdl2/pdcdisp.c | 85 ++++++++++++++++++---------------- sdl2/pdcsdl.h | 5 ++ 5 files changed, 85 insertions(+), 56 deletions(-) diff --git a/cmake/build_dependencies.cmake b/cmake/build_dependencies.cmake index 7f5657ec..bf875704 100644 --- a/cmake/build_dependencies.cmake +++ b/cmake/build_dependencies.cmake @@ -7,10 +7,11 @@ IF (PDC_SDL2_DEPS_BUILD) set(FLAGS_FOR_DYNAMIC_LINK -fPIC) ENDIF() - SET(SDL2_RELEASE 2.0.12) + SET(SDL2_RELEASE 2.26.2) ExternalProject_Add(sdl2_ext - URL https://www.libsdl.org/release/SDL2-${SDL2_RELEASE}.zip - URL_HASH "SHA256=476e84d6fcbc499cd1f4a2d3fd05a924abc165b5d0e0d53522c9604fe5a021aa" + GIT_REPOSITORY "https://github.com/libsdl-org/SDL.git" + GIT_TAG "release-${SDL2_RELEASE}" + GIT_SHALLOW true UPDATE_COMMAND "" DOWNLOAD_DIR ${CMAKE_BINARY_DIR} SOURCE_DIR ${CMAKE_BINARY_DIR}/SDL2-${SDL2_RELEASE} @@ -89,7 +90,7 @@ IF (PDC_SDL2_DEPS_BUILD) ExternalProject_Add(freetype2_ext GIT_REPOSITORY "https://git.savannah.gnu.org/git/freetype/freetype2.git" - GIT_TAG "VER-2-8-1" + GIT_TAG "VER-2-12-1" GIT_SHALLOW true UPDATE_COMMAND "" DOWNLOAD_DIR ${CMAKE_BINARY_DIR} @@ -101,9 +102,10 @@ IF (PDC_SDL2_DEPS_BUILD) -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}/${CMAKE_BUILD_TYPE} -DCMAKE_C_FLAGS=${FLAGS_FOR_DYNAMIC_LINK} ${EXTERNAL_C_FLAGS} -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} - -DWITH_HarfBuzz=OFF - -DWITH_BZip2=OFF - -DWITH_PNG=OFF + -DFT_DISABLE_HARFBUZZ=ON + -DFT_DISABLE_BZIP2=ON + -DFT_DISABLE_PNG=ON + -DFT_DISABLE_BROTLI=ON -DWITH_ZLIB=ON -DZLIB_FOUND=ON -DZLIB_LIBRARY=${ZLIB_LIBRARY} @@ -129,11 +131,12 @@ IF (PDC_SDL2_DEPS_BUILD) ENDIF() ENDIF() - SET(SDL2_TTF_RELEASE 2.0.15) + SET(SDL2_TTF_RELEASE 2.20.1) ExternalProject_Add(sdl2_ttf_ext - URL https://www.libsdl.org/projects/SDL_ttf/release/SDL2_ttf-${SDL2_TTF_RELEASE}.zip - URL_HASH "SHA256=cdb72b5b1c3b27795fa128af36f369fee5d3e38a96c350855da0b81880555dbc" + GIT_REPOSITORY "https://github.com/libsdl-org/SDL_ttf.git" + GIT_TAG "release-${SDL2_TTF_RELEASE}" + GIT_SHALLOW true PATCH_COMMAND cmake -E copy ${CMAKE_SOURCE_DIR}/cmake/sdl2_ttf/CMakeLists.txt ${CMAKE_BINARY_DIR}/sdl2_ttf/CMakeLists.txt diff --git a/cmake/sdl2_ttf/CMakeLists.txt b/cmake/sdl2_ttf/CMakeLists.txt index 38e61d12..5ed210bb 100644 --- a/cmake/sdl2_ttf/CMakeLists.txt +++ b/cmake/sdl2_ttf/CMakeLists.txt @@ -60,12 +60,24 @@ if (APPLE) endif() ADD_LIBRARY(SDL2_ttf-static STATIC ${SDL2_ttf_source_files} SDL_ttf.h) +SET_TARGET_PROPERTIES (SDL2_ttf-static PROPERTIES + DEFINE_SYMBOL DLL_EXPORT + PUBLIC_HEADER SDL_ttf.h + EXPORT_NAME SDL2_ttf-static + C_VISIBILITY_PRESET "hidden" +) INSTALL (TARGETS SDL2_ttf-static ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib COMPONENT libraries) ADD_LIBRARY(SDL2_ttf SHARED ${SDL2_ttf_source_files} SDL_ttf.h) TARGET_LINK_LIBRARIES (SDL2_ttf ${SDL2_LIBRARY} ${FT2_LIBRARY} ${ZLIB_LIBRARY} ${EXTRA_LIBS}) +SET_TARGET_PROPERTIES (SDL2_ttf PROPERTIES + DEFINE_SYMBOL DLL_EXPORT + PUBLIC_HEADER SDL_ttf.h + EXPORT_NAME SDL2_ttf + C_VISIBILITY_PRESET "hidden" +) INSTALL (TARGETS SDL2_ttf ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin diff --git a/sdl2/README.md b/sdl2/README.md index c1540cbd..17d15726 100644 --- a/sdl2/README.md +++ b/sdl2/README.md @@ -19,7 +19,7 @@ Building - The makefile recognizes the optional PDCURSES_SRCDIR environment variable, and the option "DEBUG=Y", as with the console ports. - "WIDE=Y" builds a version that not only uses 16-bit Unicode + "WIDE=Y" builds a version that not only uses 32-bit Unicode characters, but depends on the SDL2_ttf library, instead of using simple bitmap fonts. "UTF8=Y" makes PDCurses ignore the system locale, and treat all narrow-character strings as UTF-8; this option has no @@ -67,15 +67,17 @@ directory at the time of initscr(). If neither is found, it uses the built-in default font encoded in font437.h. -### 16-bit mode +### 32-bit mode Instead of a BMP, PDC_FONT points to a TrueType font. Only true monospaced fonts work well. The font can be set at compile time via PDC_FONT_PATH, and/or at runtime via pdc_ttffont. The environment variable PDC_FONT_SIZE is also available to control the font size (also as a compile-time define, and at runtime as pdc_font_size.) The -character mapping for chtypes is 16-bit Unicode (the Basic Multilingual -Plane). +character mapping for chtypes is UTF-32. However, with SDL2_ttf versions +older than 2.0.18, only the Basic Multilingual Plane characters are +available. + The default font (if not redefined) is based on the OS: @@ -157,10 +159,10 @@ for the font, icon, and background, respectively. You can set any or all of them before initscr(), and thus override any of the other ways to set them. But note that pdc_icon will be ignored if pdc_screen is preset. -pdc_sdl_render_mode (in 16-bit mode) can be set to `PDC_SDL_RENDER_SOLID`, +pdc_sdl_render_mode (in 32-bit mode) can be set to `PDC_SDL_RENDER_SOLID`, `PDC_SDL_RENDER_SHADED` or `PDC_SDL_RENDER_BLENDED`. This determines which SDL TTF -render mode will be used for rendering text: `TTF_RenderUNICODE_Solid()`, -`TTF_RenderUNICODE_Shaded()` or `TTF_RenderUNICODE_Blended()` respectively. +render mode will be used for rendering text: `TTF_RenderGlyph32_Solid()`, +`TTF_RenderGlyph32_Shaded()` or `TTF_RenderGlyph32_Blended()` respectively. This will default to `PDC_SDL_RENDER_BLENDED`. If you wish to use this feature without including `pdcsdl.h`, you must define the following constants: diff --git a/sdl2/pdcdisp.c b/sdl2/pdcdisp.c index 35f4f65a..80a38c40 100644 --- a/sdl2/pdcdisp.c +++ b/sdl2/pdcdisp.c @@ -242,6 +242,47 @@ static bool _grprint(chtype ch, const SDL_Rect dest) rval = FALSE; /* didn't draw it -- fall back to acs_map */ return( rval); } + +static SDL_Surface *_render_ttf_glyph(chtype ch) +{ + ch &= A_CHARTEXT; + +#ifdef PDC_SDL_SUPPLEMENTARY_PLANES_SUPPORT + switch (pdc_sdl_render_mode) + { + case PDC_SDL_RENDER_SOLID: + return TTF_RenderGlyph32_Solid(pdc_ttffont, (Uint32)ch, + *(get_pdc_color( foregr))); + case PDC_SDL_RENDER_SHADED: + return TTF_RenderGlyph32_Shaded(pdc_ttffont, (Uint32)ch, + *(get_pdc_color( foregr)), + *(get_pdc_color( backgr))); + default: + return TTF_RenderGlyph32_Blended(pdc_ttffont, (Uint32)ch, + *(get_pdc_color( foregr))); + } +#else + /* no support for supplementary planes */ + if (ch > 0xffff) + ch = '?'; + + switch (pdc_sdl_render_mode) + { + case PDC_SDL_RENDER_SOLID: + return TTF_RenderGlyph_Solid(pdc_ttffont, (Uint16)ch, + *(get_pdc_color( foregr))); + break; + case PDC_SDL_RENDER_SHADED: + return TTF_RenderGlyph_Shaded(pdc_ttffont, (Uint16)ch, + *(get_pdc_color( foregr)), + *(get_pdc_color( backgr))); + default: + return TTF_RenderGlyph_Blended(pdc_ttffont, (Uint16)ch, + *(get_pdc_color( foregr))); + } +#endif +} + #endif /* draw a cursor at (y, x) */ @@ -251,9 +292,6 @@ void PDC_gotoyx(int row, int col) SDL_Rect src, dest; chtype ch; int oldrow, oldcol; -#ifdef PDC_WIDE - Uint16 chstr[2] = {0, 0}; -#endif PDC_LOG(("PDC_gotoyx() - called: row %d col %d from row %d col %d\n", row, col, SP->cursrow, SP->curscol)); @@ -296,23 +334,7 @@ void PDC_gotoyx(int row, int col) if( _is_altcharset( ch)) ch = acs_map[ch & 0x7f]; - chstr[0] = (Uint16)( ch & A_CHARTEXT); - - switch (pdc_sdl_render_mode) - { - case PDC_SDL_RENDER_SOLID: - pdc_font = TTF_RenderUNICODE_Solid(pdc_ttffont, chstr, - *(get_pdc_color( foregr))); - break; - case PDC_SDL_RENDER_SHADED: - pdc_font = TTF_RenderUNICODE_Shaded(pdc_ttffont, chstr, - *(get_pdc_color( foregr)), - *(get_pdc_color( backgr))); - break; - default: - pdc_font = TTF_RenderUNICODE_Blended(pdc_ttffont, chstr, - *(get_pdc_color( foregr))); - } + pdc_font = _render_ttf_glyph(ch); if (pdc_font) { @@ -387,7 +409,7 @@ void _new_packet(attr_t attr, int lineno, int x, int len, const chtype *srcp) SDL_Rect src, dest; int j; #ifdef PDC_WIDE - Uint16 chstr[2] = {0, 0}; + chtype prev_ch = 0; #endif attr_t sysattrs = SP->termattrs; short hcol = SP->line_color; @@ -455,28 +477,13 @@ void _new_packet(attr_t attr, int lineno, int x, int len, const chtype *srcp) if (ch != ' ') { - if (chstr[0] != ch) + if (prev_ch != ch) { - chstr[0] = (Uint16)ch; - + prev_ch = ch; if (pdc_font) SDL_FreeSurface(pdc_font); - switch (pdc_sdl_render_mode) - { - case PDC_SDL_RENDER_SOLID: - pdc_font = TTF_RenderUNICODE_Solid(pdc_ttffont, chstr, - *(get_pdc_color( foregr))); - break; - case PDC_SDL_RENDER_SHADED: - pdc_font = TTF_RenderUNICODE_Shaded(pdc_ttffont, chstr, - *(get_pdc_color( foregr)), - *(get_pdc_color( backgr))); - break; - default: - pdc_font = TTF_RenderUNICODE_Blended(pdc_ttffont, chstr, - *(get_pdc_color( foregr))); - } + pdc_font = _render_ttf_glyph(ch); } if (pdc_font) diff --git a/sdl2/pdcsdl.h b/sdl2/pdcsdl.h index ccfdf1ac..a288ce33 100644 --- a/sdl2/pdcsdl.h +++ b/sdl2/pdcsdl.h @@ -7,6 +7,11 @@ #include #ifdef PDC_WIDE # include +#if SDL_TTF_VERSION_ATLEAST(2,0,18) +/* SDL_ttf 2.0.18 introduced the the functions we use for rendering + individual characters beyond the BMP. */ +#define PDC_SDL_SUPPLEMENTARY_PLANES_SUPPORT 1 +#endif #endif #include