harfbuzz-dump-main.c 6.5 KB
Newer Older
1
/* harfbuzz-dump-main.c: Test program for OpenType
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * Copyright (C) 2000 Red Hat Software
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
#include <stdlib.h>

24
#include "harfbuzz-open.h"
25

26
#include "harfbuzz-dump.h"
27 28 29

#define N_ELEMENTS(arr) (sizeof(arr)/ sizeof((arr)[0]))

30
static int
31
croak (const char *situation, HB_Error error)
32 33 34 35 36 37
{
  fprintf (stderr, "%s: Error %d\n", situation, error);

  exit (1);
}

38
#if 0
39 40 41 42 43 44 45
enum {
  I = 1 << 0,
  M = 1 << 1,
  F = 1 << 2,
  L = 1 << 3
};

46
static void
47
print_tag (HB_UInt tag)
48 49 50
{
  fprintf (stderr, "%c%c%c%c", 
	  (unsigned char)(tag >> 24),
51 52
	  (unsigned char)((tag >> 16) & 0xff),
	  (unsigned char)((tag >> 8) & 0xff),
53 54 55
	  (unsigned char)(tag & 0xff));
}

56
static void
57
maybe_add_feature (HB_GSUB  gsub,
58 59 60
		   HB_UShort script_index,
		   HB_UInt  tag,
		   HB_UShort property)
61
{
62
  HB_Error error;
63
  HB_UShort feature_index;
64 65
  
  /* 0xffff == default language system */
66
  error = HB_GSUB_Select_Feature (gsub, tag, script_index, 0xffff, &feature_index);
67 68 69
  
  if (error)
    {
70
      if (error == HB_Err_Not_Covered)
71 72 73 74 75 76
	{
	  print_tag (tag);
	  fprintf (stderr, " not covered, ignored\n");
	  return;
	}

77
      croak ("HB_GSUB_Select_Feature", error);
78 79
    }

80 81
  if ((error = HB_GSUB_Add_Feature (gsub, feature_index, property)))
    croak ("HB_GSUB_Add_Feature", error);
82 83
}

84
static void
85 86
select_cmap (FT_Face face)
{
87
  HB_UShort  i;
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
  FT_CharMap cmap = NULL;
  
  for (i = 0; i < face->num_charmaps; i++)
    {
      if (face->charmaps[i]->platform_id == 3 && face->charmaps[i]->encoding_id == 1)
	{
	  cmap = face->charmaps[i];
	  break;
	}
    }
  
  /* we try only pid/eid (0,0) if no (3,1) map is found -- many Windows
     fonts have only rudimentary (0,0) support.                         */

  if (!cmap)
    for (i = 0; i < face->num_charmaps; i++)
      {
	if (face->charmaps[i]->platform_id == 3 && face->charmaps[i]->encoding_id == 1)
	  {
	    cmap = face->charmaps[i];
	    break;
	  }
      }

  if (cmap)
    FT_Set_Charmap (face, cmap);
  else
    {
      fprintf (stderr, "Sorry, but this font doesn't contain"
	       " any Unicode mapping table.\n");
      exit (1);
    }
}

122
static void
123
add_features (HB_GSUB gsub)
124
{
125
  HB_Error error;
126 127
  HB_UInt tag = FT_MAKE_TAG ('a', 'r', 'a', 'b');
  HB_UShort script_index;
128

129
  error = HB_GSUB_Select_Script (gsub, tag, &script_index);
130 131 132

  if (error)
    {
133
      if (error == HB_Err_Not_Covered)
134 135 136 137 138
	{
	  fprintf (stderr, "Arabic not covered, no features used\n");
	  return;
	}

139
      croak ("HB_GSUB_Select_Script", error);
140 141 142 143 144 145 146
    }

  maybe_add_feature (gsub, script_index, FT_MAKE_TAG ('i', 'n', 'i', 't'), I);
  maybe_add_feature (gsub, script_index, FT_MAKE_TAG ('m', 'e', 'd', 'i'), M);
  maybe_add_feature (gsub, script_index, FT_MAKE_TAG ('f', 'i', 'n', 'a'), F);
  maybe_add_feature (gsub, script_index, FT_MAKE_TAG ('l', 'i', 'g', 'a'), L);
}
147
#endif
148

149
#if 0
150
void 
151
dump_string (HB_GSUB_String *str)
152
{
153
  HB_UInt i;
154 155 156 157

  fprintf (stderr, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
  for (i = 0; i < str->length; i++)
    {
158
      fprintf (stderr, "%2lu: %#06x %#06x %4d %4d\n",
159 160 161 162 163 164 165 166 167
	       i,
	       str->string[i],
	       str->properties[i],
	       str->components[i],
	       str->ligIDs[i]);
    }
  fprintf (stderr, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
}

168 169
HB_UShort arabic_str[]   = { 0x645, 0x643, 0x64a, 0x644, 0x639, 0x20, 0x645, 0x627, 0x644, 0x633, 0x644, 0x627 };
HB_UShort arabic_props[] = { I|L,   M|L,   M|L,   M|L,   M|L,   F|L,   I|L,  M|L,   M|L,   M|L,   M|L,   F|L };
170 171 172 173

void
try_string (FT_Library library,
	    FT_Face    face,
174
	    HB_GSUB   gsub)
175
{
176
  HB_Error error;
177 178
  HB_GSUB_String *in_str;
  HB_GSUB_String *out_str;
179
  HB_UInt i;
180

181 182 183 184
  if ((error = HB_GSUB_String_New (face->memory, &in_str)))
    croak ("HB_GSUB_String_New", error);
  if ((error = HB_GSUB_String_New (face->memory, &out_str)))
    croak ("HB_GSUB_String_New", error);
185

186 187
  if ((error = HB_GSUB_String_Set_Length (in_str, N_ELEMENTS (arabic_str))))
    croak ("HB_GSUB_String_Set_Length", error);
188 189 190 191 192 193 194 195 196

  for (i=0; i < N_ELEMENTS (arabic_str); i++)
    {
      in_str->string[i] = FT_Get_Char_Index (face, arabic_str[i]);
      in_str->properties[i] = arabic_props[i];
      in_str->components[i] = i;
      in_str->ligIDs[i] = i;
    }

197 198
  if ((error = HB_GSUB_Apply_String (gsub, in_str, out_str)))
    croak ("HB_GSUB_Apply_String", error);
199 200 201 202

  dump_string (in_str);
  dump_string (out_str);

203 204 205 206
  if ((error = HB_GSUB_String_Done (in_str)))
    croak ("HB_GSUB_String_New", error);
  if ((error = HB_GSUB_String_Done (out_str)))
    croak ("HB_GSUB_String_New", error);
207
}
208
#endif
209 210 211 212

int 
main (int argc, char **argv)
{
213
  HB_Error error;
214 215
  FT_Library library;
  FT_Face face;
216 217
  HB_GSUB gsub;
  HB_GPOS gpos;
218 219 220 221 222 223 224 225 226 227 228 229 230

  if (argc != 2)
    {
      fprintf (stderr, "Usage: ottest MYFONT.TTF\n");
      exit(1);
    }

  if ((error = FT_Init_FreeType (&library)))
    croak ("FT_Init_FreeType", error);

  if ((error = FT_New_Face (library, argv[1], 0, &face)))
    croak ("FT_New_Face", error);

231 232 233
  printf ("<?xml version=\"1.0\"?>\n");
  printf ("<OpenType>\n");

234
  if (!(error = HB_Load_GSUB_Table (face, &gsub, NULL)))
235
    {
236
      HB_Dump_GSUB_Table (gsub, stdout);
237
      
238 239
      if ((error = HB_Done_GSUB_Table (gsub)))
	croak ("HB_Done_GSUB_Table", error);
240
    }
241
  else if (error != HB_Err_Table_Missing)
242
    fprintf (stderr, "HB_Load_GSUB_Table %x\n", error);
243

244
  if (!(error = HB_Load_GPOS_Table (face, &gpos, NULL)))
245
    {
246
      HB_Dump_GPOS_Table (gpos, stdout);
247
      
248 249
      if ((error = HB_Done_GPOS_Table (gpos)))
	croak ("HB_Done_GPOS_Table", error);
250
    }
251
  else if (error != HB_Err_Table_Missing)
252
    fprintf (stderr, "HB_Load_GPOS_Table %x\n", error);
253

254 255
  printf ("</OpenType>\n");

256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
#if 0  
  select_cmap (face);

  add_features (gsub);
  try_string (library, face, gsub);
#endif


  if ((error = FT_Done_Face (face)))
    croak ("FT_Done_Face", error);

  if ((error = FT_Done_FreeType (library)))
    croak ("FT_Done_FreeType", error);
  
  return 0;
}