nanogui
nanogui copied to clipboard
How to include Custom Fonts?
I just want to use a new ttf Font, because the default font do not support CJK. but I do not find any API to load a new font, so How to include Custom Fonts?
in NanoGUI document, I find something.
# Including Custom Fonts
NanoGUI uses the [Roboto](https://fonts.google.com/specimen/Roboto) font for text, and [Entypo](http://www.entypo.com/) font for icons. If you wish to add your own custom font, all you need is a True Type file (a .ttf extension). NanoGUI will glob all fonts found in resources by expanding resources/*.ttf. So if you had the directory structure
myproject/
CMakeLists.txt <- where this code is
fonts/
superfont.ttf
ext/
nanogui/
resources/
You simply need to copy the superfont.ttf to NanoGUI’s resources directory:
file(
COPY ${CMAKE_CURRENT_SOURCE_DIR}/fonts/superfont.ttf
DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/ext/nanogui/resources/superfont.ttf
)
When you build the code, there should be a file nanogui_resources.h generated. If everything worked, your new font should have been included.
Does this mean I have to recomplie NanoGUI? but I don't want to recomplie this library, who can help me please。 /(ㄒoㄒ)/~~
Use nvgCreateFont method to load extra font(.ttf) files. You don't need to recompile this library to support CJK.
example code :
// Change multibyte to UTF8.
// reference : https://doitnow-man.tistory.com/entry/%EC%9C%88%EB%8F%84%EC%9A%B0-window-%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%B0%8D-%ED%95%9C%EA%B8%80-%EC%B2%98%EB%A6%AC-c
std::string MultiByteToUtf8(std::string multibyte_str)
{
char* pszIn = new char[multibyte_str.length() + 1];
strncpy_s(pszIn, multibyte_str.length() + 1, multibyte_str.c_str(), multibyte_str.length());
std::string resultString;
int nLenOfUni = 0, nLenOfUTF = 0;
wchar_t* uni_wchar = NULL;
char* pszOut = NULL;
// 1. ANSI(multibyte) Length
if ((nLenOfUni = MultiByteToWideChar(CP_ACP, 0, pszIn, (int)strlen(pszIn), NULL, 0)) <= 0)
return "";
uni_wchar = new wchar_t[nLenOfUni + 1];
memset(uni_wchar, 0x00, sizeof(wchar_t) * (nLenOfUni + 1));
// 2. ANSI(multibyte) ---> unicode
nLenOfUni = MultiByteToWideChar(CP_ACP, 0, pszIn, (int)strlen(pszIn), uni_wchar, nLenOfUni);
// 3. utf8 Length
if ((nLenOfUTF = WideCharToMultiByte(CP_UTF8, 0, uni_wchar, nLenOfUni, NULL, 0, NULL, NULL)) <= 0)
{
delete[] uni_wchar;
return "";
}
pszOut = new char[nLenOfUTF + 1];
memset(pszOut, 0, sizeof(char) * (nLenOfUTF + 1));
// 4. unicode ---> utf8
nLenOfUTF = WideCharToMultiByte(CP_UTF8, 0, uni_wchar, nLenOfUni, pszOut, nLenOfUTF, NULL, NULL);
pszOut[nLenOfUTF] = 0;
resultString = pszOut;
delete[] uni_wchar;
delete[] pszOut;
return resultString;
}
class ExampleApplication : public nanogui::Screen {
public:
ExampleApplication() : nanogui::Screen(Eigen::Vector2i(1024, 768), "NanoGUI Test") {
using namespace nanogui;
// 1. Load your ttf file, name it.
nvgCreateFont(mNVGContext, "NanumGothic", "../resources/nanum-gothic/NanumGothic.ttf");
nvgCreateFont(mNVGContext, "NanumGothic-bold", "../resources/nanum-gothic/NanumGothicBold.ttf");
Window *window = new Window(this, "Button demo");
window->setPosition(Vector2i(15, 15));
window->setLayout(new GroupLayout());
/* No need to store a pointer, the data structure will be automatically
freed when the parent window is deleted */
// 2. Use like this.
new Label(window, MultiByteToUtf8("한국어,中國語,日本語,English"), "NanumGothic-bold");
...
};
Nice, this looks like a handy thing to have an example for in the codebase.