pmh_styleparser.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* PEG Markdown Highlight
  2. * Copyright 2011-2016 Ali Rantakari -- http://hasseg.org
  3. * Licensed under the GPL2+ and MIT licenses (see LICENSE for more info).
  4. *
  5. * pmh_styleparser.h
  6. *
  7. * Public interface of a parser for custom syntax highlighting stylesheets.
  8. */
  9. /** \file
  10. * \brief Style parser public interface.
  11. */
  12. #include "pmh_definitions.h"
  13. #include <stdbool.h>
  14. /**
  15. * \brief Color (ARGB) attribute value.
  16. *
  17. * All values are 0-255.
  18. */
  19. typedef struct
  20. {
  21. int red; /**< Red color component (0-255) */
  22. int green; /**< Green color component (0-255) */
  23. int blue; /**< Blue color component (0-255) */
  24. int alpha; /**< Alpha (opacity) color component (0-255) */
  25. } pmh_attr_argb_color;
  26. /** \brief Font style attribute value. */
  27. typedef struct
  28. {
  29. bool italic;
  30. bool bold;
  31. bool underlined;
  32. bool strikeout;
  33. } pmh_attr_font_styles;
  34. /** \brief Font size attribute value. */
  35. typedef struct
  36. {
  37. int size_pt; /**< The font point size */
  38. bool is_relative; /**< Whether the size is relative (i.e. size_pt points
  39. larger than the default font) */
  40. } pmh_attr_font_size;
  41. /** \brief Style attribute types. */
  42. typedef enum
  43. {
  44. pmh_attr_type_foreground_color, /**< Foreground color */
  45. pmh_attr_type_background_color, /**< Background color */
  46. pmh_attr_type_caret_color, /**< Caret (insertion point) color */
  47. pmh_attr_type_font_size_pt, /**< Font size (in points) */
  48. pmh_attr_type_font_family, /**< Font family */
  49. pmh_attr_type_font_style, /**< Font style */
  50. pmh_attr_type_strike_color, /**< Strike-through color */
  51. pmh_attr_type_other /**< Arbitrary custom attribute */
  52. } pmh_attr_type;
  53. /**
  54. * \brief Style attribute value.
  55. *
  56. * Determine which member to access in this union based on the
  57. * 'type' value of the pmh_style_attribute.
  58. *
  59. * \sa pmh_style_attribute
  60. */
  61. typedef union
  62. {
  63. pmh_attr_argb_color *argb_color; /**< ARGB color */
  64. pmh_attr_font_styles *font_styles; /**< Font styles */
  65. pmh_attr_font_size *font_size; /**< Font size */
  66. char *font_family; /**< Font family */
  67. char *string; /**< Arbitrary custom string value
  68. (use this if the attribute's type
  69. is pmh_attr_type_other) */
  70. } pmh_attr_value;
  71. /** \brief Style attribute. */
  72. typedef struct pmh_style_attribute
  73. {
  74. pmh_element_type lang_element_type; /**< The Markdown language element this
  75. style applies to */
  76. pmh_attr_type type; /**< The type of the attribute */
  77. char *name; /**< The name of the attribute (if type
  78. is pmh_attr_type_other, you can
  79. use this value to determine what
  80. the attribute is) */
  81. pmh_attr_value *value; /**< The value of the attribute */
  82. struct pmh_style_attribute *next; /**< Next attribute in linked list */
  83. } pmh_style_attribute;
  84. /** \brief Collection of styles. */
  85. typedef struct
  86. {
  87. /** Styles that apply to the editor in general */
  88. pmh_style_attribute *editor_styles;
  89. /** Styles that apply to the line in the editor where the caret (insertion
  90. point) resides */
  91. pmh_style_attribute *editor_current_line_styles;
  92. /** Styles that apply to the range of selected text in the editor */
  93. pmh_style_attribute *editor_selection_styles;
  94. /** Styles that apply to specific Markdown language elements */
  95. pmh_style_attribute **element_styles;
  96. } pmh_style_collection;
  97. /**
  98. * \brief Parse stylesheet string, return style collection
  99. *
  100. * \param[in] input The stylesheet string to parse.
  101. * \param[in] error_callback Callback function to be called when errors
  102. * occur during parsing. The first argument
  103. * to the callback function is the error
  104. * message and the second one the line number
  105. * in the original input where the error
  106. * occurred. The last argument will always
  107. * get the value you pass in for the
  108. * error_callback_context argument to this
  109. * function.
  110. * Pass in NULL to suppress error reporting.
  111. * \param[in] error_callback_context Arbitrary context pointer for the error
  112. * callback function; will be passed in as
  113. * the last argument to error_callback.
  114. *
  115. * \return A pmh_style_collection. You must pass this value to
  116. * pmh_free_style_collection() when it's not needed anymore.
  117. */
  118. pmh_style_collection *pmh_parse_styles(char *input,
  119. void(*error_callback)(char*,int,void*),
  120. void *error_callback_context);
  121. /**
  122. * \brief Free a pmh_style_collection.
  123. *
  124. * Frees a pmh_style_collection value returned by pmh_parse_styles().
  125. *
  126. * \param[in] collection The collection to free.
  127. */
  128. void pmh_free_style_collection(pmh_style_collection *collection);
  129. char *pmh_attr_name_from_type(pmh_attr_type type);
  130. pmh_attr_type pmh_attr_type_from_name(char *name);