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

Subpixel text rendering #289

Open
eliemichel opened this issue Aug 31, 2020 · 17 comments
Open

Subpixel text rendering #289

eliemichel opened this issue Aug 31, 2020 · 17 comments
Labels
bug Something isn't working perf performance related issue piet-direct2d issue in the direct2d backend

Comments

@eliemichel
Copy link

Is there any way already to perform subpixel rendering? Or any plan to support so? It'd quite useful in order to get readable text in typical UI widgets!

@cmyr
Copy link
Member

cmyr commented Sep 1, 2020

We should be using the platform default rendering options; subpixel should be disabled on recent versions of macOS (10.14+) and (I believe) enabled on windows?

Linux is another story, and I'm honestly not sure what cairo is doing.

@eliemichel
Copy link
Author

Mmm, Windows here, no subpixel rendering when using druid.

@raphlinus
Copy link
Contributor

I think I know why, it's probably because the clipping is not respecting ClearType. The relevant bit of code is push_layer_mask, where it's setting D2D1_LAYER_OPTIONS_NONE and that should probably be D2D1_LAYER_OPTIONS_INITIALIZE_FOR_CLEARTYPE.

@cmyr
Copy link
Member

cmyr commented Sep 2, 2020

I won't have a windows machine handy until early next week, @eliemichel if you wanted to try this fix a PR would be welcome!

@eliemichel
Copy link
Author

Ok, I gave it a shot, but changing the line suggested by @raphlinus is not enough:
druidaa

This part of the doc says that:

If the render target has an alpha channel and is not set to D2D1_ALPHA_MODE_IGNORE, then the default text antialiasing mode is grayscale.

So I naively tried to replace any occurence to other alpha modes by D2D1_ALPHA_MODE_IGNORE, just to see, but still no subpixel AA.

PS: Was there an easier way to try this change than cloning druid, changing all references to piet-common in the Cargo.toml files to point to my own local copy of piet?

@cmyr
Copy link
Member

cmyr commented Sep 2, 2020

the main piet backends have the ability to generate a number of reference images; you would run,

cargo run --example=test-picture  -- N

were N is the number of a picture; they're listed in piet/src/samples. For this I would try using no. 13.

@raphlinus
Copy link
Contributor

raphlinus commented Sep 2, 2020

PS: Was there an easier way to try this change than cloning druid, changing all references to piet-common in the Cargo.toml files to point to my own local copy of piet?

Yes, that's what [patch] is for.

I thought I looked at this a year or so ago, and am pretty sure I was seeing ClearType rendering. But there a few things that can go wrong.

@raphlinus raphlinus reopened this Sep 2, 2020
@raphlinus
Copy link
Contributor

Also, one issue with using the reference image command line is that it uses a different process for creating the render targets than for screen presentation, and that might be where the problem is.

@eliemichel
Copy link
Author

@cmyr oh thx but the examples (and tests as well) refer to data from outside the repo:

   Compiling piet v0.2.0-pre2 (E:\SourceCode\piet\piet)
error: couldn't read piet\src\samples\../../../snapshots/resources/Anaheim-Regular.ttf: The system cannot find the path specified. (os error 3)
  --> piet\src\samples\picture_13.rs:16:28
   |
16 |       let _ = text.load_font(include_bytes!(
   |  ____________________________^
17 | |         "../../../snapshots/resources/Anaheim-Regular.ttf"
18 | |     ));
   | |_____^
   |

@raphlinus great I did not know about patch it's perfect (I'm still pretty new to rust)

@cmyr
Copy link
Member

cmyr commented Sep 2, 2020

oops yea it's in a submodule. git submodule init && git submodule update.

@eliemichel
Copy link
Author

eliemichel commented Sep 2, 2020

Oh yes my bad then! Here is fig. 13:

d2d-test-13

No ClearType on sight..

@raphlinus
Copy link
Contributor

Hmm, this is a deeper problem than I thought. That flag should work in Windows 7, but according to the docs, Starting in Windows 8, you cannot render with ClearType in a layer. I was going to mention in an earlier comment, using axis aligned clips should improve the situation (as well as be a performance boost), but the problem is that then not all clip paths are handled the same. For example, if you clip to a rounded rect (as you might in a button), then the ClearType would also disappear.

@raphlinus
Copy link
Contributor

Yes, after a bit more digging, it seems the solution is to use the axis aligned clip methods when the clip shape is a rectangle. That'd actually be a great issue to take on.

(ClearType works when there's no clipping, but the problem got worse with the partial invalidation work, now we always clip to the invalidation region)

@cmyr cmyr added bug Something isn't working piet-direct2d issue in the direct2d backend labels Sep 2, 2020
@raphlinus raphlinus added the perf performance related issue label Sep 2, 2020
@eliemichel
Copy link
Author

Ok I don't think I am aware enough of the way Direct2D is working nor piet is designed to take this over..

@cmyr
Copy link
Member

cmyr commented Sep 2, 2020

@eliemichel no worries, thanks for opening the issue, someone will get to it before too long. :)

@smmalis37
Copy link
Collaborator

Some quick profiling on https://github.com/smmalis37/sudoku_rust turned up that 40% of all of the cpu time was being consumed inside this call to clip https://github.com/linebender/druid/blob/9564af60b12e749c2b2c6d91dde1b235cec5d159/druid/src/widget/label.rs#L339

Changing my label's line break mode to either of the other options made the app significantly more responsive feeling.

@cmyr
Copy link
Member

cmyr commented Sep 12, 2020

@smmalis37 interesting. The very least we can do is overflow by default, I'd happily take that patch in druid if you like?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working perf performance related issue piet-direct2d issue in the direct2d backend
Projects
None yet
Development

No branches or pull requests

4 participants