Font To Byte

Eirene

Newbie
Newbie
Newbie
Newbie
Status
Offline
Joined
Jun 14, 2019
Messages
2
Reaction score
0
Hi Guys


How do I convert a font to bytes and add it to the menu buttons?
 

DREDD

Administrator
Administrator
Administrator
Administrator
Status
Offline
Joined
Apr 18, 2019
Messages
147
Reaction score
249
Hi Guys


How do I convert a font to bytes and add it to the menu buttons?
Code:
#include <iostream>
#include <sstream>
#include <Windows.h>
#include <fstream>
#include <iomanip>
#include <cctype>
#include <experimental/filesystem>

namespace fs = std::experimental::filesystem;

bool GenerateCode(std::string file)
{
    const fs::path FilePath = file;
    std::ifstream GenerateFile(FilePath, std::ios::binary);
    if (GenerateFile.fail()) {
        std::cout << "Could not open file!" << std::endl;
        return false;
    }
    std::cout << "Open file: " << FilePath << "\n" << std::endl;

    GenerateFile.seekg(0, std::ios_base::end);
    size_t sizeFile = GenerateFile.tellg();
    std::cout << "Size: " << sizeFile << std::endl;
    GenerateFile.seekg(0, GenerateFile.beg);

    std::string szNameFile = FilePath.stem().string();
    std::string szOutFile = szNameFile.append(".h");
    std::cout << "Out File: " << szOutFile << std::endl;

    std::ofstream StreamOut(szOutFile, std::ios::trunc);
    if (StreamOut.fail()) {
        std::cout << "Could not create file!" << std::endl;
        return false;
    }

    std::string szNameArarry = FilePath.stem().string();
    szNameArarry.erase(std::remove_if(szNameArarry.begin(), szNameArarry.end(),
        [](auto const& c) -> bool
    {
        return !std::isalnum(c);
    }),
        szNameArarry.end());

    StreamOut << "//GenerateCode by  Kraysler, 2018\n\n//File size: " << sizeFile << std::endl;
    StreamOut << "unsigned char " << szNameArarry << "[] =" << std::endl;
    StreamOut << "{" << std::endl;
    StreamOut << "\t";

    std::stringstream ss;
    ss << std::hex << std::uppercase << std::setfill('0');
    const char* separator = "";
    int col = 0;

    auto it = std::istreambuf_iterator<char>(GenerateFile);
    auto eof = std::istreambuf_iterator<char>();
    while (!it.equal(eof)) {
        unsigned int c = *it++ & 0xFF;
        ss << separator << "0x" << std::setw(2) << c;
        separator = ", ";
        if (++col % 16 == 0) {
            if (it != eof) {
                ss << "," << std::endl;
                ss << "\t";
                separator = "";
            }
        }
    }

    StreamOut << ss.str();
    StreamOut << std::endl;
    StreamOut << "};" << std::endl;

    GenerateFile.close();
    StreamOut.close();
}
call this

Code:
ImFontConfig config;
    config.MergeMode = true;
    static const ImWchar icon_ranges[] = { ICON_MIN_FA, ICON_MAX_FA, 0 };
    this->AwesomeFont = io.Fonts->AddFontFromMemoryTTF((void*)AwesomeArry, sizeof(AwesomeArry), 20.0f, &config, icon_ranges);
at the output we get
Code:
io.Fonts->AddFontFromMemoryTTF((void*)FontArry, sizeof(FontArry), fSizeFont);
 

Eirene

Newbie
Newbie
Newbie
Newbie
Status
Offline
Joined
Jun 14, 2019
Messages
2
Reaction score
0
Code:
#include <iostream>
#include <sstream>
#include <Windows.h>
#include <fstream>
#include <iomanip>
#include <cctype>
#include <experimental/filesystem>

namespace fs = std::experimental::filesystem;

bool GenerateCode(std::string file)
{
    const fs::path FilePath = file;
    std::ifstream GenerateFile(FilePath, std::ios::binary);
    if (GenerateFile.fail()) {
        std::cout << "Could not open file!" << std::endl;
        return false;
    }
    std::cout << "Open file: " << FilePath << "\n" << std::endl;

    GenerateFile.seekg(0, std::ios_base::end);
    size_t sizeFile = GenerateFile.tellg();
    std::cout << "Size: " << sizeFile << std::endl;
    GenerateFile.seekg(0, GenerateFile.beg);

    std::string szNameFile = FilePath.stem().string();
    std::string szOutFile = szNameFile.append(".h");
    std::cout << "Out File: " << szOutFile << std::endl;

    std::ofstream StreamOut(szOutFile, std::ios::trunc);
    if (StreamOut.fail()) {
        std::cout << "Could not create file!" << std::endl;
        return false;
    }

    std::string szNameArarry = FilePath.stem().string();
    szNameArarry.erase(std::remove_if(szNameArarry.begin(), szNameArarry.end(),
        [](auto const& c) -> bool
    {
        return !std::isalnum(c);
    }),
        szNameArarry.end());

    StreamOut << "//GenerateCode by  Kraysler, 2018\n\n//File size: " << sizeFile << std::endl;
    StreamOut << "unsigned char " << szNameArarry << "[] =" << std::endl;
    StreamOut << "{" << std::endl;
    StreamOut << "\t";

    std::stringstream ss;
    ss << std::hex << std::uppercase << std::setfill('0');
    const char* separator = "";
    int col = 0;

    auto it = std::istreambuf_iterator<char>(GenerateFile);
    auto eof = std::istreambuf_iterator<char>();
    while (!it.equal(eof)) {
        unsigned int c = *it++ & 0xFF;
        ss << separator << "0x" << std::setw(2) << c;
        separator = ", ";
        if (++col % 16 == 0) {
            if (it != eof) {
                ss << "," << std::endl;
                ss << "\t";
                separator = "";
            }
        }
    }

    StreamOut << ss.str();
    StreamOut << std::endl;
    StreamOut << "};" << std::endl;

    GenerateFile.close();
    StreamOut.close();
}
call this

Code:
ImFontConfig config;
    config.MergeMode = true;
    static const ImWchar icon_ranges[] = { ICON_MIN_FA, ICON_MAX_FA, 0 };
    this->AwesomeFont = io.Fonts->AddFontFromMemoryTTF((void*)AwesomeArry, sizeof(AwesomeArry), 20.0f, &config, icon_ranges);
at the output we get
Code:
io.Fonts->AddFontFromMemoryTTF((void*)FontArry, sizeof(FontArry), fSizeFont);

Thanks for help ❤
 
Top