Skip to content

Commit

Permalink
feat(linux): use fontconfig to locate fonts
Browse files Browse the repository at this point in the history
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
  • Loading branch information
coderobe authored and lc-soft committed May 20, 2018
1 parent 42b6141 commit 2932246
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 13 deletions.
18 changes: 18 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,23 @@ else
want_font_engine=no
fi

# fontconfig
want_fontconfig=yes
AC_ARG_ENABLE(fontconfig, AC_HELP_STRING([--enable-fontconfig],
[turn on fontconfig [[default=yes]]]), [want_fontconfig=$enableval])
if test "$want_fontconfig" = yes; then
AC_CHECK_HEADERS([fontconfig/fontconfig.h], [
AC_CHECK_LIB([fontconfig], [FcInitLoadConfigAndFonts], [
want_fontconfig=yes
LCUI_LIBS="$LCUI_LIBS `pkg-config --libs fontconfig`"
CFLAGS="$CFLAGS `pkg-config --cflags-only-I fontconfig`"
AC_DEFINE_UNQUOTED([USE_FONTCONFIG], 1, [Define to 1 if you have Fontconfig.])
], [want_fontconfig=no])
], [want_fontconfig=no])
else
want_fontconfig=no
fi

# 检测图形输出设备
want_video_output=yes
want_video_driver_x11=no
Expand Down Expand Up @@ -204,6 +221,7 @@ echo -e "Build with lcui-builder support .... : $enable_builder"
echo -e "Build with libpng support .......... : $want_png"
echo -e "Build with libjpeg support ......... : $want_jpeg"
echo -e "Build with font-engine support ..... : $font_engine_name"
echo -e "Build with fontconfig support ...... : $want_fontconfig"
echo -e "Build with thread support .......... : $thread_name"
echo -e "Build with video support ........... : $video_driver_name"
echo
Expand Down
6 changes: 6 additions & 0 deletions include/LCUI/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
/* Define to 1 if you have the <dlfcn.h> header file. */
#undef HAVE_DLFCN_H

/* Define to 1 if you have the <fontconfig/fontconfig.h> header file. */
#undef HAVE_FONTCONFIG_FONTCONFIG_H

/* Define to 1 if you have the <ft2build.h> header file. */
#undef HAVE_FT2BUILD_H

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

/* Define to 1 if you have Fontconfig. */
#undef USE_FONTCONFIG

/* Define to 1 if you enabled LCUIBuilder function module. */
#undef USE_LCUI_BUILDER

Expand Down
1 change: 1 addition & 0 deletions include/LCUI/font.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@
#include <LCUI/font/textstyle.h>
#include <LCUI/font/textlayer.h>
#include <LCUI/font/charset.h>
#include <LCUI/font/fontconfig.h>

#endif
2 changes: 1 addition & 1 deletion include/LCUI/font/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
AUTOMAKE_OPTIONS=foreign

# Headers to install
pkginclude_HEADERS = fontlibrary.h textlayer.h textstyle.h charset.h
pkginclude_HEADERS = fontlibrary.h fontconfig.h textlayer.h textstyle.h charset.h
pkgincludedir=$(prefix)/include/LCUI/font
40 changes: 40 additions & 0 deletions include/LCUI/font/fontconfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* ***************************************************************************
* fontconfig.c -- The Fontconfig support module.
*
* Copyright (c) 2018, Liu Chao <lc-soft@live.cn> All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of LCUI nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef LCUI_FONTCONFIG_H
#define LCUI_FONTCONFIG_H

LCUI_BEGIN_HEADER

char* Fontconfig_GetPath( char* name );

LCUI_END_HEADER

#endif
2 changes: 1 addition & 1 deletion lcui.pc.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ Name: LCUI
Description: A small C library for building user interfaces with C, XML and CSS.
Version: @PACKAGE_VERSION@
Libs: -L${libdir} -lLCUI
Libs.private: -lpthread -lfreetype -lpng -ljpeg -lX11 -lxml2
Libs.private: -lpthread -lfreetype -lpng -ljpeg -lX11 -lxml2 -lfontconfig
Cflags: -I${includedir}
2 changes: 1 addition & 1 deletion src/font/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ SUBDIRS = in-core
AUTOMAKE_OPTIONS=foreign
AM_CFLAGS = -I$(abs_top_srcdir)/include $(CODE_COVERAGE_CFLAGS)
noinst_LTLIBRARIES = libfont.la
libfont_la_SOURCES = fontlibrary.c freetype.c charset.c textstyle.c textlayer.c in_core_font.c
libfont_la_SOURCES = fontlibrary.c freetype.c fontconfig.c charset.c textstyle.c textlayer.c in_core_font.c
71 changes: 71 additions & 0 deletions src/font/fontconfig.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* ***************************************************************************
* fontconfig.c -- The Fontconfig support module.
*
* Copyright (c) 2018, Liu Chao <lc-soft@live.cn> All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of LCUI nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef LCUI_BUILD_IN_WIN32

#include <LCUI/config.h>
#include <stdlib.h>
#include <string.h>
#include <fontconfig/fontconfig.h>

char* Fontconfig_GetPath( char* name )
{
#ifdef USE_FONTCONFIG
FcConfig* config = FcInitLoadConfigAndFonts();
char* path = "";

FcPattern* pat = FcNameParse( (const FcChar8*)name );
FcConfigSubstitute( config, pat, FcMatchPattern );
FcDefaultSubstitute( pat );

FcResult result;
FcPattern* font = FcFontMatch( config, pat, &result );

if( font ) {
FcChar8* file = NULL;
if( FcPatternGetString( font, FC_FILE, 0, &file ) == FcResultMatch ) {
size_t path_len = strlen( (char*)file ) + 1;
path = (char*)malloc( path_len );
memset( path, '\0', path_len );
strncpy( path, (char*)file, path_len );
}
FcPatternDestroy( font );
}

FcPatternDestroy( pat );
FcConfigDestroy( config );

return path;
#else
return NULL;
#endif
}

#endif
26 changes: 16 additions & 10 deletions src/font/fontlibrary.c
Original file line number Diff line number Diff line change
Expand Up @@ -855,21 +855,22 @@ void LCUI_InitFontLibrary(void)
{ FONTDIR "msyh.ttc", "Microsoft YaHei", NULL }
};
#else
#define FONTDIR "/usr/share/fonts/"
#define MAX_FONTFILE_NUM 4
#define MAX_FONTFILE_NUM 3
struct {
const char *path;
const char *family;
const char *style;
} fonts[MAX_FONTFILE_NUM] = {
{ FONTDIR "truetype/ubuntu-font-family/Ubuntu-R.ttf", "Ubuntu",
NULL },
{ FONTDIR "opentype/noto/NotoSansCJK-Regular.ttc",
"Noto Sans CJK SC", NULL },
{ FONTDIR "opentype/noto/NotoSansCJK.ttc", "Noto Sans CJK SC",
NULL },
{ FONTDIR "truetype/wqy/wqy-microhei.ttc",
"WenQuanYi Micro Hei", NULL }
{
Fontconfig_GetPath( "Ubuntu" ),
"Ubuntu", NULL
}, {
Fontconfig_GetPath( "Noto Sans CJK SC" ),
"Noto Sans CJK SC", NULL
}, {
Fontconfig_GetPath( "WenQuanYi Micro Hei" ),
"WenQuanYi Micro Hei", NULL
}
};
#endif

Expand Down Expand Up @@ -912,6 +913,11 @@ void LCUI_InitFontLibrary(void)
break;
}
}
#ifndef LCUI_BUILD_IN_WIN32
for( i = 0; i < MAX_FONTFILE_NUM; ++i ) {
free( fonts[i].path );
}
#endif
}

void LCUI_FreeFontLibrary(void)
Expand Down

0 comments on commit 2932246

Please sign in to comment.