lorid

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

chordpro.h (6785B)


      1 #include <stdint.h>
      2 #include "core.h"
      3 
      4 #ifndef _CHORDPRO_H_
      5 #define _CHORDPRO_H_
      6 
      7 #define ERROR -1.0
      8 #define EMPTY_DOUBLE -1.0
      9 #define EMPTY_INT -1
     10 #define DEFAULT_FONT_SIZE 14.0
     11 #define DEFAULT_TITLE_FONT_SIZE 18.0
     12 // INFO: Based on https://stackoverflow.com/a/417184
     13 #define URL_MAX_LEN 2000
     14 #define FONT_NAME_MAX 100
     15 #define CHORD_LEN 15
     16 
     17 enum AttrValueSyntax {
     18 	ATTRIBUTE_VALUE_SYNTAX_UNINITIALIZED,
     19 	ATTRIBUTE_VALUE_SYNTAX_QUOTATION_MARK,
     20 	ATTRIBUTE_VALUE_SYNTAX_APOSTROPHE,
     21 	ATTRIBUTE_VALUE_SYNTAX_UNQUOTED
     22 };
     23 
     24 enum ChordDiagramState {
     25 	CHORD_DIAGRAM_STATE_NAME,
     26 	CHORD_DIAGRAM_STATE_OPTION_NAME,
     27 	CHORD_DIAGRAM_STATE_BASE_FRET,
     28 	CHORD_DIAGRAM_STATE_FRETS,
     29 	CHORD_DIAGRAM_STATE_FINGERS,
     30 	CHORD_DIAGRAM_STATE_KEYS,
     31 	CHORD_DIAGRAM_STATE_DIAGRAM,
     32 	CHORD_DIAGRAM_STATE_COPY,
     33 	CHORD_DIAGRAM_STATE_DISPLAY
     34 };
     35 
     36 enum MetadataSubstitutionState {
     37 	METADATA_SUBSTITUTION_STATE_NAME,
     38 	METADATA_SUBSTITUTION_STATE_NAME_OR_INDEX,
     39 	METADATA_SUBSTITUTION_STATE_INDEX,
     40 	METADATA_SUBSTITUTION_STATE_VALUE,
     41 	METADATA_SUBSTITUTION_STATE_TRUE_TEXT,
     42 	METADATA_SUBSTITUTION_STATE_FALSE_TEXT,
     43 	METADATA_SUBSTITUTION_STATE_WAIT_FOR_PIPE
     44 };
     45 
     46 enum ChordDirective {
     47 	CHORD_DIRECTIVE_TRANSPOSE,
     48 	CHORD_DIRECTIVE_DEFINE,
     49 	/* CHORD_DIRECTIVE_CHORD */
     50 };
     51 
     52 enum DirectiveType {
     53 	DIRECTIVE_TYPE_ENVIRONMENT,
     54 	DIRECTIVE_TYPE_METADATA,
     55 	DIRECTIVE_TYPE_FORMATTING,
     56 	DIRECTIVE_TYPE_IMAGE,
     57 	DIRECTIVE_TYPE_PREAMBLE,
     58 	DIRECTIVE_TYPE_FONT,
     59 	DIRECTIVE_TYPE_CHORD,
     60 	DIRECTIVE_TYPE_OUTPUT,
     61 	DIRECTIVE_TYPE_EXTENSION,
     62 	DIRECTIVE_TYPE_CUSTOM
     63 };
     64 
     65 enum GridToken {
     66 	GRID_TOKEN_CHORD,
     67 	GRID_TOKEN_BAR_LINE_SYMBOL,
     68 	GRID_TOKEN_PLACEHOLDER
     69 };
     70 
     71 enum MetadataDirective {
     72 	METADATA_DIRECTIVE_TITLE,
     73 	METADATA_DIRECTIVE_SUBTITLE,
     74 	METADATA_DIRECTIVE_OTHER
     75 };
     76 
     77 enum OptionState {
     78 	OPTION_STATE_NAME,
     79 	OPTION_STATE_VALUE
     80 };
     81 
     82 enum Position {
     83 	POSITION_START,
     84 	POSITION_END,
     85 	POSITION_NO
     86 };
     87 
     88 enum State {
     89 	STATE_LYRICS,
     90 	STATE_BACKSLASH,
     91 	STATE_DIRECTIVE_NAME,
     92 	STATE_DIRECTIVE_VALUE,
     93 	STATE_CHORD,
     94 	STATE_ANNOTATION,
     95 	STATE_TAB,
     96 	STATE_GRID,
     97 	STATE_MARKUP_TAG,
     98 	STATE_MARKUP_TAG_START,
     99 	STATE_MARKUP_TAG_END,
    100 	STATE_MARKUP_ATTR_NAME,
    101 	STATE_MARKUP_ATTR_VALUE,
    102 	STATE_COMMENT,
    103 	STATE_MAYBE_METADATA_SUBSTITUTION,
    104 	STATE_METADATA_SUBSTITUTION
    105 };
    106 
    107 enum StylePropertyType {
    108 	STYLE_PROPERTY_TYPE_FONT,
    109 	STYLE_PROPERTY_TYPE_SIZE,
    110 	STYLE_PROPERTY_TYPE_COLOR
    111 };
    112 
    113 struct Attr {
    114 	char *name;
    115 	char *value;
    116 };
    117 
    118 /*
    119 	INFO: Depending on the 'dtype' the other
    120 	fields have a meaningful value or not.
    121 */
    122 
    123 struct ChoDirective {
    124 	enum DirectiveType dtype;
    125 	enum SectionType stype;
    126 	enum Position position;
    127 	enum StylePropertyType sprop;
    128 	enum TextType ttype;
    129 	enum BreakType btype;
    130 	enum MetadataDirective meta;
    131 	enum ChordDirective ctype;
    132 	struct ChoStyle *style;
    133 };
    134 
    135 union StylePropertyValue {
    136 	char *font_name;
    137 	double font_size;
    138 	struct RGBColor *foreground_color;
    139 };
    140 
    141 struct StyleProperty {
    142 	enum TextType ttype;
    143 	enum StylePropertyType type;
    144 	union StylePropertyValue u;
    145 };
    146 
    147 struct Tag {
    148 	char *name;
    149 	struct ChoStyle *style;
    150 	struct ChoStylePresence style_presence;
    151 	struct Attr **attrs;
    152 	bool is_closed;
    153 };
    154 
    155 struct GridContext {
    156 	int cells;
    157 	int left;
    158 	int right;
    159 	int bar_line_symbol_count;
    160 	int bar_line_symbol_in_line_count;
    161 	int tokens_per_cell;
    162 	int expected_tokens_per_cell;
    163 };
    164 
    165 typedef int index_t;
    166 
    167 struct ChoContext {
    168 	bool is_chord_already_initialized;
    169 	bool is_maybe_end_of_tab_directive;
    170 	bool directive_has_tag;
    171 	const char *chordpro_filepath;
    172 	enum State state;
    173 	enum State state_before_comment;
    174 	enum State state_before_tag;
    175 	enum State state_before_backslash;
    176 	enum State state_before_metadata_substitution;
    177 	enum TextType current_ttype;
    178 	enum TextType prev_ttype;
    179 	index_t ann;  // annotation
    180 	index_t at;   // struct Attr
    181 	index_t atn;  // struct Attr.name
    182 	index_t atv;  // struct Attr.value
    183 	index_t ch;   // struct ChoChord
    184 	index_t dia;  // struct ChordDiagram
    185 	index_t dn;   // char directive_name
    186 	index_t dv;   // char directive_value
    187 	index_t gt;   // char grid_token
    188 	index_t ia;   // struct ChoImage
    189 	index_t li;   // struct ChoLine
    190 	index_t lia;  // struct ChoLineItemAbove
    191 	index_t lii;  // struct ChoLineItem
    192 	index_t m;    // struct ChoMetadata
    193 	index_t ms;   // metadata_substitution
    194 	index_t se;   // struct ChoSection
    195 	index_t so;   // struct ChoSong
    196 	index_t t;    // char tag_start/tag_end
    197 	index_t ta;   // struct Tag
    198 	index_t te;   // struct ChoText
    199 	index_t th;   // transpose_history
    200 	int text_above_pos;
    201 	int nested_level;
    202 	size_t line_no;
    203 	int *transpose_history;
    204 	int *transpose;
    205 	struct ChoSong **songs;
    206 	struct Tag **tags;
    207 	struct Config *config;
    208 	struct ChoImage **image_assets;
    209 	struct GridContext grid;
    210 };
    211 
    212 void cho_log_enable_info_logs(void);
    213 
    214 struct ChoSong **cho_songs_parse(const char *str, const char *chordpro_filepath, struct Config *config);
    215 int cho_song_count(struct ChoSong **songs);
    216 int cho_song_compare(const void *a, const void *b);
    217 void cho_songs_free(struct ChoSong **song);
    218 
    219 int cho_line_item_count(struct ChoLineItem **items);
    220 char *cho_chord_name_generate(struct ChoChord *chord);
    221 char *cho_chord_name_generate_common(struct ChoChord *chord, struct Config *config);
    222 
    223 void cho_chords_add(struct ChoChord ***chords, struct ChoChord *chord);
    224 bool cho_chords_has(struct ChoChord **chords, struct ChoChord *chord);
    225 size_t cho_chord_count(struct ChoChord **chords);
    226 int cho_chord_compare(const void *a, const void *b);
    227 void cho_chords_free(struct ChoChord **chords);
    228 
    229 void cho_texts_free(struct ChoText **texts);
    230 
    231 bool cho_metadata_value(struct ChoMetadata **metadata, const char *name, const char *separator, char **value, struct ChoStyle **value_style);
    232 
    233 struct ChoStyle *cho_style_new(void);
    234 void cho_style_free(struct ChoStyle *style);
    235 struct ChoStyle *cho_style_copy(struct ChoStyle *style);
    236 void cho_style_print_as_toml(struct ChoStyle *style, const char *section);
    237 
    238 struct RGBColor *cho_rgbcolor_new(uint8_t red, uint8_t green, uint8_t blue);
    239 struct RGBColor *cho_color_parse(const char *str);
    240 struct RGBColor *cho_color_copy(struct RGBColor *color);
    241 enum LineStyle cho_linestyle_parse(const char *str, bool *error);
    242 
    243 // const char *cho_image_name_create(struct ChoImage *image, const char *dirname);
    244 
    245 void cho_font_free(struct Font *font);
    246 void cho_font_print(struct Font *font);
    247 struct Font *cho_font_copy(struct Font *font);
    248 void cho_fonts_free(struct Font **fonts);
    249 char *cho_font_name_normalize(const char *name);
    250 enum FontStyle cho_font_style_parse(const char *str, bool *error);
    251 const char *cho_font_style_to_config_string(enum FontStyle style);
    252 enum FontWeight cho_font_weight_parse(const char *str, bool *error);
    253 const char *cho_font_weight_to_config_string(enum FontWeight weight);
    254 
    255 void cho_debug_chord_print(struct ChoChord *chord);
    256 void cho_debug_style_print(struct ChoStyle *style);
    257 // void cho_debug_songs_print(struct ChoSong **songs);
    258 
    259 #endif /* _CHORDPRO_H_ */