# Fontmatrix ./CMakeLists.txt v1

CMAKE_MINIMUM_REQUIRED(VERSION 3.16)
PROJECT(fontmatrix)

if(POLICY CMP0177)
  cmake_policy(SET CMP0177 NEW)
endif()

SET(VERSION_MAJOR "0" CACHE STRING "")
SET(VERSION_MINOR "11" CACHE STRING "")
SET(VERSION_PATCH "0" CACHE STRING "")
SET (VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH})
ADD_DEFINITIONS(-DFONTMATRIX_VERSION_MAJOR=${VERSION_MAJOR} -DFONTMATRIX_VERSION_MINOR=${VERSION_MINOR} -DFONTMATRIX_VERSION_PATCH=${VERSION_PATCH})
MESSAGE(STATUS "Building Fontmatrix ${VERSION}")


IF(UNIX AND NOT APPLE)
	SET (CMAKE_INSTALL_PREFIX /usr/local CACHE PATH "Installation path")
	SET (FONTMATRIX_BIN bin )
	SET (FONTMATRIX_QMDIR "share/fontmatrix/qm" )
ENDIF(UNIX AND NOT APPLE)

IF(APPLE)
# 	SET (CMAKE_INSTALL_PREFIX /Applications CACHE PATH "Installation path")
# 	SET (FONTMATRIX_BIN bin "")
	set( CMAKE_OSX_ARCHITECTURES i386 )
ENDIF(APPLE)

IF(WIN32)
	SET (CMAKE_INSTALL_PREFIX c:/fontmatrix CACHE PATH "Installation path")
	SET (FONTMATRIX_BIN ".")
	SET (FONTMATRIX_QMDIR "share/qm" )
ENDIF(WIN32)

ADD_DEFINITIONS(-DPREFIX="${CMAKE_INSTALL_PREFIX}")
MESSAGE(STATUS "Fontmatrix will be installed under ${CMAKE_INSTALL_PREFIX}")



SET(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules")

# Very generic definition, mainly to sort out bugs reported by mrdocs
IF(APPLE)
	ADD_DEFINITIONS( -DPLATFORM_APPLE )
ENDIF(APPLE)

#<< harfbuzz (shaper)
IF(WANT_HARFBUZZ)
# Since Harfbuzz source code is included into
# FM source tree, it’s just a matter of activation
# and « cohérence »
	ADD_DEFINITIONS( -DHAVE_HARFBUZZ )
	SET(HARFBUZZSHAPER 1)
	MESSAGE(STATUS "Build Harbuzz shaper support")
ENDIF(WANT_HARFBUZZ)
#>> harfbuzz (shaper)

#<< ICU
IF(WANT_ICU)
# 	SET(ICU_DIR ${CMAKE_MODULE_PATH})
	FIND_PACKAGE(ICU)
	if(ICU_FOUND)
		MESSAGE(STATUS "ICU Found OK")
		ADD_DEFINITIONS( -DHAVE_ICU )
		SET(ICUSHAPER 1)
		MESSAGE(STATUS "Build ICU shaper support")
	ELSE(ICU_FOUND)
		MESSAGE(FATAL_ERROR "ICU Not Found ARGH")
	ENDIF(ICU_FOUND)
ENDIF(WANT_ICU)

#>> ICU

#<< m17n
IF(WANT_M17N)
# 	SET(M17N_DIR ${CMAKE_MODULE_PATH})
	FIND_PACKAGE(M17N)
	if(M17N_FOUND)
		MESSAGE(STATUS "M17N Found OK")
		ADD_DEFINITIONS( -DHAVE_M17N )
		SET(M17NSHAPER 1)
		MESSAGE(STATUS "Build m17n shaper support")
	ELSE(M17N_FOUND)
		MESSAGE(FATAL_ERROR "M17N Not Found ARGH")
	ENDIF(M17N_FOUND)
ENDIF(WANT_M17N)
#>> m17n

IF(WANT_PODOFO)
# 	SET(LIBPODOFO_DIR ${CMAKE_MODULE_PATH})
	FIND_PACKAGE(LIBPODOFO)
	IF(LIBPODOFO_FOUND)
		MESSAGE(STATUS "PoDoFo Library Found OK")
		ADD_DEFINITIONS(-DHAVE_PODOFO)
		SET(PODOFO 1)
	ENDIF(LIBPODOFO_FOUND)
ENDIF(WANT_PODOFO)


# I can’t remember what it does :|
ADD_DEFINITIONS( -DHIGH_PERF )


IF(CMAKE_BUILD_TYPE STREQUAL "Debug")
	MESSAGE(STATUS "Debug build")
	ADD_DEFINITIONS(-DBUILD_TYPE_DEBUG)
ELSE(CMAKE_BUILD_TYPE STREQUAL "Debug")
	ADD_DEFINITIONS(-DQT_NO_DEBUG_OUTPUT)
ENDIF(CMAKE_BUILD_TYPE STREQUAL "Debug")

# A lot from Scribus
#<< Qt
find_package(Qt6 REQUIRED COMPONENTS Core Widgets Svg SvgWidgets Sql Xml PrintSupport WebEngineWidgets LinguistTools)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
#>> Qt

SET(HARFBUZZ_DIR harfbuzz/src)
ADD_SUBDIRECTORY(${HARFBUZZ_DIR})
MESSAGE(STATUS "Adding ${CMAKE_SOURCE_DIR}/${HARFBUZZ_DIR}")
SET(HARFBUZZ_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/${HARFBUZZ_DIR})

# SET(PUZZLE_DIR libpuzzle/src)
# ADD_SUBDIRECTORY(${PUZZLE_DIR})
# MESSAGE(STATUS "Adding "${CMAKE_SOURCE_DIR}/${PUZZLE_DIR})
# SET(PUZZLE_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/${PUZZLE_DIR})

#<< FreeType2
# We give a try to the built-in cmake Freetype.cmake
FIND_PACKAGE(Freetype REQUIRED)
IF (FREETYPE_FOUND)
  MESSAGE(STATUS "FreeType2 Library Found OK")
ELSE (FREETYPE_FOUND)
  MESSAGE(FATAL_ERROR "No Freetype found")  
ENDIF(FREETYPE_FOUND)
#>> FreeType2

IF(WANT_PYTHONQT)
	FIND_PACKAGE(PythonLibs REQUIRED)
	if(PYTHONLIBS_FOUND)
		MESSAGE(STATUS "Python Library Found OK")
	else(PYTHONLIBS_FOUND)
		MESSAGE(FATAL_ERROR "No Python Library found")
	endif(PYTHONLIBS_FOUND)
	SET(PYTHONQT_INCLUDE_DIRS  ${CMAKE_SOURCE_DIR}/pythonqt/src)
	ADD_SUBDIRECTORY(${PYTHONQT_INCLUDE_DIRS})
	ADD_DEFINITIONS(-DHAVE_PYTHONQT)
ENDIF(WANT_PYTHONQT)

IF(UNIX AND NOT APPLE)
	FIND_PACKAGE(FONTCONFIG)
	IF(FONTCONFIG_FOUND)
		MESSAGE(STATUS "FontConfig Found OK")
		ADD_DEFINITIONS( -DHAVE_FONTCONFIG )
	ENDIF(FONTCONFIG_FOUND)
ENDIF(UNIX AND NOT APPLE)


SET(FM_TEMPLATES_FILES
${CMAKE_SOURCE_DIR}/src/graphic-resources/template_oneline.xml
${CMAKE_SOURCE_DIR}/src/graphic-resources/template_oneline.png
${CMAKE_SOURCE_DIR}/src/graphic-resources/template_default.xml
${CMAKE_SOURCE_DIR}/src/graphic-resources/template_default.png
)
SET(FM_STYLE_FILES
${CMAKE_SOURCE_DIR}/src/graphic-resources/html-css/info.css
${CMAKE_SOURCE_DIR}/src/graphic-resources/html-css/fontmatrix.js
${CMAKE_SOURCE_DIR}/src/graphic-resources/html-css/img/css-info-propertyname.png
${CMAKE_SOURCE_DIR}/src/graphic-resources/html-css/img/css-info-property.png
${CMAKE_SOURCE_DIR}/src/graphic-resources/html-css/img/css-shadow.png
${CMAKE_SOURCE_DIR}/src/graphic-resources/html-css/img/css-tech-info.png
)
SET(FM_WELCOME_FILES
${CMAKE_SOURCE_DIR}/src/messages/welcome_cs.html
${CMAKE_SOURCE_DIR}/src/messages/welcome_da.html
${CMAKE_SOURCE_DIR}/src/messages/welcome_de.html
${CMAKE_SOURCE_DIR}/src/messages/welcome_en.html
${CMAKE_SOURCE_DIR}/src/messages/welcome_fi.html
${CMAKE_SOURCE_DIR}/src/messages/welcome_fr.html
${CMAKE_SOURCE_DIR}/src/messages/welcome_hi.html
${CMAKE_SOURCE_DIR}/src/messages/welcome_nb.html
${CMAKE_SOURCE_DIR}/src/messages/welcome_nl.html
${CMAKE_SOURCE_DIR}/src/messages/welcome_no.html
${CMAKE_SOURCE_DIR}/src/messages/welcome_ru.html
${CMAKE_SOURCE_DIR}/src/messages/welcome_sr.html
${CMAKE_SOURCE_DIR}/src/messages/welcome_sv.html
${CMAKE_SOURCE_DIR}/src/messages/welcome_uk.html
${CMAKE_SOURCE_DIR}/src/messages/welcome_zh_CN.html
${CMAKE_SOURCE_DIR}/src/messages/welcome_zh_TW.html
)

# tell cmake to process CMakeLists.txt in that subdirectory
SET(HYPHENATE_DIR src/hyphenate)
ADD_SUBDIRECTORY(${HYPHENATE_DIR})
SET(HYPHENATE_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/${HYPHENATE_DIR})



ADD_SUBDIRECTORY(src)

# dodict
# ADD_SUBDIRECTORY(src/langs)


######## Installation ##################

IF(UNIX AND NOT APPLE)
	SET(DESKTOPDIR "share/applications/")
	SET(MANDIR "share/man/")
	SET(ICONDIR "share/icons/hicolor/48x48/apps/")
	SET(RESOURCESDIR "share/fontmatrix/resources/")
	SET(HELPDIR "share/fontmatrix/help/")
	
	INSTALL(FILES
	fontmatrix.desktop
	DESTINATION ${DESKTOPDIR}
	)
	
	INSTALL(FILES
	fontmatrix.png
	DESTINATION ${ICONDIR}
	)
	
	INSTALL(FILES
	fontmatrix.1
	DESTINATION "${MANDIR}man1"
	)
	
	INSTALL(FILES
	${FM_TEMPLATES_FILES}
	${FM_STYLE_FILES}
	${FM_WELCOME_FILES}
	DESTINATION "${RESOURCESDIR}"
	)

	INSTALL(FILES
	${FM_HELP_FILES}
	DESTINATION "${HELPDIR}"
	)
ENDIF(UNIX AND NOT APPLE)

IF(APPLE)
# we do not "install" on apple’s OS
ENDIF(APPLE)

IF(WIN32)
	SET(RESOURCESDIR "share/resources/")
	SET(HELPDIR "help/")
	INSTALL(FILES
	${FM_TEMPLATES_FILES}
	${FM_STYLE_FILES}
	${FM_WELCOME_FILES}
	${FM_HELP_FILES}
	DESTINATION "${RESOURCESDIR}"
	)

	INSTALL(FILES
	${UNICODE_RESOURCES}
	DESTINATION "${RESOURCESDIR}/Unicode/"
	)

	# Bundle Qt runtime DLLs alongside the installed executable.
	# windeployqt6 path is resolved from Qt6::Core's location at configure time.
	get_target_property(_qt_core_loc Qt6::Core IMPORTED_LOCATION)
	get_filename_component(_qt_bin_dir "${_qt_core_loc}" DIRECTORY)
	install(CODE "
		if(EXISTS \"${_qt_bin_dir}/windeployqt6.exe\")
			execute_process(COMMAND
				\"${_qt_bin_dir}/windeployqt6.exe\" --release
				\"\${CMAKE_INSTALL_PREFIX}/fontmatrix.exe\"
			)
		else()
			message(WARNING \"windeployqt6.exe not found at ${_qt_bin_dir} — Qt DLLs will not be bundled.\")
		endif()
	")

	# Install MSVC runtime DLLs (vcruntime140.dll, msvcp140.dll, etc.) alongside the exe.
	# InstallRequiredSystemLibraries locates them via CMake's knowledge of the MSVC toolchain,
	# which is reliable at configure time — unlike windeployqt6 --compiler-runtime which runs
	# at install time without the full Visual Studio environment.
	set(CMAKE_INSTALL_SYSTEM_RUNTIME_DESTINATION ".")
	include(InstallRequiredSystemLibraries)

	# Copy FreeType DLL from vcpkg (resolved at configure time via VCPKG_INSTALLATION_ROOT).
	# Use file(TO_CMAKE_PATH) to convert Windows backslashes (e.g. C:\vcpkg) to forward
	# slashes so the path is safe to embed in the generated cmake_install.cmake script.
	if(DEFINED ENV{VCPKG_INSTALLATION_ROOT})
		set(_vcpkg_root "$ENV{VCPKG_INSTALLATION_ROOT}")
		file(TO_CMAKE_PATH "${_vcpkg_root}" _vcpkg_root)
		# freetype and its transitive vcpkg dependencies (brotli, bzip2, libpng, zlib).
		file(GLOB _vcpkg_dlls
			"${_vcpkg_root}/installed/x64-windows/bin/freetype*.dll"
			"${_vcpkg_root}/installed/x64-windows/bin/zlib1.dll"
			"${_vcpkg_root}/installed/x64-windows/bin/bz2.dll"
			"${_vcpkg_root}/installed/x64-windows/bin/libpng16.dll"
			"${_vcpkg_root}/installed/x64-windows/bin/brotlidec.dll"
			"${_vcpkg_root}/installed/x64-windows/bin/brotlicommon.dll"
		)
		foreach(_dll ${_vcpkg_dlls})
			install(FILES "${_dll}" DESTINATION ".")
		endforeach()
	endif()

ENDIF(WIN32)

ADD_SUBDIRECTORY(help)
ADD_SUBDIRECTORY(samples)

SET(CPACK_PACKAGE_VERSION_MAJOR ${VERSION_MAJOR})
SET(CPACK_PACKAGE_VERSION_MINOR ${VERSION_MINOR})
SET(CPACK_PACKAGE_VERSION_PATCH ${VERSION_PATCH})
SET(CPACK_PACKAGE_NAME "Fontmatrix")
SET(CPACK_PACKAGE_VENDOR "Fontmatrix")
SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Font management application")

IF(WIN32)
	# MSI installer via CPack WiX generator using WiX Toolset 6 (wix.exe).
	# CPACK_WIX_VERSION=4 selects the WiX 4+ code-path in CMake's CPack generator,
	# which invokes `wix.exe` (WiX 4/5/6) instead of the legacy candle.exe/light.exe
	# (WiX 3). WiX 6 uses UTF-8 throughout and avoids the LGHT0311 code-page error
	# that WiX 3 raises for file names containing non-Latin-1 characters.
	SET(CPACK_WIX_VERSION 4)
	SET(CPACK_GENERATOR "WIX")
	SET(CPACK_PACKAGE_INSTALL_DIRECTORY "Fontmatrix")
	# UPGRADE_GUID must remain constant across all versions of this product.
	SET(CPACK_WIX_UPGRADE_GUID "5D8B1A2F-C3E4-4F56-8901-2B3C4D5E6F78")
	SET(CPACK_PACKAGE_EXECUTABLES "fontmatrix" "Fontmatrix")
	SET(CPACK_WIX_PROGRAM_MENU_FOLDER "Fontmatrix")
	SET(CPACK_WIX_PROPERTY_ARPURLINFOABOUT "https://github.com/eniac111/fontmatrix/")
	# Build a combined license file: copyright header + GPL text + VC++ runtime notice.
	# file(READ) + file(WRITE) runs at configure time; the original COPYING is not touched.
	file(READ "${CMAKE_SOURCE_DIR}/COPYING" _gpl_text)
	file(WRITE "${CMAKE_BINARY_DIR}/LICENSE.txt"
"Fontmatrix ${VERSION} - Font Management Application\n\
Copyright (C) 2007 Pierre Marchand and contributors\n\
https://github.com/eniac111/fontmatrix/\n\
\n\
This program is free software: you can redistribute it and/or modify\n\
it under the terms of the GNU General Public License as published below.\n\
\n\
Third-party components included in this installer:\n\
  Microsoft Visual C++ Runtime Libraries\n\
    Copyright (C) Microsoft Corporation. All rights reserved.\n\
    Redistributed under the Microsoft Visual C++ Runtime Library License.\n\
    See: https://learn.microsoft.com/en-us/legal/vc-redist-license-terms\n\
\n\
------------------------------------------------------------------------\n\
GNU GENERAL PUBLIC LICENSE\n\
------------------------------------------------------------------------\n\
\n\
${_gpl_text}")
	SET(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_BINARY_DIR}/LICENSE.txt")
ENDIF(WIN32)

INCLUDE(CPack)
