IconFontCppHeaders

repository·main·Indexed 23 days ago

https://github.com/juliettef/iconfontcppheaders

Provides language-specific bindings (C, C++, C#, Python, Rust, and Go) for popular icon fonts including Font Awesome, Google Material Design, Lucide, and others. It allows developers to reference icon code points via constants and includes a Python utility, GenerateIconFontCppHeaders.py, to convert TTF files into C/C++ byte arrays. The library provides specific integration guidance for Dear ImGui and supports various font sets such as Fork Awesome, Kenney game icons, and Codicons.

Tokens
5.7K
Snippets
8
Records
26
Agent score
81%

What's inside IconFontCppHeaders

  1. What is IconFontCppHeaders

    main

    IconFontCppHeaders provides language-specific files (C, C++, C#, Python, Rust, and Go) for using popular icon fonts in your applications. It includes support for Font Awesome, Fork Awesome, Google Material Design, Pictogrammers Material Design, Kenney game icons, Fontaudio, Codicons, and Lucide.

    Each generated language file contains:

    • ICON_* defines for each icon code point.
    • min code point (excluding ASCII characters).
    • max code point.
    • max 16 bit code point (useful for libraries like Dear ImGui that only support 16-bit code points).
  2. Supported Icon Font Sets

    main

    The repository provides language files and integration support for the following icon sets:

    • Font Awesome (FA): Versions 4, 5, 6, and 7 (Free). Note that for FA 5+, brands are in separate files.
    • Fork Awesome (FK)
    • Google Material Design Icons (MD) & Material Symbols (MS)
    • Pictogrammers Material Design Icons (MDI)
    • Kenney Game Icons (KI)
    • Fontaudio (FAD)
    • Codicons (CI) (VS Code icons)
    • Lucide (LC)

    Note: Ionicons is currently unsupported.

  3. Convert TTF font files to C and C++ headers

    main

    You can use the provided Python script GenerateIconFontCppHeaders.py to convert .ttf icon font files into C and C++ header files. Each converted file will contain a single array of bytes representing the font.

    To enable this conversion, run the script with the ttf2headerC flag set to True.

  4. Generate Font Awesome Pro language files

    main

    If you have a Font Awesome Pro subscription, you can generate the compatible language files for this project using the provided script. This process is currently documented for version 5, but is similar for version 6 and above.

    1. Download the Font Awesome Pro Web package from fontawesome.com.
    2. Place the icons.yml file (found in .. ontawesome-pro-n.n.n-web\metadata\icons.yml) in the same directory as GenerateIconFontCppHeaders.py.
    3. Run the GenerateIconFontCppHeaders.py script.
    4. Use the generated language files with the corresponding .ttf files from your Pro package:
      • fa-brands-400.ttf
      • fa-light-300.ttf
      • fa-regular-400.ttf
      • fa-solid-900.ttf
  5. Use IconFontCppHeaders with Dear ImGui

    main

    To use icon fonts in a Dear ImGui application, you need to merge the icon font into your existing font atlas. Because icon fonts often have different scaling requirements to align with standard text, it is recommended to reduce the icon font size (e.g., by a factor of 2.0f/3.0f).

    Steps:

    1. Include the specific header for the icon set you are using (e.g., #include "IconsFontAwesome5.h").
    2. Define the icon range using the provided constants (e.g., ICON_MIN_FA to ICON_MAX_16_FA).
    3. Configure ImFontConfig with MergeMode = true to combine the icon font with your default font.
    4. Use string literal concatenation to display icons alongside text (e.g., ICON_FA_NAME " Text").
    #include "IconsFontAwesome5.h"
    
    ImGuiIO& io = ImGui::GetIO();
    io.Fonts->AddFontDefault();
    float baseFontSize = 13.0f; // 13.0f is the size of the default font. Change to the font size you use.
    float iconFontSize = baseFontSize * 2.0f / 3.0f; // FontAwesome fonts need to have their sizes reduced by 2.0f/3.0f in order to align correctly
    
    // merge in icons from Font Awesome
    static const ImWchar icons_ranges[] = { ICON_MIN_FA, ICON_MAX_16_FA, 0 };
    ImFontConfig icons_config; 
    icons_config.MergeMode = true; 
    icons_config.PixelSnapH = true; 
    icons_config.GlyphMinAdvanceX = iconFontSize;
    io.Fonts->AddFontFromFileTTF( FONT_ICON_FILE_NAME_FAS, iconFontSize, &icons_config, icons_ranges );
    // use FONT_ICON_FILE_NAME_FAR if you want regular instead of solid
    
    // in an imgui window somewhere...
    ImGui::Text( ICON_FA_PAINT_BRUSH "  Paint" ); // use string literal concatenation
    // outputs a paint brush icon and 'Paint' as a string.
  6. Use Font Awesome 5 Brands icons in Go

    main

    The IconsFontAwesome5Brands variable provides a mapping of icon names to their corresponding UTF-8 encoded icon glyphs for the Font Awesome 5 Brands set. This set is intended for use with the fa-brands-400.ttf font file.

    To use these icons, access the Icons map within the IconsFontAwesome5Brands struct. The keys are the icon names (e.g., "Facebook", "Github", "Python") and the values are the string representations of the icon glyphs.

  7. Access Material Design icon definitions in Go

    main

    The IconsMaterialDesign variable provides access to Material Design icon code points in Go. It is an instance of the Font type and contains metadata about the font files used and a map of icon names to their corresponding UTF-8 encoded string representations.

    To use an icon, access its value from the Icons map using the icon's name as the key.

  8. Use Font Awesome 7 Brands icons in Go

    main

    The IconsFontAwesome7Brands variable provides a mapping of Font Awesome 7 Brand icon names to their corresponding UTF-8 encoded icon characters. This allows you to programmatically access specific brand icons by their string name.

    To use these icons, access the Icons map within the IconsFontAwesome7Brands variable. The values are strings containing the icon's byte sequence, which can be printed or rendered if the appropriate font (e.g., fa-brands-400.woff2) is loaded in your environment.

  9. Use Fontaudio icons in Go

    main

    The IconsFontaudio variable provides a mapping of icon names to their corresponding UTF-8 encoded string representations for the Fontaudio icon font. You can use these strings to render specific audio-related icons in your Go application.

    To use them, access the Icons map within the IconsFontaudio variable using the desired icon name as the key. The font files associated with these icons are fontaudio.ttf (aliased as FAD).

  10. Use Pictogrammers Material Design Icons in Go

    main

    The IconsMaterialDesignIcons variable provides access to the Pictogrammers Material Design Icons (MDI) set. It is a Font struct containing metadata about the font files and a map of icon names to their corresponding UTF-8 encoded string representations (code points).

    To use an icon, access its name from the Icons map. The map keys are the icon names (e.g., "AbTesting", "Account"), and the values are the byte sequences representing the icon character.

  11. Use Font Awesome 5 Pro Brands icons in Go

    main

    The IconsFontAwesome5ProBrands variable provides a mapping of icon names to their corresponding UTF-8 encoded icon characters for the Font Awesome 5 Pro Brands set. This is intended for use with the fa-brands-400.ttf font file.

    To use these icons, access the Icons map within the IconsFontAwesome5ProBrands struct. Each key is the icon name (e.g., "Amazon", "Github", "Python") and the value is the string containing the icon's character code.

  12. Use Material Design icon code points in Go

    main

    The IconsMaterialDesign.go file provides a mapping of Material Design icon names to their corresponding UTF-8 encoded string representations (code points). Developers can use these constants to render specific Material Design icons in Go applications that support icon fonts.

    Each entry in the map follows the pattern: "Icon_Name": "\xHH\xHH\xHH" (where the value is the UTF-8 byte sequence) and includes a comment with the Unicode hex value (e.g., // U+e417).