]> granicus.if.org Git - libass/commitdiff
Support language hint in the Script Info section
authorGrigori Goronzy <greg@blackbox>
Mon, 1 Aug 2011 13:51:27 +0000 (15:51 +0200)
committerGrigori Goronzy <greg@blackbox>
Mon, 1 Aug 2011 13:51:27 +0000 (15:51 +0200)
Parse a hint in the form "Language: xx" with xx being an ISO 639-1
language code, and pass it to HarfBuzz. This can be useful to render
scripts with language-specific character variants.

libass/ass.c
libass/ass_render.c
libass/ass_shaper.c
libass/ass_shaper.h
libass/ass_types.h

index e262cc30ea30942eba0f2c0b71756e4bc4d47ccb..712c16cb2724f19aecd3ab49e30c602bad3d79c2 100644 (file)
@@ -28,6 +28,7 @@
 #include <sys/stat.h>
 #include <unistd.h>
 #include <inttypes.h>
+#include <ctype.h>
 
 #ifdef CONFIG_ICONV
 #include <iconv.h>
@@ -69,6 +70,7 @@ void ass_free_track(ASS_Track *track)
     }
     free(track->style_format);
     free(track->event_format);
+    free(track->Language);
     if (track->styles) {
         for (i = 0; i < track->n_styles; ++i)
             ass_free_style(track, i);
@@ -595,6 +597,12 @@ static int process_info_line(ASS_Track *track, char *str)
         track->ScaledBorderAndShadow = parse_bool(str + 22);
     } else if (!strncmp(str, "Kerning:", 8)) {
         track->Kerning = parse_bool(str + 8);
+    } else if (!strncmp(str, "Language:", 9)) {
+        char *p = str + 9;
+        while (*p && isspace(*p)) p++;
+        track->Language = malloc(3);
+        strncpy(track->Language, p, 2);
+        track->Language[2] = 0;
     }
     return 0;
 }
index 8d82a7484e0355c44741c7e7225c8ae506840e68..8d55667018db8c6abc8293b75116e550d3de0246 100644 (file)
@@ -2269,6 +2269,8 @@ ass_start_frame(ASS_Renderer *render_priv, ASS_Track *track,
         render_priv->border_scale = 1.;
 
     ass_shaper_set_kerning(render_priv->shaper, track->Kerning);
+    if (track->Language)
+        ass_shaper_set_language(render_priv->shaper, track->Language);
 
     // PAR correction
     render_priv->font_scale_x = render_priv->settings.aspect /
index 15dc549dd2e3d44210b4dd8ebf54c80ba3e22a24..16026f598e64dedd54e8153f8b76a061c487b3e9 100644 (file)
@@ -45,6 +45,7 @@ struct ass_shaper {
     // OpenType features
     int n_features;
     hb_feature_t *features;
+    hb_language_t language;
     // Glyph metrics cache, to speed up shaping
     Cache *metrics_cache;
 };
@@ -428,6 +429,7 @@ static void shape_harfbuzz(ASS_Shaper *shaper, GlyphInfo *glyphs, size_t len)
         set_run_features(shaper, glyphs + k);
         hb_buffer_set_direction(runs[run].buf, direction ? HB_DIRECTION_RTL :
                 HB_DIRECTION_LTR);
+        hb_buffer_set_language(runs[run].buf, shaper->language);
         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,
@@ -558,6 +560,17 @@ void ass_shaper_set_base_direction(ASS_Shaper *shaper, FriBidiParType dir)
     shaper->base_direction = dir;
 }
 
+/**
+ * \brief Set language hint. Some languages have specific character variants,
+ * like Serbian Cyrillic.
+ * \param lang ISO 639-1 two-letter language code
+ */
+void ass_shaper_set_language(ASS_Shaper *shaper, const char *code)
+{
+    printf("setting language to '%s'\n", code);
+    shaper->language = hb_language_from_string(code);
+}
+
 /**
  * \brief Shape an event's text. Calculates directional runs and shapes them.
  * \param text_info event's text
index 894822d7511f2e558032b2b53c3950778c867b26..7e8bcc6696a14a71eedd469a98ec68ab7d555d7f 100644 (file)
@@ -31,6 +31,7 @@ void ass_shaper_set_kerning(ASS_Shaper *shaper, int kern);
 void ass_shaper_find_runs(ASS_Shaper *shaper, ASS_Renderer *render_priv,
                           GlyphInfo *glyphs, size_t len);
 void ass_shaper_set_base_direction(ASS_Shaper *shaper, FriBidiParType dir);
+void ass_shaper_set_language(ASS_Shaper *shaper, const char *code);
 void ass_shaper_shape(ASS_Shaper *shaper, TextInfo *text_info);
 void ass_shaper_cleanup(ASS_Shaper *shaper, TextInfo *text_info);
 FriBidiStrIndex *ass_shaper_reorder(ASS_Shaper *shaper, TextInfo *text_info);
index 6a6f1ae8626f45fc42900214af7093266bfb6736..20fd825ef1e9605d0855397bcc04a8b536f074f1 100644 (file)
@@ -112,6 +112,7 @@ typedef struct ass_track {
     int WrapStyle;
     int ScaledBorderAndShadow;
     int Kerning;
+    char *Language;
 
     int default_style;      // index of default style
     char *name;             // file name in case of external subs, 0 for streams