forked from synfig/synfig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lyr_freetype.cpp
1486 lines (1266 loc) · 41.1 KB
/
lyr_freetype.cpp
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
/* === S Y N F I G ========================================================= */
/*! \file lyr_freetype.cpp
** \brief Implementation of the "Text" layer
**
** \legal
** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
** Copyright (c) 2006 Paul Wise
** Copyright (c) 2007, 2008 Chris Moore
** Copyright (c) 2012-2013 Carlos López
**
** This file is part of Synfig.
**
** Synfig 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 2 of the License, or
** (at your option) any later version.
**
** Synfig 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 Synfig. If not, see <https://www.gnu.org/licenses/>.
** \endlegal
**
** ========================================================================= */
/* === H E A D E R S ======================================================= */
#define SYNFIG_LAYER
#ifdef USING_PCH
# include "pch.h"
#else
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#ifdef WITH_FONTCONFIG
#include <fontconfig/fontconfig.h>
#endif
#include "lyr_freetype.h"
#include <algorithm>
#include <glibmm.h>
#if HAVE_HARFBUZZ
#include <fribidi.h>
#include <hb-ft.h>
#endif
#include <synfig/canvasfilenaming.h>
#include <synfig/context.h>
#include <synfig/general.h>
#include <synfig/localization.h>
#include <synfig/rendering/common/task/taskcontour.h>
#include <synfig/string_helper.h>
#endif
using namespace etl;
using namespace synfig;
/* === M A C R O S ========================================================= */
// Copy of PangoStyle
// It is necessary to keep original values if Pango ever change them
// - because it would change layer rendering as Synfig stores the parameter
// value as an integer (ie. not a weight name string)
enum TextStyle{
TEXT_STYLE_NORMAL = 0,
TEXT_STYLE_OBLIQUE = 1,
TEXT_STYLE_ITALIC = 2
};
// Copy of PangoWeight
// It is necessary to keep original values if Pango ever change them
// - because it would change layer rendering as Synfig stores the parameter
// value as an integer (ie. not a weight name string)
enum TextWeight{
TEXT_WEIGHT_THIN = 100,
TEXT_WEIGHT_ULTRALIGHT = 200,
TEXT_WEIGHT_LIGHT = 300,
TEXT_WEIGHT_SEMILIGHT = 350,
TEXT_WEIGHT_BOOK = 380,
TEXT_WEIGHT_NORMAL = 400,
TEXT_WEIGHT_MEDIUM = 500,
TEXT_WEIGHT_SEMIBOLD = 600,
TEXT_WEIGHT_BOLD = 700,
TEXT_WEIGHT_ULTRABOLD = 800,
TEXT_WEIGHT_HEAVY = 900,
TEXT_WEIGHT_ULTRAHEAVY = 1000
};
enum TextDirection{
TEXT_DIRECTION_AUTO = 0,
TEXT_DIRECTION_LTR = 1,
TEXT_DIRECTION_RTL = 2,
};
/* === G L O B A L S ======================================================= */
SYNFIG_LAYER_INIT(Layer_Freetype);
SYNFIG_LAYER_SET_NAME(Layer_Freetype,"text");
SYNFIG_LAYER_SET_LOCAL_NAME(Layer_Freetype,N_("Text"));
SYNFIG_LAYER_SET_CATEGORY(Layer_Freetype,N_("Other"));
SYNFIG_LAYER_SET_VERSION(Layer_Freetype,"0.5");
#ifndef __APPLE__
static const std::vector<const char *> known_font_extensions = {".ttf", ".otf", ".ttc"};
#else
static const std::vector<const char *> known_font_extensions = {".ttf", ".otf", ".dfont", ".ttc"};
#endif
extern FT_Library ft_library;
/// NL/LF, VT, FF, CR, NEL, LS and PS
static const std::vector<uint32_t> line_endings{'\n', '\v', '\f', '\r', 0x0085, 0x2028, 0x2029};
/* === C L A S S E S ======================================================= */
#ifdef WITH_FONTCONFIG
// Allow proper finalization of FontConfig
struct FontConfigWrap {
static FcConfig* instance() {
static FontConfigWrap obj;
return obj.config;
}
FontConfigWrap(FontConfigWrap const&) = delete;
void operator=(FontConfigWrap const&) = delete;
private:
FcConfig* config = nullptr;
FontConfigWrap()
{
config = FcInitLoadConfigAndFonts();
#ifdef _WIN32
// Windows 10 (1809) Added local user fonts installed to C:\Users\%USERNAME%\AppData\Local\Microsoft\Windows\Fonts
std::string localdir = Glib::getenv("LOCALAPPDATA");
if (!localdir.empty()) {
localdir.append("\\Microsoft\\Windows\\Fonts\\");
FcConfigAppFontAddDir(config, (const FcChar8 *)localdir.c_str());
}
#endif
}
~FontConfigWrap() {
FcConfigDestroy(config);
config = nullptr;
}
};
static std::string fontconfig_get_filename(const std::string& font_fam, int style, int weight);
#endif
/// Metadata about a font. Used for font face cache indexing
struct FontMeta {
synfig::String family;
int style;
int weight;
//! Canvas file path if loaded font face file depends on it.
//! Empty string otherwise
std::string canvas_path;
explicit FontMeta(synfig::String family, int style=0, int weight=400)
: family(std::move(family)), style(style), weight(weight)
{}
bool operator==(const FontMeta& other) const
{
return family == other.family && style == other.style && weight == other.weight && canvas_path == other.canvas_path;
}
bool operator<(const FontMeta& other) const
{
if (family < other.family)
return true;
if (family != other.family)
return false;
if (style < other.style)
return true;
if (style > other.style)
return false;
if (weight < other.weight)
return true;
if (weight > other.weight)
return false;
if (canvas_path < other.canvas_path)
return true;
return false;
}
};
struct FaceInfo {
FT_Face face = nullptr;
#if HAVE_HARFBUZZ
hb_font_t *font = nullptr;
#endif
FaceInfo() = default;
explicit FaceInfo(FT_Face ft_face)
: face(ft_face)
{
#if HAVE_HARFBUZZ
font = hb_ft_font_create(face, nullptr);
#endif
}
};
/// Cache font faces for speeding up the text layer rendering
class FaceCache {
std::map<FontMeta, FaceInfo> cache;
mutable std::mutex cache_mutex;
FaceCache() = default; // Make constructor private to prevent instancing
public:
FaceInfo get(const FontMeta &meta) const {
std::lock_guard<std::mutex> lock(cache_mutex);
auto iter = cache.find(meta);
if (iter != cache.end())
return iter->second;
return FaceInfo();
}
void put(const FontMeta &meta, FaceInfo face) {
std::lock_guard<std::mutex> lock(cache_mutex);
cache[meta] = face;
}
bool has(const FontMeta &meta) const {
std::lock_guard<std::mutex> lock(cache_mutex);
auto iter = cache.find(meta);
return iter != cache.end();
}
void clear() {
std::lock_guard<std::mutex> lock(cache_mutex);
for (const auto& item : cache) {
FT_Done_Face(item.second.face);
#if HAVE_HARFBUZZ
hb_font_destroy(item.second.font);
#endif
}
cache.clear();
}
static FaceCache& instance() {
static FaceCache obj;
return obj;
}
FaceCache(const FaceCache&) = delete; // Copy prohibited
void operator=(const FaceCache&) = delete; // Assignment prohibited
FaceCache& operator=(FaceCache&&) = delete; // Move assignment prohibited
~FaceCache() {
clear();
}
};
/* === P R O C E D U R E S ================================================= */
static bool
has_valid_font_extension(const std::string &filename) {
std::string extension = etl::filename_extension(filename);
return std::find(known_font_extensions.begin(), known_font_extensions.end(), extension) != known_font_extensions.end();
}
/// Try to map a font family to a filename (without extension nor directory)
static void
get_possible_font_filenames(synfig::String family, int style, int weight, std::vector<std::string>& list)
{
// string :: tolower
std::transform(family.begin(), family.end(), family.begin(),
[](unsigned char c){ return std::tolower(c); });
enum FontSuffixStyle {FONT_SUFFIX_NONE, FONT_SUFFIX_BI_BD, FONT_SUFFIX_BI_BD_IT, FONT_SUFFIX_BI_RI};
enum FontClassification {FONT_SANS_SERIF, FONT_SERIF, FONT_MONOSPACED, FONT_SCRIPT};
struct FontFileNameEntry {
const char *alias;
const char *preffix;
const char *alternative_preffix;
FontSuffixStyle suffix_style;
FontClassification classification;
std::string get_suffix(int style, int weight) const {
std::string suffix;
switch (suffix_style) {
case FONT_SUFFIX_NONE:
break;
case FONT_SUFFIX_BI_BD:
if (weight>TEXT_WEIGHT_NORMAL)
suffix+='b';
if (style==TEXT_STYLE_ITALIC || style==TEXT_STYLE_OBLIQUE)
suffix+='i';
else if (weight>TEXT_WEIGHT_NORMAL)
suffix+='d';
break;
case FONT_SUFFIX_BI_BD_IT:
if (weight>TEXT_WEIGHT_NORMAL)
suffix+='b';
if (style==TEXT_STYLE_ITALIC || style==TEXT_STYLE_OBLIQUE)
{
suffix+='i';
if (weight<=TEXT_WEIGHT_NORMAL)
suffix+='t';
}
else if(weight>TEXT_WEIGHT_NORMAL)
suffix+='d';
break;
case FONT_SUFFIX_BI_RI:
if(weight>TEXT_WEIGHT_NORMAL)
suffix+='b';
else
suffix+='r';
if(style==TEXT_STYLE_ITALIC || style==TEXT_STYLE_OBLIQUE)
suffix+='i';
break;
}
return suffix;
}
static std::string get_alternative_suffix(int style, int weight) {
if (weight > TEXT_WEIGHT_NORMAL) {
if (style == TEXT_STYLE_ITALIC)
return " Bold Italic";
else if (style == TEXT_STYLE_OBLIQUE)
return " Bold Oblique";
else
return " Bold";
} else {
if (style == TEXT_STYLE_ITALIC)
return " Italic";
else if (style == TEXT_STYLE_OBLIQUE)
return " Oblique";
else
return "";
}
}
};
struct SpecialFontFamily {
const char * const alias;
const char * const option1;
const char * const option2;
const char * const option3;
};
const SpecialFontFamily special_font_family_db[] = {
{"sans serif", "arial", "luxi sans", "helvetica"},
{"serif", "times new roman", "luxi serif", nullptr},
{"comic", "comic sans", nullptr, nullptr},
{"courier", "courier new", nullptr, nullptr},
{"times", "times new roman", nullptr, nullptr},
{nullptr, nullptr, nullptr, nullptr}
};
const FontFileNameEntry font_filename_db[] = {
{"arial black", "ariblk", nullptr, FONT_SUFFIX_NONE, FONT_SANS_SERIF},
{"arial", "arial", "Arial", FONT_SUFFIX_BI_BD, FONT_SANS_SERIF},
{"comic sans", "comic", nullptr, FONT_SUFFIX_BI_BD, FONT_SANS_SERIF},
{"courier new", "cour", "Courier New", FONT_SUFFIX_BI_BD, FONT_MONOSPACED},
{"times new roman", "times", "Times New Roman", FONT_SUFFIX_BI_BD, FONT_SERIF},
{"trebuchet", "trebuc", "Trebuchet MS", FONT_SUFFIX_BI_BD_IT, FONT_SANS_SERIF},
{"luxi sans", "luxis", nullptr, FONT_SUFFIX_BI_RI, FONT_SANS_SERIF},
{"luxi serif", "luxir", nullptr, FONT_SUFFIX_BI_RI, FONT_SERIF},
{"luxi mono", "luxim", nullptr, FONT_SUFFIX_BI_RI, FONT_MONOSPACED},
{"luxi", "luxim", nullptr, FONT_SUFFIX_BI_RI, FONT_MONOSPACED},
{nullptr, nullptr, nullptr, FONT_SUFFIX_NONE, FONT_SANS_SERIF},
};
std::vector<std::string> possible_families;
for (int i = 0; special_font_family_db[i].alias; i++) {
const SpecialFontFamily &special_family = special_font_family_db[i];
if (special_family.alias == family) {
possible_families.push_back(special_family.option1);
if (special_family.option2) {
possible_families.push_back(special_family.option2);
if (special_family.option3)
possible_families.push_back(special_family.option3);
}
break;
}
}
if (possible_families.empty())
possible_families.push_back(family);
for (const std::string &possible_family : possible_families) {
for (int i = 0; font_filename_db[i].alias; i++) {
const FontFileNameEntry &entry = font_filename_db[i];
if (possible_family == entry.alias) {
std::string filename = entry.preffix;
filename += entry.get_suffix(style, weight);
list.push_back(filename);
filename = entry.preffix;
filename += FontFileNameEntry::get_alternative_suffix(style, weight);
list.push_back(filename);
}
}
}
}
/* === M E T H O D S ======================================================= */
Layer_Freetype::Layer_Freetype()
: face(nullptr)
{
#if HAVE_HARFBUZZ
font = nullptr;
#endif
param_size=ValueBase(Vector(0.25,0.25));
param_text=ValueBase(std::string());//_("Text Layer"));
param_color=ValueBase(Color::black());
param_origin=ValueBase(Vector(0,0));
param_orient=ValueBase(Vector(0.5,0.5));
param_compress=ValueBase(Real(1.0));
param_vcompress=ValueBase(Real(1.0));
param_weight=ValueBase(TEXT_WEIGHT_NORMAL);
param_style=ValueBase(TEXT_STYLE_NORMAL);
param_direction=ValueBase(TEXT_DIRECTION_AUTO);
param_family=ValueBase((const char*)"Sans Serif");
param_use_kerning=ValueBase(true);
param_grid_fit=ValueBase(false);
param_invert=ValueBase(false);
param_font=ValueBase(synfig::String());
font_path_from_canvas = false;
old_version=false;
set_blend_method(Color::BLEND_COMPOSITE);
need_sync=SYNC_FONT;
synfig::String family=param_family.get(synfig::String());
int style=param_style.get(int());
int weight=param_weight.get(int());
new_font(family,style,weight);
SET_INTERPOLATION_DEFAULTS();
SET_STATIC_DEFAULTS();
set_description(param_text.get(String()));
}
void
Layer_Freetype::on_canvas_set()
{
Layer_Shape::on_canvas_set();
synfig::String family=param_family.get(synfig::String());
// Is it a font family or an absolute path for a font file? No need to reload it
if (!has_valid_font_extension(family) || etl::is_absolute_path(family))
return;
int style=param_style.get(int());
int weight=param_weight.get(int());
new_font(family,style,weight);
}
void
Layer_Freetype::new_font(const synfig::String &family, int style, int weight)
{
if(
!new_font_(family,style,weight) &&
!new_font_(family,style,TEXT_WEIGHT_NORMAL) &&
!new_font_(family,TEXT_STYLE_NORMAL,weight) &&
!new_font_(family,TEXT_STYLE_NORMAL,TEXT_WEIGHT_NORMAL) &&
!new_font_("sans serif",style,weight) &&
!new_font_("sans serif",style,TEXT_WEIGHT_NORMAL) &&
!new_font_("sans serif",TEXT_STYLE_NORMAL,weight)
)
new_font_("sans serif",TEXT_STYLE_NORMAL,TEXT_WEIGHT_NORMAL);
}
bool
Layer_Freetype::new_font_(const synfig::String &font_fam_, int style, int weight)
{
FontMeta meta(font_fam_, style, weight);
if (get_canvas())
meta.canvas_path = get_canvas()->get_file_path()+ETL_DIRECTORY_SEPARATOR;
FaceCache &face_cache = FaceCache::instance();
{
FaceInfo face_info = face_cache.get(meta);
FT_Face tmp_face = face_info.face;
if (tmp_face) {
if (face != tmp_face)
need_sync |= SYNC_FONT;
face = tmp_face;
#if HAVE_HARFBUZZ
font = face_info.font;
#endif
return true;
}
}
auto cache_face = [&](FT_Face face) {
if (!font_path_from_canvas)
meta.canvas_path.clear();
FaceInfo face_info(face);
face_cache.put(meta, FaceInfo(face));
#if HAVE_HARFBUZZ
font = face_info.font;
#endif
};
if (has_valid_font_extension(font_fam_))
if (new_face(font_fam_)) {
cache_face(face);
return true;
}
#ifdef WITH_FONTCONFIG
if (new_face(fontconfig_get_filename(font_fam_, style, weight))) {
cache_face(face);
return true;
}
#endif
std::vector<std::string> filename_list;
get_possible_font_filenames(font_fam_, style, weight, filename_list);
for (std::string& filename : filename_list) {
if (new_face(filename)) {
cache_face(face);
return true;
}
}
if (new_face(font_fam_)) {
cache_face(face);
return true;
}
return false;
}
#ifdef WITH_FONTCONFIG
static std::string fontconfig_get_filename(const std::string& font_fam, int style, int weight) {
std::string filename;
FcConfig* fc = FontConfigWrap::instance();
if( !fc )
{
synfig::warning("Layer_Freetype: fontconfig: %s",_("unable to initialize"));
} else {
FcPattern* pat = FcPatternCreate();
FcPatternAddString(pat, FC_FAMILY, (const FcChar8*)font_fam.c_str());
FcPatternAddInteger(pat, FC_SLANT, style == TEXT_STYLE_NORMAL ? FC_SLANT_ROMAN : (style == TEXT_STYLE_ITALIC ? FC_SLANT_ITALIC : FC_SLANT_OBLIQUE));
int fc_weight;
#define SYNFIG_TO_FC(X) TEXT_WEIGHT_##X : fc_weight = FC_WEIGHT_##X ; break
switch (weight) {
case SYNFIG_TO_FC(NORMAL);
case SYNFIG_TO_FC(BOLD);
case SYNFIG_TO_FC(THIN);
case SYNFIG_TO_FC(ULTRALIGHT);
case SYNFIG_TO_FC(LIGHT);
case SYNFIG_TO_FC(SEMILIGHT);
case SYNFIG_TO_FC(BOOK);
case SYNFIG_TO_FC(MEDIUM);
case SYNFIG_TO_FC(SEMIBOLD);
case SYNFIG_TO_FC(ULTRABOLD);
case SYNFIG_TO_FC(HEAVY);
case TEXT_WEIGHT_ULTRAHEAVY : fc_weight = FC_WEIGHT_HEAVY ; break;
default:
fc_weight = FC_WEIGHT_NORMAL;
}
#undef SYNFIG_TO_FC
FcPatternAddInteger(pat, FC_WEIGHT, fc_weight);
FcConfigSubstitute(fc, pat, FcMatchPattern);
FcDefaultSubstitute(pat);
FcFontSet *fs = FcFontSetCreate();
FcResult result;
FcPattern *match = FcFontMatch(fc, pat, &result);
if (match)
FcFontSetAdd(fs, match);
if (pat)
FcPatternDestroy(pat);
if(fs && fs->nfont){
FcChar8* file;
if( FcPatternGetString (fs->fonts[0], FC_FILE, 0, &file) == FcResultMatch )
filename = (const char*)file;
FcFontSetDestroy(fs);
} else
synfig::warning("Layer_Freetype: fontconfig: %s",_("empty font set"));
}
return filename;
}
#endif
bool
Layer_Freetype::new_face(const String &newfont)
{
synfig::String font=param_font.get(synfig::String());
int error = 0;
FT_Long face_index=0;
// If we are already loaded, don't bother reloading.
if(face && font==newfont)
return true;
if(face)
face = nullptr;
if (newfont.empty())
return false;
std::vector<const char *> possible_font_extensions = {""};
// if newfont doesn't have a known extension, try to append those extensions
if (! has_valid_font_extension(newfont))
possible_font_extensions.insert(possible_font_extensions.end(), known_font_extensions.begin(), known_font_extensions.end());
std::string canvas_path;
if (get_canvas())
canvas_path = get_canvas()->get_file_path()+ETL_DIRECTORY_SEPARATOR;
std::vector<std::string> possible_font_directories = get_possible_font_directories(canvas_path);
for (const std::string& directory : possible_font_directories) {
for (const char *extension : possible_font_extensions) {
std::string path = (directory + newfont + extension);
error = FT_New_Face(ft_library, path.c_str(), face_index, &face);
if (!error) {
font_path_from_canvas = !canvas_path.empty() && directory == canvas_path;
break;
}
}
if (!error)
break;
}
if(error)
{
synfig::error(strprintf("Layer_Freetype: %s (err=%d): %s",_("Unable to open font face."),error,newfont.c_str()));
return false;
}
// ???
font=newfont;
need_sync |= SYNC_FONT;
return true;
}
std::vector<std::string>
Layer_Freetype::get_possible_font_directories(const std::string& canvas_path)
{
std::vector<std::string> possible_font_directories = {""};
if (!canvas_path.empty())
possible_font_directories.push_back(canvas_path);
#ifdef _WIN32
// All users fonts
std::string windir = Glib::getenv("windir");
if (windir.empty()) {
possible_font_directories.emplace_back("C:\\WINDOWS\\FONTS\\");
} else {
possible_font_directories.emplace_back(windir + "\\Fonts\\");
}
// Windows 10 (1809) Added local user fonts installed to C:\Users\%USERNAME%\AppData\Local\Microsoft\Windows\Fonts
std::string localdir = Glib::getenv("LOCALAPPDATA");
if (!localdir.empty()) {
possible_font_directories.emplace_back(localdir + "\\Microsoft\\Windows\\Fonts\\");
}
#else
#ifdef __APPLE__
std::string userdir = Glib::getenv("HOME");
if (userdir.empty()) {
synfig::error(strprintf("Layer_Freetype: %s", _("Cannot retrieve user home folder")));
} else {
possible_font_directories.push_back(userdir+"/Library/Fonts/");
}
possible_font_directories.push_back("/Library/Fonts/");
#endif
possible_font_directories.push_back("/usr/share/fonts/truetype/");
possible_font_directories.push_back("/usr/share/fonts/opentype/");
#endif
return possible_font_directories;
}
bool
Layer_Freetype::set_simple_shape_param(const synfig::String ¶m, const synfig::ValueBase &value)
{
std::lock_guard<std::mutex> lock(mutex);
IMPORT_VALUE_PLUS(param_size,
{
if(old_version)
{
synfig::Vector size=param_size.get(synfig::Vector());
size/=2.0;
param_size.set(size);
}
}
);
return false;
}
bool
Layer_Freetype::set_shape_param(const String & param, const ValueBase &value)
{
std::lock_guard<std::mutex> lock(mutex);
/*
if(param=="font" && value.same_type_as(font))
{
new_font(etl::basename(value.get(font)),style,weight);
family=etl::basename(value.get(font));
return true;
}
*/
IMPORT_VALUE_PLUS(param_family,
{
synfig::String family=param_family.get(synfig::String());
int style=param_style.get(int());
int weight=param_weight.get(int());
new_font(family,style,weight);
}
);
IMPORT_VALUE_PLUS(param_weight,
{
synfig::String family=param_family.get(synfig::String());
int style=param_style.get(int());
int weight=param_weight.get(int());
new_font(family,style,weight);
}
);
IMPORT_VALUE_PLUS(param_style,
{
synfig::String family=param_family.get(synfig::String());
int style=param_style.get(int());
int weight=param_weight.get(int());
new_font(family,style,weight);
}
);
IMPORT_VALUE_PLUS(param_direction,need_sync |= SYNC_DIRECTION);
IMPORT_VALUE_PLUS(param_text,
{
on_param_text_changed();
}
);
IMPORT_VALUE_PLUS(param_orient,need_sync |= SYNC_ORIENTATION;);
IMPORT_VALUE_PLUS(param_compress,need_sync |= SYNC_COMPRESS);
IMPORT_VALUE_PLUS(param_vcompress,need_sync |= SYNC_COMPRESS);
IMPORT_VALUE_PLUS(param_use_kerning,need_sync |= SYNC_KERNING);
IMPORT_VALUE_PLUS(param_grid_fit,need_sync |= SYNC_GRID_FIT);
if(param=="pos")
return set_param("origin", value);
return false;
}
bool
Layer_Freetype::set_param(const String & param, const ValueBase &value)
{
if (set_simple_shape_param(param, value))
return true;
return Layer_Shape::set_param(param, value);
}
ValueBase
Layer_Freetype::get_param(const String& param)const
{
EXPORT_VALUE(param_font);
EXPORT_VALUE(param_family);
EXPORT_VALUE(param_style);
EXPORT_VALUE(param_weight);
EXPORT_VALUE(param_direction);
EXPORT_VALUE(param_size);
EXPORT_VALUE(param_text);
EXPORT_VALUE(param_orient);
EXPORT_VALUE(param_compress);
EXPORT_VALUE(param_vcompress);
EXPORT_VALUE(param_use_kerning);
EXPORT_VALUE(param_grid_fit);
EXPORT_NAME();
EXPORT_VERSION();
return Layer_Shape::get_param(param);
}
Layer::Vocab
Layer_Freetype::get_param_vocab(void)const
{
Layer::Vocab ret(Layer_Composite::get_param_vocab());
// Ignore the Layer_Shape params
ret.push_back(ParamDesc("text")
.set_local_name(_("Text"))
.set_description(_("Text to Render"))
.set_hint("paragraph")
);
ret.push_back(ParamDesc("color")
.set_local_name(_("Color"))
.set_description(_("Color of the text"))
);
ret.push_back(ParamDesc("family")
.set_local_name(_("Font Family"))
.set_hint("font_family")
);
ret.push_back(ParamDesc("style")
.set_local_name(_("Style"))
.set_hint("enum")
.set_static(true)
.add_enum_value(TEXT_STYLE_NORMAL, "normal" ,_("Normal"))
.add_enum_value(TEXT_STYLE_OBLIQUE, "oblique" ,_("Oblique"))
.add_enum_value(TEXT_STYLE_ITALIC, "italic" ,_("Italic"))
);
ret.push_back(ParamDesc("weight")
.set_local_name(_("Weight"))
.set_hint("enum")
.set_static(true)
.add_enum_value(TEXT_WEIGHT_THIN, "thin" ,_("Thin"))
.add_enum_value(TEXT_WEIGHT_ULTRALIGHT, "ultralight" ,_("Ultralight"))
.add_enum_value(TEXT_WEIGHT_LIGHT, "light" ,_("Light"))
.add_enum_value(TEXT_WEIGHT_BOOK, "book" ,_("Book"))
.add_enum_value(TEXT_WEIGHT_NORMAL, "normal" ,_("Normal"))
.add_enum_value(TEXT_WEIGHT_MEDIUM, "medium" ,_("Medium"))
.add_enum_value(TEXT_WEIGHT_BOLD, "bold" ,_("Bold"))
.add_enum_value(TEXT_WEIGHT_ULTRABOLD, "ultrabold" ,_("Ultrabold"))
.add_enum_value(TEXT_WEIGHT_HEAVY, "heavy" ,_("Heavy"))
.add_enum_value(TEXT_WEIGHT_ULTRAHEAVY, "ultraheavy" ,_("Ultraheavy"))
);
ret.push_back(ParamDesc("direction")
.set_local_name(_("Direction"))
.set_description(_("The text direction: left-to-right or right-to-left"))
.set_hint("enum")
.set_static(true)
.add_enum_value(TEXT_DIRECTION_AUTO, "auto" ,_("Automatic"))
.add_enum_value(TEXT_DIRECTION_LTR, "ltr" ,_("LTR"))
.add_enum_value(TEXT_DIRECTION_RTL, "rtl" ,_("RTL"))
);
ret.push_back(ParamDesc("compress")
.set_local_name(_("Horizontal Spacing"))
.set_description(_("Defines how close the glyphs are horizontally"))
);
ret.push_back(ParamDesc("vcompress")
.set_local_name(_("Vertical Spacing"))
.set_description(_("Defines how close the text lines are vertically"))
);
ret.push_back(ParamDesc("size")
.set_local_name(_("Size"))
.set_description(_("Size of the text"))
.set_hint("size")
.set_origin("origin")
.set_is_distance()
);
ret.push_back(ParamDesc("orient")
.set_local_name(_("Orientation"))
.set_description(_("Text Orientation"))
.set_invisible_duck()
);
ret.push_back(ParamDesc("origin")
.set_local_name(_("Origin"))
.set_description(_("Text Position"))
.set_is_distance()
);
ret.push_back(ParamDesc("font")
.set_local_name(_("Font"))
.set_description(_("Filename of the font to use"))
.set_hint("filename")
.not_critical()
.hidden()
);
ret.push_back(ParamDesc("use_kerning")
.set_local_name(_("Kerning"))
.set_description(_("When checked, enables font kerning (If the font supports it)"))
);
ret.push_back(ParamDesc("grid_fit")
.set_local_name(_("Sharpen Edges"))
.set_description(_("Turn this off if you are animating the text"))
);
ret.push_back(ParamDesc("invert")
.set_local_name(_("Invert"))
);
return ret;
}
void
Layer_Freetype::sync_vfunc()
{
std::lock_guard<std::mutex> lock(sync_mtx);
clear();
std::string text = param_text.get(std::string());
if (synfig::trim(text).empty() || !face) {
lines.clear();
return;
}
const bool use_kerning = param_use_kerning.get(bool());
const bool grid_fit = param_grid_fit.get(bool());
const Vector orient = param_orient.get(Vector());
const Real compress = param_compress.get(Real());
const Real vcompress = param_vcompress.get(Real());
const int direction = param_direction.get(0);
if(text=="@_FILENAME_@" && get_canvas() && !get_canvas()->get_file_name().empty())
{
auto text=basename(get_canvas()->get_file_name());
lines = fetch_text_lines(text, direction);
}
#if HAVE_HARFBUZZ
hb_buffer_t *span_buffer = hb_buffer_create();
std::unique_ptr<hb_buffer_t, decltype(&hb_buffer_destroy)> safe_buf(span_buffer, hb_buffer_destroy); // auto delete
#endif
// Lines of glyph indices
// Depends on: font and text
std::vector<std::vector<uint32_t>> glyph_indices;
for (const TextLine& line : lines)
{
std::vector<uint32_t> glyph_index_line;
for (const TextSpan& span : line) {
#if HAVE_HARFBUZZ
hb_buffer_clear_contents(span_buffer);
hb_direction_t direction = HB_DIRECTION_LTR; // character order already fixed by FriBiDi
hb_buffer_set_direction(span_buffer, direction);
hb_buffer_set_script(span_buffer, span.script);
// hb_buffer_set_language(span_buffer, hb_language_from_string(language.c_str(), -1));
hb_buffer_add_utf32(span_buffer, span.codepoints.data(), span.codepoints.size(), 0, -1);
hb_shape(font, span_buffer, nullptr, 0);
unsigned int glyph_count;
hb_glyph_info_t *glyph_info = hb_buffer_get_glyph_infos(span_buffer, &glyph_count);
#else
size_t glyph_count = span.codepoints.size();
#endif
for (size_t i = 0; i < glyph_count; i++) {
uint32_t glyph_index;
#if HAVE_HARFBUZZ
glyph_index = glyph_info[i].codepoint;
#else
glyph_index = FT_Get_Char_Index(face, span.codepoints[i]);
#endif
glyph_index_line.push_back(glyph_index);
}
}
glyph_indices.push_back(glyph_index_line);
}
// get visual info
// Depends on: glyph indices, font and grid_fit
struct Glyph {
Vector advance;
FT_BBox bbox;
rendering::Contour::ChunkList outline;
};
std::map<uint32_t, Glyph> glyph_map;
for (const std::vector<uint32_t>& glyph_line : glyph_indices)
{
for (const uint32_t glyph_index : glyph_line) {
if (glyph_map.count(glyph_index))
continue;