From a6ac6787d3d884b4bc9e58a8c385da25d0413fe7 Mon Sep 17 00:00:00 2001
From: Clownacy <Clownacy@users.noreply.github.com>
Date: Sun, 5 May 2019 14:03:16 +0100
Subject: [PATCH] Embed the fonts in the EXE

Now CSE2.exe should be drop-in replacement for Doukutsu.exe, with
no extra files needed.
---
 CMakeLists.txt                           |   7 +++
 Makefile                                 |   5 +++
 {build_en/font => res/FONT}/cour.ttf     | Bin
 {build_jp/font => res/FONT}/msgothic.ttc | Bin
 src/Draw.cpp                             |  53 ++++++++++++-----------
 src/Resource.cpp                         |  15 +++++++
 6 files changed, 55 insertions(+), 25 deletions(-)
 rename {build_en/font => res/FONT}/cour.ttf (100%)
 rename {build_jp/font => res/FONT}/msgothic.ttc (100%)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index bf550f81..7c3e27aa 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -255,6 +255,13 @@ if(MSVC)
 	target_compile_definitions(CSE2 PRIVATE _CRT_SECURE_NO_WARNINGS)	# Shut up those stupid warnings
 endif()
 
+# Decide the embedded font
+if(JAPANESE)
+	list(APPEND RESOURCES "FONT/msgothic.ttc")
+elseif(NOT WIN32)
+	list(APPEND RESOURCES "FONT/cour.ttf")
+endif()
+
 # Magic to convert resources to header files
 add_executable(bin2h "src/misc/bin2h.c")
 if(MSVC)
diff --git a/Makefile b/Makefile
index 8f696874..e727670e 100644
--- a/Makefile
+++ b/Makefile
@@ -183,8 +183,13 @@ RESOURCES = \
 
 ifeq ($(JAPANESE), 1)
 	RESOURCES += BITMAP/pixel_jp.bmp
+	RESOURCES += FONT/msgothic.ttc
 else
 	RESOURCES += BITMAP/pixel.bmp
+
+	ifneq ($(WINDOWS), 1)
+		RESOURCES += FONT/cour.ttf
+	endif
 endif
 
 ifneq ($(WINDOWS), 1)
diff --git a/build_en/font/cour.ttf b/res/FONT/cour.ttf
similarity index 100%
rename from build_en/font/cour.ttf
rename to res/FONT/cour.ttf
diff --git a/build_jp/font/msgothic.ttc b/res/FONT/msgothic.ttc
similarity index 100%
rename from build_jp/font/msgothic.ttc
rename to res/FONT/msgothic.ttc
diff --git a/src/Draw.cpp b/src/Draw.cpp
index 1d5a7259..cf39b20d 100644
--- a/src/Draw.cpp
+++ b/src/Draw.cpp
@@ -525,9 +525,6 @@ static unsigned char* GetFontFromWindows(size_t *data_size, const char *font_nam
 
 	HFONT hfont = CreateFontA(fontHeight, fontWidth, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, charset, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FIXED_PITCH | FF_DONTCARE, font_name);
 
-	if (hfont == NULL)
-		hfont = CreateFontA(fontHeight, fontWidth, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, charset, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FIXED_PITCH | FF_DONTCARE, NULL);
-
 	if (hfont != NULL)
 	{
 		HDC hdc = CreateCompatibleDC(NULL);
@@ -539,14 +536,14 @@ static unsigned char* GetFontFromWindows(size_t *data_size, const char *font_nam
 
 			if (size != GDI_ERROR)
 			{
-				buffer = new unsigned char[size];
+				buffer = (unsigned char*)malloc(size);
 
 				if (data_size != NULL)
 					*data_size = size;
 
 				if (GetFontData(hdc, 0, 0, buffer, size) != size)
 				{
-					delete[] buffer;
+					free(buffer);
 					buffer = NULL;
 				}
 			}
@@ -575,34 +572,40 @@ void InitTextObject(const char *font_name)
 		fontWidth = 5 * magnification;
 		fontHeight = 10 * magnification;
 //	}
-	
+
+	size_t data_size;
 #ifdef WINDOWS
 	// Actually use the font Config.dat specifies
-	size_t data_size;
-	unsigned char *data = GetFontFromWindows(&data_size, font_name, fontWidth, fontHeight);
-
-	if (data != NULL)
+	unsigned char *data;
+	data = GetFontFromWindows(&data_size, font_name, fontWidth, fontHeight);
+	if (data)
 	{
 		gFont = LoadFontFromData(data, data_size, fontWidth, fontHeight);
-
-		delete[] data;
-
-		if (gFont)
-			return;
+		free(data);
 	}
+
+	if (gFont)
+		return;
+
+#ifndef JAPANESE
+	// Fall back on a default font
+	data = GetFontFromWindows(&data_size, "Courier New", fontWidth, fontHeight);
+	if (data)
+	{
+		gFont = LoadFontFromData(data, data_size, fontWidth, fontHeight);
+		free(data);
+	}
+
+	if (gFont)
+		return;
 #endif
-	// Fall back on the built-in fonts
+#endif
+	// Fall back on the built-in font
 	(void)font_name;
+	const unsigned char *res_data = FindResource("DEFAULT_FONT", "FONT", &data_size);
 
-	//Open Font.ttf
-	char path[PATH_LENGTH];
-#ifdef JAPANESE
-	sprintf(path, "%s/font/msgothic.ttc", gModulePath);
-#else
-	sprintf(path, "%s/font/cour.ttf", gModulePath);
-#endif
-
-	gFont = LoadFont(path, fontWidth, fontHeight);
+	if (res_data != NULL)
+		gFont = LoadFontFromData(res_data, data_size, fontWidth, fontHeight);
 }
 
 void PutText(int x, int y, const char *text, uint32_t color)
diff --git a/src/Resource.cpp b/src/Resource.cpp
index 9d2ab633..20d22923 100644
--- a/src/Resource.cpp
+++ b/src/Resource.cpp
@@ -74,6 +74,14 @@
 #include "Resource/CURSOR/CURSOR_IKA.bmp.h"
 #include "Resource/CURSOR/CURSOR_NORMAL.bmp.h"
 
+#ifdef JAPANESE
+#include "Resource/FONT/msgothic.ttc.h"
+#else
+#ifndef WINDOWS
+#include "Resource/FONT/cour.ttf.h"
+#endif
+#endif
+
 static const struct
 {
 	const char *type;
@@ -156,6 +164,13 @@ static const struct
 	{"CURSOR", "CURSOR_NORMAL", rCURSOR_NORMAL, sizeof(rCURSOR_NORMAL)},
 	{"CURSOR", "CURSOR_IKA", rCURSOR_IKA, sizeof(rCURSOR_IKA)},
 
+#ifdef JAPANESE
+	{"FONT", "DEFAULT_FONT", rmsgothic, sizeof(rmsgothic)},
+#else
+#ifndef WINDOWS
+	{"FONT", "DEFAULT_FONT", rcour, sizeof(rcour)},
+#endif
+#endif
 };
 
 const unsigned char* FindResource(const char *name, const char *type, size_t *size)