]> granicus.if.org Git - libass/blob - libass/ass_utils.h
Add kerning toggle for HarfBuzz shaping
[libass] / libass / ass_utils.h
1 /*
2  * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
3  *
4  * This file is part of libass.
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18
19 #ifndef LIBASS_UTILS_H
20 #define LIBASS_UTILS_H
21
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <assert.h>
28
29 #ifdef CONFIG_ENCA
30 #include <enca.h>
31 #endif
32
33 #include "ass.h"
34
35 #define MSGL_FATAL 0
36 #define MSGL_ERR 1
37 #define MSGL_WARN 2
38 #define MSGL_INFO 4
39 #define MSGL_V 6
40 #define MSGL_DBG2 7
41
42 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
43 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
44 #define FFMINMAX(c,a,b) FFMIN(FFMAX(c, a), b)
45
46 int mystrtoi(char **p, int *res);
47 int mystrtoll(char **p, long long *res);
48 int mystrtou32(char **p, int base, uint32_t *res);
49 int mystrtod(char **p, double *res);
50 int strtocolor(ASS_Library *library, char **q, uint32_t *res, int hex);
51 char parse_bool(char *str);
52 unsigned ass_utf8_get_char(char **str);
53 void ass_msg(ASS_Library *priv, int lvl, char *fmt, ...);
54 #ifdef CONFIG_ENCA
55 void *ass_guess_buffer_cp(ASS_Library *library, unsigned char *buffer,
56                           int buflen, char *preferred_language,
57                           char *fallback);
58 #endif
59
60 /* defined in ass_strtod.c */
61 double ass_strtod(const char *string, char **endPtr);
62
63 static inline int d6_to_int(int x)
64 {
65     return (x + 32) >> 6;
66 }
67 static inline int d16_to_int(int x)
68 {
69     return (x + 32768) >> 16;
70 }
71 static inline int int_to_d6(int x)
72 {
73     return x << 6;
74 }
75 static inline int int_to_d16(int x)
76 {
77     return x << 16;
78 }
79 static inline int d16_to_d6(int x)
80 {
81     return (x + 512) >> 10;
82 }
83 static inline int d6_to_d16(int x)
84 {
85     return x << 10;
86 }
87 static inline double d6_to_double(int x)
88 {
89     return x / 64.;
90 }
91 static inline int double_to_d6(double x)
92 {
93     return (int) (x * 64);
94 }
95 static inline double d16_to_double(int x)
96 {
97     return ((double) x) / 0x10000;
98 }
99 static inline int double_to_d16(double x)
100 {
101     return (int) (x * 0x10000);
102 }
103 static inline double d22_to_double(int x)
104 {
105     return ((double) x) / 0x400000;
106 }
107 static inline int double_to_d22(double x)
108 {
109     return (int) (x * 0x400000);
110 }
111
112 // Calculate cache key for a rotational angle in degrees
113 static inline int rot_key(double a)
114 {
115     const int m = double_to_d22(360.0);
116     return double_to_d22(a) % m;
117 }
118
119 #define FNV1_32A_INIT (unsigned)0x811c9dc5
120
121 static inline unsigned fnv_32a_buf(void *buf, size_t len, unsigned hval)
122 {
123     unsigned char *bp = buf;
124     unsigned char *be = bp + len;
125     while (bp < be) {
126         hval ^= (unsigned) *bp++;
127         hval +=
128             (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) +
129             (hval << 24);
130     }
131     return hval;
132 }
133 static inline unsigned fnv_32a_str(char *str, unsigned hval)
134 {
135     unsigned char *s = (unsigned char *) str;
136     while (*s) {
137         hval ^= (unsigned) *s++;
138         hval +=
139             (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) +
140             (hval << 24);
141     }
142     return hval;
143 }
144
145 #endif                          /* LIBASS_UTILS_H */