if (ENABLE_MSDF)
    include(FetchContent)

    if(NOT DEFINED MSDFGEN_REPO)
        set(MSDFGEN_REPO https://github.com/Chlumsky/msdfgen.git)
    endif ()

    if(NOT DEFINED MSDFGEN_ATRLAS_REPO)
        set(MSDFGEN_ATRLAS_REPO https://github.com/Chlumsky/msdf-atlas-gen.git)
    endif()

    if(NOT DEFINED FREETYPE_REPO)
        set(FREETYPE_REPO https://github.com/freetype/freetype.git)
    endif()
    if (NOT DEFINED FREETYPE_BUILT_FROM_SOURCES)
        unset(freetype_FOUND)
    endif()
    if (NOT IOS)
        find_package(freetype QUIET)
    endif ()
    if (NOT freetype_FOUND AND NOT FREETYPE_BUILT_FROM_SOURCES)
        message("Installing freetype from sources")
        FetchContent_Declare(
            freetype
            EXCLUDE_FROM_ALL
            GIT_REPOSITORY ${FREETYPE_REPO}
            GIT_TAG master
            GIT_SHALLOW TRUE
        )
        set(FT_DISABLE_ZLIB ON CACHE BOOL "" FORCE)
        set(FT_DISABLE_BZIP2 ON CACHE BOOL "" FORCE)
        set(FT_DISABLE_PNG ON CACHE BOOL "" FORCE)
        set(FT_DISABLE_HARFBUZZ ON CACHE BOOL "" FORCE)
        set(FT_DISABLE_BROTLI ON CACHE BOOL "" FORCE)
        FetchContent_MakeAvailable(freetype)

        set(FT_SRC_DIR "${CMAKE_BINARY_DIR}/_deps/freetype-src")
        set(FT_BUILD_DIR "${FT_SRC_DIR}/build")

        set(FREETYPE_CONFIG_FILE "${FT_SRC_DIR}/include/freetype/config/ftoption.h")
        file(READ "${FREETYPE_CONFIG_FILE}" FILE_CONTENTS)
        string(REPLACE "/* #define FT_CONFIG_OPTION_SUBPIXEL_RENDERING */" "#define FT_CONFIG_OPTION_SUBPIXEL_RENDERING" FILE_CONTENTS "${FILE_CONTENTS}")
        file(WRITE "${FREETYPE_CONFIG_FILE}" "${FILE_CONTENTS}")

        file(MAKE_DIRECTORY ${FT_BUILD_DIR})
        if (IOS)
            set(PLATFORM_CFG -DCMAKE_TOOLCHAIN_FILE=${CMAKE_CURRENT_SOURCE_DIR}/patched_freetype_iOS_toolchain.cmake)
        elseif (APPLE)
            set(PLATFORM_CFG -DCMAKE_OSX_ARCHITECTURES=arm64)
        endif()
        execute_process(
                COMMAND ${CMAKE_COMMAND} -G ${CMAKE_GENERATOR} ${PLATFORM_CFG} -DCMAKE_BUILD_TYPE:STRING=Release -DFT_DISABLE_PNG=ON -DFT_DISABLE_HARFBUZZ=ON -DFT_DISABLE_BROTLI=ON -S ${FT_SRC_DIR} -B ${FT_BUILD_DIR}
                -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=${FT_SRC_DIR}/freetype-install ${PLATFORM_ARG}
                WORKING_DIRECTORY ${FT_BUILD_DIR}
        )
        execute_process(
                COMMAND ${CMAKE_COMMAND} --build ${FT_BUILD_DIR} --config Release --target install
                WORKING_DIRECTORY ${FT_BUILD_DIR} RESULT_VARIABLE build_result
        )
        if (NOT ${build_result} EQUAL "0")
            message(FATAL_ERROR "Failed to build freetype!")
        endif()
        set(FREETYPE_INCLUDE_DIR "${FT_SRC_DIR}/freetype-install/include/freetype2" CACHE INTERNAL "ft include dir" FORCE)
        set(FREETYPE_BUILT_FROM_SOURCES ON CACHE BOOL "ft built from sources" FORCE)
        set(freetype_FOUND ON CACHE BOOL "" FORCE)
        list(APPEND CMAKE_PREFIX_PATH "${FT_SRC_DIR}/freetype-install")
    endif()

    set(MSDFGEN_DISABLE_SVG TRUE CACHE INTERNAL "disable msdfgen svg")
    set(MSDFGEN_USE_SKIA OFF CACHE BOOL "use skia" FORCE)
    set(MSDF_ATLAS_USE_SKIA OFF CACHE BOOL "use skia" FORCE)
    set(MSDF_ATLAS_MSDFGEN_EXTERNAL ON CACHE BOOL "do not build msdfgen submodule" FORCE)
    set(MSDFGEN_DYNAMIC_RUNTIME ON CACHE BOOL "msvc dynamic runtime" FORCE)
    set(MSDF_ATLAS_DYNAMIC_RUNTIME ON CACHE BOOL "msvc dynamic runtime" FORCE)
    set(MSDFGEN_DISABLE_PNG ON CACHE BOOL "disable png" FORCE)
    set(MSDFGEN_USE_VCPKG OFF CACHE BOOL "do not use vcpkg" FORCE)
    set(MSDF_ATLAS_USE_VCPKG OFF CACHE BOOL "do not use vcpkg" FORCE)

    FetchContent_Declare(
            msdfgen
            EXCLUDE_FROM_ALL
            GIT_REPOSITORY ${MSDFGEN_REPO}
            GIT_TAG v1.12
            GIT_SHALLOW TRUE
    )
    FetchContent_MakeAvailable(msdfgen)

    FetchContent_Declare(
            msdfgen_atlas
            EXCLUDE_FROM_ALL
            GIT_REPOSITORY ${MSDFGEN_ATRLAS_REPO}
            GIT_TAG master
            GIT_SHALLOW TRUE
    )
    FetchContent_MakeAvailable(msdfgen_atlas)
endif()

function(LinkMsdf TARGET)
    if (ENABLE_MSDF)
        target_link_libraries(${TARGET} PUBLIC msdfgen::msdfgen msdfgen::msdfgen-ext msdf-atlas-gen)
        if (FREETYPE_BUILT_FROM_SOURCES)
            target_include_directories(${TARGET} PUBLIC ${FREETYPE_INCLUDE_DIR})
        else()
            find_package(Freetype REQUIRED)
            target_include_directories(${TARGET} PUBLIC ${FREETYPE_INCLUDE_DIRS})
        endif()
    endif()
endfunction()