| 1 | /* Copyright (C) 2000-2012 by George Williams */ |
| 2 | /* |
| 3 | * Redistribution and use in source and binary forms, with or without |
| 4 | * modification, are permitted provided that the following conditions are met: |
| 5 | |
| 6 | * Redistributions of source code must retain the above copyright notice, this |
| 7 | * list of conditions and the following disclaimer. |
| 8 | |
| 9 | * Redistributions in binary form must reproduce the above copyright notice, |
| 10 | * this list of conditions and the following disclaimer in the documentation |
| 11 | * and/or other materials provided with the distribution. |
| 12 | |
| 13 | * The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #ifndef FONTFORGE_BASICS_H |
| 29 | #define FONTFORGE_BASICS_H |
| 30 | |
| 31 | #include <fontforge-config.h> |
| 32 | |
| 33 | #include <inttypes.h> |
| 34 | #include <limits.h> |
| 35 | #include <memory.h> |
| 36 | #include <stdbool.h> |
| 37 | #include <stdint.h> |
| 38 | #include <stdio.h> /* for NULL */ |
| 39 | #include <stdlib.h> /* for free */ |
| 40 | #include <string.h> |
| 41 | |
| 42 | typedef uint32_t unichar_t; |
| 43 | |
| 44 | #define FF_PI 3.1415926535897932 |
| 45 | |
| 46 | #if defined(_WIN32) && !defined(LIBINTL_VERSION) |
| 47 | # define vsnprintf _vsprintf_p |
| 48 | #endif |
| 49 | |
| 50 | /* A macro to mark unused function parameters with. We often |
| 51 | * have such parameters, because of extensive use of callbacks. |
| 52 | */ |
| 53 | #ifdef UNUSED |
| 54 | #elif defined(__GNUC__) |
| 55 | # define UNUSED(x) UNUSED_ ## x __attribute__((unused)) |
| 56 | #elif defined(__LCLINT__) |
| 57 | # define UNUSED(x) /*@unused@*/ x |
| 58 | #else |
| 59 | # define UNUSED(x) x |
| 60 | #endif |
| 61 | |
| 62 | /* A macro to print a string for debug purposes |
| 63 | */ |
| 64 | #ifndef NDEBUG |
| 65 | #define TRACE(...) fprintf(stderr, __VA_ARGS__) |
| 66 | #else |
| 67 | #define TRACE(...) do {} while(0) |
| 68 | #endif |
| 69 | |
| 70 | /* assert() with an otherwise unused variable |
| 71 | * to get around "unused" compiler warnings |
| 72 | */ |
| 73 | #ifndef NDEBUG |
| 74 | #define VASSERT(v) assert(v) |
| 75 | #else |
| 76 | #define VASSERT(v) ((void)(v)) |
| 77 | #endif |
| 78 | |
| 79 | #ifdef __cplusplus |
| 80 | extern "C" { |
| 81 | #endif |
| 82 | |
| 83 | extern void NoMoreMemMessage(void); |
| 84 | extern void ExpandBuffer(void** p_buf, size_t elem_size, size_t increment, |
| 85 | int* p_maxalloc); |
| 86 | |
| 87 | #ifdef __cplusplus |
| 88 | } |
| 89 | #endif |
| 90 | |
| 91 | static inline int imin(int a, int b) |
| 92 | { |
| 93 | return (a < b) ? a : b; |
| 94 | } |
| 95 | |
| 96 | static inline int imax(int a, int b) |
| 97 | { |
| 98 | return (a < b) ? b : a; |
| 99 | } |
| 100 | |
| 101 | #define IS_IN_ORDER3( a, b, c ) ( ((a)<=(b)) && ((b)<=(c)) ) |
| 102 | |
| 103 | |
| 104 | /** |
| 105 | * Many lists in FontForge are singly linked. At times you might want |
| 106 | * to append to the list which, when you only have a pointer to the |
| 107 | * start of the list can be more verbose than one would like. To use |
| 108 | * this macro you must defined a null initialized variable 'last' |
| 109 | * outside of any loop that traverses the source list. The last |
| 110 | * variable is used used by this macro to quickly append to the list |
| 111 | * as you go. This macro also assumes that the 'last' and 'newitem' |
| 112 | * types have a member "->next" which contains the single linked list |
| 113 | * pointer to the next element. |
| 114 | * |
| 115 | * Efficient list append should really be a one line call in the bulk |
| 116 | * of the code :) |
| 117 | * |
| 118 | * example: |
| 119 | * MyListObjectType* newfoolast = 0; |
| 120 | * MyListObjectType* newfoolist = 0; |
| 121 | * |
| 122 | * for( ... iterate a source collection of foos ... ) |
| 123 | * { |
| 124 | * MyListObjectType* foocopy = CopyIt( foo ); |
| 125 | * FFLIST_SINGLE_LINKED_APPEND( newfoolist, newfoolast, foocopy ); |
| 126 | * } |
| 127 | */ |
| 128 | #define FFLIST_SINGLE_LINKED_APPEND( head, last, newitem ) \ |
| 129 | if ( !last ) \ |
| 130 | { \ |
| 131 | newitem->next = 0; \ |
| 132 | head = last = newitem; \ |
| 133 | } \ |
| 134 | else \ |
| 135 | { \ |
| 136 | last->next = newitem; \ |
| 137 | last = newitem; \ |
| 138 | } |
| 139 | |
| 140 | #ifdef __GNU__ |
| 141 | // This is for GNU Hurd, not GCC. |
| 142 | // cf. <https://www.gnu.org/software/hurd/community/gsoc/project_ideas/maxpath.html> |
| 143 | # ifndef PATH_MAX |
| 144 | # define PATH_MAX 4096 |
| 145 | # endif |
| 146 | # ifndef MAXPATHLEN |
| 147 | # define MAXPATHLEN 4096 |
| 148 | # endif |
| 149 | #endif |
| 150 | |
| 151 | /* Common MIN/MAX macros */ |
| 152 | #ifndef MAX |
| 153 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) |
| 154 | #endif |
| 155 | #ifndef MIN |
| 156 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) |
| 157 | #endif |
| 158 | |
| 159 | #endif /* FONTFORGE_BASICS_H */ |