Merge lp:~3v1n0/unity/static-cairo-text-fix-layout-width into lp:unity

Proposed by Marco Trevisan (Treviño)
Status: Merged
Approved by: Andrea Azzarone
Approved revision: no longer in the source branch.
Merged at revision: 3206
Proposed branch: lp:~3v1n0/unity/static-cairo-text-fix-layout-width
Merge into: lp:unity
Diff against target: 112 lines (+31/-23)
1 file modified
unity-shared/StaticCairoText.cpp (+31/-23)
To merge this branch: bzr merge lp:~3v1n0/unity/static-cairo-text-fix-layout-width
Reviewer Review Type Date Requested Status
PS Jenkins bot (community) continuous-integration Approve
Andrea Azzarone (community) Approve
Review via email: mp+152565@code.launchpad.net

Commit message

StaticCairoText: use the maximum size for setting the layout one in DrawText

Otherwise using the text-extent could make the StaticCairoText to use more lines
than the ones that it really needs (making the last one not being drawn).

Description of the change

Static Cairo Text is using a similar code to compute the actual text size and to draw it, however when drawing it uses the text-extents computed on the previous pass to set the new PangoLayout width. This is not correct since the text-extents width can be actually smaller than the space that the text may use.

This leads to two issues: (1) the text does not use all the horizontal space it could, (2) the last line can be cut off. As you can see for example in [1] or [2].

The fix consists on setting the PangoLayout sizes to the nux Area MaximumSizes (if set) or not to set them at all.

[1] http://i.imgur.com/8C6aWHr.png
[2] http://i.imgur.com/j46yszU.png
[3] http://i.imgur.com/oAGt2Df.png
[4] http://i.imgur.com/mMgC7SZ.png

To post a comment you must log in.
Revision history for this message
Andrea Azzarone (azzar1) wrote :

LGTM,

review: Approve
Revision history for this message
PS Jenkins bot (ps-jenkins) :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'unity-shared/StaticCairoText.cpp'
2--- unity-shared/StaticCairoText.cpp 2013-01-16 15:43:38 +0000
3+++ unity-shared/StaticCairoText.cpp 2013-03-09 17:35:24 +0000
4@@ -70,7 +70,7 @@
5 Size GetTextExtents() const;
6
7 void SetAttributes(PangoLayout* layout);
8- void DrawText(CacheTexture::Ptr cached_texture, int width, int height, int line_spacing, Color const& color);
9+ void DrawText(CacheTexture::Ptr const& cached_texture);
10
11 void UpdateTexture();
12 void OnFontChanged();
13@@ -576,7 +576,7 @@
14 if (inkRect.x + inkRect.width > logRect.x + logRect.width)
15 result.width = std::ceil(static_cast<float>(inkRect.x + inkRect.width - logRect.x) / PANGO_SCALE);
16 else
17- result.width = std::ceil(static_cast<float>(logRect.width) / PANGO_SCALE);
18+ result.width = std::ceil(static_cast<float>(logRect.width) / PANGO_SCALE);
19
20 result.height = std::ceil(static_cast<float>(logRect.height) / PANGO_SCALE);
21 cached_extent_ = result;
22@@ -666,16 +666,28 @@
23 pango_layout_set_attributes(layout, attr_list);
24 }
25
26-void StaticCairoText::Impl::DrawText(CacheTexture::Ptr cached_texture,
27- int width,
28- int height,
29- int line_spacing,
30- Color const& color)
31+void StaticCairoText::Impl::DrawText(CacheTexture::Ptr const& texture)
32 {
33- if (!cached_texture)
34+ if (!texture)
35 return;
36- cached_texture->cr.reset(new CairoGraphics(CAIRO_FORMAT_ARGB32, width, height));
37- cairo_t* cr = cached_texture->cr->GetInternalContext();
38+
39+ nux::Size tex_size(parent_->GetMaximumWidth(), parent_->GetMaximumHeight());
40+ nux::Size layout_size(tex_size.width * PANGO_SCALE, tex_size.height * PANGO_SCALE);
41+
42+ if (tex_size.width == nux::AREA_MAX_WIDTH)
43+ {
44+ tex_size.width = parent_->GetWidth();
45+ layout_size.width = -1;
46+ }
47+
48+ if (tex_size.height == nux::AREA_MAX_HEIGHT)
49+ {
50+ tex_size.height = parent_->GetHeight();
51+ layout_size.height = -1;
52+ }
53+
54+ texture->cr.reset(new CairoGraphics(CAIRO_FORMAT_ARGB32, tex_size.width, tex_size.height));
55+ cairo_t* cr = texture->cr->GetInternalContext();
56
57 PangoLayout* layout = NULL;
58 PangoFontDescription* desc = NULL;
59@@ -684,7 +696,7 @@
60 GdkScreen* screen = gdk_screen_get_default(); // not ref'ed
61 GtkSettings* settings = gtk_settings_get_default(); // not ref'ed
62
63- std::string text = text_.substr(cached_texture->start_index, cached_texture->length);
64+ std::string text = text_.substr(texture->start_index, texture->length);
65
66 std::string font(GetEffectiveFont());
67 cairo_set_font_options(cr, gdk_screen_get_font_options(screen));
68@@ -697,9 +709,9 @@
69 pango_layout_set_ellipsize(layout, GetPangoEllipsizeMode());
70 pango_layout_set_alignment(layout, GetPangoAlignment());
71 pango_layout_set_markup(layout, text.c_str(), -1);
72- pango_layout_set_width(layout, width * PANGO_SCALE);
73- pango_layout_set_height(layout, height * PANGO_SCALE);
74- pango_layout_set_spacing(layout, line_spacing * PANGO_SCALE);
75+ pango_layout_set_width(layout, layout_size.width);
76+ pango_layout_set_height(layout, layout_size.height);
77+ pango_layout_set_spacing(layout, line_spacing_ * PANGO_SCALE);
78
79 pango_layout_set_height(layout, lines_);
80
81@@ -724,7 +736,7 @@
82 cairo_paint(cr);
83
84 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
85- cairo_set_source_rgba(cr, color.red, color.green, color.blue, color.alpha);
86+ cairo_set_source_rgba(cr, text_color_.red, text_color_.green, text_color_.blue, text_color_.alpha);
87
88 pango_layout_context_changed(layout);
89
90@@ -740,19 +752,15 @@
91
92 void StaticCairoText::Impl::UpdateTexture()
93 {
94- Size size = GetTextExtents();
95+ auto const& size = GetTextExtents();
96 parent_->SetBaseSize(size.width, size.height);
97- nux::Geometry const& geo = parent_->GetGeometry();
98
99 textures2D_.clear();
100- auto iter = cache_textures_.begin();
101- for (; iter != cache_textures_.end(); ++iter)
102+ for (auto const& texture : cache_textures_)
103 {
104- CacheTexture::Ptr const& cached_texture = *iter;
105- DrawText(cached_texture,
106- geo.width, cached_texture->height, line_spacing_, text_color_);
107+ DrawText(texture);
108
109- textures2D_.push_back(texture_ptr_from_cairo_graphics(*cached_texture->cr));
110+ textures2D_.push_back(texture_ptr_from_cairo_graphics(*texture->cr));
111 }
112 }
113