diff --git a/configure.ac b/configure.ac index 1650439d3..399903467 100755 --- a/configure.ac +++ b/configure.ac @@ -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 @@ -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 diff --git a/include/LCUI/config.h.in b/include/LCUI/config.h.in index 1c14d313e..1b2adbe1f 100644 --- a/include/LCUI/config.h.in +++ b/include/LCUI/config.h.in @@ -3,6 +3,9 @@ /* Define to 1 if you have the header file. */ #undef HAVE_DLFCN_H +/* Define to 1 if you have the header file. */ +#undef HAVE_FONTCONFIG_FONTCONFIG_H + /* Define to 1 if you have the header file. */ #undef HAVE_FT2BUILD_H @@ -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 diff --git a/include/LCUI/font.h b/include/LCUI/font.h index b139e5995..46fbd1a66 100644 --- a/include/LCUI/font.h +++ b/include/LCUI/font.h @@ -35,5 +35,6 @@ #include #include #include +#include #endif diff --git a/include/LCUI/font/Makefile.am b/include/LCUI/font/Makefile.am index 8d38c5458..291fdafc1 100755 --- a/include/LCUI/font/Makefile.am +++ b/include/LCUI/font/Makefile.am @@ -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 diff --git a/include/LCUI/font/fontconfig.h b/include/LCUI/font/fontconfig.h new file mode 100644 index 000000000..bca33fb29 --- /dev/null +++ b/include/LCUI/font/fontconfig.h @@ -0,0 +1,40 @@ +/* *************************************************************************** + * fontconfig.c -- The Fontconfig support module. + * + * Copyright (c) 2018, Liu Chao 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 diff --git a/lcui.pc.in b/lcui.pc.in index 386ed327f..6b2c5284c 100644 --- a/lcui.pc.in +++ b/lcui.pc.in @@ -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} diff --git a/src/font/Makefile.am b/src/font/Makefile.am index f49ee18ed..bd5c821c7 100755 --- a/src/font/Makefile.am +++ b/src/font/Makefile.am @@ -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 diff --git a/src/font/fontconfig.c b/src/font/fontconfig.c new file mode 100644 index 000000000..f01f77277 --- /dev/null +++ b/src/font/fontconfig.c @@ -0,0 +1,71 @@ +/* *************************************************************************** + * fontconfig.c -- The Fontconfig support module. + * + * Copyright (c) 2018, Liu Chao 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 +#include +#include +#include + +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 diff --git a/src/font/fontlibrary.c b/src/font/fontlibrary.c index c5ca9c68a..a9165f9e6 100644 --- a/src/font/fontlibrary.c +++ b/src/font/fontlibrary.c @@ -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 @@ -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)