Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 1 | /* |
Behdad Esfahbod | 2409d5f | 2011-04-21 17:14:28 -0400 | [diff] [blame] | 2 | * Copyright © 2010 Behdad Esfahbod |
| 3 | * Copyright © 2011 Google, Inc. |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 4 | * |
| 5 | * This is part of HarfBuzz, a text shaping library. |
| 6 | * |
| 7 | * Permission is hereby granted, without written agreement and without |
| 8 | * license or royalty fees, to use, copy, modify, and distribute this |
| 9 | * software and its documentation for any purpose, provided that the |
| 10 | * above copyright notice and the following two paragraphs appear in |
| 11 | * all copies of this software. |
| 12 | * |
| 13 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
| 14 | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
| 15 | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
| 16 | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
| 17 | * DAMAGE. |
| 18 | * |
| 19 | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
| 20 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
| 22 | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
| 23 | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 24 | * |
| 25 | * Google Author(s): Behdad Esfahbod |
| 26 | */ |
| 27 | |
| 28 | #ifdef HAVE_CONFIG_H |
| 29 | #include "config.h" |
| 30 | #endif |
| 31 | |
| 32 | #include <unistd.h> |
| 33 | #include <getopt.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <stdio.h> |
| 36 | #include <string.h> |
| 37 | #include <math.h> |
Behdad Esfahbod | cab6f65 | 2011-04-04 15:36:51 -0400 | [diff] [blame] | 38 | #include <locale.h> |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 39 | |
Behdad Esfahbod | d4bee9f | 2011-04-27 09:24:37 -0400 | [diff] [blame] | 40 | #include <glib.h> |
Behdad Esfahbod | 8df90c8 | 2011-08-10 15:26:41 +0200 | [diff] [blame] | 41 | #include <glib/gprintf.h> |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 42 | |
| 43 | #include <cairo-ft.h> |
| 44 | #include <hb-ft.h> |
| 45 | |
Behdad Esfahbod | c57d454 | 2011-04-20 18:50:27 -0400 | [diff] [blame] | 46 | |
| 47 | |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 48 | /* Controlled by cmd-line options */ |
Behdad Esfahbod | 8df90c8 | 2011-08-10 15:26:41 +0200 | [diff] [blame] | 49 | struct margin_t { |
| 50 | double t, r, b, l; |
| 51 | }; |
| 52 | static margin_t opt_margin = {18, 18, 18, 18}; |
Behdad Esfahbod | 25c4830 | 2011-08-10 16:28:38 +0200 | [diff] [blame^] | 53 | static char **opt_shapers; |
Behdad Esfahbod | 8df90c8 | 2011-08-10 15:26:41 +0200 | [diff] [blame] | 54 | static double line_space = 0; |
Behdad Esfahbod | 0c6a976 | 2011-05-31 12:59:17 -0400 | [diff] [blame] | 55 | static int face_index = 0; |
Behdad Esfahbod | 8df90c8 | 2011-08-10 15:26:41 +0200 | [diff] [blame] | 56 | static double font_size = 36; |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 57 | static const char *fore = "#000000"; |
Behdad Esfahbod | a4b781e | 2011-02-23 12:47:56 -0500 | [diff] [blame] | 58 | static const char *back = "#ffffff"; |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 59 | static const char *text = NULL; |
| 60 | static const char *font_file = NULL; |
| 61 | static const char *out_file = "/dev/stdout"; |
Behdad Esfahbod | 39a840a | 2011-04-27 14:48:19 -0400 | [diff] [blame] | 62 | static const char *direction = NULL; |
Behdad Esfahbod | 5d91c3d | 2011-03-16 17:36:32 -0300 | [diff] [blame] | 63 | static const char *script = NULL; |
Behdad Esfahbod | 39a840a | 2011-04-27 14:48:19 -0400 | [diff] [blame] | 64 | static const char *language = NULL; |
Behdad Esfahbod | e609aeb | 2011-05-18 10:17:02 -0400 | [diff] [blame] | 65 | static hb_bool_t annotate = FALSE; |
Behdad Esfahbod | 1b4a2cc | 2011-04-04 14:45:28 -0400 | [diff] [blame] | 66 | static hb_bool_t debug = FALSE; |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 67 | |
Behdad Esfahbod | 8df90c8 | 2011-08-10 15:26:41 +0200 | [diff] [blame] | 68 | static hb_feature_t *features = NULL; |
| 69 | static unsigned int num_features; |
| 70 | |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 71 | /* Ugh, global vars. Ugly, but does the job */ |
| 72 | static int width = 0; |
| 73 | static int height = 0; |
| 74 | static cairo_surface_t *surface = NULL; |
| 75 | static cairo_pattern_t *fore_pattern = NULL; |
| 76 | static cairo_pattern_t *back_pattern = NULL; |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 77 | static cairo_font_face_t *cairo_face; |
| 78 | |
| 79 | |
Behdad Esfahbod | 25c4830 | 2011-08-10 16:28:38 +0200 | [diff] [blame^] | 80 | static gchar * |
| 81 | shapers_to_string (void) |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 82 | { |
Behdad Esfahbod | 25c4830 | 2011-08-10 16:28:38 +0200 | [diff] [blame^] | 83 | GString *shapers = g_string_new (NULL); |
| 84 | const char **shaper_list = hb_shape_list_shapers (); |
| 85 | |
| 86 | for (; *shaper_list; shaper_list++) { |
| 87 | g_string_append (shapers, *shaper_list); |
| 88 | g_string_append_c (shapers, ','); |
| 89 | } |
| 90 | g_string_truncate (shapers, MAX (0, (gint)shapers->len - 1)); |
| 91 | |
| 92 | return g_string_free (shapers, FALSE); |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 93 | } |
| 94 | |
Behdad Esfahbod | 25c4830 | 2011-08-10 16:28:38 +0200 | [diff] [blame^] | 95 | |
Behdad Esfahbod | 8df90c8 | 2011-08-10 15:26:41 +0200 | [diff] [blame] | 96 | static G_GNUC_NORETURN gboolean |
| 97 | show_version (const char *name G_GNUC_UNUSED, |
| 98 | const char *arg G_GNUC_UNUSED, |
| 99 | gpointer data G_GNUC_UNUSED, |
| 100 | GError **error G_GNUC_UNUSED) |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 101 | { |
Behdad Esfahbod | 25c4830 | 2011-08-10 16:28:38 +0200 | [diff] [blame^] | 102 | g_printf ("%s (%s) %s\n", g_get_prgname (), PACKAGE_NAME, PACKAGE_VERSION); |
Behdad Esfahbod | 8df90c8 | 2011-08-10 15:26:41 +0200 | [diff] [blame] | 103 | |
Behdad Esfahbod | 25c4830 | 2011-08-10 16:28:38 +0200 | [diff] [blame^] | 104 | char *shapers = shapers_to_string (); |
| 105 | g_printf ("Available shapers: %s\n", shapers); |
| 106 | g_free (shapers); |
Behdad Esfahbod | 8df90c8 | 2011-08-10 15:26:41 +0200 | [diff] [blame] | 107 | if (strcmp (HB_VERSION_STRING, hb_version_string ())) |
Behdad Esfahbod | 25c4830 | 2011-08-10 16:28:38 +0200 | [diff] [blame^] | 108 | g_printf ("Linked HarfBuzz library has a different version: %s\n", hb_version_string ()); |
Behdad Esfahbod | 8df90c8 | 2011-08-10 15:26:41 +0200 | [diff] [blame] | 109 | |
| 110 | exit(0); |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 111 | } |
| 112 | |
Behdad Esfahbod | b2da26d | 2011-04-01 15:48:43 -0400 | [diff] [blame] | 113 | |
Behdad Esfahbod | 8df90c8 | 2011-08-10 15:26:41 +0200 | [diff] [blame] | 114 | static gboolean |
| 115 | parse_features (const char *name G_GNUC_UNUSED, |
| 116 | const char *arg, |
| 117 | gpointer data G_GNUC_UNUSED, |
| 118 | GError **error G_GNUC_UNUSED); |
| 119 | |
| 120 | static gboolean |
| 121 | parse_margin (const char *name G_GNUC_UNUSED, |
| 122 | const char *arg, |
| 123 | gpointer data G_GNUC_UNUSED, |
| 124 | GError **error G_GNUC_UNUSED) |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 125 | { |
Behdad Esfahbod | 8df90c8 | 2011-08-10 15:26:41 +0200 | [diff] [blame] | 126 | switch (sscanf (arg, "%f %f %f %f", &opt_margin.t, &opt_margin.r, &opt_margin.b, &opt_margin.l)) { |
| 127 | case 1: opt_margin.r = opt_margin.t; |
| 128 | case 2: opt_margin.b = opt_margin.t; |
| 129 | case 3: opt_margin.l = opt_margin.r; |
| 130 | case 4: return TRUE; |
| 131 | default: |
| 132 | g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, |
| 133 | "%s argument should be one to four space-separated numbers", |
| 134 | name); |
| 135 | return FALSE; |
| 136 | } |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 137 | } |
| 138 | |
Behdad Esfahbod | 25c4830 | 2011-08-10 16:28:38 +0200 | [diff] [blame^] | 139 | static gboolean |
| 140 | parse_shapers (const char *name G_GNUC_UNUSED, |
| 141 | const char *arg, |
| 142 | gpointer data G_GNUC_UNUSED, |
| 143 | GError **error G_GNUC_UNUSED) |
| 144 | { |
| 145 | opt_shapers = g_strsplit (arg, ",", 0); |
| 146 | return TRUE; |
| 147 | } |
| 148 | |
Behdad Esfahbod | fb9ca1b | 2011-04-04 14:50:09 -0400 | [diff] [blame] | 149 | |
Behdad Esfahbod | 8df90c8 | 2011-08-10 15:26:41 +0200 | [diff] [blame] | 150 | void |
| 151 | fail (const char *format, ...) |
| 152 | { |
| 153 | const char *msg; |
| 154 | |
| 155 | va_list vap; |
| 156 | va_start (vap, format); |
| 157 | msg = g_strdup_vprintf (format, vap); |
| 158 | g_printerr ("%s: %s\n", g_get_prgname (), msg); |
| 159 | |
| 160 | exit (1); |
| 161 | } |
| 162 | |
Behdad Esfahbod | 8df90c8 | 2011-08-10 15:26:41 +0200 | [diff] [blame] | 163 | void |
| 164 | parse_options (int argc, char *argv[]) |
| 165 | { |
Behdad Esfahbod | 8df90c8 | 2011-08-10 15:26:41 +0200 | [diff] [blame] | 166 | GOptionEntry entries[] = |
| 167 | { |
Behdad Esfahbod | a21add6 | 2011-08-10 16:07:49 +0200 | [diff] [blame] | 168 | {"version", 0, G_OPTION_FLAG_NO_ARG, |
| 169 | G_OPTION_ARG_CALLBACK, (gpointer) &show_version, "Show version numbers", NULL}, |
| 170 | {"debug", 0, 0, G_OPTION_ARG_NONE, &debug, "Free all resources before exit", NULL}, |
| 171 | {"output", 0, 0, G_OPTION_ARG_STRING, &out_file, "Set output file name", "filename"}, |
Behdad Esfahbod | 8df90c8 | 2011-08-10 15:26:41 +0200 | [diff] [blame] | 172 | |
Behdad Esfahbod | a21add6 | 2011-08-10 16:07:49 +0200 | [diff] [blame] | 173 | {"annotate", 0, 0, G_OPTION_ARG_NONE, &annotate, "Annotate output rendering", NULL}, |
| 174 | {"background", 0, 0, G_OPTION_ARG_STRING, &back, "Set background color", "red/#rrggbb/#rrggbbaa"}, |
| 175 | {"foreground", 0, 0, G_OPTION_ARG_STRING, &fore, "Set foreground color", "red/#rrggbb/#rrggbbaa"}, |
| 176 | {"line-space", 0, 0, G_OPTION_ARG_DOUBLE, &font_size, "Set space between lines (default: 0)", "units"}, |
| 177 | {"margin", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_margin, "Margin around output", "one to four numbers"}, |
Behdad Esfahbod | 8df90c8 | 2011-08-10 15:26:41 +0200 | [diff] [blame] | 178 | |
Behdad Esfahbod | 25c4830 | 2011-08-10 16:28:38 +0200 | [diff] [blame^] | 179 | {"shapers", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_shapers, "Comma-separated list of shapers", "list"}, |
Behdad Esfahbod | a21add6 | 2011-08-10 16:07:49 +0200 | [diff] [blame] | 180 | {"direction", 0, 0, G_OPTION_ARG_STRING, &direction, "Set text direction (default: auto)", "ltr/rtl/ttb/btt"}, |
| 181 | {"language", 0, 0, G_OPTION_ARG_STRING, &language, "Set text language (default: $LANG)", "langstr"}, |
| 182 | {"script", 0, 0, G_OPTION_ARG_STRING, &script, "Set text script (default: auto)", "ISO-15924 tag"}, |
| 183 | {"features", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_features, "Font features to apply to text", "TODO"}, |
Behdad Esfahbod | 8df90c8 | 2011-08-10 15:26:41 +0200 | [diff] [blame] | 184 | |
Behdad Esfahbod | a21add6 | 2011-08-10 16:07:49 +0200 | [diff] [blame] | 185 | {"face-index", 0, 0, G_OPTION_ARG_INT, &face_index, "Face index (default: 0)", "index"}, |
| 186 | {"font-size", 0, 0, G_OPTION_ARG_DOUBLE, &font_size, "Font size", "size"}, |
Behdad Esfahbod | 8df90c8 | 2011-08-10 15:26:41 +0200 | [diff] [blame] | 187 | |
| 188 | {NULL} |
| 189 | }; |
| 190 | GError *error = NULL; |
| 191 | GError *parse_error = NULL; |
| 192 | GOptionContext *context; |
| 193 | size_t len; |
| 194 | |
| 195 | context = g_option_context_new ("- FONT-FILE TEXT"); |
| 196 | |
| 197 | g_option_context_add_main_entries (context, entries, NULL); |
| 198 | |
| 199 | if (!g_option_context_parse (context, &argc, &argv, &parse_error)) |
| 200 | { |
| 201 | if (parse_error != NULL) |
| 202 | fail ("%s", parse_error->message); |
| 203 | else |
| 204 | fail ("Option parse error"); |
| 205 | exit(1); |
| 206 | } |
| 207 | g_option_context_free(context); |
Behdad Esfahbod | 8df90c8 | 2011-08-10 15:26:41 +0200 | [diff] [blame] | 208 | |
| 209 | if (argc != 3) { |
| 210 | g_printerr ("Usage: %s [OPTION...] FONT-FILE TEXT\n", g_get_prgname ()); |
| 211 | exit (1); |
| 212 | } |
| 213 | |
| 214 | font_file = argv[1]; |
| 215 | text = g_locale_to_utf8 (argv[2], -1, NULL, NULL, &error); |
| 216 | } |
| 217 | |
| 218 | |
| 219 | |
Behdad Esfahbod | fb9ca1b | 2011-04-04 14:50:09 -0400 | [diff] [blame] | 220 | static void |
| 221 | parse_space (char **pp) |
| 222 | { |
| 223 | char c; |
| 224 | #define ISSPACE(c) ((c)==' '||(c)=='\f'||(c)=='\n'||(c)=='\r'||(c)=='\t'||(c)=='\v') |
| 225 | while (c = **pp, ISSPACE (c)) |
| 226 | (*pp)++; |
| 227 | #undef ISSPACE |
| 228 | } |
| 229 | |
| 230 | static hb_bool_t |
| 231 | parse_char (char **pp, char c) |
| 232 | { |
| 233 | parse_space (pp); |
| 234 | |
| 235 | if (**pp != c) |
| 236 | return FALSE; |
| 237 | |
| 238 | (*pp)++; |
| 239 | return TRUE; |
| 240 | } |
| 241 | |
| 242 | static hb_bool_t |
| 243 | parse_uint (char **pp, unsigned int *pv) |
| 244 | { |
| 245 | char *p = *pp; |
| 246 | unsigned int v; |
| 247 | |
| 248 | v = strtol (p, pp, 0); |
| 249 | |
| 250 | if (p == *pp) |
| 251 | return FALSE; |
| 252 | |
| 253 | *pv = v; |
| 254 | return TRUE; |
| 255 | } |
| 256 | |
| 257 | |
| 258 | static hb_bool_t |
| 259 | parse_feature_value_prefix (char **pp, hb_feature_t *feature) |
| 260 | { |
| 261 | if (parse_char (pp, '-')) |
| 262 | feature->value = 0; |
| 263 | else { |
| 264 | parse_char (pp, '+'); |
| 265 | feature->value = 1; |
| 266 | } |
| 267 | |
| 268 | return TRUE; |
| 269 | } |
| 270 | |
| 271 | static hb_bool_t |
| 272 | parse_feature_tag (char **pp, hb_feature_t *feature) |
| 273 | { |
| 274 | char *p = *pp, c; |
| 275 | |
| 276 | parse_space (pp); |
| 277 | |
Behdad Esfahbod | 08da7a3 | 2011-04-21 16:59:10 -0400 | [diff] [blame] | 278 | #define ISALNUM(c) (('a' <= (c) && (c) <= 'z') || ('A' <= (c) && (c) <= 'Z') || ('0' <= (c) && (c) <= '9')) |
| 279 | while (c = **pp, ISALNUM(c)) |
Behdad Esfahbod | fb9ca1b | 2011-04-04 14:50:09 -0400 | [diff] [blame] | 280 | (*pp)++; |
Behdad Esfahbod | 08da7a3 | 2011-04-21 16:59:10 -0400 | [diff] [blame] | 281 | #undef ISALNUM |
Behdad Esfahbod | fb9ca1b | 2011-04-04 14:50:09 -0400 | [diff] [blame] | 282 | |
| 283 | if (p == *pp) |
| 284 | return FALSE; |
| 285 | |
| 286 | **pp = '\0'; |
| 287 | feature->tag = hb_tag_from_string (p); |
| 288 | **pp = c; |
| 289 | |
| 290 | return TRUE; |
| 291 | } |
| 292 | |
| 293 | static hb_bool_t |
| 294 | parse_feature_indices (char **pp, hb_feature_t *feature) |
| 295 | { |
| 296 | hb_bool_t has_start; |
| 297 | |
| 298 | feature->start = 0; |
| 299 | feature->end = (unsigned int) -1; |
| 300 | |
| 301 | if (!parse_char (pp, '[')) |
| 302 | return TRUE; |
| 303 | |
| 304 | has_start = parse_uint (pp, &feature->start); |
| 305 | |
| 306 | if (parse_char (pp, ':')) { |
| 307 | parse_uint (pp, &feature->end); |
| 308 | } else { |
| 309 | if (has_start) |
| 310 | feature->end = feature->start + 1; |
| 311 | } |
| 312 | |
| 313 | return parse_char (pp, ']'); |
| 314 | } |
| 315 | |
| 316 | static hb_bool_t |
| 317 | parse_feature_value_postfix (char **pp, hb_feature_t *feature) |
| 318 | { |
| 319 | return !parse_char (pp, '=') || parse_uint (pp, &feature->value); |
| 320 | } |
| 321 | |
| 322 | |
| 323 | static hb_bool_t |
| 324 | parse_one_feature (char **pp, hb_feature_t *feature) |
| 325 | { |
| 326 | return parse_feature_value_prefix (pp, feature) && |
| 327 | parse_feature_tag (pp, feature) && |
| 328 | parse_feature_indices (pp, feature) && |
| 329 | parse_feature_value_postfix (pp, feature) && |
| 330 | (parse_char (pp, ',') || **pp == '\0'); |
| 331 | } |
| 332 | |
| 333 | static void |
| 334 | skip_one_feature (char **pp) |
| 335 | { |
| 336 | char *e; |
| 337 | e = strchr (*pp, ','); |
| 338 | if (e) |
| 339 | *pp = e + 1; |
| 340 | else |
| 341 | *pp = *pp + strlen (*pp); |
| 342 | } |
| 343 | |
Behdad Esfahbod | 8df90c8 | 2011-08-10 15:26:41 +0200 | [diff] [blame] | 344 | static gboolean |
| 345 | parse_features (const char *name G_GNUC_UNUSED, |
| 346 | const char *arg, |
| 347 | gpointer data G_GNUC_UNUSED, |
| 348 | GError **error G_GNUC_UNUSED) |
Behdad Esfahbod | b2da26d | 2011-04-01 15:48:43 -0400 | [diff] [blame] | 349 | { |
Behdad Esfahbod | 8df90c8 | 2011-08-10 15:26:41 +0200 | [diff] [blame] | 350 | char *s = (char *) arg; |
Behdad Esfahbod | b2da26d | 2011-04-01 15:48:43 -0400 | [diff] [blame] | 351 | char *p; |
Behdad Esfahbod | b2da26d | 2011-04-01 15:48:43 -0400 | [diff] [blame] | 352 | |
| 353 | num_features = 0; |
| 354 | features = NULL; |
| 355 | |
| 356 | if (!*s) |
Behdad Esfahbod | 8df90c8 | 2011-08-10 15:26:41 +0200 | [diff] [blame] | 357 | return TRUE; |
Behdad Esfahbod | b2da26d | 2011-04-01 15:48:43 -0400 | [diff] [blame] | 358 | |
| 359 | /* count the features first, so we can allocate memory */ |
| 360 | p = s; |
| 361 | do { |
| 362 | num_features++; |
| 363 | p = strchr (p, ','); |
| 364 | if (p) |
Behdad Esfahbod | fb9ca1b | 2011-04-04 14:50:09 -0400 | [diff] [blame] | 365 | p++; |
Behdad Esfahbod | b2da26d | 2011-04-01 15:48:43 -0400 | [diff] [blame] | 366 | } while (p); |
| 367 | |
Behdad Esfahbod | f1425a5 | 2011-04-27 12:15:06 -0400 | [diff] [blame] | 368 | features = (hb_feature_t *) calloc (num_features, sizeof (*features)); |
Behdad Esfahbod | b2da26d | 2011-04-01 15:48:43 -0400 | [diff] [blame] | 369 | |
| 370 | /* now do the actual parsing */ |
| 371 | p = s; |
Behdad Esfahbod | fb9ca1b | 2011-04-04 14:50:09 -0400 | [diff] [blame] | 372 | num_features = 0; |
| 373 | while (*p) { |
| 374 | if (parse_one_feature (&p, &features[num_features])) |
| 375 | num_features++; |
| 376 | else |
| 377 | skip_one_feature (&p); |
Behdad Esfahbod | b2da26d | 2011-04-01 15:48:43 -0400 | [diff] [blame] | 378 | } |
Behdad Esfahbod | 8df90c8 | 2011-08-10 15:26:41 +0200 | [diff] [blame] | 379 | |
| 380 | return TRUE; |
Behdad Esfahbod | b2da26d | 2011-04-01 15:48:43 -0400 | [diff] [blame] | 381 | } |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 382 | |
| 383 | |
| 384 | static cairo_glyph_t * |
| 385 | _hb_cr_text_glyphs (cairo_t *cr, |
Behdad Esfahbod | 31f18ab | 2011-06-15 09:49:58 -0400 | [diff] [blame] | 386 | const char *utf8, int len, |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 387 | unsigned int *pnum_glyphs) |
| 388 | { |
| 389 | cairo_scaled_font_t *scaled_font = cairo_get_scaled_font (cr); |
| 390 | FT_Face ft_face = cairo_ft_scaled_font_lock_face (scaled_font); |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 391 | hb_font_t *hb_font = hb_ft_font_create (ft_face, NULL); |
| 392 | hb_buffer_t *hb_buffer; |
| 393 | cairo_glyph_t *cairo_glyphs; |
| 394 | hb_glyph_info_t *hb_glyph; |
| 395 | hb_glyph_position_t *hb_position; |
| 396 | unsigned int num_glyphs, i; |
Behdad Esfahbod | 1883af3 | 2011-05-16 15:18:16 -0400 | [diff] [blame] | 397 | hb_position_t x, y; |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 398 | |
Behdad Esfahbod | d4bee9f | 2011-04-27 09:24:37 -0400 | [diff] [blame] | 399 | hb_buffer = hb_buffer_create (0); |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 400 | |
Behdad Esfahbod | 39a840a | 2011-04-27 14:48:19 -0400 | [diff] [blame] | 401 | if (direction) |
| 402 | hb_buffer_set_direction (hb_buffer, hb_direction_from_string (direction)); |
Behdad Esfahbod | 5d91c3d | 2011-03-16 17:36:32 -0300 | [diff] [blame] | 403 | if (script) |
Behdad Esfahbod | 0e8d35c | 2011-04-15 19:07:10 -0400 | [diff] [blame] | 404 | hb_buffer_set_script (hb_buffer, hb_script_from_string (script)); |
Behdad Esfahbod | c0af193 | 2011-04-15 19:26:24 -0400 | [diff] [blame] | 405 | if (language) |
| 406 | hb_buffer_set_language (hb_buffer, hb_language_from_string (language)); |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 407 | |
Behdad Esfahbod | d4bee9f | 2011-04-27 09:24:37 -0400 | [diff] [blame] | 408 | if (len < 0) |
Behdad Esfahbod | 31f18ab | 2011-06-15 09:49:58 -0400 | [diff] [blame] | 409 | len = strlen (utf8); |
| 410 | hb_buffer_add_utf8 (hb_buffer, utf8, len, 0, len); |
Behdad Esfahbod | d4bee9f | 2011-04-27 09:24:37 -0400 | [diff] [blame] | 411 | |
Behdad Esfahbod | 25c4830 | 2011-08-10 16:28:38 +0200 | [diff] [blame^] | 412 | if (!hb_shape_full (hb_font, hb_buffer, features, num_features, NULL, opt_shapers)) |
| 413 | fail ("All shapers failed"); |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 414 | |
| 415 | num_glyphs = hb_buffer_get_length (hb_buffer); |
Ryan Lortie | 70566be | 2011-04-15 18:32:36 -0400 | [diff] [blame] | 416 | hb_glyph = hb_buffer_get_glyph_infos (hb_buffer, NULL); |
| 417 | hb_position = hb_buffer_get_glyph_positions (hb_buffer, NULL); |
Behdad Esfahbod | 9aa6f96 | 2011-05-16 15:08:31 -0400 | [diff] [blame] | 418 | cairo_glyphs = cairo_glyph_allocate (num_glyphs); |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 419 | x = 0; |
Behdad Esfahbod | 1883af3 | 2011-05-16 15:18:16 -0400 | [diff] [blame] | 420 | y = 0; |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 421 | for (i = 0; i < num_glyphs; i++) |
| 422 | { |
| 423 | cairo_glyphs[i].index = hb_glyph->codepoint; |
Behdad Esfahbod | 1883af3 | 2011-05-16 15:18:16 -0400 | [diff] [blame] | 424 | cairo_glyphs[i].x = ( hb_position->x_offset + x) * (1./64); |
| 425 | cairo_glyphs[i].y = (-hb_position->y_offset + y) * (1./64); |
| 426 | x += hb_position->x_advance; |
| 427 | y += -hb_position->y_advance; |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 428 | |
| 429 | hb_glyph++; |
| 430 | hb_position++; |
| 431 | } |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 432 | hb_buffer_destroy (hb_buffer); |
| 433 | hb_font_destroy (hb_font); |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 434 | cairo_ft_scaled_font_unlock_face (scaled_font); |
| 435 | |
| 436 | if (pnum_glyphs) |
| 437 | *pnum_glyphs = num_glyphs; |
| 438 | return cairo_glyphs; |
| 439 | } |
| 440 | |
| 441 | static cairo_t * |
| 442 | create_context (void) |
| 443 | { |
| 444 | cairo_t *cr; |
| 445 | unsigned int fr, fg, fb, fa, br, bg, bb, ba; |
| 446 | |
| 447 | if (surface) |
| 448 | cairo_surface_destroy (surface); |
| 449 | if (back_pattern) |
| 450 | cairo_pattern_destroy (back_pattern); |
| 451 | if (fore_pattern) |
| 452 | cairo_pattern_destroy (fore_pattern); |
| 453 | |
| 454 | br = bg = bb = ba = 255; |
| 455 | sscanf (back + (*back=='#'), "%2x%2x%2x%2x", &br, &bg, &bb, &ba); |
| 456 | fr = fg = fb = 0; fa = 255; |
| 457 | sscanf (fore + (*fore=='#'), "%2x%2x%2x%2x", &fr, &fg, &fb, &fa); |
| 458 | |
Behdad Esfahbod | e609aeb | 2011-05-18 10:17:02 -0400 | [diff] [blame] | 459 | if (!annotate && ba == 255 && fa == 255 && br == bg && bg == bb && fr == fg && fg == fb) { |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 460 | /* grayscale. use A8 surface */ |
| 461 | surface = cairo_image_surface_create (CAIRO_FORMAT_A8, width, height); |
| 462 | cr = cairo_create (surface); |
| 463 | cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE); |
| 464 | cairo_set_source_rgba (cr, 1., 1., 1., br / 255.); |
| 465 | cairo_paint (cr); |
| 466 | back_pattern = cairo_pattern_reference (cairo_get_source (cr)); |
| 467 | cairo_set_source_rgba (cr, 1., 1., 1., fr / 255.); |
| 468 | fore_pattern = cairo_pattern_reference (cairo_get_source (cr)); |
| 469 | } else { |
| 470 | /* color. use (A)RGB surface */ |
| 471 | if (ba != 255) |
| 472 | surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height); |
| 473 | else |
| 474 | surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height); |
| 475 | cr = cairo_create (surface); |
| 476 | cairo_set_source_rgba (cr, br / 255., bg / 255., bb / 255., ba / 255.); |
| 477 | cairo_paint (cr); |
| 478 | back_pattern = cairo_pattern_reference (cairo_get_source (cr)); |
| 479 | cairo_set_source_rgba (cr, fr / 255., fg / 255., fb / 255., fa / 255.); |
| 480 | fore_pattern = cairo_pattern_reference (cairo_get_source (cr)); |
| 481 | } |
| 482 | |
| 483 | cairo_set_font_face (cr, cairo_face); |
| 484 | |
| 485 | return cr; |
| 486 | } |
| 487 | |
| 488 | static void |
| 489 | draw (void) |
| 490 | { |
| 491 | cairo_t *cr; |
| 492 | cairo_font_extents_t font_extents; |
| 493 | |
| 494 | cairo_glyph_t *glyphs = NULL; |
| 495 | unsigned int num_glyphs = 0; |
| 496 | |
| 497 | const char *end, *p = text; |
| 498 | double x, y; |
| 499 | |
| 500 | cr= create_context (); |
| 501 | |
| 502 | cairo_set_font_size (cr, font_size); |
| 503 | cairo_font_extents (cr, &font_extents); |
| 504 | |
| 505 | height = 0; |
| 506 | width = 0; |
| 507 | |
Behdad Esfahbod | 8df90c8 | 2011-08-10 15:26:41 +0200 | [diff] [blame] | 508 | x = opt_margin.l; |
| 509 | y = opt_margin.t; |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 510 | |
| 511 | do { |
| 512 | cairo_text_extents_t extents; |
| 513 | |
| 514 | end = strchr (p, '\n'); |
| 515 | if (!end) |
| 516 | end = p + strlen (p); |
| 517 | |
| 518 | if (p != text) |
| 519 | y += line_space; |
| 520 | |
| 521 | if (p != end) { |
| 522 | glyphs = _hb_cr_text_glyphs (cr, p, end - p, &num_glyphs); |
Behdad Esfahbod | 1b4a2cc | 2011-04-04 14:45:28 -0400 | [diff] [blame] | 523 | |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 524 | cairo_glyph_extents (cr, glyphs, num_glyphs, &extents); |
| 525 | |
| 526 | y += ceil (font_extents.ascent); |
| 527 | width = MAX (width, extents.x_advance); |
| 528 | cairo_save (cr); |
| 529 | cairo_translate (cr, x, y); |
Behdad Esfahbod | e609aeb | 2011-05-18 10:17:02 -0400 | [diff] [blame] | 530 | if (annotate) { |
| 531 | unsigned int i; |
| 532 | cairo_save (cr); |
| 533 | |
| 534 | /* Draw actual glyph origins */ |
| 535 | cairo_set_source_rgba (cr, 1., 0., 0., .5); |
| 536 | cairo_set_line_width (cr, 5); |
| 537 | cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); |
| 538 | for (i = 0; i < num_glyphs; i++) { |
| 539 | cairo_move_to (cr, glyphs[i].x, glyphs[i].y); |
| 540 | cairo_rel_line_to (cr, 0, 0); |
| 541 | } |
| 542 | cairo_stroke (cr); |
| 543 | |
| 544 | cairo_restore (cr); |
| 545 | } |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 546 | cairo_show_glyphs (cr, glyphs, num_glyphs); |
| 547 | cairo_restore (cr); |
| 548 | y += ceil (font_extents.height - ceil (font_extents.ascent)); |
Behdad Esfahbod | 1b4a2cc | 2011-04-04 14:45:28 -0400 | [diff] [blame] | 549 | |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 550 | cairo_glyph_free (glyphs); |
| 551 | } |
| 552 | |
| 553 | p = end + 1; |
| 554 | } while (*end); |
| 555 | |
Behdad Esfahbod | 8df90c8 | 2011-08-10 15:26:41 +0200 | [diff] [blame] | 556 | height = y + opt_margin.b; |
| 557 | width += opt_margin.l + opt_margin.r; |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 558 | |
| 559 | cairo_destroy (cr); |
| 560 | } |
| 561 | |
| 562 | |
| 563 | |
| 564 | int |
| 565 | main (int argc, char **argv) |
| 566 | { |
Behdad Esfahbod | 31f18ab | 2011-06-15 09:49:58 -0400 | [diff] [blame] | 567 | static FT_Library ft_library; |
| 568 | static FT_Face ft_face; |
Behdad Esfahbod | b7b2968 | 2011-03-02 01:01:03 -0500 | [diff] [blame] | 569 | cairo_status_t status; |
| 570 | |
Behdad Esfahbod | cab6f65 | 2011-04-04 15:36:51 -0400 | [diff] [blame] | 571 | setlocale (LC_ALL, ""); |
| 572 | |
Behdad Esfahbod | 8df90c8 | 2011-08-10 15:26:41 +0200 | [diff] [blame] | 573 | parse_options (argc, argv); |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 574 | |
| 575 | FT_Init_FreeType (&ft_library); |
Behdad Esfahbod | 0c6a976 | 2011-05-31 12:59:17 -0400 | [diff] [blame] | 576 | if (FT_New_Face (ft_library, font_file, face_index, &ft_face)) { |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 577 | fprintf (stderr, "Failed to open font file `%s'\n", font_file); |
| 578 | exit (1); |
| 579 | } |
| 580 | cairo_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0); |
| 581 | |
| 582 | draw (); |
| 583 | draw (); |
| 584 | |
Behdad Esfahbod | b7b2968 | 2011-03-02 01:01:03 -0500 | [diff] [blame] | 585 | status = cairo_surface_write_to_png (surface, out_file); |
| 586 | if (status != CAIRO_STATUS_SUCCESS) { |
| 587 | fprintf (stderr, "Failed to write output file `%s': %s\n", |
| 588 | out_file, cairo_status_to_string (status)); |
| 589 | exit (1); |
| 590 | } |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 591 | |
Behdad Esfahbod | 1b4a2cc | 2011-04-04 14:45:28 -0400 | [diff] [blame] | 592 | if (debug) { |
| 593 | free (features); |
Behdad Esfahbod | 25c4830 | 2011-08-10 16:28:38 +0200 | [diff] [blame^] | 594 | g_free (opt_shapers); |
Behdad Esfahbod | 1b4a2cc | 2011-04-04 14:45:28 -0400 | [diff] [blame] | 595 | |
| 596 | cairo_pattern_destroy (fore_pattern); |
| 597 | cairo_pattern_destroy (back_pattern); |
| 598 | cairo_surface_destroy (surface); |
| 599 | cairo_font_face_destroy (cairo_face); |
| 600 | cairo_debug_reset_static_data (); |
| 601 | |
| 602 | FT_Done_Face (ft_face); |
| 603 | FT_Done_FreeType (ft_library); |
| 604 | } |
| 605 | |
Behdad Esfahbod | 5353bf4 | 2011-02-22 18:06:19 -0500 | [diff] [blame] | 606 | return 0; |
| 607 | } |
Behdad Esfahbod | c57d454 | 2011-04-20 18:50:27 -0400 | [diff] [blame] | 608 | |
| 609 | |