2014-09-12 20:34:49 +02:00
|
|
|
/*
|
2017-07-26 17:03:36 +02:00
|
|
|
* \brief Interface for obtaining widget style information
|
2014-09-12 20:34:49 +02:00
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2009-09-11
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 13:23:52 +01:00
|
|
|
* Copyright (C) 2014-2017 Genode Labs GmbH
|
2014-09-12 20:34:49 +02:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 13:23:52 +01:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2014-09-12 20:34:49 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _STYLE_DATABASE_H_
|
|
|
|
#define _STYLE_DATABASE_H_
|
|
|
|
|
|
|
|
/* gems includes */
|
|
|
|
#include <gems/file.h>
|
|
|
|
#include <gems/png_image.h>
|
2018-04-17 13:35:30 +02:00
|
|
|
#include <gems/cached_font.h>
|
|
|
|
#include <gems/vfs_font.h>
|
2014-09-12 20:34:49 +02:00
|
|
|
|
|
|
|
/* local includes */
|
|
|
|
#include "types.h"
|
|
|
|
|
|
|
|
namespace Menu_view { struct Style_database; }
|
|
|
|
|
|
|
|
|
|
|
|
class Menu_view::Style_database
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
enum { PATH_MAX_LEN = 200 };
|
|
|
|
|
2018-04-17 13:35:30 +02:00
|
|
|
typedef String<PATH_MAX_LEN> Path;
|
|
|
|
|
|
|
|
typedef ::File::Reading_failed Reading_failed;
|
|
|
|
|
2014-09-12 20:34:49 +02:00
|
|
|
struct Texture_entry : List<Texture_entry>::Element
|
|
|
|
{
|
2018-04-17 13:35:30 +02:00
|
|
|
Path const path;
|
|
|
|
::File png_file;
|
2014-09-12 20:34:49 +02:00
|
|
|
Png_image png_image;
|
|
|
|
Texture<Pixel_rgb888> &texture;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* \throw Reading_failed
|
|
|
|
*/
|
2019-01-21 15:46:48 +01:00
|
|
|
Texture_entry(Ram_allocator &ram, Region_map &rm,
|
2018-04-17 13:35:30 +02:00
|
|
|
Allocator &alloc, Path const &path)
|
2014-09-12 20:34:49 +02:00
|
|
|
:
|
|
|
|
path(path),
|
2018-04-17 13:35:30 +02:00
|
|
|
png_file(path.string(), alloc),
|
2017-01-11 19:13:04 +01:00
|
|
|
png_image(ram, rm, alloc, png_file.data<void>()),
|
2014-09-12 20:34:49 +02:00
|
|
|
texture(*png_image.texture<Pixel_rgb888>())
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Font_entry : List<Font_entry>::Element
|
|
|
|
{
|
2018-04-17 13:35:30 +02:00
|
|
|
Path const path;
|
2018-03-08 18:27:42 +01:00
|
|
|
|
2018-04-17 13:35:30 +02:00
|
|
|
Cached_font::Limit _font_cache_limit { 256*1024 };
|
|
|
|
Vfs_font _vfs_font;
|
|
|
|
Cached_font _cached_font;
|
2018-03-08 18:27:42 +01:00
|
|
|
|
2018-04-17 13:35:30 +02:00
|
|
|
Text_painter::Font const &font() const { return _cached_font; }
|
2014-09-12 20:34:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* \throw Reading_failed
|
|
|
|
*/
|
2018-04-17 13:35:30 +02:00
|
|
|
Font_entry(Directory const &fonts_dir, Path const &path, Allocator &alloc)
|
|
|
|
try :
|
2014-09-12 20:34:49 +02:00
|
|
|
path(path),
|
2018-04-17 13:35:30 +02:00
|
|
|
_vfs_font(alloc, fonts_dir, path),
|
|
|
|
_cached_font(alloc, _vfs_font, _font_cache_limit)
|
2014-09-12 20:34:49 +02:00
|
|
|
{ }
|
2018-04-17 13:35:30 +02:00
|
|
|
catch (...) { throw Reading_failed(); }
|
2014-09-12 20:34:49 +02:00
|
|
|
};
|
|
|
|
|
2019-01-21 15:46:48 +01:00
|
|
|
Ram_allocator &_ram;
|
2018-04-17 13:35:30 +02:00
|
|
|
Region_map &_rm;
|
|
|
|
Allocator &_alloc;
|
|
|
|
Directory const &_fonts_dir;
|
2017-01-11 19:13:04 +01:00
|
|
|
|
2014-09-12 20:34:49 +02:00
|
|
|
/*
|
|
|
|
* The list is mutable because it is populated as a side effect of
|
|
|
|
* calling the const lookup function.
|
|
|
|
*/
|
2019-01-14 20:04:40 +01:00
|
|
|
List<Texture_entry> mutable _textures { };
|
|
|
|
List<Font_entry> mutable _fonts { };
|
2014-09-12 20:34:49 +02:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T const *_lookup(List<T> &list, char const *path) const
|
|
|
|
{
|
|
|
|
for (T const *e = list.first(); e; e = e->next())
|
|
|
|
if (Genode::strcmp(e->path.string(), path) == 0)
|
|
|
|
return e;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Assemble path name 'styles/<widget>/<style>/<name>.<extension>'
|
|
|
|
*/
|
2016-11-25 16:54:49 +01:00
|
|
|
static Path _construct_path(Xml_node node, char const *name,
|
|
|
|
char const *extension)
|
2014-09-12 20:34:49 +02:00
|
|
|
{
|
2016-11-25 16:54:49 +01:00
|
|
|
typedef String<64> Style;
|
|
|
|
Style const style = node.attribute_value("style", Style("default"));
|
2014-09-12 20:34:49 +02:00
|
|
|
|
2016-11-25 16:54:49 +01:00
|
|
|
return Path("/styles/", node.type(), "/", style, "/", name, ".", extension);
|
2014-09-12 20:34:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2019-01-21 15:46:48 +01:00
|
|
|
Style_database(Ram_allocator &ram, Region_map &rm, Allocator &alloc,
|
2018-04-17 13:35:30 +02:00
|
|
|
Directory const &fonts_dir)
|
2017-01-11 19:13:04 +01:00
|
|
|
:
|
2018-04-17 13:35:30 +02:00
|
|
|
_ram(ram), _rm(rm), _alloc(alloc), _fonts_dir(fonts_dir)
|
2017-01-11 19:13:04 +01:00
|
|
|
{ }
|
|
|
|
|
2014-09-12 20:34:49 +02:00
|
|
|
Texture<Pixel_rgb888> const *texture(Xml_node node, char const *png_name) const
|
|
|
|
{
|
|
|
|
Path const path = _construct_path(node, png_name, "png");
|
|
|
|
|
|
|
|
if (Texture_entry const *e = _lookup(_textures, path.string()))
|
|
|
|
return &e->texture;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load and remember PNG image
|
|
|
|
*/
|
|
|
|
try {
|
2017-01-11 19:13:04 +01:00
|
|
|
Texture_entry *e = new (_alloc)
|
|
|
|
Texture_entry(_ram, _rm, _alloc, path.string());
|
2014-09-12 20:34:49 +02:00
|
|
|
|
|
|
|
_textures.insert(e);
|
|
|
|
return &e->texture;
|
|
|
|
|
2018-04-17 13:35:30 +02:00
|
|
|
} catch (Reading_failed) {
|
2016-11-25 16:54:49 +01:00
|
|
|
warning("could not read texture data from file \"", path.string(), "\"");
|
|
|
|
return nullptr;
|
2014-09-12 20:34:49 +02:00
|
|
|
}
|
|
|
|
|
2016-11-25 16:54:49 +01:00
|
|
|
return nullptr;
|
2014-09-12 20:34:49 +02:00
|
|
|
}
|
|
|
|
|
2018-04-17 13:35:30 +02:00
|
|
|
Text_painter::Font const *font(Xml_node node) const
|
2014-09-12 20:34:49 +02:00
|
|
|
{
|
2018-04-17 13:35:30 +02:00
|
|
|
Path const path = node.attribute_value("font", Path("text/regular"));
|
2014-09-12 20:34:49 +02:00
|
|
|
if (Font_entry const *e = _lookup(_fonts, path.string()))
|
2018-04-17 13:35:30 +02:00
|
|
|
return &e->font();
|
2014-09-12 20:34:49 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Load and remember font
|
|
|
|
*/
|
|
|
|
try {
|
2017-01-11 19:13:04 +01:00
|
|
|
Font_entry *e = new (_alloc)
|
2018-04-17 13:35:30 +02:00
|
|
|
Font_entry(_fonts_dir, path, _alloc);
|
2014-09-12 20:34:49 +02:00
|
|
|
|
|
|
|
_fonts.insert(e);
|
2018-04-17 13:35:30 +02:00
|
|
|
return &e->font();
|
2014-09-12 20:34:49 +02:00
|
|
|
|
2018-04-17 13:35:30 +02:00
|
|
|
} catch (Reading_failed) {
|
2014-09-12 20:34:49 +02:00
|
|
|
|
2016-11-25 16:54:49 +01:00
|
|
|
warning("could not read font from file \"", path.string(), "\"");
|
|
|
|
return nullptr;
|
2014-09-12 20:34:49 +02:00
|
|
|
}
|
|
|
|
|
2016-11-25 16:54:49 +01:00
|
|
|
return nullptr;
|
2014-09-12 20:34:49 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* _STYLE_DATABASE_H_ */
|