MNX Document Model
Loading...
Searching...
No Matches
FormattedText.h
1/*
2 * Copyright (C) 2025, Robert Patterson
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22#pragma once
23#include <string>
24#include <string_view>
25#include <vector>
26
27#include "BaseTypes.h"
28#include "ContentArray.h"
29#include "Enumerations.h"
30
31namespace mnx {
32
43namespace text {
44
49class TextContentObject : public ContentObject<TextContentObject>
50{
51public:
52 using ContentObject::ContentObject;
53 std::string_view defaultType() const override { return "text"; }
54};
55
70
75class Text : public TextContentObject
76{
77public:
79 struct Required
80 {
81 std::string text;
82 };
83
85 Text(const std::shared_ptr<json>& root, json_pointer pointer)
87 {
88 }
89
94 Text(Base& parent, std::string_view key, const std::string& text)
96 {
97 set_text(text);
98 }
99
101 operator Required() const { return { text() }; }
102
104 static Required make(const std::string& text) { return { text }; }
105
106 MNX_REQUIRED_PROPERTY(std::string, text);
108
109 inline static constexpr std::string_view ContentTypeValue = "text";
110};
111
117{
118public:
120 struct Required
121 {
122 std::vector<std::string> glyphs;
123 };
124
126 Smufl(const std::shared_ptr<json>& root, json_pointer pointer)
128 {
129 }
130
135 Smufl(Base& parent, std::string_view key, const std::vector<std::string>& glyphs)
137 {
138 create_glyphs().assign(glyphs);
139 }
140
142 operator Required() const { return { glyphs().toStdVector() }; }
143
145 static Required make(const std::vector<std::string>& glyphs) { return { glyphs }; }
146
147 using TextContentObject::TextContentObject;
148
151
152 inline static constexpr std::string_view ContentTypeValue = "smufl";
153};
154
155} // namespace text
156
161class FormattedText : public ContentArray<text::TextContentObject>
162{
163public:
164 using ContentArray<text::TextContentObject>::ContentArray;
165
166 text::Smufl appendSmufl(const std::vector<std::string>& glyphs)
167 { return appendWithType<text::Smufl>(glyphs); }
168
169 text::Text appendText(const std::string& text)
170 { return appendWithType<text::Text>(text); }
171};
172
173} // namespace mnx
Represents an MNX array, encapsulating property access.
Definition BaseTypes.h:531
Base class wrapper for all MNX JSON nodes.
Definition BaseTypes.h:212
json_pointer pointer() const
Returns the json_pointer for this node.
Definition BaseTypes.h:274
T parent() const
Returns the parent object for this node.
Definition BaseTypes.h:260
const std::shared_ptr< json > & root() const
Returns the root.
Definition BaseTypes.h:297
Class for content arrays.
Definition ContentArray.h:49
Base class for objects that are elements of content arrays.
Definition BaseTypes.h:742
Container for formatted-text content objects.
Definition FormattedText.h:162
Represents an MNX object, encapsulating property access.
Definition BaseTypes.h:429
Object()
Constructor fresh document or detached instance.
Definition BaseTypes.h:432
Represents a formatted text fragment expressed as SMuFL glyphs.
Definition FormattedText.h:117
Smufl(const std::shared_ptr< json > &root, json_pointer pointer)
Constructor for existing Smufl objects.
Definition FormattedText.h:126
static constexpr std::string_view ContentTypeValue
type value that identifies the type within the content array
Definition FormattedText.h:152
MNX_REQUIRED_CHILD(Array< std::string >, glyphs)
the smufl glyphs in this chunk
Smufl(Base &parent, std::string_view key, const std::vector< std::string > &glyphs)
Creates a new Smufl class as a child of a JSON element.
Definition FormattedText.h:135
static Required make(const std::vector< std::string > &glyphs)
Create a Required instance for Smufl.
Definition FormattedText.h:145
MNX_OPTIONAL_CHILD(Style, style)
the style for the glyphs in thie chunk
Describes styling for formatted text content.
Definition FormattedText.h:61
MNX_OPTIONAL_PROPERTY_WITH_DEFAULT(FontWeight, weight, FontWeight::Plain)
the font weight
MNX_OPTIONAL_PROPERTY(std::string, font)
the name of the font
MNX_OPTIONAL_PROPERTY(double, size)
the font size
MNX_OPTIONAL_PROPERTY_WITH_DEFAULT(FontStyle, fontStyle, FontStyle::Plain)
the font style
Base class for formatted-text content objects.
Definition FormattedText.h:50
Represents a formatted text fragment.
Definition FormattedText.h:76
static Required make(const std::string &text)
Create a Required instance for Text.
Definition FormattedText.h:104
MNX_OPTIONAL_CHILD(Style, style)
the style for this formatted chunk
MNX_REQUIRED_PROPERTY(std::string, text)
the text for this formatted chunk
Text(Base &parent, std::string_view key, const std::string &text)
Creates a new Text class as a child of a JSON element.
Definition FormattedText.h:94
Text(const std::shared_ptr< json > &root, json_pointer pointer)
Constructor for existing Text objects.
Definition FormattedText.h:85
static constexpr std::string_view ContentTypeValue
type value that identifies the type within the content array
Definition FormattedText.h:109
object model for MNX format
json::json_pointer json_pointer
JSON pointer class for MNX.
Definition BaseTypes.h:68
FontWeight
Specifies the weight of text glyphs.
Definition Enumerations.h:181
@ Plain
plain (non-bold) text
FontStyle
Specifies the style of text glyphs.
Definition Enumerations.h:171
@ Plain
plain (non-italic) text
initializer class for Smufl
Definition FormattedText.h:121
std::vector< std::string > glyphs
the smufl glyphs in this chunk
Definition FormattedText.h:122
initializer class for Text
Definition FormattedText.h:80
std::string text
the text to be represented
Definition FormattedText.h:81