gfx/harfbuzz/src/test-buffer-serialize.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/harfbuzz/src/test-buffer-serialize.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,129 @@
     1.4 +/*
     1.5 + * Copyright © 2010,2011,2013  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 +#ifdef HAVE_FREETYPE
    1.36 +#include "hb-ft.h"
    1.37 +#endif
    1.38 +
    1.39 +#ifdef HAVE_GLIB
    1.40 +# include <glib.h>
    1.41 +# if !GLIB_CHECK_VERSION (2, 22, 0)
    1.42 +#  define g_mapped_file_unref g_mapped_file_free
    1.43 +# endif
    1.44 +#endif
    1.45 +#include <stdlib.h>
    1.46 +#include <stdio.h>
    1.47 +
    1.48 +int
    1.49 +main (int argc, char **argv)
    1.50 +{
    1.51 +  hb_blob_t *blob = NULL;
    1.52 +
    1.53 +  if (argc != 2) {
    1.54 +    fprintf (stderr, "usage: %s font-file\n", argv[0]);
    1.55 +    exit (1);
    1.56 +  }
    1.57 +
    1.58 +  /* Create the blob */
    1.59 +  {
    1.60 +    const char *font_data;
    1.61 +    unsigned int len;
    1.62 +    hb_destroy_func_t destroy;
    1.63 +    void *user_data;
    1.64 +    hb_memory_mode_t mm;
    1.65 +
    1.66 +#ifdef HAVE_GLIB
    1.67 +    GMappedFile *mf = g_mapped_file_new (argv[1], false, NULL);
    1.68 +    font_data = g_mapped_file_get_contents (mf);
    1.69 +    len = g_mapped_file_get_length (mf);
    1.70 +    destroy = (hb_destroy_func_t) g_mapped_file_unref;
    1.71 +    user_data = (void *) mf;
    1.72 +    mm = HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE;
    1.73 +#else
    1.74 +    FILE *f = fopen (argv[1], "rb");
    1.75 +    fseek (f, 0, SEEK_END);
    1.76 +    len = ftell (f);
    1.77 +    fseek (f, 0, SEEK_SET);
    1.78 +    font_data = (const char *) malloc (len);
    1.79 +    if (!font_data) len = 0;
    1.80 +    len = fread ((char *) font_data, 1, len, f);
    1.81 +    destroy = free;
    1.82 +    user_data = (void *) font_data;
    1.83 +    fclose (f);
    1.84 +    mm = HB_MEMORY_MODE_WRITABLE;
    1.85 +#endif
    1.86 +
    1.87 +    blob = hb_blob_create (font_data, len, mm, user_data, destroy);
    1.88 +  }
    1.89 +
    1.90 +  hb_face_t *face = hb_face_create (blob, 0 /* first face */);
    1.91 +  hb_blob_destroy (blob);
    1.92 +  blob = NULL;
    1.93 +
    1.94 +  unsigned int upem = hb_face_get_upem (face);
    1.95 +  hb_font_t *font = hb_font_create (face);
    1.96 +  hb_face_destroy (face);
    1.97 +  hb_font_set_scale (font, upem, upem);
    1.98 +#ifdef HAVE_FREETYPE
    1.99 +  hb_ft_font_set_funcs (font);
   1.100 +#endif
   1.101 +
   1.102 +  hb_buffer_t *buf;
   1.103 +  buf = hb_buffer_create ();
   1.104 +
   1.105 +  bool ret = true;
   1.106 +  char line[BUFSIZ], out[BUFSIZ];
   1.107 +  while (fgets (line, sizeof(line), stdin) != 0)
   1.108 +  {
   1.109 +    hb_buffer_clear_contents (buf);
   1.110 +
   1.111 +    const char *p = line;
   1.112 +    while (hb_buffer_deserialize_glyphs (buf,
   1.113 +					 p, -1, &p,
   1.114 +					 font,
   1.115 +					 HB_BUFFER_SERIALIZE_FORMAT_JSON))
   1.116 +      ;
   1.117 +    if (*p && *p != '\n')
   1.118 +      ret = false;
   1.119 +
   1.120 +    hb_buffer_serialize_glyphs (buf, 0, hb_buffer_get_length (buf),
   1.121 +				out, sizeof (out), NULL,
   1.122 +				font, HB_BUFFER_SERIALIZE_FORMAT_JSON,
   1.123 +				HB_BUFFER_SERIALIZE_FLAG_DEFAULT);
   1.124 +    puts (out);
   1.125 +  }
   1.126 +
   1.127 +  hb_buffer_destroy (buf);
   1.128 +
   1.129 +  hb_font_destroy (font);
   1.130 +
   1.131 +  return !ret;
   1.132 +}

mercurial