Skip to content

Commit 2932246

Browse files
coderobelc-soft
authored andcommitted
feat(linux): use fontconfig to locate fonts
Font paths are not guaranteed to be the same across different linux systems, this commit introduces Fontconfig support to locate the font files based on their font names. see also: #136
1 parent 42b6141 commit 2932246

File tree

9 files changed

+155
-13
lines changed

9 files changed

+155
-13
lines changed

configure.ac

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,23 @@ else
107107
want_font_engine=no
108108
fi
109109

110+
# fontconfig
111+
want_fontconfig=yes
112+
AC_ARG_ENABLE(fontconfig, AC_HELP_STRING([--enable-fontconfig],
113+
[turn on fontconfig [[default=yes]]]), [want_fontconfig=$enableval])
114+
if test "$want_fontconfig" = yes; then
115+
AC_CHECK_HEADERS([fontconfig/fontconfig.h], [
116+
AC_CHECK_LIB([fontconfig], [FcInitLoadConfigAndFonts], [
117+
want_fontconfig=yes
118+
LCUI_LIBS="$LCUI_LIBS `pkg-config --libs fontconfig`"
119+
CFLAGS="$CFLAGS `pkg-config --cflags-only-I fontconfig`"
120+
AC_DEFINE_UNQUOTED([USE_FONTCONFIG], 1, [Define to 1 if you have Fontconfig.])
121+
], [want_fontconfig=no])
122+
], [want_fontconfig=no])
123+
else
124+
want_fontconfig=no
125+
fi
126+
110127
# 检测图形输出设备
111128
want_video_output=yes
112129
want_video_driver_x11=no
@@ -204,6 +221,7 @@ echo -e "Build with lcui-builder support .... : $enable_builder"
204221
echo -e "Build with libpng support .......... : $want_png"
205222
echo -e "Build with libjpeg support ......... : $want_jpeg"
206223
echo -e "Build with font-engine support ..... : $font_engine_name"
224+
echo -e "Build with fontconfig support ...... : $want_fontconfig"
207225
echo -e "Build with thread support .......... : $thread_name"
208226
echo -e "Build with video support ........... : $video_driver_name"
209227
echo

include/LCUI/config.h.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
/* Define to 1 if you have the <dlfcn.h> header file. */
44
#undef HAVE_DLFCN_H
55

6+
/* Define to 1 if you have the <fontconfig/fontconfig.h> header file. */
7+
#undef HAVE_FONTCONFIG_FONTCONFIG_H
8+
69
/* Define to 1 if you have the <ft2build.h> header file. */
710
#undef HAVE_FT2BUILD_H
811

@@ -144,6 +147,9 @@
144147
/* Define to 1 if you have the ANSI C header files. */
145148
#undef STDC_HEADERS
146149

150+
/* Define to 1 if you have Fontconfig. */
151+
#undef USE_FONTCONFIG
152+
147153
/* Define to 1 if you enabled LCUIBuilder function module. */
148154
#undef USE_LCUI_BUILDER
149155

include/LCUI/font.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@
3535
#include <LCUI/font/textstyle.h>
3636
#include <LCUI/font/textlayer.h>
3737
#include <LCUI/font/charset.h>
38+
#include <LCUI/font/fontconfig.h>
3839

3940
#endif

include/LCUI/font/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
AUTOMAKE_OPTIONS=foreign
22

33
# Headers to install
4-
pkginclude_HEADERS = fontlibrary.h textlayer.h textstyle.h charset.h
4+
pkginclude_HEADERS = fontlibrary.h fontconfig.h textlayer.h textstyle.h charset.h
55
pkgincludedir=$(prefix)/include/LCUI/font

include/LCUI/font/fontconfig.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* ***************************************************************************
2+
* fontconfig.c -- The Fontconfig support module.
3+
*
4+
* Copyright (c) 2018, Liu Chao <lc-soft@live.cn> All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are met:
8+
*
9+
* * Redistributions of source code must retain the above copyright notice,
10+
* this list of conditions and the following disclaimer.
11+
* * Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
* * Neither the name of LCUI nor the names of its contributors may be used
15+
* to endorse or promote products derived from this software without
16+
* specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
#ifndef LCUI_FONTCONFIG_H
32+
#define LCUI_FONTCONFIG_H
33+
34+
LCUI_BEGIN_HEADER
35+
36+
char* Fontconfig_GetPath( char* name );
37+
38+
LCUI_END_HEADER
39+
40+
#endif

lcui.pc.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ Name: LCUI
77
Description: A small C library for building user interfaces with C, XML and CSS.
88
Version: @PACKAGE_VERSION@
99
Libs: -L${libdir} -lLCUI
10-
Libs.private: -lpthread -lfreetype -lpng -ljpeg -lX11 -lxml2
10+
Libs.private: -lpthread -lfreetype -lpng -ljpeg -lX11 -lxml2 -lfontconfig
1111
Cflags: -I${includedir}

src/font/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ SUBDIRS = in-core
22
AUTOMAKE_OPTIONS=foreign
33
AM_CFLAGS = -I$(abs_top_srcdir)/include $(CODE_COVERAGE_CFLAGS)
44
noinst_LTLIBRARIES = libfont.la
5-
libfont_la_SOURCES = fontlibrary.c freetype.c charset.c textstyle.c textlayer.c in_core_font.c
5+
libfont_la_SOURCES = fontlibrary.c freetype.c fontconfig.c charset.c textstyle.c textlayer.c in_core_font.c

src/font/fontconfig.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* ***************************************************************************
2+
* fontconfig.c -- The Fontconfig support module.
3+
*
4+
* Copyright (c) 2018, Liu Chao <lc-soft@live.cn> All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are met:
8+
*
9+
* * Redistributions of source code must retain the above copyright notice,
10+
* this list of conditions and the following disclaimer.
11+
* * Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
* * Neither the name of LCUI nor the names of its contributors may be used
15+
* to endorse or promote products derived from this software without
16+
* specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
#ifndef LCUI_BUILD_IN_WIN32
32+
33+
#include <LCUI/config.h>
34+
#include <stdlib.h>
35+
#include <string.h>
36+
#include <fontconfig/fontconfig.h>
37+
38+
char* Fontconfig_GetPath( char* name )
39+
{
40+
#ifdef USE_FONTCONFIG
41+
FcConfig* config = FcInitLoadConfigAndFonts();
42+
char* path = "";
43+
44+
FcPattern* pat = FcNameParse( (const FcChar8*)name );
45+
FcConfigSubstitute( config, pat, FcMatchPattern );
46+
FcDefaultSubstitute( pat );
47+
48+
FcResult result;
49+
FcPattern* font = FcFontMatch( config, pat, &result );
50+
51+
if( font ) {
52+
FcChar8* file = NULL;
53+
if( FcPatternGetString( font, FC_FILE, 0, &file ) == FcResultMatch ) {
54+
size_t path_len = strlen( (char*)file ) + 1;
55+
path = (char*)malloc( path_len );
56+
memset( path, '\0', path_len );
57+
strncpy( path, (char*)file, path_len );
58+
}
59+
FcPatternDestroy( font );
60+
}
61+
62+
FcPatternDestroy( pat );
63+
FcConfigDestroy( config );
64+
65+
return path;
66+
#else
67+
return NULL;
68+
#endif
69+
}
70+
71+
#endif

src/font/fontlibrary.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -855,21 +855,22 @@ void LCUI_InitFontLibrary(void)
855855
{ FONTDIR "msyh.ttc", "Microsoft YaHei", NULL }
856856
};
857857
#else
858-
#define FONTDIR "/usr/share/fonts/"
859-
#define MAX_FONTFILE_NUM 4
858+
#define MAX_FONTFILE_NUM 3
860859
struct {
861860
const char *path;
862861
const char *family;
863862
const char *style;
864863
} fonts[MAX_FONTFILE_NUM] = {
865-
{ FONTDIR "truetype/ubuntu-font-family/Ubuntu-R.ttf", "Ubuntu",
866-
NULL },
867-
{ FONTDIR "opentype/noto/NotoSansCJK-Regular.ttc",
868-
"Noto Sans CJK SC", NULL },
869-
{ FONTDIR "opentype/noto/NotoSansCJK.ttc", "Noto Sans CJK SC",
870-
NULL },
871-
{ FONTDIR "truetype/wqy/wqy-microhei.ttc",
872-
"WenQuanYi Micro Hei", NULL }
864+
{
865+
Fontconfig_GetPath( "Ubuntu" ),
866+
"Ubuntu", NULL
867+
}, {
868+
Fontconfig_GetPath( "Noto Sans CJK SC" ),
869+
"Noto Sans CJK SC", NULL
870+
}, {
871+
Fontconfig_GetPath( "WenQuanYi Micro Hei" ),
872+
"WenQuanYi Micro Hei", NULL
873+
}
873874
};
874875
#endif
875876

@@ -912,6 +913,11 @@ void LCUI_InitFontLibrary(void)
912913
break;
913914
}
914915
}
916+
#ifndef LCUI_BUILD_IN_WIN32
917+
for( i = 0; i < MAX_FONTFILE_NUM; ++i ) {
918+
free( fonts[i].path );
919+
}
920+
#endif
915921
}
916922

917923
void LCUI_FreeFontLibrary(void)

0 commit comments

Comments
 (0)