*/
#include "config.h"
+#include "ass_compat.h"
#include "ass_shaper.h"
#include "ass_render.h"
#include "ass_font.h"
#include "ass_parse.h"
#include "ass_cache.h"
-
-#define MAX_RUNS 50
+#include <limits.h>
+#include <stdbool.h>
#ifdef CONFIG_HARFBUZZ
#include <hb-ft.h>
enum {
VERT = 0,
VKNA,
- KERN
+ KERN,
+ LIGA,
+ CLIG
};
-#define NUM_FEATURES 3
+#define NUM_FEATURES 5
#endif
struct ass_shaper {
*/
void ass_shaper_info(ASS_Library *lib)
{
- ass_msg(lib, MSGL_V, "Shaper: FriBidi "
+ ass_msg(lib, MSGL_INFO, "Shaper: FriBidi "
FRIBIDI_VERSION " (SIMPLE)"
#ifdef CONFIG_HARFBUZZ
" HarfBuzz-ng %s (COMPLEX)", hb_version_string()
* \brief grow arrays, if needed
* \param new_size requested size
*/
-static void check_allocations(ASS_Shaper *shaper, size_t new_size)
+static bool check_allocations(ASS_Shaper *shaper, size_t new_size)
{
if (new_size > shaper->n_glyphs) {
- shaper->event_text = realloc(shaper->event_text, sizeof(FriBidiChar) * new_size);
- shaper->ctypes = realloc(shaper->ctypes, sizeof(FriBidiCharType) * new_size);
- shaper->emblevels = realloc(shaper->emblevels, sizeof(FriBidiLevel) * new_size);
- shaper->cmap = realloc(shaper->cmap, sizeof(FriBidiStrIndex) * new_size);
+ if (!ASS_REALLOC_ARRAY(shaper->event_text, new_size) ||
+ !ASS_REALLOC_ARRAY(shaper->ctypes, new_size) ||
+ !ASS_REALLOC_ARRAY(shaper->emblevels, new_size) ||
+ !ASS_REALLOC_ARRAY(shaper->cmap, new_size))
+ return false;
}
+ return true;
}
/**
* \brief set up the HarfBuzz OpenType feature list with some
* standard features.
*/
-static void init_features(ASS_Shaper *shaper)
+static bool init_features(ASS_Shaper *shaper)
{
shaper->features = calloc(sizeof(hb_feature_t), NUM_FEATURES);
+ if (!shaper->features)
+ return false;
shaper->n_features = NUM_FEATURES;
shaper->features[VERT].tag = HB_TAG('v', 'e', 'r', 't');
- shaper->features[VERT].end = INT_MAX;
+ shaper->features[VERT].end = UINT_MAX;
shaper->features[VKNA].tag = HB_TAG('v', 'k', 'n', 'a');
- shaper->features[VKNA].end = INT_MAX;
+ shaper->features[VKNA].end = UINT_MAX;
shaper->features[KERN].tag = HB_TAG('k', 'e', 'r', 'n');
- shaper->features[KERN].end = INT_MAX;
+ shaper->features[KERN].end = UINT_MAX;
+ shaper->features[LIGA].tag = HB_TAG('l', 'i', 'g', 'a');
+ shaper->features[LIGA].end = UINT_MAX;
+ shaper->features[CLIG].tag = HB_TAG('c', 'l', 'i', 'g');
+ shaper->features[CLIG].end = UINT_MAX;
+
+ return true;
}
/**
*/
static void set_run_features(ASS_Shaper *shaper, GlyphInfo *info)
{
- // enable vertical substitutions for @font runs
- if (info->font->desc.vertical)
- shaper->features[VERT].value = shaper->features[VKNA].value = 1;
- else
- shaper->features[VERT].value = shaper->features[VKNA].value = 0;
+ // enable vertical substitutions for @font runs
+ if (info->font->desc.vertical)
+ shaper->features[VERT].value = shaper->features[VKNA].value = 1;
+ else
+ shaper->features[VERT].value = shaper->features[VKNA].value = 0;
+
+ // disable ligatures if horizontal spacing is non-standard
+ if (info->hspacing)
+ shaper->features[LIGA].value = shaper->features[CLIG].value = 0;
+ else
+ shaper->features[LIGA].value = shaper->features[CLIG].value = 1;
}
/**
GlyphMetricsHashValue *
get_cached_metrics(struct ass_shaper_metrics_data *metrics, FT_Face face,
- hb_codepoint_t glyph)
+ hb_codepoint_t unicode, hb_codepoint_t glyph)
{
GlyphMetricsHashValue *val;
-
metrics->hash_key.glyph_index = glyph;
- val = ass_cache_get(metrics->metrics_cache, &metrics->hash_key);
-
- if (!val) {
+ if (!ass_cache_get(metrics->metrics_cache, &metrics->hash_key, &val)) {
int load_flags = FT_LOAD_DEFAULT | FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH
| FT_LOAD_IGNORE_TRANSFORM;
- GlyphMetricsHashValue new_val;
- if (FT_Load_Glyph(face, glyph, load_flags))
+ if (FT_Load_Glyph(face, glyph, load_flags)) {
+ ass_cache_cancel(val);
return NULL;
+ }
- memcpy(&new_val.metrics, &face->glyph->metrics, sizeof(FT_Glyph_Metrics));
+ memcpy(&val->metrics, &face->glyph->metrics, sizeof(FT_Glyph_Metrics));
// if @font rendering is enabled and the glyph should be rotated,
// make cached_h_advance pick up the right advance later
- if (metrics->vertical && glyph >= VERTICAL_LOWER_BOUND)
- new_val.metrics.horiAdvance = new_val.metrics.vertAdvance;
+ if (metrics->vertical && unicode >= VERTICAL_LOWER_BOUND)
+ val->metrics.horiAdvance = val->metrics.vertAdvance;
- val = ass_cache_put(metrics->metrics_cache, &metrics->hash_key, &new_val);
+ ass_cache_inc_ref(metrics->hash_key.font);
+ ass_cache_commit(val);
}
return val;
hb_codepoint_t variation, hb_codepoint_t *glyph, void *user_data)
{
FT_Face face = font_data;
+ struct ass_shaper_metrics_data *metrics_priv = user_data;
if (variation)
- *glyph = FT_Face_GetCharVariantIndex(face, unicode, variation);
+ *glyph = FT_Face_GetCharVariantIndex(face, ass_font_index_magic(face, unicode), variation);
else
- *glyph = FT_Get_Char_Index(face, unicode);
+ *glyph = FT_Get_Char_Index(face, ass_font_index_magic(face, unicode));
+
+ // rotate glyph advances for @fonts while we still know the Unicode codepoints
+ if (*glyph != 0)
+ get_cached_metrics(metrics_priv, face, unicode, *glyph);
return *glyph != 0;
}
{
FT_Face face = font_data;
struct ass_shaper_metrics_data *metrics_priv = user_data;
- GlyphMetricsHashValue *metrics = get_cached_metrics(metrics_priv, face, glyph);
+ GlyphMetricsHashValue *metrics = get_cached_metrics(metrics_priv, face, 0, glyph);
if (!metrics)
return 0;
{
FT_Face face = font_data;
struct ass_shaper_metrics_data *metrics_priv = user_data;
- GlyphMetricsHashValue *metrics = get_cached_metrics(metrics_priv, face, glyph);
+ GlyphMetricsHashValue *metrics = get_cached_metrics(metrics_priv, face, 0, glyph);
if (!metrics)
return 0;
{
FT_Face face = font_data;
struct ass_shaper_metrics_data *metrics_priv = user_data;
- GlyphMetricsHashValue *metrics = get_cached_metrics(metrics_priv, face, glyph);
+ GlyphMetricsHashValue *metrics = get_cached_metrics(metrics_priv, face, 0, glyph);
if (!metrics)
return 0;
{
FT_Face face = font_data;
struct ass_shaper_metrics_data *metrics_priv = user_data;
- GlyphMetricsHashValue *metrics = get_cached_metrics(metrics_priv, face, glyph);
+ GlyphMetricsHashValue *metrics = get_cached_metrics(metrics_priv, face, 0, glyph);
if (!metrics)
return 0;
extents->x_bearing = metrics->metrics.horiBearingX;
extents->y_bearing = metrics->metrics.horiBearingY;
extents->width = metrics->metrics.width;
- extents->height = metrics->metrics.height;
+ extents->height = -metrics->metrics.height;
return 1;
}
return lang;
}
+/**
+ * \brief Feed a run of shaped characters into the GlyphInfo array.
+ *
+ * \param glyphs GlyphInfo array
+ * \param buf buffer of shaped run
+ * \param offset offset into GlyphInfo array
+ */
+static void
+shape_harfbuzz_process_run(GlyphInfo *glyphs, hb_buffer_t *buf, int offset)
+{
+ int j;
+ int num_glyphs = hb_buffer_get_length(buf);
+ hb_glyph_info_t *glyph_info = hb_buffer_get_glyph_infos(buf, NULL);
+ hb_glyph_position_t *pos = hb_buffer_get_glyph_positions(buf, NULL);
+
+ for (j = 0; j < num_glyphs; j++) {
+ unsigned idx = glyph_info[j].cluster + offset;
+ GlyphInfo *info = glyphs + idx;
+ GlyphInfo *root = info;
+
+ // if we have more than one glyph per cluster, allocate a new one
+ // and attach to the root glyph
+ if (info->skip == 0) {
+ while (info->next)
+ info = info->next;
+ info->next = malloc(sizeof(GlyphInfo));
+ if (info->next) {
+ memcpy(info->next, info, sizeof(GlyphInfo));
+ info = info->next;
+ info->next = NULL;
+ }
+ }
+
+ // set position and advance
+ info->skip = 0;
+ info->glyph_index = glyph_info[j].codepoint;
+ info->offset.x = pos[j].x_offset * info->scale_x;
+ info->offset.y = -pos[j].y_offset * info->scale_y;
+ info->advance.x = pos[j].x_advance * info->scale_x;
+ info->advance.y = -pos[j].y_advance * info->scale_y;
+
+ // accumulate advance in the root glyph
+ root->cluster_advance.x += info->advance.x;
+ root->cluster_advance.y += info->advance.y;
+ }
+}
+
/**
* \brief Shape event text with HarfBuzz. Full OpenType shaping.
* \param glyphs glyph clusters
*/
static void shape_harfbuzz(ASS_Shaper *shaper, GlyphInfo *glyphs, size_t len)
{
- int i, j;
- int run = 0;
- struct {
- int offset;
- int end;
- hb_buffer_t *buf;
- hb_font_t *font;
- } runs[MAX_RUNS];
-
- for (i = 0; i < len && run < MAX_RUNS; i++, run++) {
- // get length and level of the current run
- int k = i;
- int level = glyphs[i].shape_run_id;
- int direction = shaper->emblevels[k] % 2;
- hb_script_t script = glyphs[i].script;
- while (i < (len - 1) && level == glyphs[i+1].shape_run_id)
- i++;
- runs[run].offset = k;
- runs[run].end = i;
- runs[run].buf = hb_buffer_create();
- runs[run].font = get_hb_font(shaper, glyphs + k);
- set_run_features(shaper, glyphs + k);
- hb_buffer_pre_allocate(runs[run].buf, i - k + 1);
- hb_buffer_set_direction(runs[run].buf, direction ? HB_DIRECTION_RTL :
- HB_DIRECTION_LTR);
- hb_buffer_set_language(runs[run].buf,
- hb_shaper_get_run_language(shaper, script));
- hb_buffer_set_script(runs[run].buf, script);
- hb_buffer_add_utf32(runs[run].buf, shaper->event_text + k, i - k + 1,
- 0, i - k + 1);
- hb_shape(runs[run].font, runs[run].buf, shaper->features,
- shaper->n_features);
- }
+ int i;
+ hb_buffer_t *buf = hb_buffer_create();
+ hb_segment_properties_t props = HB_SEGMENT_PROPERTIES_DEFAULT;
// Initialize: skip all glyphs, this is undone later as needed
for (i = 0; i < len; i++)
glyphs[i].skip = 1;
- // Update glyph indexes, positions and advances from the shaped runs
- for (i = 0; i < run; i++) {
- int num_glyphs = hb_buffer_get_length(runs[i].buf);
- hb_glyph_info_t *glyph_info = hb_buffer_get_glyph_infos(runs[i].buf, NULL);
- hb_glyph_position_t *pos = hb_buffer_get_glyph_positions(runs[i].buf, NULL);
-
- for (j = 0; j < num_glyphs; j++) {
- int idx = glyph_info[j].cluster + runs[i].offset;
- GlyphInfo *info = glyphs + idx;
- GlyphInfo *root = info;
-
- // if we have more than one glyph per cluster, allocate a new one
- // and attach to the root glyph
- if (info->skip == 0) {
- while (info->next)
- info = info->next;
- info->next = malloc(sizeof(GlyphInfo));
- memcpy(info->next, info, sizeof(GlyphInfo));
- info = info->next;
- info->next = NULL;
- }
+ for (i = 0; i < len; i++) {
+ int offset = i;
+ hb_font_t *font = get_hb_font(shaper, glyphs + offset);
+ int level = glyphs[offset].shape_run_id;
+ int direction = shaper->emblevels[offset] % 2;
- // set position and advance
- info->skip = 0;
- info->glyph_index = glyph_info[j].codepoint;
- info->offset.x = pos[j].x_offset * info->scale_x;
- info->offset.y = -pos[j].y_offset * info->scale_y;
- info->advance.x = pos[j].x_advance * info->scale_x;
- info->advance.y = -pos[j].y_advance * info->scale_y;
-
- // accumulate advance in the root glyph
- root->cluster_advance.x += info->advance.x;
- root->cluster_advance.y += info->advance.y;
- }
- }
+ // advance in text until end of run
+ while (i < (len - 1) && level == glyphs[i+1].shape_run_id)
+ i++;
- // Free runs and associated data
- for (i = 0; i < run; i++) {
- hb_buffer_destroy(runs[i].buf);
+ hb_buffer_pre_allocate(buf, i - offset + 1);
+ hb_buffer_add_utf32(buf, shaper->event_text + offset, i - offset + 1,
+ 0, i - offset + 1);
+
+ props.direction = direction ? HB_DIRECTION_RTL : HB_DIRECTION_LTR;
+ props.script = glyphs[offset].script;
+ props.language = hb_shaper_get_run_language(shaper, props.script);
+ hb_buffer_set_segment_properties(buf, &props);
+
+ set_run_features(shaper, glyphs + offset);
+ hb_shape(font, buf, shaper->features, shaper->n_features);
+
+ shape_harfbuzz_process_run(glyphs, buf, offset);
+ hb_buffer_reset(buf);
}
+ hb_buffer_destroy(buf);
}
/**
GlyphInfo *info = glyphs + i;
FT_Face face = info->font->faces[info->face_index];
info->symbol = shaper->event_text[i];
- info->glyph_index = FT_Get_Char_Index(face, shaper->event_text[i]);
+ info->glyph_index = FT_Get_Char_Index(face, ass_font_index_magic(face, shaper->event_text[i]));
}
free(joins);
/**
* \brief Toggle kerning for HarfBuzz shaping.
- * NOTE: currently only works with OpenType fonts, the TrueType fallback *always*
- * kerns. It's a bug in HarfBuzz.
+ * \param shaper shaper instance
+ * \param kern toggle kerning
*/
void ass_shaper_set_kerning(ASS_Shaper *shaper, int kern)
{
if (info->symbol == 0xfffc)
continue;
// set size and get glyph index
- ass_font_get_index(render_priv->fontconfig_priv, info->font,
+ ass_font_get_index(render_priv->fontselect, info->font,
info->symbol, &info->face_index, &info->glyph_index);
- // shape runs share the same font face and size
+ // shape runs break on: xbord, ybord, xshad, yshad,
+ // all four colors, all four alphas, be, blur, fn, fs,
+ // fscx, fscy, fsp, bold, italic, underline, strikeout,
+ // frx, fry, frz, fax, fay, karaoke start, karaoke type,
+ // and on every line break
if (i > 0 && (last->font != info->font ||
+ last->face_index != info->face_index ||
+ last->script != info->script ||
last->font_size != info->font_size ||
+ last->c[0] != info->c[0] ||
+ last->c[1] != info->c[1] ||
+ last->c[2] != info->c[2] ||
+ last->c[3] != info->c[3] ||
+ last->be != info->be ||
+ last->blur != info->blur ||
+ last->shadow_x != info->shadow_x ||
+ last->shadow_y != info->shadow_y ||
+ last->frx != info->frx ||
+ last->fry != info->fry ||
+ last->frz != info->frz ||
+ last->fax != info->fax ||
+ last->fay != info->fay ||
last->scale_x != info->scale_x ||
last->scale_y != info->scale_y ||
- last->face_index != info->face_index ||
- last->script != info->script))
+ last->border_style != info->border_style ||
+ last->border_x != info->border_x ||
+ last->border_y != info->border_y ||
+ last->hspacing != info->hspacing ||
+ last->italic != info->italic ||
+ last->bold != info->bold ||
+ last->flags != info->flags))
shape_run++;
info->shape_run_id = shape_run;
}
/**
* \brief Shape an event's text. Calculates directional runs and shapes them.
* \param text_info event's text
+ * \return success, when 0
*/
-void ass_shaper_shape(ASS_Shaper *shaper, TextInfo *text_info)
+int ass_shaper_shape(ASS_Shaper *shaper, TextInfo *text_info)
{
- int i, last_break;
+ int i, ret, last_break;
FriBidiParType dir;
GlyphInfo *glyphs = text_info->glyphs;
- check_allocations(shaper, text_info->length);
+ if (!check_allocations(shaper, text_info->length))
+ return -1;
// Get bidi character types and embedding levels
last_break = 0;
dir = shaper->base_direction;
fribidi_get_bidi_types(shaper->event_text + last_break,
i - last_break + 1, shaper->ctypes + last_break);
- fribidi_get_par_embedding_levels(shaper->ctypes + last_break,
+ ret = fribidi_get_par_embedding_levels(shaper->ctypes + last_break,
i - last_break + 1, &dir, shaper->emblevels + last_break);
+ if (ret == 0)
+ return -1;
last_break = i + 1;
}
}
shape_fribidi(shaper, glyphs, text_info->length);
ass_shaper_skip_characters(text_info);
#endif
+
+ return 0;
}
/**
ASS_Shaper *ass_shaper_new(size_t prealloc)
{
ASS_Shaper *shaper = calloc(sizeof(*shaper), 1);
+ if (!shaper)
+ return NULL;
shaper->base_direction = FRIBIDI_PAR_ON;
- check_allocations(shaper, prealloc);
+ if (!check_allocations(shaper, prealloc))
+ goto error;
#ifdef CONFIG_HARFBUZZ
- init_features(shaper);
+ if (!init_features(shaper))
+ goto error;
shaper->metrics_cache = ass_glyph_metrics_cache_create();
+ if (!shaper->metrics_cache)
+ goto error;
#endif
return shaper;
+
+error:
+ ass_shaper_free(shaper);
+ return NULL;
}
/**
* \brief Calculate reorder map to render glyphs in visual order
+ * \param shaper shaper instance
+ * \param text_info text to be reordered
+ * \return map of reordered characters, or NULL
*/
FriBidiStrIndex *ass_shaper_reorder(ASS_Shaper *shaper, TextInfo *text_info)
{
- int i;
+ int i, ret;
// Initialize reorder map
for (i = 0; i < text_info->length; i++)
LineInfo *line = text_info->lines + i;
FriBidiParType dir = FRIBIDI_PAR_ON;
- fribidi_reorder_line(0,
+ ret = fribidi_reorder_line(0,
shaper->ctypes + line->offset, line->len, 0, dir,
shaper->emblevels + line->offset, NULL,
shaper->cmap + line->offset);
+ if (ret == 0)
+ return NULL;
}
return shaper->cmap;
}
/**
- * \brief Resolve a Windows font charset number to a suitable
- * base direction. 177 and 178 are Hebrew and Arabic respectively, and
- * they map to RTL. Everything else maps to LTR for compatibility
- * reasons. The special value -1, which is not a legal Windows font charset
- * number, can be used for autodetection.
+ * \brief Resolve a Windows font charset number to a suitable base
+ * direction. Generally, use LTR for compatibility with VSFilter. The
+ * special value -1, which is not a legal Windows font charset number,
+ * can be used for autodetection.
* \param enc Windows font encoding
*/
FriBidiParType resolve_base_direction(int enc)
switch (enc) {
case -1:
return FRIBIDI_PAR_ON;
- case 177:
- case 178:
- return FRIBIDI_PAR_RTL;
default:
return FRIBIDI_PAR_LTR;
}