Text.hpp

Functionality similar to String, but tracks text formatting for easy conversion.

Text should be functionally interchangable with string, but can easily convert to HTML, Latex, RTF, or other formats that support bold, italic, super/sub-scripting, fonts, etc.

The main Text class tracks a string of text (called simply “text”) and any special styles associated with each text position (in “style_map”). The current TextEncoding class helps guide the conversion from one text encoding to another.

Internally, styles that all encodings should be able to handle (or at least be aware of) are: BASIC FORMATS : “bold” “code” “italic” “strike” “subscript” “superscript” “underline” “no_break” “color:NAME” “font:NAME” “size:POINT_SIZE”

STRUCTURAL : “heading:1” through “heading:6” different levels of headings. “blockquote” “bullet:1” through “bullet:6” (only on character after bulleted; stops an newline) “indent:1” through “indent:6” (same as bullet, but no bullet) “ordered:1” through “ordered:6” (numbers; reset less-tabbed structure is used) “link:URL” (create a link to the specified URL) “image:URL” (include in the specified image)

Note

Status: ALPHA

struct TextEncoding_Interface
#include <Text.hpp>

Subclassed by TextEncoding, TextEncoding_None

Public Functions

inline virtual ~TextEncoding_Interface()
virtual String GetName() const = 0
virtual void Append(Text&, const String&) = 0
virtual String Encode(const Text&) const = 0
virtual Ptr<TextEncoding_Interface> Clone() const = 0
virtual void PrintDebug(std::ostream&) const = 0
class TextEncoding_None : public TextEncoding_Interface
#include <Text.hpp>

Public Functions

inline TextEncoding_None()
inline virtual String GetName() const override
virtual void Append(Text &text, const String &in) override
virtual String Encode(const Text &text) const override
virtual Ptr<TextEncoding_Interface> Clone() const override
virtual void PrintDebug(std::ostream &os) const override
class Text
#include <Text.hpp>

A Text widget handles putting text on a web page that can be controlled and modified.

Public Functions

inline Text()
inline Text(const Text &in)
template<typename ...Ts>
inline Text(Ts&&... in)
inline ~Text()
inline Text &operator=(const Text &in)
inline Text &operator=(Text &&in)
template<typename T>
inline Text &operator=(const T &in)
inline size_t GetSize() const
inline const String &GetText() const
inline const BitVector &GetStyle(const String &style) const
inline operator const String&() const

Automatic conversion back to an unformatted string.

inline bool HasEncoding(const String &name) const

Test if this Text object is aware of how to use a specified encoding.

Parameters:

name – Name of the encoding to test for.

Returns:

A true/false indicate if the named encoding is known.

inline String GetEncodingName() const

Get the name of the current encoding being applied.

Returns:

Name of the current encoding.

inline Text &SetEncoding(const String &name)

Change the current encoding being used to another known encoding type.

Parameters:

name – Name of the encoding type to be used.

Returns:

A reference to this object itself.

template<typename ENCODING_T, typename ...EXTRA_Ts>
inline String AddEncoding(EXTRA_Ts&&... args)

Add an encoding to this Text object; new encodings automatically become active.

Add a new encoding to this Text object. Newly added encodings automatically become active (use SetEncoding() to choose a different encoding option). Will give a warning if encoding already exists, and then replace it.

Template Parameters:
  • ENCODING_T – The type of the new encoding to use

  • ...EXTRA_Ts – Automatically set by variadic arguments.

Parameters:

...args – Any extra arguments to configure this new encoding (passed to constructor)

Returns:

Name of the encoding that was created.

inline void RemoveEncoding(const String &name)

Remove an encoding with a provided name.

Parameters:

name – Name of the encoding to remove.

template<typename ENCODING_T, typename ...EXTRA_Ts>
inline String ActivateEncoding(const String &name, EXTRA_Ts&&... args)

Set an encoding as active, creating it if needed.

Template Parameters:
  • ENCODING_T – The type of the new encoding to use

  • ...EXTRA_Ts – Automatically set by variadic arguments.

Parameters:
  • name – Name of the encoding that we want activated / added

  • ...args – Any extra arguments to configure this new encoding (passed to constructor)

Returns:

Name of the encoding that was created.

template<typename T, typename ...EXTRA_Ts>
inline Text &Append(T &&in, EXTRA_Ts&&... in_extra)

Append potentially-formatted text through the current encoding.

template<typename ENCODING_T, typename IN_T>
inline Text &AppendAs(const String &encode_name, IN_T &&in)

Specify the encoding of a value being appended.

template<typename T>
inline Text &Append_Raw(T &&in)
template<typename T>
inline Text &operator<<(T &&in)
template<typename T>
inline Text &operator+=(T &&in)
inline String Encode() const

Convert text to a string using the current encoding.

Returns:

The resulting string.

inline void Resize(size_t new_size)
inline char GetChar(size_t pos) const
inline Text &Set(size_t pos, char in)
template<bool IS_CONST>
inline Text &Set(size_t pos, TextCharRef<IS_CONST> in)
inline TextCharRef<false> operator[](size_t pos)
inline TextCharRef<true> operator[](size_t pos) const
inline size_t size() const
inline void resize(size_t new_size)
template<typename ...Ts>
inline Text &assign(Ts&&... in)
inline TextCharRef<false> front()
inline TextCharRef<true> front() const
inline TextCharRef<false> back()
inline TextCharRef<true> back() const
inline bool empty() const
template<typename ...Ts>
inline bool starts_with(Ts&&... in) const
template<typename ...Ts>
inline bool ends_with(Ts&&... in) const
template<typename ...Ts>
inline size_t find(Ts&&... in) const
template<typename ...Ts>
inline size_t rfind(Ts&&... in) const
template<typename ...Ts>
inline size_t find_first_of(Ts&&... in) const
template<typename ...Ts>
inline size_t find_first_not_of(Ts&&... in) const
template<typename ...Ts>
inline size_t find_last_of(Ts&&... in) const
template<typename ...Ts>
inline size_t find_last_not_of(Ts&&... in) const
inline Text &SetStyle(String style)
inline Text &Bold()
inline Text &Code()
inline Text &Italic()
inline Text &Strike()
inline Text &Subscript()
inline Text &Superscript()
inline Text &Underline()
inline Text &SetStyle(String style, size_t pos)
inline Text &Bold(size_t pos)
inline Text &Code(size_t pos)
inline Text &Italic(size_t pos)
inline Text &Strike(size_t pos)
inline Text &Subscript(size_t pos)
inline Text &Superscript(size_t pos)
inline Text &Underline(size_t pos)
inline Text &SetStyle(String style, size_t start, size_t end)
inline Text &Bold(size_t start, size_t end)
inline Text &Code(size_t start, size_t end)
inline Text &Italic(size_t start, size_t end)
inline Text &Strike(size_t start, size_t end)
inline Text &Subscript(size_t start, size_t end)
inline Text &Superscript(size_t start, size_t end)
inline Text &Underline(size_t start, size_t end)
inline vector<String> GetStyles(size_t pos = MAX_SIZE_T) const

Return the set of active styles in this text.

Parameters:

pos – optional position to specify only styles used at position.

inline bool HasStyle(const String &style) const
inline bool HasBold() const
inline bool HasCode() const
inline bool HasItalic() const
inline bool HasStrike() const
inline bool HasSubscript() const
inline bool HasSuperscript() const
inline bool HasUnderline() const
inline bool HasStyle(const String &style, size_t pos) const
inline bool HasBold(size_t pos) const
inline bool HasCode(size_t pos) const
inline bool HasItalic(size_t pos) const
inline bool HasStrike(size_t pos) const
inline bool HasSubscript(size_t pos) const
inline bool HasSuperscript(size_t pos) const
inline bool HasUnderline(size_t pos) const
inline Text &Clear()
inline Text &Clear(size_t pos)
inline Text &Clear(const String &style)
inline Text &ClearBold()
inline Text &ClearCode()
inline Text &ClearItalic()
inline Text &ClearStrike()
inline Text &ClearSubscript()
inline Text &ClearSuperscript()
inline Text &ClearUnderline()
inline Text &Clear(const String &style, size_t pos)
inline Text &ClearBold(size_t pos)
inline Text &ClearCode(size_t pos)
inline Text &ClearItalic(size_t pos)
inline Text &ClearStrike(size_t pos)
inline Text &ClearSubscript(size_t pos)
inline Text &ClearSuperscript(size_t pos)
inline Text &ClearUnderline(size_t pos)
inline Text &Clear(const String &style, size_t start, size_t end)
inline Text &ClearBold(size_t start, size_t end)
inline Text &ClearCode(size_t start, size_t end)
inline Text &ClearItalic(size_t start, size_t end)
inline Text &ClearStrike(size_t start, size_t end)
inline Text &ClearSubscript(size_t start, size_t end)
inline Text &ClearSuperscript(size_t start, size_t end)
inline Text &ClearUnderline(size_t start, size_t end)
inline String ToDebugString()
inline void PrintDebug(std::ostream &os = std::cout)

Protected Types

using encoding_ptr_t = Ptr<TextEncoding_Interface>

Protected Functions

inline void Cleanup()
inline void CloneEncodings(const Text &in)

Protected Attributes

String text = ""
std::unordered_map<String, BitVector> style_map
encoding_ptr_t encoding_ptr = nullptr
std::map<String, encoding_ptr_t> encodings