Commit d6a2d8517d32cb2cec15a6b48d7686e7bc178310

sammy 2008-04-13T21:14:44

* Merged trunk commits [664], [665], [666], [667], [669], [670], [671], [672] and [684], by patrick and henry: + Added FTLayout.h, FTSimpleLayout.h and FTSimpleLayout.cpp to implement a framework for layout managers and an implementation of a simple layout manager. + Updated FTGLDemo to use the new FTSimpleLayout. Changes include: - Added a font origin to specify the location to render the font. - The default text is now blatantly plagarized from the back cover of the OGL red book. - The font size is much smaller. - Font metrics are rendered differently depending on the current layout manager. - The FTSimpleLayout alignment mode is now output with other font information. - The space bar no longer cycles through the fonts. The cursor up/down keys do. - The cursor left/right keys increment/decrement the size of the current font. - The page up/page down keys cycle through the layout managers. - The home/end keys increment and decrement the line length of a simple layout - The tab key cycles through the alignment modes of a simple layout. + Fixed a bug where the trackball rotation was applied after translation. + Minor reformatting and enabled texture fonts to be selected + Un-inlined private methods...because they are called by other private inlined methods + Minor formatting changes + Adding FTLayout

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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
diff --git a/demo/FTGLDemo.cpp b/demo/FTGLDemo.cpp
index 272d113..db2569a 100644
--- a/demo/FTGLDemo.cpp
+++ b/demo/FTGLDemo.cpp
@@ -2,6 +2,7 @@
 
 #include <stdlib.h>
 #include <stdio.h>
+#include <string.h>
 
 #ifdef __APPLE_CC__
 	#include <GLUT/glut.h>
@@ -17,6 +18,7 @@
 #include "FTGLTextureFont.h"
 #include "FTGLPixmapFont.h"
 #include "FTGLBitmapFont.h"
+#include "FTSimpleLayout.h"
 
 // YOU'LL PROBABLY WANT TO CHANGE THESE
 #ifdef __linux__
@@ -49,8 +51,18 @@ GLint w_win = 640, h_win = 480;
 int mode = INTERACTIVE;
 int carat = 0;
 
+FTSimpleLayout simpleLayout;
+FTLayout			*layouts[] = { &simpleLayout, NULL};
+int currentLayout = 0;
+const int NumLayouts = 2;
+
+const float InitialLineLength = 300.0f;
+
+const float OX = -100;
+const float OY = 200;
+
 //wchar_t myString[16] = { 0x6FB3, 0x9580};
-wchar_t myString[16];
+char myString[4096];
 
 static FTFont* fonts[6];
 static FTGLPixmapFont* infoFont;
@@ -123,7 +135,7 @@ void setUpFonts( const char* fontfile)
 			exit(1);
 		}
 		
-		if( !fonts[x]->FaceSize( 144))
+		if( !fonts[x]->FaceSize( 24))
 		{
 			fprintf( stderr, "Failed to set size");
 			exit(1);
@@ -144,19 +156,27 @@ void setUpFonts( const char* fontfile)
 	
 	infoFont->FaceSize( 18);
 
-	myString[0] = 65;
-	myString[1] = 0;
+        strcpy(myString, "OpenGL is a powerful software interface for "
+               "graphics hardware that allows graphics programmers to produce "
+               "high-quality color images of 3D objects. abcdefghijklmnopqrstu"
+               "vwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
 }
 
 
 void renderFontmetrics()
 {
-	float x1, y1, z1, x2, y2, z2;
-	fonts[current_font]->BBox( myString, x1, y1, z1, x2, y2, z2);
-	
-	// Draw the bounding box
-	glDisable( GL_LIGHTING);
-	glDisable( GL_TEXTURE_2D);
+    float x1, y1, z1, x2, y2, z2;
+
+    // If there is a layout use it to compute the bbox, otherwise query as
+    // a string.
+    if(layouts[currentLayout])
+        layouts[currentLayout]->BBox(myString, x1, y1, z1, x2, y2, z2);
+    else
+        fonts[current_font]->BBox(myString, x1, y1, z1, x2, y2, z2);
+
+    // Draw the bounding box
+    glDisable( GL_LIGHTING);
+    glDisable( GL_TEXTURE_2D);
     glEnable( GL_LINE_SMOOTH);
     glEnable(GL_BLEND);
     glBlendFunc( GL_SRC_ALPHA, GL_ONE); // GL_ONE_MINUS_SRC_ALPHA
@@ -193,14 +213,27 @@ void renderFontmetrics()
 			glVertex3f( x2, y1, z2);
 		glEnd();
 	}
-		
-		// Draw the baseline, Ascender and Descender
-	glBegin( GL_LINES);
-		glColor3f( 0.0, 0.0, 1.0);
-		glVertex3f( 0.0, 0.0, 0.0);
-		glVertex3f( fonts[current_font]->Advance( myString), 0.0, 0.0);
-		glVertex3f( 0.0, fonts[current_font]->Ascender(), 0.0);
-		glVertex3f( 0.0, fonts[current_font]->Descender(), 0.0);
+	
+   if (!layouts[currentLayout]) {
+		/* There is no layout. Draw the baseline, Ascender and Descender */
+      glBegin( GL_LINES);
+         glColor3f( 0.0, 0.0, 1.0);
+         glVertex3f( 0.0, 0.0, 0.0);
+         glVertex3f( fonts[current_font]->Advance( myString), 0.0, 0.0);
+         glVertex3f( 0.0, fonts[current_font]->Ascender(), 0.0);
+         glVertex3f( 0.0, fonts[current_font]->Descender(), 0.0);
+   } else if (layouts[currentLayout] && (dynamic_cast <FTSimpleLayout *>(layouts[currentLayout]))) {
+      float lineWidth = ((FTSimpleLayout *)layouts[currentLayout])->GetLineLength();
+      
+      /* The layout is a simple layout.  Render guides that mark the edges of the wrap region */
+      glColor3f(0.5,1.0,1.0);
+      glBegin( GL_LINES);
+         glVertex3f( 0, 10000, 0);
+         glVertex3f( 0,-10000, 0);
+         glVertex3f( lineWidth, 10000, 0);
+         glVertex3f( lineWidth,-10000, 0);
+      glEnd();
+   } /* Render layout specific metrics (if/elseif layouts[currentLayout]) */
 		
 	glEnd();
 	
@@ -260,6 +293,16 @@ void renderFontInfo()
 	
 	glRasterPos2f( 20.0f , 20.0f + infoFont->LineHeight());
 	infoFont->Render(fontfile);
+   
+   if (layouts[currentLayout] && (dynamic_cast <FTSimpleLayout *>(layouts[currentLayout]))) { 
+      glRasterPos2f( 20.0f , 20.0f + 2*(infoFont->Ascender() - infoFont->Descender()));
+      switch (((FTSimpleLayout *)layouts[currentLayout])->GetAlignment()) {
+         case FTSimpleLayout::ALIGN_LEFT: infoFont->Render("Align Left"); break;
+         case FTSimpleLayout::ALIGN_RIGHT: infoFont->Render("Align Right"); break;
+         case FTSimpleLayout::ALIGN_CENTER: infoFont->Render("Align Center"); break;
+         case FTSimpleLayout::ALIGN_JUST: infoFont->Render("Align Justified"); break;
+      } /* Output the alignment mode of the layout (switch GetAlignment()) */
+   } /* If the current layout is a simple layout output the alignemnt mode (if layouts[currentLayout]) */
 }
 
 
@@ -293,18 +336,21 @@ void do_display (void)
 
 	}
 
-	glColor3f( 1.0, 1.0, 1.0);
-// If you do want to switch the color of bitmaps rendered with glBitmap,
-// you will need to explicitly call glRasterPos3f (or its ilk) to lock
-// in a changed current color.
+    glColor3f( 1.0, 1.0, 1.0);
+    // If you do want to switch the color of bitmaps rendered with glBitmap,
+    // you will need to explicitly call glRasterPos (or its ilk) to lock
+    // in a changed current color.
 
-	glPushMatrix();
-        fonts[current_font]->Render( myString);
-	glPopMatrix();
+    glPushMatrix();
+        if(layouts[currentLayout])
+            layouts[currentLayout]->Render(myString);
+        else
+            fonts[current_font]->Render( myString);
+    glPopMatrix();
 
-	glPushMatrix();
+    glPushMatrix();
         renderFontmetrics();
-	glPopMatrix();
+    glPopMatrix();
 
     renderFontInfo();
 }
@@ -320,13 +366,14 @@ void display(void)
 	{
 		case FTGL_BITMAP:
 		case FTGL_PIXMAP:
-			glRasterPos2i( w_win / 2, h_win / 2);
-			glTranslatef(  w_win / 2, h_win / 2, 0.0);
+			glRasterPos2i((long)( w_win / 2 + OX),(long)( h_win / 2 + OY));
+			glTranslatef(w_win / 2 + OX,h_win / 2 + OY, 0.0);
 			break;
 		case FTGL_OUTLINE:
 		case FTGL_POLYGON:
 		case FTGL_EXTRUDE:
 		case FTGL_TEXTURE:
+         glTranslatef(OX,OY,0);
 			tbMatrix();
 			break;
 	}
@@ -363,7 +410,11 @@ void myinit( const char* fontfile)
 	tbAnimate( GL_FALSE);
 
     setUpFonts( fontfile);
-       
+
+    // Configure the simple layout
+    simpleLayout.SetLineLength(InitialLineLength);
+    simpleLayout.SetFont(fonts[current_font]);
+
     glGenTextures(1, &textureID);
     glBindTexture(GL_TEXTURE_2D, textureID);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
@@ -392,11 +443,17 @@ void parsekey(unsigned char key, int x, int y)
 				carat = 0;
 			}
 			break;
-		case ' ':
-			current_font++;
-			if(current_font > 5)
-				current_font = 0;
-			break;
+      case '\t':
+         if (layouts[currentLayout] && (dynamic_cast <FTSimpleLayout *>(layouts[currentLayout]))) {
+            FTSimpleLayout *simpleLayout = (FTSimpleLayout *)layouts[currentLayout];
+            switch (simpleLayout->GetAlignment()) {
+               case FTSimpleLayout::ALIGN_LEFT: 	simpleLayout->SetAlignment(FTSimpleLayout::ALIGN_RIGHT); break;
+               case FTSimpleLayout::ALIGN_RIGHT: 	simpleLayout->SetAlignment(FTSimpleLayout::ALIGN_CENTER); break;
+               case FTSimpleLayout::ALIGN_CENTER:	simpleLayout->SetAlignment(FTSimpleLayout::ALIGN_JUST); break;
+               case FTSimpleLayout::ALIGN_JUST: 	simpleLayout->SetAlignment(FTSimpleLayout::ALIGN_LEFT); break;
+            } /* Decrement the layout (switch GetAlignemnt()) */
+         } /* If the current layout is a simple layout change it's alignment properties (if simpleLayout) */
+         break;
 		default:
 			if( mode == INTERACTIVE)
 			{
@@ -408,7 +465,7 @@ void parsekey(unsigned char key, int x, int y)
 			{
 				myString[carat] = key;
 				myString[carat + 1] = 0;
-				carat = carat > 14 ? 14 : carat + 1;
+				carat = carat > 2000 ? 2000 : carat + 1;
 			}
 	}
 	
@@ -417,27 +474,78 @@ void parsekey(unsigned char key, int x, int y)
 }
 
 
+void parseSpecialKey(int key, int x, int y)
+{
+    FTSimpleLayout *simpleLayout = NULL;
+
+    // If the currentLayout is a simple layout store a pointer in simpleLayout
+    if(layouts[currentLayout]
+        && (dynamic_cast <FTSimpleLayout *>(layouts[currentLayout])))
+    {
+        simpleLayout = (FTSimpleLayout *)layouts[currentLayout];
+    }
+
+    switch (key)
+    {
+    case GLUT_KEY_UP:
+        current_font = (current_font + 1)%5;
+        break;
+    case GLUT_KEY_DOWN:
+        current_font = (current_font + 4)%5;
+        break;
+    case GLUT_KEY_PAGE_UP:
+        currentLayout = (currentLayout + 1)%NumLayouts;
+        break;
+    case GLUT_KEY_PAGE_DOWN:
+        currentLayout = (currentLayout + NumLayouts - 1)%NumLayouts;
+        break;
+    case GLUT_KEY_HOME:
+        /* If the current layout is simple decrement its line length */
+        if (simpleLayout) simpleLayout->SetLineLength(simpleLayout->GetLineLength() - 10.0f);
+        break;
+    case GLUT_KEY_END:
+        /* If the current layout is simple increment its line length */
+        if (simpleLayout) simpleLayout->SetLineLength(simpleLayout->GetLineLength() + 10.0f);
+        break;
+    case GLUT_KEY_LEFT:
+        fonts[current_font]->FaceSize(fonts[current_font]->FaceSize() - 1);
+        break;      
+    case GLUT_KEY_RIGHT:
+        fonts[current_font]->FaceSize(fonts[current_font]->FaceSize() + 1);
+        break;
+    }
+
+    // If the current layout is a simple layout, update its font.
+    if(simpleLayout)
+    { 
+        simpleLayout->SetFont(fonts[current_font]);
+    }
+
+    glutPostRedisplay();
+}
+
+
 void motion(int x, int y)
 {
-	tbMotion( x, y);
+    tbMotion( x, y);
 }
 
 void mouse(int button, int state, int x, int y)
 {
-	tbMouse( button, state, x, y);
+    tbMouse( button, state, x, y);
 }
 
 void myReshape(int w, int h)
 {
-	glMatrixMode (GL_MODELVIEW);
-	glViewport (0, 0, w, h);
-	glLoadIdentity();
-		
-	w_win = w;
-	h_win = h;
-	SetCamera();
-	
-	tbReshape(w_win, h_win);
+    glMatrixMode (GL_MODELVIEW);
+    glViewport (0, 0, w, h);
+    glLoadIdentity();
+
+    w_win = w;
+    h_win = h;
+    SetCamera();
+
+    tbReshape(w_win, h_win);
 }
 
 void SetCamera(void)
@@ -487,6 +595,7 @@ int main(int argc, char *argv[])
 	glutCreateWindow("FTGL TEST");
 	glutDisplayFunc(display);
 	glutKeyboardFunc(parsekey);
+   glutSpecialFunc(parseSpecialKey);
 	glutMouseFunc(mouse);
     glutMotionFunc(motion);
 	glutReshapeFunc(myReshape);
diff --git a/include/FTFont.h b/include/FTFont.h
index f99d1dd..2c0ff8c 100644
--- a/include/FTFont.h
+++ b/include/FTFont.h
@@ -338,8 +338,8 @@ class FTGL_EXPORT FTFont
          *                  will be incremented by the kerning advance of 
          *                  char and nextChr.
          */
-        inline void DoRender(const unsigned int chr,
-                             const unsigned int nextChr, FTPoint &origin);
+        void DoRender(const unsigned int chr,
+                      const unsigned int nextChr, FTPoint &origin);
 
         /**
          * Check that the glyph at <code>chr</code> exist. If not load it.
@@ -347,7 +347,7 @@ class FTGL_EXPORT FTFont
          * @param chr  character index
          * @return <code>true</code> if the glyph can be created.
          */
-        inline bool CheckGlyph( const unsigned int chr);
+        bool CheckGlyph( const unsigned int chr);
 
         /**
          * An object that holds a list of glyphs
diff --git a/include/FTLayout.h b/include/FTLayout.h
new file mode 100644
index 0000000..43f0410
--- /dev/null
+++ b/include/FTLayout.h
@@ -0,0 +1,107 @@
+#ifndef    __FTLayout__
+#define    __FTLayout__
+
+#include "FTGL.h"
+#include "FTPoint.h"
+#include "FTFont.h"
+
+/**
+ * FTLayout is the interface for layout managers that render text.
+ *
+ * Specific layout manager classes are derived from this class. This class
+ * is abstract and deriving classes must implement the protected
+ * <code>Render</code> methods to render formatted text and 
+ * <code>BBox</code> methods to determine the bounding box of output text.
+ *
+ * @see     FTFont
+ */
+class FTGL_EXPORT FTLayout {
+    public:        
+        /**
+         * Get the bounding box for a formatted string.
+         *
+         * @param string    a char string
+         * @param llx       lower left near x coord
+         * @param lly       lower left near y coord
+         * @param llz       lower left near z coord
+         * @param urx       upper right far x coord
+         * @param ury       upper right far y coord
+         * @param urz       upper right far z coord
+         */
+        virtual void BBox(const char* String,float& llx,float& lly,float& llz,float& urx,float& ury,float& urz) = 0;
+
+        /**
+         * Get the bounding box for a formatted string.
+         *
+         * @param string    a wchar_t string
+         * @param llx       lower left near x coord
+         * @param lly       lower left near y coord
+         * @param llz       lower left near z coord
+         * @param urx       upper right far x coord
+         * @param ury       upper right far y coord
+         * @param urz       upper right far z coord
+         */
+        virtual void BBox(const wchar_t* String,float& llx,float& lly,float& llz,float& urx,float& ury,float& urz) = 0;
+            
+        /**
+         * Render a string of characters
+         * 
+         * @param string    'C' style string to be output.   
+         */
+        virtual void Render(const char *String) = 0;
+
+        /**
+         * Render a string of characters
+         * 
+         * @param string    wchar_t string to be output.     
+         */
+        virtual void Render(const wchar_t *String) = 0;
+   protected:
+        /**
+         * Current pen or cursor position;
+         */
+        FTPoint pen;
+
+        /**
+         * Expose <code>FTFont::DoRender</code> method to derived classes.
+         * 
+         * @param font 		 The font that contains the glyph.
+         * @param chr       current character
+         * @param nextChr   next character
+         * @see FTFont::DoRender
+         */
+        void DoRender(FTFont *font,const unsigned int chr,const unsigned int nextChr)
+            { font->DoRender(chr,nextChr,pen); }
+        
+        /**
+         * Expose <code>FTFont::CheckGlyph</code> method to derived classes.
+         *
+         * @param font The font that contains the glyph.
+         * @param chr  character index
+         */
+        void CheckGlyph(FTFont *font,const unsigned int Chr)
+            { font->CheckGlyph(Chr); }
+            
+         /**
+          * Expose the FTFont <code>glyphList</code> to our derived classes.
+          * 
+          * @param font The font to perform the query on.
+          * @param Char The character corresponding to the glyph to query.
+          *
+          * @return A pointer to the glyphList of font.
+          */ 
+         FTGlyphContainer *GetGlyphs(FTFont *font)
+            { return(font->glyphList); }
+            
+         /** 
+          * Expose the FTFont <code>charSize</code> to our derived classes.
+          *
+          * @param The font to perform the query on.
+          *
+          * @return A reference to the charSize object of font.
+          */
+         FTSize &GetCharSize(FTFont *font)
+            { return(font->charSize); }
+}; /* class FTLayout */
+#endif  /* __FTLayout__ */
+
diff --git a/include/FTSimpleLayout.h b/include/FTSimpleLayout.h
new file mode 100644
index 0000000..43aeaa4
--- /dev/null
+++ b/include/FTSimpleLayout.h
@@ -0,0 +1,262 @@
+#ifndef    __FTSimpleLayout__
+#define    __FTSimpleLayout__
+
+#include "FTLayout.h"
+#include "FTBBox.h"
+
+class FTFont;
+
+class FTGL_EXPORT FTSimpleLayout : public FTLayout {
+    public:        
+        /**
+         * Initializes line spacing to 1.0, alignment to 
+         * ALIGN_LEFT and wrap to 100.0
+         */
+        FTSimpleLayout();
+        
+        /**
+         * Get the bounding box for a string.
+         *
+         * @param string    a char string
+         * @param llx       lower left near x coord
+         * @param lly       lower left near y coord
+         * @param llz       lower left near z coord
+         * @param urx       upper right far x coord
+         * @param ury       upper right far y coord
+         * @param urz       upper right far z coord
+         */
+        virtual void BBox( const char* string, float& llx, float& lly, float& llz, float& urx, float& ury, float& urz);
+        
+        /**
+         * Get the bounding box for a string.
+         *
+         * @param string    a wchar_t string
+         * @param llx       lower left near x coord
+         * @param lly       lower left near y coord
+         * @param llz       lower left near z coord
+         * @param urx       upper right far x coord
+         * @param ury       upper right far y coord
+         * @param urz       upper right far z coord
+         */
+        virtual void BBox( const wchar_t* string, float& llx, float& lly, float& llz, float& urx, float& ury, float& urz);
+                        
+        /**
+         * Render a string of characters
+         * 
+         * @param string    'C' style string to be output.   
+         */
+        virtual void Render( const char* string );
+
+        /**
+         * Render a string of characters
+         * 
+         * @param string    wchar_t string to be output.     
+         */
+        virtual void Render( const wchar_t* string );
+        
+        /**
+         * Render a string of characters and distribute extra space amongst
+         * the whitespace regions of the string.
+         *
+         * @param String		'C' style string to output.
+         * @param ExtraSpace  The amount of extra space to add to each run of 
+         *                    whitespace.
+         */
+        void RenderSpace(const char *String,const float ExtraSpace = 0.0)
+            { pen.X(0); pen.Y(0); RenderSpace(String, 0, -1, ExtraSpace); }
+
+        /**
+         * Render a string of characters and distribute extra space amongst
+         * the whitespace regions of the string.
+         *
+         * @param String		wchar_t string to output.
+         * @param ExtraSpace  The amount of extra space to add to each run of 
+         *                    whitespace.
+         */
+        void RenderSpace(const wchar_t *String,const float ExtraSpace = 0.0)
+            { pen.X(0); pen.Y(0); RenderSpace(String, 0, -1, ExtraSpace); }
+
+        typedef enum {ALIGN_LEFT,ALIGN_CENTER,ALIGN_RIGHT,ALIGN_JUST} TextAlignment;
+        
+        /**
+         * Set he font to use for rendering the text.  
+         *
+         * @param fontInit A pointer to the new font.  The font is
+         *                 referenced by this but will not be 
+         *                 disposed of when this is deleted.
+         */
+        void SetFont(FTFont *fontInit) 
+            { currentFont = fontInit; }
+        /**
+         * @return The current font.
+         */
+        FTFont *GetFont() 
+            { return(currentFont); }
+        /**
+         * The maximum line length for formatting text.
+         *
+         * @param LineLength The new line length.
+         */
+        void SetLineLength(const float LineLength)
+            { lineLength = LineLength; }
+            
+        /**
+         * @return The current line length.
+         */
+        float GetLineLength() const
+            { return(lineLength); }
+            
+        /**
+         * The text alignment mode used to distribute
+         * space within a line or rendered text.
+         *
+         * @param Alignment The new alignment mode.
+         */
+        void SetAlignment(const TextAlignment Alignment) 
+            { alignment = Alignment; }
+        /**
+         * @return The text alignment mode.
+         */
+        TextAlignment GetAlignment() const
+            { return(alignment); }
+            
+        /**
+         * Sets the line height.
+         *
+         * @param LineSpacing The height of each line of text expressed as
+         *                    a percentage of the current fonts line height.
+         */
+        void SetLineSpacing(const float LineSpacing) 
+            { lineSpacing = LineSpacing; }
+            
+        /**
+         * @return The line spacing.
+         */
+        float GetLineSpacing() const
+            { return(lineSpacing); }
+   protected:
+        /**
+         * Render a string of characters and distribute extra space amongst
+         * the whitespace regions of the string.  Note that this method
+         * does not reset the pen position before rendering.  This method
+         * provides the impelmentation for other RenderSpace methods and
+         * thus should be overloaded when attempting to overload any
+         * RenderSpace methods.
+         *
+         * @param String	A buffer of wchar_t characters to output.
+         * @param Start    The index of the first character in String to output.
+         * @param Stop     The index of the last character in String to output.
+         * @param ExtraSpace The amount of extra space to distribute amongst
+         *                   the characters.
+         */
+        virtual void RenderSpace(const char *String,const int Start,const int Stop,const float ExtraSpace = 0.0);
+
+        /**
+         * Render a string of characters and distribute extra space amongst
+         * the whitespace regions of the string.  Note that this method
+         * does not reset the pen position before rendering.  This method
+         * provides the impelmentation for other RenderSpace methods and
+         * thus should be overloaded when attempting to overload any
+         * RenderSpace methods.
+         *
+         * @param String	A buffer of wchar_t characters to output.
+         * @param Start    The index of the first character in String to output.
+         * @param Stop     The index of the last character in String to output.
+         * @param ExtraSpace The amount of extra space to distribute amongst
+         *                   the characters.
+         */
+        virtual void RenderSpace(const wchar_t *String,const int Start,const int Stop,const float ExtraSpace = 0.0);
+    private:        
+        /**
+         * Either render a string of characters and wrap lines
+         * longer than a threshold or compute the bounds
+         * of a string of characters when wrapped.  The functionality
+         * of this method is exposed by the BBoxWrapped and
+         * RenderWrapped methods.
+         *
+         * @param Buffer 		wchar_t style string to output.
+         * @param bounds      A pointer to a bounds object.  If non null
+         *                    the bounds of the text when laid out
+         *                    will be stored in bounds.  If null the
+         *                    text will be rendered.
+         */      
+        virtual void WrapText(const char *Buffer,FTBBox *bounds = NULL);
+        
+        /**
+         * Either render a string of characters and wrap lines
+         * longer than a threshold or compute the bounds
+         * of a string of characters when wrapped.  The functionality
+         * of this method is exposed by the BBoxWrapped and
+         * RenderWrapped methods.
+         *
+         * @param Buffer 		wchar_t style string to output.
+         * @param bounds      A pointer to a bounds object.  If non null
+         *                    the bounds of the text when laid out
+         *                    will be stored in bounds.  If null the
+         *                    text will be rendered.
+         */        
+        virtual void WrapText(const wchar_t *Buffer,FTBBox *bounds = NULL);
+
+        /**
+         * A helper method used by WrapText to either output the text or
+         * compute it's bounds.
+         *
+         * @param Buffer   A pointer to an array of character data.
+         * @param StartIdx The index of the first character to process.
+         * @param EndIdx   The index of the last character to process.  If 
+         *                 < 0 then characters will be parsed until a '\0'
+         *                 is encountered.
+         * @param RemainingWidth The amount of extra space left on the line.
+         * @param bounds     A pointer to a bounds object.  If non null the
+         *                   bounds will be initialized or expanded by the 
+         *                   bounds of the line.  If null the text will be 
+         *                   rendered.  If the bounds are invalid (lower > upper)
+         *                   they will be initialized.  Otherwise they
+         *                   will be expanded.
+         */
+        void OutputWrapped(const char *Buffer,const int StartIdx,const int EndIdx,const float RemainingWidth,FTBBox *bounds);
+
+        /**
+         * A helper method used by WrapText to either output the text or
+         * compute it's bounds.
+         *
+         * @param Buffer   A pointer to an array of character data.
+         * @param StartIdx The index of the first character to process.
+         * @param EndIdx   The index of the last character to process.  If 
+         *                 < 0 then characters will be parsed until a '\0'
+         *                 is encountered.
+         * @param RemainingWidth The amount of extra space left on the line.
+         * @param bounds     A pointer to a bounds object.  If non null the
+         *                   bounds will be initialized or expanded by the 
+         *                   bounds of the line.  If null the text will be 
+         *                   rendered.  If the bounds are invalid (lower > upper)
+         *                   they will be initialized.  Otherwise they
+         *                   will be expanded.
+         */
+        void OutputWrapped(const wchar_t *Buffer,const int StartIdx,const int EndIdx,const float RemainingWidth,FTBBox *bounds);
+        
+        /**
+         * The font to use for rendering the text.  The font is
+         * referenced by this but will not be disposed of when this
+         * is deleted.
+         */
+        FTFont          *currentFont;
+        /**
+         * The maximum line length for formatting text.
+         */
+        float				lineLength;
+        
+        /**
+         * The text alignment mode used to distribute
+         * space within a line or rendered text.
+         */
+        TextAlignment 	alignment;
+        
+        /**
+         * The height of each line of text expressed as
+         * a percentage of the fonts line height.
+         */
+        float 				lineSpacing;
+}; /* class FTSimpleLayout */
+#endif  /* __FTSimpleLayout__ */
+
diff --git a/mac/FTGL.pbproj/project.pbxproj b/mac/FTGL.pbproj/project.pbxproj
index ac245fa..fd1cc64 100644
--- a/mac/FTGL.pbproj/project.pbxproj
+++ b/mac/FTGL.pbproj/project.pbxproj
@@ -158,6 +158,8 @@
 				F532B52D0394A92901608C9F,
 				BA79CB0303A176E7002F90D7,
 				BA3F38F603B1D529004F6ED7,
+				BA2329E00440D51B009E9A9E,
+				BA2329E20440D52C009E9A9E,
 			);
 			isa = PBXHeadersBuildPhase;
 			runOnlyForDeploymentPostprocessing = 0;
@@ -187,6 +189,7 @@
 				66BA08D201C4638C000ABFD4,
 				F532B52C0394A92901608C9F,
 				BA79CB0203A176E7002F90D7,
+				BA2329DE0440D502009E9A9E,
 			);
 			isa = PBXSourcesBuildPhase;
 			runOnlyForDeploymentPostprocessing = 0;
@@ -238,6 +241,7 @@
 				66BA08AE01C4638C000ABFD4,
 				66BA08AF01C4638C000ABFD4,
 				66BA08B001C4638C000ABFD4,
+				BA2329E10440D52C009E9A9E,
 				66BA08B101C4638C000ABFD4,
 				66BA08B201C4638C000ABFD4,
 				BA3F38F503B1D529004F6ED7,
@@ -249,6 +253,8 @@
 				F532B52B0394A92901608C9F,
 				66BA08B701C4638C000ABFD4,
 				66BA08B801C4638C000ABFD4,
+				BA2329DD0440D502009E9A9E,
+				BA2329DF0440D51B009E9A9E,
 				66BA08B901C4638C000ABFD4,
 				66BA08BA01C4638C000ABFD4,
 				66BA08BB01C4638C000ABFD4,
@@ -775,6 +781,7 @@
 				66BA08F101C4658D000ABFD4,
 				66BA08F201C4658D000ABFD4,
 				F5D2501F021F19CD019B29E7,
+				BAB31C69043B9C3500B0313F,
 			);
 			isa = PBXSourcesBuildPhase;
 			runOnlyForDeploymentPostprocessing = 0;
@@ -902,6 +909,45 @@
 			settings = {
 			};
 		};
+		BA2329DD0440D502009E9A9E = {
+			fileEncoding = 30;
+			isa = PBXFileReference;
+			name = FTSimpleLayout.cpp;
+			path = ../src/FTSimpleLayout.cpp;
+			refType = 2;
+		};
+		BA2329DE0440D502009E9A9E = {
+			fileRef = BA2329DD0440D502009E9A9E;
+			isa = PBXBuildFile;
+			settings = {
+			};
+		};
+		BA2329DF0440D51B009E9A9E = {
+			fileEncoding = 30;
+			isa = PBXFileReference;
+			name = FTSimpleLayout.h;
+			path = ../include/FTSimpleLayout.h;
+			refType = 2;
+		};
+		BA2329E00440D51B009E9A9E = {
+			fileRef = BA2329DF0440D51B009E9A9E;
+			isa = PBXBuildFile;
+			settings = {
+			};
+		};
+		BA2329E10440D52C009E9A9E = {
+			fileEncoding = 30;
+			isa = PBXFileReference;
+			name = FTLayout.h;
+			path = ../include/FTLayout.h;
+			refType = 2;
+		};
+		BA2329E20440D52C009E9A9E = {
+			fileRef = BA2329E10440D52C009E9A9E;
+			isa = PBXBuildFile;
+			settings = {
+			};
+		};
 		BA3F38F103B1D4FD004F6ED7 = {
 			fileEncoding = 30;
 			isa = PBXFileReference;
@@ -1006,6 +1052,18 @@
 			settings = {
 			};
 		};
+		BAB31C68043B8DEA00B0313F = {
+			fileRef = F580402702C549A2013DAE1F;
+			isa = PBXBuildFile;
+			settings = {
+			};
+		};
+		BAB31C69043B9C3500B0313F = {
+			fileRef = F580402702C549A2013DAE1F;
+			isa = PBXBuildFile;
+			settings = {
+			};
+		};
 		BADB7E0E03AF1241004F6ED7 = {
 			fileRef = 66BA088F01C4635F000ABFD4;
 			isa = PBXBuildFile;
@@ -1636,6 +1694,7 @@
 			buildActionMask = 2147483647;
 			files = (
 				F597CC8F02C5420F01F03BCF,
+				BAB31C68043B8DEA00B0313F,
 			);
 			isa = PBXSourcesBuildPhase;
 			runOnlyForDeploymentPostprocessing = 0;
diff --git a/src/FTFont.cpp b/src/FTFont.cpp
index 6f308cb..974d8c2 100644
--- a/src/FTFont.cpp
+++ b/src/FTFont.cpp
@@ -186,7 +186,8 @@ void FTFont::BBox(const char* string, const int start, const int end,
         }
 
         /* Expand totalBox by each glyph in String (for idx) */
-        for(int i = start + 1; (end < 0 && string[i]) || end >= 0; i++)
+        for(int i = start + 1; (end < 0 && string[i])
+                                 || (end >= 0 && i < end); i++)
         {
             if(CheckGlyph(string[i]))
             {
@@ -227,7 +228,8 @@ void FTFont::BBox(const wchar_t* string, const int start, const int end,
         }
 
         /* Expand totalBox by each glyph in String (for idx) */
-        for(int i = start + 1; (end < 0 && string[i]) || end >= 0; i++)
+        for(int i = start + 1; (end < 0 && string[i])
+                                 || (end >= 0 && i < end); i++)
         {
             if(CheckGlyph(string[i]))
             {
diff --git a/src/FTSimpleLayout.cpp b/src/FTSimpleLayout.cpp
new file mode 100644
index 0000000..37b4e79
--- /dev/null
+++ b/src/FTSimpleLayout.cpp
@@ -0,0 +1,347 @@
+#include <ctype.h>
+
+#include "FTFont.h"
+#include "FTGlyphContainer.h"
+#include "FTBBox.h"
+
+#include "FTSimpleLayout.h"
+
+
+FTSimpleLayout::FTSimpleLayout() {
+   currentFont = NULL;
+   lineLength = 100.0f;
+   alignment = ALIGN_LEFT;
+   lineSpacing = 1.0f;
+} /* FTSimpleLayout::FTSimpleLayout() */
+
+void FTSimpleLayout::BBox(const char *String,float& llx, float& lly, float& llz, float& urx, float& ury, float& urz) {
+   FTBBox bounds; 
+   
+   WrapText(String,&bounds); 
+   llx = bounds.lowerX; lly = bounds.lowerY; llz = bounds.lowerZ;
+   urx = bounds.upperX; ury = bounds.upperY; urz = bounds.upperZ;
+} /* FTSimpleLayout::BBox() */
+
+void FTSimpleLayout::BBox(const wchar_t *String,float& llx, float& lly, float& llz, float& urx, float& ury, float& urz) {
+   FTBBox bounds; 
+   
+   WrapText(String,&bounds); 
+   llx = bounds.lowerX; lly = bounds.lowerY; llz = bounds.lowerZ;
+   urx = bounds.upperX; ury = bounds.upperY; urz = bounds.upperZ;
+} /* FTSimpleLayout::BBox() */
+
+void FTSimpleLayout::Render(const char *String) {
+   pen.X(0.0f); pen.Y(0.0f);
+   WrapText(String,NULL);
+} /* FTSimpleLayout::Render() */
+
+void FTSimpleLayout::Render(const wchar_t* String) {
+   pen.X(0.0f); pen.Y(0.0f);
+   WrapText(String,NULL);
+} /* FTSimpleLayout::Render() */
+
+void FTSimpleLayout::WrapText(const char *Buffer,FTBBox *bounds) {
+   int breakIdx = 0;          // The index of the last break character
+   int lineStart = 0;         // The character index of the line start
+   float nextStart = 0.0;     // The total width of the line being generated
+   float breakWidth = 0.0;    // The width of the line up to the last word break
+   float currentWidth = 0.0;  // The width of all characters on the line being generated
+   float prevWidth;           // The width of all characters but the current glyph
+   float wordLength = 0.0;    // The length of the block since the last break character
+   float glyphWidth,advance;
+   FTBBox glyphBounds;
+   /* Reset the pen position */
+   pen.Y(0);
+   
+   if (bounds) {
+      bounds->Invalidate();
+   } /* If we have bounds mark them invalid (if bounds) */
+   
+   for (int charIdx = 0;Buffer[charIdx];charIdx++) {         
+      /* Find the width of the current glyph */
+      CheckGlyph(currentFont,Buffer[charIdx]);
+      glyphBounds = GetGlyphs(currentFont)->BBox(Buffer[charIdx]);
+      glyphWidth = glyphBounds.upperX - glyphBounds.lowerX;
+      
+      advance = GetGlyphs(currentFont)->Advance(Buffer[charIdx],Buffer[charIdx + 1]);
+      prevWidth = currentWidth;
+      /* Compute the width of all glyphs up to the end of Buffer[charIdx] */
+      currentWidth = nextStart + glyphWidth;
+      /* Compute the position of the next glyph */
+      nextStart += advance;
+      
+      if ((currentWidth > lineLength) || (Buffer[charIdx] == '\n')) {
+         /* A non whitespace character has exceeded the line length.  Or a */
+         /* newline character has forced a line break.  Output the last    */
+         /* line and start a new line after the break character.           */
+         
+         if (!breakIdx || (Buffer[charIdx] == '\n')) {
+            /* Break on the previous character */
+            breakIdx = charIdx - 1;
+            breakWidth = prevWidth;
+            /* None of the previous word will be carried to the next line */
+            wordLength = 0;
+            /* If the current character is a newline discard it's advance */
+            if (Buffer[charIdx] == '\n') advance = 0;
+         } /* If we have not yet found a break break on the last character (if !breakIdx) */
+         
+         float remainingWidth = lineLength - breakWidth;
+
+         /* Render the current substring */
+         if (Buffer[breakIdx + 1] == '\n') {
+            breakIdx++;
+            OutputWrapped(Buffer,lineStart,breakIdx -1,remainingWidth,bounds);
+         } else {
+            OutputWrapped(Buffer,lineStart,breakIdx   ,remainingWidth,bounds);
+         } /* If the break character is a newline do not render it (if Buffer[charIdx]) */
+
+         /* Store the start of the next line */
+         lineStart = breakIdx + 1;
+         // TODO: Is Height() the right value here?
+         pen.Y(pen.Y() - GetCharSize(currentFont).Height()*lineSpacing);
+         /* The current width is the width since the last break */
+         nextStart = wordLength + advance;
+         wordLength += advance;
+         currentWidth = wordLength + advance;
+         /* Reset the safe break for the next line */
+         breakIdx = 0;
+      } else if (isspace(Buffer[charIdx])) {
+         /* This is the last word break position */
+         wordLength = 0;
+         breakIdx = charIdx;
+
+         if (!charIdx || !isspace(Buffer[charIdx - 1])) {
+            /* Record the width of the start of the block */
+            breakWidth = currentWidth;
+         } /* Check to see if this is the first whitespace character in a run (if !isspace()) */
+      } else {
+         wordLength += advance;
+      } /* See if Buffer[charIdx] is a space, a break or a regular charactger (if/elseif/else Buffer[]) */
+   } /* Scan the input for all characters that need output (for charIdx) */
+   
+   float remainingWidth = lineLength - currentWidth;
+   /* Render any remaining text on the last line */
+   if (alignment == ALIGN_JUST) {
+      alignment = ALIGN_LEFT;
+      OutputWrapped(Buffer,lineStart,-1,remainingWidth,bounds);
+      alignment = ALIGN_JUST;
+   } else {
+      OutputWrapped(Buffer,lineStart,-1,remainingWidth,bounds);
+   } /* Disable justification for the last row (if/else Alignemnt) */
+} /* FTSimpleLayout::WrapText() */
+
+void FTSimpleLayout::WrapText(const wchar_t* Buffer,FTBBox *bounds) {
+   int breakIdx = 0;           // The index of the last break character
+   int lineStart = 0;          // The character index of the line start
+   float nextStart = 0.0f;     // The total width of the line being generated
+   float breakWidth = 0.0f;    // The width of the line up to the last word break
+   float currentWidth = 0.0f;  // The width of all characters on the line being generated
+   float prevWidth;            // The width of all characters but the current glyph
+   float wordLength = 0.0f;    // The length of the block since the last break character
+   float glyphWidth,advance;
+   FTBBox glyphBounds;
+   /* Reset the pen position */
+   pen.Y(0);
+   
+   if (bounds) {
+      bounds->Invalidate();
+   } /* If we have bounds mark them invalid (if bounds) */
+   
+   for (int charIdx = 0;Buffer[charIdx];charIdx++) {         
+      /* Find the width of the current glyph */
+      CheckGlyph(currentFont,Buffer[charIdx]);
+      glyphBounds = GetGlyphs(currentFont)->BBox(Buffer[charIdx]);
+      glyphWidth = glyphBounds.upperX - glyphBounds.lowerX;
+      
+      advance = GetGlyphs(currentFont)->Advance(Buffer[charIdx],Buffer[charIdx + 1]);
+      prevWidth = currentWidth;
+      /* Compute the width of all glyphs up to the end of Buffer[charIdx] */
+      currentWidth = nextStart + glyphWidth;
+      /* Compute the position of the next glyph */
+      nextStart += advance;
+      
+      if ((currentWidth > lineLength) || (Buffer[charIdx] == '\n')) {
+         /* A non whitespace character has exceeded the line length.  Or a */
+         /* newline character has forced a line break.  Output the last    */
+         /* line and start a new line after the break character.           */
+         
+         if (!breakIdx || (Buffer[charIdx] == '\n')) {
+            /* Break on the previous character */
+            breakIdx = charIdx - 1;
+            breakWidth = prevWidth;
+            /* None of the previous word will be carried to the next line */
+            wordLength = 0;
+            /* If the current character is a newline discard it's advance */
+            if (Buffer[charIdx] == '\n') advance = 0;
+         } /* If we have not yet found a break break on the last character (if !breakIdx) */
+         
+         float remainingWidth = lineLength - breakWidth;
+
+         /* Render the current substring */
+         if (Buffer[breakIdx + 1] == '\n') {
+            breakIdx++;
+            OutputWrapped(Buffer,lineStart,breakIdx -1,remainingWidth,bounds);
+         } else {
+            OutputWrapped(Buffer,lineStart,breakIdx   ,remainingWidth,bounds);
+         } /* If the break character is a newline do not render it (if Buffer[charIdx]) */
+
+         /* Store the start of the next line */
+         lineStart = breakIdx + 1;
+         // TODO: Is Height() the right value here?
+         pen.Y(pen.Y() - GetCharSize(currentFont).Height()*lineSpacing);
+         /* The current width is the width since the last break */
+         nextStart = wordLength + advance;
+         wordLength += advance;
+         currentWidth = wordLength + advance;
+         /* Reset the safe break for the next line */
+         breakIdx = 0;
+      } else if (isspace(Buffer[charIdx])) {
+         /* This is the last word break position */
+         wordLength = 0;
+         breakIdx = charIdx;
+
+         if (!charIdx || !isspace(Buffer[charIdx - 1])) {
+            /* Record the width of the start of the block */
+            breakWidth = currentWidth;
+         } /* Check to see if this is the first whitespace character in a run (if !isspace()) */
+      } else {
+         wordLength += advance;
+      } /* See if Buffer[charIdx] is a space, a break or a regular charactger (if/elseif/else Buffer[]) */
+   } /* Scan the input for all characters that need output (for charIdx) */
+   
+   float remainingWidth = lineLength - currentWidth;
+   /* Render any remaining text on the last line */
+   if (alignment == ALIGN_JUST) {
+      alignment = ALIGN_LEFT;
+      OutputWrapped(Buffer,lineStart,-1,remainingWidth,bounds);
+      alignment = ALIGN_JUST;
+   } else {
+      OutputWrapped(Buffer,lineStart,-1,remainingWidth,bounds);
+   } /* Disable justification for the last row (if/else Alignemnt) */
+} /* FTSimpleLayout::WrapText() */
+
+void FTSimpleLayout::OutputWrapped(const char *Buffer,const int StartIdx,const int EndIdx,const float RemainingWidth,FTBBox *bounds) {
+   float distributeWidth = 0.0;
+   switch (alignment) {
+      case ALIGN_LEFT:
+         pen.X(0);
+         break;
+      case ALIGN_CENTER:
+         pen.X(RemainingWidth/2);
+         break;
+      case ALIGN_RIGHT:
+         pen.X(RemainingWidth);
+         break;
+      case ALIGN_JUST:
+         pen.X(0);
+         distributeWidth = RemainingWidth;
+         break;
+   } /* Allign the text according as specified by Alignment (switch Alignment) */
+
+   if (bounds) {
+      float llx,lly,llz,urx,ury,urz;
+      currentFont->BBox(Buffer,StartIdx,EndIdx,llx,lly,llz,urx,ury,urz);
+
+      /* Add the extra space to the upper x dimension */
+      urx += distributeWidth;
+      // TODO: It's a little silly to convert from a FTBBox to floats and back again, but I don't want to 
+      //       implement yet another method for finding the bounding box as a BBox.
+      FTBBox temp(llx,lly,llz,urx,ury,urz);
+      temp.Move(FTPoint(pen.X(), pen.Y(), 0.0f));
+      
+      if (!bounds->IsValid()) {
+         *bounds = temp;
+      } else {
+         *bounds += temp;
+      } /* See if this is the first area to be added to the bounds (if/else bounds) */
+   } else {
+      RenderSpace(Buffer,StartIdx,EndIdx,distributeWidth);
+   } /* If we have bounds expand them by the lines bounds, otherwise render the line (if/else bounds) */
+} /* FTSimpleLayout::OutputWrapped() */
+
+void FTSimpleLayout::OutputWrapped(const wchar_t *Buffer,const int StartIdx,const int EndIdx,const float RemainingWidth,FTBBox *bounds) {
+   float distributeWidth = 0.0;
+   switch (alignment) {
+      case ALIGN_LEFT:
+         pen.X(0); 
+         break;
+      case ALIGN_CENTER:
+         pen.X(RemainingWidth/2);
+         break;
+      case ALIGN_RIGHT:
+         pen.X(RemainingWidth);
+         break;
+      case ALIGN_JUST:
+         pen.X(0);
+         distributeWidth = RemainingWidth;
+         break;
+   } /* Allign the text according as specified by Alignment (switch Alignment) */
+
+   if (bounds) {
+      float llx,lly,llz,urx,ury,urz;
+      currentFont->BBox(Buffer,StartIdx,EndIdx,llx,lly,llz,urx,ury,urz);
+
+      /* Add the extra space to the upper x dimension */
+      urx += distributeWidth;
+      // TODO: It's a little silly to convert from a FTBBox to floats and back again, but I don't want to 
+      //       implement yet another method for finding the bounding box as a BBox.
+      FTBBox temp(llx,lly,llz,urx,ury,urz);
+      temp.Move(FTPoint(pen.X(), pen.Y(), 0.0f));
+      
+      if (!bounds->IsValid()) {
+         *bounds = temp;
+      } else {
+         *bounds += temp;
+      } /* See if this is the first area to be added to the bounds (if/else bounds) */
+   } else {
+      RenderSpace(Buffer,StartIdx,EndIdx,distributeWidth);
+   } /* If we have bounds expand them by the lines bounds, otherwise render the line (if/else bounds) */
+} /* FTSimpleLayout::OutputWrapped() */
+
+void FTSimpleLayout::RenderSpace(const char *String,const int StartIdx,const int EndIdx,const float ExtraSpace) {
+   float space = 0.0;
+   
+   if (ExtraSpace > 0.0) {
+      int numSpaces = 0;
+
+      for (int idx = StartIdx;((EndIdx < 0) && String[idx]) || ((EndIdx >= 0) && (idx <= EndIdx));idx++) {
+         if ((idx > StartIdx) && !isspace(String[idx]) && isspace(String[idx - 1])) {
+            numSpaces++;
+         } /* If this is the end of a space block increment the counter (if isspace()) */
+      } /* Count the number of space blocks in the input (for idx) */
+      
+      space = ExtraSpace/numSpaces;
+   } /* If there is space to distribute count the number of spaces (if ExtraSpace) */
+   
+   for (int idx = StartIdx;((EndIdx < 0) && String[idx]) || ((EndIdx >= 0) && (idx <= EndIdx));idx++) {
+      if ((idx > StartIdx) && !isspace(String[idx]) && isspace(String[idx - 1])) {
+         pen.X(pen.X() + space);
+      } /* If this is the end of a space block distribute the extra space into it (if isspace()) */
+
+      DoRender(currentFont,String[idx],String[idx + 1]);
+   } /* Output all characters of the string (for idx) */
+} /* FTSimpleLayout::RenderSpace() */
+
+void FTSimpleLayout::RenderSpace(const wchar_t *String,const int StartIdx,const int EndIdx,const float ExtraSpace) {
+   float space = 0.0;
+   
+   if (ExtraSpace > 0.0) {
+      int numSpaces = 0;
+
+      for (int idx = StartIdx;((EndIdx < 0) && String[idx]) || ((EndIdx >= 0) && (idx <= EndIdx));idx++) {
+         if ((idx > StartIdx) && !isspace(String[idx]) && isspace(String[idx - 1])) {
+            numSpaces++;
+         } /* If this is the end of a space block increment the counter (if isspace()) */
+      } /* Count the number of space blocks in the input (for idx) */
+      
+      space = ExtraSpace/numSpaces;
+   } /* If there is space to distribute count the number of spaces (if ExtraSpace) */
+   
+   for (int idx = StartIdx;((EndIdx < 0) && String[idx]) || ((EndIdx >= 0) && (idx <= EndIdx));idx++) {
+      if ((idx > StartIdx) && !isspace(String[idx]) && isspace(String[idx - 1])) {
+         pen.X(pen.X() + space);
+      } /* If this is the end of a space block distribute the extra space into it (if isspace()) */
+
+      DoRender(currentFont,String[idx],String[idx + 1]);
+   } /* Output all characters of the string (for idx) */
+} /* FTSimpleLayout::RenderSpace() */
diff --git a/src/Makefile.am b/src/Makefile.am
index aa3308e..92a1607 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -22,6 +22,7 @@ libftgl_la_SOURCES = \
 	FTPixmapGlyph.cpp \
 	FTPoint.cpp \
 	FTPolyGlyph.cpp \
+	FTSimpleLayout.cpp \
 	FTSize.cpp \
 	FTTextureGlyph.cpp \
 	FTVectoriser.cpp \
@@ -49,12 +50,14 @@ ftgl_HEADERS = \
 	../include/FTGLTextureFont.h \
 	../include/FTGlyph.h \
 	../include/FTGlyphContainer.h \
+	../include/FTLayout.h \
 	../include/FTLibrary.h \
 	../include/FTList.h \
 	../include/FTOutlineGlyph.h \
 	../include/FTPixmapGlyph.h \
 	../include/FTPoint.h \
 	../include/FTPolyGlyph.h \
+	../include/FTSimpleLayout.h \
 	../include/FTSize.h \
 	../include/FTTextureGlyph.h \
 	../include/FTVector.h \
diff --git a/test/demo.cpp b/test/demo.cpp
index 6e9239a..df51f7b 100644
--- a/test/demo.cpp
+++ b/test/demo.cpp
@@ -1,6 +1,3 @@
-// source changed by mrn@paus.ch/ max rheiner
-// original source: henryj@paradise.net.nz
-
 #include <iostream>
 #include <stdlib.h> // exit()
 
@@ -16,7 +13,7 @@
 #include "FTGLTextureFont.h"
 #include "FTGLPixmapFont.h"
 
-//#include "mmgr.h"
+#include "mmgr.h"
 
 static FTFont* fonts[5];
 static int width;
@@ -25,13 +22,13 @@ static int height;
 
 // YOU'LL PROBABLY WANT TO CHANGE THESE
 #ifdef __linux__
-	#define DEFAULT_FONT "/usr/share/fonts/truetype/arial.ttf"
+#   define DEFAULT_FONT "/usr/share/fonts/truetype/arial.ttf"
 #endif
 #ifdef __APPLE_CC__
-	#define DEFAULT_FONT "/Users/henry/Development/PROJECTS/FTGL/test/font_pack/arial.ttf"
+#   define DEFAULT_FONT "/Users/henry/Development/PROJECTS/FTGL/test/font_pack/arial.ttf"
 #endif
 #ifdef WIN32
-	#define DEFAULT_FONT "C:\\WINNT\\Fonts\\arial.ttf"
+#   define DEFAULT_FONT "C:\\WINNT\\Fonts\\arial.ttf"
 #endif
 
 
@@ -68,97 +65,95 @@ my_init( const char* font_filename )
 static void
 do_ortho()
 {
-  int w;
-  int h;
-  GLdouble size;
-  GLdouble aspect;
-
-  w = width;
-  h = height;
-  aspect = (GLdouble)w / (GLdouble)h;
-
-  // Use the whole window.
-  glViewport(0, 0, w, h);
-
-  // We are going to do some 2-D orthographic drawing.
-  glMatrixMode(GL_PROJECTION);
-  glLoadIdentity();
-  size = (GLdouble)((w >= h) ? w : h) / 2.0;
-  if (w <= h) {
-    aspect = (GLdouble)h/(GLdouble)w;
-    glOrtho(-size, size, -size*aspect, size*aspect,
-            -100000.0, 100000.0);
-  }
-  else {
-    aspect = (GLdouble)w/(GLdouble)h;
-    glOrtho(-size*aspect, size*aspect, -size, size,
-            -100000.0, 100000.0);
-  }
-
-  // Make the world and window coordinates coincide so that 1.0 in
-  // model space equals one pixel in window space.
-  glScaled(aspect, aspect, 1.0);
-
-   // Now determine where to draw things.
-  glMatrixMode(GL_MODELVIEW);
-  glLoadIdentity();
+    int w;
+    int h;
+    GLdouble size;
+    GLdouble aspect;
+    
+    w = width;
+    h = height;
+    aspect = (GLdouble)w / (GLdouble)h;
+    
+    // Use the whole window.
+    glViewport(0, 0, w, h);
+    
+    // We are going to do some 2-D orthographic drawing.
+    glMatrixMode(GL_PROJECTION);
+    glLoadIdentity();
+    size = (GLdouble)((w >= h) ? w : h) / 2.0;
+    if (w <= h) {
+        aspect = (GLdouble)h/(GLdouble)w;
+        glOrtho(-size, size, -size*aspect, size*aspect,
+                -100000.0, 100000.0);
+    }
+    else {
+        aspect = (GLdouble)w/(GLdouble)h;
+        glOrtho(-size*aspect, size*aspect, -size, size,
+                -100000.0, 100000.0);
+    }
+    
+    // Make the world and window coordinates coincide so that 1.0 in
+    // model space equals one pixel in window space.
+    glScaled(aspect, aspect, 1.0);
+    
+    // Now determine where to draw things.
+    glMatrixMode(GL_MODELVIEW);
+    glLoadIdentity();
 }
 
 
 void
 my_reshape(int w, int h)
 {
-  width = w;
-  height = h;
-
-  do_ortho( );
+    width = w;
+    height = h;
+    
+    do_ortho( );
 }
 
 void
 my_handle_key(unsigned char key, int x, int y)
 {
-   switch (key) {
-
-	   //!!ELLERS
-   case 'q':   // Esc or 'q' Quits the program.
-   case 27:    
-	   {
-       for (int i=0; i<5; i++) {
-           if (fonts[i]) {
-               delete fonts[i];
-               fonts[i] = 0;
-           }
-       }
-      exit(1);
-	   }
-      break;
-
-   default:
-      break;
-   }
+    switch (key) {
+        case 'q':   // Esc or 'q' Quits the program.
+        case 27:
+        {
+            for (int i=0; i<5; i++) {
+                if (fonts[i]) {
+                    delete fonts[i];
+                    fonts[i] = 0;
+                }
+            }
+            exit(1);
+        }
+            break;
+        default:
+            break;
+    }
 }
 
 void
 draw_scene()
 {
-   /* Set up some strings with the characters to draw. */
-   unsigned int count = 0;
-   char string[8][256];
-   int i;
-   for (i=1; i < 32; i++) { /* Skip zero - it's the null terminator! */
-      string[0][count] = i;
-      count++;
-   }
-   string[0][count] = '\0';
+    /* Set up some strings with the characters to draw. */
+    unsigned int count = 0;
+    char string[8][256];
+    int i;
+    for (i=1; i < 32; i++) { /* Skip zero - it's the null terminator! */
+        string[0][count] = i;
+        count++;
+    }
 
-   count = 0;
-   for (i=32; i < 64; i++) {
-      string[1][count] = i;
-      count++;
-   }
-   string[1][count] = '\0';
+    string[0][count] = '\0';
 
-   count = 0;
+    count = 0;
+    for (i=32; i < 64; i++) {
+        string[1][count] = i;
+        count++;
+    }
+    string[1][count] = '\0';
+
+    count = 0;
    for (i=64; i < 96; i++) {
       string[2][count] = i;
       count++;
@@ -253,72 +248,72 @@ my_idle()
 int 
 file_exists( const char * fontFilePath )
 {
-	FILE * fp = fopen( fontFilePath, "r" );
-
-	if ( fp == NULL )
-	{
-		// That fopen failed does _not_ definitely mean the file isn't there 
-		// but for now this is ok
-		return 0;
-	}
-	fclose( fp );
-	return 1;
+    FILE * fp = fopen( fontFilePath, "r" );
+
+    if ( fp == NULL )
+    {
+        // That fopen failed does _not_ definitely mean the file isn't there 
+        // but for now this is ok
+        return 0;
+    }
+    fclose( fp );
+    return 1;
 }
 
 void
 usage( const char * program )
 {
-	std::cerr << "Usage: " << program << " <fontFilePath.ttf>\n" << std::endl;
+    std::cerr << "Usage: " << program << " <fontFilePath.ttf>\n" << std::endl;
 }
 
 int
 main(int argc, char **argv)
 {
-	char * fontFilePath;
-
-	glutInitWindowSize(600, 600);
-	glutInit(&argc, argv);
-	glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
-
-	glutCreateWindow("FTGL demo");
-
-	if ( argc >= 2 ) 
-	{
-		if ( !file_exists( argv[ 1 ] ))
-		{
-			usage( argv[ 0 ]);
-			std::cerr << "Couldn't open file '" << argv[ 1 ] << "'" << std::endl;
-			exit( -1 );
-		}
-		fontFilePath = argv[ 1 ];
-	}
-	else 
-	{
-		// try a default font
-		fontFilePath = DEFAULT_FONT;
-
-		if ( !file_exists( fontFilePath ))
-		{
-			usage( argv[ 0 ]);
-			std::cerr << "Couldn't open default file '" << fontFilePath << "'" << std::endl;
-			exit( -1 );
-		}
-	}
-
-	my_init( fontFilePath );
-
-	glutDisplayFunc(my_display);
-	glutReshapeFunc(my_reshape);
-	glutIdleFunc(my_idle);
-	glutKeyboardFunc(my_handle_key);
-
-	glutMainLoop();
-        
-        for(int x = 0; x < 5; ++x)
+    char * fontFilePath;
+
+    glutInitWindowSize(600, 600);
+    glutInit(&argc, argv);
+    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
+
+    glutCreateWindow("FTGL demo");
+
+    if ( argc >= 2 ) 
+    {
+        if ( !file_exists( argv[ 1 ] ))
+        {
+            usage( argv[ 0 ]);
+            std::cerr << "Couldn't open file '" << argv[ 1 ] << "'" << std::endl;
+            exit( -1 );
+        }
+        fontFilePath = argv[ 1 ];
+    }
+    else 
+    {
+        // try a default font
+        fontFilePath = DEFAULT_FONT;
+
+        if ( !file_exists( fontFilePath ))
         {
-            delete fonts[x];
+            usage( argv[ 0 ]);
+            std::cerr << "Couldn't open default file '" << fontFilePath << "'" << std::endl;
+            exit( -1 );
         }
+    }
+
+    my_init( fontFilePath );
+
+    glutDisplayFunc(my_display);
+    glutReshapeFunc(my_reshape);
+    glutIdleFunc(my_idle);
+    glutKeyboardFunc(my_handle_key);
+
+    glutMainLoop();
+        
+    for(int x = 0; x < 5; ++x)
+    {
+        delete fonts[x];
+    }
         
-	return 0;
+    return 0;
 }