From c853ffb5b2f75f5a889ed2e3ef89b818a736e87a Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 13 Apr 2024 13:50:49 +0200 Subject: Adding upstream version 1.3+ds. Signed-off-by: Daniel Baumann --- src/ui/syntax.h | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 src/ui/syntax.h (limited to 'src/ui/syntax.h') diff --git a/src/ui/syntax.h b/src/ui/syntax.h new file mode 100644 index 0000000..528ad93 --- /dev/null +++ b/src/ui/syntax.h @@ -0,0 +1,134 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** @file Syntax coloring via Gtksourceview and Pango markup. + */ +/* Authors: + * Rafael Siejakowski + * + * Copyright (C) 2022 Authors + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#ifndef SEEN_UI_UI_SYNTAX_H +#define SEEN_UI_UI_SYNTAX_H + +#include +#include +#include +#include +#include +#include + +#include "color.h" + +namespace Inkscape::UI::Syntax { + +/** The style of a single element in a (Pango markup)-enabled widget. */ +struct Style +{ + std::optional color; + std::optional background; + uint8_t bold : 1; + uint8_t italic : 1; + uint8_t underline : 1; + + Style() + : bold{false} + , italic{false} + , underline{false} + {} + + bool isDefault() const { return !color && !background && !bold && !italic && !underline; } + Glib::ustring openingTag() const; + Glib::ustring closingTag() const; +}; + +/** The styles used for simple XML syntax highlighting. */ +struct XMLStyles +{ + Style prolog; + Style comment; + Style angular_brackets; + Style tag_name; + Style attribute_name; + Style attribute_value; + Style content; + Style error; +}; + +/** @brief A formatter for XML syntax, based on Pango markup. + * + * This mechanism is used in the TreeView in the XML Dialog, + * where the syntax highlighting of XML tags is accomplished + * via Pango markup. + */ +class XMLFormatter +{ +public: + XMLFormatter() = default; + XMLFormatter(XMLStyles &&styles) + : _style{styles} + {} + + void setStyle(XMLStyles const &new_style) { _style = new_style; } + void setStyle(XMLStyles &&new_style) { _style = new_style; } + + void openTag(char const *tag_name); + void addAttribute(char const *attribute_name, char const *attribute_value); + Glib::ustring finishTag(bool self_close = false); + + Glib::ustring formatContent(char const* content, bool wrap_in_quotes = true) const; + Glib::ustring formatComment(char const* comment, bool wrap_in_comment_marks = true) const; + Glib::ustring formatProlog(char const* prolog) const { return _format(_style.prolog, prolog); } + +private: + Glib::ustring _format(Style const &style, Glib::ustring const &content) const; + Glib::ustring _format(Style const &style, char const *content) const + { + return style.openingTag() + Glib::Markup::escape_text(content) + style.closingTag(); + } + + XMLStyles _style; + Glib::ustring _wip; +}; + +/// Build XML styles from a GTKSourceView syntax color theme. +XMLStyles build_xml_styles(const Glib::ustring& syntax_theme); + +/// Syntax highlighting mode (language). +enum class SyntaxMode +{ + PlainText, ///< Plain text (no highlighting). + InlineCss, ///< Inline CSS (contents of a style="..." attribute). + CssStyle, ///< File-scope CSS (contents of a CSS file or a