Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GUI] Support drawing text on canvas #1070

Merged
merged 10 commits into from May 31, 2020
Merged

Conversation

archibate
Copy link
Collaborator

@archibate
Copy link
Collaborator Author

Btw, why is our font resolution so bad?
图片

@archibate archibate requested review from k-ye and yuanming-hu and removed request for yuanming-hu May 28, 2020 16:35
@archibate
Copy link
Collaborator Author

Can't we just use the platform provided API to draw text? (e.g. TextOut in win32)

@yuanming-hu
Copy link
Member

yuanming-hu commented May 29, 2020

Thanks! Brain fried today but I'll take a look first thing tomorrow.

Can't we just use the platform provided API to draw text? (e.g. TextOut in win32)

GUI::text worked pretty well without these artifacts. I'll take a look.

Copy link
Member

@yuanming-hu yuanming-hu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much! Please address the comments.

To make this feature available to users, please

  1. Add a submodule containing https://github.com/yuanming-hu/taichi_assets/tree/master/fonts/go
  2. Fix the font path:
    auto ttf_path = root_dir + std::string("/assets/fonts/go/Go-Regular.ttf");
  3. Make sure the font file is available in release mode. See the shipping of examples as an example: https://github.com/taichi-dev/taichi/pull/1010/files
  4. Add doc

python/taichi/misc/gui.py Outdated Show resolved Hide resolved
python/taichi/misc/gui.py Outdated Show resolved Hide resolved
@archibate
Copy link
Collaborator Author

Can't we just load the system default font, or compile the font into libtaichi.so? It's tricky to copytree/rmtree nested-dir like external/fonts.

@archibate
Copy link
Collaborator Author

Problem:
When in release_mode, we ttf_path = "/fonts/Go.ttf".
When in dev_mode, we ttf_path = "/external/fonts/Go.ttf".
So how do I detect if it's dev_mode in C++?

@yuanming-hu yuanming-hu mentioned this pull request May 29, 2020
18 tasks
@archibate
Copy link
Collaborator Author

Now I just use external/fonts to keep consistency in release_mode, sleeping now, ff2 mt oyo fuat :)

@archibate
Copy link
Collaborator Author

How to test in release mode?

@yuanming-hu
Copy link
Member

yuanming-hu commented May 30, 2020

Thanks for implementing everything here!

Can't we just load the system default font, or compile the font into libtaichi.so? It's tricky to copytree/rmtree nested-dir like external/fonts.

Loading a system font (for all platforms) can be tricky...

So how do I detect if it's dev_mode in C++?

Please use is_release() in C++.

How to test in release mode?

Usually I do

cd python
python3 build.py test

@archibate
Copy link
Collaborator Author

(aug) [bate@archit taichi]$ cd python
(aug) [bate@archit python]$ python build.py test
[Taichi] mode=development
[Taichi] preparing sandbox at /tmp/taichi-krj62zja
[Taichi] sandbox prepared
[Taichi] <dev mode>, supported archs: [cpu, cuda, opengl], commit 98138bdb, python 3.8.2
Traceback (most recent call last):
  File "build.py", line 26, in <module>
    assert False, "Missing environment variable PYPI_PWD"
AssertionError: Missing environment variable PYPI_PWD

Why it needs PYPI_PWD? Wasn't it just test?

@yuanming-hu
Copy link
Member

Oh, my bad. Please use any value for that environment variable for now. In the future let's fix this on test mode.

@archibate
Copy link
Collaborator Author

FYI it also needs CXX=clang++ env.

(guitext) [bate@archit test_env]$ p keyboard.py 
[Taichi] mode=release
[Taichi] version 0.6.6, supported archs: [cpu, cuda, opengl], commit 5cab3f28, python 3.8.2
Traceback (most recent call last):
  File "keyboard.py", line 26, in <module>
    gui.text(f'({x:.3}, {y:.3})', (x, y))
AttributeError: 'GUI' object has no attribute 'text'

commit 5cab3f28? So build.py test only work for the latest [release]?

@yuanming-hu
Copy link
Member

yuanming-hu commented May 31, 2020

Maybe you have an existing v0.6.6 installation via pip? Please remove that first.

@archibate
Copy link
Collaborator Author

archibate commented May 31, 2020

Fixed. Now comes to:

Traceback (most recent call last):
  File "keyboard.py", line 26, in <module>
    gui.text(f'({x:.3}, {y:.3})', (x, y))
  File "/home/bate/.local/lib/python3.8/site-packages/taichi/misc/gui.py", line 143, in text
    self.canvas.text(context, pos, font_size, color)
RuntimeError: [image_buffer.cpp:write_text@120] Font file not found: /home/bate/.taichi//external/fonts/NotoSansMono-Regular.ttf

/home/bate/.taichi/?
I want /home/bate/.local/lib/python/site-packages/taichi/.
UPDATE: get_python_package_dir() doesn't work in dev_mode.

@yuanming-hu
Copy link
Member

yuanming-hu commented May 31, 2020

libdevice_path might be a good reference.

std::string libdevice_path() {
std::string folder;
if (is_release()) {
folder = compiled_lib_dir;
} else {
folder = fmt::format("{}/external/cuda_libdevice", get_repo_dir());
}
auto cuda_version_string = get_cuda_version_string();
auto cuda_version_major = int(std::atof(cuda_version_string.c_str()));
return fmt::format("{}/slim_libdevice.{}.bc", folder, cuda_version_major);
}

Btw, we should avoid committing huge binary files in the repo. Could you make the font file a submodule? Also how about sticking to the font file in taichi_assets, which I'm sure has a friendly license?

Thanks for working on this!

@archibate
Copy link
Collaborator Author

archibate commented May 31, 2020

I added https://github.com/taichi-dev/taichi_assets for storing font files, and may have other binary files in future (that's why I want the name taichi_assets instead of taichi_fonts, the latter is to limited).
So can we remove or rename https://github.com/yuanming-hu/taichi_assets to taichi_legacy_assets now? It's legacy and no longer used except for it's release page (CI resources) anyway.


Btw, I'll add ti rel_test as an alias of

rm -rf ~/.local/lib/python3.8/site-packages/taichi{,-0*.dist-info}/ && nc && (pushd python; PYPI_PWD=233 CXX=clang++ python build.py test; popd)

after #1085.

@archibate
Copy link
Collaborator Author

Working very well in release mode!
图片

Copy link
Member

@yuanming-hu yuanming-hu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Feel free to merge after committing the RGB conversion suggestion and adding brief documentation on this new function.

python/taichi/misc/gui.py Outdated Show resolved Hide resolved
Copy link
Member

@yuanming-hu yuanming-hu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Some final nits. Feel free to merge after applying them.

docs/gui.rst Outdated
Comment on lines 153 to 160
.. function:: gui.text(context, pos, font_size = 15, color = 0xFFFFFF)

:parameter gui: (GUI) the window object
:parameter pow: (tuple of 2) the top-left point position of the fonts / texts
:parameter font_size: (optional, scalar) the size of font (in height)
:parameter color: (optional, RGB hex) the foreground color of text

Draw a line of text on screen.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.. function:: gui.text(context, pos, font_size = 15, color = 0xFFFFFF)
:parameter gui: (GUI) the window object
:parameter pow: (tuple of 2) the top-left point position of the fonts / texts
:parameter font_size: (optional, scalar) the size of font (in height)
:parameter color: (optional, RGB hex) the foreground color of text
Draw a line of text on screen.
.. function:: gui.text(content, font_size = 15, color = 0xFFFFFF)
:parameter gui: (GUI) the GUI object
:parameter content: (``str``) the text to print
:parameter pos: (tuple of 2) the top-left anchor position of the text
:parameter font_size: (optional, scalar) font size (height, in pixels)
:parameter color: (optional, RGB hex) text foreground color
Draw text on the screen.

python/taichi/misc/gui.py Outdated Show resolved Hide resolved
python/taichi/misc/gui.py Outdated Show resolved Hide resolved
@archibate archibate added the GAMES201 GAMES 201 students' wishlist label May 31, 2020
@archibate archibate merged commit c8b2e94 into taichi-dev:master May 31, 2020
@yuanming-hu yuanming-hu mentioned this pull request May 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GAMES201 GAMES 201 students' wishlist
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[GUI] Draw text on canvas
3 participants