Changeset View
Changeset View
Standalone View
Standalone View
source/blender/nodes/geometry/nodes/node_geo_string_to_curves.cc
| /* | /* | ||||
| * This program is free software; you can redistribute it and/or | * This program is free software; you can redistribute it and/or | ||||
| * modify it under the terms of the GNU General Public License | * modify it under the terms of the GNU General Public License | ||||
| * as published by the Free Software Foundation; either version 2 | * as published by the Free Software Foundation; either version 2 | ||||
| * of the License, or (at your option) any later version. | * of the License, or (at your option) any later version. | ||||
| * | * | ||||
| * This program is distributed in the hope that it will be useful, | * This program is distributed in the hope that it will be useful, | ||||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
| * GNU General Public License for more details. | * GNU General Public License for more details. | ||||
| * | * | ||||
| * You should have received a copy of the GNU General Public License | * You should have received a copy of the GNU General Public License | ||||
| * along with this program; if not, write to the Free Software Foundation, | * along with this program; if not, write to the Free Software Foundation, | ||||
| * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
| */ | */ | ||||
| #include <codecvt> | |||||
| #include <locale> | |||||
| #include "DNA_curve_types.h" | #include "DNA_curve_types.h" | ||||
| #include "DNA_vfont_types.h" | #include "DNA_vfont_types.h" | ||||
| #include "BKE_curve.h" | #include "BKE_curve.h" | ||||
| #include "BKE_font.h" | #include "BKE_font.h" | ||||
| #include "BKE_spline.hh" | #include "BKE_spline.hh" | ||||
| #include "BLI_hash.h" | #include "BLI_hash.h" | ||||
| ▲ Show 20 Lines • Show All 174 Lines • ▼ Show 20 Lines | static TextLayout get_text_layout(GeoNodeExecParams ¶ms) | ||||
| MEM_SAFE_FREE(cu.tb); | MEM_SAFE_FREE(cu.tb); | ||||
| return layout; | return layout; | ||||
| } | } | ||||
| /* Returns a mapping of UTF-32 character code to instance handle. */ | /* Returns a mapping of UTF-32 character code to instance handle. */ | ||||
| static Map<int, int> create_curve_instances(GeoNodeExecParams ¶ms, | static Map<int, int> create_curve_instances(GeoNodeExecParams ¶ms, | ||||
| const float fontsize, | const float fontsize, | ||||
| const std::u32string &charcodes, | const Span<char32_t> charcodes, | ||||
| InstancesComponent &instance_component) | InstancesComponent &instance_component) | ||||
| { | { | ||||
| VFont *vfont = (VFont *)params.node().id; | VFont *vfont = (VFont *)params.node().id; | ||||
| Map<int, int> handles; | Map<int, int> handles; | ||||
| for (int i : IndexRange(charcodes.length())) { | for (int i : charcodes.index_range()) { | ||||
| if (handles.contains(charcodes[i])) { | if (handles.contains(charcodes[i])) { | ||||
| continue; | continue; | ||||
| } | } | ||||
| Curve cu = {nullptr}; | Curve cu = {nullptr}; | ||||
| cu.type = OB_FONT; | cu.type = OB_FONT; | ||||
| cu.resolu = 12; | cu.resolu = 12; | ||||
| cu.vfont = vfont; | cu.vfont = vfont; | ||||
| CharInfo charinfo = {0}; | CharInfo charinfo = {0}; | ||||
| Show All 9 Lines | for (int i : charcodes.index_range()) { | ||||
| GeometrySet geometry_set_curve = GeometrySet::create_with_curve(curve_eval.release()); | GeometrySet geometry_set_curve = GeometrySet::create_with_curve(curve_eval.release()); | ||||
| handles.add_new(charcodes[i], instance_component.add_reference(std::move(geometry_set_curve))); | handles.add_new(charcodes[i], instance_component.add_reference(std::move(geometry_set_curve))); | ||||
| } | } | ||||
| return handles; | return handles; | ||||
| } | } | ||||
| static void add_instances_from_handles(InstancesComponent &instances, | static void add_instances_from_handles(InstancesComponent &instances, | ||||
| const Map<int, int> &char_handles, | const Map<int, int> &char_handles, | ||||
| const std::u32string &charcodes, | const Span<char32_t> charcodes, | ||||
| const Span<float2> positions) | const Span<float2> positions) | ||||
| { | { | ||||
| instances.resize(positions.size()); | instances.resize(positions.size()); | ||||
| MutableSpan<int> handles = instances.instance_reference_handles(); | MutableSpan<int> handles = instances.instance_reference_handles(); | ||||
| MutableSpan<float4x4> transforms = instances.instance_transforms(); | MutableSpan<float4x4> transforms = instances.instance_transforms(); | ||||
| MutableSpan<int> instance_ids = instances.instance_ids(); | MutableSpan<int> instance_ids = instances.instance_ids(); | ||||
| threading::parallel_for(IndexRange(positions.size()), 256, [&](IndexRange range) { | threading::parallel_for(IndexRange(positions.size()), 256, [&](IndexRange range) { | ||||
| Show All 16 Lines | static void geo_node_string_to_curves_exec(GeoNodeExecParams params) | ||||
| } | } | ||||
| if (layout.positions.size() == 0) { | if (layout.positions.size() == 0) { | ||||
| params.set_output("Curves", GeometrySet()); | params.set_output("Curves", GeometrySet()); | ||||
| return; | return; | ||||
| } | } | ||||
| /* Convert UTF-8 encoded string to UTF-32. */ | /* Convert UTF-8 encoded string to UTF-32. */ | ||||
| std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> converter; | size_t len_bytes; | ||||
| std::u32string utf32_text = converter.from_bytes(layout.text); | size_t len_chars = BLI_strlen_utf8_ex(layout.text.c_str(), &len_bytes); | ||||
| Array<char32_t> char_codes(len_chars + 1); | |||||
| BLI_str_utf8_as_utf32(char_codes.data(), layout.text.c_str(), len_chars + 1); | |||||
| /* Create and add instances. */ | /* Create and add instances. */ | ||||
| GeometrySet geometry_set_out; | GeometrySet geometry_set_out; | ||||
| InstancesComponent &instances = geometry_set_out.get_component_for_write<InstancesComponent>(); | InstancesComponent &instances = geometry_set_out.get_component_for_write<InstancesComponent>(); | ||||
| Map<int, int> char_handles = create_curve_instances( | Map<int, int> char_handles = create_curve_instances( | ||||
| params, layout.final_font_size, utf32_text, instances); | params, layout.final_font_size, char_codes, instances); | ||||
| add_instances_from_handles(instances, char_handles, utf32_text, layout.positions); | add_instances_from_handles(instances, char_handles, char_codes, layout.positions); | ||||
| params.set_output("Curves", std::move(geometry_set_out)); | params.set_output("Curves", std::move(geometry_set_out)); | ||||
| } | } | ||||
| } // namespace blender::nodes | } // namespace blender::nodes | ||||
| void register_node_type_geo_string_to_curves() | void register_node_type_geo_string_to_curves() | ||||
| { | { | ||||
| Show All 16 Lines | |||||