Relicense to ISC
[libass.git] / libass / ass_cache.h
blob472bf359bedb6978b8d2c07c22e9542087090584
1 /*
2 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
4 * This file is part of libass.
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.
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.
19 #ifndef LIBASS_CACHE_H
20 #define LIBASS_CACHE_H
22 #include "ass.h"
23 #include "ass_font.h"
24 #include "ass_bitmap.h"
26 typedef void (*HashmapItemDtor) (void *key, size_t key_size,
27 void *value, size_t value_size);
28 typedef int (*HashmapKeyCompare) (void *key1, void *key2,
29 size_t key_size);
30 typedef unsigned (*HashmapHash) (void *key, size_t key_size);
32 typedef struct hashmap_item {
33 void *key;
34 void *value;
35 struct hashmap_item *next;
36 } HashmapItem;
37 typedef HashmapItem *hashmap_item_p;
39 typedef struct {
40 int nbuckets;
41 size_t key_size, value_size;
42 hashmap_item_p *root;
43 HashmapItemDtor item_dtor; // a destructor for hashmap key/value pairs
44 HashmapKeyCompare key_compare;
45 HashmapHash hash;
46 size_t cache_size;
47 // stats
48 int hit_count;
49 int miss_count;
50 int count;
51 ASS_Library *library;
52 } Hashmap;
54 Hashmap *hashmap_init(ASS_Library *library, size_t key_size,
55 size_t value_size, int nbuckets,
56 HashmapItemDtor item_dtor,
57 HashmapKeyCompare key_compare,
58 HashmapHash hash);
59 void hashmap_done(Hashmap *map);
60 void *hashmap_insert(Hashmap *map, void *key, void *value);
61 void *hashmap_find(Hashmap *map, void *key);
63 Hashmap *ass_font_cache_init(ASS_Library *library);
64 ASS_Font *ass_font_cache_find(Hashmap *, ASS_FontDesc *desc);
65 void *ass_font_cache_add(Hashmap *, ASS_Font *font);
66 void ass_font_cache_done(Hashmap *);
68 // Create definitions for bitmap_hash_key and glyph_hash_key
69 #define CREATE_STRUCT_DEFINITIONS
70 #include "ass_cache_template.h"
72 typedef struct {
73 Bitmap *bm; // the actual bitmaps
74 Bitmap *bm_o;
75 Bitmap *bm_s;
76 } BitmapHashValue;
78 Hashmap *ass_bitmap_cache_init(ASS_Library *library);
79 void *cache_add_bitmap(Hashmap *, BitmapHashKey *key,
80 BitmapHashValue *val);
81 BitmapHashValue *cache_find_bitmap(Hashmap *bitmap_cache,
82 BitmapHashKey *key);
83 Hashmap *ass_bitmap_cache_reset(Hashmap *bitmap_cache);
84 void ass_bitmap_cache_done(Hashmap *bitmap_cache);
87 typedef struct {
88 unsigned char *a;
89 unsigned char *b;
90 } CompositeHashValue;
92 Hashmap *ass_composite_cache_init(ASS_Library *library);
93 void *cache_add_composite(Hashmap *, CompositeHashKey *key,
94 CompositeHashValue *val);
95 CompositeHashValue *cache_find_composite(Hashmap *composite_cache,
96 CompositeHashKey *key);
97 Hashmap *ass_composite_cache_reset(Hashmap *composite_cache);
98 void ass_composite_cache_done(Hashmap *composite_cache);
101 typedef struct {
102 FT_Glyph glyph;
103 FT_Glyph outline_glyph;
104 FT_BBox bbox_scaled; // bbox after scaling, but before rotation
105 FT_Vector advance; // 26.6, advance distance to the next bitmap in line
106 int asc, desc; // ascender/descender of a drawing
107 } GlyphHashValue;
109 Hashmap *ass_glyph_cache_init(ASS_Library *library);
110 void *cache_add_glyph(Hashmap *, GlyphHashKey *key,
111 GlyphHashValue *val);
112 GlyphHashValue *cache_find_glyph(Hashmap *glyph_cache,
113 GlyphHashKey *key);
114 Hashmap *ass_glyph_cache_reset(Hashmap *glyph_cache);
115 void ass_glyph_cache_done(Hashmap *glyph_cache);
117 #endif /* LIBASS_CACHE_H */