File: ft-text-renderer.cc

package info (click to toggle)
octave 6.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 124,192 kB
  • sloc: cpp: 322,665; ansic: 68,088; fortran: 20,980; objc: 8,121; sh: 7,719; yacc: 4,266; lex: 4,123; perl: 1,530; java: 1,366; awk: 1,257; makefile: 424; xml: 147
file content (1558 lines) | stat: -rw-r--r-- 43,219 bytes parent folder | download
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
////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2009-2021 The Octave Project Developers
//
// See the file COPYRIGHT.md in the top-level directory of this
// distribution or <https://octave.org/copyright/>.
//
// This file is part of Octave.
//
// Octave is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Octave is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Octave; see the file COPYING.  If not, see
// <https://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////

#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include "base-text-renderer.h"
#include "ft-text-renderer.h"

#if defined (HAVE_FREETYPE)

#if defined (HAVE_PRAGMA_GCC_DIAGNOSTIC)
#  pragma GCC diagnostic push
#  pragma GCC diagnostic ignored "-Wold-style-cast"
#endif

#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_H

#if defined (HAVE_FONTCONFIG)
#  include <fontconfig/fontconfig.h>
#endif

#if defined (HAVE_PRAGMA_GCC_DIAGNOSTIC)
#  pragma GCC diagnostic pop
#endif

#include <clocale>
#include <cwchar>
#include <map>
#include <utility>

#include "singleton-cleanup.h"
#include "unistr-wrappers.h"

#include "defaults.h"
#include "error.h"
#include "file-ops.h"
#include "oct-env.h"
#include "pr-output.h"
#include "text-renderer.h"

namespace octave
{
  // FIXME: maybe issue at most one warning per glyph/font/size/weight
  //        combination.

  static void
  warn_missing_glyph (FT_ULong c)
  {
    warning_with_id ("Octave:missing-glyph",
                     "text_renderer: skipping missing glyph for character '%lx'", c);
  }

  static void
  warn_glyph_render (FT_ULong c)
  {
    warning_with_id ("Octave:glyph-render",
                     "text_renderer: unable to render glyph for character '%lx'", c);
  }

#if defined (_MSC_VER)
  // FIXME: is this really needed?
  //
  // This is just a trick to avoid multiple symbol definitions.
  // PermMatrix.h contains a dllexport'ed Array<octave_idx_type>
  // that will cause MSVC not to generate a new instantiation and
  // use the imported one instead.
#  include "PermMatrix.h"
#endif

  // Forward declaration
  static void ft_face_destroyed (void *object);

  class
  ft_manager
  {
  private:

    ft_manager (void)
      : library (), freetype_initialized (false), fontconfig_initialized (false)
    {
      if (FT_Init_FreeType (&library))
        error ("unable to initialize FreeType library");
      else
        freetype_initialized = true;

#if defined (HAVE_FONTCONFIG)
      if (! FcInit ())
        error ("unable to initialize fontconfig library");
      else
        fontconfig_initialized = true;
#endif
    }

  public:

    // No copying!

    ft_manager (const ft_manager&) = delete;

    ft_manager& operator = (const ft_manager&) = delete;

  private:

    ~ft_manager (void)
    {
      if (freetype_initialized)
        FT_Done_FreeType (library);

#if defined (HAVE_FONTCONFIG)
      // FIXME: Skip the call to FcFini because it can trigger the assertion
      //
      //   octave: fccache.c:507: FcCacheFini: Assertion 'fcCacheChains[i] == ((void *)0)' failed.
      //
      // if (fontconfig_initialized)
      //   FcFini ();
#endif
    }

  public:

    static bool instance_ok (void)
    {
      bool retval = true;

      if (! instance)
        {
          instance = new ft_manager ();
          singleton_cleanup_list::add (cleanup_instance);
        }

      return retval;
    }

    static void cleanup_instance (void) { delete instance; instance = nullptr; }

    static FT_Face get_font (const std::string& name, const std::string& weight,
                             const std::string& angle, double size)
    {
      return (instance_ok ()
              ? instance->do_get_font (name, weight, angle, size)
              : nullptr);
    }

    static octave_map get_system_fonts (void)
    {
      return (instance_ok ()
              ? instance->do_get_system_fonts ()
              : octave_map ());
    }

    static void font_destroyed (FT_Face face)
    {
      if (instance_ok ())
        instance->do_font_destroyed (face);
    }

  private:

    static ft_manager *instance;

    typedef std::pair<std::string, double> ft_key;
    typedef std::map<ft_key, FT_Face> ft_cache;

    // Cache the fonts loaded by FreeType.  This cache only contains
    // weak references to the fonts, strong references are only present
    // in class text_renderer.
    ft_cache cache;

    static octave_map do_get_system_fonts (void)
    {
      static octave_map font_map;

      if (font_map.isempty ())
        {
#if defined (HAVE_FONTCONFIG)
          FcConfig *config = FcConfigGetCurrent();
          FcPattern *pat = FcPatternCreate ();
          FcObjectSet *os = FcObjectSetBuild (FC_FAMILY, FC_SLANT, FC_WEIGHT,
                                              FC_CHARSET, nullptr);
          FcFontSet *fs = FcFontList (config, pat, os);

          if (fs->nfont > 0)
            {
              // Mark fonts that have at least all printable ASCII chars
              FcCharSet *minimal_charset =  FcCharSetCreate ();
              for (int i = 32; i < 127; i++)
                FcCharSetAddChar (minimal_charset, static_cast<FcChar32> (i));

              string_vector fields (4);
              fields(0) = "family";
              fields(1) = "angle";
              fields(2) = "weight";
              fields(3) = "suitable";

              dim_vector dv (1, fs->nfont);
              Cell families (dv);
              Cell angles (dv);
              Cell weights (dv);
              Cell suitable (dv);

              unsigned char *family;
              int val;
              for (int i = 0; fs && i < fs->nfont; i++)
                {
                  FcPattern *font = fs->fonts[i];
                  if (FcPatternGetString (font, FC_FAMILY, 0, &family)
                      == FcResultMatch)
                    families(i) = std::string (reinterpret_cast<char*> (family));
                  else
                    families(i) = "unknown";

                  if (FcPatternGetInteger (font, FC_SLANT, 0, &val)
                      == FcResultMatch)
                    angles(i) = (val == FC_SLANT_ITALIC
                                 || val == FC_SLANT_OBLIQUE)
                                ? "italic" : "normal";
                  else
                    angles(i) = "unknown";

                  if (FcPatternGetInteger (font, FC_WEIGHT, 0, &val)
                      == FcResultMatch)
                    weights(i) = (val == FC_WEIGHT_BOLD
                                  || val == FC_WEIGHT_DEMIBOLD)
                                 ? "bold" : "normal";
                  else
                    weights(i) = "unknown";

                  FcCharSet *cset;
                  if (FcPatternGetCharSet (font, FC_CHARSET, 0, &cset)
                      == FcResultMatch)
                    suitable(i) = (FcCharSetIsSubset (minimal_charset, cset)
                                   ? true : false);
                  else
                    suitable(i) = false;
                }

              font_map = octave_map (dv, fields);

              font_map.assign ("family", families);
              font_map.assign ("angle", angles);
              font_map.assign ("weight", weights);
              font_map.assign ("suitable", suitable);

              if (fs)
                FcFontSetDestroy (fs);
            }
#endif
        }

      return font_map;
    }

    FT_Face do_get_font (const std::string& name, const std::string& weight,
                         const std::string& angle, double size)
    {
      FT_Face retval = nullptr;

#if defined (HAVE_FT_REFERENCE_FACE)
      // Look first into the font cache, then use fontconfig.  If the font
      // is present in the cache, simply add a reference and return it.

      ft_key key (name + ':' + weight + ':' + angle, size);
      ft_cache::const_iterator it = cache.find (key);

      if (it != cache.end ())
        {
          FT_Reference_Face (it->second);
          return it->second;
        }
#endif

      static std::string fonts_dir;

      if (fonts_dir.empty ())
        {
          fonts_dir = sys::env::getenv ("OCTAVE_FONTS_DIR");

          if (fonts_dir.empty ())
#if defined (SYSTEM_FREEFONT_DIR)
            fonts_dir = SYSTEM_FREEFONT_DIR;
#else
            fonts_dir = config::oct_fonts_dir ();
#endif
        }


      // Default font file
      std::string file;

      if (! fonts_dir.empty ())
        {
          file = fonts_dir + sys::file_ops::dir_sep_str () + "FreeSans";

          if (weight == "bold")
            file += "Bold";

          if (angle == "italic" || angle == "oblique")
            file += "Oblique";

          file += ".otf";
        }

#if defined (HAVE_FONTCONFIG)
      if (name != "*" && fontconfig_initialized)
        {
          int fc_weight, fc_angle;

          if (weight == "bold")
            fc_weight = FC_WEIGHT_BOLD;
          else
            fc_weight = FC_WEIGHT_NORMAL;

          if (angle == "italic")
            fc_angle = FC_SLANT_ITALIC;
          else if (angle == "oblique")
            fc_angle = FC_SLANT_OBLIQUE;
          else
            fc_angle = FC_SLANT_ROMAN;

          FcPattern *pat = FcPatternCreate ();

          FcPatternAddString (pat, FC_FAMILY,
                              (reinterpret_cast<const FcChar8 *>
                               (name.c_str ())));

          FcPatternAddInteger (pat, FC_WEIGHT, fc_weight);
          FcPatternAddInteger (pat, FC_SLANT, fc_angle);
          FcPatternAddDouble (pat, FC_PIXEL_SIZE, size);

          if (FcConfigSubstitute (nullptr, pat, FcMatchPattern))
            {
              FcResult res;
              FcPattern *match;

              FcDefaultSubstitute (pat);
              match = FcFontMatch (nullptr, pat, &res);

              // FIXME: originally, this test also required that
              // res != FcResultNoMatch.  Is that really needed?
              if (match)
                {
                  unsigned char *tmp;

                  FcPatternGetString (match, FC_FILE, 0, &tmp);
                  file = reinterpret_cast<char *> (tmp);
                }
              else
                ::warning ("could not match any font: %s-%s-%s-%g, using default font",
                           name.c_str (), weight.c_str (), angle.c_str (),
                           size);

              if (match)
                FcPatternDestroy (match);
            }

          FcPatternDestroy (pat);
        }
#endif

      if (file.empty ())
        ::warning ("unable to find default font files");
      else
        {
          if (FT_New_Face (library, file.c_str (), 0, &retval))
            ::warning ("ft_manager: unable to load font: %s", file.c_str ());
#if defined (HAVE_FT_REFERENCE_FACE)
          else
            {
              // Install a finalizer to notify ft_manager that the font is
              // being destroyed.  The class ft_manager only keeps weak
              // references to font objects.

              retval->generic.data = new ft_key (key);
              retval->generic.finalizer = ft_face_destroyed;

              // Insert loaded font into the cache.
              if (FT_Reference_Face (retval) == 0)
                cache[key] = retval;
            }
#endif
        }

      return retval;
    }

    void do_font_destroyed (FT_Face face)
    {
      if (face->generic.data)
        {
          ft_key *pkey = reinterpret_cast<ft_key *> (face->generic.data);

          cache.erase (*pkey);
          delete pkey;
          face->generic.data = nullptr;
          FT_Done_Face (face);
        }
    }

  private:
    FT_Library library;
    bool freetype_initialized;
    bool fontconfig_initialized;
  };

  ft_manager *ft_manager::instance = nullptr;

  static void
  ft_face_destroyed (void *object)
  {
    ft_manager::font_destroyed (reinterpret_cast<FT_Face> (object));
  }

  class
  OCTINTERP_API
  ft_text_renderer : public base_text_renderer
  {
  public:

    enum
    {
      MODE_BBOX   = 0,
      MODE_RENDER = 1
    };

    enum
    {
      ROTATION_0   = 0,
      ROTATION_90  = 1,
      ROTATION_180 = 2,
      ROTATION_270 = 3
    };

  public:

    ft_text_renderer (void)
      : base_text_renderer (), font (), bbox (1, 4, 0.0), halign (0),
        xoffset (0), line_yoffset (0), yoffset (0), mode (MODE_BBOX),
        color (dim_vector (1, 3), 0), m_do_strlist (false), m_strlist (),
        line_xoffset (0), m_ymin (0), m_ymax (0), m_deltax (0),
        m_max_fontsize (0), m_antialias (true)
    { }

    // No copying!

    ft_text_renderer (const ft_text_renderer&) = delete;

    ft_text_renderer& operator = (const ft_text_renderer&) = delete;

    ~ft_text_renderer (void) = default;

    void visit (text_element_string& e);

    void visit (text_element_list& e);

    void visit (text_element_subscript& e);

    void visit (text_element_superscript& e);

    void visit (text_element_color& e);

    void visit (text_element_fontsize& e);

    void visit (text_element_fontname& e);

    void visit (text_element_fontstyle& e);

    void visit (text_element_symbol& e);

    void visit (text_element_combined& e);

    void reset (void);

    uint8NDArray get_pixels (void) const { return pixels; }

    Matrix get_boundingbox (void) const { return bbox; }

    uint8NDArray render (text_element *elt, Matrix& box,
                         int rotation = ROTATION_0);

    Matrix get_extent (text_element *elt, double rotation = 0.0);
    Matrix get_extent (const std::string& txt, double rotation,
                       const caseless_str& interpreter);

    void set_anti_aliasing (bool val) { m_antialias = val; };

    void set_font (const std::string& name, const std::string& weight,
                   const std::string& angle, double size);

    octave_map get_system_fonts (void);

    void set_color (const Matrix& c);

    void set_mode (int m);

    void text_to_pixels (const std::string& txt,
                         uint8NDArray& pxls, Matrix& bbox,
                         int halign, int valign, double rotation,
                         const caseless_str& interpreter,
                         bool handle_rotation);

  private:

    int rotation_to_mode (double rotation) const;

    // Class to hold information about fonts and a strong
    // reference to the font objects loaded by FreeType.

    class ft_font : public text_renderer::font
    {
    public:

      ft_font (void)
        : text_renderer::font (), face (nullptr) { }

      ft_font (const std::string& nm, const std::string& wt,
               const std::string& ang, double sz, FT_Face f = nullptr)
        : text_renderer::font (nm, wt, ang, sz), face (f)
      { }

      ft_font (const ft_font& ft);

      ~ft_font (void)
      {
        if (face)
          FT_Done_Face (face);
      }

      ft_font& operator = (const ft_font& ft);

      bool is_valid (void) const { return get_face (); }

      FT_Face get_face (void) const;

    private:

      mutable FT_Face face;
    };

    void push_new_line (void);

    void update_line_bbox (void);

    void compute_bbox (void);

    int compute_line_xoffset (const Matrix& lb) const;

    FT_UInt process_character (FT_ULong code, FT_UInt previous = 0);

  public:

    void text_to_strlist (const std::string& txt,
                          std::list<text_renderer::string>& lst, Matrix& bbox,
                          int halign, int valign, double rotation,
                          const caseless_str& interp);

  private:

    // The current font used by the renderer.
    ft_font font;

    // Used to stored the bounding box corresponding to the rendered text.
    // The bounding box has the form [x, y, w, h] where x and y represent the
    // coordinates of the bottom left corner relative to the anchor point of
    // the text (== start of text on the baseline).  Due to font descent or
    // multiple lines, the value y is usually negative.
    Matrix bbox;

    // Used to stored the rendered text.  It's a 3D matrix with size MxNx4
    // where M and N are the width and height of the bounding box.
    uint8NDArray pixels;

    // Used to store the bounding box of each line.  This is used to layout
    // multiline text properly.
    std::list<Matrix> line_bbox;

    // The current horizontal alignment.  This is used to align multi-line text.
    int halign;

    // The X offset for the next glyph.
    int xoffset;

    // The Y offset of the baseline for the current line.
    int line_yoffset;

    // The Y offset of the baseline for the next glyph.  The offset is relative
    // to line_yoffset.  The total Y offset is computed with:
    // line_yoffset + yoffset.
    int yoffset;

    // The current mode of the rendering process (box computing or rendering).
    int mode;

    // The base color of the rendered text.
    uint8NDArray color;

    // A list of parsed strings to be used for printing.
    bool m_do_strlist;
    std::list<text_renderer::string> m_strlist;

    // The X offset of the baseline for the current line.
    int line_xoffset;

    // Min and max y coordinates of all glyphs in a line.
    FT_Pos m_ymin;
    FT_Pos m_ymax;

    // Difference between the advance and the actual extent of the latest glyph
    FT_Pos m_deltax;

    // Used for computing the distance between lines.
    double m_max_fontsize;

    // Anti-aliasing.
    bool m_antialias;

  };

  void
  ft_text_renderer::set_font (const std::string& name,
                              const std::string& weight,
                              const std::string& angle, double size)
  {
    // FIXME: take "fontunits" into account
    font = ft_font (name, weight, angle, size, nullptr);
  }

  octave_map
  ft_text_renderer::get_system_fonts (void)
  {
    return ft_manager::get_system_fonts ();
  }

  void
  ft_text_renderer::push_new_line (void)
  {
    switch (mode)
      {
      case MODE_BBOX:
        {
          // Create a new bbox entry based on the current font.

          FT_Face face = font.get_face ();

          if (face)
            {
              Matrix bb (1, 5, 0.0);

              line_bbox.push_back (bb);

              xoffset = yoffset = 0;
              m_ymin = m_ymax = m_deltax = 0;
            }
        }
        break;

      case MODE_RENDER:
        {
          // Move to the next line bbox, adjust xoffset based on alignment
          // and yoffset based on the old and new line bbox.

          Matrix old_bbox = line_bbox.front ();
          line_bbox.pop_front ();
          Matrix new_bbox = line_bbox.front ();

          xoffset = line_xoffset = compute_line_xoffset (new_bbox);
          line_yoffset -= (-old_bbox(1) + math::round (0.4 * m_max_fontsize)
                           + (new_bbox(3) + new_bbox(1)));
          yoffset = 0;
          m_ymin = m_ymax = m_deltax = 0;
        }
        break;
      }
  }

  int
  ft_text_renderer::compute_line_xoffset (const Matrix& lb) const
  {
    if (! bbox.isempty ())
      {
        switch (halign)
          {
          case 0:
            return 0;
          case 1:
            return (bbox(2) - lb(2)) / 2;
          case 2:
            return (bbox(2) - lb(2));
          }
      }

    return 0;
  }

  void
  ft_text_renderer::compute_bbox (void)
  {
    // Stack the various line bbox together and compute the final
    // bounding box for the entire text string.

    bbox = Matrix ();

    switch (line_bbox.size ())
      {
      case 0:
        break;

      case 1:
        bbox = line_bbox.front ().extract (0, 0, 0, 3);
        break;

      default:
        for (const auto& lbox : line_bbox)
          {
            if (bbox.isempty ())
              bbox = lbox.extract (0, 0, 0, 3);
            else
              {
                double delta = math::round (0.4 * m_max_fontsize) + lbox(3);
                bbox(1) -= delta;
                bbox(3) += delta;
                bbox(2) = math::max (bbox(2), lbox(2));
              }
          }
        break;
      }
  }

  void
  ft_text_renderer::update_line_bbox (void)
  {
    // Called after a font change, when in MODE_BBOX mode, to update the
    // current line bbox with the new font metrics.  This also includes the
    // current yoffset, that is the offset of the current glyph's baseline
    // the line's baseline.

    if (mode == MODE_BBOX)
      {
        Matrix& bb = line_bbox.back ();
        bb(1) = m_ymin;
        // Add one pixel to the bbox height to avoid occasional text clipping.
        // See bug #55328.
        bb(3) = (m_ymax + 1) - m_ymin;
        if (m_deltax > 0)
          bb(2) += m_deltax;
      }
  }

  void
  ft_text_renderer::set_mode (int m)
  {
    mode = m;

    switch (mode)
      {
      case MODE_BBOX:
        xoffset = line_yoffset = yoffset = 0;
        m_max_fontsize = 0.0;
        bbox = Matrix (1, 4, 0.0);
        line_bbox.clear ();
        push_new_line ();
        break;

      case MODE_RENDER:
        if (bbox.numel () != 4)
          {
            ::error ("ft_text_renderer: invalid bounding box, cannot render");

            xoffset = line_yoffset = yoffset = 0;
            pixels = uint8NDArray ();
          }
        else
          {
            dim_vector d (4, octave_idx_type (bbox(2)),
                          octave_idx_type (bbox(3)));
            pixels = uint8NDArray (d, static_cast<uint8_t> (0));
            xoffset = compute_line_xoffset (line_bbox.front ());
            line_yoffset = -bbox(1);
            yoffset = 0;
          }
        break;

      default:
        error ("ft_text_renderer: invalid mode '%d'", mode);
        break;
      }
  }
  bool is_opaque (const FT_GlyphSlot &glyph, const int x, const int y)
  {
    // Borrowed from https://stackoverflow.com/questions/14800827/
    //    indexing-pixels-in-a-monochrome-freetype-glyph-buffer
    int pitch = std::abs (glyph->bitmap.pitch);
    unsigned char *row = &glyph->bitmap.buffer[pitch * y];
    char cvalue = row[x >> 3];

    return ((cvalue & (128 >> (x & 7))) != 0);
  }

  FT_UInt
  ft_text_renderer::process_character (FT_ULong code, FT_UInt previous)
  {
    FT_Face face = font.get_face ();
    FT_UInt glyph_index = 0;

    if (face)
      {
        glyph_index = FT_Get_Char_Index (face, code);

        if (code != '\n' && code != '\t'
            && (! glyph_index
                || FT_Load_Glyph (face, glyph_index, FT_LOAD_DEFAULT)))
          {
            glyph_index = 0;
            warn_missing_glyph (code);
          }
        else if ((code == '\n') || (code == '\t'))
          {
            glyph_index = FT_Get_Char_Index (face, ' ');
            if (! glyph_index
                || FT_Load_Glyph (face, glyph_index, FT_LOAD_DEFAULT))
              {
                glyph_index = 0;
                warn_missing_glyph (' ');
              }
            else if (code == '\n')
              push_new_line ();
            else
              {
                // Advance to next multiple of 4 times the width of the "space"
                // character.
                int x_tab = 4 * (face->glyph->advance.x >> 6);
                xoffset = (1 + std::floor (1. * xoffset / x_tab)) * x_tab;
              }
          }
        else
          {
            switch (mode)
              {
              case MODE_RENDER:
                if (FT_Render_Glyph (face->glyph, (m_antialias
                                                   ? FT_RENDER_MODE_NORMAL
                                                   : FT_RENDER_MODE_MONO)))
                  {
                    glyph_index = 0;
                    warn_glyph_render (code);
                  }
                else
                  {
                    FT_Bitmap& bitmap = face->glyph->bitmap;
                    int x0, y0;

                    if (previous)
                      {
                        FT_Vector delta;

                        FT_Get_Kerning (face, previous, glyph_index,
                                        FT_KERNING_DEFAULT, &delta);
                        xoffset += (delta.x >> 6);
                      }

                    x0 = xoffset + face->glyph->bitmap_left;
                    y0 = line_yoffset + yoffset + (face->glyph->bitmap_top - 1);

                    // 'w' seems to have a negative -1
                    // face->glyph->bitmap_left, this is so we don't index out
                    // of bound, and assumes we've allocated the right amount of
                    // horizontal space in the bbox.
                    if (x0 < 0)
                      x0 = 0;

                    for (int r = 0; static_cast<unsigned int> (r) < bitmap.rows; r++)
                      for (int c = 0; static_cast<unsigned int> (c) < bitmap.width; c++)
                        {
                          unsigned char pix
                            = (m_antialias
                               ? bitmap.buffer[r*bitmap.width+c]
                               : (is_opaque (face->glyph, c, r) ? 255 : 0));

                          if (x0+c < 0 || x0+c >= pixels.dim2 ()
                              || y0-r < 0 || y0-r >= pixels.dim3 ())
                            {
                              // ::warning ("ft_text_renderer: x %d,  y %d",
                              //            x0+c, y0-r);
                            }
                          else if (pixels(3, x0+c, y0-r).value () == 0)
                            {
                              pixels(0, x0+c, y0-r) = color(0);
                              pixels(1, x0+c, y0-r) = color(1);
                              pixels(2, x0+c, y0-r) = color(2);
                              pixels(3, x0+c, y0-r) = pix;
                            }
                        }

                    xoffset += (face->glyph->advance.x >> 6);
                  }
                break;

              case MODE_BBOX:
                Matrix& bb = line_bbox.back ();

                // If we have a previous glyph, use kerning information.  This
                // usually means moving a bit backward before adding the next
                // glyph.  That is, "delta.x" is usually < 0.
                if (previous)
                  {
                    FT_Vector delta;

                    FT_Get_Kerning (face, previous, glyph_index,
                                    FT_KERNING_DEFAULT, &delta);

                    xoffset += (delta.x >> 6);
                  }

                // Extend current X offset box by the width of the current
                // glyph.  Then extend the line bounding box if necessary.

                xoffset += (face->glyph->advance.x >> 6);
                bb(2) = math::max (bb(2), xoffset);

                // Store the actual bbox vertical coordinates of this character
                FT_Glyph glyph;
                if (FT_Get_Glyph (face->glyph, &glyph))
                  warn_glyph_render (code);
                else
                  {
                    FT_BBox  glyph_bbox;
                    FT_Glyph_Get_CBox (glyph, FT_GLYPH_BBOX_UNSCALED,
                                       &glyph_bbox);
                    m_deltax = (glyph_bbox.xMax - face->glyph->advance.x) >> 6;
                    m_ymin = math::min ((glyph_bbox.yMin >> 6) + yoffset,
                                        m_ymin);
                    m_ymax = math::max ((glyph_bbox.yMax >> 6) + yoffset,
                                        m_ymax);
                    FT_Done_Glyph (glyph);
                    update_line_bbox ();
                  }
                break;
              }
          }
      }

    return glyph_index;
  }

  void
  ft_text_renderer::text_to_strlist (const std::string& txt,
                                     std::list<text_renderer::string>& lst,
                                     Matrix& box,
                                     int ha, int va, double rot,
                                     const caseless_str& interp)
  {
    uint8NDArray pxls;

    // First run text_to_pixels which will also build the string list

    m_strlist = std::list<text_renderer::string> ();

    unwind_protect frame;
    frame.protect_var (m_do_strlist);
    frame.protect_var (m_strlist);
    m_do_strlist = true;

    text_to_pixels (txt, pxls, box, ha, va, rot, interp, false);

    lst = m_strlist;
  }

  void
  ft_text_renderer::visit (text_element_string& e)
  {
    if (font.is_valid ())
      {
        m_max_fontsize = std::max (m_max_fontsize, font.get_size ());
        FT_UInt glyph_index, previous = 0;

        std::string str = e.string_value ();
        const uint8_t *c = reinterpret_cast<const uint8_t *> (str.c_str ());
        uint32_t u32_c;

        size_t n = str.size ();
        size_t icurr = 0;
        size_t ibegin = 0;

        // Initialize a new string
        std::string fname = font.get_face ()->family_name;
        text_renderer::string fs (str, font, xoffset, yoffset);
        std::vector<double> xdata;

        while (n > 0)
          {
            // Retrieve the length and the u32 representation of the current
            // character
            int mblen = octave_u8_strmbtouc_wrapper (&u32_c, c + icurr);
            if (mblen < 1)
              {
                // This is not an UTF-8 character, use a replacement character
                mblen = 1;
                u32_c = 0xFFFD;
              }

            n -= mblen;

            if (m_do_strlist && mode == MODE_RENDER)
              {
                if (u32_c == 10)
                  {
                    // Finish previous string in m_strlist before processing
                    // the newline character
                    fs.set_y (line_yoffset + yoffset);
                    fs.set_color (color);

                    std::string s = str.substr (ibegin, icurr - ibegin);
                    if (! s.empty ())
                      {
                        fs.set_string (s);
                        fs.set_y (line_yoffset + yoffset);
                        fs.set_xdata (xdata);
                        fs.set_family (fname);
                        m_strlist.push_back (fs);
                      }
                  }
                else
                  xdata.push_back (xoffset);
              }

            glyph_index = process_character (u32_c, previous);

            if (u32_c == 10)
              {
                previous = 0;

                if (m_do_strlist && mode == MODE_RENDER)
                  {
                    // Start a new string in m_strlist
                    ibegin = icurr+1;
                    xdata.clear ();
                    fs = text_renderer::string (str.substr (ibegin), font,
                                                line_xoffset, yoffset);
                  }
              }
            else
              previous = glyph_index;

            icurr += mblen;
          }

        if (m_do_strlist && mode == MODE_RENDER && ! fs.get_string ().empty ())
          {
            fs.set_y (line_yoffset + yoffset);
            fs.set_color (color);
            fs.set_xdata (xdata);
            fs.set_family (fname);
            m_strlist.push_back (fs);
          }
      }
  }

  void
  ft_text_renderer::visit (text_element_list& e)
  {
    // Save and restore (after processing the list) the current font and color.

    ft_font saved_font (font);
    uint8NDArray saved_color (color);

    text_processor::visit (e);

    font = saved_font;
    color = saved_color;
  }

  void
  ft_text_renderer::visit (text_element_subscript& e)
  {
    ft_font saved_font (font);
    int saved_line_yoffset = line_yoffset;
    int saved_yoffset = yoffset;

    double sz = font.get_size ();

    // Reducing font size by 70% produces decent results.
    set_font (font.get_name (), font.get_weight (), font.get_angle (),
              std::max (5.0, sz * 0.7));

    if (font.is_valid ())
      {
        // Shifting the baseline by 15% of the font size gives decent results.
        yoffset -= std::ceil (sz * 0.15);

        if (mode == MODE_BBOX)
          update_line_bbox ();
      }

    text_processor::visit (e);

    font = saved_font;
    // If line_yoffset changed, this means we moved to a new line; hence yoffset
    // cannot be restored, because the saved value is not relevant anymore.
    if (line_yoffset == saved_line_yoffset)
      yoffset = saved_yoffset;
  }

  void
  ft_text_renderer::visit (text_element_superscript& e)
  {
    ft_font saved_font (font);
    int saved_line_yoffset = line_yoffset;
    int saved_yoffset = yoffset;

    double sz = font.get_size ();

    // Reducing font size by 70% produces decent results.
    set_font (font.get_name (), font.get_weight (), font.get_angle (),
              std::max (5.0, sz * 0.7));

    if (saved_font.is_valid ())
      {
        // Shifting the baseline by 40% of the font size gives decent results.
        yoffset += std::ceil (sz * 0.4);

        if (mode == MODE_BBOX)
          update_line_bbox ();
      }

    text_processor::visit (e);

    font = saved_font;
    // If line_yoffset changed, this means we moved to a new line; hence yoffset
    // cannot be restored, because the saved value is not relevant anymore.
    if (line_yoffset == saved_line_yoffset)
      yoffset = saved_yoffset;
  }

  void
  ft_text_renderer::visit (text_element_color& e)
  {
    if (mode == MODE_RENDER)
      set_color (e.get_color ());
  }

  void
  ft_text_renderer::visit (text_element_fontsize& e)
  {
    double sz = e.get_fontsize ();

    // FIXME: Matlab documentation says that the font size is expressed
    //        in the text object FontUnit.

    set_font (font.get_name (), font.get_weight (), font.get_angle (), sz);

    if (mode == MODE_BBOX)
      update_line_bbox ();
  }

  void
  ft_text_renderer::visit (text_element_fontname& e)
  {
    set_font (e.get_fontname (), font.get_weight (), font.get_angle (),
              font.get_size ());

    if (mode == MODE_BBOX)
      update_line_bbox ();
  }

  void
  ft_text_renderer::visit (text_element_fontstyle& e)
  {
    switch (e.get_fontstyle ())
      {
      case text_element_fontstyle::normal:
        set_font (font.get_name (), "normal", "normal", font.get_size ());
        break;

      case text_element_fontstyle::bold:
        set_font (font.get_name (), "bold", "normal", font.get_size ());
        break;

      case text_element_fontstyle::italic:
        set_font (font.get_name (), "normal", "italic", font.get_size ());
        break;

      case text_element_fontstyle::oblique:
        set_font (font.get_name (), "normal", "oblique", font.get_size ());
        break;
      }

    if (mode == MODE_BBOX)
      update_line_bbox ();
  }

  void
  ft_text_renderer::visit (text_element_symbol& e)
  {
    uint32_t code = e.get_symbol_code ();

    std::vector<double> xdata (1, xoffset);
    text_renderer::string fs ("-", font, xoffset, yoffset);

    if (code != text_element_symbol::invalid_code && font.is_valid ())
      {
        process_character (code);
        if (m_do_strlist && mode == MODE_RENDER)
          {
            fs.set_code (code);
            fs.set_xdata (xdata);
          }
      }
    else if (font.is_valid ())
      ::warning ("ignoring unknown symbol: %d", e.get_symbol ());

    if (m_do_strlist && mode == MODE_RENDER && fs.get_code ())
      {
        fs.set_y (line_yoffset + yoffset);
        fs.set_color (color);
        fs.set_family (font.get_face ()->family_name);
        m_strlist.push_back (fs);
      }
  }

  void
  ft_text_renderer::visit (text_element_combined& e)
  {
    int saved_xoffset = xoffset;
    int max_xoffset = xoffset;

    for (auto *txt_elt : e)
      {
        xoffset = saved_xoffset;
        txt_elt->accept (*this);
        max_xoffset = math::max (xoffset, max_xoffset);
      }

    xoffset = max_xoffset;
  }

  void
  ft_text_renderer::reset (void)
  {
    set_mode (MODE_BBOX);
    set_color (Matrix (1, 3, 0.0));
    m_strlist = std::list<text_renderer::string> ();
  }

  void
  ft_text_renderer::set_color (const Matrix& c)
  {
    if (c.numel () == 3)
      {
        color(0) = static_cast<uint8_t> (c(0)*255);
        color(1) = static_cast<uint8_t> (c(1)*255);
        color(2) = static_cast<uint8_t> (c(2)*255);
      }
    else
      ::warning ("ft_text_renderer::set_color: invalid color");
  }

  uint8NDArray
  ft_text_renderer::render (text_element *elt, Matrix& box, int rotation)
  {
    set_mode (MODE_BBOX);
    elt->accept (*this);
    compute_bbox ();
    box = bbox;

    set_mode (MODE_RENDER);

    if (pixels.numel () > 0)
      {
        elt->accept (*this);

        switch (rotation)
          {
          case ROTATION_0:
            break;

          case ROTATION_90:
            {
              Array<octave_idx_type> perm (dim_vector (3, 1));
              perm(0) = 0;
              perm(1) = 2;
              perm(2) = 1;
              pixels = pixels.permute (perm);

              Array<idx_vector> idx (dim_vector (3, 1));
              idx(0) = idx_vector (':');
              idx(1) = idx_vector (pixels.dim2 ()-1, -1, -1);
              idx(2) = idx_vector (':');
              pixels = uint8NDArray (pixels.index (idx));
            }
            break;

          case ROTATION_180:
            {
              Array<idx_vector> idx (dim_vector (3, 1));
              idx(0) = idx_vector (':');
              idx(1) = idx_vector (pixels.dim2 ()-1, -1, -1);
              idx(2) = idx_vector (pixels.dim3 ()-1, -1, -1);
              pixels = uint8NDArray (pixels.index (idx));
            }
            break;

          case ROTATION_270:
            {
              Array<octave_idx_type> perm (dim_vector (3, 1));
              perm(0) = 0;
              perm(1) = 2;
              perm(2) = 1;
              pixels = pixels.permute (perm);

              Array<idx_vector> idx (dim_vector (3, 1));
              idx(0) = idx_vector (':');
              idx(1) = idx_vector (':');
              idx(2) = idx_vector (pixels.dim3 ()-1, -1, -1);
              pixels = uint8NDArray (pixels.index (idx));
            }
            break;
          }
      }

    return pixels;
  }

  // Note:
  // x-extent accurately measures width of glyphs.
  // y-extent is overly large because it is measured from baseline-to-baseline.
  // Calling routines, such as ylabel, may need to account for this mismatch.

  Matrix
  ft_text_renderer::get_extent (text_element *elt, double rotation)
  {
    set_mode (MODE_BBOX);
    elt->accept (*this);
    compute_bbox ();

    Matrix extent (1, 2, 0.0);

    switch (rotation_to_mode (rotation))
      {
      case ROTATION_0:
      case ROTATION_180:
        extent(0) = bbox(2);
        extent(1) = bbox(3);
        break;

      case ROTATION_90:
      case ROTATION_270:
        extent(0) = bbox(3);
        extent(1) = bbox(2);
      }

    return extent;
  }

  Matrix
  ft_text_renderer::get_extent (const std::string& txt, double rotation,
                                const caseless_str& interpreter)
  {
    text_element *elt = text_parser::parse (txt, interpreter);
    Matrix extent = get_extent (elt, rotation);
    delete elt;

    return extent;
  }

  int
  ft_text_renderer::rotation_to_mode (double rotation) const
  {
    // Clip rotation to range [0, 360]
    while (rotation < 0)
      rotation += 360.0;
    while (rotation > 360.0)
      rotation -= 360.0;

    if (rotation == 0.0)
      return ROTATION_0;
    else if (rotation == 90.0)
      return ROTATION_90;
    else if (rotation == 180.0)
      return ROTATION_180;
    else if (rotation == 270.0)
      return ROTATION_270;
    else
      return ROTATION_0;
  }

  void
  ft_text_renderer::text_to_pixels (const std::string& txt,
                                    uint8NDArray& pxls, Matrix& box,
                                    int _halign, int valign, double rotation,
                                    const caseless_str& interpreter,
                                    bool handle_rotation)
  {
    int rot_mode = rotation_to_mode (rotation);

    halign = _halign;

    text_element *elt = text_parser::parse (txt, interpreter);
    pxls = render (elt, box, rot_mode);
    delete elt;

    if (pxls.isempty ())
      return;  // nothing to render

    switch (halign)
      {
      case 1:
        box(0) = -box(2)/2;
        break;

      case 2:
        box(0) = -box(2);
        break;

      default:
        box(0) = 0;
        break;
      }

    switch (valign)
      {
      case 1:
        box(1) = -box(3)/2;
        break;

      case 2:
        box(1) = -box(3);
        break;

      case 3:
        break;

      case 4:
        box(1) = -box(3)-box(1);
        break;

      default:
        box(1) = 0;
        break;
      }

    if (handle_rotation)
      {
        switch (rot_mode)
          {
          case ROTATION_90:
            std::swap (box(0), box(1));
            std::swap (box(2), box(3));
            box(0) = -box(0)-box(2);
            break;

          case ROTATION_180:
            box(0) = -box(0)-box(2);
            box(1) = -box(1)-box(3);
            break;

          case ROTATION_270:
            std::swap (box(0), box(1));
            std::swap (box(2), box(3));
            box(1) = -box(1)-box(3);
            break;
          }
      }
  }

  ft_text_renderer::ft_font::ft_font (const ft_font& ft)
    : text_renderer::font (ft), face (nullptr)
  {
#if defined (HAVE_FT_REFERENCE_FACE)
    FT_Face ft_face = ft.get_face ();

    if (ft_face && FT_Reference_Face (ft_face) == 0)
      face = ft_face;
#endif
  }

  ft_text_renderer::ft_font&
  ft_text_renderer::ft_font::operator = (const ft_font& ft)
  {
    if (&ft != this)
      {
        text_renderer::font::operator = (ft);

        if (face)
          {
            FT_Done_Face (face);
            face = nullptr;
          }

#if defined (HAVE_FT_REFERENCE_FACE)
        FT_Face ft_face = ft.get_face ();

        if (ft_face && FT_Reference_Face (ft_face) == 0)
          face = ft_face;
#endif
      }

    return *this;
  }

  FT_Face
  ft_text_renderer::ft_font::get_face (void) const
  {
    if (! face && ! name.empty ())
      {
        face = ft_manager::get_font (name, weight, angle, size);

        if (face)
          {
            if (FT_Set_Char_Size (face, 0, size*64, 0, 0))
              ::warning ("ft_text_renderer: unable to set font size to %g", size);
          }
        else
          ::warning ("ft_text_renderer: unable to load appropriate font");
      }

    return face;
  }
}

#endif

namespace octave
{
  base_text_renderer *
  make_ft_text_renderer (void)
  {
#if defined (HAVE_FREETYPE)
    return new ft_text_renderer ();
#else
    return 0;
#endif
  }
}