1be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod/*
2be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod * Copyright © 2010,2011  Google, Inc.
3be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod *
4be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod *  This is part of HarfBuzz, a text shaping library.
5be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod *
6be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod * Permission is hereby granted, without written agreement and without
7be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod * license or royalty fees, to use, copy, modify, and distribute this
8be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod * software and its documentation for any purpose, provided that the
9be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod * above copyright notice and the following two paragraphs appear in
10be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod * all copies of this software.
11be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod *
12be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod * DAMAGE.
17be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod *
18be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod *
24be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod * Google Author(s): Behdad Esfahbod
25be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod */
26be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod
27be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod#ifdef HAVE_CONFIG_H
28be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod#include "config.h"
29be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod#endif
30be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod
31be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod#include "hb.h"
32be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod
33be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod#ifdef HAVE_GLIB
34c2bc818706df56022c8bb922df2b741cb120f7e4Behdad Esfahbod# include <glib.h>
35c2bc818706df56022c8bb922df2b741cb120f7e4Behdad Esfahbod# if !GLIB_CHECK_VERSION (2, 22, 0)
36c2bc818706df56022c8bb922df2b741cb120f7e4Behdad Esfahbod#  define g_mapped_file_unref g_mapped_file_free
37c2bc818706df56022c8bb922df2b741cb120f7e4Behdad Esfahbod# endif
38be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod#endif
39be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod#include <stdlib.h>
40be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod#include <stdio.h>
41be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod
42be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod#ifdef HAVE_FREETYPE
43be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod#include "hb-ft.h"
44be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod#endif
45be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod
46be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbodint
47be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbodmain (int argc, char **argv)
48be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod{
49be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod  hb_blob_t *blob = NULL;
50be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod
51be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod  if (argc != 2) {
52be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod    fprintf (stderr, "usage: %s font-file.ttf\n", argv[0]);
53be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod    exit (1);
54be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod  }
55be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod
56be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod  /* Create the blob */
57be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod  {
58be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod    const char *font_data;
59be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod    unsigned int len;
60be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod    hb_destroy_func_t destroy;
61be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod    void *user_data;
62be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod    hb_memory_mode_t mm;
63be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod
64be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod#ifdef HAVE_GLIB
658a7e70ef65952fc16b8c1d5f1126c94d9d81e755Behdad Esfahbod    GMappedFile *mf = g_mapped_file_new (argv[1], false, NULL);
66be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod    font_data = g_mapped_file_get_contents (mf);
67be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod    len = g_mapped_file_get_length (mf);
68c2bc818706df56022c8bb922df2b741cb120f7e4Behdad Esfahbod    destroy = (hb_destroy_func_t) g_mapped_file_unref;
69be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod    user_data = (void *) mf;
70be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod    mm = HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE;
71be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod#else
72be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod    FILE *f = fopen (argv[1], "rb");
73be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod    fseek (f, 0, SEEK_END);
74be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod    len = ftell (f);
75be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod    fseek (f, 0, SEEK_SET);
76be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod    font_data = (const char *) malloc (len);
77be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod    if (!font_data) len = 0;
78be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod    len = fread ((char *) font_data, 1, len, f);
79be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod    destroy = free;
80be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod    user_data = (void *) font_data;
81be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod    fclose (f);
82be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod    mm = HB_MEMORY_MODE_WRITABLE;
83be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod#endif
84be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod
85be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod    blob = hb_blob_create (font_data, len, mm, user_data, destroy);
86be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod  }
87be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod
88be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod  printf ("Opened font file %s: %u bytes long\n", argv[1], hb_blob_get_length (blob));
89be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod
90be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod  /* Create the face */
91be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod  hb_face_t *face = hb_face_create (blob, 0 /* first face */);
92be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod  hb_blob_destroy (blob);
93be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod  blob = NULL;
94be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod  unsigned int upem = hb_face_get_upem (face);
95be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod
96be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod  hb_font_t *font = hb_font_create (face);
97be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod  hb_font_set_scale (font, upem, upem);
98be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod
99be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod#ifdef HAVE_FREETYPE
100be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod  hb_ft_font_set_funcs (font);
101be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod#endif
102be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod
103be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod  hb_buffer_t *buffer = hb_buffer_create ();
104be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod
105be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod  hb_buffer_add_utf8 (buffer, "\xe0\xa4\x95\xe0\xa5\x8d\xe0\xa4\xb0\xe0\xa5\x8d\xe0\xa4\x95", -1, 0, -1);
106ef9e02eddfc9a37d50723e96839635d79191d849Behdad Esfahbod  hb_buffer_guess_segment_properties (buffer);
107be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod
108be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod  hb_shape (font, buffer, NULL, 0);
109be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod
110be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod  unsigned int count = hb_buffer_get_length (buffer);
111be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod  hb_glyph_info_t *infos = hb_buffer_get_glyph_infos (buffer, NULL);
112be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod  hb_glyph_position_t *positions = hb_buffer_get_glyph_positions (buffer, NULL);
113be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod
114be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod  for (unsigned int i = 0; i < count; i++)
115be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod  {
116be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod    hb_glyph_info_t *info = &infos[i];
117be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod    hb_glyph_position_t *pos = &positions[i];
118be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod
119be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod    printf ("cluster %d	glyph 0x%x at	(%d,%d)+(%d,%d)\n",
120be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod	    info->cluster,
121be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod	    info->codepoint,
122be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod	    pos->x_offset,
123be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod	    pos->x_offset,
124be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod	    pos->x_advance,
125be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod	    pos->y_advance);
126be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod
127be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod  }
128be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod
129be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod  hb_buffer_destroy (buffer);
130be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod  hb_font_destroy (font);
131be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod  hb_face_destroy (face);
132be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod
133be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod  return 0;
134be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod}
135be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod
136be73a5f9368136ecbdb211b96516ad0c554c8201Behdad Esfahbod
137