shithub: cstory

Download patch

ref: 9973dddbbd6dc1d0e3d598ef4025b9c980d848e8
parent: a542a5c11d9ca451ed4ffeb5456150fc09cd96f6
author: Clownacy <Clownacy@users.noreply.github.com>
date: Thu Sep 17 09:07:23 EDT 2020

Restore the FreeType code

Now you can select either the FreeType fonts or the pre-rendered
fonts with CMake's 'FREETYPE_FONTS' option.

--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -22,6 +22,7 @@
 option(DEBUG_SAVE "Re-enable the ability to drag-and-drop save files onto the window" OFF)
 option(DOCONFIG "Compile a DoConfig clone tool - not useful for console ports" ON)
 option(LANCZOS_RESAMPLER "Use Lanczos filtering for audio resampling instead of linear-interpolation (Lanczos is more performance-intensive, but higher quality)" OFF)
+option(FREETYPE_FONTS "Use FreeType2 to render the DejaVu Mono (English) or Migu1M (Japanese) fonts, instead of using pre-rendered copies of Courier New (English) and MS Gothic (Japanese)" OFF)
 
 set(BACKEND_RENDERER "SDLTexture" CACHE STRING "Which renderer the game should use: 'OpenGL3' for an OpenGL 3.2 renderer, 'OpenGLES2' for an OpenGL ES 2.0 renderer, 'SDLTexture' for SDL2's hardware-accelerated Texture API, 'SDLSurface' for SDL2's software-rendered Surface API, 'Wii U' for the Wii U's hardware-accelerated GX2 API, or 'Software' for a handwritten software renderer")
 set(BACKEND_AUDIO "SDL2" CACHE STRING "Which audio backend the game should use: 'SDL2', 'miniaudio', 'WiiU-Hardware', 'WiiU-Software', or 'Null'")
@@ -278,6 +279,10 @@
 	target_compile_definitions(CSE2 PRIVATE LANCZOS_RESAMPLER)
 endif()
 
+if(FREETYPE_FONTS)
+	target_compile_definitions(CSE2 PRIVATE FREETYPE_FONTS)
+endif()
+
 if(PKG_CONFIG_STATIC_LIBS)
 	target_link_options(CSE2 PRIVATE "-static")
 endif()
@@ -570,41 +575,43 @@
 	endif()
 endif()
 
-if(NOT FORCE_LOCAL_LIBS)
-	find_package(Freetype)
+if(FREETYPE_FONTS)
+	if(NOT FORCE_LOCAL_LIBS)
+		find_package(Freetype)
 
-	if (PKG_CONFIG_FOUND)
-		pkg_check_modules(freetype2 QUIET IMPORTED_TARGET freetype2)
+		if (PKG_CONFIG_FOUND)
+			pkg_check_modules(freetype2 QUIET IMPORTED_TARGET freetype2)
+		endif()
 	endif()
-endif()
 
-if(TARGET PkgConfig::freetype2)
-	# pkg-config
-	if (PKG_CONFIG_STATIC_LIBS)
-		message(STATUS "Using system FreeType (pkg-config, static)")
-		target_compile_options(CSE2 PRIVATE ${freetype2_STATIC_CFLAGS})
-		target_link_libraries(CSE2 PRIVATE ${freetype2_STATIC_LDFLAGS})
+	if(TARGET PkgConfig::freetype2)
+		# pkg-config
+		if (PKG_CONFIG_STATIC_LIBS)
+			message(STATUS "Using system FreeType (pkg-config, static)")
+			target_compile_options(CSE2 PRIVATE ${freetype2_STATIC_CFLAGS})
+			target_link_libraries(CSE2 PRIVATE ${freetype2_STATIC_LDFLAGS})
+		else()
+			message(STATUS "Using system FreeType (pkg-config, dynamic)")
+			target_compile_options(CSE2 PRIVATE ${freetype2_CFLAGS})
+			target_link_libraries(CSE2 PRIVATE ${freetype2_LDFLAGS})
+		endif()
+	elseif(FREETYPE_FOUND)
+		message(STATUS "Using system FreeType (CMake)")
+		target_include_directories(CSE2 PRIVATE ${FREETYPE_INCLUDE_DIRS})
+		target_link_libraries(CSE2 PRIVATE ${FREETYPE_LIBRARIES})
 	else()
-		message(STATUS "Using system FreeType (pkg-config, dynamic)")
-		target_compile_options(CSE2 PRIVATE ${freetype2_CFLAGS})
-		target_link_libraries(CSE2 PRIVATE ${freetype2_LDFLAGS})
+		# Compile it ourselves
+		message(STATUS "Using local FreeType")
+		if(FORCE_LOCAL_LIBS)
+			set(CMAKE_DISABLE_FIND_PACKAGE_HarfBuzz ON CACHE INTERNAL "" FORCE)
+			set(CMAKE_DISABLE_FIND_PACKAGE_ZLIB ON CACHE INTERNAL "" FORCE)
+			set(CMAKE_DISABLE_FIND_PACKAGE_PNG ON CACHE INTERNAL "" FORCE)
+			set(CMAKE_DISABLE_FIND_PACKAGE_BZip2 ON CACHE INTERNAL "" FORCE)
+			set(CMAKE_DISABLE_FIND_PACKAGE_BrotliDec ON CACHE INTERNAL "" FORCE)
+		endif()
+		add_subdirectory("external/freetype" EXCLUDE_FROM_ALL)
+		target_link_libraries(CSE2 PRIVATE freetype)
 	endif()
-elseif(FREETYPE_FOUND)
-	message(STATUS "Using system FreeType (CMake)")
-	target_include_directories(CSE2 PRIVATE ${FREETYPE_INCLUDE_DIRS})
-	target_link_libraries(CSE2 PRIVATE ${FREETYPE_LIBRARIES})
-else()
-	# Compile it ourselves
-	message(STATUS "Using local FreeType")
-	if(FORCE_LOCAL_LIBS)
-		set(CMAKE_DISABLE_FIND_PACKAGE_HarfBuzz ON CACHE INTERNAL "" FORCE)
-		set(CMAKE_DISABLE_FIND_PACKAGE_ZLIB ON CACHE INTERNAL "" FORCE)
-		set(CMAKE_DISABLE_FIND_PACKAGE_PNG ON CACHE INTERNAL "" FORCE)
-		set(CMAKE_DISABLE_FIND_PACKAGE_BZip2 ON CACHE INTERNAL "" FORCE)
-		set(CMAKE_DISABLE_FIND_PACKAGE_BrotliDec ON CACHE INTERNAL "" FORCE)
-	endif()
-	add_subdirectory("external/freetype" EXCLUDE_FROM_ALL)
-	target_link_libraries(CSE2 PRIVATE freetype)
 endif()
 
 if(BACKEND_RENDERER MATCHES "OpenGL3")
--- a/src/Draw.cpp
+++ b/src/Draw.cpp
@@ -657,7 +657,8 @@
 void InitTextObject(const char *name)
 {
 	(void)name;	// Unused in this branch
-/*
+
+#ifdef FREETYPE_FONTS
 	std::string path = gDataPath + "/Font/font";
 
 	// Get font size
@@ -706,7 +707,7 @@
 	}
 
 	font = LoadFont(path.c_str(), width, height);
-*/
+#else
 	std::string bitmap_path;
 	std::string metadata_path;
 
@@ -724,6 +725,7 @@
 	}
 
 	font = LoadBitmapFont(bitmap_path.c_str(), metadata_path.c_str());
+#endif
 }
 
 void PutText(int x, int y, const char *text, unsigned long color)
--- a/src/Font.cpp
+++ b/src/Font.cpp
@@ -5,9 +5,11 @@
 #include <string.h>
 #include <math.h>
 
-#include <ft2build.h>
-#include FT_FREETYPE_H
-#include FT_BITMAP_H
+#ifdef FREETYPE_FONTS
+	#include <ft2build.h>
+	#include FT_FREETYPE_H
+	#include FT_BITMAP_H
+#endif
 
 #include "Bitmap.h"
 #include "File.h"
@@ -46,6 +48,11 @@
 
 typedef struct Font
 {
+#ifdef FREETYPE_FONTS
+	FT_Library library;
+	FT_Face face;
+	unsigned char *data;
+#else
 	unsigned char *image_buffer;
 	size_t image_buffer_width;
 	size_t image_buffer_height;
@@ -53,11 +60,7 @@
 	size_t glyph_slot_height;
 	size_t total_local_glyphs;
 	Glyph *local_glyphs;
-/*
-	FT_Library library;
-	FT_Face face;
-	unsigned char *data;
-*/
+#endif
 	Glyph glyphs[TOTAL_GLYPH_SLOTS];
 	Glyph *glyph_list_head;
 	RenderBackend_GlyphAtlas *atlas;
@@ -999,34 +1002,7 @@
 	// Couldn't find glyph - overwrite the old at the end.
 	// The one at the end hasn't been used in a while anyway.
 
-	for (size_t i = 0; i < font->total_local_glyphs; ++i)
-	{
-		if (font->local_glyphs[i].unicode_value == unicode_value)
-		{
-			glyph->unicode_value = font->local_glyphs[i].unicode_value;
-			glyph->width = font->local_glyphs[i].width;
-			glyph->height = font->local_glyphs[i].height;
-			glyph->x_offset = font->local_glyphs[i].x_offset;
-			glyph->y_offset = font->local_glyphs[i].y_offset;
-			glyph->x_advance = font->local_glyphs[i].x_advance;
-
-			RenderBackend_UploadGlyph(font->atlas, glyph->x, glyph->y, &font->image_buffer[font->local_glyphs[i].y * font->image_buffer_width + font->local_glyphs[i].x], glyph->width, glyph->height, font->image_buffer_width);
-
-			*glyph_pointer = glyph->next;
-			glyph->next = font->glyph_list_head;
-			font->glyph_list_head = glyph;
-
-			return glyph;
-		}
-	}
-
-
-
-
-
-
-
-/*
+#ifdef FREETYPE_FONTS
 	unsigned int glyph_index = FT_Get_Char_Index(font->face, unicode_value);
 
 #ifdef ENABLE_FONT_ANTIALIASING
@@ -1078,7 +1054,7 @@
 			glyph->y_offset = (font->face->size->metrics.ascender + (64 / 2)) / 64 - font->face->glyph->bitmap_top;
 			glyph->x_advance = font->face->glyph->advance.x / 64;
 
-			RenderBackend_UploadGlyph(font->atlas, glyph->x, glyph->y, bitmap.buffer, glyph->width, glyph->height);
+			RenderBackend_UploadGlyph(font->atlas, glyph->x, glyph->y, bitmap.buffer, glyph->width, glyph->height, glyph->width);
 
 			FT_Bitmap_Done(font->library, &bitmap);
 
@@ -1091,12 +1067,33 @@
 
 		FT_Bitmap_Done(font->library, &bitmap);
 	}
-*/
+#else
+	for (size_t i = 0; i < font->total_local_glyphs; ++i)
+	{
+		if (font->local_glyphs[i].unicode_value == unicode_value)
+		{
+			glyph->unicode_value = font->local_glyphs[i].unicode_value;
+			glyph->width = font->local_glyphs[i].width;
+			glyph->height = font->local_glyphs[i].height;
+			glyph->x_offset = font->local_glyphs[i].x_offset;
+			glyph->y_offset = font->local_glyphs[i].y_offset;
+			glyph->x_advance = font->local_glyphs[i].x_advance;
 
+			RenderBackend_UploadGlyph(font->atlas, glyph->x, glyph->y, &font->image_buffer[font->local_glyphs[i].y * font->image_buffer_width + font->local_glyphs[i].x], glyph->width, glyph->height, font->image_buffer_width);
+
+			*glyph_pointer = glyph->next;
+			glyph->next = font->glyph_list_head;
+			font->glyph_list_head = glyph;
+
+			return glyph;
+		}
+	}
+#endif
+
 	return NULL;
 }
 
-/*
+#ifdef FREETYPE_FONTS
 Font* LoadFontFromData(const unsigned char *data, size_t data_size, size_t cell_width, size_t cell_height)
 {
 	Font *font = (Font*)malloc(sizeof(Font));
@@ -1173,8 +1170,7 @@
 
 	return font;
 }
-*/
-
+#else
 Font* LoadBitmapFont(const char *bitmap_path, const char *metadata_path)
 {
 	size_t bitmap_width, bitmap_height;
@@ -1261,6 +1257,7 @@
 
 	return NULL;
 }
+#endif
 
 void DrawText(Font *font, RenderBackend_Surface *surface, int x, int y, unsigned long colour, const char *string)
 {
@@ -1303,12 +1300,15 @@
 	if (font != NULL)
 	{
 		RenderBackend_DestroyGlyphAtlas(font->atlas);
+
+	#ifdef FREETYPE_FONTS
+		FT_Done_Face(font->face);
+		free(font->data);
+		FT_Done_FreeType(font->library);
+	#else
 		free(font->local_glyphs);
 		FreeBitmap(font->image_buffer);
-
-//		FT_Done_Face(font->face);
-//		free(font->data);
-//		FT_Done_FreeType(font->library);
+	#endif
 
 		free(font);
 	}
--- a/src/Font.h
+++ b/src/Font.h
@@ -6,8 +6,12 @@
 
 typedef struct Font Font;
 
-//Font* LoadFontFromData(const unsigned char *data, size_t data_size, size_t cell_width, size_t cell_height);
-//Font* LoadFont(const char *font_filename, size_t cell_width, size_t cell_height);
+#ifdef FREETYPE_FONTS
+Font* LoadFontFromData(const unsigned char *data, size_t data_size, size_t cell_width, size_t cell_height);
+Font* LoadFont(const char *font_filename, size_t cell_width, size_t cell_height);
+#else
 Font* LoadBitmapFont(const char *bitmap_path, const char *metadata_path);
+#endif
+
 void DrawText(Font *font, RenderBackend_Surface *surface, int x, int y, unsigned long colour, const char *string);
 void UnloadFont(Font *font);