Commit c261e7bdb44995b1f3f6f5de6942ba56a92a6064

Frank Heckenbach 2019-02-07T22:03:32

implement LegacyOpenGLState, see README-LegacyOpenGLState

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
diff --git a/ChangeLog b/ChangeLog
index 6630c7a..ac154f7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,19 +1,37 @@
+2019-02-07 21:05  Frank Heckenbach <f.heckenbach@fh-soft.de>
+
+  * src/FTGL/FTLibrary.h, src/FTLibrary.cpp:
+    new setting LegacyOpenGLState(), see README-LegacyOpenGLState
+
+  * README-LegacyOpenGLState: new file
+
+  * src/FTLibrary.h: move to src/FTGL
+  * src/FTGL/ftgl.h: include FTLibrary.h
+  * src/Makefile.am, src/CMakeLists.txt: add FTGL/FTLibrary.h
+
+  * src/FTFont/FTBitmapFont.cpp,
+    src/FTFont/FTBufferFont.cpp,
+    src/FTFont/FTOutlineFont.cpp,
+    src/FTFont/FTPixmapFont.cpp,
+    src/FTFont/FTTextureFont.cpp:
+    use FTLibrary::Instance().GetLegacyOpenGLStateSet()
+
 2018-11-21 01:45  Frank Heckenbach <f.heckenbach@fh-soft.de>
 
-	* NEWS, configure.ac, ppa_upload.sh, msvc/config.h, CMakeLists.txt:
-	  * Mark package as being version 2.3.1.
+  * NEWS, configure.ac, ppa_upload.sh, msvc/config.h, CMakeLists.txt:
+    * Mark package as being version 2.3.1.
 
 2018-11-18 01:00  Frank Heckenbach <f.heckenbach@fh-soft.de>
 
-	* configure.ac: find FT2 via PKG_CHECK_MODULES instead of freetype2.m4
-	* Makefile.am: remove reference to m4/freetype2.m4
-	* m4/freetype2.m4: removed
+  * configure.ac: find FT2 via PKG_CHECK_MODULES instead of freetype2.m4
+  * Makefile.am: remove reference to m4/freetype2.m4
+  * m4/freetype2.m4: removed
 
 2018-10-14 21:17  Frank Heckenbach <f.heckenbach@fh-soft.de>
 
-	* NEWS, configure.ac, ppa_upload.sh, msvc/config.h, CMakeLists.txt,
-	  debian/rules:
-	  * Mark package as being version 2.3.0.
+  * NEWS, configure.ac, ppa_upload.sh, msvc/config.h, CMakeLists.txt,
+    debian/rules:
+    * Mark package as being version 2.3.0.
 
 2016-10-19 20:38  Richard Ulrich <richi@paraeasy.ch>
 
diff --git a/README-LegacyOpenGLState b/README-LegacyOpenGLState
new file mode 100644
index 0000000..1ef4746
--- /dev/null
+++ b/README-LegacyOpenGLState
@@ -0,0 +1,86 @@
+TL/DR: Skip to "WORK-AROUND".
+
+PROBLEM
+=======
+
+Unfortunately, FTGL has contained two strictly incompatible
+behaviours for a long time:
+
+The initial library set some OpenGL state, e.g. glEnable(GL_BLEND)
+and glBlendFunc(), from the rendering functions. Not only may this
+not be what the user wants, the blending function used is also not
+quite correct. See
+https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=742469) for a
+detailed explanation and example.
+
+A fix made by sammy in 2009, see
+https://github.com/frankheckenbach/ftgl/commit/29603ae3fa88c5b9e079a6db23be2cdea95aef39
+removed these settings, but it was never picked up by Debian and
+possibly other distributions.
+
+As a result, programs assuming the new behaviour would behave
+wrongly without the fix. This cannot be changed from within those
+programs, i.e. outside of the library, because the library without
+the fix would override the settings.
+
+OTOH, programs built against the original version would assume these
+settings and not render correctly with the fix applied. These could
+be fixed in principle by making the settings from within the
+programs, but after almost 10 years of ignoring the problem, some of
+those programs have apparently become unmaintained, or maintained
+just to the bare minimum, so expecting them all to fix the issue
+seems unrealistic.
+
+So as a kludge, version 2.4 of FTGL adds a new flag to choose the
+behaviour, as described under "WORK-AROUND" (not a real solution,
+but the best we can realistically get).
+
+To avoid mismatch between the two behaviours, the library will only
+allow to set this flag to the same value (but any number of times)
+within one program run. So, e.g. if your code includes some
+libraries that in turn use FTGL, and they all set this flag to
+false, all is fine. But if some of them set it to false, and some to
+true, things can't work, as described above. To avoid confusion, on
+an attempt to set the flag to a different value than was set before,
+FTGL will throw an exception (a std::logic_error which should not be
+caught in general usually, neither here in particular).
+
+
+WORK-AROUND
+===========
+
+The handling of OpenGL state should now be set with
+
+  FTLibrary::Instance().LegacyOpenGLState(On);
+
+You should add a call to this function near the start of all your
+programs that use FTGL, and in some strategic place in all libraries
+that use FTGL.
+
+If your code relies on FTGL automatically setting some OpenGL state,
+like the versions in Debian and possibly other distributions do,
+pass "true" for the "On" parameter. If you don't call
+LegacyOpenGLState, this is the default behaviour for now, but in the
+future, FTGL will warn about it and finally fail.
+
+If you need manual control over the OpenGL state, like the versions
+on GitHub do, pass "false".
+
+Even if you use the legacy version ("true"), you might want to
+consider changing to manual setting ("false"). To retain the old
+behaviour then, make the following OpenGL calls from your code,
+either globally, or before each respective rendering, possibly
+protected with glPushAttrib/glPopAttrib:
+
+  glEnable(GL_BLEND);
+  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+
+To get a better rendering function, instead add:
+
+  #define GL_GLEXT_PROTOTYPES
+
+before including OpenGL headers, and then do:
+
+  glEnable(GL_BLEND);
+  glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
+                      GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
diff --git a/debian/copyright_hints b/debian/copyright_hints
index ad088aa..f2516df 100644
--- a/debian/copyright_hints
+++ b/debian/copyright_hints
@@ -180,7 +180,7 @@ Files: ./demo/FTGLDemo.cpp
  ./src/FTFace.h
  ./src/FTGlyphContainer.h
  ./src/FTLibrary.cpp
- ./src/FTLibrary.h
+ ./src/FTGL/FTLibrary.h
  ./src/FTList.h
  ./src/FTSize.cpp
  ./src/FTSize.h
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 123d470..693e49f 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -39,6 +39,7 @@ SET(libftgl_la_SOURCES
 
 SET(ftgl_headers
     FTGL/ftgl.h
+    FTGL/FTLibrary.h
     FTGL/FTBBox.h
     FTGL/FTBuffer.h
     FTGL/FTPoint.h
diff --git a/src/FTFace.cpp b/src/FTFace.cpp
index eb6ffff..f9f1a9f 100644
--- a/src/FTFace.cpp
+++ b/src/FTFace.cpp
@@ -28,7 +28,7 @@
 
 #include "FTFace.h"
 #include "FTCleanup.h"
-#include "FTLibrary.h"
+#include "FTGL/FTLibrary.h"
 
 #include FT_TRUETYPE_TABLES_H
 
diff --git a/src/FTFont/FTBitmapFont.cpp b/src/FTFont/FTBitmapFont.cpp
index 1bb774c..78d898d 100644
--- a/src/FTFont/FTBitmapFont.cpp
+++ b/src/FTFont/FTBitmapFont.cpp
@@ -30,6 +30,7 @@
 
 #include "FTInternals.h"
 #include "FTBitmapFontImpl.h"
+#include "FTGL/FTLibrary.h"
 
 
 //
@@ -68,6 +69,14 @@ inline FTPoint FTBitmapFontImpl::RenderI(const T* string, const int len,
                                          FTPoint position, FTPoint spacing,
                                          int renderMode)
 {
+    bool LegacyOpenGLState = FTLibrary::Instance().GetLegacyOpenGLStateSet();
+    if(LegacyOpenGLState)
+      {
+        // Protect GL_BLEND
+        glPushAttrib(GL_COLOR_BUFFER_BIT);
+        glDisable(GL_BLEND);
+      }
+
     // Protect glPixelStorei() calls (also in FTBitmapGlyphImpl::RenderImpl)
     glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
 
@@ -79,6 +88,8 @@ inline FTPoint FTBitmapFontImpl::RenderI(const T* string, const int len,
 
     glPopClientAttrib();
 
+    if (LegacyOpenGLState)
+      glPopAttrib();
     return tmp;
 }
 
diff --git a/src/FTFont/FTBufferFont.cpp b/src/FTFont/FTBufferFont.cpp
index d8175a3..ce04cf5 100644
--- a/src/FTFont/FTBufferFont.cpp
+++ b/src/FTFont/FTBufferFont.cpp
@@ -31,6 +31,7 @@
 
 #include "FTInternals.h"
 #include "FTBufferFontImpl.h"
+#include "FTGL/FTLibrary.h"
 
 
 //
@@ -230,12 +231,33 @@ inline FTPoint FTBufferFontImpl::RenderI(const T* string, const int len,
     int cacheIndex = -1;
     bool inCache = false;
 
-    // Protect blending functions and GL_TEXTURE_2D
+    // Protect blending functions, GL_TEXTURE_2D and optionally GL_BLEND
     glPushAttrib(GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT | GL_TEXTURE_ENV_MODE);
 
     // Protect glPixelStorei() calls
     glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
 
+    if(FTLibrary::Instance().GetLegacyOpenGLStateSet())
+      {
+        glEnable(GL_BLEND);
+        /*
+         * Note: This is the historic legacy behaviour.
+         *
+         * A better blending function (see
+         * https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=742469) is:
+         *
+         *   glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
+         *                       GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
+         *
+         * To use it, set
+         *
+         *   FTLibrary::Instance().LegacyOpenGLState(false);
+         *
+         * and set GL_BLEND and the blending function yourself.
+         */
+        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+      }
+
     glEnable(GL_TEXTURE_2D);
     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
 
diff --git a/src/FTFont/FTOutlineFont.cpp b/src/FTFont/FTOutlineFont.cpp
index 0b21a1f..c2b543e 100644
--- a/src/FTFont/FTOutlineFont.cpp
+++ b/src/FTFont/FTOutlineFont.cpp
@@ -30,6 +30,7 @@
 
 #include "FTInternals.h"
 #include "FTOutlineFontImpl.h"
+#include "FTGL/FTLibrary.h"
 
 
 //
@@ -93,10 +94,31 @@ inline FTPoint FTOutlineFontImpl::RenderI(const T* string, const int len,
                                           FTPoint position, FTPoint spacing,
                                           int renderMode)
 {
-    // Protect GL_TEXTURE_2D, glHint() and GL_LINE_SMOOTH
+    // Protect GL_TEXTURE_2D, glHint(), GL_LINE_SMOOTH and optionally GL_BLEND
     glPushAttrib(GL_ENABLE_BIT | GL_HINT_BIT | GL_LINE_BIT
                   | GL_COLOR_BUFFER_BIT);
 
+    if(FTLibrary::Instance().GetLegacyOpenGLStateSet())
+      {
+        glEnable(GL_BLEND);
+        /*
+         * Note: This is the historic legacy behaviour.
+         *
+         * A better blending function (see
+         * https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=742469) is:
+         *
+         *   glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
+         *                       GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
+         *
+         * To use it, set
+         *
+         *   FTLibrary::Instance().LegacyOpenGLState(false);
+         *
+         * and set GL_BLEND and the blending function yourself.
+         */
+        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+      }
+
     glDisable(GL_TEXTURE_2D);
     glEnable(GL_LINE_SMOOTH);
     glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
diff --git a/src/FTFont/FTPixmapFont.cpp b/src/FTFont/FTPixmapFont.cpp
index 7f9c623..bfa2a7c 100644
--- a/src/FTFont/FTPixmapFont.cpp
+++ b/src/FTFont/FTPixmapFont.cpp
@@ -30,6 +30,7 @@
 
 #include "FTInternals.h"
 #include "FTPixmapFontImpl.h"
+#include "FTGL/FTLibrary.h"
 
 
 //
@@ -84,7 +85,7 @@ inline FTPoint FTPixmapFontImpl::RenderI(const T* string, const int len,
                                          FTPoint position, FTPoint spacing,
                                          int renderMode)
 {
-    // Protect GL_TEXTURE_2D and glPixelTransferf()
+    // Protect GL_TEXTURE_2D, glPixelTransferf() and optionally GL_BLEND
     glPushAttrib(GL_ENABLE_BIT | GL_PIXEL_MODE_BIT | GL_COLOR_BUFFER_BIT
                   | GL_POLYGON_BIT);
 
@@ -94,6 +95,27 @@ inline FTPoint FTPixmapFontImpl::RenderI(const T* string, const int len,
     // Needed on OSX
     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
 
+    if(FTLibrary::Instance().GetLegacyOpenGLStateSet())
+      {
+        glEnable(GL_BLEND);
+        /*
+         * Note: This is the historic legacy behaviour.
+         *
+         * A better blending function (see
+         * https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=742469) is:
+         *
+         *   glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
+         *                       GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
+         *
+         * To use it, set
+         *
+         *   FTLibrary::Instance().LegacyOpenGLState(false);
+         *
+         * and set GL_BLEND and the blending function yourself.
+         */
+        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+      }
+
     glDisable(GL_TEXTURE_2D);
 
     GLfloat ftglColour[4];
diff --git a/src/FTFont/FTTextureFont.cpp b/src/FTFont/FTTextureFont.cpp
index 14fe3ff..97e8768 100644
--- a/src/FTFont/FTTextureFont.cpp
+++ b/src/FTFont/FTTextureFont.cpp
@@ -35,6 +35,7 @@
 
 #include "../FTGlyph/FTTextureGlyphImpl.h"
 #include "./FTTextureFontImpl.h"
+#include "FTGL/FTLibrary.h"
 
 
 //
@@ -239,9 +240,30 @@ inline FTPoint FTTextureFontImpl::RenderI(const T* string, const int len,
                                           FTPoint position, FTPoint spacing,
                                           int renderMode)
 {
-    // Protect GL_TEXTURE_2D
+    // Protect GL_TEXTURE_2D and optionally GL_BLEND
     glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_TEXTURE_ENV_MODE);
 
+    if(FTLibrary::Instance().GetLegacyOpenGLStateSet())
+      {
+        glEnable(GL_BLEND);
+        /*
+         * Note: This is the historic legacy behaviour.
+         *
+         * A better blending function (see
+         * https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=742469) is:
+         *
+         *   glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
+         *                       GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
+         *
+         * To use it, set
+         *
+         *   FTLibrary::Instance().LegacyOpenGLState(false);
+         *
+         * and set GL_BLEND and the blending function yourself.
+         */
+        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+      }
+
     glEnable(GL_TEXTURE_2D);
     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
 
diff --git a/src/FTGL/FTLibrary.h b/src/FTGL/FTLibrary.h
new file mode 100644
index 0000000..d6641b2
--- /dev/null
+++ b/src/FTGL/FTLibrary.h
@@ -0,0 +1,142 @@
+/*
+ * FTGL - OpenGL font library
+ *
+ * Copyright (c) 2001-2004 Henry Maddocks <ftgl@opengl.geek.nz>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * 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.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef     __FTLibrary__
+#define     __FTLibrary__
+
+#ifdef __cplusplus
+
+#include <ft2build.h>
+#include FT_FREETYPE_H
+//#include FT_CACHE_H
+
+#include "FTGL/ftgl.h"
+#include <atomic>
+
+/**
+ * FTLibrary class is the global accessor for the Freetype library.
+ *
+ * This class encapsulates the Freetype Library. This is a singleton class
+ * and ensures that only one FT_Library is in existence at any one time.
+ * All constructors are private therefore clients cannot create or
+ * instantiate this class themselves and must access it's methods via the
+ * static <code>FTLibrary::Instance()</code> function.
+ *
+ * Just because this class returns a valid <code>FTLibrary</code> object
+ * doesn't mean that the Freetype Library has been successfully initialised.
+ * Clients should check for errors. You can initialise the library AND check
+ * for errors using the following code...
+ * <code>err = FTLibrary::Instance().Error();</code>
+ *
+ * @see "Freetype 2 Documentation"
+ *
+ */
+class FTLibrary
+{
+    public:
+        /**
+         * Global acces point to the single FTLibrary object.
+         *
+         * @return  The global <code>FTLibrary</code> object.
+         */
+        static FTLibrary& Instance();
+
+        /**
+         * Gets a pointer to the native Freetype library.
+         *
+         * @return A handle to a FreeType library instance.
+         */
+        const FT_Library* const GetLibrary() const { return library; }
+
+        /**
+         * Queries the library for errors.
+         *
+         * @return  The current error code.
+         */
+        FT_Error Error() const { return err; }
+
+        /**
+         * Destructor
+         *
+         * Disposes of the Freetype library
+         */
+        ~FTLibrary();
+
+        /**
+         * See README-LegacyOpenGLState
+         *
+         * Choose incompatible legacy behaviour, see commit
+         * 29603ae3fa88c5b9e079a6db23be2cdea95aef39.
+         *
+         * May only be set to the same value (but any number of times)
+         * within one program.
+         */
+        void LegacyOpenGLState(bool On);
+        bool GetLegacyOpenGLStateSet() const { return LegacyOpenGLStateHandling; }
+
+    private:
+        /**
+         * Default constructors.
+         *
+         * Made private to stop clients creating their own FTLibrary
+         * objects.
+         */
+        FTLibrary();
+        FTLibrary(const FT_Library&){}
+        FTLibrary& operator=(const FT_Library&) { return *this; }
+
+        /**
+         * Initialises the Freetype library
+         *
+         * Even though this function indicates success via the return value,
+         * clients can't see this so must check the error codes. This function
+         * is only ever called by the default c_stor
+         *
+         * @return  <code>true</code> if the Freetype library was
+         *          successfully initialised, <code>false</code>
+         *          otherwise.
+         */
+        bool Initialise();
+
+        /**
+         * Freetype library handle.
+         */
+        FT_Library* library;
+//      FTC_Manager* manager;
+
+        /**
+         * Current error code. Zero means no error.
+         */
+        FT_Error err;
+
+        /**
+         * Flag set by LegacyOpenGLState, -1 means implicitly on (default).
+         */
+        std::atomic <int> LegacyOpenGLStateHandling;
+};
+
+#endif //__cplusplus
+
+#endif  //  __FTLibrary__
diff --git a/src/FTGL/ftgl.h b/src/FTGL/ftgl.h
index 97bb0e2..6080055 100644
--- a/src/FTGL/ftgl.h
+++ b/src/FTGL/ftgl.h
@@ -134,6 +134,8 @@ namespace FTGL
     #define FTGL_EXPORT
 #endif
 
+#include <FTGL/FTLibrary.h>
+
 #include <FTGL/FTPoint.h>
 #include <FTGL/FTBBox.h>
 #include <FTGL/FTBuffer.h>
diff --git a/src/FTLibrary.cpp b/src/FTLibrary.cpp
index 225f469..55c55f9 100644
--- a/src/FTLibrary.cpp
+++ b/src/FTLibrary.cpp
@@ -25,10 +25,11 @@
 
 #include "config.h"
 
-#include "FTLibrary.h"
+#include "FTGL/FTLibrary.h"
 #include "FTCleanup.h"
+#include <stdexcept>
 
-const FTLibrary& FTLibrary::Instance()
+FTLibrary& FTLibrary::Instance()
 {
     static FTLibrary ftlib;
     return ftlib;
@@ -51,7 +52,8 @@ FTLibrary::~FTLibrary()
 
 FTLibrary::FTLibrary()
 :   library(0),
-    err(0)
+    err(0),
+    LegacyOpenGLStateHandling(-1)
 {
     Initialise();
 }
@@ -76,3 +78,12 @@ bool FTLibrary::Initialise()
 
     return true;
 }
+
+
+void FTLibrary::LegacyOpenGLState(bool On)
+{
+  int Old = LegacyOpenGLStateHandling.exchange(On);
+  if (Old >= 0 && Old != On)
+    throw std::logic_error
+      ("FTGL: inconsistent LegacyOpenGLState setting, see README-LegacyOpenGLState");
+}
diff --git a/src/FTLibrary.h b/src/FTLibrary.h
deleted file mode 100644
index b99aba2..0000000
--- a/src/FTLibrary.h
+++ /dev/null
@@ -1,122 +0,0 @@
-/*
- * FTGL - OpenGL font library
- *
- * Copyright (c) 2001-2004 Henry Maddocks <ftgl@opengl.geek.nz>
- *
- * Permission is hereby granted, free of charge, to any person obtaining
- * a copy of this software and associated documentation files (the
- * "Software"), to deal in the Software without restriction, including
- * without limitation the rights to use, copy, modify, merge, publish,
- * 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.
- * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
- * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
- * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
- * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
-
-#ifndef     __FTLibrary__
-#define     __FTLibrary__
-
-#include <ft2build.h>
-#include FT_FREETYPE_H
-//#include FT_CACHE_H
-
-#include "FTGL/ftgl.h"
-
-
-/**
- * FTLibrary class is the global accessor for the Freetype library.
- *
- * This class encapsulates the Freetype Library. This is a singleton class
- * and ensures that only one FT_Library is in existence at any one time.
- * All constructors are private therefore clients cannot create or
- * instantiate this class themselves and must access it's methods via the
- * static <code>FTLibrary::Instance()</code> function.
- *
- * Just because this class returns a valid <code>FTLibrary</code> object
- * doesn't mean that the Freetype Library has been successfully initialised.
- * Clients should check for errors. You can initialise the library AND check
- * for errors using the following code...
- * <code>err = FTLibrary::Instance().Error();</code>
- *
- * @see "Freetype 2 Documentation"
- *
- */
-class FTLibrary
-{
-    public:
-        /**
-         * Global acces point to the single FTLibrary object.
-         *
-         * @return  The global <code>FTLibrary</code> object.
-         */
-        static const FTLibrary& Instance();
-
-        /**
-         * Gets a pointer to the native Freetype library.
-         *
-         * @return A handle to a FreeType library instance.
-         */
-        const FT_Library* const GetLibrary() const { return library; }
-
-        /**
-         * Queries the library for errors.
-         *
-         * @return  The current error code.
-         */
-        FT_Error Error() const { return err; }
-
-        /**
-         * Destructor
-         *
-         * Disposes of the Freetype library
-         */
-        ~FTLibrary();
-
-    private:
-        /**
-         * Default constructors.
-         *
-         * Made private to stop clients creating their own FTLibrary
-         * objects.
-         */
-        FTLibrary();
-        FTLibrary(const FT_Library&){}
-        FTLibrary& operator=(const FT_Library&) { return *this; }
-
-        /**
-         * Initialises the Freetype library
-         *
-         * Even though this function indicates success via the return value,
-         * clients can't see this so must check the error codes. This function
-         * is only ever called by the default c_stor
-         *
-         * @return  <code>true</code> if the Freetype library was
-         *          successfully initialised, <code>false</code>
-         *          otherwise.
-         */
-        bool Initialise();
-
-        /**
-         * Freetype library handle.
-         */
-        FT_Library* library;
-//      FTC_Manager* manager;
-
-        /**
-         * Current error code. Zero means no error.
-         */
-        FT_Error err;
-
-};
-
-#endif  //  __FTLibrary__
diff --git a/src/Makefile.am b/src/Makefile.am
index 1302375..949d7d7 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -48,6 +48,7 @@ ftgl_HEADERS = $(ftgl_headers)
 
 ftgl_headers = \
     FTGL/ftgl.h \
+    FTGL/FTLibrary.h \
     FTGL/FTBBox.h \
     FTGL/FTBuffer.h \
     FTGL/FTPoint.h \
diff --git a/test/.FTLibrary-Test.cpp.swp b/test/.FTLibrary-Test.cpp.swp
deleted file mode 100644
index 28fdf84..0000000
Binary files a/test/.FTLibrary-Test.cpp.swp and /dev/null differ