Blender V5.3
grease_pencil_io_export_pdf.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_bounds.hh"
6
8#include "BKE_scene.hh"
9
10#include "BLI_math_color_c.hh"
11
13
15#include "DNA_scene_types.h"
16
17#include "BKE_curves.hh"
19
20#include "grease_pencil_io.hh"
22
23#include "hpdf.h"
24
25#include <iostream>
26
30
32
34 public:
36
37 HPDF_Doc pdf_;
38 HPDF_Page page_;
39
40 bool export_scene(Scene &scene, StringRefNull filepath);
41 void export_grease_pencil_objects(int frame_number);
42 void export_grease_pencil_layer(const Object &object,
44 const bke::greasepencil::Drawing &drawing);
45
46 bool create_document();
47 bool add_page(Scene &scene);
48
49 void write_path(const float4x4 &transform,
50 Span<float3> positions,
51 const OffsetIndices<int> points_by_curve,
52 const Span<int> shape,
53 const VArray<bool> &cyclic,
54 const VArray<int8_t> &types,
56 const float opacity,
57 std::optional<float> width,
58 std::optional<float> miter_limit_angle);
59 bool write_to_file(StringRefNull filepath);
60};
61
63{
64 bool result = false;
66
67 if (!create_document()) {
68 return false;
69 }
70
71 switch (params_.frame_mode) {
73 const int frame_number = scene.r.cfra;
74
75 this->prepare_render_params(scene, frame_number);
76 this->add_page(scene);
77 this->export_grease_pencil_objects(frame_number);
78 result = this->write_to_file(filepath);
79 break;
80 }
83 const bool only_selected = (params_.frame_mode == ExportParams::FrameMode::Selected);
84 if (only_selected && (!ob_eval || ob_eval->type != OB_GREASE_PENCIL)) {
85 /* For exporting "Selected Frames", the active object is required to be a grease pencil
86 * object, from which we will read selected frames from. */
87 break;
88 }
89 const int orig_frame = scene.r.cfra;
90 for (int frame_number = scene.r.sfra; frame_number <= scene.r.efra; frame_number++) {
91 if (only_selected) {
92 if (!ob_eval) {
93 break;
94 }
95 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(ob_eval->data);
96 if (!this->is_selected_frame(grease_pencil, frame_number)) {
97 continue;
98 }
99 }
100
101 scene.r.cfra = frame_number;
103
104 this->prepare_render_params(scene, frame_number);
105 this->add_page(scene);
106 this->export_grease_pencil_objects(frame_number);
107 }
108
109 result = this->write_to_file(filepath);
110
111 /* Back to original frame. */
112 scene.r.cfra = orig_frame;
115 break;
116 }
117 default:
118 break;
119 }
120
121 return result;
122}
123
125{
127
129
130 for (const ObjectInfo &info : objects) {
131 const Object *ob = info.object;
132
133 /* Use evaluated version to get strokes with modifiers. */
134 const Object *ob_eval = DEG_get_evaluated(context_.depsgraph, ob);
135 BLI_assert(ob_eval->type == OB_GREASE_PENCIL);
136 const GreasePencil *grease_pencil_eval = id_cast<const GreasePencil *>(ob_eval->data);
137
138 for (const bke::greasepencil::Layer *layer : grease_pencil_eval->layers()) {
139 if (!layer->is_visible()) {
140 continue;
141 }
142 const Drawing *drawing = grease_pencil_eval->get_drawing_at(*layer, frame_number);
143 if (drawing == nullptr) {
144 continue;
145 }
146
147 const bke::CurvesGeometry &curves = drawing->strokes();
148 if (curves.has_curve_with_type(
149 {CURVE_TYPE_CATMULL_ROM, CURVE_TYPE_BEZIER, CURVE_TYPE_NURBS}))
150 {
151 IndexMaskMemory memory;
152 const IndexMask non_poly_selection = curves.indices_for_curve_type(CURVE_TYPE_POLY, memory)
153 .complement(curves.curves_range(), memory);
154 Drawing export_drawing;
156 non_poly_selection);
157 export_drawing.tag_topology_changed();
158 export_grease_pencil_layer(*ob_eval, *layer, export_drawing);
159 }
160 else {
161 export_grease_pencil_layer(*ob_eval, *layer, *drawing);
162 }
163 }
164 }
165}
166
169 const bke::greasepencil::Drawing &drawing)
170{
172
173 const float4x4 layer_to_world = layer.to_world_space(object);
174
175 auto write_shape = [&](const Span<float3> positions,
176 const std::optional<Span<float3>> /*positions_left*/,
177 const std::optional<Span<float3>> /*positions_right*/,
178 const OffsetIndices<int> points_by_curve,
179 const Span<int> shape,
180 const VArray<bool> &cyclic,
181 const VArray<int8_t> &types,
182 const ColorGeometry4f &color,
183 const float opacity,
184 const std::optional<float> width,
185 const std::optional<float> miter_limit_angle,
186 const bool /*round_cap*/,
187 const bool /*is_outline*/) {
188 write_path(layer_to_world,
189 positions,
190 points_by_curve,
191 shape,
192 cyclic,
193 types,
194 color,
195 opacity,
196 width,
197 miter_limit_angle);
198 };
199
200 foreach_shape_in_layer(object, layer, drawing, write_shape);
201}
202
204{
205 auto hpdf_error_handler = [](HPDF_STATUS error_no, HPDF_STATUS detail_no, void * /*user_data*/) {
206 printf("ERROR: error_no=%04X, detail_no=%u\n", (HPDF_UINT)error_no, (HPDF_UINT)detail_no);
207 };
208
209 pdf_ = HPDF_New(hpdf_error_handler, nullptr);
210 if (!pdf_) {
211 std::cout << "error: cannot create PdfDoc object\n";
212 return false;
213 }
214 return true;
215}
216
217constexpr double meter_to_inches_factor = 1000.0 / 25.4;
218constexpr double default_pdf_ppi = 72.0;
219
221{
222 page_ = HPDF_AddPage(pdf_);
223 if (!page_) {
224 std::cout << "error: cannot create PdfPage\n";
225 return false;
226 }
227
228 /* Pixels per meter. */
229 double2 ppm;
230 BKE_scene_ppm_get(&scene.r, ppm);
231
232 /* Covert pixels per meter to pixels per inch. */
234
235 double2 scale_factor = default_pdf_ppi / ppi;
236 HPDF_Page_Concat(page_, scale_factor.x, 0.0f, 0.0f, scale_factor.y, 0.0f, 0.0f);
237
238 if (camera_persmat_) {
239 HPDF_Page_SetWidth(page_, camera_rect_.size().x * scale_factor.x);
240 HPDF_Page_SetHeight(page_, camera_rect_.size().y * scale_factor.y);
241 }
242 else {
243 HPDF_Page_SetWidth(page_, screen_rect_.size().x * scale_factor.x);
244 HPDF_Page_SetHeight(page_, screen_rect_.size().y * scale_factor.y);
245 }
246
247 return true;
248}
249
251 const Span<float3> positions,
252 const OffsetIndices<int> points_by_curve,
253 const Span<int> shape,
254 const VArray<bool> &cyclic,
255 const VArray<int8_t> & /*types*/,
256 const ColorGeometry4f &color,
257 const float opacity,
258 std::optional<float> width,
259 std::optional<float> miter_limit_angle)
260{
261 if (miter_limit_angle) {
262 if (*miter_limit_angle <= GP_STROKE_MITER_ANGLE_ROUND) {
263 HPDF_Page_SetLineJoin(page_, HPDF_ROUND_JOIN);
264 }
265 else if (*miter_limit_angle >= GP_STROKE_MITER_ANGLE_BEVEL) {
266 HPDF_Page_SetLineJoin(page_, HPDF_BEVEL_JOIN);
267 }
268 else {
269 /* Convert the Miter angle to the Miter limit. */
270 const float miter_limit = 1.0f / math::sin(*miter_limit_angle / 2.0f);
271
272 HPDF_Page_SetLineJoin(page_, HPDF_MITER_JOIN);
273 HPDF_Page_SetMiterLimit(page_, miter_limit);
274 }
275 }
276
277 if (width) {
278 HPDF_Page_SetLineWidth(page_, std::max(*width, 1.0f));
279 }
280
281 const float total_opacity = color.a * opacity;
282
283 HPDF_Page_GSave(page_);
284 HPDF_ExtGState gstate = (total_opacity < 1.0f) ? HPDF_CreateExtGState(pdf_) : nullptr;
285
286 ColorGeometry4f srgb;
288 if (width) {
289 HPDF_Page_SetRGBFill(page_, srgb.r, srgb.g, srgb.b);
290 HPDF_Page_SetRGBStroke(page_, srgb.r, srgb.g, srgb.b);
291 if (gstate) {
292 HPDF_ExtGState_SetAlphaFill(gstate, std::clamp(total_opacity, 0.0f, 1.0f));
293 HPDF_ExtGState_SetAlphaStroke(gstate, std::clamp(total_opacity, 0.0f, 1.0f));
294 }
295 }
296 else {
297 HPDF_Page_SetRGBFill(page_, srgb.r, srgb.g, srgb.b);
298 if (gstate) {
299 HPDF_ExtGState_SetAlphaFill(gstate, std::clamp(total_opacity, 0.0f, 1.0f));
300 }
301 }
302 if (gstate) {
303 HPDF_Page_SetExtGState(page_, gstate);
304 }
305
306 for (const int curve_i : shape) {
307 const IndexRange points = points_by_curve[curve_i];
308 const Span<float3> curve_pos = positions.slice(points);
309
310 for (const int i : curve_pos.index_range()) {
311 const float2 screen_co = this->project_to_screen(transform, curve_pos[i]);
312 if (i == 0) {
313 HPDF_Page_MoveTo(page_, screen_co.x, screen_co.y);
314 }
315 else {
316 HPDF_Page_LineTo(page_, screen_co.x, screen_co.y);
317 }
318 }
319 if (cyclic[curve_i]) {
320 HPDF_Page_ClosePath(page_);
321 }
322 }
323
324 if (width) {
325 HPDF_Page_Stroke(page_);
326 }
327 else {
328 HPDF_Page_Fill(page_);
329 }
330
331 HPDF_Page_GRestore(page_);
332}
333
335{
336 /* Support unicode character paths on Windows. */
337 HPDF_STATUS result = 0;
338
339 /* TODO: It looks `libharu` does not support unicode. */
340#if 0 /* `ifdef WIN32` */
341 wchar_t *filepath_16 = alloc_utf16_from_8(filepath.c_str(), 0);
342 std::wstring wstr(filepath_16);
343 result = HPDF_SaveToFile(pdf_, wstr.c_str());
344 free(filepath_16);
345#else
346 result = HPDF_SaveToFile(pdf_, filepath.c_str());
347#endif
348
349 return (result == 0) ? true : false;
350}
351
352bool export_pdf(const IOContext &context,
353 const ExportParams &params,
354 Scene &scene,
355 StringRefNull filepath)
356{
357 PDFExporter exporter(context, params);
358 return exporter.export_scene(scene, filepath);
359}
360
361} // namespace blender::io::grease_pencil
Low-level operations for curves.
Low-level operations for grease pencil.
#define GP_STROKE_MITER_ANGLE_BEVEL
#define GP_STROKE_MITER_ANGLE_ROUND
ChannelStorageType r
ChannelStorageType g
ChannelStorageType b
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr const char * c_str() const
bke::CurvesGeometry & strokes_for_write()
const bke::CurvesGeometry & strokes() const
GreasePencilExporter(const IOContext &context, const ExportParams &params)
void foreach_shape_in_layer(const Object &object, const bke::greasepencil::Layer &layer, const bke::greasepencil::Drawing &drawing, WriteShapeFn shape_fn)
void prepare_render_params(Scene &scene, int frame_number)
bool is_selected_frame(const GreasePencil &grease_pencil, int frame_number) const
float2 project_to_screen(const float4x4 &transform, const float3 &position) const
GreasePencilExporter(const IOContext &context, const ExportParams &params)
void write_path(const float4x4 &transform, Span< float3 > positions, const OffsetIndices< int > points_by_curve, const Span< int > shape, const VArray< bool > &cyclic, const VArray< int8_t > &types, const ColorGeometry4f &color, const float opacity, std::optional< float > width, std::optional< float > miter_limit_angle)
void export_grease_pencil_layer(const Object &object, const bke::greasepencil::Layer &layer, const bke::greasepencil::Drawing &drawing)
bool export_scene(Scene &scene, StringRefNull filepath)
#define layer
void free(Subdiv *subdiv)
Definition subdiv.cc:192
CurvesGeometry resample_to_evaluated(const CurvesGeometry &src_curves, const IndexMask &selection, const ResampleCurvesOutputAttributeIDs &output_ids={})
bool export_pdf(const IOContext &context, const ExportParams &params, Scene &scene, StringRefNull filepath)
T sin(const AngleRadianBase< T > &a)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_scene_graph_update_for_newframe(Depsgraph *depsgraph)
Definition scene.cc:2953
void linearrgb_to_srgb_v3_v3(float srgb[3], const float linear[3])
VecBase< double, 2 > double2
MatBase< float, 4, 4 > float4x4
void BKE_scene_ppm_get(const RenderData *rd, double r_ppm[2])
Definition scene.cc:3487
VecBase< float, 2 > float2
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
bool BKE_scene_camera_switch_update(Scene *scene)
Definition scene.cc:2480
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
struct RenderData r
wchar_t * alloc_utf16_from_8(const char *in8, size_t add)
Definition utfconv.cc:294