Euphoria
vfs_imagegenerator.cc
Go to the documentation of this file.
2 
3 #include "core/image.h"
4 #include "core/image_draw.h"
5 #include "base/stringutils.h"
6 #include "base/stringmerger.h"
7 #include "log/log.h"
8 #include "io/vfs_path.h"
9 
10 namespace eu::core
11 {
12  void
14  (
15  io::FileSystem* fs,
16  const io::DirPath& base
17  )
18  {
19  auto root = std::make_shared<ReadRootImageGenerator>(base);
20  fs->add_read_root(root);
21  }
22 
23 
24  std::shared_ptr<MemoryChunk>
26  {
27  const auto [dir, command] = path.split_directories_and_file();
28  if(dir != base)
29  {
30  return MemoryChunk::create_null();
31  }
32 
33  const auto color_name = to_lower(command);
34 
35  const auto found_color = from_string_to_enum<NamedColor>(color_name);
36 
37  if(!found_color.single_match)
38  {
39  LOG_WARN
40  (
41  "Invalid color name: {0} for path {1} closest matches are {2}",
42  color_name,
43  path,
44  string_mergers::english_or.merge(from_enum_to_string(found_color.values))
45  );
46  return MemoryChunk::create_null();
47  }
48 
49  const auto color = found_color.values[0];
50 
51  Image image;
52  image.setup_no_alpha_support(128, 128);
53  clear(&image, {color});
54  return image.write(ImageWriteFormat::png);
55  }
56 
57 
58  void
59  ReadRootImageGenerator::add_description(std::vector<std::string>* strings)
60  {
61  strings->emplace_back(fmt::format("{}<color>", base));
62  }
63 
64 
66  : base(b)
67  {
69  }
70 
71 
74  {
75  ASSERT(!path.contains_relative());
76 
77  io::FileList ret;
78 
80  {
81  if(path == base.get_parent_directory())
82  {
83  ret.add(base.get_name() + "/", true, false);
84  }
85  }
86 
87  if(path == base)
88  {
89  const auto names = get_all_names_from_enum<NamedColor>();
90  for(const auto& n: names)
91  {
92  ret.add(n, true, true);
93  }
94  }
95 
96  return ret;
97  }
98 
99 }
ParserBase * base
Definition: argparse.cc:887
#define ASSERT(x)
Definition: assert.h:29
std::string to_lower(const std::string &str)
Generate a string containing only lower characters.
Definition: stringutils.cc:143
#define LOG_WARN(...)
Definition: log.h:8
void clear(Image *image, const Rgbai &color)
Definition: image_draw.cc:32
constexpr StringMerger english_or
Definition: stringmerger.h:90
std::string from_enum_to_string(T t)
Definition: enumtostring.h:116
String utility functions.
static std::shared_ptr< MemoryChunk > create_null()
Definition: memorychunk.cc:47
static void add(io::FileSystem *fs, const io::DirPath &base)
void add_description(std::vector< std::string > *strings) override
io::FileList list_files(const io::DirPath &path) override
std::shared_ptr< MemoryChunk > read_file(const io::FilePath &path) override
ReadRootImageGenerator(const io::DirPath &base)
std::string get_name() const
Definition: vfs_path.cc:300
DirPath get_parent_directory() const
Definition: vfs_path.cc:286
bool contains_relative() const
Definition: vfs_path.cc:270
static DirPath from_root()
Definition: vfs_path.cc:218
void add(const ListedFile &file)
Definition: vfs.cc:35
std::tuple< DirPath, std::string > split_directories_and_file() const
Definition: vfs_path.cc:116
void add_read_root(const std::shared_ptr< ReadRoot > &root)
Definition: vfs.cc:72