Blender V5.3
node_composite_string_to_image.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include <optional>
6
7#include "DNA_vfont_types.h"
8
9#include "BKE_vfont.hh"
10
11#include "UI_resources.hh"
12
13#include "COM_node_operation.hh"
14
16
18
21 "LEFT",
22 ICON_ALIGN_LEFT,
23 N_("Left"),
24 N_("Align text to the left")},
26 "CENTER",
27 ICON_ALIGN_CENTER,
28 N_("Center"),
29 N_("Align text to the center")},
31 "RIGHT",
32 ICON_ALIGN_RIGHT,
33 N_("Right"),
34 N_("Align text to the right")},
35 {0, nullptr, 0, nullptr, nullptr},
36};
37
40 "TOP",
41 ICON_ALIGN_TOP,
42 N_("Top"),
43 N_("Align text to the top")},
45 "TOP_BASELINE",
46 ICON_ALIGN_TOP,
47 N_("Top Baseline"),
48 N_("Align text to the top line's baseline")},
50 "MIDDLE",
51 ICON_ALIGN_MIDDLE,
52 N_("Middle"),
53 N_("Align text to the middle")},
55 "BOTTOM_BASELINE",
56 ICON_ALIGN_BOTTOM,
57 N_("Bottom Baseline"),
58 N_("Align text to the bottom line's baseline")},
60 "BOTTOM",
61 ICON_ALIGN_BOTTOM,
62 N_("Bottom"),
63 N_("Align text to the bottom")},
64 {0, nullptr, 0, nullptr, nullptr},
65};
66
68{
69 b.use_custom_socket_order();
70 b.allow_any_socket_order();
71
72 b.add_output<decl::Float>("Image"_ustr)
73 .structure_type(StructureType::Dynamic)
74 .description("The image containing the paragraph of text");
75
76 b.add_input<decl::String>("String"_ustr).optional_label();
77 b.add_input<decl::Font>("Font"_ustr)
78 .default_value_fn(
79 [](const bNode & /*node*/) { return id_cast<ID *>(BKE_vfont_builtin_ensure()); })
80 .optional_label();
81 b.add_input<decl::Float>("Size"_ustr)
82 .default_value(128.0f)
84 .min(0.0f)
85 .description("The height of each line in pixels");
86
87 {
88 PanelDeclarationBuilder &panel = b.add_panel("Alignment"_ustr).default_closed(true);
89 panel.add_input<decl::Menu>("Horizontal Alignment"_ustr)
92 .optional_label();
93 panel.add_input<decl::Menu>("Vertical Alignment"_ustr)
96 .optional_label();
97 }
98
99 {
100 PanelDeclarationBuilder &panel = b.add_panel("Wrap"_ustr).default_closed(true);
101 panel.add_input<decl::Bool>("Wrap"_ustr)
102 .default_value(true)
103 .panel_toggle()
104 .description("Wrap text into new lines if it exceeds the specified width");
105 panel.add_input<decl::Int>("Width"_ustr, "Wrap Width"_ustr)
106 .default_value(1920)
107 .min(0)
109 .description(
110 "The maximum width of each line in pixels. Lines with larger widths will be wrapped "
111 "into new lines");
112 }
113}
114
115using namespace blender::compositor;
116
118 public:
120
121 void execute() override
122 {
123 const std::string string = this->get_input("String").get_single_value_default<std::string>();
124 const VFont *font = this->get_input("Font").get_single_value_default<VFont *>();
125 const float size = this->get_input("Size").get_single_value_default<float>();
126
127 const Result &string_image = this->context().cache_manager().string_images.get(
128 this->context(),
129 string,
130 font,
131 size,
134 this->get_wrap_width());
135 if (!string_image.is_allocated()) {
137 return;
138 }
139
140 Result &output = this->get_result("Image");
141 output.share_data(string_image);
142 }
143
145 {
146 const auto horizontal_alignment = CMPNodeStringToImageHorizontalAlignment(
147 this->get_input("Horizontal Alignment").get_single_value_default<MenuValue>().value);
148 switch (horizontal_alignment) {
155 }
156
159 }
160
181
182 std::optional<int> get_wrap_width()
183 {
184 const bool use_wrap = this->get_input("Wrap").get_single_value_default<bool>();
185 if (!use_wrap) {
186 return std::nullopt;
187 }
188
189 return math::max(0, this->get_input("Wrap Width").get_single_value_default<int>());
190 }
191};
192
194{
195 return new StringToImageOperation(context, node);
196}
197
198static void node_register()
199{
200 static bke::bNodeType ntype;
201
202 cmp_node_type_base(&ntype, "CompositorNodeStringToImage"_ustr);
203 ntype.ui_name = "String To Image";
204 ntype.ui_description = "Generates an image containing the given paragraph of text";
205 ntype.nclass = NODE_CLASS_INPUT;
206 ntype.declare = node_declare;
208
210}
212
213} // namespace blender::nodes::node_composite_string_to_image_cc
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define BLI_assert_unreachable()
#define output
NodeOperation(Context &context, const bNode &node)
Result & get_result(StringRef identifier)
Definition operation.cc:40
Context & context() const
Definition operation.cc:57
Result & get_input(StringRef identifier) const
Definition operation.cc:35
Result & get(Context &context, const std::string string, const VFont *font, const float size, const HorizontalAlignment horizontal_alignment, const VerticalAlignment vertical_alignment, const std::optional< int > wrap_width)
DeclType::Builder & add_input(UString name, UString identifier=""_ustr)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
T max(const T &a, const T &b)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static const EnumPropertyItem rna_node_compositor_string_to_image_vertical_alignment_items[]
static const EnumPropertyItem rna_node_compositor_string_to_image_horizontal_alignment_items[]
VFont * BKE_vfont_builtin_ensure()
Definition vfont.cc:387
CMPNodeStringToImageHorizontalAlignment
@ CMP_NODE_STRING_TO_IMAGE_HORIZONTAL_ALIGNMENT_LEFT
@ CMP_NODE_STRING_TO_IMAGE_HORIZONTAL_ALIGNMENT_RIGHT
@ CMP_NODE_STRING_TO_IMAGE_HORIZONTAL_ALIGNMENT_CENTER
CMPNodeStringToImageVerticalAlignment
@ CMP_NODE_STRING_TO_IMAGE_VERTICAL_ALIGNMENT_BOTTOM_BASELINE
@ CMP_NODE_STRING_TO_IMAGE_VERTICAL_ALIGNMENT_TOP_BASELINE
@ CMP_NODE_STRING_TO_IMAGE_VERTICAL_ALIGNMENT_BOTTOM
@ CMP_NODE_STRING_TO_IMAGE_VERTICAL_ALIGNMENT_TOP
@ CMP_NODE_STRING_TO_IMAGE_VERTICAL_ALIGNMENT_MIDDLE
void cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:379
NodeDeclareFunction declare
Definition BKE_node.hh:393
#define N_(msgid)