Friday, October 29, 2021

Converting a Pango Markup Span Tag to a List of Text Widget Tag Options

Its always been a trouble task sharing formatted text between the gnocl::text widget and other display widgets such as the gnocl::label, gnocl::tree and gnocl::list widgets. This diffculty arises from not being able to define text attributes as pango markup, this is understandable given that fact that there is not a smooth overlap between the widget and pango attributes.

What was needed was a way of bridging the two requirments, being able to take a markup <span> tag and converting to an -opt val list.

Parsing markup strings using the pango api is not terribly helpful, it is complex and only works on complete strings, "<span .... ...>" would simply through up a pango formatting error.

The solution has been much simpler by using a series of string mapping operations in the source module with the resul being the creation of a new gnocl::pango subcommand "markup2options". For instance, if: 

set markup "<span font='Serif italic bold 24' foreground='green' background='red'>"

then calling

gnocl::pango markup2options $markup

returns

-font "Serif italic bold 24" -foreground "green" -background "red"

Using this a working script would produce something along the lines of:

$txt tag create test {*}[gnocl::pango markup2options $markup]
$txt insert end $sometext -tags test


No comments: