Implement a resource system.
Currently, icons, fonts, etc are converted to C structures at compile
time and are hardcoded to the binary. This presents several problems:
* Cross-compilation is complicated. Right now, it is necessary
to be able to run executables for the target platform; this
happens to work with wine-binfmt installed, but is rather ugly.
* Icons can only have one resolution. On OS X, modern software is
expected to take advantage of high-DPI ("Retina") screens and
use so-called @2x assets when ran in high-DPI mode.
* Localization is complicated. Win32 and OS X provide built-in
support for loading the resource appropriate for the user's
locale.
* Embedding strings can only be done as raw strings, using C++'s
R"(...)" literals. This precludes embedding sizable strings,
e.g. JavaScript libraries as used in Three.js export, and makes
git history less useful. Not embedding the libraries means we
have to rely on external CDNs, which requires an Internet
connection and adds a glaring point of failure.
* Linux distribution guidelines are violated. All architecture-
independent data, especially large data such as fonts, is
expected to be in /usr/share, not in the binary.
* Customization is impossible without recompilation. Minor
modifications like adding a few missing vector font characters
or adjusting localization require a complete development
environment, which is unreasonable to expect from users of
a mechanical CAD.
As such, this commit adds a resource system that bundles (and
sometimes builds) resources with the executable. Where they go is
platform-dependent:
* on Win32: into resources of the executable, which allows us to
keep distributing one file;
* on OS X: into the app bundle;
* on other *nix: into /usr/share/solvespace/ or ../res/ (relative
to the executable path), the latter allowing us to run freshly
built executables without installation.
It also subsides the platform-specific resources that are in src/.
The resource system is not yet used for anything; this will be added
in later commits.
2016-04-21 15:54:18 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Discovery and loading of our resources (icons, fonts, templates, etc).
|
|
|
|
//
|
|
|
|
// Copyright 2016 whitequark
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
#include "solvespace.h"
|
2016-04-22 13:35:22 +00:00
|
|
|
#include <png.h>
|
Implement a resource system.
Currently, icons, fonts, etc are converted to C structures at compile
time and are hardcoded to the binary. This presents several problems:
* Cross-compilation is complicated. Right now, it is necessary
to be able to run executables for the target platform; this
happens to work with wine-binfmt installed, but is rather ugly.
* Icons can only have one resolution. On OS X, modern software is
expected to take advantage of high-DPI ("Retina") screens and
use so-called @2x assets when ran in high-DPI mode.
* Localization is complicated. Win32 and OS X provide built-in
support for loading the resource appropriate for the user's
locale.
* Embedding strings can only be done as raw strings, using C++'s
R"(...)" literals. This precludes embedding sizable strings,
e.g. JavaScript libraries as used in Three.js export, and makes
git history less useful. Not embedding the libraries means we
have to rely on external CDNs, which requires an Internet
connection and adds a glaring point of failure.
* Linux distribution guidelines are violated. All architecture-
independent data, especially large data such as fonts, is
expected to be in /usr/share, not in the binary.
* Customization is impossible without recompilation. Minor
modifications like adding a few missing vector font characters
or adjusting localization require a complete development
environment, which is unreasonable to expect from users of
a mechanical CAD.
As such, this commit adds a resource system that bundles (and
sometimes builds) resources with the executable. Where they go is
platform-dependent:
* on Win32: into resources of the executable, which allows us to
keep distributing one file;
* on OS X: into the app bundle;
* on other *nix: into /usr/share/solvespace/ or ../res/ (relative
to the executable path), the latter allowing us to run freshly
built executables without installation.
It also subsides the platform-specific resources that are in src/.
The resource system is not yet used for anything; this will be added
in later commits.
2016-04-21 15:54:18 +00:00
|
|
|
|
|
|
|
namespace SolveSpace {
|
|
|
|
|
2016-04-22 13:35:22 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Resource loading functions
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
Implement a resource system.
Currently, icons, fonts, etc are converted to C structures at compile
time and are hardcoded to the binary. This presents several problems:
* Cross-compilation is complicated. Right now, it is necessary
to be able to run executables for the target platform; this
happens to work with wine-binfmt installed, but is rather ugly.
* Icons can only have one resolution. On OS X, modern software is
expected to take advantage of high-DPI ("Retina") screens and
use so-called @2x assets when ran in high-DPI mode.
* Localization is complicated. Win32 and OS X provide built-in
support for loading the resource appropriate for the user's
locale.
* Embedding strings can only be done as raw strings, using C++'s
R"(...)" literals. This precludes embedding sizable strings,
e.g. JavaScript libraries as used in Three.js export, and makes
git history less useful. Not embedding the libraries means we
have to rely on external CDNs, which requires an Internet
connection and adds a glaring point of failure.
* Linux distribution guidelines are violated. All architecture-
independent data, especially large data such as fonts, is
expected to be in /usr/share, not in the binary.
* Customization is impossible without recompilation. Minor
modifications like adding a few missing vector font characters
or adjusting localization require a complete development
environment, which is unreasonable to expect from users of
a mechanical CAD.
As such, this commit adds a resource system that bundles (and
sometimes builds) resources with the executable. Where they go is
platform-dependent:
* on Win32: into resources of the executable, which allows us to
keep distributing one file;
* on OS X: into the app bundle;
* on other *nix: into /usr/share/solvespace/ or ../res/ (relative
to the executable path), the latter allowing us to run freshly
built executables without installation.
It also subsides the platform-specific resources that are in src/.
The resource system is not yet used for anything; this will be added
in later commits.
2016-04-21 15:54:18 +00:00
|
|
|
std::string LoadString(const std::string &name) {
|
|
|
|
size_t size;
|
|
|
|
const void *data = LoadResource(name, &size);
|
|
|
|
if(data == NULL) oops();
|
|
|
|
|
|
|
|
return std::string(static_cast<const char *>(data), size);
|
|
|
|
}
|
|
|
|
|
2016-04-22 13:35:22 +00:00
|
|
|
Pixmap LoadPNG(const std::string &name) {
|
|
|
|
size_t size;
|
|
|
|
const void *data = LoadResource(name, &size);
|
|
|
|
if(data == NULL) oops();
|
|
|
|
|
|
|
|
Pixmap pixmap = Pixmap::FromPNG(static_cast<const uint8_t *>(data), size);
|
|
|
|
if(pixmap.IsEmpty()) oops();
|
|
|
|
|
|
|
|
return pixmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Pixmap manipulation
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void Pixmap::Clear() {
|
|
|
|
*this = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
static Pixmap ReadPNGIntoPixmap(png_struct *png_ptr, png_info *info_ptr) {
|
|
|
|
png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_EXPAND | PNG_TRANSFORM_GRAY_TO_RGB, NULL);
|
|
|
|
|
|
|
|
Pixmap pixmap = {};
|
|
|
|
pixmap.width = png_get_image_width(png_ptr, info_ptr);
|
|
|
|
pixmap.height = png_get_image_height(png_ptr, info_ptr);
|
|
|
|
pixmap.hasAlpha = png_get_color_type(png_ptr, info_ptr) & PNG_COLOR_MASK_ALPHA;
|
|
|
|
|
|
|
|
size_t stride = pixmap.width * pixmap.GetBytesPerPixel();
|
|
|
|
if(stride % 4 != 0) stride += 4 - stride % 4;
|
|
|
|
pixmap.stride = stride;
|
|
|
|
|
|
|
|
pixmap.data = std::unique_ptr<uint8_t[]>(new uint8_t[pixmap.stride * pixmap.height]);
|
|
|
|
uint8_t **rows = png_get_rows(png_ptr, info_ptr);
|
|
|
|
for(size_t y = 0; y < pixmap.height; y++) {
|
|
|
|
memcpy(&pixmap.data[pixmap.stride * y], rows[y],
|
|
|
|
pixmap.width * pixmap.GetBytesPerPixel());
|
|
|
|
}
|
|
|
|
|
|
|
|
return pixmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
Pixmap Pixmap::FromPNG(const uint8_t *data, size_t size) {
|
|
|
|
Pixmap pixmap = {};
|
|
|
|
struct Slice { const uint8_t *data; size_t size; };
|
|
|
|
Slice dataSlice = { data, size };
|
|
|
|
png_struct *png_ptr = NULL;
|
|
|
|
png_info *info_ptr = NULL;
|
|
|
|
|
|
|
|
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
|
|
|
if(!png_ptr) goto exit;
|
|
|
|
info_ptr = png_create_info_struct(png_ptr);
|
|
|
|
if(!info_ptr) goto exit;
|
|
|
|
|
|
|
|
if(setjmp(png_jmpbuf(png_ptr))) goto exit;
|
|
|
|
|
|
|
|
png_set_read_fn(png_ptr, &dataSlice,
|
|
|
|
[](png_struct *png_ptr, uint8_t *data, size_t size) {
|
|
|
|
Slice *dataSlice = (Slice *)png_get_io_ptr(png_ptr);
|
|
|
|
if(size <= dataSlice->size) {
|
|
|
|
memcpy(data, dataSlice->data, size);
|
|
|
|
dataSlice->data += size;
|
|
|
|
dataSlice->size -= size;
|
|
|
|
} else {
|
|
|
|
png_error(png_ptr, "EOF");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
pixmap = ReadPNGIntoPixmap(png_ptr, info_ptr);
|
|
|
|
|
|
|
|
exit:
|
|
|
|
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
|
|
|
return pixmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
Pixmap Pixmap::FromPNG(FILE *f) {
|
|
|
|
Pixmap pixmap = {};
|
|
|
|
|
|
|
|
png_struct *png_ptr = NULL;
|
|
|
|
png_info *info_ptr = NULL;
|
|
|
|
|
|
|
|
uint8_t header[8];
|
|
|
|
if(fread(header, 1, sizeof(header), f) != sizeof(header)) goto exit;
|
|
|
|
if(png_sig_cmp(header, 0, sizeof(header))) goto exit;
|
|
|
|
|
|
|
|
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
|
|
|
if(!png_ptr) goto exit;
|
|
|
|
info_ptr = png_create_info_struct(png_ptr);
|
|
|
|
if(!info_ptr) goto exit;
|
|
|
|
|
|
|
|
if(setjmp(png_jmpbuf(png_ptr))) goto exit;
|
|
|
|
|
|
|
|
png_init_io(png_ptr, f);
|
|
|
|
png_set_sig_bytes(png_ptr, sizeof(header));
|
|
|
|
|
|
|
|
pixmap = ReadPNGIntoPixmap(png_ptr, info_ptr);
|
|
|
|
|
|
|
|
exit:
|
|
|
|
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
|
|
|
return pixmap;
|
|
|
|
}
|
|
|
|
|
Implement a resource system.
Currently, icons, fonts, etc are converted to C structures at compile
time and are hardcoded to the binary. This presents several problems:
* Cross-compilation is complicated. Right now, it is necessary
to be able to run executables for the target platform; this
happens to work with wine-binfmt installed, but is rather ugly.
* Icons can only have one resolution. On OS X, modern software is
expected to take advantage of high-DPI ("Retina") screens and
use so-called @2x assets when ran in high-DPI mode.
* Localization is complicated. Win32 and OS X provide built-in
support for loading the resource appropriate for the user's
locale.
* Embedding strings can only be done as raw strings, using C++'s
R"(...)" literals. This precludes embedding sizable strings,
e.g. JavaScript libraries as used in Three.js export, and makes
git history less useful. Not embedding the libraries means we
have to rely on external CDNs, which requires an Internet
connection and adds a glaring point of failure.
* Linux distribution guidelines are violated. All architecture-
independent data, especially large data such as fonts, is
expected to be in /usr/share, not in the binary.
* Customization is impossible without recompilation. Minor
modifications like adding a few missing vector font characters
or adjusting localization require a complete development
environment, which is unreasonable to expect from users of
a mechanical CAD.
As such, this commit adds a resource system that bundles (and
sometimes builds) resources with the executable. Where they go is
platform-dependent:
* on Win32: into resources of the executable, which allows us to
keep distributing one file;
* on OS X: into the app bundle;
* on other *nix: into /usr/share/solvespace/ or ../res/ (relative
to the executable path), the latter allowing us to run freshly
built executables without installation.
It also subsides the platform-specific resources that are in src/.
The resource system is not yet used for anything; this will be added
in later commits.
2016-04-21 15:54:18 +00:00
|
|
|
}
|