Source

libeasyfc / libeasyfc / ezfc-font.c

Full commit
  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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* 
 * ezfc-font.c
 * Copyright (C) 2011-2012 Akira TAGOH
 * 
 * Authors:
 *   Akira TAGOH  <akira@tagoh.org>
 * 
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 * 
 * This library 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <fontconfig/fontconfig.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_TRUETYPE_TABLES_H
#include <libeasyfc/ezfc-error.h>
#include "ezfc-mem.h"
#include <libeasyfc/ezfc-font.h>
#include "ezfc-font-private.h"


/**
 * SECTION: ezfc-font
 * @Short_Description: A collection of the functions for fonts
 * @Title: Fonts
 *
 * This collects some functions related to fonts.
 *
 * Since: 0.7
 */
typedef enum {
	EZFC_FONT_UNKNOWN   = 0,
	EZFC_FONT_SANS      = 1 << 0,
	EZFC_FONT_SERIF     = 1 << 1,
	EZFC_FONT_MONOSPACE = 1 << 2,
	EZFC_FONT_CURSIVE   = 1 << 3,
	EZFC_FONT_FANTASY   = 1 << 4,
	EZFC_FONT_END
} ezfc_font_variant_t;
typedef GList * (ezfc_proc_t) (GList *, const FcPattern *);

extern FT_Library _ezfc_get_freetype(void);

static gchar *__ezfc_font_alias_names[] = {
	"sans",
	"sans-serif",
	"serif",
	"monospace",
	"cursive",
	"fantasy",
	NULL
};

/*< private >*/
static FT_Face
_ezfc_font_get_face(const FcPattern *pattern)
{
	FcChar8 *file;
	int idx;
	FT_Face face = NULL;

	FcPatternGetString(pattern, FC_FILE, 0, &file);
	FcPatternGetInteger(pattern, FC_INDEX, 0, &idx);

	if (FT_New_Face(_ezfc_get_freetype(), (char *)file, idx, &face)) {
		g_warning("Unable to open the font file '%s' index %d",
			  file, idx);
	}

	return face;
}

static ezfc_font_variant_t
_ezfc_font_get_font_type(const gchar *font)
{
	gint i;

	for (i = 0; __ezfc_font_alias_names[i] != NULL; i++) {
		if (g_ascii_strcasecmp(font, __ezfc_font_alias_names[i]) == 0) {
			if (i == 0)
				i++;
			return 1 << (i - 1);
		}
	}

	return EZFC_FONT_UNKNOWN;
}

static ezfc_font_variant_t
_ezfc_font_get_font_type_from_pattern(const FcPattern *pattern)
{
	int spacing = 0;
	ezfc_font_variant_t retval = EZFC_FONT_UNKNOWN;
	TT_OS2 *os2;
	FT_Face face = NULL;
	FT_Byte cls_id, subcls_id;

	FcPatternGetInteger(pattern, FC_SPACING, 0, &spacing);
	if (spacing == FC_MONO ||
	    spacing == FC_DUAL ||
	    spacing == FC_CHARCELL) {
		retval = EZFC_FONT_MONOSPACE;
	}

	face = _ezfc_font_get_face(pattern);
	os2 = (TT_OS2 *)FT_Get_Sfnt_Table(face, ft_sfnt_os2);
	if (!os2)
		goto bail;
	/* See http://www.microsoft.com/typography/otspec/os2.htm#fc
	 * and http://www.microsoft.com/typography/otspec/ibmfc.htm
	 * for the reference of sFamilyClass.
	 */
	cls_id = (os2->sFamilyClass >> 8) & 0xff;
	subcls_id = os2->sFamilyClass & 0xff;
	switch (cls_id) {
	    case 0: /* No Classification */
	    case 6: /* reserved for future use */
	    case 11: /* reserved for future use */
	    case 13: /* Reserved */
	    case 14: /* Reserved */
		    break;
	    case 1: /* Oldstyle Serifs */
		    retval |= EZFC_FONT_SERIF;
		    if (subcls_id == 8) /* Calligraphic */
			    retval |= EZFC_FONT_CURSIVE;
		    break;
	    case 2: /* Transitional Serifs */
	    case 3: /* Modern Serifs */
		    retval |= EZFC_FONT_SERIF;
		    if (subcls_id == 2) /* Script */
			    retval |= EZFC_FONT_CURSIVE;
		    break;
	    case 4: /* Clarendon Serifs */
	    case 5: /* Slab Serifs */
	    case 7: /* Freeform Serifs */
		    retval |= EZFC_FONT_SERIF;
		    break;
	    case 8: /* Sans Serif */
		    retval |= EZFC_FONT_SANS;
		    break;
	    case 9: /* Ornamentals */
	    case 12: /* Symbolic */
		    retval |= EZFC_FONT_FANTASY;
		    break;
	    case 10: /* Scripts */
		    retval |= EZFC_FONT_CURSIVE;
		    break;
	    default:
		    g_warning("Unknown sFamilyClass class ID: %d", cls_id);
		    break;
	}
	/* See http://www.monotypeimaging.com/ProductsServices/pan1.aspx
	 * for the reference of Panose
	 */
	if (os2->panose[0] == 3) {
		/* Latin Hand Written */
		retval |= EZFC_FONT_CURSIVE;
	} else if (os2->panose[0] >= 4 &&
		   os2->panose[0] <= 5) {
		/* 4...Latin Decorative
		 * 5...Latin Symbol
		 */
		retval |= EZFC_FONT_FANTASY;
	} else if (os2->panose[0] == 2 &&
		   ((os2->panose[1] >= 11 &&
		     os2->panose[1] <= 13) ||
		    os2->panose[1] == 15)) {
		/* 2..Latin Text */
		/*   11...Normal Sans
		 *   12...Obtuse Sans
		 *   13...Perpendicular Sans
		 *   15...Pounded
		 */
		retval |= EZFC_FONT_SANS;
	} else if (os2->panose[0] == 1) {
		/* "Not Fit" in the family kind should be ignored. */
	} else if (os2->panose[0] == 0 &&
		   os2->panose[1] == 1) {
		/* Unable to determine the alias type */
	} else {
		retval |= EZFC_FONT_SERIF;
	}
	if ((os2->panose[0] == 2 &&
	     os2->panose[3] == 9) ||
	    (os2->panose[0] == 3 &&
	     os2->panose[3] == 3) ||
	    (os2->panose[0] == 4 &&
	     os2->panose[3] == 9) ||
	    (os2->panose[0] == 5 &&
	     os2->panose[3] == 3)) {
		retval |= EZFC_FONT_MONOSPACE;
	}
  bail:
	FT_Done_Face(face);

	return retval;
}

static gboolean
_ezfc_font_is_font_a(const FcPattern *pattern,
		     const gchar     *alias_name)
{
	ezfc_font_variant_t type = _ezfc_font_get_font_type(alias_name);
	ezfc_font_variant_t ptype = _ezfc_font_get_font_type_from_pattern(pattern);

	return (type & ptype) == type;
}

static GList *
_ezfc_font_get_fonts_proc(const gchar *language,
			  const gchar *alias_name,
			  gboolean     localized_font_name,
			  ezfc_proc_t  func)
{
	FcPattern *pat;
	FcFontSet *fs;
	FcObjectSet *os;
	GList *retval = NULL;
	FcLangSet *ls = NULL;

	g_return_val_if_fail (func != NULL, NULL);

	if (alias_name) {
		g_return_val_if_fail (ezfc_font_is_alias_font(alias_name), NULL);
	}
	pat = FcPatternCreate();
	if (language && language[0] != 0) {
		ls = FcLangSetCreate();
		FcLangSetAdd(ls, (const FcChar8 *)language);
		FcPatternAddLangSet(pat, FC_LANG, ls);
	}
	if (!localized_font_name)
		FcPatternAddString(pat, FC_NAMELANG, (const FcChar8 *)"en");
	os = FcObjectSetBuild(FC_FAMILY, FC_FILE, FC_INDEX, FC_SPACING, NULL);
	fs = FcFontList(NULL, pat, os);
	FcObjectSetDestroy(os);
	FcPatternDestroy(pat);
	if (ls)
		FcLangSetDestroy(ls);
	if (fs) {
		gint i;

		for (i = 0; i < fs->nfont; i++) {
			if (!alias_name || _ezfc_font_is_font_a(fs->fonts[i], alias_name)) {
				retval = func(retval, fs->fonts[i]);
			}
		}
		FcFontSetDestroy(fs);
	}

	return retval;
}

static GList *
_ezfc_font_get_fonts_cb_family(GList           *list,
			       const FcPattern *pattern)
{
	FcChar8 *family;
	GList *l;
	gboolean added = FALSE;

	FcPatternGetString(pattern, FC_FAMILY, 0, &family);
	for (l = list; l != NULL; l = g_list_next(l)) {
		gint r = g_strcmp0(l->data, (const gchar *)family);
		GList *ll;

		if (r == 0) {
			/* do not add it */
			added = TRUE;
			break;
		} else if (r > 0) {
			ll = g_list_prepend(l, g_strdup((const gchar *)family));

			if (list == l)
				list = ll;
			added = TRUE;
			break;
		}
	}
	if (!added)
		list = g_list_append(list, g_strdup((const gchar *)family));

	return list;
}

static GList *
_ezfc_font_get_fonts_cb_pattern(GList           *list,
				const FcPattern *pattern)
{
	return g_list_append(list, FcPatternDuplicate(pattern));
}

/*< protected >*/
void
ezfc_font_init(ezfc_font_t *font)
{
	ezfc_font_private_t *priv = (ezfc_font_private_t *)font;

	g_return_if_fail (font != NULL);

	priv->check_font_existence = TRUE;
}

/*< public >*/

/**
 * ezfc_font_is_alias_font:
 * @alias_name: the alias font name
 *
 * Checks if @alias_name is one of sans-serif, serif, monospace, cursive or fantasy.
 *
 * Returns: %TRUE if @alias_name is an alias font name, otherwise %FALSE.
 */
gboolean
ezfc_font_is_alias_font(const gchar *alias_name)
{
	ezfc_font_variant_t t = _ezfc_font_get_font_type(alias_name);

	return t != EZFC_FONT_UNKNOWN;
}

/**
 * ezfc_font_get_list:
 * @language: (allow-none): the language name fontconfig can deal with.
 * @alias_name: (allow-none): the alias name to obtain the fonts list for.
 * @localized_font_name: %TRUE to include the localized font name if available,
 *                       %FALSE for English font name only.
 *
 * Obtains the fonts list being assigned to @alias_name for @language.
 *
 * Note that @localized_font_name doesn't take effect yet. this is just
 * a reservation for future improvement.
 *
 * Returns: (element-type utf8) (transfer full): a #GList contains the font family name.
 *          if no valid families, %NULL then.
 */
GList *
ezfc_font_get_list(const gchar *language,
		   const gchar *alias_name,
		   gboolean     localized_font_name)
{
	return _ezfc_font_get_fonts_proc(language, alias_name,
					 localized_font_name,
					 _ezfc_font_get_fonts_cb_family);
}

/**
 * ezfc_font_get_pattern_list:
 * @language: (allow-none): the language name fontconfig can deal with.
 * @alias_name: (allow-none): the alias name to obtain the fonts pettern list for.
 *
 * Obtains #FcPattern list being assigned to @alias_name for @language.
 *
 * Returns: (element-type FcPattern) (transfer full): a #GList contains #FcPattern, otherwise %NULL.
 */
GList *
ezfc_font_get_pattern_list(const gchar *language,
			   const gchar *alias_name)
{
	return _ezfc_font_get_fonts_proc(language, alias_name, TRUE,
					 _ezfc_font_get_fonts_cb_pattern);
}

/**
 * ezfc_font_get_alias_name_from_pattern:
 * @pattern: a #FcPattern.
 *
 * Analize @pattern and returns a alias name string according to the result.
 *
 * Returns: (transfer container) (element-type utf8): a #GList containing
 *          a static string for the alias name.
 */
GList *
ezfc_font_get_alias_name_from_pattern(const FcPattern *pattern)
{
	GList *retval = NULL;
	ezfc_font_variant_t type = _ezfc_font_get_font_type_from_pattern(pattern);
	gint i, j;

	if (type == EZFC_FONT_UNKNOWN)
		return NULL;

	for (i = EZFC_FONT_SANS, j = 1; i < EZFC_FONT_END; i <<= 1, j++) {
		if ((type & i) == i)
			retval = g_list_append(retval, __ezfc_font_alias_names[j]);
	}

	return retval;
}

/**
 * ezfc_font_new:
 *
 * Create an instance of #ezfc_font_t.
 *
 * Returns: a #ezfc_font_t.
 */
ezfc_font_t *
ezfc_font_new(void)
{
	ezfc_font_private_t *retval = ezfc_mem_alloc_object(sizeof (ezfc_font_private_t));

	if (retval) {
		ezfc_font_init((ezfc_font_t *)retval);
	}

	return (ezfc_font_t *)retval;
}

/**
 * ezfc_font_ref:
 * @font: a #ezfc_font_t.
 *
 * Increases the reference count of @font.
 *
 * Returns: (transfer none): the same @font object.
 */
ezfc_font_t *
ezfc_font_ref(ezfc_font_t *font)
{
	ezfc_font_private_t *priv = (ezfc_font_private_t *)font;

	return ezfc_mem_ref(&priv->parent);
}

/**
 * ezfc_font_unref:
 * @font: a #ezfc_font_t.
 *
 * Decreases the reference count of @font. when its reference count
 * drops to 0, the object is finalized (i.e. its memory is freed).
 */
void
ezfc_font_unref(ezfc_font_t *font)
{
	ezfc_font_private_t *priv = (ezfc_font_private_t *)font;

	if (priv)
		ezfc_mem_unref(&priv->parent);
}

/**
 * ezfc_font_get_pattern:
 * @font: a #ezfc_font_t.
 *
 * Obtains #FcPattern in #ezfc_font_t.
 *
 * Returns: a duplicate of #FcPattern in the instance. it has to be freed.
 *          %NULL if @font doesn't have any font pattern.
 */
FcPattern *
ezfc_font_get_pattern(ezfc_font_t *font)
{
	ezfc_font_private_t *priv = (ezfc_font_private_t *)font;

	g_return_val_if_fail (font != NULL, NULL);

	if (priv->pattern)
		return FcPatternDuplicate(priv->pattern);

	return NULL;
}

/**
 * ezfc_font_set_pattern:
 * @font: a #ezfc_font_t.
 * @pattern: a #FcPattern.
 * @error: (allow-none): a #GError.
 *
 * Set @pattern as the font pattern. @font keeps a duplicate instance of
 * @pattern.
 *
 * Returns: %TRUE if it successfully is set. otherwise %FALSE.
 */
gboolean
ezfc_font_set_pattern(ezfc_font_t      *font,
		      const FcPattern  *pattern,
		      GError          **error)
{
	ezfc_font_private_t *priv = (ezfc_font_private_t *)font;
	FcPattern *pat;
	gboolean retval = TRUE;
	GError *err = NULL;

	g_return_val_if_fail (font != NULL, FALSE);
	g_return_val_if_fail (pattern != NULL, FALSE);

	pat = FcPatternDuplicate(pattern);
	if (!pat) {
		g_set_error(&err, EZFC_ERROR, EZFC_ERR_OOM,
			    "Unable to make a duplicate of FcPattern");
		retval = FALSE;
		goto bail;
	}
	if (priv->pattern)
		ezfc_mem_remove_ref(&priv->parent, priv->pattern);
	priv->pattern = pat;
	ezfc_mem_add_ref(&priv->parent, pat, (ezfc_destroy_func_t)FcPatternDestroy);

  bail:
	if (err) {
		if (error)
			*error = g_error_copy(err);
		else
			g_warning(err->message);
		g_error_free(err);
	}

	return retval;
}

/**
 * ezfc_font_get_family:
 * @font: a #ezfc_font_t.
 *
 * Obtains the font family name in @font.
 *
 * Returns: the font name.
 */
const gchar *
ezfc_font_get_family(ezfc_font_t *font)
{
	ezfc_font_private_t *priv = (ezfc_font_private_t *)font;
	gchar *family = NULL;

	g_return_val_if_fail (font != NULL, NULL);

	if (priv->pattern &&
	    FcPatternGetString(priv->pattern, FC_FAMILY, 0, (FcChar8 **)&family) != FcResultMatch)
		return NULL;

	return family;
}

/**
 * ezfc_font_set_family:
 * @font: a #ezfc_font_t.
 * @font_name: a font name.
 * @error: (allow-none): a #GError.
 *
 * Set @font_name as the font family name used for the font font.
 *
 * Returns: %TRUE if it successfully is set. otherwise %FALSE.
 */
gboolean
ezfc_font_set_family(ezfc_font_t   *font,
		     const gchar   *font_name,
		     GError       **error)
{
	ezfc_font_private_t *priv = (ezfc_font_private_t *)font;
	FcPattern *pat, *match = NULL;
	FcResult result;
	gboolean retval = TRUE;
	GError *err = NULL;

	g_return_val_if_fail (font != NULL, FALSE);
	g_return_val_if_fail (font_name != NULL, FALSE);

	pat = FcNameParse((FcChar8 *)font_name);
	if (!pat) {
		g_set_error(&err, EZFC_ERROR, EZFC_ERR_FAIL_ON_FC,
			    "Unable to parse `%s'",
			    font_name);
		retval = FALSE;
		goto bail;
	}
	if (!priv->check_font_existence) {
		retval = ezfc_font_set_pattern(font, pat, &err);
		goto bail;
	}
	if ((match = FcFontMatch(NULL, pat, &result))) {
		FcChar8 *v1, *v2;

		if (FcPatternGetString(pat, FC_FAMILY, 0, &v1) != FcResultMatch) {
			g_set_error(&err, EZFC_ERROR, EZFC_ERR_NO_FAMILY,
				    "Pattern doesn't contain any family name");
			retval = FALSE;
			goto bail1;
		}
		if (FcPatternGetString(match, FC_FAMILY, 0, &v2) != FcResultMatch) {
			g_set_error(&err, EZFC_ERROR, EZFC_ERR_NO_FAMILY,
				    "Pattern doesn't contain any family name");
			retval = FALSE;
			goto bail1;
		}
		if (FcStrCmpIgnoreCase(v1, v2) == 0) {
			retval = ezfc_font_set_pattern(font, pat, &err);
		} else {
			g_set_error(&err, EZFC_ERROR, EZFC_ERR_NO_VALID_FONT,
				    "No such fonts available: %s",
				    font_name);
			retval = FALSE;
			goto bail1;
		}
	  bail1:
		FcPatternDestroy(match);
	} else {
		g_set_error(&err, EZFC_ERROR, EZFC_ERR_NO_VALID_FONT,
			    "No such fonts available: %s",
			    font_name);
		retval = FALSE;
		goto bail;
	}
  bail:
	FcPatternDestroy(pat);

	if (err) {
		if (error)
			*error = g_error_copy(err);
		else
			g_warning(err->message);
		g_error_free(err);
	}

	return retval;
}

/**
 * ezfc_font_check_existence:
 * @font: a #ezfc_font_t.
 * @flag: a boolean value.
 *
 * Set a flag whether checking the font existence when invoking
 * ezfc_font_set_family().
 */
void
ezfc_font_check_existence(ezfc_font_t *font,
			  gboolean     flag)
{
	ezfc_font_private_t *priv = (ezfc_font_private_t *)font;

	g_return_if_fail (font != NULL);

	priv->check_font_existence = (flag == TRUE);
}

/**
 * ezfc_font_set_hinting:
 * @font: a #ezfc_font_t.
 * @flag: a boolean value.
 *
 * Set a flag whether the font use the own hints for rendering
 */
void
ezfc_font_set_hinting(ezfc_font_t *font,
		      gboolean     flag)
{
	ezfc_font_private_t *priv = (ezfc_font_private_t *)font;

	g_return_if_fail (font != NULL);

	priv->masks |= EZFC_FONT_MASK_HINTING;
	priv->hinting = (flag == TRUE);
}

/**
 * ezfc_font_get_hinting:
 * @font: a #ezfc_font_t.
 *
 * Obtain a boolean value about the hinting usage in @font.
 *
 * Returns: %TRUE if the hinting is enabled. otherwise %FALSE.
 */
gboolean
ezfc_font_get_hinting(ezfc_font_t *font)
{
	ezfc_font_private_t *priv = (ezfc_font_private_t *)font;

	g_return_val_if_fail (font != NULL, FALSE);

	return priv->hinting;
}

/**
 * ezfc_font_set_autohinting:
 * @font: a #ezfc_font_t.
 * @flag: a boolean value.
 *
 * Set a flag whether the font use the auto-hinting for rendering
 */
void
ezfc_font_set_autohinting(ezfc_font_t *font,
			  gboolean     flag)
{
	ezfc_font_private_t *priv = (ezfc_font_private_t *)font;

	g_return_if_fail (font != NULL);

	priv->masks |= EZFC_FONT_MASK_AUTOHINT;
	priv->autohinting = (flag == TRUE);
}

/**
 * ezfc_font_get_autohinting:
 * @font: a #ezfc_font_t.
 *
 * Obtain a boolean value about the auto-hinting usage in @font.
 *
 * Returns: %TRUE if the auto-hinting is enabled. otherwise %FALSE.
 */
gboolean
ezfc_font_get_autohinting(ezfc_font_t *font)
{
	ezfc_font_private_t *priv = (ezfc_font_private_t *)font;

	g_return_val_if_fail (font != NULL, FALSE);

	return priv->autohinting;
}

/**
 * ezfc_font_set_hintstyle:
 * @font: a #ezfc_font_t.
 * @hintstyle: a #ezfc_font_hintstyle_t.
 *
 * Set a hintstyle for @font.
 */
void
ezfc_font_set_hintstyle(ezfc_font_t           *font,
			ezfc_font_hintstyle_t  hintstyle)
{
	ezfc_font_private_t *priv = (ezfc_font_private_t *)font;

	g_return_if_fail (font != NULL);
	g_return_if_fail (hintstyle > EZFC_FONT_HINTSTYLE_UNKNOWN && hintstyle < EZFC_FONT_HINTSTYLE_END);

	priv->masks |= EZFC_FONT_MASK_HINTSTYLE;
	priv->hintstyle = hintstyle;
}

/**
 * ezfc_font_get_hintstyle:
 * @font: a #ezfc_font_t.
 *
 * Obtain the hintstyle in @font.
 *
 * Returns: a #ezfc_font_hintstyle_t.
 */
ezfc_font_hintstyle_t
ezfc_font_get_hintstyle(ezfc_font_t *font)
{
	ezfc_font_private_t *priv = (ezfc_font_private_t *)font;

	g_return_val_if_fail (font != NULL, EZFC_FONT_HINTSTYLE_UNKNOWN);

	return priv->hintstyle;
}

/**
 * ezfc_font_set_antialiasing:
 * @font: a #ezfc_font_t.
 * @flag: a boolean value.
 *
 * Set a flag whether the font use the antialiasing.
 */
void
ezfc_font_set_antialiasing(ezfc_font_t *font,
			   gboolean     flag)
{
	ezfc_font_private_t *priv = (ezfc_font_private_t *)font;

	g_return_if_fail (font != NULL);

	priv->masks |= EZFC_FONT_MASK_ANTIALIAS;
	priv->antialiasing = (flag == TRUE);
	if (flag && ezfc_font_get_embedded_bitmap(font))
		ezfc_font_set_embedded_bitmap(font, FALSE);
}

/**
 * ezfc_font_get_antialiasing:
 * @font: a #ezfc_font_t.
 *
 * Obtain a boolean value about the anti-aliasing usage in @font.
 *
 * Returns: %TRUE if the antialiasing is enabled. otherwise %FALSE.
 */
gboolean
ezfc_font_get_antialiasing(ezfc_font_t *font)
{
	ezfc_font_private_t *priv = (ezfc_font_private_t *)font;

	g_return_val_if_fail (font != NULL, FALSE);

	return priv->antialiasing;
}

/**
 * ezfc_font_set_embedded_bitmap:
 * @font: a #ezfc_font_t.
 * @flag: a boolean value.
 *
 * Set a flag whether the font use the embedded bitmap.
 * Note that Enabling the embedded bitmap causes disabling the antialias.
 */
void
ezfc_font_set_embedded_bitmap(ezfc_font_t *font,
			      gboolean     flag)
{
	ezfc_font_private_t *priv = (ezfc_font_private_t *)font;

	g_return_if_fail (font != NULL);

	priv->masks |= EZFC_FONT_MASK_EMBEDDED_BITMAP;
	priv->embedded_bitmap = (flag == TRUE);
	/* This implies changing the antialias too */
	if (flag)
		ezfc_font_set_antialiasing(font, FALSE);
}

/**
 * ezfc_font_get_embedded_bitmap:
 * @font: a #ezfc_font_t.
 *
 * Obtain a boolean value about the embedded bitmap usage in @font.
 *
 * Returns: %TRUE if the embedded bitmap is enabled, otherwise %FALSE.
 */
gboolean
ezfc_font_get_embedded_bitmap(ezfc_font_t *font)
{
	ezfc_font_private_t *priv = (ezfc_font_private_t *)font;

	g_return_val_if_fail (font != NULL, FALSE);

	return priv->embedded_bitmap;
}

/**
 * ezfc_font_set_rgba:
 * @font: a #ezfc_font_t.
 * @val: an integer value corresponding to FC_RGBA_*.
 *
 * Set @val as the sub-pixel ordering
 */
void
ezfc_font_set_rgba(ezfc_font_t *font,
		   gint         val)
{
	ezfc_font_private_t *priv = (ezfc_font_private_t *)font;

	g_return_if_fail (font != NULL);

	priv->masks |= EZFC_FONT_MASK_RGBA;
	priv->rgba = val;
}

/**
 * ezfc_font_get_rgba:
 * @font: a #ezfc_font_t.
 *
 * Obtains current sub-pixel ordering in @font.
 *
 * Returns: the sub-pixel ordering value in the integer.
 */
gint
ezfc_font_get_rgba(ezfc_font_t *font)
{
	ezfc_font_private_t *priv = (ezfc_font_private_t *)font;

	g_return_val_if_fail (font != NULL, FC_RGBA_UNKNOWN);

	return priv->rgba;
}

/**
 * ezfc_font_get_boolean_masks:
 * @font: a #ezfc_font_t.
 *
 * Obtain masks that any boolean values are set to @font.
 * This is set when ezfc_font_set_hinting(), ezfc_font_set_autohinting()
 * and ezfc_font_set_antialiasing() is called.
 *
 * Returns: an integer value
 */
gint
ezfc_font_get_boolean_masks(ezfc_font_t *font)
{
	ezfc_font_private_t *priv = (ezfc_font_private_t *)font;

	g_return_val_if_fail (font != NULL, 0);

	return priv->masks;
}

/**
 * ezfc_font_set_subpixel_rendering:
 * @font: a #ezfc_font_t.
 * @mode: a #ezfc_font_subpixel_render_t.
 *
 * This is just convenient to change the several configuration for subpixel
 * rendering.
 *
 * Returns: %TRUE if the sub-pixel rendering is enabled. otherwise %FALSE.
 */
gboolean
ezfc_font_set_subpixel_rendering(ezfc_font_t                 *font,
				 ezfc_font_subpixel_render_t  mode)
{
	g_return_val_if_fail (font != NULL, FALSE);
	g_return_val_if_fail (mode < EZFC_FONT_ANTIALIAS_END, FALSE);

	switch (mode) {
	    case EZFC_FONT_ANTIALIAS_NONE:
		    ezfc_font_set_antialiasing(font, FALSE);
		    ezfc_font_set_rgba(font, FC_RGBA_NONE);
		    break;
	    case EZFC_FONT_ANTIALIAS_GRAY:
		    ezfc_font_set_antialiasing(font, TRUE);
		    ezfc_font_set_rgba(font, FC_RGBA_NONE);
		    break;
	    case EZFC_FONT_ANTIALIAS_RGB:
		    ezfc_font_set_antialiasing(font, TRUE);
		    ezfc_font_set_rgba(font, FC_RGBA_RGB);
		    break;
	    case EZFC_FONT_ANTIALIAS_BGR:
		    ezfc_font_set_antialiasing(font, TRUE);
		    ezfc_font_set_rgba(font, FC_RGBA_BGR);
		    break;
	    case EZFC_FONT_ANTIALIAS_VRGB:
		    ezfc_font_set_antialiasing(font, TRUE);
		    ezfc_font_set_rgba(font, FC_RGBA_VRGB);
		    break;
	    case EZFC_FONT_ANTIALIAS_VBGR:
		    ezfc_font_set_antialiasing(font, TRUE);
		    ezfc_font_set_rgba(font, FC_RGBA_VBGR);
		    break;
	    default:
		    g_return_val_if_reached (FALSE);
	}

	return TRUE;
}

/**
 * ezfc_font_get_subpixel_rendering:
 * @font: a #ezfc_font_t.
 *
 * Obtain current status about the sub-pixel rendering in @font.
 *
 * Returns: current mode in the sub-pixel rendering.
 */
ezfc_font_subpixel_render_t
ezfc_font_get_subpixel_rendering(ezfc_font_t *font)
{
	gboolean antialias;
	gint rgba;
	ezfc_font_subpixel_render_t retval;

	g_return_val_if_fail (font != NULL, EZFC_FONT_ANTIALIAS_UNKNOWN);

	antialias = ezfc_font_get_antialiasing(font);
	rgba = ezfc_font_get_rgba(font);

	if (antialias) {
		switch (rgba) {
		    case FC_RGBA_NONE:
			    retval = EZFC_FONT_ANTIALIAS_GRAY;
			    break;
		    case FC_RGBA_RGB:
			    retval = EZFC_FONT_ANTIALIAS_RGB;
			    break;
		    case FC_RGBA_BGR:
			    retval = EZFC_FONT_ANTIALIAS_BGR;
			    break;
		    case FC_RGBA_VRGB:
			    retval = EZFC_FONT_ANTIALIAS_VRGB;
			    break;
		    case FC_RGBA_VBGR:
			    retval = EZFC_FONT_ANTIALIAS_VBGR;
			    break;
		    default:
			    retval = EZFC_FONT_ANTIALIAS_UNKNOWN;
			    break;
		}
	} else {
		/* no matter what rgba is, if antialias is false,
		 * it should be None.
		 */
		retval = EZFC_FONT_ANTIALIAS_NONE;
	}

	return retval;
}