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
=== modified file 'unity-shared/StaticCairoText.cpp'
--- unity-shared/StaticCairoText.cpp 2013-01-16 15:43:38 +0000
+++ unity-shared/StaticCairoText.cpp 2013-03-09 17:35:24 +0000
@@ -70,7 +70,7 @@
70 Size GetTextExtents() const;70 Size GetTextExtents() const;
7171
72 void SetAttributes(PangoLayout* layout);72 void SetAttributes(PangoLayout* layout);
73 void DrawText(CacheTexture::Ptr cached_texture, int width, int height, int line_spacing, Color const& color);73 void DrawText(CacheTexture::Ptr const& cached_texture);
7474
75 void UpdateTexture();75 void UpdateTexture();
76 void OnFontChanged();76 void OnFontChanged();
@@ -576,7 +576,7 @@
576 if (inkRect.x + inkRect.width > logRect.x + logRect.width)576 if (inkRect.x + inkRect.width > logRect.x + logRect.width)
577 result.width = std::ceil(static_cast<float>(inkRect.x + inkRect.width - logRect.x) / PANGO_SCALE);577 result.width = std::ceil(static_cast<float>(inkRect.x + inkRect.width - logRect.x) / PANGO_SCALE);
578 else578 else
579 result.width = std::ceil(static_cast<float>(logRect.width) / PANGO_SCALE);579 result.width = std::ceil(static_cast<float>(logRect.width) / PANGO_SCALE);
580580
581 result.height = std::ceil(static_cast<float>(logRect.height) / PANGO_SCALE);581 result.height = std::ceil(static_cast<float>(logRect.height) / PANGO_SCALE);
582 cached_extent_ = result;582 cached_extent_ = result;
@@ -666,16 +666,28 @@
666 pango_layout_set_attributes(layout, attr_list);666 pango_layout_set_attributes(layout, attr_list);
667}667}
668668
669void StaticCairoText::Impl::DrawText(CacheTexture::Ptr cached_texture,669void StaticCairoText::Impl::DrawText(CacheTexture::Ptr const& texture)
670 int width,
671 int height,
672 int line_spacing,
673 Color const& color)
674{670{
675 if (!cached_texture)671 if (!texture)
676 return;672 return;
677 cached_texture->cr.reset(new CairoGraphics(CAIRO_FORMAT_ARGB32, width, height));673
678 cairo_t* cr = cached_texture->cr->GetInternalContext();674 nux::Size tex_size(parent_->GetMaximumWidth(), parent_->GetMaximumHeight());
675 nux::Size layout_size(tex_size.width * PANGO_SCALE, tex_size.height * PANGO_SCALE);
676
677 if (tex_size.width == nux::AREA_MAX_WIDTH)
678 {
679 tex_size.width = parent_->GetWidth();
680 layout_size.width = -1;
681 }
682
683 if (tex_size.height == nux::AREA_MAX_HEIGHT)
684 {
685 tex_size.height = parent_->GetHeight();
686 layout_size.height = -1;
687 }
688
689 texture->cr.reset(new CairoGraphics(CAIRO_FORMAT_ARGB32, tex_size.width, tex_size.height));
690 cairo_t* cr = texture->cr->GetInternalContext();
679691
680 PangoLayout* layout = NULL;692 PangoLayout* layout = NULL;
681 PangoFontDescription* desc = NULL;693 PangoFontDescription* desc = NULL;
@@ -684,7 +696,7 @@
684 GdkScreen* screen = gdk_screen_get_default(); // not ref'ed696 GdkScreen* screen = gdk_screen_get_default(); // not ref'ed
685 GtkSettings* settings = gtk_settings_get_default(); // not ref'ed697 GtkSettings* settings = gtk_settings_get_default(); // not ref'ed
686698
687 std::string text = text_.substr(cached_texture->start_index, cached_texture->length);699 std::string text = text_.substr(texture->start_index, texture->length);
688700
689 std::string font(GetEffectiveFont());701 std::string font(GetEffectiveFont());
690 cairo_set_font_options(cr, gdk_screen_get_font_options(screen));702 cairo_set_font_options(cr, gdk_screen_get_font_options(screen));
@@ -697,9 +709,9 @@
697 pango_layout_set_ellipsize(layout, GetPangoEllipsizeMode());709 pango_layout_set_ellipsize(layout, GetPangoEllipsizeMode());
698 pango_layout_set_alignment(layout, GetPangoAlignment());710 pango_layout_set_alignment(layout, GetPangoAlignment());
699 pango_layout_set_markup(layout, text.c_str(), -1);711 pango_layout_set_markup(layout, text.c_str(), -1);
700 pango_layout_set_width(layout, width * PANGO_SCALE);712 pango_layout_set_width(layout, layout_size.width);
701 pango_layout_set_height(layout, height * PANGO_SCALE);713 pango_layout_set_height(layout, layout_size.height);
702 pango_layout_set_spacing(layout, line_spacing * PANGO_SCALE);714 pango_layout_set_spacing(layout, line_spacing_ * PANGO_SCALE);
703715
704 pango_layout_set_height(layout, lines_);716 pango_layout_set_height(layout, lines_);
705717
@@ -724,7 +736,7 @@
724 cairo_paint(cr);736 cairo_paint(cr);
725737
726 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);738 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
727 cairo_set_source_rgba(cr, color.red, color.green, color.blue, color.alpha);739 cairo_set_source_rgba(cr, text_color_.red, text_color_.green, text_color_.blue, text_color_.alpha);
728740
729 pango_layout_context_changed(layout);741 pango_layout_context_changed(layout);
730742
@@ -740,19 +752,15 @@
740752
741void StaticCairoText::Impl::UpdateTexture()753void StaticCairoText::Impl::UpdateTexture()
742{754{
743 Size size = GetTextExtents();755 auto const& size = GetTextExtents();
744 parent_->SetBaseSize(size.width, size.height);756 parent_->SetBaseSize(size.width, size.height);
745 nux::Geometry const& geo = parent_->GetGeometry();
746757
747 textures2D_.clear();758 textures2D_.clear();
748 auto iter = cache_textures_.begin();759 for (auto const& texture : cache_textures_)
749 for (; iter != cache_textures_.end(); ++iter)
750 {760 {
751 CacheTexture::Ptr const& cached_texture = *iter;761 DrawText(texture);
752 DrawText(cached_texture,
753 geo.width, cached_texture->height, line_spacing_, text_color_);
754762
755 textures2D_.push_back(texture_ptr_from_cairo_graphics(*cached_texture->cr));763 textures2D_.push_back(texture_ptr_from_cairo_graphics(*texture->cr));
756 }764 }
757}765}
758766