Skip to content

Commit

Permalink
gui: fix font scaling for zip themes
Browse files Browse the repository at this point in the history
All fonts were extracted to /tmp/extract.bin which was deleted after
the initial load, so reloading for scaling failed.

- extract fonts to /tmp with original name and don't delete them
- minor code cleanup

Change-Id: If8a0f657a7ef4c418fd5cc8550a24de44a38f303
  • Loading branch information
that1 authored and Dees-Troy committed Mar 17, 2016
1 parent c2dafbb commit b240f4a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
19 changes: 11 additions & 8 deletions gui/resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,27 @@ int Resource::ExtractResource(ZipArchive* pZip, std::string folderName, std::str
return ret;
}

void Resource::LoadImage(ZipArchive* pZip, std::string file, gr_surface* source)
void Resource::LoadImage(ZipArchive* pZip, std::string file, gr_surface* surface)
{
int rc = 0;
if (ExtractResource(pZip, "images", file, ".png", TMP_RESOURCE_NAME) == 0)
{
res_create_surface(TMP_RESOURCE_NAME, source);
rc = res_create_surface(TMP_RESOURCE_NAME, surface);
unlink(TMP_RESOURCE_NAME);
}
else if (ExtractResource(pZip, "images", file, "", TMP_RESOURCE_NAME) == 0)
{
// JPG includes the .jpg extension in the filename so extension should be blank
res_create_surface(TMP_RESOURCE_NAME, source);
rc = res_create_surface(TMP_RESOURCE_NAME, surface);
unlink(TMP_RESOURCE_NAME);
}
else if (!pZip)
{
// File name in xml may have included .png so try without adding .png
res_create_surface(file.c_str(), source);
rc = res_create_surface(file.c_str(), surface);
}
if (rc != 0)
LOGINFO("Failed to load image from %s%s, error %d\n", file.c_str(), pZip ? " (zip)" : "", rc);
}

void Resource::CheckAndScaleImage(gr_surface source, gr_surface* destination, int retain_aspect)
Expand Down Expand Up @@ -122,7 +125,6 @@ void FontResource::LoadFont(xml_node<>* node, ZipArchive* pZip)

if(file.size() >= 4 && file.compare(file.size()-4, 4, ".ttf") == 0)
{
m_type = TYPE_TTF;
int font_size = 0;

if (origFontSize != 0) {
Expand All @@ -144,10 +146,11 @@ void FontResource::LoadFont(xml_node<>* node, ZipArchive* pZip)
if(attr)
dpi = atoi(attr->value());

if (ExtractResource(pZip, "fonts", file, "", TMP_RESOURCE_NAME) == 0)
// we can't use TMP_RESOURCE_NAME here because the ttf subsystem is caching the name and scaling needs to reload the font
std::string tmpname = "/tmp/" + file;
if (ExtractResource(pZip, "fonts", file, "", tmpname) == 0)
{
mFont = gr_ttf_loadFont(TMP_RESOURCE_NAME, font_size, dpi);
unlink(TMP_RESOURCE_NAME);
mFont = gr_ttf_loadFont(tmpname.c_str(), font_size, dpi);
}
else
{
Expand Down
11 changes: 1 addition & 10 deletions gui/resources.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,13 @@ class Resource

protected:
static int ExtractResource(ZipArchive* pZip, std::string folderName, std::string fileName, std::string fileExtn, std::string destFile);
static void LoadImage(ZipArchive* pZip, std::string file, gr_surface* source);
static void LoadImage(ZipArchive* pZip, std::string file, gr_surface* surface);
static void CheckAndScaleImage(gr_surface source, gr_surface* destination, int retain_aspect);
};

class FontResource : public Resource
{
public:
enum Type
{
TYPE_TWRP,
#ifndef TW_DISABLE_TTF
TYPE_TTF,
#endif
};

FontResource(xml_node<>* node, ZipArchive* pZip);
virtual ~FontResource();

Expand All @@ -54,7 +46,6 @@ class FontResource : public Resource

protected:
void* mFont;
Type m_type;

private:
void LoadFont(xml_node<>* node, ZipArchive* pZip);
Expand Down

0 comments on commit b240f4a

Please sign in to comment.