From 76ee903d84d5b477016fb6ed6bdee1a21e237903 Mon Sep 17 00:00:00 2001 From: Vojtech Bocek Date: Sun, 7 Sep 2014 15:01:56 +0200 Subject: Add support for TrueType fonts * Keeps original font system in place * Uses the same API as original font system: - You can render only one line at a time - You can only use one font and color for one gr_text* call * Caches all rendered text, with a string cache limited to 400 entries, then it trucates to 250, which results in memory usage hovering around 5-10MB Change-Id: I36107b9dcd8d57bae4486fce8b8f64e49ef3d906 Signed-off-by: Vojtech Bocek --- gui/Android.mk | 11 ++++ gui/console.cpp | 2 +- gui/devices/1024x600/res/ui.xml | 2 +- gui/devices/1024x768/res/ui.xml | 2 +- gui/devices/1080x1920/res/ui.xml | 6 +- gui/devices/1200x1920/res/ui.xml | 6 +- gui/devices/1280x800/res/ui.xml | 2 +- gui/devices/1440x2560/res/ui.xml | 6 +- gui/devices/1600x2560/res/ui.xml | 6 +- gui/devices/1920x1200/res/ui.xml | 2 +- gui/devices/240x240/res/ui.xml | 6 +- gui/devices/2560x1600/res/ui.xml | 2 +- gui/devices/280x280/res/ui.xml | 6 +- gui/devices/320x320/res/ui.xml | 6 +- gui/devices/320x480/res/ui.xml | 6 +- gui/devices/480x800/res/ui.xml | 6 +- gui/devices/480x854/res/ui.xml | 6 +- gui/devices/540x960/res/ui.xml | 6 +- gui/devices/720x1280/res/ui.xml | 6 +- gui/devices/800x1280/res/ui.xml | 6 +- gui/devices/800x480/res/ui.xml | 2 +- .../common/res/fonts/RobotoCondensed-Regular.ttf | Bin 0 -> 125332 bytes gui/fileselector.cpp | 2 +- gui/input.cpp | 2 +- gui/listbox.cpp | 2 +- gui/partitionlist.cpp | 2 +- gui/resources.cpp | 67 +++++++++++++++++++-- gui/resources.hpp | 9 +++ gui/slidervalue.cpp | 2 +- gui/text.cpp | 2 +- 30 files changed, 133 insertions(+), 58 deletions(-) create mode 100644 gui/devices/common/res/fonts/RobotoCondensed-Regular.ttf (limited to 'gui') diff --git a/gui/Android.mk b/gui/Android.mk index 52d5f5573..baae3edf8 100644 --- a/gui/Android.mk +++ b/gui/Android.mk @@ -59,6 +59,9 @@ endif ifeq ($(TW_OEM_BUILD), true) LOCAL_CFLAGS += -DTW_OEM_BUILD endif +ifeq ($(TW_DISABLE_TTF), true) + LOCAL_CFLAGS += -DTW_DISABLE_TTF +endif ifeq ($(DEVICE_RESOLUTION),) $(warning ********************************************************************************) @@ -104,6 +107,13 @@ ifeq ($(TW_CUSTOM_THEME),) else TWRP_THEME_LOC := $(TW_CUSTOM_THEME) endif + +ifeq ($(TW_DISABLE_TTF), true) + TWRP_REMOVE_FONT := rm -f $(TARGET_RECOVERY_ROOT_OUT)/res/fonts/*.ttf +else + TWRP_REMOVE_FONT := rm -f $(TARGET_RECOVERY_ROOT_OUT)/res/fonts/*.dat +endif + TWRP_RES_GEN := $(intermediates)/twrp ifneq ($(TW_USE_TOOLBOX), true) TWRP_SH_TARGET := /sbin/busybox @@ -116,6 +126,7 @@ $(TWRP_RES_GEN): cp -fr $(TWRP_RES_LOC)/* $(TARGET_RECOVERY_ROOT_OUT)/res/ cp -fr $(TWRP_THEME_LOC)/* $(TARGET_RECOVERY_ROOT_OUT)/res/ $(TWRP_COMMON_XML) + $(TWRP_REMOVE_FONT) mkdir -p $(TARGET_RECOVERY_ROOT_OUT)/sbin/ ln -sf $(TWRP_SH_TARGET) $(TARGET_RECOVERY_ROOT_OUT)/sbin/sh ln -sf /sbin/pigz $(TARGET_RECOVERY_ROOT_OUT)/sbin/gzip diff --git a/gui/console.cpp b/gui/console.cpp index 897c58203..aad392ca3 100644 --- a/gui/console.cpp +++ b/gui/console.cpp @@ -177,7 +177,7 @@ GUIConsole::GUIConsole(xml_node<>* node) : GUIObject(node) } } - gr_getFontDetails(mFont, &mFontHeight, NULL); + mFontHeight = gr_getMaxFontHeight(mFont ? mFont->GetResource() : NULL); SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH); SetRenderPos(mConsoleX, mConsoleY); return; diff --git a/gui/devices/1024x600/res/ui.xml b/gui/devices/1024x600/res/ui.xml index 87248a686..4d6f3178f 100644 --- a/gui/devices/1024x600/res/ui.xml +++ b/gui/devices/1024x600/res/ui.xml @@ -14,7 +14,7 @@ - + diff --git a/gui/devices/1024x768/res/ui.xml b/gui/devices/1024x768/res/ui.xml index 407e18b31..29f169089 100644 --- a/gui/devices/1024x768/res/ui.xml +++ b/gui/devices/1024x768/res/ui.xml @@ -14,7 +14,7 @@ - + diff --git a/gui/devices/1080x1920/res/ui.xml b/gui/devices/1080x1920/res/ui.xml index 0d547a6e9..95c48a513 100644 --- a/gui/devices/1080x1920/res/ui.xml +++ b/gui/devices/1080x1920/res/ui.xml @@ -14,9 +14,9 @@ - - - + + + diff --git a/gui/devices/1200x1920/res/ui.xml b/gui/devices/1200x1920/res/ui.xml index 0778692c7..428880d76 100644 --- a/gui/devices/1200x1920/res/ui.xml +++ b/gui/devices/1200x1920/res/ui.xml @@ -14,9 +14,9 @@ - - - + + + diff --git a/gui/devices/1280x800/res/ui.xml b/gui/devices/1280x800/res/ui.xml index bfb1a3a3a..6f6c2bd36 100644 --- a/gui/devices/1280x800/res/ui.xml +++ b/gui/devices/1280x800/res/ui.xml @@ -14,7 +14,7 @@ - + diff --git a/gui/devices/1440x2560/res/ui.xml b/gui/devices/1440x2560/res/ui.xml index ae25d33c8..fe55dfdbf 100644 --- a/gui/devices/1440x2560/res/ui.xml +++ b/gui/devices/1440x2560/res/ui.xml @@ -14,9 +14,9 @@ - - - + + + diff --git a/gui/devices/1600x2560/res/ui.xml b/gui/devices/1600x2560/res/ui.xml index 9703881e3..8561b2ddf 100644 --- a/gui/devices/1600x2560/res/ui.xml +++ b/gui/devices/1600x2560/res/ui.xml @@ -14,9 +14,9 @@ - - - + + + diff --git a/gui/devices/1920x1200/res/ui.xml b/gui/devices/1920x1200/res/ui.xml index d8d8a7d5b..3e8c9f13f 100644 --- a/gui/devices/1920x1200/res/ui.xml +++ b/gui/devices/1920x1200/res/ui.xml @@ -14,7 +14,7 @@ - + diff --git a/gui/devices/240x240/res/ui.xml b/gui/devices/240x240/res/ui.xml index 4cc25dd87..294e5951b 100644 --- a/gui/devices/240x240/res/ui.xml +++ b/gui/devices/240x240/res/ui.xml @@ -14,9 +14,9 @@ - - - + + + diff --git a/gui/devices/2560x1600/res/ui.xml b/gui/devices/2560x1600/res/ui.xml index ca0d8838e..cb0c12e1f 100644 --- a/gui/devices/2560x1600/res/ui.xml +++ b/gui/devices/2560x1600/res/ui.xml @@ -14,7 +14,7 @@ - + diff --git a/gui/devices/280x280/res/ui.xml b/gui/devices/280x280/res/ui.xml index 5a705a0e9..99532edcb 100644 --- a/gui/devices/280x280/res/ui.xml +++ b/gui/devices/280x280/res/ui.xml @@ -14,9 +14,9 @@ - - - + + + diff --git a/gui/devices/320x320/res/ui.xml b/gui/devices/320x320/res/ui.xml index a9be8c985..f66852942 100644 --- a/gui/devices/320x320/res/ui.xml +++ b/gui/devices/320x320/res/ui.xml @@ -14,9 +14,9 @@ - - - + + + diff --git a/gui/devices/320x480/res/ui.xml b/gui/devices/320x480/res/ui.xml index 57baf5ff3..cccd5b3b4 100644 --- a/gui/devices/320x480/res/ui.xml +++ b/gui/devices/320x480/res/ui.xml @@ -14,9 +14,9 @@ - - - + + + diff --git a/gui/devices/480x800/res/ui.xml b/gui/devices/480x800/res/ui.xml index aad982291..940ad4333 100644 --- a/gui/devices/480x800/res/ui.xml +++ b/gui/devices/480x800/res/ui.xml @@ -14,9 +14,9 @@ - - - + + + diff --git a/gui/devices/480x854/res/ui.xml b/gui/devices/480x854/res/ui.xml index ea0cf77cd..dce1d8843 100644 --- a/gui/devices/480x854/res/ui.xml +++ b/gui/devices/480x854/res/ui.xml @@ -14,9 +14,9 @@ - - - + + + diff --git a/gui/devices/540x960/res/ui.xml b/gui/devices/540x960/res/ui.xml index 58d6c9df6..37c3e26d5 100644 --- a/gui/devices/540x960/res/ui.xml +++ b/gui/devices/540x960/res/ui.xml @@ -14,9 +14,9 @@ - - - + + + diff --git a/gui/devices/720x1280/res/ui.xml b/gui/devices/720x1280/res/ui.xml index f44998f66..a7ff19285 100644 --- a/gui/devices/720x1280/res/ui.xml +++ b/gui/devices/720x1280/res/ui.xml @@ -14,9 +14,9 @@ - - - + + + diff --git a/gui/devices/800x1280/res/ui.xml b/gui/devices/800x1280/res/ui.xml index b074931b6..e0036bfc5 100644 --- a/gui/devices/800x1280/res/ui.xml +++ b/gui/devices/800x1280/res/ui.xml @@ -14,9 +14,9 @@ - - - + + + diff --git a/gui/devices/800x480/res/ui.xml b/gui/devices/800x480/res/ui.xml index 0ee0e94fc..9acb7a112 100644 --- a/gui/devices/800x480/res/ui.xml +++ b/gui/devices/800x480/res/ui.xml @@ -14,7 +14,7 @@ - + diff --git a/gui/devices/common/res/fonts/RobotoCondensed-Regular.ttf b/gui/devices/common/res/fonts/RobotoCondensed-Regular.ttf new file mode 100644 index 000000000..b9fc49c95 Binary files /dev/null and b/gui/devices/common/res/fonts/RobotoCondensed-Regular.ttf differ diff --git a/gui/fileselector.cpp b/gui/fileselector.cpp index 4f90ca3a8..cf7a9a908 100644 --- a/gui/fileselector.cpp +++ b/gui/fileselector.cpp @@ -333,7 +333,7 @@ GUIFileSelector::GUIFileSelector(xml_node<>* node) : GUIObject(node) } // Retrieve the line height - gr_getFontDetails(mFont ? mFont->GetResource() : NULL, &mFontHeight, NULL); + mFontHeight = gr_getMaxFontHeight(mFont ? mFont->GetResource() : NULL); mLineHeight = mFontHeight; mHeaderH = mFontHeight; diff --git a/gui/input.cpp b/gui/input.cpp index 61b0cff1f..84ee17b16 100644 --- a/gui/input.cpp +++ b/gui/input.cpp @@ -139,7 +139,7 @@ GUIInput::GUIInput(xml_node<>* node) attr = child->first_attribute("resource"); if (attr) { mFont = PageManager::FindResource(attr->value()); - gr_getFontDetails(mFont ? mFont->GetResource() : NULL, &mFontHeight, NULL); + mFontHeight = gr_getMaxFontHeight(mFont ? mFont->GetResource() : NULL); } } diff --git a/gui/listbox.cpp b/gui/listbox.cpp index e09ec5391..851b37348 100644 --- a/gui/listbox.cpp +++ b/gui/listbox.cpp @@ -271,7 +271,7 @@ GUIListBox::GUIListBox(xml_node<>* node) : GUIObject(node) } // Retrieve the line height - gr_getFontDetails(mFont ? mFont->GetResource() : NULL, &mFontHeight, NULL); + mFontHeight = gr_getMaxFontHeight(mFont ? mFont->GetResource() : NULL); mLineHeight = mFontHeight; mHeaderH = mFontHeight; diff --git a/gui/partitionlist.cpp b/gui/partitionlist.cpp index 317e17805..2d464e1b6 100644 --- a/gui/partitionlist.cpp +++ b/gui/partitionlist.cpp @@ -273,7 +273,7 @@ GUIPartitionList::GUIPartitionList(xml_node<>* node) : GUIObject(node) } // Retrieve the line height - gr_getFontDetails(mFont ? mFont->GetResource() : NULL, &mFontHeight, NULL); + mFontHeight = gr_getMaxFontHeight(mFont ? mFont->GetResource() : NULL); mLineHeight = mFontHeight; mHeaderH = mFontHeight; diff --git a/gui/resources.cpp b/gui/resources.cpp index 8d430b1ec..4fce0ca44 100644 --- a/gui/resources.cpp +++ b/gui/resources.cpp @@ -65,27 +65,82 @@ FontResource::FontResource(xml_node<>* node, ZipArchive* pZip) : Resource(node, pZip) { std::string file; + xml_attribute<>* attr; mFont = NULL; if (!node) return; - if (node->first_attribute("filename")) - file = node->first_attribute("filename")->value(); + attr = node->first_attribute("filename"); + if (!attr) + return; + + file = attr->value(); - if (ExtractResource(pZip, "fonts", file, ".dat", TMP_RESOURCE_NAME) == 0) +#ifndef TW_DISABLE_TTF + if(file.size() >= 4 && file.compare(file.size()-4, 4, ".ttf") == 0) { - mFont = gr_loadFont(TMP_RESOURCE_NAME); - unlink(TMP_RESOURCE_NAME); + m_type = TYPE_TTF; + + attr = node->first_attribute("size"); + if(!attr) + return; + + int size = atoi(attr->value()); + int dpi = 300; + + attr = node->first_attribute("dpi"); + if(attr) + dpi = atoi(attr->value()); + + if (ExtractResource(pZip, "fonts", file, "", TMP_RESOURCE_NAME) == 0) + { + mFont = gr_ttf_loadFont(TMP_RESOURCE_NAME, size, dpi); + unlink(TMP_RESOURCE_NAME); + } + else + { + file = std::string("/res/fonts/") + file; + mFont = gr_ttf_loadFont(file.c_str(), size, dpi); + } } else +#endif { - mFont = gr_loadFont(file.c_str()); + m_type = TYPE_TWRP; + + if(file.size() >= 4 && file.compare(file.size()-4, 4, ".ttf") == 0) + { + attr = node->first_attribute("fallback"); + if (!attr) + return; + + file = attr->value(); + } + + if (ExtractResource(pZip, "fonts", file, ".dat", TMP_RESOURCE_NAME) == 0) + { + mFont = gr_loadFont(TMP_RESOURCE_NAME); + unlink(TMP_RESOURCE_NAME); + } + else + { + mFont = gr_loadFont(file.c_str()); + } } } FontResource::~FontResource() { + if(mFont) + { +#ifndef TW_DISABLE_TTF + if(m_type == TYPE_TTF) + gr_ttf_freeFont(mFont); + else +#endif + gr_freeFont(mFont); + } } ImageResource::ImageResource(xml_node<>* node, ZipArchive* pZip) diff --git a/gui/resources.hpp b/gui/resources.hpp index 874836e52..3cb528176 100644 --- a/gui/resources.hpp +++ b/gui/resources.hpp @@ -38,6 +38,14 @@ typedef enum { class FontResource : public Resource { public: + enum Type + { + TYPE_TWRP, +#ifndef TW_DISABLE_TTF + TYPE_TTF, +#endif + }; + FontResource(xml_node<>* node, ZipArchive* pZip); virtual ~FontResource(); @@ -46,6 +54,7 @@ public: protected: void* mFont; + Type m_type; }; class ImageResource : public Resource diff --git a/gui/slidervalue.cpp b/gui/slidervalue.cpp index 5b4d57f1c..700d7d5d7 100644 --- a/gui/slidervalue.cpp +++ b/gui/slidervalue.cpp @@ -198,7 +198,7 @@ GUISliderValue::GUISliderValue(xml_node<>* node) : GUIObject(node) } } - gr_getFontDetails(mFont ? mFont->GetResource() : NULL, (unsigned*) &mFontHeight, NULL); + mFontHeight = gr_getMaxFontHeight(mFont ? mFont->GetResource() : NULL); if(mShowCurr) { diff --git a/gui/text.cpp b/gui/text.cpp index c594f4824..29d7ad948 100644 --- a/gui/text.cpp +++ b/gui/text.cpp @@ -97,7 +97,7 @@ GUIText::GUIText(xml_node<>* node) mLastValue = parseText(); if (mLastValue != mText) mIsStatic = 0; - gr_getFontDetails(mFont ? mFont->GetResource() : NULL, (unsigned*) &mFontHeight, NULL); + mFontHeight = gr_getMaxFontHeight(mFont ? mFont->GetResource() : NULL); return; } -- cgit v1.2.3