Commit 9ae936f32cf5931975a805b92c6ed411e508591f

sammy 2008-04-22T23:47:24

* C bindings for the FTLayout interface. Code by Eric Beets.

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
diff --git a/demo/FTGLDemo.cpp b/demo/FTGLDemo.cpp
index 9f0bce3..db865cb 100644
--- a/demo/FTGLDemo.cpp
+++ b/demo/FTGLDemo.cpp
@@ -345,16 +345,16 @@ void renderFontInfo()
         // Output the alignment mode of the layout
         switch (((FTSimpleLayout *)layouts[currentLayout])->GetAlignment())
         {
-            case FTSimpleLayout::ALIGN_LEFT:
+            case FTGL::ALIGN_LEFT:
                 infoFont->Render("Align Left");
                 break;
-            case FTSimpleLayout::ALIGN_RIGHT:
+            case FTGL::ALIGN_RIGHT:
                 infoFont->Render("Align Right");
                 break;
-            case FTSimpleLayout::ALIGN_CENTER:
+            case FTGL::ALIGN_CENTER:
                 infoFont->Render("Align Center");
                 break;
-            case FTSimpleLayout::ALIGN_JUST:
+            case FTGL::ALIGN_JUSTIFY:
                 infoFont->Render("Align Justified");
                 break;
         }
@@ -512,17 +512,17 @@ void parsekey(unsigned char key, int x, int y)
             // Decrement the layout
             switch (simpleLayout->GetAlignment())
             {
-            case FTSimpleLayout::ALIGN_LEFT:
-                simpleLayout->SetAlignment(FTSimpleLayout::ALIGN_RIGHT);
+            case FTGL::ALIGN_LEFT:
+                simpleLayout->SetAlignment(FTGL::ALIGN_RIGHT);
                 break;
-            case FTSimpleLayout::ALIGN_RIGHT:
-                simpleLayout->SetAlignment(FTSimpleLayout::ALIGN_CENTER);
+            case FTGL::ALIGN_RIGHT:
+                simpleLayout->SetAlignment(FTGL::ALIGN_CENTER);
                 break;
-            case FTSimpleLayout::ALIGN_CENTER:
-                simpleLayout->SetAlignment(FTSimpleLayout::ALIGN_JUST);
+            case FTGL::ALIGN_CENTER:
+                simpleLayout->SetAlignment(FTGL::ALIGN_JUSTIFY);
                 break;
-            case FTSimpleLayout::ALIGN_JUST:
-                simpleLayout->SetAlignment(FTSimpleLayout::ALIGN_LEFT);
+            case FTGL::ALIGN_JUSTIFY:
+                simpleLayout->SetAlignment(FTGL::ALIGN_LEFT);
                 break;
             }
         }
diff --git a/demo/FTGLMFontDemo.cpp b/demo/FTGLMFontDemo.cpp
index 09a6eb5..4bceab9 100644
--- a/demo/FTGLMFontDemo.cpp
+++ b/demo/FTGLMFontDemo.cpp
@@ -368,16 +368,16 @@ void renderFontInfo()
         // Output the alignment mode of the layout
         switch (((FTSimpleLayout *)layouts[currentLayout])->GetAlignment())
         {
-            case FTSimpleLayout::ALIGN_LEFT:
+            case FTGL::ALIGN_LEFT:
                 infoFont->Render("Align Left");
                 break;
-            case FTSimpleLayout::ALIGN_RIGHT:
+            case FTGL::ALIGN_RIGHT:
                 infoFont->Render("Align Right");
                 break;
-            case FTSimpleLayout::ALIGN_CENTER:
+            case FTGL::ALIGN_CENTER:
                 infoFont->Render("Align Center");
                 break;
-            case FTSimpleLayout::ALIGN_JUST:
+            case FTGL::ALIGN_JUSTIFY:
                 infoFont->Render("Align Justified");
                 break;
         }
@@ -517,17 +517,17 @@ void parsekey(unsigned char key, int x, int y)
             // Decrement the layout
             switch (simpleLayout->GetAlignment())
             {
-            case FTSimpleLayout::ALIGN_LEFT:
-                simpleLayout->SetAlignment(FTSimpleLayout::ALIGN_RIGHT);
+            case FTGL::ALIGN_LEFT:
+                simpleLayout->SetAlignment(FTGL::ALIGN_RIGHT);
                 break;
-            case FTSimpleLayout::ALIGN_RIGHT:
-                simpleLayout->SetAlignment(FTSimpleLayout::ALIGN_CENTER);
+            case FTGL::ALIGN_RIGHT:
+                simpleLayout->SetAlignment(FTGL::ALIGN_CENTER);
                 break;
-            case FTSimpleLayout::ALIGN_CENTER:
-                simpleLayout->SetAlignment(FTSimpleLayout::ALIGN_JUST);
+            case FTGL::ALIGN_CENTER:
+                simpleLayout->SetAlignment(FTGL::ALIGN_JUSTIFY);
                 break;
-            case FTSimpleLayout::ALIGN_JUST:
-                simpleLayout->SetAlignment(FTSimpleLayout::ALIGN_LEFT);
+            case FTGL::ALIGN_JUSTIFY:
+                simpleLayout->SetAlignment(FTGL::ALIGN_LEFT);
                 break;
             }
         }
diff --git a/include/FTGL.h b/include/FTGL.h
index 88104c6..57f137e 100644
--- a/include/FTGL.h
+++ b/include/FTGL.h
@@ -39,7 +39,11 @@
 namespace FTGL
 {
     typedef enum{RENDER_FRONT = 0x01, RENDER_BACK = 0x02, RENDER_SIDE = 0x04}ftglRenderMode;
+    typedef enum{ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT, ALIGN_JUSTIFY} TextAlignment;
 }
+#else
+    typedef enum{RENDER_FRONT = 0x01, RENDER_BACK = 0x02, RENDER_SIDE = 0x04}ftglRenderMode;
+    typedef enum{ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT, ALIGN_JUSTIFY} TextAlignment;
 #endif
 
 typedef double   FTGL_DOUBLE;
diff --git a/include/FTInternals.h b/include/FTInternals.h
index ea748ac..ac0fc2d 100644
--- a/include/FTInternals.h
+++ b/include/FTInternals.h
@@ -41,6 +41,9 @@
 #include    "FTGLExtrdFont.h"
 #include    "FTGLTextureFont.h"
 
+#include    "FTLayout.h"
+#include    "FTSimpleLayout.h"
+
 #include    <stdlib.h>
 #include    <stdio.h>
 
@@ -96,6 +99,32 @@ static inline FTGLfont *createFTFont(ftglType type, const char *fontname)
     return ftgl;
 }
 
+typedef enum ftglLayoutType
+{
+    Simple,
+};
+
+struct FTGLlayout
+{
+    FTLayout *ptr;
+    FTGLfont *font;
+    ftglLayoutType type;
+};
+
+static inline FTGLlayout *createFTLayout(ftglLayoutType type)
+{
+    FTGLlayout *layout = (FTGLlayout*)malloc(sizeof(FTGLlayout));
+    layout->font = NULL;
+    layout->type = type;
+    switch(type)
+    {
+        case Simple:
+            layout->ptr = new FTSimpleLayout();
+            break;
+    }
+    return layout;
+}
+
 #ifdef __cplusplus
 }
 }
diff --git a/include/FTLayout.h b/include/FTLayout.h
index 3393b17..6940b2e 100644
--- a/include/FTLayout.h
+++ b/include/FTLayout.h
@@ -35,6 +35,8 @@
 #ifndef    __FTLayout__
 #define    __FTLayout__
 
+#ifdef __cplusplus
+
 #include "FTGL.h"
 #include "FTPoint.h"
 #include "FTFont.h"
@@ -160,5 +162,22 @@ class FTGL_EXPORT FTLayout
             { return(font->charSize); }
 };
 
+#endif //__cplusplus
+
+#ifdef __cplusplus
+extern "C" {
+namespace C {
+#endif
+
+typedef struct FTGLlayout FTGLlayout;
+
+FTGL_EXPORT void ftglDestroyLayout(FTGLlayout*);
+
+#ifdef __cplusplus
+}
+}
+#endif
+
 #endif  /* __FTLayout__ */
 
+
diff --git a/include/FTSimpleLayout.h b/include/FTSimpleLayout.h
index 0c79f41..f0a1d19 100644
--- a/include/FTSimpleLayout.h
+++ b/include/FTSimpleLayout.h
@@ -35,6 +35,8 @@
 #ifndef    __FTSimpleLayout__
 #define    __FTSimpleLayout__
 
+#ifdef __cplusplus
+
 #include "FTLayout.h"
 #include "FTBBox.h"
 
@@ -129,9 +131,6 @@ class FTGL_EXPORT FTSimpleLayout : public FTLayout
         void RenderSpace(const wchar_t *string, const float ExtraSpace = 0.0)
             { pen.X(0); pen.Y(0); RenderSpace(string, 0, -1, 0, ExtraSpace); }
 
-        typedef enum
-            { ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT, ALIGN_JUST } TextAlignment;
-
         /**
          * Set he font to use for rendering the text.
          *
@@ -166,12 +165,12 @@ class FTGL_EXPORT FTSimpleLayout : public FTLayout
          *
          * @param Alignment The new alignment mode.
          */
-        void SetAlignment(const TextAlignment Alignment)
+        void SetAlignment(const FTGL::TextAlignment Alignment)
             { alignment = Alignment; }
         /**
          * @return The text alignment mode.
          */
-        TextAlignment GetAlignment() const
+        FTGL::TextAlignment GetAlignment() const
             { return(alignment); }
 
         /**
@@ -317,7 +316,7 @@ class FTGL_EXPORT FTSimpleLayout : public FTLayout
          * The text alignment mode used to distribute
          * space within a line or rendered text.
          */
-        TextAlignment alignment;
+        FTGL::TextAlignment alignment;
 
         /**
          * The height of each line of text expressed as
@@ -349,5 +348,40 @@ class FTGL_EXPORT FTSimpleLayout : public FTLayout
                             const float RemainingWidth, FTBBox *bounds, int renderMode);
 };
 
+#endif //__cplusplus
+
+#ifdef __cplusplus
+extern "C" {
+namespace C {
+#endif
+
+FTGL_EXPORT void  ftglLayoutBBox           (FTGLlayout *, const char*, float []);
+
+FTGL_EXPORT void  ftglLayoutRender         (FTGLlayout *, const char*);
+FTGL_EXPORT void  ftglLayoutRenderMode     (FTGLlayout *, int);
+FTGL_EXPORT void  ftglLayoutRenderSpace    (FTGLlayout *, const float);
+
+FTGL_EXPORT void      ftglLayoutSetFont    (FTGLlayout *, FTGLfont*);
+FTGL_EXPORT FTGLfont* ftglLayoutGetFont    (FTGLlayout *);
+
+FTGL_EXPORT void  ftglLayoutSetLineLength  (FTGLlayout *, const float);
+FTGL_EXPORT float ftglLayoutGetLineLength  (FTGLlayout *);
+
+#ifdef __cplusplus
+FTGL_EXPORT void                ftglLayoutSetAlignment   (FTGLlayout *, const FTGL::TextAlignment);
+FTGL_EXPORT FTGL::TextAlignment ftglLayoutGetAlignement  (FTGLlayout *);
+#else
+FTGL_EXPORT void                ftglLayoutSetAlignment   (FTGLlayout *, const TextAlignment);
+FTGL_EXPORT TextAlignment       ftglLayoutGetAlignement  (FTGLlayout *);
+#endif
+
+FTGL_EXPORT void  ftglLayoutSetLineSpacing (FTGLlayout *, const float);
+FTGL_EXPORT float ftglLayoutGetLineSpacing (FTGLlayout *);
+
+#ifdef __cplusplus
+}
+}
+#endif
+
 #endif  /* __FTSimpleLayout__ */
 
diff --git a/src/FTGlue.cpp b/src/FTGlue.cpp
index ce5a038..906f56a 100644
--- a/src/FTGlue.cpp
+++ b/src/FTGlue.cpp
@@ -11,10 +11,10 @@
  * distribute, sublicense, and/or sell copies of the Software, and to
  * permit persons to whom the Software is furnished to do so, subject to
  * the following conditions:
- * 
+ *
  * The above copyright notice and this permission notice shall be
  * included in all copies or substantial portions of the Software.
- * 
+ *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
@@ -40,12 +40,16 @@
 namespace C
 {
 
+//
+//  C wrappers for FTFont
+//
+
 #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); \
+            fprintf(stderr, "FTGL warning: NULL pointer in %s\n", #cname); \
             cxxerr; \
         } \
         switch(f->type) \
@@ -63,7 +67,7 @@ namespace C
             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); \
+        fprintf(stderr, "FTGL warning: %s not implemented for %d\n", #cname, f->type); \
         cxxerr; \
     }
 
@@ -138,5 +142,100 @@ 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, ());
 
+#undef C_FUN
+
+//
+//  C wrappers for FTLayout
+//
+
+#define C_FUN(cret, cname, cargs, cxxerr, cxxname, cxxarg) \
+    cret cname cargs \
+    { \
+        if(!f || !f->ptr) \
+        { \
+            fprintf(stderr, "FTGL warning: NULL pointer in %s\n", #cname); \
+            cxxerr; \
+        } \
+        switch(f->type) \
+        { \
+            case Simple: \
+                return dynamic_cast<FTSimpleLayout*>(f->ptr)->cxxname cxxarg; \
+        } \
+        fprintf(stderr, "FTGL warning: %s not implemented for %d\n", #cname, f->type); \
+        cxxerr; \
+    }
+
+// FTLayout::~FTLayout();
+C_FUN(void, ftglDestroyLayout, (FTGLlayout *f), return, ~FTLayout, ());
+
+// virtual void BBox(const char* string, float& llx, float& lly, float& llz, float& urx, float& ury, float& urz)
+C_FUN(void, ftglLayoutBBox, (FTGLlayout *f, const char * s, float c[6]),
+      return, BBox, (s, c[0], c[1], c[2], c[3], c[4], c[5]));
+
+// virtual void Render(const char* string);
+C_FUN(void, ftglLayoutRender, (FTGLlayout *f, const char *s),
+      return, Render, (s));
+
+// virtual void Render(const char* string, int renderMode);
+C_FUN(void, ftglLayoutRenderMode, (FTGLlayout *f, const char *s, int r),
+      return, Render, (s, r));
+
+// void RenderSpace(const char *string, const float ExtraSpace = 0.0)
+C_FUN(void, ftglLayoutRenderSpace, (FTGLlayout *f, const char *s,
+        const float e), return, RenderSpace, (s, e));
+
+// void SetFont(FTFont *fontInit)
+void ftglLayoutSetFont(FTGLlayout *f, FTGLfont *font)
+{
+    if(!f || !f->ptr)
+    {
+        fprintf(stderr, "FTGL warning: NULL pointer in %s\n", __func__);
+        return;
+    }
+    switch(f->type)
+    {
+        case Simple:
+            f->font = font;
+            return dynamic_cast<FTSimpleLayout*>(f->ptr)->SetFont(font->ptr);
+    }
+    fprintf(stderr, "FTGL warning: %s not implemented for %d\n",
+                    __func__, f->type);
+}
+
+// FTFont *GetFont()
+FTGLfont *ftglLayoutGetFont(FTGLlayout *f)
+{
+    if(!f || !f->ptr)
+    {
+        fprintf(stderr, "FTGL warning: NULL pointer in %s\n", __func__);
+        return NULL;
+    }
+    return f->font;
+}
+
+// void SetLineLength(const float LineLength);
+C_FUN(void, ftglLayoutSetLineLength, (FTGLlayout *f, const float length),
+      return, SetLineLength, (length));
+
+// float GetLineLength() const
+C_FUN(float, ftglLayoutGetLineLength, (FTGLlayout *f),
+      return 0.0f, GetLineLength, ());
+
+// void SetAlignment(const TextAlignment Alignment)
+C_FUN(void, ftglLayoutSetAlignment, (FTGLlayout *f, FTGL::TextAlignment a),
+      return, SetAlignment, (a));
+
+// TextAlignment GetAlignment() const
+C_FUN(FTGL::TextAlignment, ftglLayoutGetAlignement, (FTGLlayout *f),
+      return FTGL::ALIGN_LEFT, GetAlignment, ());
+
+// void SetLineSpacing(const float LineSpacing)
+C_FUN(void, ftglLayoutSetLineSpacing, (FTGLlayout *f, const float l),
+      return, SetLineSpacing, (l));
+
+// float GetLineSpacing() const
+C_FUN(float, ftglLayoutGetLineSpacing, (FTGLlayout *f),
+      return 0.0f, GetLineSpacing, ());
+
 } // namespace C
 
diff --git a/src/FTSimpleLayout.cpp b/src/FTSimpleLayout.cpp
index 0217021..fe0007a 100644
--- a/src/FTSimpleLayout.cpp
+++ b/src/FTSimpleLayout.cpp
@@ -34,6 +34,8 @@
 
 #include "config.h"
 
+#include "FTInternals.h"
+
 #include <ctype.h>
 
 #include "FTInternals.h"
@@ -49,7 +51,7 @@ FTSimpleLayout::FTSimpleLayout()
 {
     currentFont = NULL;
     lineLength = 100.0f;
-    alignment = ALIGN_LEFT;
+    alignment = FTGL::ALIGN_LEFT;
     lineSpacing = 1.0f;
 }
 
@@ -215,11 +217,11 @@ inline void FTSimpleLayout::WrapTextI(const T *buf, int renderMode, FTBBox *boun
     float remainingWidth = lineLength - currentWidth;
     // Render any remaining text on the last line
     // Disable justification for the last row
-    if(alignment == ALIGN_JUST)
+    if(alignment == FTGL::ALIGN_JUSTIFY)
     {
-        alignment = ALIGN_LEFT;
+        alignment = FTGL::ALIGN_LEFT;
         OutputWrapped(buf, lineStart, -1, remainingWidth, bounds, renderMode);
-        alignment = ALIGN_JUST;
+        alignment = FTGL::ALIGN_JUSTIFY;
     }
     else
     {
@@ -250,16 +252,16 @@ inline void FTSimpleLayout::OutputWrappedI(const T *buf, const int start,
     // Align the text according as specified by Alignment
     switch (alignment)
     {
-        case ALIGN_LEFT:
+        case FTGL::ALIGN_LEFT:
             pen.X(0);
             break;
-        case ALIGN_CENTER:
+        case FTGL::ALIGN_CENTER:
             pen.X(RemainingWidth / 2);
             break;
-        case ALIGN_RIGHT:
+        case FTGL::ALIGN_RIGHT:
             pen.X(RemainingWidth);
             break;
-        case ALIGN_JUST:
+        case FTGL::ALIGN_JUSTIFY:
             pen.X(0);
             distributeWidth = RemainingWidth;
             break;
@@ -367,3 +369,12 @@ void FTSimpleLayout::RenderSpace(const wchar_t *string, const int start,
     RenderSpaceI(string, start, end, renderMode, ExtraSpace);
 }
 
+namespace C
+{
+    extern "C" FTGLlayout* ftglCreateSimpleLayout()
+    {
+        FTGLlayout *layout = createFTLayout(Simple);
+        return layout;
+    }
+}
+