Merge lp:~3v1n0/unity/saticcairo-text-sizes-fix 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: 3221
Proposed branch: lp:~3v1n0/unity/saticcairo-text-sizes-fix
Merge into: lp:unity
Diff against target: 300 lines (+66/-61)
3 files modified
unity-shared/SearchBar.cpp (+1/-1)
unity-shared/SearchBar.h (+0/-1)
unity-shared/StaticCairoText.cpp (+65/-59)
To merge this branch: bzr merge lp:~3v1n0/unity/saticcairo-text-sizes-fix
Reviewer Review Type Date Requested Status
PS Jenkins bot (community) continuous-integration Approve
Andrea Azzarone (community) Approve
Review via email: mp+153554@code.launchpad.net

Commit message

StaticCairoText: recompute the extents if they exceed the maximum size

Keep the texture the most little that we can, then adjust its position when drawing.

Description of the change

Both the visual issues introduced in recent static-cairo text fixed, but keeping the fixes of the previous branch.
See: http://i.imgur.com/1tDNgzv.png

Now we we always compute the extents leaving the text to use all the spaces it needs, then we adjust it to match our maximum size.

Also the texture is using only the space it needs, while we adjust its position (based on alignment) when drawing.

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

Works here. Changes looks good. Thanks!

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/SearchBar.cpp'
2--- unity-shared/SearchBar.cpp 2012-12-17 18:00:40 +0000
3+++ unity-shared/SearchBar.cpp 2013-03-15 15:09:25 +0000
4@@ -187,7 +187,7 @@
5 show_filters_->SetFont(SHOW_FILTERS_LABEL_DEFAULT_FONT.c_str());
6 show_filters_->SetTextColor(nux::color::White);
7 show_filters_->SetTextAlignment(StaticCairoText::NUX_ALIGN_RIGHT);
8- show_filters_->SetLines(1);
9+ show_filters_->SetLines(-1);
10
11 nux::BaseTexture* arrow;
12 arrow = style.GetGroupExpandIcon();
13
14=== modified file 'unity-shared/SearchBar.h'
15--- unity-shared/SearchBar.h 2012-12-14 12:14:34 +0000
16+++ unity-shared/SearchBar.h 2013-03-15 15:09:25 +0000
17@@ -68,7 +68,6 @@
18 sigc::signal<void, std::string const&> live_search_reached;
19
20 private:
21-
22 void Init();
23
24 void OnFontChanged(GtkSettings* settings, GParamSpec* pspec=NULL);
25
26=== modified file 'unity-shared/StaticCairoText.cpp'
27--- unity-shared/StaticCairoText.cpp 2013-03-09 17:24:16 +0000
28+++ unity-shared/StaticCairoText.cpp 2013-03-15 15:09:25 +0000
29@@ -67,6 +67,8 @@
30
31 std::shared_ptr<CairoGraphics> cr;
32 };
33+
34+ void UpdateBaseSize();
35 Size GetTextExtents() const;
36
37 void SetAttributes(PangoLayout* layout);
38@@ -92,7 +94,7 @@
39 EllipsizeState ellipsize_;
40 AlignState align_;
41 AlignState valign_;
42- UnderlineState underline_;
43+ UnderlineState underline_;
44
45 std::string font_;
46
47@@ -114,7 +116,7 @@
48 , text_color_(color::White)
49 , ellipsize_(NUX_ELLIPSIZE_END)
50 , align_(NUX_ALIGN_LEFT)
51- , valign_(NUX_ALIGN_TOP)
52+ , valign_(NUX_ALIGN_CENTRE)
53 , underline_(NUX_UNDERLINE_NONE)
54 , lines_(-2) // should find out why -2...
55 // the desired height of the layout in Pango units if positive, or desired
56@@ -173,7 +175,6 @@
57 : View(NUX_FILE_LINE_PARAM)
58 , pimpl(new Impl(this, text))
59 {
60- SetMinimumSize(1, 1);
61 SetAcceptKeyNavFocusOnMouseDown(false);
62 }
63
64@@ -182,7 +183,6 @@
65 : View(NUX_FILE_LINE_PARAM)
66 , pimpl(new Impl(this, escape_text ? GetEscapedText(text) : text))
67 {
68- SetMinimumSize(1, 1);
69 SetAcceptKeyNavFocusOnMouseDown(false);
70 }
71
72@@ -226,17 +226,14 @@
73
74 void StaticCairoText::PreLayoutManagement()
75 {
76- Geometry geo = GetGeometry();
77+ Geometry const& geo = GetGeometry();
78
79 pimpl->pre_layout_size_.width = geo.width;
80 pimpl->pre_layout_size_.height = geo.height;
81+ pimpl->UpdateBaseSize();
82
83- SetBaseSize(pimpl->cached_extent_.width,
84- pimpl->cached_extent_.height);
85 if (pimpl->textures2D_.empty())
86- {
87 pimpl->UpdateTexture();
88- }
89
90 View::PreLayoutManagement();
91 }
92@@ -293,15 +290,28 @@
93 gfxContext.GetRenderStates().GetBlend(alpha, src, dest);
94 gfxContext.GetRenderStates().SetBlend(true, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
95
96- Color col = color::Black;
97- col.alpha = 0;
98- gfxContext.QRP_Color(base.x,
99- base.y,
100- base.width,
101- base.height,
102- col);
103-
104- int current_y = base.y + ((base.height - pimpl->cached_extent_.height) / 2);
105+ gfxContext.QRP_Color(base.x, base.y, base.width, base.height, color::Transparent);
106+
107+ int current_x = base.x;
108+ int current_y = base.y;
109+
110+ if (pimpl->align_ == NUX_ALIGN_CENTRE)
111+ {
112+ current_x += std::round((base.width - pimpl->cached_extent_.width) / 2.0f);
113+ }
114+ else if (pimpl->align_ == NUX_ALIGN_RIGHT)
115+ {
116+ current_x += base.width - pimpl->cached_extent_.width;
117+ }
118+
119+ if (pimpl->valign_ == NUX_ALIGN_CENTRE)
120+ {
121+ current_y += std::round((base.height - pimpl->cached_extent_.height) / 2.0f);
122+ }
123+ else if (pimpl->valign_ == NUX_ALIGN_BOTTOM)
124+ {
125+ current_y += base.height - pimpl->cached_extent_.height;
126+ }
127
128 for (BaseTexturePtr tex : pimpl->textures2D_)
129 {
130@@ -309,7 +319,7 @@
131 if (!text_tex)
132 break;
133
134- gfxContext.QRP_1Tex(base.x,
135+ gfxContext.QRP_1Tex(current_x,
136 current_y,
137 text_tex->GetWidth(),
138 text_tex->GetHeight(),
139@@ -406,7 +416,7 @@
140 void StaticCairoText::SetFont(std::string const& font)
141 {
142 if (pimpl->font_ != font)
143- {
144+ {
145 pimpl->font_ = font;
146 pimpl->need_new_extent_cache_ = true;
147 Size s = GetTextExtents();
148@@ -501,7 +511,8 @@
149 void StaticCairoText::AddProperties(GVariantBuilder* builder)
150 {
151 unity::variant::BuilderWrapper(builder)
152- .add(GetGeometry());
153+ .add(GetGeometry())
154+ .add("text", pimpl->text_);
155 }
156
157 std::string StaticCairoText::Impl::GetEffectiveFont() const
158@@ -524,8 +535,6 @@
159 PangoLayout* layout = NULL;
160 PangoFontDescription* desc = NULL;
161 PangoContext* pangoCtx = NULL;
162- PangoRectangle inkRect = {0, 0, 0, 0};
163- PangoRectangle logRect = {0, 0, 0, 0};
164 int dpi = 0;
165 GdkScreen* screen = gdk_screen_get_default(); // is not ref'ed
166 GtkSettings* settings = gtk_settings_get_default(); // is not ref'ed
167@@ -537,8 +546,7 @@
168
169 Size result;
170 std::string font = GetEffectiveFont();
171-
172- int maxwidth = parent_->GetMaximumWidth();
173+ nux::Size layout_size(-1, lines_ < 0 ? lines_ : std::numeric_limits<int>::min());
174
175 surface = cairo_image_surface_create(CAIRO_FORMAT_A1, 1, 1);
176 cr = cairo_create(surface);
177@@ -550,8 +558,8 @@
178 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
179 pango_layout_set_ellipsize(layout, GetPangoEllipsizeMode());
180 pango_layout_set_alignment(layout, GetPangoAlignment());
181- pango_layout_set_height(layout, lines_);
182- pango_layout_set_width(layout, maxwidth * PANGO_SCALE);
183+ pango_layout_set_width(layout, layout_size.width);
184+ pango_layout_set_height(layout, layout_size.height);
185 pango_layout_set_markup(layout, text_.c_str(), -1);
186 pango_layout_set_spacing(layout, line_spacing_ * PANGO_SCALE);
187
188@@ -570,15 +578,15 @@
189 (float) dpi / (float) PANGO_SCALE);
190 }
191 pango_layout_context_changed(layout);
192- pango_layout_get_extents(layout, &inkRect, &logRect);
193-
194- // logRect has some issues using italic style
195- if (inkRect.x + inkRect.width > logRect.x + logRect.width)
196- result.width = std::ceil(static_cast<float>(inkRect.x + inkRect.width - logRect.x) / PANGO_SCALE);
197- else
198- result.width = std::ceil(static_cast<float>(logRect.width) / PANGO_SCALE);
199-
200- result.height = std::ceil(static_cast<float>(logRect.height) / PANGO_SCALE);
201+ pango_layout_get_pixel_size(layout, &result.width, &result.height);
202+
203+ if (result.width > parent_->GetMaximumWidth())
204+ {
205+ pango_layout_set_width(layout, parent_->GetMaximumWidth() * PANGO_SCALE);
206+ pango_layout_context_changed(layout);
207+ pango_layout_get_pixel_size(layout, &result.width, &result.height);
208+ }
209+
210 cached_extent_ = result;
211 baseline_ = pango_layout_get_baseline(layout) / PANGO_SCALE;
212 need_new_extent_cache_ = false;
213@@ -671,22 +679,8 @@
214 if (!texture)
215 return;
216
217- nux::Size tex_size(parent_->GetMaximumWidth(), parent_->GetMaximumHeight());
218- nux::Size layout_size(tex_size.width * PANGO_SCALE, tex_size.height * PANGO_SCALE);
219-
220- if (tex_size.width == nux::AREA_MAX_WIDTH)
221- {
222- tex_size.width = parent_->GetWidth();
223- layout_size.width = -1;
224- }
225-
226- if (tex_size.height == nux::AREA_MAX_HEIGHT)
227- {
228- tex_size.height = parent_->GetHeight();
229- layout_size.height = -1;
230- }
231-
232- texture->cr.reset(new CairoGraphics(CAIRO_FORMAT_ARGB32, tex_size.width, tex_size.height));
233+ nux::Size layout_size(-1, lines_ < 0 ? lines_ : std::numeric_limits<int>::min());
234+ texture->cr.reset(new CairoGraphics(CAIRO_FORMAT_ARGB32, cached_extent_.width, cached_extent_.height));
235 cairo_t* cr = texture->cr->GetInternalContext();
236
237 PangoLayout* layout = NULL;
238@@ -702,8 +696,9 @@
239 cairo_set_font_options(cr, gdk_screen_get_font_options(screen));
240
241 layout = pango_cairo_create_layout(cr);
242+
243+
244 desc = pango_font_description_from_string(font.c_str());
245-
246 pango_layout_set_font_description(layout, desc);
247 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
248 pango_layout_set_ellipsize(layout, GetPangoEllipsizeMode());
249@@ -713,8 +708,6 @@
250 pango_layout_set_height(layout, layout_size.height);
251 pango_layout_set_spacing(layout, line_spacing_ * PANGO_SCALE);
252
253- pango_layout_set_height(layout, lines_);
254-
255 SetAttributes(layout);
256
257 pangoCtx = pango_layout_get_context(layout); // is not ref'ed
258@@ -732,14 +725,22 @@
259 (float) dpi / (float) PANGO_SCALE);
260 }
261
262+ Size result;
263+ pango_layout_context_changed(layout);
264+ pango_layout_get_pixel_size(layout, &result.width, &result.height);
265+
266+ if (result.width > parent_->GetMaximumWidth())
267+ {
268+ pango_layout_set_width(layout, parent_->GetMaximumWidth() * PANGO_SCALE);
269+ pango_layout_context_changed(layout);
270+ }
271+
272 cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
273 cairo_paint(cr);
274
275 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
276 cairo_set_source_rgba(cr, text_color_.red, text_color_.green, text_color_.blue, text_color_.alpha);
277
278- pango_layout_context_changed(layout);
279-
280 cairo_move_to(cr, 0.0f, 0.0f);
281 pango_cairo_show_layout(cr, layout);
282
283@@ -750,10 +751,15 @@
284 g_object_unref(layout);
285 }
286
287+void StaticCairoText::Impl::UpdateBaseSize()
288+{
289+ parent_->SetBaseSize(cached_extent_.width, cached_extent_.height);
290+}
291+
292 void StaticCairoText::Impl::UpdateTexture()
293 {
294- auto const& size = GetTextExtents();
295- parent_->SetBaseSize(size.width, size.height);
296+ GetTextExtents();
297+ UpdateBaseSize();
298
299 textures2D_.clear();
300 for (auto const& texture : cache_textures_)