Browse Source

Fix UTF-8 filenames for Font and Image.

tags/v2.0.2
Andrew Belt 2 years ago
parent
commit
e026ae3301
3 changed files with 34 additions and 3 deletions
  1. +1
    -0
      include/system.hpp
  2. +21
    -0
      src/system.cpp
  3. +12
    -3
      src/window/Window.cpp

+ 1
- 0
include/system.hpp View File

@@ -115,6 +115,7 @@ std::string getExtension(const std::string& path);
Throws on error.
*/
std::vector<uint8_t> readFile(const std::string& path);
uint8_t* readFile(const std::string& path, size_t* size);

/** Writes a memory buffer to a file, overwriting if already exists.
Throws on error.


+ 21
- 0
src/system.cpp View File

@@ -305,6 +305,27 @@ std::vector<uint8_t> readFile(const std::string& path) {
}


uint8_t* readFile(const std::string& path, size_t* size) {
FILE* f = std::fopen(path.c_str(), "rb");
if (!f)
throw Exception("Cannot read file %s", path.c_str());
DEFER({
std::fclose(f);
});

// Get file size so we can make a single allocation
std::fseek(f, 0, SEEK_END);
size_t len = std::ftell(f);
std::fseek(f, 0, SEEK_SET);

uint8_t* data = (uint8_t*) std::malloc(len);
std::fread(data, 1, len, f);
if (size)
*size = len;
return data;
}


void writeFile(const std::string& path, const std::vector<uint8_t>& data) {
FILE* f = std::fopen(path.c_str(), "wb");
if (!f)


+ 12
- 3
src/window/Window.cpp View File

@@ -37,9 +37,16 @@ Font::~Font() {

void Font::loadFile(const std::string& filename, NVGcontext* vg) {
this->vg = vg;
handle = nvgCreateFont(vg, filename.c_str(), filename.c_str());
if (handle < 0)
std::string name = system::getStem(filename);
size_t size;
// Transfer ownership of font data to font object
uint8_t* data = system::readFile(filename, &size);
// Don't use nvgCreateFont because it doesn't properly handle UTF-8 filenames on Windows.
handle = nvgCreateFontMem(vg, name.c_str(), data, size, 0);
if (handle < 0) {
std::free(data);
throw Exception("Failed to load font %s", filename.c_str());
}
INFO("Loaded font %s", filename.c_str());
}

@@ -58,7 +65,9 @@ Image::~Image() {

void Image::loadFile(const std::string& filename, NVGcontext* vg) {
this->vg = vg;
handle = nvgCreateImage(vg, filename.c_str(), NVG_IMAGE_REPEATX | NVG_IMAGE_REPEATY);
std::vector<uint8_t> data = system::readFile(filename);
// Don't use nvgCreateImage because it doesn't properly handle UTF-8 filenames on Windows.
handle = nvgCreateImageMem(vg, NVG_IMAGE_REPEATX | NVG_IMAGE_REPEATY, data.data(), data.size());
if (handle <= 0)
throw Exception("Failed to load image %s", filename.c_str());
INFO("Loaded image %s", filename.c_str());


Loading…
Cancel
Save