File: font_loader.h

package info (click to toggle)
cataclysm-dda 0.C%2Bgit20190228.faafa3a-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 181,636 kB
  • sloc: cpp: 256,609; python: 2,621; makefile: 862; sh: 495; perl: 37; xml: 33
file content (95 lines) | stat: -rw-r--r-- 3,701 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#pragma once
#ifndef FONT_LOADER_H
#define FONT_LOADER_H

#include <fstream>
#include <stdexcept>
#include <string>

#include "debug.h"
#include "filesystem.h"
#include "json.h"
#include "path_info.h"

class font_loader
{
    public:
        bool fontblending = false;
        std::string typeface;
        std::string map_typeface;
        std::string overmap_typeface;
        int fontwidth = 8;
        int fontheight = 16;
        int fontsize = 16;
        int map_fontwidth = 8;
        int map_fontheight = 16;
        int map_fontsize = 16;
        int overmap_fontwidth = 8;
        int overmap_fontheight = 16;
        int overmap_fontsize = 16;

    private:
        void load_throws( const std::string &path ) {
            try {
                std::ifstream stream( path.c_str(), std::ifstream::binary );
                JsonIn json( stream );
                JsonObject config = json.get_object();
                config.read( "fontblending", fontblending );
                config.read( "fontwidth", fontwidth );
                config.read( "fontheight", fontheight );
                config.read( "fontsize", fontsize );
                config.read( "typeface", typeface );
                config.read( "map_fontwidth", map_fontwidth );
                config.read( "map_fontheight", map_fontheight );
                config.read( "map_fontsize", map_fontsize );
                config.read( "map_typeface", map_typeface );
                config.read( "overmap_fontwidth", overmap_fontwidth );
                config.read( "overmap_fontheight", overmap_fontheight );
                config.read( "overmap_fontsize", overmap_fontsize );
                config.read( "overmap_typeface", overmap_typeface );
            } catch( const std::exception &err ) {
                throw std::runtime_error( std::string( "loading font settings from " ) + path + " failed: " +
                                          err.what() );
            }
        }
        void save( const std::string &path ) const {
            std::ofstream stream( path.c_str(), std::ofstream::binary );
            JsonOut json( stream, true ); // pretty-print
            json.start_object();
            json.member( "fontblending", fontblending );
            json.member( "fontwidth", fontwidth );
            json.member( "fontheight", fontheight );
            json.member( "fontsize", fontsize );
            json.member( "typeface", typeface );
            json.member( "map_fontwidth", map_fontwidth );
            json.member( "map_fontheight", map_fontheight );
            json.member( "map_fontsize", map_fontsize );
            json.member( "map_typeface", map_typeface );
            json.member( "overmap_fontwidth", overmap_fontwidth );
            json.member( "overmap_fontheight", overmap_fontheight );
            json.member( "overmap_fontsize", overmap_fontsize );
            json.member( "overmap_typeface", overmap_typeface );
            json.end_object();
            stream << "\n";
            stream.close();
            if( !stream.good() ) {
                DebugLog( D_ERROR, D_SDL ) << "saving font settings to " << path << " failed";
            }
        }

    public:
        /// @throws std::exception upon any kind of error.
        void load() {
            const std::string fontdata = FILENAMES["fontdata"];
            const std::string legacy_fontdata = FILENAMES["legacy_fontdata"];
            if( file_exist( fontdata ) ) {
                load_throws( fontdata );
            } else {
                load_throws( legacy_fontdata );
                assure_dir_exist( FILENAMES["config_dir"] );
                save( fontdata );
            }
        }
};

#endif