From 66b94ee7d39083bc8c624756b0639f0470efe39b Mon Sep 17 00:00:00 2001 From: yggverse Date: Thu, 1 Aug 2024 15:56:27 +0300 Subject: [PATCH] add new tags word wrap support --- src/Abstract/Model/Gtk/Pango/Markup.php | 47 +++++++++++++++++++----- src/Interface/Model/Gtk/Pango/Markup.php | 3 ++ src/Model/Gtk/Pango/Markup/Gemtext.php | 3 +- 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/Abstract/Model/Gtk/Pango/Markup.php b/src/Abstract/Model/Gtk/Pango/Markup.php index a95607ae..87bb718f 100644 --- a/src/Abstract/Model/Gtk/Pango/Markup.php +++ b/src/Abstract/Model/Gtk/Pango/Markup.php @@ -82,24 +82,33 @@ class Markup implements \Yggverse\Yoda\Interface\Model\Gtk\Pango\Markup ): string { return sprintf( - '* %s', // @TODO + '%s', // @TODO self::_wrap( htmlspecialchars( - $value + sprintf( + '* %s', + $value + ) ), + self::TAG_LIST, $width ) ); } public static function quote( - string $value + string $value, + int $width = self::WRAP_WIDTH ): string { return sprintf( '%s', - htmlspecialchars( - $value + self::_wrap( + htmlspecialchars( + $value + ), + self::TAG_QUOTE, + $width ) ); } @@ -113,6 +122,7 @@ class Markup implements \Yggverse\Yoda\Interface\Model\Gtk\Pango\Markup htmlspecialchars( $value ), + self::TAG_TEXT, $width ); } @@ -131,8 +141,12 @@ class Markup implements \Yggverse\Yoda\Interface\Model\Gtk\Pango\Markup bool $close ): string { - if (in_array($const, [self::TAG_CODE])) - { + if (in_array($const, [ + self::TAG_CODE, + self::TAG_LIST, + self::TAG_QUOTE, + self::TAG_TEXT + ])) { return sprintf( $close ? '' : '<%s>', $const @@ -145,6 +159,7 @@ class Markup implements \Yggverse\Yoda\Interface\Model\Gtk\Pango\Markup // @TODO optimization wanted, wordwrap / set_line_wrap not solution protected static function _wrap( string $string, + string $tag, int $width, string $break = PHP_EOL, int $line = 1, @@ -163,9 +178,21 @@ class Markup implements \Yggverse\Yoda\Interface\Model\Gtk\Pango\Markup if (isset($words[$line])) { $label->set_markup( - implode( - ' ' , $words[$line] - ) . ' ' . $word + sprintf( + '%s%s%s', + self::tag( + $tag, + false + ), + implode( + ' ' , $words[$line] + ) . ' ' . $word, + self::tag( + $tag, + true + ) + ) + ); if ($label->get_layout()->get_pixel_size()['width'] > $width) diff --git a/src/Interface/Model/Gtk/Pango/Markup.php b/src/Interface/Model/Gtk/Pango/Markup.php index 56a50b8e..aa26f406 100644 --- a/src/Interface/Model/Gtk/Pango/Markup.php +++ b/src/Interface/Model/Gtk/Pango/Markup.php @@ -12,6 +12,9 @@ interface Markup { public const ENCODING = 'UTF-8'; public const TAG_CODE = 'tt'; + public const TAG_QUOTE = 'i'; + public const TAG_TEXT = 'span'; + public const TAG_LIST = 'span'; public const WRAP_WIDTH = 320; public static function code( diff --git a/src/Model/Gtk/Pango/Markup/Gemtext.php b/src/Model/Gtk/Pango/Markup/Gemtext.php index a3611c6b..eb3d2228 100644 --- a/src/Model/Gtk/Pango/Markup/Gemtext.php +++ b/src/Model/Gtk/Pango/Markup/Gemtext.php @@ -157,7 +157,8 @@ class Gemtext extends \Yggverse\Yoda\Abstract\Model\Gtk\Pango\Markup else { $line[] = self::quote( - $entity->getText() + $entity->getText(), + $width ); }