summaryrefslogtreecommitdiff
path: root/src/gxlayout/fi.c
blob: a3f8c3a1f1c82a207985fb60158d593884f3b0d1 (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
/***************************************************************************/
/*                                                                         */
/*  fi.c                                                                   */
/*                                                                         */
/*    2 characters ligature test program for AAT/TrueTypeGX font driver.   */
/*                                                                         */
/*  Copyright 2004 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.                         */
/***************************************************************************/

/* "fi" is the typical alphabet ligature.      *
 * "fl" is another good candidate to be tried. */

const char * target = "fi";

#include <ft2build.h>
#include FT_LAYOUT_H
#include FT_GXLAYOUT_H
#include FT_INTERNAL_OBJECTS_H	/* for FT_FACE_MEMORY */

#include <stdio.h>

FT_Error try_lig(FT_Face face, char a, char b);

int
main(int argc, char ** argv)
{
  FT_Error error;
  int result = 0;
  FT_Library library;
  FT_Face face;
  
  if ( argc != 2 )
    {
      fprintf(stderr, "Usage: %s fontfile\n", argv[0]);
      return 1;
    }
  
  error = FT_Init_FreeType ( &library );
  if ( error )
    {
      fprintf(stderr, "Abort: Fail to initialize FT2\n");
      result = 1;
      goto Fail;
    }
  
  error = FT_New_Face (library, argv[1], 0, &face);  
  if ( error )
    {
      fprintf(stderr, "Abort: Fail to open the file\n");
      result = 1;
      goto Fail;
    }
  if ( !( face->face_flags & FT_FACE_FLAG_GLYPH_SUBSTITUTION) )
    {
      fprintf(stderr, "Abort: No substitution table for the face\n");
      result = 1;
      goto Fail;
    }
  
  fprintf(stdout, "-------------------\n");
  fprintf(stdout,
	  "File: %s\nName: %s\n", 
	  argv[1], 
	  ((FT_Face)face)->family_name);
  
  error = try_lig( face, target[0], target[1] );
  if ( error )
    {
      fprintf(stderr, "Abort: Fail to substitute\n");
      result = 1;
    }
  
  FT_Done_Face ( face );

 Fail:
  FT_Done_FreeType  (library);
  return result;
} 

FT_Error 
try_lig( FT_Face face, char c1, char c2 )
{
  FT_Error error = FT_Err_Ok;
  FTL_EngineType engine_type;
  FTL_GlyphArray in, out;
  FTL_FeaturesRequest request;

  /* Get the engine type */
  if (( error = FTL_Query_EngineType( face, &engine_type ) ))
    goto Fail;
  
  /* Ignore if the engine type is not GX.  */
  if ( engine_type != FTL_TRUETYPEGX_ENGINE )
    {
      fprintf(stderr, "Abort: Not GX font.\n");
      goto Fail;
    }

  /* Allocate input and output glyphs arrays. 
     The lenght for input has already been known: 2 */
  FTL_New_Glyphs_Array ( FT_FACE_MEMORY(face), &in );
  FTL_New_Glyphs_Array ( FT_FACE_MEMORY(face), &out );
  FTL_Set_Glyphs_Array_Length ( in, 2 );

  /* Get glyph id for c1 and c2 */
  in->glyphs[0].gid = FT_Get_Char_Index( face, c1 );
  in->glyphs[1].gid = FT_Get_Char_Index( face, c2 );
  fprintf(stdout, "[IN]%c: %u, %c: %u\n", 
	  c1, in->glyphs[0].gid,
	  c2, in->glyphs[1].gid);
  
  /* Create a features-request. */
  FTL_New_FeaturesRequest ( face, &request );
  
  /* -------------------------------------
   * YOU CAN SET SOME LAYOUT SETTINGS HERE 
   * -------------------------------------
   * In this program, just use default.
   * -------------------------------------
   */ 
  
  /* Activeate the features request */
  FTL_Activate_FeaturesRequest( request );

  /* Do substitute the glyphs */
  FTL_Substitute_Glyphs( face, in, out );

  /* Free the features-request */
  FTL_Done_FeaturesRequest ( request );
  
  fprintf(stdout, 
	  "[OUT]%c: %u, %c: %u%s\n", 
	  c1, out->glyphs[0].gid,
	  c2, out->glyphs[1].gid, 
	  (out->glyphs[1].gid == 0xFFFF)? "<empty>": "");

  /* Free glyphs arrays */
  FTL_Done_Glyphs_Array ( in );
  FTL_Done_Glyphs_Array ( out );    

 Fail:
  return error;
}


/* END */