1# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2# file Copyright.txt or https://cmake.org/licensing for details.
3
4#[=======================================================================[.rst:
5FindFreetype
6------------
7
8Find the FreeType font renderer includes and library.
9
10Imported Targets
11^^^^^^^^^^^^^^^^
12
13.. versionadded:: 3.10
14
15This module defines the following :prop_tgt:`IMPORTED` target:
16
17``Freetype::Freetype``
18  The Freetype ``freetype`` library, if found
19
20Result Variables
21^^^^^^^^^^^^^^^^
22
23This module will set the following variables in your project:
24
25``FREETYPE_FOUND``
26  true if the Freetype headers and libraries were found
27``FREETYPE_INCLUDE_DIRS``
28  directories containing the Freetype headers. This is the
29  concatenation of the variables:
30
31  ``FREETYPE_INCLUDE_DIR_ft2build``
32    directory holding the main Freetype API configuration header
33  ``FREETYPE_INCLUDE_DIR_freetype2``
34    directory holding Freetype public headers
35``FREETYPE_LIBRARIES``
36  the library to link against
37``FREETYPE_VERSION_STRING``
38  the version of freetype found
39
40.. versionadded:: 3.7
41  Debug and Release variants are found separately.
42
43Hints
44^^^^^
45
46The user may set the environment variable ``FREETYPE_DIR`` to the root
47directory of a Freetype installation.
48#]=======================================================================]
49
50# Created by Eric Wing.
51# Modifications by Alexander Neundorf.
52# This file has been renamed to "FindFreetype.cmake" instead of the correct
53# "FindFreeType.cmake" in order to be compatible with the one from KDE4, Alex.
54
55# Ugh, FreeType seems to use some #include trickery which
56# makes this harder than it should be. It looks like they
57# put ft2build.h in a common/easier-to-find location which
58# then contains a #include to a more specific header in a
59# more specific location (#include <freetype/config/ftheader.h>).
60# Then from there, they need to set a bunch of #define's
61# so you can do something like:
62# #include FT_FREETYPE_H
63# Unfortunately, using CMake's mechanisms like include_directories()
64# wants explicit full paths and this trickery doesn't work too well.
65# I'm going to attempt to cut out the middleman and hope
66# everything still works.
67
68set(FREETYPE_FIND_ARGS
69  HINTS
70    ENV FREETYPE_DIR
71  PATHS
72    ENV GTKMM_BASEPATH
73    [HKEY_CURRENT_USER\\SOFTWARE\\gtkmm\\2.4;Path]
74    [HKEY_LOCAL_MACHINE\\SOFTWARE\\gtkmm\\2.4;Path]
75)
76
77find_path(
78  FREETYPE_INCLUDE_DIR_ft2build
79  ft2build.h
80  ${FREETYPE_FIND_ARGS}
81  PATH_SUFFIXES
82    include/freetype2
83    include
84    freetype2
85)
86
87find_path(
88  FREETYPE_INCLUDE_DIR_freetype2
89  NAMES
90    freetype/config/ftheader.h
91    config/ftheader.h
92  ${FREETYPE_FIND_ARGS}
93  PATH_SUFFIXES
94    include/freetype2
95    include
96    freetype2
97)
98
99if(NOT FREETYPE_LIBRARY)
100  find_library(FREETYPE_LIBRARY_RELEASE
101    NAMES
102      freetype
103      libfreetype
104      freetype219
105    ${FREETYPE_FIND_ARGS}
106    PATH_SUFFIXES
107      lib
108  )
109  find_library(FREETYPE_LIBRARY_DEBUG
110    NAMES
111      freetyped
112      libfreetyped
113      freetype219d
114    ${FREETYPE_FIND_ARGS}
115    PATH_SUFFIXES
116      lib
117  )
118  include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
119  select_library_configurations(FREETYPE)
120else()
121  # on Windows, ensure paths are in canonical format (forward slahes):
122  file(TO_CMAKE_PATH "${FREETYPE_LIBRARY}" FREETYPE_LIBRARY)
123endif()
124
125unset(FREETYPE_FIND_ARGS)
126
127# set the user variables
128if(FREETYPE_INCLUDE_DIR_ft2build AND FREETYPE_INCLUDE_DIR_freetype2)
129  set(FREETYPE_INCLUDE_DIRS "${FREETYPE_INCLUDE_DIR_ft2build};${FREETYPE_INCLUDE_DIR_freetype2}")
130  list(REMOVE_DUPLICATES FREETYPE_INCLUDE_DIRS)
131endif()
132set(FREETYPE_LIBRARIES "${FREETYPE_LIBRARY}")
133
134if(EXISTS "${FREETYPE_INCLUDE_DIR_freetype2}/freetype/freetype.h")
135  set(FREETYPE_H "${FREETYPE_INCLUDE_DIR_freetype2}/freetype/freetype.h")
136elseif(EXISTS "${FREETYPE_INCLUDE_DIR_freetype2}/freetype.h")
137  set(FREETYPE_H "${FREETYPE_INCLUDE_DIR_freetype2}/freetype.h")
138endif()
139
140if(FREETYPE_INCLUDE_DIR_freetype2 AND FREETYPE_H)
141  file(STRINGS "${FREETYPE_H}" freetype_version_str
142       REGEX "^#[\t ]*define[\t ]+FREETYPE_(MAJOR|MINOR|PATCH)[\t ]+[0-9]+$")
143
144  unset(FREETYPE_VERSION_STRING)
145  foreach(VPART MAJOR MINOR PATCH)
146    foreach(VLINE ${freetype_version_str})
147      if(VLINE MATCHES "^#[\t ]*define[\t ]+FREETYPE_${VPART}[\t ]+([0-9]+)$")
148        set(FREETYPE_VERSION_PART "${CMAKE_MATCH_1}")
149        if(FREETYPE_VERSION_STRING)
150          string(APPEND FREETYPE_VERSION_STRING ".${FREETYPE_VERSION_PART}")
151        else()
152          set(FREETYPE_VERSION_STRING "${FREETYPE_VERSION_PART}")
153        endif()
154        unset(FREETYPE_VERSION_PART)
155      endif()
156    endforeach()
157  endforeach()
158endif()
159
160include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
161
162find_package_handle_standard_args(
163  Freetype
164  REQUIRED_VARS
165    FREETYPE_LIBRARY
166    FREETYPE_INCLUDE_DIRS
167  VERSION_VAR
168    FREETYPE_VERSION_STRING
169)
170
171mark_as_advanced(
172  FREETYPE_INCLUDE_DIR_freetype2
173  FREETYPE_INCLUDE_DIR_ft2build
174)
175
176if(Freetype_FOUND)
177  if(NOT TARGET Freetype::Freetype)
178    add_library(Freetype::Freetype UNKNOWN IMPORTED)
179    set_target_properties(Freetype::Freetype PROPERTIES
180      INTERFACE_INCLUDE_DIRECTORIES "${FREETYPE_INCLUDE_DIRS}")
181
182    if(FREETYPE_LIBRARY_RELEASE)
183      set_property(TARGET Freetype::Freetype APPEND PROPERTY
184        IMPORTED_CONFIGURATIONS RELEASE)
185      set_target_properties(Freetype::Freetype PROPERTIES
186        IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "C"
187        IMPORTED_LOCATION_RELEASE "${FREETYPE_LIBRARY_RELEASE}")
188    endif()
189
190    if(FREETYPE_LIBRARY_DEBUG)
191      set_property(TARGET Freetype::Freetype APPEND PROPERTY
192        IMPORTED_CONFIGURATIONS DEBUG)
193      set_target_properties(Freetype::Freetype PROPERTIES
194        IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
195        IMPORTED_LOCATION_DEBUG "${FREETYPE_LIBRARY_DEBUG}")
196    endif()
197
198    if(NOT FREETYPE_LIBRARY_RELEASE AND NOT FREETYPE_LIBRARY_DEBUG)
199      set_target_properties(Freetype::Freetype PROPERTIES
200        IMPORTED_LINK_INTERFACE_LANGUAGES "C"
201        IMPORTED_LOCATION "${FREETYPE_LIBRARY}")
202    endif()
203  endif()
204endif()
205