gfx/harfbuzz/src/test.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/harfbuzz/src/test.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,136 @@
     1.4 +/*
     1.5 + * Copyright © 2010,2011  Google, Inc.
     1.6 + *
     1.7 + *  This is part of HarfBuzz, a text shaping library.
     1.8 + *
     1.9 + * Permission is hereby granted, without written agreement and without
    1.10 + * license or royalty fees, to use, copy, modify, and distribute this
    1.11 + * software and its documentation for any purpose, provided that the
    1.12 + * above copyright notice and the following two paragraphs appear in
    1.13 + * all copies of this software.
    1.14 + *
    1.15 + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
    1.16 + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
    1.17 + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
    1.18 + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
    1.19 + * DAMAGE.
    1.20 + *
    1.21 + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
    1.22 + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
    1.23 + * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
    1.24 + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
    1.25 + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
    1.26 + *
    1.27 + * Google Author(s): Behdad Esfahbod
    1.28 + */
    1.29 +
    1.30 +#ifdef HAVE_CONFIG_H
    1.31 +#include "config.h"
    1.32 +#endif
    1.33 +
    1.34 +#include "hb.h"
    1.35 +
    1.36 +#ifdef HAVE_GLIB
    1.37 +# include <glib.h>
    1.38 +# if !GLIB_CHECK_VERSION (2, 22, 0)
    1.39 +#  define g_mapped_file_unref g_mapped_file_free
    1.40 +# endif
    1.41 +#endif
    1.42 +#include <stdlib.h>
    1.43 +#include <stdio.h>
    1.44 +
    1.45 +#ifdef HAVE_FREETYPE
    1.46 +#include "hb-ft.h"
    1.47 +#endif
    1.48 +
    1.49 +int
    1.50 +main (int argc, char **argv)
    1.51 +{
    1.52 +  hb_blob_t *blob = NULL;
    1.53 +
    1.54 +  if (argc != 2) {
    1.55 +    fprintf (stderr, "usage: %s font-file.ttf\n", argv[0]);
    1.56 +    exit (1);
    1.57 +  }
    1.58 +
    1.59 +  /* Create the blob */
    1.60 +  {
    1.61 +    const char *font_data;
    1.62 +    unsigned int len;
    1.63 +    hb_destroy_func_t destroy;
    1.64 +    void *user_data;
    1.65 +    hb_memory_mode_t mm;
    1.66 +
    1.67 +#ifdef HAVE_GLIB
    1.68 +    GMappedFile *mf = g_mapped_file_new (argv[1], false, NULL);
    1.69 +    font_data = g_mapped_file_get_contents (mf);
    1.70 +    len = g_mapped_file_get_length (mf);
    1.71 +    destroy = (hb_destroy_func_t) g_mapped_file_unref;
    1.72 +    user_data = (void *) mf;
    1.73 +    mm = HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE;
    1.74 +#else
    1.75 +    FILE *f = fopen (argv[1], "rb");
    1.76 +    fseek (f, 0, SEEK_END);
    1.77 +    len = ftell (f);
    1.78 +    fseek (f, 0, SEEK_SET);
    1.79 +    font_data = (const char *) malloc (len);
    1.80 +    if (!font_data) len = 0;
    1.81 +    len = fread ((char *) font_data, 1, len, f);
    1.82 +    destroy = free;
    1.83 +    user_data = (void *) font_data;
    1.84 +    fclose (f);
    1.85 +    mm = HB_MEMORY_MODE_WRITABLE;
    1.86 +#endif
    1.87 +
    1.88 +    blob = hb_blob_create (font_data, len, mm, user_data, destroy);
    1.89 +  }
    1.90 +
    1.91 +  printf ("Opened font file %s: %u bytes long\n", argv[1], hb_blob_get_length (blob));
    1.92 +
    1.93 +  /* Create the face */
    1.94 +  hb_face_t *face = hb_face_create (blob, 0 /* first face */);
    1.95 +  hb_blob_destroy (blob);
    1.96 +  blob = NULL;
    1.97 +  unsigned int upem = hb_face_get_upem (face);
    1.98 +
    1.99 +  hb_font_t *font = hb_font_create (face);
   1.100 +  hb_font_set_scale (font, upem, upem);
   1.101 +
   1.102 +#ifdef HAVE_FREETYPE
   1.103 +  hb_ft_font_set_funcs (font);
   1.104 +#endif
   1.105 +
   1.106 +  hb_buffer_t *buffer = hb_buffer_create ();
   1.107 +
   1.108 +  hb_buffer_add_utf8 (buffer, "\xe0\xa4\x95\xe0\xa5\x8d\xe0\xa4\xb0\xe0\xa5\x8d\xe0\xa4\x95", -1, 0, -1);
   1.109 +  hb_buffer_guess_segment_properties (buffer);
   1.110 +
   1.111 +  hb_shape (font, buffer, NULL, 0);
   1.112 +
   1.113 +  unsigned int count = hb_buffer_get_length (buffer);
   1.114 +  hb_glyph_info_t *infos = hb_buffer_get_glyph_infos (buffer, NULL);
   1.115 +  hb_glyph_position_t *positions = hb_buffer_get_glyph_positions (buffer, NULL);
   1.116 +
   1.117 +  for (unsigned int i = 0; i < count; i++)
   1.118 +  {
   1.119 +    hb_glyph_info_t *info = &infos[i];
   1.120 +    hb_glyph_position_t *pos = &positions[i];
   1.121 +
   1.122 +    printf ("cluster %d	glyph 0x%x at	(%d,%d)+(%d,%d)\n",
   1.123 +	    info->cluster,
   1.124 +	    info->codepoint,
   1.125 +	    pos->x_offset,
   1.126 +	    pos->x_offset,
   1.127 +	    pos->x_advance,
   1.128 +	    pos->y_advance);
   1.129 +
   1.130 +  }
   1.131 +
   1.132 +  hb_buffer_destroy (buffer);
   1.133 +  hb_font_destroy (font);
   1.134 +  hb_face_destroy (face);
   1.135 +
   1.136 +  return 0;
   1.137 +}
   1.138 +
   1.139 +

mercurial