Skip to content

Commit

Permalink
pango_text::set_layout(): Receive the layout as a function parameter
Browse files Browse the repository at this point in the history
This allows the function to be used when the same canvas uses multiple
layouts.

The function also receives the text as a string view now.
  • Loading branch information
jyrkive committed May 5, 2017
1 parent afcca42 commit a3a0bb8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
26 changes: 13 additions & 13 deletions src/font/text.cpp
Expand Up @@ -157,7 +157,7 @@ gui2::point pango_text::get_cursor_position(

// First we need to determine the byte offset, if more routines need it it
// would be a good idea to make it a separate function.
std::unique_ptr<PangoLayoutIter, std::function<void(PangoLayoutIter*)>> itor(
std::unique_ptr<PangoLayoutIter, void(*)(PangoLayoutIter*)> itor(
pango_layout_get_iter(layout_.get()), pango_layout_iter_free);

// Go the wanted line.
Expand Down Expand Up @@ -279,7 +279,7 @@ gui2::point pango_text::get_column_line(const gui2::point& position) const
bool pango_text::set_text(const std::string& text, const bool markedup)
{
if(markedup != markedup_text_ || text != text_) {
// assert(layout_);
assert(layout_ != nullptr);

const ucs4::string wide = unicode_cast<ucs4::string>(text);
const std::string narrow = unicode_cast<utf8::string>(wide);
Expand All @@ -289,7 +289,7 @@ bool pango_text::set_text(const std::string& text, const bool markedup)
<< "' contains invalid utf-8, trimmed the invalid parts.\n";
}
if(markedup) {
if(!this->set_markup(narrow)) {
if(!this->set_markup(narrow, *layout_)) {
return false;
}
} else {
Expand Down Expand Up @@ -693,8 +693,8 @@ void pango_text::create_surface_buffer(const size_t size) const
for (auto & c : surface_buffer_) { c = 0; }
}

bool pango_text::set_markup(const std::string & text) {
return this->set_markup_helper(link_aware_ ? this->format_link_tokens(text) : text);
bool pango_text::set_markup(utils::string_view text, PangoLayout& layout) {
return this->set_markup_helper(link_aware_ ? this->format_link_tokens(text.to_str()) : text, layout);
}

std::string pango_text::format_link_tokens(const std::string & text) const {
Expand Down Expand Up @@ -728,13 +728,13 @@ std::string pango_text::handle_token(const std::string & token) const
}
}

bool pango_text::set_markup_helper(const std::string& text)
bool pango_text::set_markup_helper(utils::string_view text, PangoLayout& layout)
{
if(pango_parse_markup(text.c_str(), text.size()
, 0, nullptr, nullptr, nullptr, nullptr)) {
if(pango_parse_markup(text.str, text.size,
0, nullptr, nullptr, nullptr, nullptr)) {

/* Markup is valid so set it. */
pango_layout_set_markup(layout_.get(), text.c_str(), text.size());
pango_layout_set_markup(&layout, text.str, text.size);
return true;
}

Expand All @@ -746,14 +746,14 @@ bool pango_text::set_markup_helper(const std::string& text)
* So only try to recover from broken ampersands, by simply replacing them
* with the escaped version.
*/
std::string semi_escaped{semi_escape_text(text)};
std::string semi_escaped{semi_escape_text(text.to_str())};

/*
* If at least one ampersand is replaced the semi-escaped string
* is longer than the original. If this isn't the case then the
* markup wasn't (only) broken by ampersands in the first place.
*/
if(text.size() == semi_escaped.size()
if(text.size == semi_escaped.size()
|| !pango_parse_markup(semi_escaped.c_str(), semi_escaped.size()
, 0, nullptr, nullptr, nullptr, nullptr)) {

Expand All @@ -762,7 +762,7 @@ bool pango_text::set_markup_helper(const std::string& text)
<< " text '" << text
<< "' has broken markup, set to normal text.\n";

this->set_text(_("The text contains invalid markup: ") + text, false);
this->set_text(_("The text contains invalid markup: ") + text.to_str(), false);
return false;
}

Expand All @@ -771,7 +771,7 @@ bool pango_text::set_markup_helper(const std::string& text)
<< " text '" << text
<< "' has unescaped ampersands '&', escaped them.\n";

pango_layout_set_markup(layout_.get(), semi_escaped.c_str(), semi_escaped.size());
pango_layout_set_markup(&layout, semi_escaped.c_str(), semi_escaped.size());
return true;
}

Expand Down
9 changes: 5 additions & 4 deletions src/font/text.hpp
Expand Up @@ -18,6 +18,7 @@
#include "font/font_options.hpp"
#include "color.hpp"
#include "sdl/surface.hpp"
#include "serialization/string_utils.hpp"
#include "serialization/unicode_types.hpp"

#include <pango/pango.h>
Expand Down Expand Up @@ -247,8 +248,8 @@ class pango_text
private:

/***** ***** ***** ***** Pango variables ***** ***** ***** *****/
std::unique_ptr<PangoContext, std::function<void(void*)>> context_;
std::unique_ptr<PangoLayout, std::function<void(void*)>> layout_;
std::unique_ptr<PangoContext, void(*)(void*)> context_;
std::unique_ptr<PangoLayout, void(*)(void*)> layout_;
mutable PangoRectangle rect_;

/** The SDL surface to render upon used as a cache. */
Expand Down Expand Up @@ -399,9 +400,9 @@ class pango_text
* unrecoverable error occurred and the text is
* set as plain text with an error message.
*/
bool set_markup(const std::string& text);
bool set_markup(utils::string_view text, PangoLayout& layout);

bool set_markup_helper(const std::string & text);
bool set_markup_helper(utils::string_view text, PangoLayout& layout);

std::string format_link_tokens(const std::string & text) const;

Expand Down
12 changes: 12 additions & 0 deletions src/serialization/string_utils.hpp
Expand Up @@ -20,6 +20,7 @@

#include <algorithm>
#include <map>
#include <ostream>
#include <set>
#include <sstream>
#include <string>
Expand Down Expand Up @@ -54,10 +55,21 @@ class string_view

string_view(const string_view&) = default;

friend std::ostream& operator<<(std::ostream& stream, const string_view& str)
{
stream.write(str.str, str.size);
return stream;
}

explicit operator std::string() const
{
return std::string(str, size);
}

std::string to_str() const
{
return std::string(str, size);
}
};

bool isnewline(const char c);
Expand Down

0 comments on commit a3a0bb8

Please sign in to comment.