annotate libass/ass_shaper.c @ 36217:98774518a470

Detect one more case of software OpenGL implementation.
author reimar
date Sun, 09 Jun 2013 14:15:09 +0000
parents 4f4a90b80a9a
children c3aaaf17c721
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
34300
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
1 /*
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
2 * Copyright (C) 2011 Grigori Goronzy <greg@chown.ath.cx>
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
3 *
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
4 * This file is part of libass.
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
5 *
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
6 * Permission to use, copy, modify, and distribute this software for any
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
7 * purpose with or without fee is hereby granted, provided that the above
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
8 * copyright notice and this permission notice appear in all copies.
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
9 *
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
17 */
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
18
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
19 #include "config.h"
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
20
34342
575ad51cc996 Allow compiling libass without fribidi again.
reimar
parents: 34300
diff changeset
21 #ifdef CONFIG_FRIBIDI
34300
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
22 #include <fribidi/fribidi.h>
34342
575ad51cc996 Allow compiling libass without fribidi again.
reimar
parents: 34300
diff changeset
23 #endif
34300
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
24
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
25 #include "ass_shaper.h"
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
26 #include "ass_render.h"
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
27 #include "ass_font.h"
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
28 #include "ass_parse.h"
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
29 #include "ass_cache.h"
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
30
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
31 #define MAX_RUNS 50
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
32
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
33 #ifdef CONFIG_HARFBUZZ
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
34 #include <hb-ft.h>
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
35 enum {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
36 VERT = 0,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
37 VKNA,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
38 KERN
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
39 };
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
40 #define NUM_FEATURES 3
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
41 #endif
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
42
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
43 struct ass_shaper {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
44 ASS_ShapingLevel shaping_level;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
45
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
46 // FriBidi log2vis
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
47 int n_glyphs;
34342
575ad51cc996 Allow compiling libass without fribidi again.
reimar
parents: 34300
diff changeset
48 #ifdef CONFIG_FRIBIDI
34300
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
49 FriBidiChar *event_text;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
50 FriBidiCharType *ctypes;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
51 FriBidiLevel *emblevels;
34342
575ad51cc996 Allow compiling libass without fribidi again.
reimar
parents: 34300
diff changeset
52 #endif
34300
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
53 FriBidiStrIndex *cmap;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
54 FriBidiParType base_direction;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
55
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
56 #ifdef CONFIG_HARFBUZZ
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
57 // OpenType features
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
58 int n_features;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
59 hb_feature_t *features;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
60 hb_language_t language;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
61
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
62 // Glyph metrics cache, to speed up shaping
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
63 Cache *metrics_cache;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
64 #endif
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
65 };
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
66
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
67 #ifdef CONFIG_HARFBUZZ
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
68 struct ass_shaper_metrics_data {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
69 Cache *metrics_cache;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
70 GlyphMetricsHashKey hash_key;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
71 int vertical;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
72 };
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
73
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
74 struct ass_shaper_font_data {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
75 hb_font_t *fonts[ASS_FONT_MAX_FACES];
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
76 hb_font_funcs_t *font_funcs[ASS_FONT_MAX_FACES];
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
77 struct ass_shaper_metrics_data *metrics_data[ASS_FONT_MAX_FACES];
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
78 };
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
79 #endif
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
80
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
81 /**
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
82 * \brief Print version information
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
83 */
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
84 void ass_shaper_info(ASS_Library *lib)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
85 {
34342
575ad51cc996 Allow compiling libass without fribidi again.
reimar
parents: 34300
diff changeset
86 ass_msg(lib, MSGL_V, "Shaper:"
575ad51cc996 Allow compiling libass without fribidi again.
reimar
parents: 34300
diff changeset
87 #ifdef CONFIG_FRIBIDI
575ad51cc996 Allow compiling libass without fribidi again.
reimar
parents: 34300
diff changeset
88 " FriBidi " FRIBIDI_VERSION " (SIMPLE)"
575ad51cc996 Allow compiling libass without fribidi again.
reimar
parents: 34300
diff changeset
89 #endif
34300
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
90 #ifdef CONFIG_HARFBUZZ
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
91 " HarfBuzz-ng %s (COMPLEX)", hb_version_string()
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
92 #endif
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
93 );
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
94 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
95
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
96 /**
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
97 * \brief grow arrays, if needed
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
98 * \param new_size requested size
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
99 */
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
100 static void check_allocations(ASS_Shaper *shaper, size_t new_size)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
101 {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
102 if (new_size > shaper->n_glyphs) {
34342
575ad51cc996 Allow compiling libass without fribidi again.
reimar
parents: 34300
diff changeset
103 #ifdef CONFIG_FRIBIDI
34300
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
104 shaper->event_text = realloc(shaper->event_text, sizeof(FriBidiChar) * new_size);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
105 shaper->ctypes = realloc(shaper->ctypes, sizeof(FriBidiCharType) * new_size);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
106 shaper->emblevels = realloc(shaper->emblevels, sizeof(FriBidiLevel) * new_size);
34342
575ad51cc996 Allow compiling libass without fribidi again.
reimar
parents: 34300
diff changeset
107 #endif
34300
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
108 shaper->cmap = realloc(shaper->cmap, sizeof(FriBidiStrIndex) * new_size);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
109 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
110 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
111
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
112 /**
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
113 * \brief Free shaper and related data
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
114 */
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
115 void ass_shaper_free(ASS_Shaper *shaper)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
116 {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
117 #ifdef CONFIG_HARFBUZZ
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
118 ass_cache_done(shaper->metrics_cache);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
119 free(shaper->features);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
120 #endif
34342
575ad51cc996 Allow compiling libass without fribidi again.
reimar
parents: 34300
diff changeset
121 #ifdef CONFIG_FRIBIDI
34300
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
122 free(shaper->event_text);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
123 free(shaper->ctypes);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
124 free(shaper->emblevels);
34342
575ad51cc996 Allow compiling libass without fribidi again.
reimar
parents: 34300
diff changeset
125 #endif
34300
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
126 free(shaper->cmap);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
127 free(shaper);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
128 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
129
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
130 void ass_shaper_font_data_free(ASS_ShaperFontData *priv)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
131 {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
132 #ifdef CONFIG_HARFBUZZ
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
133 int i;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
134 for (i = 0; i < ASS_FONT_MAX_FACES; i++)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
135 if (priv->fonts[i]) {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
136 free(priv->metrics_data[i]);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
137 hb_font_destroy(priv->fonts[i]);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
138 hb_font_funcs_destroy(priv->font_funcs[i]);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
139 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
140 free(priv);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
141 #endif
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
142 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
143
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
144 #ifdef CONFIG_HARFBUZZ
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
145 /**
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
146 * \brief set up the HarfBuzz OpenType feature list with some
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
147 * standard features.
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
148 */
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
149 static void init_features(ASS_Shaper *shaper)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
150 {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
151 shaper->features = calloc(sizeof(hb_feature_t), NUM_FEATURES);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
152
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
153 shaper->n_features = NUM_FEATURES;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
154 shaper->features[VERT].tag = HB_TAG('v', 'e', 'r', 't');
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
155 shaper->features[VERT].end = INT_MAX;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
156 shaper->features[VKNA].tag = HB_TAG('v', 'k', 'n', 'a');
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
157 shaper->features[VKNA].end = INT_MAX;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
158 shaper->features[KERN].tag = HB_TAG('k', 'e', 'r', 'n');
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
159 shaper->features[KERN].end = INT_MAX;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
160 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
161
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
162 /**
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
163 * \brief Set features depending on properties of the run
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
164 */
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
165 static void set_run_features(ASS_Shaper *shaper, GlyphInfo *info)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
166 {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
167 // enable vertical substitutions for @font runs
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
168 if (info->font->desc.vertical)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
169 shaper->features[VERT].value = shaper->features[VKNA].value = 1;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
170 else
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
171 shaper->features[VERT].value = shaper->features[VKNA].value = 0;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
172 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
173
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
174 /**
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
175 * \brief Update HarfBuzz's idea of font metrics
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
176 * \param hb_font HarfBuzz font
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
177 * \param face associated FreeType font face
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
178 */
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
179 static void update_hb_size(hb_font_t *hb_font, FT_Face face)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
180 {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
181 hb_font_set_scale (hb_font,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
182 ((uint64_t) face->size->metrics.x_scale * (uint64_t) face->units_per_EM) >> 16,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
183 ((uint64_t) face->size->metrics.y_scale * (uint64_t) face->units_per_EM) >> 16);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
184 hb_font_set_ppem (hb_font, face->size->metrics.x_ppem,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
185 face->size->metrics.y_ppem);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
186 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
187
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
188
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
189 /*
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
190 * Cached glyph metrics getters follow
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
191 *
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
192 * These functions replace HarfBuzz' standard FreeType font functions
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
193 * and provide cached access to essential glyph metrics. This usually
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
194 * speeds up shaping a lot. It also allows us to use custom load flags.
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
195 *
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
196 */
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
197
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
198 GlyphMetricsHashValue *
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
199 get_cached_metrics(struct ass_shaper_metrics_data *metrics, FT_Face face,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
200 hb_codepoint_t glyph)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
201 {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
202 GlyphMetricsHashValue *val;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
203
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
204 metrics->hash_key.glyph_index = glyph;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
205 val = ass_cache_get(metrics->metrics_cache, &metrics->hash_key);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
206
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
207 if (!val) {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
208 int load_flags = FT_LOAD_DEFAULT | FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
209 | FT_LOAD_IGNORE_TRANSFORM;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
210 GlyphMetricsHashValue new_val;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
211
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
212 if (FT_Load_Glyph(face, glyph, load_flags))
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
213 return NULL;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
214
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
215 memcpy(&new_val.metrics, &face->glyph->metrics, sizeof(FT_Glyph_Metrics));
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
216 val = ass_cache_put(metrics->metrics_cache, &metrics->hash_key, &new_val);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
217 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
218
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
219 return val;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
220 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
221
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
222 static hb_bool_t
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
223 get_glyph(hb_font_t *font, void *font_data, hb_codepoint_t unicode,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
224 hb_codepoint_t variation, hb_codepoint_t *glyph, void *user_data)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
225 {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
226 FT_Face face = font_data;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
227
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
228 if (variation)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
229 *glyph = FT_Face_GetCharVariantIndex(face, unicode, variation);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
230 else
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
231 *glyph = FT_Get_Char_Index(face, unicode);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
232
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
233 return *glyph != 0;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
234 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
235
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
236 static hb_position_t
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
237 cached_h_advance(hb_font_t *font, void *font_data, hb_codepoint_t glyph,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
238 void *user_data)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
239 {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
240 FT_Face face = font_data;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
241 struct ass_shaper_metrics_data *metrics_priv = user_data;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
242 GlyphMetricsHashValue *metrics = get_cached_metrics(metrics_priv, face, glyph);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
243
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
244 if (!metrics)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
245 return 0;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
246
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
247 if (metrics_priv->vertical && glyph > VERTICAL_LOWER_BOUND)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
248 return metrics->metrics.vertAdvance;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
249
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
250 return metrics->metrics.horiAdvance;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
251 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
252
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
253 static hb_position_t
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
254 cached_v_advance(hb_font_t *font, void *font_data, hb_codepoint_t glyph,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
255 void *user_data)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
256 {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
257 FT_Face face = font_data;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
258 struct ass_shaper_metrics_data *metrics_priv = user_data;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
259 GlyphMetricsHashValue *metrics = get_cached_metrics(metrics_priv, face, glyph);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
260
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
261 if (!metrics)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
262 return 0;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
263
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
264 return metrics->metrics.vertAdvance;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
265
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
266 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
267
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
268 static hb_bool_t
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
269 cached_h_origin(hb_font_t *font, void *font_data, hb_codepoint_t glyph,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
270 hb_position_t *x, hb_position_t *y, void *user_data)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
271 {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
272 return 1;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
273 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
274
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
275 static hb_bool_t
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
276 cached_v_origin(hb_font_t *font, void *font_data, hb_codepoint_t glyph,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
277 hb_position_t *x, hb_position_t *y, void *user_data)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
278 {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
279 FT_Face face = font_data;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
280 struct ass_shaper_metrics_data *metrics_priv = user_data;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
281 GlyphMetricsHashValue *metrics = get_cached_metrics(metrics_priv, face, glyph);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
282
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
283 if (!metrics)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
284 return 0;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
285
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
286 *x = metrics->metrics.horiBearingX - metrics->metrics.vertBearingX;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
287 *y = metrics->metrics.horiBearingY - (-metrics->metrics.vertBearingY);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
288
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
289 return 1;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
290 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
291
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
292 static hb_position_t
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
293 get_h_kerning(hb_font_t *font, void *font_data, hb_codepoint_t first,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
294 hb_codepoint_t second, void *user_data)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
295 {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
296 FT_Face face = font_data;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
297 FT_Vector kern;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
298
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
299 if (FT_Get_Kerning (face, first, second, FT_KERNING_DEFAULT, &kern))
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
300 return 0;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
301
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
302 return kern.x;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
303 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
304
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
305 static hb_position_t
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
306 get_v_kerning(hb_font_t *font, void *font_data, hb_codepoint_t first,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
307 hb_codepoint_t second, void *user_data)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
308 {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
309 return 0;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
310 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
311
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
312 static hb_bool_t
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
313 cached_extents(hb_font_t *font, void *font_data, hb_codepoint_t glyph,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
314 hb_glyph_extents_t *extents, void *user_data)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
315 {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
316 FT_Face face = font_data;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
317 struct ass_shaper_metrics_data *metrics_priv = user_data;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
318 GlyphMetricsHashValue *metrics = get_cached_metrics(metrics_priv, face, glyph);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
319
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
320 if (!metrics)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
321 return 0;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
322
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
323 extents->x_bearing = metrics->metrics.horiBearingX;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
324 extents->y_bearing = metrics->metrics.horiBearingY;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
325 extents->width = metrics->metrics.width;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
326 extents->height = metrics->metrics.height;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
327
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
328 return 1;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
329 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
330
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
331 static hb_bool_t
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
332 get_contour_point(hb_font_t *font, void *font_data, hb_codepoint_t glyph,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
333 unsigned int point_index, hb_position_t *x,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
334 hb_position_t *y, void *user_data)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
335 {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
336 FT_Face face = font_data;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
337 int load_flags = FT_LOAD_DEFAULT | FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
338 | FT_LOAD_IGNORE_TRANSFORM;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
339
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
340 if (FT_Load_Glyph(face, glyph, load_flags))
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
341 return 0;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
342
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
343 if (point_index >= (unsigned)face->glyph->outline.n_points)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
344 return 0;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
345
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
346 *x = face->glyph->outline.points[point_index].x;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
347 *y = face->glyph->outline.points[point_index].y;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
348
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
349 return 1;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
350 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
351
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
352 /**
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
353 * \brief Retrieve HarfBuzz font from cache.
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
354 * Create it from FreeType font, if needed.
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
355 * \param info glyph cluster
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
356 * \return HarfBuzz font
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
357 */
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
358 static hb_font_t *get_hb_font(ASS_Shaper *shaper, GlyphInfo *info)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
359 {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
360 ASS_Font *font = info->font;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
361 hb_font_t **hb_fonts;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
362
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
363 if (!font->shaper_priv)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
364 font->shaper_priv = calloc(sizeof(ASS_ShaperFontData), 1);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
365
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
366
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
367 hb_fonts = font->shaper_priv->fonts;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
368 if (!hb_fonts[info->face_index]) {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
369 hb_fonts[info->face_index] =
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
370 hb_ft_font_create(font->faces[info->face_index], NULL);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
371
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
372 // set up cached metrics access
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
373 font->shaper_priv->metrics_data[info->face_index] =
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
374 calloc(sizeof(struct ass_shaper_metrics_data), 1);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
375 struct ass_shaper_metrics_data *metrics =
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
376 font->shaper_priv->metrics_data[info->face_index];
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
377 metrics->metrics_cache = shaper->metrics_cache;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
378 metrics->vertical = info->font->desc.vertical;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
379
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
380 hb_font_funcs_t *funcs = hb_font_funcs_create();
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
381 font->shaper_priv->font_funcs[info->face_index] = funcs;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
382 hb_font_funcs_set_glyph_func(funcs, get_glyph,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
383 metrics, NULL);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
384 hb_font_funcs_set_glyph_h_advance_func(funcs, cached_h_advance,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
385 metrics, NULL);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
386 hb_font_funcs_set_glyph_v_advance_func(funcs, cached_v_advance,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
387 metrics, NULL);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
388 hb_font_funcs_set_glyph_h_origin_func(funcs, cached_h_origin,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
389 metrics, NULL);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
390 hb_font_funcs_set_glyph_v_origin_func(funcs, cached_v_origin,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
391 metrics, NULL);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
392 hb_font_funcs_set_glyph_h_kerning_func(funcs, get_h_kerning,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
393 metrics, NULL);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
394 hb_font_funcs_set_glyph_v_kerning_func(funcs, get_v_kerning,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
395 metrics, NULL);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
396 hb_font_funcs_set_glyph_extents_func(funcs, cached_extents,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
397 metrics, NULL);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
398 hb_font_funcs_set_glyph_contour_point_func(funcs, get_contour_point,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
399 metrics, NULL);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
400 hb_font_set_funcs(hb_fonts[info->face_index], funcs,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
401 font->faces[info->face_index], NULL);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
402 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
403
35262
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
404 // XXX: this is a rather crude hack
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
405 const double ft_size = 256.0;
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
406 ass_face_set_size(font->faces[info->face_index], ft_size);
34300
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
407 update_hb_size(hb_fonts[info->face_index], font->faces[info->face_index]);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
408
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
409 // update hash key for cached metrics
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
410 struct ass_shaper_metrics_data *metrics =
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
411 font->shaper_priv->metrics_data[info->face_index];
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
412 metrics->hash_key.font = info->font;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
413 metrics->hash_key.face_index = info->face_index;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
414 metrics->hash_key.size = info->font_size;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
415 metrics->hash_key.scale_x = double_to_d6(info->scale_x);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
416 metrics->hash_key.scale_y = double_to_d6(info->scale_y);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
417
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
418 return hb_fonts[info->face_index];
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
419 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
420
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
421 /**
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
422 * \brief Shape event text with HarfBuzz. Full OpenType shaping.
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
423 * \param glyphs glyph clusters
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
424 * \param len number of clusters
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
425 */
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
426 static void shape_harfbuzz(ASS_Shaper *shaper, GlyphInfo *glyphs, size_t len)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
427 {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
428 int i, j;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
429 int run = 0;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
430 struct {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
431 int offset;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
432 int end;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
433 hb_buffer_t *buf;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
434 hb_font_t *font;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
435 } runs[MAX_RUNS];
35262
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
436 const double ft_size = 256.0;
34300
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
437
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
438 for (i = 0; i < len && run < MAX_RUNS; i++, run++) {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
439 // get length and level of the current run
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
440 int k = i;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
441 int level = glyphs[i].shape_run_id;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
442 int direction = shaper->emblevels[k] % 2;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
443 while (i < (len - 1) && level == glyphs[i+1].shape_run_id)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
444 i++;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
445 runs[run].offset = k;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
446 runs[run].end = i;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
447 runs[run].buf = hb_buffer_create();
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
448 runs[run].font = get_hb_font(shaper, glyphs + k);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
449 set_run_features(shaper, glyphs + k);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
450 hb_buffer_pre_allocate(runs[run].buf, i - k + 1);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
451 hb_buffer_set_direction(runs[run].buf, direction ? HB_DIRECTION_RTL :
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
452 HB_DIRECTION_LTR);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
453 hb_buffer_set_language(runs[run].buf, shaper->language);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
454 hb_buffer_add_utf32(runs[run].buf, shaper->event_text + k, i - k + 1,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
455 0, i - k + 1);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
456 hb_shape(runs[run].font, runs[run].buf, shaper->features,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
457 shaper->n_features);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
458 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
459
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
460 // Initialize: skip all glyphs, this is undone later as needed
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
461 for (i = 0; i < len; i++)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
462 glyphs[i].skip = 1;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
463
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
464 // Update glyph indexes, positions and advances from the shaped runs
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
465 for (i = 0; i < run; i++) {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
466 int num_glyphs = hb_buffer_get_length(runs[i].buf);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
467 hb_glyph_info_t *glyph_info = hb_buffer_get_glyph_infos(runs[i].buf, NULL);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
468 hb_glyph_position_t *pos = hb_buffer_get_glyph_positions(runs[i].buf, NULL);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
469
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
470 for (j = 0; j < num_glyphs; j++) {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
471 int idx = glyph_info[j].cluster + runs[i].offset;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
472 GlyphInfo *info = glyphs + idx;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
473 GlyphInfo *root = info;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
474
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
475 // if we have more than one glyph per cluster, allocate a new one
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
476 // and attach to the root glyph
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
477 if (info->skip == 0) {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
478 while (info->next)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
479 info = info->next;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
480 info->next = malloc(sizeof(GlyphInfo));
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
481 memcpy(info->next, info, sizeof(GlyphInfo));
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
482 info = info->next;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
483 info->next = NULL;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
484 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
485
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
486 // set position and advance
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
487 info->skip = 0;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
488 info->glyph_index = glyph_info[j].codepoint;
35262
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
489 info->offset.x = pos[j].x_offset * info->scale_x * (info->font_size / ft_size);
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
490 info->offset.y = -pos[j].y_offset * info->scale_y * (info->font_size / ft_size);
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
491 info->advance.x = pos[j].x_advance * info->scale_x * (info->font_size / ft_size);
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
492 info->advance.y = -pos[j].y_advance * info->scale_y * (info->font_size / ft_size);
34300
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
493
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
494 // accumulate advance in the root glyph
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
495 root->cluster_advance.x += info->advance.x;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
496 root->cluster_advance.y += info->advance.y;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
497 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
498 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
499
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
500 // Free runs and associated data
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
501 for (i = 0; i < run; i++) {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
502 hb_buffer_destroy(runs[i].buf);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
503 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
504
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
505 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
506 #endif
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
507
34342
575ad51cc996 Allow compiling libass without fribidi again.
reimar
parents: 34300
diff changeset
508 #ifdef CONFIG_FRIBIDI
34300
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
509 /**
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
510 * \brief Shape event text with FriBidi. Does mirroring and simple
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
511 * Arabic shaping.
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
512 * \param len number of clusters
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
513 */
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
514 static void shape_fribidi(ASS_Shaper *shaper, GlyphInfo *glyphs, size_t len)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
515 {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
516 int i;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
517 FriBidiJoiningType *joins = calloc(sizeof(*joins), len);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
518
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
519 // shape on codepoint level
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
520 fribidi_get_joining_types(shaper->event_text, len, joins);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
521 fribidi_join_arabic(shaper->ctypes, len, shaper->emblevels, joins);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
522 fribidi_shape(FRIBIDI_FLAGS_DEFAULT | FRIBIDI_FLAGS_ARABIC,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
523 shaper->emblevels, len, joins, shaper->event_text);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
524
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
525 // update indexes
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
526 for (i = 0; i < len; i++) {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
527 GlyphInfo *info = glyphs + i;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
528 FT_Face face = info->font->faces[info->face_index];
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
529 info->symbol = shaper->event_text[i];
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
530 info->glyph_index = FT_Get_Char_Index(face, shaper->event_text[i]);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
531 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
532
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
533 free(joins);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
534 }
34342
575ad51cc996 Allow compiling libass without fribidi again.
reimar
parents: 34300
diff changeset
535 #endif
34300
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
536
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
537 /**
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
538 * \brief Toggle kerning for HarfBuzz shaping.
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
539 * NOTE: currently only works with OpenType fonts, the TrueType fallback *always*
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
540 * kerns. It's a bug in HarfBuzz.
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
541 */
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
542 void ass_shaper_set_kerning(ASS_Shaper *shaper, int kern)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
543 {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
544 #ifdef CONFIG_HARFBUZZ
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
545 shaper->features[KERN].value = !!kern;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
546 #endif
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
547 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
548
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
549 /**
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
550 * \brief Find shape runs according to the event's selected fonts
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
551 */
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
552 void ass_shaper_find_runs(ASS_Shaper *shaper, ASS_Renderer *render_priv,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
553 GlyphInfo *glyphs, size_t len)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
554 {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
555 int i;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
556 int shape_run = 0;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
557
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
558 for (i = 0; i < len; i++) {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
559 GlyphInfo *last = glyphs + i - 1;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
560 GlyphInfo *info = glyphs + i;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
561 // skip drawings
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
562 if (info->symbol == 0xfffc)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
563 continue;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
564 // set size and get glyph index
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
565 ass_font_get_index(render_priv->fontconfig_priv, info->font,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
566 info->symbol, &info->face_index, &info->glyph_index);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
567 // shape runs share the same font face and size
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
568 if (i > 0 && (last->font != info->font ||
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
569 last->font_size != info->font_size ||
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
570 last->face_index != info->face_index))
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
571 shape_run++;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
572 info->shape_run_id = shape_run;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
573 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
574
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
575 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
576
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
577 /**
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
578 * \brief Set base direction (paragraph direction) of the text.
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
579 * \param dir base direction
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
580 */
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
581 void ass_shaper_set_base_direction(ASS_Shaper *shaper, FriBidiParType dir)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
582 {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
583 shaper->base_direction = dir;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
584 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
585
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
586 /**
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
587 * \brief Set language hint. Some languages have specific character variants,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
588 * like Serbian Cyrillic.
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
589 * \param lang ISO 639-1 two-letter language code
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
590 */
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
591 void ass_shaper_set_language(ASS_Shaper *shaper, const char *code)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
592 {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
593 #ifdef CONFIG_HARFBUZZ
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
594 shaper->language = hb_language_from_string(code, -1);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
595 #endif
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
596 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
597
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
598 /**
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
599 * Set shaping level. Essentially switches between FriBidi and HarfBuzz.
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
600 */
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
601 void ass_shaper_set_level(ASS_Shaper *shaper, ASS_ShapingLevel level)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
602 {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
603 shaper->shaping_level = level;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
604 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
605
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
606 /**
35262
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
607 * \brief Remove all zero-width invisible characters from the text.
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
608 * \param text_info text
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
609 */
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
610 static void ass_shaper_skip_characters(TextInfo *text_info)
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
611 {
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
612 int i;
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
613 GlyphInfo *glyphs = text_info->glyphs;
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
614
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
615 for (i = 0; i < text_info->length; i++) {
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
616 // Skip direction override control characters
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
617 if ((glyphs[i].symbol <= 0x202e && glyphs[i].symbol >= 0x202a)
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
618 || (glyphs[i].symbol <= 0x200f && glyphs[i].symbol >= 0x200b)
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
619 || (glyphs[i].symbol <= 0x2063 && glyphs[i].symbol >= 0x2060)
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
620 || glyphs[i].symbol == 0xfeff
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
621 || glyphs[i].symbol == 0x00ad
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
622 || glyphs[i].symbol == 0x034f) {
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
623 glyphs[i].symbol = 0;
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
624 glyphs[i].skip++;
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
625 }
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
626 }
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
627 }
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
628
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
629 /**
34300
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
630 * \brief Shape an event's text. Calculates directional runs and shapes them.
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
631 * \param text_info event's text
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
632 */
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
633 void ass_shaper_shape(ASS_Shaper *shaper, TextInfo *text_info)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
634 {
34342
575ad51cc996 Allow compiling libass without fribidi again.
reimar
parents: 34300
diff changeset
635 #ifndef CONFIG_FRIBIDI
575ad51cc996 Allow compiling libass without fribidi again.
reimar
parents: 34300
diff changeset
636 check_allocations(shaper, text_info->length);
575ad51cc996 Allow compiling libass without fribidi again.
reimar
parents: 34300
diff changeset
637 #else
34300
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
638 int i, last_break;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
639 FriBidiParType dir;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
640 GlyphInfo *glyphs = text_info->glyphs;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
641
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
642 check_allocations(shaper, text_info->length);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
643
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
644 // Get bidi character types and embedding levels
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
645 last_break = 0;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
646 for (i = 0; i < text_info->length; i++) {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
647 shaper->event_text[i] = glyphs[i].symbol;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
648 // embedding levels should be calculated paragraph by paragraph
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
649 if (glyphs[i].symbol == '\n' || i == text_info->length - 1) {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
650 dir = shaper->base_direction;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
651 fribidi_get_bidi_types(shaper->event_text + last_break,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
652 i - last_break + 1, shaper->ctypes + last_break);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
653 fribidi_get_par_embedding_levels(shaper->ctypes + last_break,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
654 i - last_break + 1, &dir, shaper->emblevels + last_break);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
655 last_break = i + 1;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
656 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
657 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
658
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
659 // add embedding levels to shape runs for final runs
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
660 for (i = 0; i < text_info->length; i++) {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
661 glyphs[i].shape_run_id += shaper->emblevels[i];
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
662 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
663
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
664 #ifdef CONFIG_HARFBUZZ
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
665 switch (shaper->shaping_level) {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
666 case ASS_SHAPING_SIMPLE:
35274
4f4a90b80a9a libass: Fixed RTL languages
SubJunk
parents: 35262
diff changeset
667 shape_fribidi(shaper, glyphs, text_info->length);
35262
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
668 ass_shaper_skip_characters(text_info);
34300
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
669 break;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
670 case ASS_SHAPING_COMPLEX:
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
671 shape_harfbuzz(shaper, glyphs, text_info->length);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
672 break;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
673 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
674 #else
35274
4f4a90b80a9a libass: Fixed RTL languages
SubJunk
parents: 35262
diff changeset
675 shape_fribidi(shaper, glyphs, text_info->length);
35262
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
676 ass_shaper_skip_characters(text_info);
34300
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
677 #endif
34342
575ad51cc996 Allow compiling libass without fribidi again.
reimar
parents: 34300
diff changeset
678 #endif
34300
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
679 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
680
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
681 /**
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
682 * \brief Create a new shaper instance and preallocate data structures
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
683 * \param prealloc preallocation size
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
684 */
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
685 ASS_Shaper *ass_shaper_new(size_t prealloc)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
686 {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
687 ASS_Shaper *shaper = calloc(sizeof(*shaper), 1);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
688
34342
575ad51cc996 Allow compiling libass without fribidi again.
reimar
parents: 34300
diff changeset
689 #ifdef CONFIG_FRIBIDI
34300
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
690 shaper->base_direction = FRIBIDI_PAR_ON;
34342
575ad51cc996 Allow compiling libass without fribidi again.
reimar
parents: 34300
diff changeset
691 #endif
34300
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
692 check_allocations(shaper, prealloc);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
693
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
694 #ifdef CONFIG_HARFBUZZ
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
695 init_features(shaper);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
696 shaper->metrics_cache = ass_glyph_metrics_cache_create();
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
697 #endif
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
698
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
699 return shaper;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
700 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
701
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
702
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
703 /**
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
704 * \brief clean up additional data temporarily needed for shaping and
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
705 * (e.g. additional glyphs allocated)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
706 */
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
707 void ass_shaper_cleanup(ASS_Shaper *shaper, TextInfo *text_info)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
708 {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
709 int i;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
710
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
711 for (i = 0; i < text_info->length; i++) {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
712 GlyphInfo *info = text_info->glyphs + i;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
713 info = info->next;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
714 while (info) {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
715 GlyphInfo *next = info->next;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
716 free(info);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
717 info = next;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
718 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
719 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
720 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
721
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
722 /**
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
723 * \brief Calculate reorder map to render glyphs in visual order
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
724 */
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
725 FriBidiStrIndex *ass_shaper_reorder(ASS_Shaper *shaper, TextInfo *text_info)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
726 {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
727 int i;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
728
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
729 // Initialize reorder map
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
730 for (i = 0; i < text_info->length; i++)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
731 shaper->cmap[i] = i;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
732
34342
575ad51cc996 Allow compiling libass without fribidi again.
reimar
parents: 34300
diff changeset
733 #ifdef CONFIG_FRIBIDI
34300
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
734 // Create reorder map line-by-line
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
735 for (i = 0; i < text_info->n_lines; i++) {
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
736 LineInfo *line = text_info->lines + i;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
737 int level;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
738 FriBidiParType dir = FRIBIDI_PAR_ON;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
739
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
740 level = fribidi_reorder_line(0,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
741 shaper->ctypes + line->offset, line->len, 0, dir,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
742 shaper->emblevels + line->offset, NULL,
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
743 shaper->cmap + line->offset);
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
744 }
34342
575ad51cc996 Allow compiling libass without fribidi again.
reimar
parents: 34300
diff changeset
745 #endif
34300
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
746
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
747 return shaper->cmap;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
748 }
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
749
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
750 /**
35262
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
751 * \brief Resolve a Windows font charset number to a suitable
34300
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
752 * base direction. 177 and 178 are Hebrew and Arabic respectively, and
35262
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
753 * they map to RTL. Everything else maps to LTR for compatibility
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
754 * reasons. The special value -1, which is not a legal Windows font charset
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
755 * number, can be used for autodetection.
34300
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
756 * \param enc Windows font encoding
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
757 */
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
758 FriBidiParType resolve_base_direction(int enc)
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
759 {
34342
575ad51cc996 Allow compiling libass without fribidi again.
reimar
parents: 34300
diff changeset
760 #ifdef CONFIG_FRIBIDI
34300
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
761 switch (enc) {
35262
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34342
diff changeset
762 case -1:
34300
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
763 return FRIBIDI_PAR_ON;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
764 case 177:
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
765 case 178:
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
766 return FRIBIDI_PAR_RTL;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
767 default:
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
768 return FRIBIDI_PAR_LTR;
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
769 }
34342
575ad51cc996 Allow compiling libass without fribidi again.
reimar
parents: 34300
diff changeset
770 #else
575ad51cc996 Allow compiling libass without fribidi again.
reimar
parents: 34300
diff changeset
771 return 0;
575ad51cc996 Allow compiling libass without fribidi again.
reimar
parents: 34300
diff changeset
772 #endif
34300
77976b68285b Commit added forgotten in previous commit.
reimar
parents:
diff changeset
773 }