| Summary: | [painting] GC.drawString draws 1 pixel too high compared to StyledText | ||||||
|---|---|---|---|---|---|---|---|
| Product: | [Eclipse Project] Platform | Reporter: | Dani Megert <daniel_megert> | ||||
| Component: | Text | Assignee: | Dani Megert <daniel_megert> | ||||
| Status: | RESOLVED FIXED | QA Contact: | |||||
| Severity: | normal | ||||||
| Priority: | P3 | CC: | aleherb+eclipse, eclipse.felipe, snorthov | ||||
| Version: | 3.3 | ||||||
| Target Milestone: | 3.3 M4 | ||||||
| Hardware: | PC | ||||||
| OS: | Windows XP | ||||||
| Whiteboard: | |||||||
| Attachments: |
|
||||||
|
Description
Dani Megert
Created attachment 54404 [details]
SWT snippet showing the bug
Is not GC drawing one pixel too high, it is StyledText drawing one pixel to low. The height of line in styledtext has to be high enough to fit the normal font, the bold, the italic, and the bold-italic. And when it draws it uses the max baseline of them all. That is avoid the line moving down when the user types the first bold word in the line. The fix is to use TextLayou to draw (you will still need to set the baseline on it). Or use: int baseline = text.getBaseline(index); FontMetrics fm = event.gc.getFontMetrics(); int fontBaseline = fm.getAscent() + fm.getLeading(); event.gc.drawString(SEARCH_STRING, topLeft.x, topLeft.y + baseline - fontBaseline, true); Using TextLayout: int baseline = text.getBaseline(index); int lineHeight = text.getLineHeight(index); TextLayout tl = new TextLayout(display); tl.setText(SEARCH_STRING); tl.setFont(event.gc.getFont()); tl.setAscent(baseline); tl.setDescent(lineHeight-baseline); tl.draw(event.gc, topLeft.x, topLeft.y); tl.dispose(); Note: it works with some fonts cause in some fonts the normal, the bold, the italic, and the bold-italic version all have the same metrics. Final note: Personally I would use TextLayout instead of GC.drawString to workaround bug 78268. Which of the two is cheaper? This code might be called often. (In reply to comment #3) > Which of the two is cheaper? This code might be called often. GC is cheaper Note that TextLayout objects need to be disposed. Fixed in HEAD. Available in builds > N20061201-0010. |