34#include <fmt/format.h>
55static std::string
get_layer_id(
const NSVGshape &shape,
const int prefix)
57 return (shape.id_parent[0] ==
'\0') ? fmt::format(
"Layer_{:03d}", prefix) :
58 fmt::format(
"{:s}", shape.id_parent);
67 uint8_t(((pack) >> 24) & 0xFF)};
69 float(rgb_u[1]) / 255.0f,
70 float(rgb_u[2]) / 255.0f,
71 float(rgb_u[3]) / 255.0f};
91 avg_color /= stops.
size();
102 switch (NSVGpaintType(svg_paint.type)) {
103 case NSVG_PAINT_UNDEF:
105 case NSVG_PAINT_NONE:
107 case NSVG_PAINT_COLOR:
109 case NSVG_PAINT_LINEAR_GRADIENT:
111 case NSVG_PAINT_RADIAL_GRADIENT:
124 const int old_curves_num =
curves.curves_num();
125 const int old_points_num =
curves.points_num();
130 for (NSVGpath *path = shape.paths; path; path = path->next) {
131 if (path->npts == 0) {
134 BLI_assert(path->npts >= 1 && path->npts ==
int(path->npts / 3) * 3 + 1);
137 int point_num = (path->npts + 2) / 3;
144 float2 pos_last = svg_path_data[(point_num - 1) * 3];
148 pos_last = svg_path_data[(point_num - 1) * 3];
151 new_curve_offsets.
append(point_num);
156 new_curve_offsets.
append(0);
158 new_curve_offsets, old_points_num);
160 const IndexRange new_curves_range = {old_curves_num, new_points_by_curve.
size()};
162 const int points_num = new_points_by_curve.
total_size() + old_points_num;
165 if (old_curves_num > 0) {
169 .slice(old_curves_num, new_curve_offsets.
size())
170 .copy_from(new_curve_offsets);
172 curves.resize(points_num, curves_num);
173 curves.offsets_for_write().copy_from(new_offsets);
175 curves.tag_topology_changed();
177 return new_curves_range;
181 const NSVGshape &shape,
184 const int shape_index,
185 const int material_index)
193 curves.update_curve_types();
218 materials.
span.slice(curves_range).fill(material_index);
221 fill_colors.
span.slice(curves_range).fill(shape_color);
223 if (fill_opacities) {
224 fill_opacities.span.slice(curves_range).fill(shape_color.
a);
227 const bool use_stroke = bool(shape.stroke.type);
228 const bool use_fill = bool(shape.fill.type);
241 hide_stroke.
span.slice(curves_range).fill(!use_stroke);
245 fill_ids.span.slice(curves_range).fill(use_fill ? shape_index + 1 : 0);
249 int curve_index = curves_range.
start();
250 for (NSVGpath *path = shape.paths; path; path = path->next) {
251 if (path->npts == 0) {
254 const IndexRange points = points_by_curve[curve_index];
259 const bool closed = bool(path->closed) || (points.
size() * 3 - 2 != path->npts);
260 cyclic[curve_index] = closed;
269 const float2 pos_handle_left = closed ? svg_path_data[points.
size() * 3 - 1] : pos_center;
271 const float2 pos_handle_right = svg_path_data[1];
274 float3(pos_handle_left, 0.0f));
280 radii.
span[points.
first()] = shape.strokeWidth * path_width_scale;
283 vertex_colors.span[points.
first()] = point_color;
285 if (point_opacities) {
286 point_opacities.span[points.
first()] = point_color.a;
291 const int point_index = points[i];
292 const float2 pos_center = svg_path_data[i * 3];
293 float2 pos_handle_left = svg_path_data[i * 3 - 1];
294 const float2 pos_handle_right = (i < points.
size() - 1 + closed) ? svg_path_data[i * 3 + 1] :
298 float3(pos_handle_left, 0.0f));
300 float3(pos_handle_right, 0.0f));
304 radii.
span[point_index] = shape.strokeWidth * path_width_scale;
307 vertex_colors.span[point_index] = point_color;
309 if (point_opacities) {
310 point_opacities.span[point_index] = point_color.a;
319 fill_opacities.finish();
321 vertex_colors.finish();
322 point_opacities.finish();
323 curves.tag_positions_changed();
324 curves.tag_radii_changed();
329 const std::optional<Bounds<float3>>
bounds = [&]() {
330 std::optional<Bounds<float3>>
bounds;
358 constexpr const char *svg_units =
"mm";
359 constexpr float svg_dpi = 96.0f;
365 NSVGimage *svg_data =
nullptr;
366 svg_data = nsvgParseFromFile(abs_filepath, svg_units, svg_dpi);
367 if (svg_data ==
nullptr) {
378 nsvgDelete(svg_data);
384 std::optional<int> mat_index_stroke;
385 std::optional<int> mat_index_fill;
386 std::optional<int> mat_index_both;
393 const float svg_scale = 0.001f / scene_unit_scale *
params_.
scale;
399 bool has_color_gradient =
false;
402 std::string prv_id =
"*";
405 for (NSVGshape *shape = svg_data->shapes; shape; shape = shape->next) {
407 if (prv_id != layer_id) {
416 if (layer_node && layer_node->
is_layer()) {
427 if (drawing ==
nullptr) {
435 const bool is_fill = bool(shape->fill.type);
436 const bool is_stroke = bool(shape->stroke.type) || !is_fill;
438 if (is_stroke && is_fill) {
439 if (!mat_index_both) {
443 material_index = *mat_index_both;
445 else if (is_stroke) {
446 if (!mat_index_stroke) {
450 material_index = *mat_index_stroke;
453 if (!mat_index_fill) {
457 material_index = *mat_index_fill;
460 if (
ELEM(shape->fill.type, NSVG_PAINT_LINEAR_GRADIENT, NSVG_PAINT_RADIAL_GRADIENT)) {
461 has_color_gradient =
true;
471 curves, *shape, new_curves_range,
transform, shape_index, material_index);
478 nsvgDelete(svg_data);
485 if (has_color_gradient) {
488 "SVG has gradients, Grease Pencil color will be approximated");
497 return importer.
read(filepath);
Low-level operations for curves.
Low-level operations for grease pencil.
#define BLI_assert_unreachable()
MutableSpan< T > as_mutable_span()
constexpr int64_t first() const
constexpr int64_t one_after_last() const
constexpr int64_t size() const
constexpr bool is_empty() const
constexpr int64_t start() const
constexpr IndexRange index_range() const
constexpr IndexRange drop_front(int64_t n) const
Span< NewT > constexpr cast() const
constexpr Span drop_back(int64_t n) const
constexpr const T & first() const
constexpr int64_t size() const
constexpr IndexRange index_range() const
constexpr bool is_empty() const
constexpr const char * c_str() const
void append(const T &value)
std::optional< Bounds< float3 > > bounds_min_max(bool use_radius=true) const
void translate(const float3 &translation)
bool add(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
GSpanAttributeWriter lookup_for_write_span(StringRef name)
bke::CurvesGeometry & strokes_for_write()
const bke::CurvesGeometry & strokes() const
void tag_positions_changed()
const Layer & as_layer() const
GreasePencilImporter(const IOContext &context, const ImportParams ¶ms)
Object * create_object(StringRefNull name)
const ImportParams params_
int32_t create_material(StringRefNull name)
GreasePencilImporter(const IOContext &context, const ImportParams ¶ms)
bool read(StringRefNull filepath)
Bounds< T > merge(const Bounds< T > &a, const Bounds< T > &b)
std::string filename(const std::string_view &str, size_t pos)
BLI_INLINE Texture * wrap(MTLTexture *texture)
static void shift_to_bounds_center(GreasePencil &grease_pencil)
static ColorGeometry4f average_gradient_color(const NSVGgradient &svg_gradient)
bool import_svg(const IOContext &context, const ImportParams ¶ms, StringRefNull filepath)
static ColorGeometry4f unpack_nano_color(const uint pack)
static ColorGeometry4f convert_svg_color(const NSVGpaint &svg_paint)
static IndexRange extend_curves_geometry(bke::CurvesGeometry &curves, const NSVGshape &shape)
static std::string get_layer_id(const NSVGshape &shape, const int prefix)
static void shape_attributes_to_curves(bke::CurvesGeometry &curves, const NSVGshape &shape, const IndexRange curves_range, const float4x4 &transform, const int shape_index, const int material_index)
MatBase< T, NumCol, NumRow > scale(const MatBase< T, NumCol, NumRow > &mat, const VectorT &scale)
EulerXYZBase< float > EulerXYZ
T average(const VecBase< T, Size > &a)
VecBase< T, 3 > to_scale(const MatBase< T, NumCol, NumRow > &mat)
bool almost_equal_relative(const VecBase< T, Size > &a, const VecBase< T, Size > &b, const T &epsilon_factor)
MatT from_rotation(const RotationT &rotation)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
OffsetIndices< int > accumulate_counts_to_offsets(MutableSpan< int > counts_to_offsets, int start_offset=0)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ GP_LAYER_TREE_NODE_USE_LIGHTS
VecBase< uint8_t, 4 > uchar4
char * STRNCPY(char(&dst)[N], const char *src)
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
MINLINE void srgb_to_linearrgb_v4(float linear[4], const float srgb[4])
const char * BKE_main_blendfile_path_from_global()
MatBase< float, 4, 4 > float4x4
VecBase< float, 4 > float4
VecBase< float, 2 > float2
void BKE_report(ReportList *reports, eReportType type, const char *message)
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
void void void BLI_path_split_file_part(const char *filepath, char *file, size_t file_maxncpy) ATTR_NONNULL(1
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
VecBase< float, 3 > float3
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
MutableVArraySpan< T > span