summaryrefslogtreecommitdiff
path: root/src/otlayout/otdemo.c
blob: ffe1ae196a8ed11db04a53a308036db277b57a04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/***************************************************************************/
/*                                                                         */
/*  otdemo.c                                                               */
/*                                                                         */
/*    Demo program for FreeType/OpenType font driver implementation (body).*/
/*                                                                         */
/*  Copyright 2003 by                                                      */
/*  Masatake YAMATO and Redhat K.K.                                        */
/*                                                                         */
/*  This file may only be used,                                            */
/*  modified, and distributed under the terms of the FreeType project      */
/*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
/*  this file you indicate that you have read the license and              */
/*  understand and accept it fully.                                        */
/*                                                                         */
/***************************************************************************/

/***************************************************************************/
/* Development of the code in this file is support of                      */
/* Information-technology Promotion Agency, Japan.                         */
/***************************************************************************/

#include <ft2build.h>
#include FT_LAYOUT_H
#include FT_OTLAYOUT_H
#include <stdio.h>
#include <stdlib.h>
#include <popt.h>

/* KANA specific implementation */

/* Script */
#define OT_SCRIPT_KANA         FT_MAKE_TAG('k', 'a', 'n', 'a')

/* Language */
#define OT_LANGUAGE_JAPANESE   FT_MAKE_TAG('J', 'A', 'N', ' ')

/* Feature */
#define OT_FEATURE_VERT        FT_MAKE_TAG('v', 'e', 'r', 't')
#define OT_FEATURE_VRT2        FT_MAKE_TAG('v', 'r', 't', '2')

/* Property bit */
enum {
	OT_VERT = 1 << 0,
	OT_VRT2 = 1 << 1
};

/* Property */
enum {
	OT_VERT_P = (~OT_VERT) & (~OT_VRT2)
};

void doit(FT_Face face);

int
main(int argc, char ** argv)
{
  FT_Library library;
  FT_Face    face;
  poptContext optCon;
  int rc;
  const char * file;
  static int arg_memprof = 0;

  struct poptOption optTable [] = {
    { "memprof", 'm',  POPT_ARG_NONE, &arg_memprof, 0, "Enter to infinite loop to run under memprof", ""},
    POPT_AUTOHELP
    POPT_TABLEEND
  };

  FTL_EngineType engine_type;

  optCon = poptGetContext(argv[0], argc, (const char **)argv, optTable, 0);
  poptSetOtherOptionHelp(optCon, "[options] otfont\n");      
  rc = poptReadDefaultConfig (optCon, 0);  
  if (rc < 0)
    {
      fprintf(stderr, "Fail to read .popt config file: %s\n",
	      poptStrerror(rc));
      exit (1);
    }
  while ((rc = poptGetNextOpt(optCon)) > 0) 
    if (rc != -1)
      {
	fprintf(stderr, "bad argument %s: %s\n",
		poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
		poptStrerror(rc));
	exit (1);
      }

  if (FT_Init_FreeType (&library))
    {
      fprintf(stderr, "Error in %s\n", "FT_Init_FreeType");
      exit (1);
    }

  file = poptGetArg(optCon);
  if (!file)
    {
      poptPrintHelp(optCon, stderr, 0);
      exit(1);
    }

  if ( FT_New_Face (library, file, 0, &face) )
    {
      fprintf(stderr, "Error in %s: %s\n", "FT_New_Face", file);
      exit (1);
    }


  if ( !( face->face_flags & FT_FACE_FLAG_GLYPH_SUBSTITUTION) )
    {
      fprintf(stderr, "Abort: No substitution table for the face\n");
      exit(1);
    }
  
  if ( FTL_Query_EngineType( face, &engine_type )
       ||  ( engine_type != FTL_OPENTYPE_ENGINE ) )
    {
      fprintf(stderr, "No OT table is existed: %s\n", file);
      exit ( 1 );
    }

  doit( face );
  
  if ( FT_Done_Face ( face ) )
    fprintf(stderr, "Error in %s: %s\n", "FT_Done_Face", file);
  if ( FT_Done_FreeType  (library) )
    {
      fprintf(stderr, "Error in %s\n", "FT_Done_FreeType");
      exit(1);
    }
  if ( arg_memprof || getenv("_MEMPROF_SOCKET") )
    {
      fprintf(stderr, "Enter infinite loop for memprof\n");
      while (1);
    }
  return 0;
} 

void
doit(FT_Face face)
{
  FTL_FeaturesRequest request;
  FT_UInt script_index, feature_index;
  FT_Memory memory = face->stream->memory;
  FTL_Glyphs_Array in, out;
  FT_Int i;
  if ( FTL_New_FeaturesRequest( face, &request ) )
    {
      fprintf(stderr, "error: %s\n", "FTL_New_FeaturesRequest");
      return ;
    }
  if ( !OTL_FeaturesRequest_Find_Script ( (OTL_FeaturesRequest)request,
					  OT_TABLE_GSUB,
					  OT_SCRIPT_KANA,
					  &script_index ) )
    {
      fprintf(stderr, "cannot find script: %s\n", 
	      "OTL_FeaturesRequest_Find_Script");      
      return ;
    }

  if ( !OTL_FeaturesRequest_Find_Feature( (OTL_FeaturesRequest)request,
					  OT_TABLE_GSUB,
					  OT_FEATURE_VERT,
					  script_index,
					  0xffff, /* default language system */
					  &feature_index ) )
    {
      fprintf(stderr, "cannot find feature: %s\n", 
	      "OTL_FeaturesRequest_Find_Feature");
      return ;
    }
  OTL_FeaturesRequest_Add_Feature ( (OTL_FeaturesRequest)request,
				    OT_TABLE_GSUB,
				    feature_index,
				    OT_VERT );
  
  if ( FTL_New_Glyphs_Array ( memory, &in )
       || FTL_New_Glyphs_Array ( memory, &out ) )
    {
      fprintf(stderr, "mem error: %s\n", "FTL_New_Glyphs_Array");
      return ;
    }
  if ( FTL_Set_Glyphs_Array_Length ( in, 14751 ) )
    {
      fprintf(stderr, "mem error: %s\n", 
	      "FTL_Set_Glyphs_Array_Length");
      return ;
    }

  for ( i = 0; i < 14751; i++ )
    {
      in->glyphs[i].gid     = i;
      in->glyphs[i].ot_prop = OT_VERT_P;
    }
  FTL_Activate_FeaturesRequest( request );
  FTL_Substitute_Glyphs ( face, in, out );

  for ( i = 0; i < 14751; i++ )
    {
      if ( out->glyphs[i].gid != i )
	fprintf( stdout, "%d => %u\n", i, out->glyphs[i].gid );
    }
  FTL_Done_Glyphs_Array ( out );
  FTL_Done_Glyphs_Array ( in );
  FTL_Done_FeaturesRequest ( request );
}

/* END */