fterrors.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /****************************************************************************
  2. *
  3. * fterrors.h
  4. *
  5. * FreeType error code handling (specification).
  6. *
  7. * Copyright (C) 1996-2023 by
  8. * David Turner, Robert Wilhelm, and Werner Lemberg.
  9. *
  10. * This file is part of the FreeType project, and may only be used,
  11. * modified, and distributed under the terms of the FreeType project
  12. * license, LICENSE.TXT. By continuing to use, modify, or distribute
  13. * this file you indicate that you have read the license and
  14. * understand and accept it fully.
  15. *
  16. */
  17. /**************************************************************************
  18. *
  19. * @section:
  20. * error_enumerations
  21. *
  22. * @title:
  23. * Error Enumerations
  24. *
  25. * @abstract:
  26. * How to handle errors and error strings.
  27. *
  28. * @description:
  29. * The header file `fterrors.h` (which is automatically included by
  30. * `freetype.h`) defines the handling of FreeType's enumeration
  31. * constants. It can also be used to generate error message strings
  32. * with a small macro trick explained below.
  33. *
  34. * **Error Formats**
  35. *
  36. * The configuration macro `FT_CONFIG_OPTION_USE_MODULE_ERRORS` can be
  37. * defined in `ftoption.h` in order to make the higher byte indicate the
  38. * module where the error has happened (this is not compatible with
  39. * standard builds of FreeType~2, however). See the file `ftmoderr.h`
  40. * for more details.
  41. *
  42. * **Error Message Strings**
  43. *
  44. * Error definitions are set up with special macros that allow client
  45. * applications to build a table of error message strings. The strings
  46. * are not included in a normal build of FreeType~2 to save space (most
  47. * client applications do not use them).
  48. *
  49. * To do so, you have to define the following macros before including
  50. * this file.
  51. *
  52. * ```
  53. * FT_ERROR_START_LIST
  54. * ```
  55. *
  56. * This macro is called before anything else to define the start of the
  57. * error list. It is followed by several `FT_ERROR_DEF` calls.
  58. *
  59. * ```
  60. * FT_ERROR_DEF( e, v, s )
  61. * ```
  62. *
  63. * This macro is called to define one single error. 'e' is the error
  64. * code identifier (e.g., `Invalid_Argument`), 'v' is the error's
  65. * numerical value, and 's' is the corresponding error string.
  66. *
  67. * ```
  68. * FT_ERROR_END_LIST
  69. * ```
  70. *
  71. * This macro ends the list.
  72. *
  73. * Additionally, you have to undefine `FTERRORS_H_` before #including
  74. * this file.
  75. *
  76. * Here is a simple example.
  77. *
  78. * ```
  79. * #undef FTERRORS_H_
  80. * #define FT_ERRORDEF( e, v, s ) { e, s },
  81. * #define FT_ERROR_START_LIST {
  82. * #define FT_ERROR_END_LIST { 0, NULL } };
  83. *
  84. * const struct
  85. * {
  86. * int err_code;
  87. * const char* err_msg;
  88. * } ft_errors[] =
  89. *
  90. * #include <freetype/fterrors.h>
  91. * ```
  92. *
  93. * An alternative to using an array is a switch statement.
  94. *
  95. * ```
  96. * #undef FTERRORS_H_
  97. * #define FT_ERROR_START_LIST switch ( error_code ) {
  98. * #define FT_ERRORDEF( e, v, s ) case v: return s;
  99. * #define FT_ERROR_END_LIST }
  100. * ```
  101. *
  102. * If you use `FT_CONFIG_OPTION_USE_MODULE_ERRORS`, `error_code` should
  103. * be replaced with `FT_ERROR_BASE(error_code)` in the last example.
  104. */
  105. /* */
  106. /* In previous FreeType versions we used `__FTERRORS_H__`. However, */
  107. /* using two successive underscores in a non-system symbol name */
  108. /* violates the C (and C++) standard, so it was changed to the */
  109. /* current form. In spite of this, we have to make */
  110. /* */
  111. /* ``` */
  112. /* #undefine __FTERRORS_H__ */
  113. /* ``` */
  114. /* */
  115. /* work for backward compatibility. */
  116. /* */
  117. #if !( defined( FTERRORS_H_ ) && defined ( __FTERRORS_H__ ) )
  118. #define FTERRORS_H_
  119. #define __FTERRORS_H__
  120. /* include module base error codes */
  121. #include <freetype/ftmoderr.h>
  122. /*******************************************************************/
  123. /*******************************************************************/
  124. /***** *****/
  125. /***** SETUP MACROS *****/
  126. /***** *****/
  127. /*******************************************************************/
  128. /*******************************************************************/
  129. #undef FT_NEED_EXTERN_C
  130. /* FT_ERR_PREFIX is used as a prefix for error identifiers. */
  131. /* By default, we use `FT_Err_`. */
  132. /* */
  133. #ifndef FT_ERR_PREFIX
  134. #define FT_ERR_PREFIX FT_Err_
  135. #endif
  136. /* FT_ERR_BASE is used as the base for module-specific errors. */
  137. /* */
  138. #ifdef FT_CONFIG_OPTION_USE_MODULE_ERRORS
  139. #ifndef FT_ERR_BASE
  140. #define FT_ERR_BASE FT_Mod_Err_Base
  141. #endif
  142. #else
  143. #undef FT_ERR_BASE
  144. #define FT_ERR_BASE 0
  145. #endif /* FT_CONFIG_OPTION_USE_MODULE_ERRORS */
  146. /* If FT_ERRORDEF is not defined, we need to define a simple */
  147. /* enumeration type. */
  148. /* */
  149. #ifndef FT_ERRORDEF
  150. #define FT_INCLUDE_ERR_PROTOS
  151. #define FT_ERRORDEF( e, v, s ) e = v,
  152. #define FT_ERROR_START_LIST enum {
  153. #define FT_ERROR_END_LIST FT_ERR_CAT( FT_ERR_PREFIX, Max ) };
  154. #ifdef __cplusplus
  155. #define FT_NEED_EXTERN_C
  156. extern "C" {
  157. #endif
  158. #endif /* !FT_ERRORDEF */
  159. /* this macro is used to define an error */
  160. #define FT_ERRORDEF_( e, v, s ) \
  161. FT_ERRORDEF( FT_ERR_CAT( FT_ERR_PREFIX, e ), v + FT_ERR_BASE, s )
  162. /* this is only used for <module>_Err_Ok, which must be 0! */
  163. #define FT_NOERRORDEF_( e, v, s ) \
  164. FT_ERRORDEF( FT_ERR_CAT( FT_ERR_PREFIX, e ), v, s )
  165. #ifdef FT_ERROR_START_LIST
  166. FT_ERROR_START_LIST
  167. #endif
  168. /* now include the error codes */
  169. #include <freetype/fterrdef.h>
  170. #ifdef FT_ERROR_END_LIST
  171. FT_ERROR_END_LIST
  172. #endif
  173. /*******************************************************************/
  174. /*******************************************************************/
  175. /***** *****/
  176. /***** SIMPLE CLEANUP *****/
  177. /***** *****/
  178. /*******************************************************************/
  179. /*******************************************************************/
  180. #ifdef FT_NEED_EXTERN_C
  181. }
  182. #endif
  183. #undef FT_ERROR_START_LIST
  184. #undef FT_ERROR_END_LIST
  185. #undef FT_ERRORDEF
  186. #undef FT_ERRORDEF_
  187. #undef FT_NOERRORDEF_
  188. #undef FT_NEED_EXTERN_C
  189. #undef FT_ERR_BASE
  190. /* FT_ERR_PREFIX is needed internally */
  191. #ifndef FT2_BUILD_LIBRARY
  192. #undef FT_ERR_PREFIX
  193. #endif
  194. /* FT_INCLUDE_ERR_PROTOS: Control whether function prototypes should be */
  195. /* included with */
  196. /* */
  197. /* #include <freetype/fterrors.h> */
  198. /* */
  199. /* This is only true where `FT_ERRORDEF` is */
  200. /* undefined. */
  201. /* */
  202. /* FT_ERR_PROTOS_DEFINED: Actual multiple-inclusion protection of */
  203. /* `fterrors.h`. */
  204. #ifdef FT_INCLUDE_ERR_PROTOS
  205. #undef FT_INCLUDE_ERR_PROTOS
  206. #ifndef FT_ERR_PROTOS_DEFINED
  207. #define FT_ERR_PROTOS_DEFINED
  208. FT_BEGIN_HEADER
  209. /**************************************************************************
  210. *
  211. * @function:
  212. * FT_Error_String
  213. *
  214. * @description:
  215. * Retrieve the description of a valid FreeType error code.
  216. *
  217. * @input:
  218. * error_code ::
  219. * A valid FreeType error code.
  220. *
  221. * @return:
  222. * A C~string or `NULL`, if any error occurred.
  223. *
  224. * @note:
  225. * FreeType has to be compiled with `FT_CONFIG_OPTION_ERROR_STRINGS` or
  226. * `FT_DEBUG_LEVEL_ERROR` to get meaningful descriptions.
  227. * 'error_string' will be `NULL` otherwise.
  228. *
  229. * Module identification will be ignored:
  230. *
  231. * ```c
  232. * strcmp( FT_Error_String( FT_Err_Unknown_File_Format ),
  233. * FT_Error_String( BDF_Err_Unknown_File_Format ) ) == 0;
  234. * ```
  235. */
  236. FT_EXPORT( const char* )
  237. FT_Error_String( FT_Error error_code );
  238. /* */
  239. FT_END_HEADER
  240. #endif /* FT_ERR_PROTOS_DEFINED */
  241. #endif /* FT_INCLUDE_ERR_PROTOS */
  242. #endif /* !(FTERRORS_H_ && __FTERRORS_H__) */
  243. /* END */