~ubuntu-branches/debian/squeeze/camlimages/squeeze

« back to all changes in this revision

Viewing changes to freetype2/ftintf.c

  • Committer: Bazaar Package Importer
  • Author(s): Sylvain Le Gall, Ralf Treinen, Sylvain Le Gall
  • Date: 2009-03-05 00:19:32 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20090305001932-f0hstlmun8hxvs0r
Tags: 1:3.0.1-1
[ Ralf Treinen ]
* Updated debian/watch.

[ Sylvain Le Gall ]
* New upstream version:
  * Remove useless patches
  * Adapt debian/rules and other debhelper files
  * Add debian/patches/fix_3_0_1 to fix various problem (probably due to
    OCaml 3.11 migration)
* Depends on version 2.12 of lablgtk2
* Add dh-ocaml build-dependency (rules/ocaml.mk)
* Add ${misc:Depends} to dependencies
* Update Homepage field into debian/control and debian/copyright
* Add license version for debian packaging
* Directly use eng.html rather than creating a linked index.html file

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***********************************************************************/
2
 
/*                                                                     */
3
 
/*                           Objective Caml                            */
4
 
/*                                                                     */
5
 
/*            Jun Furuse, projet Cristal, INRIA Rocquencourt           */
6
 
/*                                                                     */
7
 
/*  Copyright 1999,2000                                                */
8
 
/*  Institut National de Recherche en Informatique et en Automatique.  */
9
 
/*  Distributed only by permission.                                    */
10
 
/*                                                                     */
11
 
/***********************************************************************/
12
 
#include <config.h>
13
 
#include <string.h>
14
 
 
15
 
#include <caml/mlvalues.h>
16
 
#include <caml/alloc.h>
17
 
#include <caml/memory.h>
18
 
#include <caml/callback.h>
19
 
#include <caml/fail.h>
20
 
 
21
 
#if (HAVE_FREETYPE && HAVE_FREETYPE2)
22
 
 
23
 
#include <ft2build.h>
24
 
#include FT_FREETYPE_H
25
 
 
26
 
value init_FreeType()
27
 
{
28
 
  CAMLparam0();
29
 
  FT_Library *library;
30
 
 
31
 
  if( (library = stat_alloc( sizeof(FT_Library) )) == NULL ){
32
 
    failwith( "init_FreeType: Memory over" );
33
 
  }
34
 
  if( FT_Init_FreeType( library ) ){
35
 
    failwith( "FT_Init_FreeType" );
36
 
  }
37
 
  CAMLreturn( (value) library );
38
 
}
39
 
 
40
 
value done_FreeType( library )
41
 
     value library;
42
 
{
43
 
  CAMLparam1(library);
44
 
  if ( FT_Done_FreeType( *(FT_Library *)library ) ){
45
 
    failwith( "FT_Done_FreeType" );
46
 
  }
47
 
  stat_free( (void *) library );
48
 
  CAMLreturn(Val_unit);
49
 
}
50
 
 
51
 
#include <stdio.h>
52
 
value new_Face( library, fontpath, idx )
53
 
     value library;
54
 
     value fontpath;
55
 
     value idx;
56
 
{
57
 
  CAMLparam3(library, fontpath, idx );
58
 
  FT_Face *face;
59
 
  
60
 
  if( (face = stat_alloc( sizeof(FT_Face) )) == NULL ){
61
 
    failwith( "new_Face: Memory over" );
62
 
  }
63
 
  if( FT_New_Face( *(FT_Library *)library, String_val( fontpath ), Int_val( idx ), face ) ){
64
 
    failwith( "new_Face: Could not open face" );
65
 
  }
66
 
  CAMLreturn( (value) face );
67
 
}
68
 
 
69
 
value face_info( facev )
70
 
     value facev;
71
 
{
72
 
  CAMLparam1(facev);
73
 
  CAMLlocal1(res);
74
 
 
75
 
  FT_Face face = *(FT_Face *)facev;
76
 
  res = alloc_tuple(14);
77
 
  Store_field(res, 0, Val_int( face->num_faces ));
78
 
  Store_field(res, 1, Val_int( face->num_glyphs ));
79
 
  Store_field(res, 2, copy_string( face->family_name == NULL ? "" : face->family_name ));
80
 
  Store_field(res, 3, copy_string( face->style_name == NULL ? "" : face->style_name ));
81
 
  Store_field(res, 4, Val_bool( FT_HAS_HORIZONTAL( face ) ));
82
 
  Store_field(res, 5, Val_bool( FT_HAS_VERTICAL( face ) ));
83
 
  Store_field(res, 6, Val_bool( FT_HAS_KERNING( face ) ));
84
 
  Store_field(res, 7, Val_bool( FT_IS_SCALABLE( face ) ));
85
 
  Store_field(res, 8, Val_bool( FT_IS_SFNT( face ) ));
86
 
  Store_field(res, 9, Val_bool( FT_IS_FIXED_WIDTH( face ) ));
87
 
  Store_field(res,10, Val_bool( FT_HAS_FIXED_SIZES( face ) ));
88
 
  Store_field(res,11, Val_bool( FT_HAS_FAST_GLYPHS( face ) ));
89
 
  Store_field(res,12, Val_bool( FT_HAS_GLYPH_NAMES( face ) ));
90
 
  Store_field(res,13, Val_bool( FT_HAS_MULTIPLE_MASTERS( face ) ));
91
 
  
92
 
  CAMLreturn(res);
93
 
}
94
 
 
95
 
value done_Face( face )
96
 
     value face;
97
 
{
98
 
  CAMLparam1(face);
99
 
  if ( FT_Done_Face( *(FT_Face *) face ) ){
100
 
    failwith("FT_Done_Face");
101
 
  }
102
 
  CAMLreturn( Val_unit );
103
 
}
104
 
 
105
 
value get_num_glyphs( face )
106
 
     value face;
107
 
{
108
 
  CAMLparam1(face);
109
 
  CAMLreturn( Val_int ((*(FT_Face *) face)->num_glyphs) );
110
 
}
111
 
 
112
 
 
113
 
value set_Char_Size( face, char_w, char_h, res_h, res_v )
114
 
     value face;
115
 
     value char_w, char_h; /* 26.6 1 = 1/64pt */
116
 
     value res_h, res_v; /* dpi */
117
 
{
118
 
  CAMLparam5( face, char_w, char_h, res_h, res_v );
119
 
  if ( FT_Set_Char_Size( *(FT_Face *) face,
120
 
                    Int_val(char_w), Int_val(char_h),
121
 
                         Int_val(res_h), Int_val(res_v) ) ){
122
 
    failwith("FT_Set_Char_Size");
123
 
  }
124
 
  CAMLreturn(Val_unit);
125
 
}
126
 
 
127
 
/* to be done: query at face->fixed_sizes
128
 
 */
129
 
 
130
 
value set_Pixel_Sizes( face, pixel_w, pixel_h )
131
 
     value face;
132
 
     value pixel_w, pixel_h; /* dot */
133
 
{
134
 
  CAMLparam3(face,pixel_w,pixel_h);
135
 
  if ( FT_Set_Pixel_Sizes( *(FT_Face *) face,
136
 
                         Int_val(pixel_w), Int_val(pixel_h) ) ){
137
 
    failwith("FT_Set_Pixel_Sizes");
138
 
  }
139
 
  CAMLreturn(Val_unit);
140
 
}
141
 
 
142
 
value val_CharMap( charmapp )
143
 
     FT_CharMap *charmapp;
144
 
{
145
 
  CAMLparam0();
146
 
  CAMLlocal1(res);
147
 
  
148
 
  res = alloc_tuple(2);
149
 
  Store_field(res,0, Val_int((*charmapp)->platform_id));
150
 
  Store_field(res,1, Val_int((*charmapp)->encoding_id));
151
 
 
152
 
  CAMLreturn(res);
153
 
}
154
 
 
155
 
value get_CharMaps( facev )
156
 
     value facev;
157
 
{
158
 
  CAMLparam1(facev);
159
 
  CAMLlocal3(list,last_cell,new_cell);
160
 
  int i = 0;
161
 
  FT_Face face;
162
 
 
163
 
  face = *(FT_Face *) facev;
164
 
 
165
 
  list = last_cell = Val_unit;
166
 
  
167
 
  while( i < face->num_charmaps ){
168
 
    new_cell = alloc_tuple(2);
169
 
    Store_field(new_cell,0, val_CharMap( face->charmaps + i ));
170
 
    Store_field(new_cell,1, Val_unit);
171
 
    if( i == 0 ){
172
 
      list = new_cell;
173
 
    } else {
174
 
      Store_field(last_cell,1, new_cell);
175
 
    }
176
 
    last_cell = new_cell;
177
 
    i++;
178
 
  }
179
 
 
180
 
  CAMLreturn(list);
181
 
}
182
 
 
183
 
value set_CharMap( facev, charmapv )
184
 
     value facev;
185
 
     value charmapv;
186
 
{
187
 
  CAMLparam2(facev,charmapv);
188
 
  int i = 0;
189
 
  FT_Face face;
190
 
  FT_CharMap charmap;
191
 
  int my_pid, my_eid;
192
 
  
193
 
  face = *(FT_Face *) facev;
194
 
  my_pid = Int_val(Field(charmapv, 0));
195
 
  my_eid = Int_val(Field(charmapv, 1));
196
 
 
197
 
  while( i < face->num_charmaps ){
198
 
    charmap = face->charmaps[i];
199
 
    if ( charmap->platform_id == my_pid && 
200
 
         charmap->encoding_id == my_eid ){
201
 
      if ( FT_Set_Charmap( face, charmap ) ){
202
 
        failwith("FT_Set_Charmap");
203
 
      }
204
 
      CAMLreturn(Val_unit);
205
 
    } else {
206
 
      i++;
207
 
    }
208
 
  }
209
 
  failwith("freetype:set_charmaps: selected pid+eid do not exist");
210
 
}
211
 
 
212
 
value get_Char_Index( face, code )
213
 
     value face, code;
214
 
{
215
 
  CAMLparam2(face,code);
216
 
  CAMLreturn(Val_int(FT_Get_Char_Index( *(FT_Face *)face, Int_val(code))));
217
 
}
218
 
 
219
 
value load_Glyph( face, index, flags )
220
 
     value face, index, flags;
221
 
{
222
 
  CAMLparam3(face,index,flags);
223
 
  CAMLlocal1(res);
224
 
 
225
 
  if( FT_Load_Glyph( *(FT_Face *) face, Int_val(index), FT_LOAD_DEFAULT | Int_val(flags)) ){
226
 
    failwith("FT_Load_Glyph");
227
 
  }
228
 
 
229
 
  res = alloc_tuple(2);
230
 
  Store_field(res,0, Val_int( (*(FT_Face*)face)->glyph->advance.x ));
231
 
  Store_field(res,1, Val_int( (*(FT_Face*)face)->glyph->advance.y ));
232
 
 
233
 
  CAMLreturn(res);
234
 
}
235
 
 
236
 
value load_Char( face, code, flags )
237
 
     value face, code, flags;
238
 
{
239
 
  CAMLparam3(face,code,flags);
240
 
  CAMLlocal1(res);
241
 
 
242
 
  /* FT_Load_Glyph(face, FT_Get_Char_Index( face, code )) */
243
 
  if( FT_Load_Char( *(FT_Face *) face, Int_val(code), FT_LOAD_DEFAULT | Int_val(flags)) ){
244
 
    failwith("FT_Load_Char");
245
 
  }
246
 
 
247
 
  res = alloc_tuple(2);
248
 
  Store_field(res,0, Val_int( (*(FT_Face*)face)->glyph->advance.x ));
249
 
  Store_field(res,1, Val_int( (*(FT_Face*)face)->glyph->advance.y ));
250
 
 
251
 
  CAMLreturn(res);
252
 
}
253
 
 
254
 
value render_Glyph_of_Face( face, mode )
255
 
     value face;
256
 
     value mode;
257
 
{
258
 
  CAMLparam2(face,mode);
259
 
  if (FT_Render_Glyph( (*(FT_Face *)face)->glyph , Int_val(mode) )){
260
 
    failwith("FT_Render_Glyph");
261
 
  }
262
 
  CAMLreturn(Val_unit);
263
 
}
264
 
 
265
 
value render_Char( face, code, flags, mode )
266
 
     value face, code, flags, mode;
267
 
{
268
 
  CAMLparam4(face,code,flags,mode);
269
 
  CAMLlocal1(res);
270
 
 
271
 
  /* FT_Load_Glyph(face, FT_Get_Char_Index( face, code ), FT_LOAD_RENDER) */
272
 
  if( FT_Load_Char( *(FT_Face *) face, Int_val(code), 
273
 
                    FT_LOAD_RENDER | 
274
 
                    Int_val(flags) | 
275
 
                    (Int_val(mode) ? FT_LOAD_MONOCHROME : 0)) ){
276
 
    failwith("FT_Load_Char");
277
 
  }
278
 
 
279
 
  res = alloc_tuple(2);
280
 
  Store_field(res,0, Val_int( (*(FT_Face*)face)->glyph->advance.x ));
281
 
  Store_field(res,1, Val_int( (*(FT_Face*)face)->glyph->advance.y ));
282
 
 
283
 
  CAMLreturn(res);
284
 
}
285
 
 
286
 
value set_Transform( face, vmatrix, vpen ) 
287
 
     value face, vmatrix, vpen;
288
 
{
289
 
  CAMLparam3(face, vmatrix, vpen);
290
 
  FT_Matrix matrix;
291
 
  FT_Vector pen;
292
 
 
293
 
  matrix.xx = (FT_Fixed)( Int_val(Field(vmatrix,0)) );
294
 
  matrix.xy = (FT_Fixed)( Int_val(Field(vmatrix,1)) );
295
 
  matrix.yx = (FT_Fixed)( Int_val(Field(vmatrix,2)) );
296
 
  matrix.yy = (FT_Fixed)( Int_val(Field(vmatrix,3)) );
297
 
  pen.x = (FT_Fixed)( Int_val(Field(vpen,0)) );
298
 
  pen.y = (FT_Fixed)( Int_val(Field(vpen,1)) );
299
 
 
300
 
  FT_Set_Transform( *(FT_Face *)face, &matrix, &pen );
301
 
  
302
 
  CAMLreturn(Val_unit);
303
 
}
304
 
 
305
 
value get_Bitmap_Info( vface )
306
 
     value vface;
307
 
{
308
 
  CAMLparam1(vface);
309
 
  CAMLlocal1(res);
310
 
 
311
 
  FT_GlyphSlot glyph = (*(FT_Face *)vface)->glyph;
312
 
  FT_Bitmap bitmap = glyph->bitmap;
313
 
 
314
 
  switch ( bitmap.pixel_mode ) {
315
 
  case ft_pixel_mode_grays:
316
 
    if ( bitmap.num_grays != 256 ){
317
 
      failwith("get_Bitmap_Info: unknown num_grays");
318
 
    }
319
 
    break;
320
 
  case ft_pixel_mode_mono:
321
 
    break;
322
 
  default:
323
 
    failwith("get_Bitmap_Info: unknown pixel mode");
324
 
  }
325
 
 
326
 
  res = alloc_tuple(5);
327
 
  Store_field(res,0, Val_int(glyph->bitmap_left));
328
 
  Store_field(res,1, Val_int(glyph->bitmap_top));
329
 
  Store_field(res,2, Val_int(bitmap.width));
330
 
  Store_field(res,3, Val_int(bitmap.rows));
331
 
 
332
 
  CAMLreturn(res);
333
 
}
334
 
 
335
 
value read_Bitmap( vface, vx, vy ) /* This "y" is in Y upwards convention */
336
 
     value vface, vx, vy;
337
 
{
338
 
  /* no boundary check !!! */
339
 
  int x, y;
340
 
  CAMLparam3(vface, vx, vy);
341
 
 
342
 
  FT_Bitmap bitmap = (*(FT_Face *)vface)->glyph->bitmap;
343
 
 
344
 
  unsigned char *row;
345
 
 
346
 
  x = Int_val(vx);
347
 
  y = Int_val(vy);
348
 
  
349
 
  switch ( bitmap.pixel_mode ) {
350
 
  case ft_pixel_mode_grays:
351
 
    if (bitmap.pitch > 0){
352
 
      row = bitmap.buffer + (bitmap.rows - 1 - y) * bitmap.pitch;
353
 
    } else {
354
 
      row = bitmap.buffer - y * bitmap.pitch;
355
 
    }
356
 
    CAMLreturn (Val_int(row[x]));
357
 
 
358
 
  case ft_pixel_mode_mono:
359
 
    if (bitmap.pitch > 0){
360
 
      row = bitmap.buffer + (bitmap.rows - 1 - y) * bitmap.pitch;
361
 
    } else {
362
 
      row = bitmap.buffer - y * bitmap.pitch;
363
 
    }
364
 
    CAMLreturn (Val_int(row[x >> 3] & (128 >> (x & 7)) ? 255 : 0));
365
 
    break;
366
 
 
367
 
  default:
368
 
    failwith("read_Bitmap: unknown pixel mode");
369
 
  }
370
 
 
371
 
}
372
 
 
373
 
value get_Glyph_Metrics( face )
374
 
     value face;
375
 
{
376
 
  CAMLparam1(face);
377
 
  CAMLlocal3(res1,res2,res);
378
 
  
379
 
  /* no soundness check ! */
380
 
  FT_Glyph_Metrics *metrics = &((*(FT_Face *)face)->glyph->metrics);
381
 
 
382
 
  res1 = alloc_tuple(3);  
383
 
  Store_field(res1,0, Val_int(metrics->horiBearingX));
384
 
  Store_field(res1,1, Val_int(metrics->horiBearingY));
385
 
  Store_field(res1,2, Val_int(metrics->horiAdvance));
386
 
                          
387
 
  res2 = alloc_tuple(3);  
388
 
  Store_field(res2,0, Val_int(metrics->vertBearingX));
389
 
  Store_field(res2,1, Val_int(metrics->vertBearingY));
390
 
  Store_field(res2,2, Val_int(metrics->vertAdvance));
391
 
 
392
 
  res = alloc_tuple(4);
393
 
  Store_field(res,0, Val_int(metrics->width));
394
 
  Store_field(res,1, Val_int(metrics->height));
395
 
  Store_field(res,2, res1);
396
 
  Store_field(res,3, res2);
397
 
 
398
 
  CAMLreturn(res);
399
 
}
400
 
 
401
 
value get_Size_Metrics( face )
402
 
     value face;
403
 
{
404
 
  CAMLparam1(face);
405
 
  CAMLlocal1(res);
406
 
 
407
 
  FT_Size_Metrics *imetrics = &((*(FT_Face*)face)->size->metrics);
408
 
 
409
 
  res = alloc_tuple(4);
410
 
  Store_field(res,0, Val_int(imetrics->x_ppem));
411
 
  Store_field(res,1, Val_int(imetrics->y_ppem));
412
 
  Store_field(res,2, Val_int(imetrics->x_scale));
413
 
  Store_field(res,3, Val_int(imetrics->y_scale));
414
 
 
415
 
  CAMLreturn(res);
416
 
}
417
 
 
418
 
value get_Outline_Contents(value face) {
419
 
/* *****************************************************************
420
 
    
421
 
   Concrete definitions of TT_Outline might vary from version to
422
 
   version.
423
 
 
424
 
   This definition assumes freetype 2.0.1
425
 
 
426
 
     ( anyway, this function is wrong...)
427
 
     
428
 
 ***************************************************************** */
429
 
  CAMLparam1(face);
430
 
  CAMLlocal5(points, tags, contours, res, tmp);
431
 
  int i;
432
 
 
433
 
  FT_Outline* outline = &((*(FT_Face *)face)->glyph->outline);
434
 
 
435
 
  int n_contours = outline->n_contours;
436
 
  int n_points   = outline->n_points;
437
 
 
438
 
  points   = alloc_tuple(n_points);
439
 
  tags     = alloc_tuple(n_points);
440
 
  contours = alloc_tuple(n_contours);
441
 
 
442
 
  for( i=0; i<n_points; i++ ) {
443
 
    FT_Vector* raw_points = outline->points;
444
 
    char* raw_flags  = outline->tags;
445
 
    tmp = alloc_tuple(2);
446
 
    /* caution: 26.6 fixed into 31 bit */
447
 
    Store_field(tmp, 0, Val_int(raw_points[i].x)); 
448
 
    Store_field(tmp, 1, Val_int(raw_points[i].y)); 
449
 
    Store_field(points, i, tmp);
450
 
    if ( raw_flags[i] & FT_Curve_Tag_On ) {  
451
 
      Store_field(tags, i, Val_int(0)); /* On point */
452
 
    } else if ( raw_flags[i] & FT_Curve_Tag_Cubic ) {  
453
 
      Store_field(tags, i, Val_int(2)); /* Off point, cubic */
454
 
    } else {
455
 
      Store_field(tags, i, Val_int(1)); /* Off point, conic */ 
456
 
    }
457
 
  }
458
 
 
459
 
  for( i=0; i<n_contours; i++ ) {
460
 
    short* raw_contours = outline->contours;
461
 
    Store_field(contours, i, Val_int(raw_contours[i]));
462
 
  }
463
 
  
464
 
  res = alloc_tuple(5);
465
 
  Store_field(res, 0, Val_int(n_contours));
466
 
  Store_field(res, 1, Val_int(n_points));
467
 
  Store_field(res, 2, points);
468
 
  Store_field(res, 3, tags);
469
 
  Store_field(res, 4, contours);
470
 
  
471
 
  CAMLreturn(res);
472
 
}
473
 
 
474
 
#endif