Commit 8b9739ee5a0241f4828fb3a44ba9bb98bdf95ec2

sammy 2008-04-11T16:24:56

* C bindings for FTGL, written by Éric Beets <ericbeets@free.fr>.

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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
diff --git a/include/FTFont.h b/include/FTFont.h
index bce8de6..be73d02 100644
--- a/include/FTFont.h
+++ b/include/FTFont.h
@@ -2,8 +2,11 @@
 #define    __FTFont__
 
 #include <ft2build.h>
+
 #include FT_FREETYPE_H
 
+#ifdef __cplusplus
+
 #include "FTFace.h"
 #include "FTGL.h"
 
@@ -271,6 +274,44 @@ class FTGL_EXPORT FTFont
         
 };
 
+#endif //__cplusplus
+
+#ifdef __cplusplus
+extern "C" {
+namespace C {
+#endif
+
+typedef struct FTGLfont FTGLfont;
+
+FTGL_EXPORT int ftglAttachFile (FTGLfont*, const char*);
+FTGL_EXPORT int ftglAttachData (FTGLfont*, const unsigned char *, size_t);
+
+FTGL_EXPORT int          ftglCharMap      (FTGLfont*, FT_Encoding);
+FTGL_EXPORT unsigned int ftglCharMapCount (FTGLfont*);
+FTGL_EXPORT FT_Encoding* CharMapList      (FTGLfont*);
+
+FTGL_EXPORT int          ftglSetFaceSize    (FTGLfont*, unsigned int );
+FTGL_EXPORT int          ftglSetFaceSizeRes (FTGLfont*,
+                                             unsigned int, unsigned int);
+FTGL_EXPORT unsigned int ftglGetFaceSize    (FTGLfont*);
+FTGL_EXPORT void         ftglSetDepth       (FTGLfont*, float);
+
+FTGL_EXPORT void ftglUseDisplayList (FTGLfont*, int);
+
+FTGL_EXPORT float ftglAscender   (FTGLfont*);
+FTGL_EXPORT float ftglDescender  (FTGLfont*);
+FTGL_EXPORT float ftglLineHeight (FTGLfont*);
+FTGL_EXPORT void  ftglBBox       (FTGLfont*, const char *, float[]);
+FTGL_EXPORT float ftglAdvance    (FTGLfont*, const char *);
+FTGL_EXPORT void  ftglRender     (FTGLfont*, const char *);
+
+FTGL_EXPORT FT_Error ftglError(FTGLfont*);
+
+#ifdef __cplusplus
+}
+}
+#endif
 
 #endif  //  __FTFont__
 
+
diff --git a/include/FTGLBitmapFont.h b/include/FTGLBitmapFont.h
index 3e6aa32..3f98b71 100644
--- a/include/FTGLBitmapFont.h
+++ b/include/FTGLBitmapFont.h
@@ -1,6 +1,8 @@
 #ifndef __FTGLBitmapFont__
 #define __FTGLBitmapFont__
 
+#ifdef __cplusplus
+
 #include "FTFont.h"
 #include "FTGL.h"
 
@@ -62,4 +64,17 @@ class FTGL_EXPORT FTGLBitmapFont : public FTFont
         inline virtual FTGlyph* MakeGlyph( unsigned int g);
                 
 };
+#endif //__cplusplus
+
+#ifdef __cplusplus
+extern "C" {
+namespace C {
+#endif
+FTGL_EXPORT FTGLfont *ftglBitmapFontMake(const char *fontname);
+#ifdef __cplusplus
+}
+}
+#endif
+
 #endif  //  __FTGLBitmapFont__
+
diff --git a/include/FTGLExtrdFont.h b/include/FTGLExtrdFont.h
index d234461..6804aac 100644
--- a/include/FTGLExtrdFont.h
+++ b/include/FTGLExtrdFont.h
@@ -1,9 +1,12 @@
 #ifndef __FTGLExtrdFont__
 #define __FTGLExtrdFont__
 
+#ifdef __cplusplus
+
 #include "FTFont.h"
 #include "FTGL.h"
 
+
 class FTGlyph;
 
 /**
@@ -58,6 +61,17 @@ class FTGL_EXPORT FTGLExtrdFont : public FTFont
         float depth;
 };
 
+#endif //__cplusplus
+
+#ifdef __cplusplus
+extern "C" {
+namespace C {
+#endif
+FTGL_EXPORT FTGLfont *ftglExtrdFontMake(const char *fontname);
+#ifdef __cplusplus
+}
+}
+#endif
 
 #endif	//	__FTGLExtrdFont__
 
diff --git a/include/FTGLOutlineFont.h b/include/FTGLOutlineFont.h
index fce7cf5..da6a7fd 100644
--- a/include/FTGLOutlineFont.h
+++ b/include/FTGLOutlineFont.h
@@ -1,6 +1,7 @@
 #ifndef     __FTGLOutlineFont__
 #define     __FTGLOutlineFont__
 
+#ifdef __cplusplus
 
 #include "FTFont.h"
 #include "FTGL.h"
@@ -61,4 +62,19 @@ class FTGL_EXPORT FTGLOutlineFont : public FTFont
         inline virtual FTGlyph* MakeGlyph( unsigned int g);
         
 };
+
+#endif //__cplusplus
+
+#ifdef __cplusplus
+namespace C {
+extern "C" {
+#endif
+
+FTGL_EXPORT FTGLfont *ftglOutlineFontMake(const char *fontname);
+
+#ifdef __cplusplus
+}
+}
+#endif
+
 #endif // __FTGLOutlineFont__
diff --git a/include/FTGLPixmapFont.h b/include/FTGLPixmapFont.h
index 053427b..f2e9b6e 100644
--- a/include/FTGLPixmapFont.h
+++ b/include/FTGLPixmapFont.h
@@ -1,6 +1,7 @@
 #ifndef     __FTGLPixmapFont__
 #define     __FTGLPixmapFont__
 
+#ifdef __cplusplus
 
 #include "FTFont.h"
 #include "FTGL.h"
@@ -63,6 +64,17 @@ class FTGL_EXPORT FTGLPixmapFont : public FTFont
         
 };
 
+#endif //__cplusplus
+
+#ifdef __cplusplus
+extern "C" {
+namespace C {
+#endif
+FTGL_EXPORT FTGLfont *ftglPixmapFontMake(const char *fontname);
+#ifdef __cplusplus
+}
+}
+#endif
 
 #endif  //  __FTGLPixmapFont__
 
diff --git a/include/FTGLPolygonFont.h b/include/FTGLPolygonFont.h
index 3a07de1..b6729ce 100644
--- a/include/FTGLPolygonFont.h
+++ b/include/FTGLPolygonFont.h
@@ -1,6 +1,7 @@
 #ifndef     __FTGLPolygonFont__
 #define     __FTGLPolygonFont__
 
+#ifdef __cplusplus
 
 #include "FTFont.h"
 #include "FTGL.h"
@@ -48,6 +49,17 @@ class FTGL_EXPORT FTGLPolygonFont : public FTFont
         
 };
 
+#endif //__cplusplus
+
+#ifdef __cplusplus
+extern "C" {
+namespace C {
+#endif
+FTGL_EXPORT FTGLfont *ftglPolygonFontMake(const char *fontname);
+#ifdef __cplusplus
+}
+}
+#endif
 
 #endif  //  __FTGLPolygonFont__
 
diff --git a/include/FTGLTextureFont.h b/include/FTGLTextureFont.h
index 7e97d6c..d5820aa 100644
--- a/include/FTGLTextureFont.h
+++ b/include/FTGLTextureFont.h
@@ -1,6 +1,8 @@
 #ifndef     __FTGLTextureFont__
 #define     __FTGLTextureFont__
 
+#ifdef __cplusplus
+
 #include "FTFont.h"
 #include "FTVector.h"
 #include "FTGL.h"
@@ -145,7 +147,19 @@ class  FTGL_EXPORT FTGLTextureFont : public FTFont
 
 };
 
+#endif //__cplusplus
+
+#ifdef __cplusplus
+extern "C" {
+namespace C {
+#endif
+FTGL_EXPORT FTGLfont *ftglTextureFontMake(const char *fontname);
+#ifdef __cplusplus
+}
+}
+#endif
 
 #endif // __FTGLTextureFont__
 
 
+
diff --git a/include/FTInternals.h b/include/FTInternals.h
new file mode 100644
index 0000000..6d55b36
--- /dev/null
+++ b/include/FTInternals.h
@@ -0,0 +1,69 @@
+#include    "FTFont.h"
+#include    "FTGLBitmapFont.h"
+#include    "FTGLPixmapFont.h"
+#include    "FTGLOutlineFont.h"
+#include    "FTGLPolygonFont.h"
+#include    "FTGLExtrdFont.h"
+#include    "FTGLTextureFont.h"
+#include    <stdlib.h>
+#include    <stdio.h>
+
+#ifndef __FTINTERNALS_H__
+#define __FTINTERNALS_H__
+
+#ifdef __cplusplus
+extern "C" {
+namespace C {
+#endif
+
+typedef enum ftglType
+{
+    Bitmap,
+    Pixmap,
+    Outline,
+    Polygon,
+    Extrude,
+    Texture,
+};
+
+struct FTGLfont
+{
+    FTFont *ptr;
+    ftglType type;
+};
+
+static inline FTGLfont *createFTFont(ftglType type, const char *fontname)
+{
+    FTGLfont *ftgl = (FTGLfont*)malloc(sizeof(FTGLfont));
+    ftgl->type = type;
+    switch(type)
+    {
+        case Bitmap:
+            ftgl->ptr = new FTGLBitmapFont(fontname);
+            break;
+        case Pixmap:
+            ftgl->ptr = new FTGLPixmapFont(fontname);
+            break;
+        case Outline:
+            ftgl->ptr = new FTGLOutlineFont(fontname);
+            break;
+        case Polygon:
+            ftgl->ptr = new FTGLPolygonFont(fontname);
+            break;
+        case Extrude:
+            ftgl->ptr = new FTGLExtrdFont(fontname);
+            break;
+        case Texture:
+            ftgl->ptr = new FTGLTextureFont(fontname);
+            break;
+    }
+    return ftgl;
+}
+
+#ifdef __cplusplus
+}
+}
+#endif
+
+#endif  //__FTINTERNALS_H__
+
diff --git a/src/FTGLBitmapFont.cpp b/src/FTGLBitmapFont.cpp
index ef3bb6b..05aa6b2 100644
--- a/src/FTGLBitmapFont.cpp
+++ b/src/FTGLBitmapFont.cpp
@@ -1,4 +1,5 @@
 #include "config.h"
+#include "FTInternals.h"
 
 #include "FTGLBitmapFont.h"
 #include "FTBitmapGlyph.h"
@@ -66,3 +67,17 @@ void FTGLBitmapFont::Render( const wchar_t* string)
     glPopClientAttrib();
 }
 
+#ifdef __cplusplus
+extern "C" {
+namespace C {
+#endif
+extern "C" FTGLfont* ftglBitmapFontMake(const char *fontname)
+{
+    FTGLfont *ftgl = createFTFont(Bitmap, fontname);
+    return ftgl;
+}
+#ifdef __cplusplus
+}
+}
+#endif
+
diff --git a/src/FTGLExtrdFont.cpp b/src/FTGLExtrdFont.cpp
index 4f264ff..dee644c 100644
--- a/src/FTGLExtrdFont.cpp
+++ b/src/FTGLExtrdFont.cpp
@@ -1,4 +1,5 @@
 #include "config.h"
+#include "FTInternals.h"
 
 #include "FTGLExtrdFont.h"
 #include "FTExtrdGlyph.h"
@@ -34,4 +35,17 @@ FTGlyph* FTGLExtrdFont::MakeGlyph( unsigned int glyphIndex)
     return NULL;
 }
 
+#ifdef __cplusplus
+extern "C" {
+namespace C {
+#endif
+extern "C" FTGLfont* ftglExtrdFontMake(const char *fontname)
+{
+    FTGLfont *ftgl = createFTFont(Extrude, fontname);
+    return ftgl;
+}
+#ifdef __cplusplus
+}
+}
+#endif
 
diff --git a/src/FTGLOutlineFont.cpp b/src/FTGLOutlineFont.cpp
index 714d38c..8fbb4da 100644
--- a/src/FTGLOutlineFont.cpp
+++ b/src/FTGLOutlineFont.cpp
@@ -1,4 +1,5 @@
 #include "config.h"
+#include    "FTInternals.h"
 
 #include "FTGLOutlineFont.h"
 #include "FTOutlineGlyph.h"
@@ -66,3 +67,16 @@ void FTGLOutlineFont::Render( const wchar_t* string)
     glPopAttrib();
 }
 
+#ifdef __cplusplus
+extern "C" {
+namespace C {
+#endif
+extern "C" FTGLfont* ftglOutlineFontMake(const char *fontname)
+{
+    FTGLfont *ftgl = createFTFont(Outline, fontname);
+    return ftgl;
+}
+#ifdef __cplusplus
+}
+}
+#endif
diff --git a/src/FTGLPixmapFont.cpp b/src/FTGLPixmapFont.cpp
index 135a0b2..f17bf51 100644
--- a/src/FTGLPixmapFont.cpp
+++ b/src/FTGLPixmapFont.cpp
@@ -1,4 +1,5 @@
 #include "config.h"
+#include "FTInternals.h"
 
 #include "FTGLPixmapFont.h"
 #include "FTPixmapGlyph.h"
@@ -82,4 +83,17 @@ void FTGLPixmapFont::Render( const wchar_t* string)
     glPopAttrib();
 }
 
+#ifdef __cplusplus
+extern "C" {
+namespace C {
+#endif
+extern "C" FTGLfont* ftglPixmapFontMake(const char *fontname)
+{
+    FTGLfont *ftgl = createFTFont(Pixmap, fontname);
+    return ftgl;
+}
+#ifdef __cplusplus
+}
+}
+#endif
 
diff --git a/src/FTGLPolygonFont.cpp b/src/FTGLPolygonFont.cpp
index 1246ba5..2863192 100644
--- a/src/FTGLPolygonFont.cpp
+++ b/src/FTGLPolygonFont.cpp
@@ -1,4 +1,5 @@
 #include "config.h"
+#include "FTInternals.h"
 
 #include "FTGLPolygonFont.h"
 #include "FTPolyGlyph.h"
@@ -32,4 +33,17 @@ FTGlyph* FTGLPolygonFont::MakeGlyph( unsigned int g)
     return NULL;
 }
 
+#ifdef __cplusplus
+extern "C" {
+namespace C {
+#endif
+extern "C" FTGLfont* ftglPolygonFontMake(const char *fontname)
+{
+    FTGLfont *ftgl = createFTFont(Polygon, fontname);
+    return ftgl;
+}
+#ifdef __cplusplus
+}
+}
+#endif
 
diff --git a/src/FTGLTextureFont.cpp b/src/FTGLTextureFont.cpp
index 3f180d4..9d4ac41 100644
--- a/src/FTGLTextureFont.cpp
+++ b/src/FTGLTextureFont.cpp
@@ -1,4 +1,5 @@
 #include "config.h"
+#include "FTInternals.h"
 
 #include <cassert>
 #include <string> // For memset
@@ -182,3 +183,17 @@ void FTGLTextureFont::Render( const wchar_t* string)
     glPopAttrib();
 }
 
+#ifdef __cplusplus
+extern "C" {
+namespace C {
+#endif
+extern "C" FTGLfont* ftglTextureFontMake(const char *fontname)
+{
+    FTGLfont *ftgl = createFTFont(Texture, fontname);
+    return ftgl;
+}
+#ifdef __cplusplus
+}
+}
+#endif
+
diff --git a/src/FTGlue.cpp b/src/FTGlue.cpp
new file mode 100644
index 0000000..7512033
--- /dev/null
+++ b/src/FTGlue.cpp
@@ -0,0 +1,103 @@
+#include "FTInternals.h"
+
+#ifdef __cplusplus
+extern "C" {
+namespace C {
+#endif
+
+#define C_FUN(cret, cname, cargs, cxxerr, cxxname, cxxarg) \
+    cret cname cargs \
+    { \
+        if(!f || !f->ptr) \
+        { \
+            fprintf(stderr, "FTGL Warning: NULL pointer .. %s\n", #cname); \
+            cxxerr; \
+        } \
+        switch(f->type) \
+        { \
+            case Bitmap: \
+                return dynamic_cast<FTGLBitmapFont*>(f->ptr)->cxxname cxxarg; \
+            case Extrude: \
+                return dynamic_cast<FTGLExtrdFont*>(f->ptr)->cxxname cxxarg; \
+            case Outline: \
+                return dynamic_cast<FTGLOutlineFont*>(f->ptr)->cxxname cxxarg; \
+            case Pixmap: \
+                return dynamic_cast<FTGLPixmapFont*>(f->ptr)->cxxname cxxarg; \
+            case Polygon: \
+                return dynamic_cast<FTGLPolygonFont*>(f->ptr)->cxxname cxxarg; \
+            case Texture: \
+                return dynamic_cast<FTGLTextureFont*>(f->ptr)->cxxname cxxarg; \
+        } \
+        fprintf(stderr, "FTGL Warning: Function not implemented yet .. %s for %d\n", #cname, f->type); \
+        cxxerr; \
+    }
+
+// bool FTFont::Attach(const char* fontFilePath);
+C_FUN(int, ftglAttachFile, (FTGLfont *f, const char* path),
+      return 0, Attach, (path));
+
+// bool FTFont::Attach(const unsigned char *pBufferBytes,
+//                     size_t bufferSizeInBytes);
+C_FUN(int, ftglAttachData, (FTGLfont *f, const unsigned char *p, size_t s),
+      return 0, Attach, (p, s));
+
+// bool FTFont::CharMap(FT_Encoding encoding);
+C_FUN(int, ftglCharMap, (FTGLfont *f, FT_Encoding enc),
+      return 0, CharMap, (enc));
+
+// unsigned int FTFont::CharMapCount();
+C_FUN(unsigned int, ftglCharMapCount, (FTGLfont *f),
+      return 0, CharMapCount, ());
+
+// FT_Encoding* FTFont::CharMapList();
+C_FUN(FT_Encoding *, ftglCharMapList, (FTGLfont* f),
+      return NULL, CharMapList, ());
+
+// virtual bool FTFont::FaceSize(const unsigned int size,
+//                               const unsigned int res = 72);
+C_FUN(int, ftglSetFaceSize, (FTGLfont *f, unsigned int s),
+      return 0, FaceSize, (s));
+C_FUN(int, ftglSetFaceSizeRes, (FTGLfont *f, unsigned int s, unsigned int r),
+      return 0, FaceSize, (s, r));
+
+// unsigned int FTFont::FaceSize() const;
+// XXX: need to call FaceSize() as FTFont::FaceSize() because of FTGLTexture
+C_FUN(unsigned int, ftglGetFaceSize, (FTGLfont *f),
+      return 0, FTFont::FaceSize, ());
+
+// virtual void FTFont::Depth(float depth);
+C_FUN(void, ftglSetDepth, (FTGLfont *f, float d), return, Depth, (d));
+
+// void FTFont::UseDisplayList(bool useList);
+C_FUN(void, ftglUseDisplayList, (FTGLfont *f, int l),
+      return, UseDisplayList, (l));
+
+// float FTFont::Ascender() const;
+C_FUN(float, ftglAscender, (FTGLfont *f), return 0.f, Ascender, ());
+
+// float FTFont::Descender() const;
+C_FUN(float, ftglDescender, (FTGLfont *f), return 0.f, Descender, ());
+
+// float FTFont::LineHeight() const;
+C_FUN(float, ftglLineHeight, (FTGLfont *f), return 0.f, LineHeight, ());
+
+// void FTFont::BBox(const char* string, float& llx, float& lly, float& llz,
+//                   float& urx, float& ury, float& urz);
+C_FUN(void, ftglBBox, (FTGLfont *f, const char* s, float c[6]),
+      return, BBox, (s, c[0], c[1], c[2], c[3], c[4], c[5]));
+
+// float FTFont::Advance(const char* string);
+C_FUN(float, ftglAdvance, (FTGLfont *f, const char* s),
+      return 0.f, Advance, (s));
+
+// virtual void FTFont::Render(const char* string);
+C_FUN(void, ftglRender, (FTGLfont *f, const char * s), return, Render, (s));
+
+// FT_Error FTFont::Error() const;
+C_FUN(FT_Error, ftglError, (FTGLfont *f), return -1, Error, ());
+
+#ifdef __cplusplus
+} // extern "C"
+} // namespace C
+#endif
+
diff --git a/src/Makefile.am b/src/Makefile.am
index e18633c..aa3308e 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -16,6 +16,7 @@ libftgl_la_SOURCES = \
 	FTGLTextureFont.cpp \
 	FTGlyph.cpp \
 	FTGlyphContainer.cpp \
+	FTGlue.cpp \
 	FTLibrary.cpp \
 	FTOutlineGlyph.cpp \
 	FTPixmapGlyph.cpp \
@@ -59,3 +60,4 @@ ftgl_HEADERS = \
 	../include/FTVector.h \
 	../include/FTVectoriser.h
 
+