lorid

convert chordpro to pdf
git clone git://git.relim.de/lorid.git
Log | Files | Refs | README | LICENSE

core.h (7547B)


      1 #include <stdint.h>
      2 #include <stdarg.h>
      3 
      4 #ifndef _CORE_H_
      5 #define _CORE_H_
      6 
      7 #ifdef ENABLE_DEBUG
      8 #define DEBUG(msg) util_log(__FILE__, __LINE__, LOG_DEBUG, msg);
      9 #else
     10 #define DEBUG(msg)
     11 #endif
     12 
     13 #define LENGTH(x) (sizeof x / sizeof x[0])
     14 
     15 #define COLOR_BOLD_RED "\033[1;31m"
     16 #define COLOR_BOLD_ORANGE "\033[1;33m"
     17 #define COLOR_BOLD_WHITE "\033[1;37m"
     18 #define COLOR_BOLD_BLUE "\033[1;34m"
     19 #define COLOR_RESET "\033[0m"
     20 
     21 #define MAX_STRINGS 12
     22 
     23 enum Alignment {
     24 	ALIGNMENT_LEFT,
     25 	ALIGNMENT_CENTER,
     26 	ALIGNMENT_RIGHT
     27 };
     28 
     29 enum Anchor {
     30 	ANCHOR_PAPER,
     31 	ANCHOR_PAGE,
     32 	ANCHOR_COLUMN,
     33 	ANCHOR_LINE,
     34 	ANCHOR_FLOAT
     35 };
     36 
     37 enum BreakType {
     38 	BREAK_TYPE_LINE,
     39 	BREAK_TYPE_PAGE,
     40 	BREAK_TYPE_COLUMN
     41 };
     42 
     43 enum ChordDiagramContent {
     44 	CHORD_DIAGRAM_CONTENT_UNINITIALIZED,
     45 	CHORD_DIAGRAM_CONTENT_STRING,
     46 	CHORD_DIAGRAM_CONTENT_KEYBOARD,
     47 	CHORD_DIAGRAM_CONTENT_CHORD_MAP
     48 };
     49 
     50 enum ChordQualifier {
     51 	CHORD_QUALIFIER_MIN,
     52 	CHORD_QUALIFIER_MAJ,
     53 	CHORD_QUALIFIER_AUG,
     54 	CHORD_QUALIFIER_DIM
     55 };
     56 
     57 enum FileType {
     58 	FILE_TYPE_ERROR,
     59 	FILE_TYPE_FOLDER,
     60 	FILE_TYPE_REG_FILE,
     61 	FILE_TYPE_OTHER
     62 };
     63 
     64 enum FontStyle {
     65 	FONT_STYLE_ROMAN,
     66 	FONT_STYLE_OBLIQUE,
     67 	FONT_STYLE_ITALIC
     68 };
     69 
     70 enum FontWeight {
     71 	FONT_WEIGHT_REGULAR,
     72 	FONT_WEIGHT_BOLD
     73 };
     74 
     75 enum Instrument {
     76 	INSTRUMENT_GUITAR,
     77 	INSTRUMENT_KEYBOARD,
     78 	INSTRUMENT_MANDOLIN,
     79 	INSTRUMENT_UKULELE
     80 };
     81 
     82 enum LineStyle {
     83 	LINE_STYLE_SINGLE,
     84 	LINE_STYLE_DOUBLE,
     85 	LINE_STYLE_NONE
     86 };
     87 
     88 enum LogLevel {
     89 	LOG_INFO,
     90 	LOG_WARN,
     91 	LOG_ERR,
     92 	LOG_DEBUG
     93 };
     94 
     95 enum NotationSystem {
     96 	NOTATION_SYSTEM_COMMON,
     97 	NOTATION_SYSTEM_GERMAN,
     98 	NOTATION_SYSTEM_SCANDINAVIAN,
     99 	NOTATION_SYSTEM_LATIN,
    100 	NOTATION_SYSTEM_ROMAN,
    101 	NOTATION_SYSTEM_NASHVILLE,
    102 	NOTATION_SYSTEM_CUSTOM
    103 };
    104 
    105 enum NoteType {
    106 	NOTE_TYPE_NOTE,
    107 	NOTE_TYPE_SHARP,
    108 	NOTE_TYPE_FLAT
    109 };
    110 
    111 enum ParseMode {
    112 	PARSE_MODE_STRICT,
    113 	PARSE_MODE_RELAXED
    114 };
    115 
    116 enum SectionType {
    117 	SECTION_TYPE_UNINITIALIZED,
    118 	SECTION_TYPE_NEWSONG,
    119 	SECTION_TYPE_CHORUS,
    120 	SECTION_TYPE_VERSE,
    121 	SECTION_TYPE_BRIDGE,
    122 	SECTION_TYPE_TAB,
    123 	SECTION_TYPE_GRID,
    124 	SECTION_TYPE_CUSTOM
    125 };
    126 
    127 enum SizeType {
    128 	SIZE_TYPE_POINT,
    129 	SIZE_TYPE_PERCENT,
    130 	SIZE_TYPE_EM,
    131 	SIZE_TYPE_EX
    132 };
    133 
    134 enum TextType {
    135 	TEXT_TYPE_CHORD,
    136 	TEXT_TYPE_ANNOT,
    137 	TEXT_TYPE_CHORUS,
    138 	TEXT_TYPE_FOOTER,
    139 	TEXT_TYPE_GRID,
    140 	TEXT_TYPE_TAB,
    141 	TEXT_TYPE_TOC,
    142 	TEXT_TYPE_TOC_TITLE,
    143 	TEXT_TYPE_TEXT,
    144 	TEXT_TYPE_TITLE,
    145 	TEXT_TYPE_SUBTITLE,
    146 	TEXT_TYPE_LABEL,
    147 	TEXT_TYPE_COMMENT,
    148 	TEXT_TYPE_COMMENT_ITALIC,
    149 	TEXT_TYPE_COMMENT_BOX,
    150 	TEXT_TYPE_LENGTH
    151 };
    152 
    153 struct FontPresence {
    154 	bool family;
    155 	bool style;
    156 	bool weight;
    157 	bool size;
    158 };
    159 
    160 struct ChoStylePresence {
    161 	struct FontPresence font;
    162 	bool foreground_color;
    163 	bool background_color;
    164 	bool underline_style;
    165 	bool underline_color;
    166 	bool overline_style;
    167 	bool overline_color;
    168 	bool strikethrough;
    169 	bool strikethrough_color;
    170 	bool boxed;
    171 	bool boxed_color;
    172 	bool rise;
    173 	bool href;
    174 };
    175 
    176 struct Font {
    177 	char *family; // INFO: Exact font family name, e.g. from `fc-list : family`
    178 	enum FontStyle style;
    179 	enum FontWeight weight;
    180 	double size;
    181 };
    182 
    183 struct RGBColor {
    184 	uint8_t red;
    185 	uint8_t green;
    186 	uint8_t blue;
    187 };
    188 
    189 struct ChoStyle {
    190 	struct Font *font;
    191 	struct RGBColor *foreground_color;
    192 	struct RGBColor *background_color;
    193 	struct RGBColor *underline_color;
    194 	struct RGBColor *overline_color;
    195 	struct RGBColor *strikethrough_color;
    196 	struct RGBColor *boxed_color;
    197 	char *href;
    198 	enum LineStyle underline_style;
    199 	enum LineStyle overline_style;
    200 	bool strikethrough;
    201 	bool boxed;
    202 	double rise;
    203 };
    204 
    205 struct ChoChord {
    206 	struct ChoStyle *style;
    207 	bool is_canonical;
    208 	char *name;
    209 	char *root;
    210 	enum ChordQualifier qual;
    211 	char *ext;
    212 	char *bass;
    213 	char *display;
    214 };
    215 
    216 struct ChoText {
    217 	struct ChoStyle *style;
    218 	char *text;
    219 };
    220 
    221 struct ChoLineItem {
    222 	bool is_text;
    223 	union {
    224 		struct ChoImage *image;
    225 		struct ChoText *text;
    226 	} u;
    227 };
    228 
    229 struct ChoLineItemAbove {
    230 	int position;
    231 	bool is_chord;
    232 	union {
    233 		struct ChoChord *chord;
    234 		struct ChoText *annot;
    235 	} u;
    236 };
    237 
    238 struct ChoLine {
    239 	struct ChoLineItemAbove **text_above;
    240 	struct ChoLineItem **items;
    241 	enum BreakType btype;
    242 };
    243 
    244 struct ChoMetadata {
    245 	char *name;
    246 	char *value;
    247 	struct ChoStyle *style;
    248 };
    249 
    250 struct ChoSection {
    251 	enum SectionType type;
    252 	struct ChoText *label;
    253 	struct ChoLine **lines;
    254 	bool is_closed;
    255 };
    256 
    257 struct ChoSong {
    258 	struct ChoMetadata **metadata;
    259 	struct ChoSection **sections;
    260 	struct ChordDiagram **diagrams;
    261 	bool present_text_types[TEXT_TYPE_LENGTH];
    262 };
    263 
    264 struct ChoImage {
    265 	bool is_asset;
    266 	char *id;
    267 	char *src;
    268 	struct Size *width;
    269 	struct Size *height;
    270 	struct Size *width_scale;
    271 	struct Size *height_scale;
    272 	enum Alignment align;
    273 	double border;
    274 	struct Size *spread_space;
    275 	char *href;
    276 	struct Size *x;
    277 	struct Size *y;
    278 	enum Anchor anchor;
    279 	struct Size *dx;
    280 	struct Size *dy;
    281 	struct Size *w;
    282 	struct Size *h;
    283 	bool bbox;
    284 };
    285 
    286 struct Size {
    287 	enum SizeType type;
    288 	double d;
    289 };
    290 
    291 struct StringDiagram {
    292 	char *name;
    293 	int8_t base_fret;
    294 	int8_t frets[MAX_STRINGS];
    295 	int8_t fingers[MAX_STRINGS];
    296 };
    297 
    298 struct KeyboardDiagram {
    299 	char *name;
    300 	int8_t keys[24];
    301 };
    302 
    303 struct ChordMap {
    304 	char *name;
    305 	char *display;
    306 };
    307 
    308 struct ChordDiagram {
    309 	bool show;
    310 	struct RGBColor *color;
    311 	enum ChordDiagramContent type;
    312 	union {
    313 		struct StringDiagram *sd;
    314 		struct KeyboardDiagram *kd;
    315 		struct ChordMap *cm;
    316 	} u;
    317 };
    318 
    319 struct InstrumentInfo {
    320 	char *name;
    321 	char *description;
    322 	char *tuning;
    323 };
    324 
    325 struct Note {
    326 	char *note;
    327 	char *sharp;
    328 	char *flat;
    329 };
    330 
    331 struct ConfigChords {
    332 	enum NotationSystem notation_system;
    333 	enum ParseMode mode;
    334 };
    335 
    336 struct ConfigChorus {
    337 	char *label;
    338 	bool quote;
    339 };
    340 
    341 struct ConfigParser {
    342 	struct ConfigChords *chords;
    343 	struct Note **notes;
    344 };
    345 
    346 struct ConfigChordDiagram {
    347 	bool show;
    348 	enum Instrument instrument;
    349 };
    350 
    351 struct ConfigToc {
    352 	bool show;
    353 	char *title;
    354 };
    355 
    356 struct ConfigPageNo {
    357 	bool show;
    358 	enum Alignment align;
    359 };
    360 
    361 struct ConfigOutput {
    362 	struct ConfigChorus *chorus;
    363 	struct ConfigChordDiagram *diagram;
    364 	struct ConfigPageNo *page_no;
    365 	struct ConfigToc *toc;
    366 	struct ChoStyle *styles[TEXT_TYPE_LENGTH];
    367 	struct Note **notes;
    368 	enum NotationSystem notation_system;
    369 	bool start_song_on_new_page;
    370 };
    371 
    372 struct Config {
    373 	bool verbose_logging;
    374 	char *metadata_separator;
    375 	struct ConfigOutput *output;
    376 	struct ConfigParser *parser;
    377 };
    378 
    379 void util_log_enable_info_logs(void);
    380 
    381 void *emalloc(size_t size);
    382 void *erealloc(void *ptr, size_t size);
    383 void util_vlog(const char *file, size_t line_no, enum LogLevel level, const char *msg, va_list va);
    384 void util_log(const char *file, size_t line_no, enum LogLevel level, const char *msg, ...);
    385 
    386 bool str_starts_with(const char *str, const char *part);
    387 char *str_normalize(const char *str);
    388 char *str_trim(const char *str);
    389 char *str_remove_leading_whitespace(const char *str);
    390 int str_compare(const char *a, const char *b);
    391 long str_to_number(const char *str);
    392 int str_index_of(const char *str, char c);
    393 
    394 bool strs_has(char **strs, const char *str);
    395 void strs_add(char ***strs, const char *str);
    396 int strs_get_index_if_in(char **strs, const char *str);
    397 void strs_free(char **strs);
    398 
    399 
    400 enum FileType file_type(const char *path);
    401 char *file_read(FILE *fp);
    402 char *file_extension_replace_or_add(const char *filepath, const char *extension);
    403 bool file_extension_equals(const char *filepath, const char *extension);
    404 
    405 char *filepath_add_ending_slash_if_missing(const char *path);
    406 char *filepath_basename(const char *path);
    407 char *filepath_dirname(const char *path);
    408 char *filepath_resolve_tilde(const char *path);
    409 char *filepath_as_dot_file(const char *path);
    410 
    411 struct Size *size_create(const char *str);
    412 struct Size *size_copy(struct Size *size);
    413 const char *size_to_string(struct Size *size);
    414 
    415 const char *is_base_font(struct Font *font);
    416 
    417 #endif /* _CORE_H_ */