Skip to content

Commit

Permalink
add font-chef files
Browse files Browse the repository at this point in the history
  • Loading branch information
mobius3 authored and Leonardo committed May 25, 2020
1 parent a57d7bc commit 5bbfdbb
Show file tree
Hide file tree
Showing 53 changed files with 33,197 additions and 0 deletions.
29 changes: 29 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
cmake_minimum_required(VERSION 3.0)
cmake_policy(SET CMP0063 NEW)
project(font-chef LANGUAGES C CXX VERSION 1.0.0)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake-modules)

set(CMAKE_C_STANDARD 99)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_C_VISIBILITY_PRESET hidden)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN YES)

option(FONT_CHEF_BUILD_DOCUMENTATION "Builds documentation using Doxygen" OFF)
option(FONT_CHEF_BUILD_EXAMPLES "Builds examples. Needs SDL2 already installed." OFF)

if (NOT APPLE)
set(CMAKE_INSTALL_RPATH $ORIGIN)
endif()

add_subdirectory(third-party EXCLUDE_FROM_ALL)
add_subdirectory(src)

if (FONT_CHEF_BUILD_DOCUMENTATION)
add_subdirectory(doc)
endif()

include(cmake/setup-exports.cmake)
include(cmake/font-chef-cpack.cmake)
80 changes: 80 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Font Chef

*Font Chef* is a cross-platform C99 library to create character atlas of pre-rasterized glyphs from a font at a specified size and color. It is mostly useful in situations where you cannot afford to rasterize a piece of text again whenever it changes.

It abstracts [stb_truetype](https://github.com/nothings/stb/blob/master/stb_truetype.h) to render glyphs to a pixmap and to produce appropriate clipping rects to later display those glyphs.

**Features**

- Small, clean and easy-to-use API
- Ships with C++ wrapper classes
- Considers kerning when resolving rendering rects
- Ships with many standard unicode blocks to choose from
- Rendering API agnostic (it does not render anything directly, it returns pixels and clipping rects)
- Fully documented with examples for each function
- No external dependencies

## Integrating with your code

### Using pre-built releases

Download a pre-built release package suitable for your platform and
uncompress it somewhere you'll remember later (usually where you put
other development libraries). Let's assume you uncompressed font-chef to
`/home/me/libs/font-chef`.

**Using CMake**: If you're using CMake, specify the variable
`CMAKE_PREFIX_PATH` to point to it before running CMake in your project:

```shell script
cmake -DCMAKE_PREFIX_PATH=/home/me/libs
```

Afterwards you can use `find_package(font-chef)` and
`target_link_libraries(<your-executable> PUBLIC font-chef)` to link
`<your-executable>` against font-chef.

**Not using CMake**: Assuming you uncompressed font-chef to the same path as
above, you should configure your build system to look for include files
inside `/home/me/libs/font-chef/include` and to look for shared objects to
link against inside `/home/me/libs/font-chef/lib`. In case of Windows, you
should also point your linker to `font-chef/bin` as well.

### Using a source release and CMake

Uncompress font-chef in a folder inside your project (e.g,
`your-project/third-party/font-chef`) and then use
`add_subdirectory(third-party/font-chef EXCLUDE_FROM_ALL)` to add the
library target. Afterwards you can use `find_package(font-chef)` and
`target_link_libraries(<your-executable> PUBLIC font-chef)` to link
`<your-executable>` against font-chef.

Compiling the `.c` files directly in your project is not recommended nor
supported.

## Compile from source

You'll need CMake installed and in your path and also capable of finding
you compiler and linker. Then, after checking out this repository:

```shell script
mkdir build/
cd build/
cmake ..
make
```

## Examples

Examples for C and C++ are in the [src/examples](src/examples) folder. To build them, when running cmake as in [Compile from source](#compile-from-source), add the following variable:

```shell script
cmake .. -DFONT_CHEF_BUILD_EXAMPLES=1
```

You will need SDL2 available and CMake needs to be able to find it.

## Documentation

See [here](https://mobius3.github.io/font-chef)

215 changes: 215 additions & 0 deletions cmake-modules/FindSDL2.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
# This file is mostly based around the old FindSDL.cmake that is included in
# CMake's installation. This will attempt to find SDL2 library and include
# directories installed in your system and variables below accordingly. With
# the latest additions, it will also define targets to be used in
# target_link_libraries.
#
# This module defines
# TARGET SDL2, so you can target_link_libraries(YourLib SDL2)
# TARGET SDL2main. You don't need to use this explicity, it is automatically
# added as a dependency.
# SDL2_LIBRARY, a list of link flags, including SDL
# SDL2_FOUND, if false, do not try to link to SDL2
# SDL2_INCLUDE_DIR, where to find SDL.h
#
# This module responds to the the flag:
# SDL2_BUILDING_LIBRARY
# If this is defined, then no SDL2main will be linked in because
# only applications need main().
# Otherwise, it is assumed you are building an application and this
# module will attempt to locate and set the the proper link flags
# as part of the returned SDL2_LIBRARY variable.
#
# Don't forget to include SDLmain.h and SDLmain.m your project for the
# OS X framework based version. (Other versions link to -lSDL2main which
# this module will try to find on your behalf.) Also for OS X, this
# module will automatically add the -framework Cocoa on your behalf.
#
#
# Additional Note: If you see an empty SDL2_LIBRARY_TEMP in your configuration
# and no SDL2_LIBRARY, it means CMake did not find your SDL2 library
# (SDL2.dll, libsdl2.so, SDL2.framework, etc).
# Set SDL2_LIBRARY_TEMP to point to your SDL2 library, and configure again.
# Similarly, if you see an empty SDL2MAIN_LIBRARY, you should set this value
# as appropriate. These values are used to generate the final SDL2_LIBRARY
# variable, but when these values are unset, SDL2_LIBRARY does not get created.
#
#
# $SDL2DIR is an environment variable that would
# correspond to the ./configure --prefix=$SDL2DIR
# used in building SDL2.
# l.e.galup 9-20-02
#
# Modified by Eric Wing.
# Added code to assist with automated building by using environmental variables
# and providing a more controlled/consistent search behavior.
# Added new modifications to recognize OS X frameworks and
# additional Unix paths (FreeBSD, etc).
# Also corrected the header search path to follow "proper" SDL guidelines.
# Added a search for SDL2main which is needed by some platforms.
# Added a search for threads which is needed by some platforms.
# Added needed compile switches for MinGW.
#
# On OSX, this will prefer the Framework version (if found) over others.
# People will have to manually change the cache values of
# SDL2_LIBRARY to override this selection or set the CMake environment
# CMAKE_INCLUDE_PATH to modify the search paths.
#
# Note that the header path has changed from SDL2/SDL.h to just SDL.h
# This needed to change because "proper" SDL convention
# is #include "SDL.h", not <SDL2/SDL.h>. This is done for portability
# reasons because not all systems place things in SDL2/ (see FreeBSD).

#=============================================================================
# Copyright 2003-2009 Kitware, Inc.
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
# (To distribute this file outside of CMake, substitute the full
# License text for the above reference.)

set(SDL2_SEARCH_PATHS
~/Library/Frameworks
/Library/Frameworks
/usr/local
/usr
/sw # Fink
/opt/local # DarwinPorts
/opt/csw # Blastwave
/opt
${SDL2_ROOT}
)

find_path(SDL2_INCLUDE_DIR SDL.h
HINTS
$ENV{SDL2DIR}
PATH_SUFFIXES include/SDL2 include
PATHS ${SDL2_SEARCH_PATHS}
)

if (CMAKE_SIZEOF_VOID_P EQUAL 8)
set(PATH_SUFFIXES lib64 lib/x64 lib)
else ()
set(PATH_SUFFIXES lib/x86 lib)
endif ()

find_library(SDL2_LIBRARY_LOCATION
NAMES SDL2
HINTS
$ENV{SDL2DIR}
PATH_SUFFIXES ${PATH_SUFFIXES}
PATHS ${SDL2_SEARCH_PATHS}
)

set(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_LOCATION})

if (NOT SDL2_BUILDING_LIBRARY)
if (NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework")
# Non-OS X framework versions expect you to also dynamically link to
# SDL2main. This is mainly for Windows and OS X. Other (Unix) platforms
# seem to provide SDL2main for compatibility even though they don't
# necessarily need it.
find_library(SDL2MAIN_LIBRARY
NAMES SDL2main
HINTS
$ENV{SDL2DIR}
PATH_SUFFIXES ${PATH_SUFFIXES}
PATHS ${SDL2_SEARCH_PATHS}
)
endif()
endif()

# SDL2 may require threads on your system.
# The Apple build may not need an explicit flag because one of the
# frameworks may already provide it.
# But for non-OSX systems, I will use the CMake Threads package.
if (NOT APPLE)
find_package(Threads)
endif ()

# MinGW needs an additional link flag, -mwindows
# It's total link flags should look like -lmingw32 -lSDL2main -lSDL2 -mwindows
if (MINGW)
set(MINGW32_LIBRARY mingw32 "-mwindows" CACHE STRING "mwindows for MinGW")
endif ()

if (SDL2_LIBRARY_TEMP)
# If SDL2 was found, add a library target.
add_library(SDL2 SHARED IMPORTED)

if (WIN32)
find_file(SDL2_DLL
SDL2.dll
HINTS
$ENV{SDL2DIR}
PATH_SUFFIXES ${PATH_SUFFIXES}
PATHS ${SDL2_SEARCH_PATHS}
)
set_target_properties(SDL2 PROPERTIES
IMPORTED_LOCATION ${SDL2_DLL}
IMPORTED_IMPLIB ${SDL2_LIBRARY_LOCATION}
INTERFACE_INCLUDE_DIRECTORIES ${SDL2_INCLUDE_DIR})
else()
set_target_properties(SDL2 PROPERTIES
IMPORTED_LOCATION ${SDL2_LIBRARY_LOCATION}
INTERFACE_INCLUDE_DIRECTORIES ${SDL2_INCLUDE_DIR})
endif(WIN32)

# For SDL2main
if (NOT SDL2_BUILDING_LIBRARY)
if (SDL2MAIN_LIBRARY)
# If the SDL2 main library is needed and found, add a target to it.
# Also, makes the top-level SDL2 target depend on it.
add_library(SDL2main STATIC IMPORTED)
set_property(TARGET SDL2main PROPERTY IMPORTED_LOCATION ${SDL2MAIN_LIBRARY})
target_link_libraries(SDL2 INTERFACE SDL2main)

# Keep increasing SDL2_LIBRARY_TEMP
set(SDL2_LIBRARY_TEMP ${SDL2MAIN_LIBRARY} ${SDL2_LIBRARY_TEMP})
endif ()
endif ()

# For OS X, SDL2 uses Cocoa as a backend so it must link to Cocoa.
# CMake doesn't display the -framework Cocoa string in the UI even
# though it actually is there if I modify a pre-used variable.
# I think it has something to do with the CACHE STRING.
# So I use a temporary variable until the end so I can set the
# "real" variable in one-shot.
if (APPLE)
set(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} "-framework Cocoa")
get_property(INTERFACE_LINK_LIBRARIES TARGET SDL2 PROPERTY INTERFACE_LINK_LIBRARIES)
set_property(TARGET SDL2 PROPERTY
INTERFACE_LINK_LIBRARIES ${INTERFACE_LINK_LIBRARIES} "-framework Cocoa")
endif ()

# For threads, as mentioned Apple doesn't need this.
# In fact, there seems to be a problem if I used the Threads package
# and try using this line, so I'm just skipping it entirely for OS X.
if (NOT APPLE)
set(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT})
get_property(INTERFACE_LINK_LIBRARIES TARGET SDL2 PROPERTY INTERFACE_LINK_LIBRARIES)
set_property(TARGET SDL2 PROPERTY
INTERFACE_LINK_LIBRARIES ${INTERFACE_LINK_LIBRARIES} ${CMAKE_THREADS_LIBS_INIT})
endif ()

# For MinGW library
if (MINGW)
set(SDL2_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL2_LIBRARY_TEMP})
get_property(INTERFACE_LINK_LIBRARIES TARGET SDL2 PROPERTY INTERFACE_LINK_LIBRARIES)
set_property(TARGET SDL2 PROPERTY
INTERFACE_LINK_LIBRARIES ${INTERFACE_LINK_LIBRARIES} ${MINGW32_LIBRARY})
endif ()

# Set the final string here so the GUI reflects the final state.
set(SDL2_LIBRARY ${SDL2_LIBRARY_TEMP} CACHE STRING "Where the SDL2 Library can be found")
# Set the temp variable to INTERNAL so it is not seen in the CMake GUI
set(SDL2_LIBRARY_TEMP "${SDL2_LIBRARY_TEMP}" CACHE INTERNAL "")
endif ()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(SDL2 REQUIRED_VARS SDL2_LIBRARY SDL2_INCLUDE_DIR)
1 change: 1 addition & 0 deletions cmake/font-chef-config.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include("${CMAKE_CURRENT_LIST_DIR}/font-chef-targets.cmake")
33 changes: 33 additions & 0 deletions cmake/font-chef-cpack.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
set(FONT_CHEF_ARCH "x86")
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
set(FONT_CHEF_ARCH "x64")
endif ()

string(TOLOWER ${CMAKE_SYSTEM_NAME} FONT_CHEF_SYSTEM_NAME)
if (MSVC)
string(TOLOWER "${CMAKE_C_COMPILER_ID}${MSVC_TOOLSET_VERSION}" FONT_CHEF_COMPILER_TARGET)
else ()
string(TOLOWER "${CMAKE_C_COMPILER_ID}" FONT_CHEF_COMPILER_TARGET)
endif (MSVC)

set(CPACK_PACKAGE_VENDOR "Leonardo G. L. de Freitas")
set(CPACK_PACKAGE_DESCRIPTION
"Font Chef is a cross-platform C99 library to create character atlas of pre-rasterized glyphs from a font at a specified size and color")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "${CMAKE_PROJECT_DESCRIPTION}")
set(CPACK_PACKAGE_FILE_NAME
"${PROJECT_NAME}-${PROJECT_VERSION}-${FONT_CHEF_SYSTEM_NAME}-${FONT_CHEF_ARCH}-${FONT_CHEF_COMPILER_TARGET}")
set(CPACK_SOURCE_PACKAGE_FILE_NAME
"${PROJECT_NAME}-${PROJECT_VERSION}")
set(CPACK_PACKAGE_NAME KiWi)

set(CPACK_SOURCE_IGNORE_FILES .*build.*/ .idea/ .git/)

if (WIN32)
set(CPACK_SOURCE_GENERATOR "ZIP")
set(CPACK_GENERATOR "ZIP")
else()
set(CPACK_SOURCE_GENERATOR "TGZ")
set(CPACK_GENERATOR "TGZ")
endif()

include(CPack)
24 changes: 24 additions & 0 deletions cmake/setup-exports.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
include(CMakePackageConfigHelpers)
include(GNUInstallDirs)

# Setup install of exported targets
install(EXPORT font-chef-targets
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/font-chef
)

# Macro to write config
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/font-chef-config-version.cmake"
VERSION ${font-chef_VERSION}
COMPATIBILITY SameMajorVersion
)

# Setup install of version config
install(
FILES
cmake/font-chef-config.cmake
"${CMAKE_CURRENT_BINARY_DIR}/font-chef-config-version.cmake"
DESTINATION
${CMAKE_INSTALL_LIBDIR}/cmake/font-chef
COMPONENT "Font Chef Development"
)
Loading

0 comments on commit 5bbfdbb

Please sign in to comment.