rmagick 7.2.0 → 7.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,6 +11,7 @@
11
11
 
12
12
  #include "rmagick.h"
13
13
  #include "float.h"
14
+ #include "ruby/ractor.h"
14
15
 
15
16
  static void Draw_compact(void *drawptr);
16
17
  static void Draw_mark(void *);
@@ -19,6 +20,12 @@ static size_t Draw_memsize(const void *);
19
20
  static VALUE new_DrawOptions(void);
20
21
  static VALUE get_type_metrics(int, VALUE *, VALUE, gvl_function_t);
21
22
 
23
+ #if defined(HAVE_RB_RACTOR_LOCAL_STORAGE_VALUE_NEWKEY)
24
+ static rb_ractor_local_key_t dummy_img_key;
25
+ #else
26
+ static VALUE dummy_img = Qnil;
27
+ #endif
28
+
22
29
  const rb_data_type_t rm_draw_data_type = {
23
30
  "Magick::Draw",
24
31
  {
@@ -28,7 +35,7 @@ const rb_data_type_t rm_draw_data_type = {
28
35
  Draw_compact,
29
36
  },
30
37
  0, 0,
31
- RUBY_TYPED_FROZEN_SHAREABLE,
38
+ RUBY_TYPED_FROZEN_SHAREABLE | RM_TYPED_FREE_IMMEDIATELY,
32
39
  };
33
40
 
34
41
 
@@ -48,7 +55,7 @@ DEFINE_GVL_STUB3(GetTypeMetrics, Image *, const DrawInfo *, TypeMetric *);
48
55
 
49
56
 
50
57
  /**
51
- * Fetch the Draw struct and ensure it has been initialized (draw->info != NULL).
58
+ * Fetch the MagickDraw struct and ensure it has been initialized (draw->info != NULL).
52
59
  *
53
60
  * Draw_alloc() leaves draw->info NULL until #initialize runs, so a Draw obtained
54
61
  * via .allocate (or a subclass whose #initialize skips super) would otherwise
@@ -57,15 +64,15 @@ DEFINE_GVL_STUB3(GetTypeMetrics, Image *, const DrawInfo *, TypeMetric *);
57
64
  * No Ruby usage (internal function)
58
65
  *
59
66
  * @param self the Draw object
60
- * @return the initialized Draw struct
67
+ * @return the initialized MagickDraw struct
61
68
  * @throw RuntimeError if the Draw object has not been initialized
62
69
  */
63
- static Draw *
70
+ static MagickDraw *
64
71
  get_draw(VALUE self)
65
72
  {
66
- Draw *draw;
73
+ MagickDraw *draw;
67
74
 
68
- TypedData_Get_Struct(self, Draw, &rm_draw_data_type, draw);
75
+ TypedData_Get_Struct(self, MagickDraw, &rm_draw_data_type, draw);
69
76
  if (!draw->info)
70
77
  {
71
78
  rb_raise(rb_eRuntimeError, "%s has not been initialized", rb_obj_classname(self));
@@ -75,23 +82,23 @@ get_draw(VALUE self)
75
82
 
76
83
 
77
84
  /**
78
- * Get the Draw struct of a Draw object for a method that changes it.
85
+ * Get the MagickDraw struct of a Draw object for a method that changes it.
79
86
  *
80
87
  * No Ruby usage (internal function)
81
88
  *
82
89
  * @param self the Draw object
83
- * @return the initialized Draw struct
90
+ * @return the initialized MagickDraw struct
84
91
  * @throw FrozenError if the Draw object is frozen
85
- * @throw RuntimeError if an offloaded call is using the Draw object
92
+ * @throw RuntimeError if a call in flight is using the Draw object
86
93
  */
87
- static Draw *
94
+ static MagickDraw *
88
95
  get_writable_draw(VALUE self)
89
96
  {
90
- Draw *draw;
97
+ MagickDraw *draw;
91
98
 
92
99
  rb_check_frozen(self);
93
100
  draw = get_draw(self);
94
- rm_gvl_check_writable(draw);
101
+ rm_gvl_check_writable(self);
95
102
  return draw;
96
103
  }
97
104
 
@@ -105,10 +112,13 @@ get_writable_draw(VALUE self)
105
112
  VALUE
106
113
  Draw_affine_eq(VALUE self, VALUE matrix)
107
114
  {
108
- Draw *draw;
115
+ MagickDraw *draw;
116
+ AffineMatrix affine;
109
117
 
118
+ get_writable_draw(self);
119
+ Export_AffineMatrix(&affine, matrix);
110
120
  draw = get_writable_draw(self);
111
- Export_AffineMatrix(&draw->info->affine, matrix);
121
+ draw->info->affine = affine;
112
122
  return matrix;
113
123
  }
114
124
 
@@ -122,7 +132,7 @@ Draw_affine_eq(VALUE self, VALUE matrix)
122
132
  VALUE
123
133
  Draw_align_eq(VALUE self, VALUE align)
124
134
  {
125
- Draw *draw;
135
+ MagickDraw *draw;
126
136
 
127
137
  draw = get_writable_draw(self);
128
138
  VALUE_TO_ENUM(align, draw->info->align, AlignType);
@@ -139,7 +149,7 @@ Draw_align_eq(VALUE self, VALUE align)
139
149
  VALUE
140
150
  Draw_decorate_eq(VALUE self, VALUE decorate)
141
151
  {
142
- Draw *draw;
152
+ MagickDraw *draw;
143
153
 
144
154
  draw = get_writable_draw(self);
145
155
  VALUE_TO_ENUM(decorate, draw->info->decorate, DecorationType);
@@ -156,10 +166,13 @@ Draw_decorate_eq(VALUE self, VALUE decorate)
156
166
  VALUE
157
167
  Draw_density_eq(VALUE self, VALUE density)
158
168
  {
159
- Draw *draw;
169
+ MagickDraw *draw;
170
+ char *value;
160
171
 
172
+ get_writable_draw(self);
173
+ value = StringValueCStr(density);
161
174
  draw = get_writable_draw(self);
162
- magick_clone_string(&draw->info->density, StringValueCStr(density));
175
+ magick_clone_string(&draw->info->density, value);
163
176
 
164
177
  return density;
165
178
  }
@@ -174,10 +187,13 @@ Draw_density_eq(VALUE self, VALUE density)
174
187
  VALUE
175
188
  Draw_encoding_eq(VALUE self, VALUE encoding)
176
189
  {
177
- Draw *draw;
190
+ MagickDraw *draw;
191
+ char *value;
178
192
 
193
+ get_writable_draw(self);
194
+ value = StringValueCStr(encoding);
179
195
  draw = get_writable_draw(self);
180
- magick_clone_string(&draw->info->encoding, StringValueCStr(encoding));
196
+ magick_clone_string(&draw->info->encoding, value);
181
197
 
182
198
  return encoding;
183
199
  }
@@ -192,51 +208,97 @@ Draw_encoding_eq(VALUE self, VALUE encoding)
192
208
  VALUE
193
209
  Draw_fill_eq(VALUE self, VALUE fill)
194
210
  {
195
- Draw *draw;
211
+ MagickDraw *draw;
212
+ PixelColor color;
196
213
 
214
+ get_writable_draw(self);
215
+ Color_to_PixelColor(&color, fill);
197
216
  draw = get_writable_draw(self);
198
- Color_to_PixelColor(&draw->info->fill, fill);
217
+ draw->info->fill = color;
199
218
  return fill;
200
219
  }
201
220
 
202
221
 
203
222
  /**
204
- * Accept an image as a fill pattern.
223
+ * Call get_writable_draw under rb_protect.
205
224
  *
206
- * @param pattern [Magick::Image, Magick::ImageList] Either an imagelist or an image. If an
207
- * imagelist, uses the current image.
208
- * @return [Magick::Image] the given pattern image
209
- * @see #stroke_pattern=
210
- * @see #tile=
225
+ * No Ruby usage (internal function)
226
+ *
227
+ * @param self this object
228
+ * @return the MagickDraw struct
211
229
  */
212
- VALUE
213
- Draw_fill_pattern_eq(VALUE self, VALUE pattern)
230
+ static VALUE
231
+ writable_draw(VALUE self)
214
232
  {
215
- Draw *draw;
233
+ return (VALUE)get_writable_draw(self);
234
+ }
216
235
 
217
- draw = get_writable_draw(self);
218
236
 
219
- if (draw->info->fill_pattern != NULL)
220
- {
221
- // Do not trace destruction
222
- DestroyImage(draw->info->fill_pattern);
223
- draw->info->fill_pattern = NULL;
224
- }
237
+ /**
238
+ * Set the fill or stroke pattern to a copy of an image.
239
+ *
240
+ * No Ruby usage (internal function)
241
+ *
242
+ * @param self this object
243
+ * @param pattern the image or imagelist, or nil
244
+ * @param stroke whether to set the stroke pattern instead of the fill pattern
245
+ * @return the image the pattern was copied from, or nil
246
+ */
247
+ static VALUE
248
+ set_pattern(VALUE self, VALUE pattern, bool stroke)
249
+ {
250
+ MagickDraw *draw;
251
+ Image *copy = NULL;
252
+ Image **slot;
253
+ int state;
225
254
 
255
+ get_writable_draw(self);
226
256
  if (!NIL_P(pattern))
227
257
  {
228
- Image *image;
229
-
230
258
  pattern = rm_cur_image(pattern);
231
- image = rm_check_readable(pattern);
232
259
  // Do not trace creation
233
- draw->info->fill_pattern = rm_clone_image(image);
260
+ copy = rm_copy_image(rm_check_readable(pattern), pattern);
261
+ }
262
+
263
+ draw = (MagickDraw *)rb_protect(writable_draw, self, &state);
264
+ if (state)
265
+ {
266
+ if (copy)
267
+ {
268
+ DestroyImage(copy);
269
+ }
270
+ rb_jump_tag(state);
234
271
  }
235
272
 
273
+ // DestroyDrawInfo destroys the clone
274
+ slot = stroke ? &draw->info->stroke_pattern : &draw->info->fill_pattern;
275
+ if (*slot)
276
+ {
277
+ // Do not trace destruction
278
+ DestroyImage(*slot);
279
+ }
280
+ *slot = copy;
281
+
236
282
  return pattern;
237
283
  }
238
284
 
239
285
 
286
+ /**
287
+ * Accept an image as a fill pattern.
288
+ *
289
+ * @param pattern [Magick::Image, Magick::ImageList] Either an imagelist or an image. If an
290
+ * imagelist, uses the current image.
291
+ * @return [Magick::Image] the given pattern image
292
+ * @see #stroke_pattern=
293
+ * @see #tile=
294
+ */
295
+ VALUE
296
+ Draw_fill_pattern_eq(VALUE self, VALUE pattern)
297
+ {
298
+ return set_pattern(self, pattern, false);
299
+ }
300
+
301
+
240
302
  /**
241
303
  * Set the font name.
242
304
  *
@@ -246,10 +308,13 @@ Draw_fill_pattern_eq(VALUE self, VALUE pattern)
246
308
  VALUE
247
309
  Draw_font_eq(VALUE self, VALUE font)
248
310
  {
249
- Draw *draw;
311
+ MagickDraw *draw;
312
+ char *value;
250
313
 
314
+ get_writable_draw(self);
315
+ value = StringValueCStr(font);
251
316
  draw = get_writable_draw(self);
252
- magick_clone_string(&draw->info->font, StringValueCStr(font));
317
+ magick_clone_string(&draw->info->font, value);
253
318
 
254
319
  return font;
255
320
  }
@@ -264,10 +329,13 @@ Draw_font_eq(VALUE self, VALUE font)
264
329
  VALUE
265
330
  Draw_font_family_eq(VALUE self, VALUE family)
266
331
  {
267
- Draw *draw;
332
+ MagickDraw *draw;
333
+ char *value;
268
334
 
335
+ get_writable_draw(self);
336
+ value = StringValueCStr(family);
269
337
  draw = get_writable_draw(self);
270
- magick_clone_string(&draw->info->family, StringValueCStr(family));
338
+ magick_clone_string(&draw->info->family, value);
271
339
 
272
340
  return family;
273
341
  }
@@ -282,7 +350,7 @@ Draw_font_family_eq(VALUE self, VALUE family)
282
350
  VALUE
283
351
  Draw_font_stretch_eq(VALUE self, VALUE stretch)
284
352
  {
285
- Draw *draw;
353
+ MagickDraw *draw;
286
354
 
287
355
  draw = get_writable_draw(self);
288
356
  VALUE_TO_ENUM(stretch, draw->info->stretch, StretchType);
@@ -299,7 +367,7 @@ Draw_font_stretch_eq(VALUE self, VALUE stretch)
299
367
  VALUE
300
368
  Draw_font_style_eq(VALUE self, VALUE style)
301
369
  {
302
- Draw *draw;
370
+ MagickDraw *draw;
303
371
 
304
372
  draw = get_writable_draw(self);
305
373
  VALUE_TO_ENUM(style, draw->info->style, StyleType);
@@ -317,7 +385,7 @@ Draw_font_style_eq(VALUE self, VALUE style)
317
385
  VALUE
318
386
  Draw_font_weight_eq(VALUE self, VALUE weight)
319
387
  {
320
- Draw *draw;
388
+ MagickDraw *draw;
321
389
  size_t w;
322
390
 
323
391
  draw = get_writable_draw(self);
@@ -383,7 +451,7 @@ Draw_font_weight_eq(VALUE self, VALUE weight)
383
451
  VALUE
384
452
  Draw_gravity_eq(VALUE self, VALUE grav)
385
453
  {
386
- Draw *draw;
454
+ MagickDraw *draw;
387
455
 
388
456
  draw = get_writable_draw(self);
389
457
  VALUE_TO_ENUM(grav, draw->info->gravity, GravityType);
@@ -401,10 +469,13 @@ Draw_gravity_eq(VALUE self, VALUE grav)
401
469
  VALUE
402
470
  Draw_kerning_eq(VALUE self, VALUE kerning)
403
471
  {
404
- Draw *draw;
472
+ MagickDraw *draw;
473
+ double value;
405
474
 
475
+ get_writable_draw(self);
476
+ value = NUM2DBL(kerning);
406
477
  draw = get_writable_draw(self);
407
- draw->info->kerning = NUM2DBL(kerning);
478
+ draw->info->kerning = value;
408
479
  return kerning;
409
480
  }
410
481
 
@@ -418,10 +489,13 @@ Draw_kerning_eq(VALUE self, VALUE kerning)
418
489
  VALUE
419
490
  Draw_interline_spacing_eq(VALUE self, VALUE spacing)
420
491
  {
421
- Draw *draw;
492
+ MagickDraw *draw;
493
+ double value;
422
494
 
495
+ get_writable_draw(self);
496
+ value = NUM2DBL(spacing);
423
497
  draw = get_writable_draw(self);
424
- draw->info->interline_spacing = NUM2DBL(spacing);
498
+ draw->info->interline_spacing = value;
425
499
  return spacing;
426
500
  }
427
501
 
@@ -435,10 +509,13 @@ Draw_interline_spacing_eq(VALUE self, VALUE spacing)
435
509
  VALUE
436
510
  Draw_interword_spacing_eq(VALUE self, VALUE spacing)
437
511
  {
438
- Draw *draw;
512
+ MagickDraw *draw;
513
+ double value;
439
514
 
515
+ get_writable_draw(self);
516
+ value = NUM2DBL(spacing);
440
517
  draw = get_writable_draw(self);
441
- draw->info->interword_spacing = NUM2DBL(spacing);
518
+ draw->info->interword_spacing = value;
442
519
  return spacing;
443
520
  }
444
521
 
@@ -452,11 +529,12 @@ Draw_interword_spacing_eq(VALUE self, VALUE spacing)
452
529
  * - Returns Qnil if there is no image
453
530
  *
454
531
  * @param image the Image to convert
532
+ * @param owner the Draw object that holds image
455
533
  * @return Ruby string representation of image
456
534
  * @see str_to_image
457
535
  */
458
536
  static VALUE
459
- image_to_str(Image *image)
537
+ image_to_str(Image *image, VALUE owner)
460
538
  {
461
539
  VALUE dimg = Qnil;
462
540
 
@@ -466,12 +544,23 @@ image_to_str(Image *image)
466
544
  size_t length;
467
545
  Info *info;
468
546
  ExceptionInfo *exception;
547
+ Image *copy;
469
548
 
549
+ // Writing the pattern changes it, and a frozen Draw may be shared with
550
+ // other Ractors.
551
+ copy = rm_copy_image(image, owner);
470
552
  info = CloneImageInfo(NULL);
471
553
  exception = AcquireExceptionInfo();
472
- DECLARE_GVL_CALL(call, ImageToBlob, info, image, &length, exception);
473
- blob = call.keep_thread().run<unsigned char *>();
554
+ DECLARE_GVL_CALL(call, ImageToBlob, info, copy, &length, exception);
555
+ blob = call.keep_thread().destroy(copy).release(info).release(exception).free_result().run<unsigned char *>();
556
+ DestroyImage(copy);
474
557
  DestroyImageInfo(info);
558
+ if (blob && exception->severity >= ErrorException)
559
+ {
560
+ magick_free((void*)blob);
561
+ blob = NULL;
562
+ length = 0;
563
+ }
475
564
  CHECK_EXCEPTION();
476
565
  DestroyExceptionInfo(exception);
477
566
  dimg = rb_str_new((char *)blob, (long)length);
@@ -511,10 +600,11 @@ Image *str_to_image(VALUE str)
511
600
  info = CloneImageInfo(NULL);
512
601
  exception = AcquireExceptionInfo();
513
602
  DECLARE_GVL_CALL(call, BlobToImage, info, RSTRING_PTR(str), (size_t)RSTRING_LEN(str), exception);
514
- image = call.keep_thread().run<Image *>();
603
+ image = call.keep_thread().release(info).release(exception).run<Image *>();
515
604
  DestroyImageInfo(info);
516
- CHECK_EXCEPTION();
605
+ rm_check_exception(exception, image, DestroyOnError);
517
606
  DestroyExceptionInfo(exception);
607
+ image = rm_first_image(image);
518
608
  }
519
609
 
520
610
  RB_GC_GUARD(str);
@@ -523,6 +613,21 @@ Image *str_to_image(VALUE str)
523
613
  }
524
614
 
525
615
 
616
+ /**
617
+ * Copy the primitives of a Draw so that changing the copy leaves the Draw alone.
618
+ *
619
+ * No Ruby usage (internal function)
620
+ *
621
+ * @param primitives the primitives, or 0 if there are none
622
+ * @return a copy of the String, or primitives itself if it is not a String
623
+ */
624
+ static VALUE
625
+ copy_primitives(VALUE primitives)
626
+ {
627
+ return RB_TYPE_P(primitives, T_STRING) ? rb_str_dup(primitives) : primitives;
628
+ }
629
+
630
+
526
631
  /**
527
632
  * Dump custom marshal for Draw objects.
528
633
  *
@@ -539,7 +644,7 @@ Image *str_to_image(VALUE str)
539
644
  VALUE
540
645
  Draw_marshal_dump(VALUE self)
541
646
  {
542
- Draw *draw;
647
+ MagickDraw *draw;
543
648
  VALUE ddraw;
544
649
 
545
650
  draw = get_draw(self);
@@ -558,8 +663,8 @@ Draw_marshal_dump(VALUE self)
558
663
  rb_hash_aset(ddraw, CSTR2SYM("fill"), Pixel_from_PixelColor(&draw->info->fill));
559
664
  rb_hash_aset(ddraw, CSTR2SYM("stroke"), Pixel_from_PixelColor(&draw->info->stroke));
560
665
  rb_hash_aset(ddraw, CSTR2SYM("stroke_width"), rb_float_new(draw->info->stroke_width));
561
- rb_hash_aset(ddraw, CSTR2SYM("fill_pattern"), image_to_str(draw->info->fill_pattern));
562
- rb_hash_aset(ddraw, CSTR2SYM("stroke_pattern"), image_to_str(draw->info->stroke_pattern));
666
+ rb_hash_aset(ddraw, CSTR2SYM("fill_pattern"), image_to_str(draw->info->fill_pattern, self));
667
+ rb_hash_aset(ddraw, CSTR2SYM("stroke_pattern"), image_to_str(draw->info->stroke_pattern, self));
563
668
  rb_hash_aset(ddraw, CSTR2SYM("stroke_antialias"), draw->info->stroke_antialias ? Qtrue : Qfalse);
564
669
  rb_hash_aset(ddraw, CSTR2SYM("text_antialias"), draw->info->text_antialias ? Qtrue : Qfalse);
565
670
  rb_hash_aset(ddraw, CSTR2SYM("decorate"), INT2FIX(draw->info->decorate));
@@ -581,14 +686,83 @@ Draw_marshal_dump(VALUE self)
581
686
  #endif
582
687
  rb_hash_aset(ddraw, CSTR2SYM("kerning"), rb_float_new(draw->info->kerning));
583
688
  rb_hash_aset(ddraw, CSTR2SYM("interword_spacing"), rb_float_new(draw->info->interword_spacing));
689
+ rb_hash_aset(ddraw, CSTR2SYM("interline_spacing"), rb_float_new(draw->info->interline_spacing));
584
690
 
585
691
  // Non-DrawInfo fields
586
- rb_hash_aset(ddraw, CSTR2SYM("primitives"), draw->primitives);
692
+ rb_hash_aset(ddraw, CSTR2SYM("primitives"), copy_primitives(draw->primitives));
587
693
 
588
694
  return ddraw;
589
695
  }
590
696
 
591
697
 
698
+ /**
699
+ * Replace a string of a DrawInfo with a dumped value.
700
+ *
701
+ * No Ruby usage (internal function)
702
+ *
703
+ * @param field the string to replace
704
+ * @param obj the dumped value, or nil for no string
705
+ */
706
+ static void
707
+ load_string(char **field, VALUE obj)
708
+ {
709
+ if (NIL_P(obj))
710
+ {
711
+ magick_clone_string(field, NULL);
712
+ }
713
+ else
714
+ {
715
+ VALUE str = rb_String(obj);
716
+ magick_clone_string(field, StringValueCStr(str));
717
+ RB_GC_GUARD(str);
718
+ }
719
+ }
720
+
721
+
722
+ /**
723
+ * Move the values that Draw#marshal_load loads from one DrawInfo to another.
724
+ *
725
+ * No Ruby usage (internal function)
726
+ *
727
+ * @param info the DrawInfo of the Draw
728
+ * @param loaded the loaded values, which get the old strings and patterns of info
729
+ */
730
+ static void
731
+ move_loaded_values(DrawInfo *info, DrawInfo *loaded)
732
+ {
733
+ std::swap(info->geometry, loaded->geometry);
734
+ info->affine = loaded->affine;
735
+ info->gravity = loaded->gravity;
736
+ info->fill = loaded->fill;
737
+ info->stroke = loaded->stroke;
738
+ info->stroke_width = loaded->stroke_width;
739
+ std::swap(info->fill_pattern, loaded->fill_pattern);
740
+ std::swap(info->stroke_pattern, loaded->stroke_pattern);
741
+ info->stroke_antialias = loaded->stroke_antialias;
742
+ info->text_antialias = loaded->text_antialias;
743
+ info->decorate = loaded->decorate;
744
+ std::swap(info->font, loaded->font);
745
+ std::swap(info->family, loaded->family);
746
+ info->style = loaded->style;
747
+ info->stretch = loaded->stretch;
748
+ info->weight = loaded->weight;
749
+ std::swap(info->encoding, loaded->encoding);
750
+ info->pointsize = loaded->pointsize;
751
+ std::swap(info->density, loaded->density);
752
+ info->align = loaded->align;
753
+ info->undercolor = loaded->undercolor;
754
+ info->clip_units = loaded->clip_units;
755
+ #if defined(IMAGEMAGICK_7)
756
+ info->alpha = loaded->alpha;
757
+ #else
758
+ info->opacity = loaded->opacity;
759
+ #endif
760
+ info->kerning = loaded->kerning;
761
+ info->interword_spacing = loaded->interword_spacing;
762
+ info->interline_spacing = loaded->interline_spacing;
763
+ }
764
+
765
+
592
766
  /**
593
767
  * Load the marshalled object
594
768
  *
@@ -598,65 +772,98 @@ Draw_marshal_dump(VALUE self)
598
772
  VALUE
599
773
  Draw_marshal_load(VALUE self, VALUE ddraw)
600
774
  {
601
- Draw *draw;
602
- VALUE val;
775
+ MagickDraw *draw, *loaded;
776
+ DrawInfo *info;
777
+ VALUE loaded_obj, primitives, val;
778
+ bool has_interline_spacing;
603
779
 
604
- TypedData_Get_Struct(self, Draw, &rm_draw_data_type, draw);
780
+ rb_check_frozen(self);
781
+ rm_gvl_check_writable(self);
782
+ TypedData_Get_Struct(self, MagickDraw, &rm_draw_data_type, draw);
605
783
 
606
784
  Check_Type(ddraw, T_HASH);
607
785
 
608
- if (draw->info == NULL)
786
+ loaded_obj = DrawOptions_alloc(Class_DrawOptions);
787
+ TypedData_Get_Struct(loaded_obj, MagickDraw, &rm_draw_data_type, loaded);
788
+ loaded->info = CloneDrawInfo(NULL, (DrawInfo *) NULL);
789
+ if (!loaded->info)
609
790
  {
610
- ImageInfo *image_info;
611
-
612
- image_info = CloneImageInfo(NULL);
613
- draw->info = CloneDrawInfo(image_info, (DrawInfo *) NULL);
614
- DestroyImageInfo(image_info);
791
+ rb_raise(rb_eNoMemError, "not enough memory to continue");
615
792
  }
616
- OBJ_TO_MAGICK_STRING(draw->info->geometry, rb_hash_aref(ddraw, CSTR2SYM("geometry")));
793
+ info = loaded->info;
794
+
795
+ load_string(&info->geometry, rb_hash_aref(ddraw, CSTR2SYM("geometry")));
617
796
 
618
797
  val = rb_hash_aref(ddraw, CSTR2SYM("affine"));
619
- Export_AffineMatrix(&draw->info->affine, val);
798
+ Export_AffineMatrix(&info->affine, val);
620
799
 
621
- draw->info->gravity = (GravityType) FIX2INT(rb_hash_aref(ddraw, CSTR2SYM("gravity")));
800
+ info->gravity = (GravityType) FIX2INT(rb_hash_aref(ddraw, CSTR2SYM("gravity")));
622
801
 
623
802
  val = rb_hash_aref(ddraw, CSTR2SYM("fill"));
624
- Color_to_PixelColor(&draw->info->fill, val);
803
+ Color_to_PixelColor(&info->fill, val);
625
804
 
626
805
  val = rb_hash_aref(ddraw, CSTR2SYM("stroke"));
627
- Color_to_PixelColor(&draw->info->stroke, val);
628
-
629
- draw->info->stroke_width = NUM2DBL(rb_hash_aref(ddraw, CSTR2SYM("stroke_width")));
630
- draw->info->fill_pattern = str_to_image(rb_hash_aref(ddraw, CSTR2SYM("fill_pattern")));
631
- draw->info->stroke_pattern = str_to_image(rb_hash_aref(ddraw, CSTR2SYM("stroke_pattern")));
632
- draw->info->stroke_antialias = (MagickBooleanType)RTEST(rb_hash_aref(ddraw, CSTR2SYM("stroke_antialias")));
633
- draw->info->text_antialias = (MagickBooleanType)RTEST(rb_hash_aref(ddraw, CSTR2SYM("text_antialias")));
634
- draw->info->decorate = (DecorationType) FIX2INT(rb_hash_aref(ddraw, CSTR2SYM("decorate")));
635
- OBJ_TO_MAGICK_STRING(draw->info->font, rb_hash_aref(ddraw, CSTR2SYM("font")));
636
- OBJ_TO_MAGICK_STRING(draw->info->family, rb_hash_aref(ddraw, CSTR2SYM("family")));
637
-
638
- draw->info->style = (StyleType) FIX2INT(rb_hash_aref(ddraw, CSTR2SYM("style")));
639
- draw->info->stretch = (StretchType) FIX2INT(rb_hash_aref(ddraw, CSTR2SYM("stretch")));
640
- draw->info->weight = NUM2ULONG(rb_hash_aref(ddraw, CSTR2SYM("weight")));
641
- OBJ_TO_MAGICK_STRING(draw->info->encoding, rb_hash_aref(ddraw, CSTR2SYM("encoding")));
642
- draw->info->pointsize = NUM2DBL(rb_hash_aref(ddraw, CSTR2SYM("pointsize")));
643
- OBJ_TO_MAGICK_STRING(draw->info->density, rb_hash_aref(ddraw, CSTR2SYM("density")));
644
- draw->info->align = (AlignType) FIX2INT(rb_hash_aref(ddraw, CSTR2SYM("align")));
806
+ Color_to_PixelColor(&info->stroke, val);
807
+
808
+ info->stroke_width = NUM2DBL(rb_hash_aref(ddraw, CSTR2SYM("stroke_width")));
809
+ info->fill_pattern = str_to_image(rb_hash_aref(ddraw, CSTR2SYM("fill_pattern")));
810
+ info->stroke_pattern = str_to_image(rb_hash_aref(ddraw, CSTR2SYM("stroke_pattern")));
811
+ info->stroke_antialias = (MagickBooleanType)RTEST(rb_hash_aref(ddraw, CSTR2SYM("stroke_antialias")));
812
+ info->text_antialias = (MagickBooleanType)RTEST(rb_hash_aref(ddraw, CSTR2SYM("text_antialias")));
813
+ info->decorate = (DecorationType) FIX2INT(rb_hash_aref(ddraw, CSTR2SYM("decorate")));
814
+ load_string(&info->font, rb_hash_aref(ddraw, CSTR2SYM("font")));
815
+ load_string(&info->family, rb_hash_aref(ddraw, CSTR2SYM("family")));
816
+
817
+ info->style = (StyleType) FIX2INT(rb_hash_aref(ddraw, CSTR2SYM("style")));
818
+ info->stretch = (StretchType) FIX2INT(rb_hash_aref(ddraw, CSTR2SYM("stretch")));
819
+ info->weight = NUM2ULONG(rb_hash_aref(ddraw, CSTR2SYM("weight")));
820
+ load_string(&info->encoding, rb_hash_aref(ddraw, CSTR2SYM("encoding")));
821
+ info->pointsize = NUM2DBL(rb_hash_aref(ddraw, CSTR2SYM("pointsize")));
822
+ load_string(&info->density, rb_hash_aref(ddraw, CSTR2SYM("density")));
823
+ info->align = (AlignType) FIX2INT(rb_hash_aref(ddraw, CSTR2SYM("align")));
645
824
 
646
825
  val = rb_hash_aref(ddraw, CSTR2SYM("undercolor"));
647
- Color_to_PixelColor(&draw->info->undercolor, val);
826
+ Color_to_PixelColor(&info->undercolor, val);
648
827
 
649
- draw->info->clip_units = (ClipPathUnits)FIX2INT(rb_hash_aref(ddraw, CSTR2SYM("clip_units")));
828
+ info->clip_units = (ClipPathUnits)FIX2INT(rb_hash_aref(ddraw, CSTR2SYM("clip_units")));
650
829
  #if defined(IMAGEMAGICK_7)
651
- draw->info->alpha = NUM2QUANTUM(rb_hash_aref(ddraw, CSTR2SYM("alpha")));
830
+ info->alpha = APP2QUANTUM(rb_hash_aref(ddraw, CSTR2SYM("alpha")));
652
831
  #else
653
- draw->info->opacity = NUM2QUANTUM(rb_hash_aref(ddraw, CSTR2SYM("opacity")));
832
+ info->opacity = APP2QUANTUM(rb_hash_aref(ddraw, CSTR2SYM("opacity")));
654
833
  #endif
655
- draw->info->kerning = NUM2DBL(rb_hash_aref(ddraw, CSTR2SYM("kerning")));
656
- draw->info->interword_spacing = NUM2DBL(rb_hash_aref(ddraw, CSTR2SYM("interword_spacing")));
834
+ info->kerning = NUM2DBL(rb_hash_aref(ddraw, CSTR2SYM("kerning")));
835
+ info->interword_spacing = NUM2DBL(rb_hash_aref(ddraw, CSTR2SYM("interword_spacing")));
836
+ val = rb_hash_lookup2(ddraw, CSTR2SYM("interline_spacing"), Qundef);
837
+ has_interline_spacing = val != Qundef;
838
+ if (has_interline_spacing)
839
+ {
840
+ info->interline_spacing = NUM2DBL(val);
841
+ }
657
842
 
658
- draw->primitives = rb_hash_aref(ddraw, CSTR2SYM("primitives"));
843
+ primitives = copy_primitives(rb_hash_aref(ddraw, CSTR2SYM("primitives")));
659
844
 
845
+ // Converting the values runs Ruby code, and loading a pattern releases
846
+ // the GVL, so other code can freeze self or start a call that uses it.
847
+ rb_check_frozen(self);
848
+ rm_gvl_check_writable(self);
849
+ if (draw->info)
850
+ {
851
+ if (!has_interline_spacing)
852
+ {
853
+ info->interline_spacing = draw->info->interline_spacing;
854
+ }
855
+ move_loaded_values(draw->info, info);
856
+ DestroyDrawInfo(info);
857
+ }
858
+ else
859
+ {
860
+ draw->info = info;
861
+ }
862
+ loaded->info = NULL;
863
+ draw->primitives = primitives;
864
+
865
+ RB_GC_GUARD(loaded_obj);
866
+ RB_GC_GUARD(primitives);
660
867
  RB_GC_GUARD(val);
661
868
 
662
869
  return self;
@@ -672,10 +879,13 @@ Draw_marshal_load(VALUE self, VALUE ddraw)
672
879
  VALUE
673
880
  Draw_pointsize_eq(VALUE self, VALUE pointsize)
674
881
  {
675
- Draw *draw;
882
+ MagickDraw *draw;
883
+ double value;
676
884
 
885
+ get_writable_draw(self);
886
+ value = NUM2DBL(pointsize);
677
887
  draw = get_writable_draw(self);
678
- draw->info->pointsize = NUM2DBL(pointsize);
888
+ draw->info->pointsize = value;
679
889
  return pointsize;
680
890
  }
681
891
 
@@ -689,13 +899,14 @@ Draw_pointsize_eq(VALUE self, VALUE pointsize)
689
899
  VALUE
690
900
  Draw_rotation_eq(VALUE self, VALUE deg)
691
901
  {
692
- Draw *draw;
902
+ MagickDraw *draw;
693
903
  double degrees;
694
904
  AffineMatrix affine, current;
695
905
 
906
+ get_writable_draw(self);
907
+ degrees = NUM2DBL(deg);
696
908
  draw = get_writable_draw(self);
697
909
 
698
- degrees = NUM2DBL(deg);
699
910
  if (fabs(degrees) > DBL_EPSILON)
700
911
  {
701
912
  current = draw->info->affine;
@@ -726,10 +937,13 @@ Draw_rotation_eq(VALUE self, VALUE deg)
726
937
  VALUE
727
938
  Draw_stroke_eq(VALUE self, VALUE stroke)
728
939
  {
729
- Draw *draw;
940
+ MagickDraw *draw;
941
+ PixelColor color;
730
942
 
943
+ get_writable_draw(self);
944
+ Color_to_PixelColor(&color, stroke);
731
945
  draw = get_writable_draw(self);
732
- Color_to_PixelColor(&draw->info->stroke, stroke);
946
+ draw->info->stroke = color;
733
947
  return stroke;
734
948
  }
735
949
 
@@ -745,29 +959,7 @@ Draw_stroke_eq(VALUE self, VALUE stroke)
745
959
  VALUE
746
960
  Draw_stroke_pattern_eq(VALUE self, VALUE pattern)
747
961
  {
748
- Draw *draw;
749
-
750
- draw = get_writable_draw(self);
751
-
752
- if (draw->info->stroke_pattern != NULL)
753
- {
754
- // Do not trace destruction
755
- DestroyImage(draw->info->stroke_pattern);
756
- draw->info->stroke_pattern = NULL;
757
- }
758
-
759
- if (!NIL_P(pattern))
760
- {
761
- Image *image;
762
-
763
- // DestroyDrawInfo destroys the clone
764
- pattern = rm_cur_image(pattern);
765
- image = rm_check_readable(pattern);
766
- // Do not trace creation
767
- draw->info->stroke_pattern = rm_clone_image(image);
768
- }
769
-
770
- return pattern;
962
+ return set_pattern(self, pattern, true);
771
963
  }
772
964
 
773
965
 
@@ -780,10 +972,13 @@ Draw_stroke_pattern_eq(VALUE self, VALUE pattern)
780
972
  VALUE
781
973
  Draw_stroke_width_eq(VALUE self, VALUE stroke_width)
782
974
  {
783
- Draw *draw;
975
+ MagickDraw *draw;
976
+ double value;
784
977
 
978
+ get_writable_draw(self);
979
+ value = NUM2DBL(stroke_width);
785
980
  draw = get_writable_draw(self);
786
- draw->info->stroke_width = NUM2DBL(stroke_width);
981
+ draw->info->stroke_width = value;
787
982
  return stroke_width;
788
983
  }
789
984
 
@@ -797,7 +992,7 @@ Draw_stroke_width_eq(VALUE self, VALUE stroke_width)
797
992
  VALUE
798
993
  Draw_text_antialias_eq(VALUE self, VALUE text_antialias)
799
994
  {
800
- Draw *draw;
995
+ MagickDraw *draw;
801
996
 
802
997
  draw = get_writable_draw(self);
803
998
  draw->info->text_antialias = (MagickBooleanType) RTEST(text_antialias);
@@ -827,10 +1022,13 @@ Draw_tile_eq(VALUE self, VALUE image)
827
1022
  VALUE
828
1023
  Draw_undercolor_eq(VALUE self, VALUE undercolor)
829
1024
  {
830
- Draw *draw;
1025
+ MagickDraw *draw;
1026
+ PixelColor color;
831
1027
 
1028
+ get_writable_draw(self);
1029
+ Color_to_PixelColor(&color, undercolor);
832
1030
  draw = get_writable_draw(self);
833
- Color_to_PixelColor(&draw->info->undercolor, undercolor);
1031
+ draw->info->undercolor = color;
834
1032
  return undercolor;
835
1033
  }
836
1034
 
@@ -844,7 +1042,7 @@ struct Draw_annotate_args
844
1042
  VALUE x_arg;
845
1043
  VALUE y_arg;
846
1044
  VALUE text;
847
- Draw *draw;
1045
+ MagickDraw *draw;
848
1046
  AffineMatrix keep;
849
1047
  #if defined(IMAGEMAGICK_7)
850
1048
  ExceptionInfo *exception;
@@ -852,6 +1050,89 @@ struct Draw_annotate_args
852
1050
  };
853
1051
 
854
1052
 
1053
+ // A DrawInfo whose patterns to copy, and an object that the caller uses after
1054
+ // the copies
1055
+ struct CopyPatterns_Args
1056
+ {
1057
+ DrawInfo *info;
1058
+ VALUE held;
1059
+ };
1060
+
1061
+
1062
+ // Give a pattern that CloneDrawInfo shared a pixel cache of its own
1063
+ static void
1064
+ copy_pattern(Image **pattern, VALUE held)
1065
+ {
1066
+ if (*pattern)
1067
+ {
1068
+ Image *copy = rm_copy_image(*pattern, Qnil, held);
1069
+
1070
+ DestroyImage(*pattern);
1071
+ *pattern = copy;
1072
+ }
1073
+ }
1074
+
1075
+
1076
+ static VALUE
1077
+ copy_patterns(VALUE arg)
1078
+ {
1079
+ struct CopyPatterns_Args *args = (struct CopyPatterns_Args *)arg;
1080
+
1081
+ copy_pattern(&args->info->fill_pattern, args->held);
1082
+ copy_pattern(&args->info->stroke_pattern, args->held);
1083
+ return Qnil;
1084
+ }
1085
+
1086
+
1087
+ // Copy the DrawInfo of a Draw for a call that draws. A frozen Draw may be
1088
+ // shared with other Ractors, whose calls would read the pixel cache of a
1089
+ // pattern through its default nexus at once.
1090
+ static DrawInfo *
1091
+ clone_draw_info(VALUE self, const MagickDraw *draw, VALUE image_arg)
1092
+ {
1093
+ DrawInfo *info = CloneDrawInfo(NULL, draw->info);
1094
+ int state = 0;
1095
+
1096
+ if (OBJ_FROZEN(self))
1097
+ {
1098
+ struct CopyPatterns_Args args = { info, image_arg };
1099
+
1100
+ rb_protect(copy_patterns, (VALUE)&args, &state);
1101
+ if (state)
1102
+ {
1103
+ DestroyDrawInfo(info);
1104
+ rb_jump_tag(state);
1105
+ }
1106
+ }
1107
+ return info;
1108
+ }
1109
+
1110
+
1111
+ // Mark a Draw for a call. The copy of its DrawInfo shares the pixel caches of
1112
+ // its patterns, which ImageMagick reads through a working area that only one
1113
+ // call can use, so a call that may use them takes the Draw for itself.
1114
+ static rm_gvl_call &
1115
+ mark_draw(rm_gvl_call &call, VALUE obj, const MagickDraw *draw)
1116
+ {
1117
+ if (draw->info->fill_pattern || draw->info->stroke_pattern)
1118
+ {
1119
+ return call.update(obj);
1120
+ }
1121
+ return call.read(obj);
1122
+ }
1123
+
1124
+
1125
+ // Change back the affine matrix that the block of Draw#annotate may have changed
1126
+ static void
1127
+ restore_affine(struct Draw_annotate_args *annotate)
1128
+ {
1129
+ if (memcmp(&annotate->draw->info->affine, &annotate->keep, sizeof(annotate->keep)) != 0)
1130
+ {
1131
+ annotate->draw->info->affine = annotate->keep;
1132
+ }
1133
+ }
1134
+
1135
+
855
1136
  /**
856
1137
  * Release what annotate_body acquired and restore the affine matrix.
857
1138
  *
@@ -865,14 +1146,8 @@ static VALUE
865
1146
  annotate_ensure(VALUE arg)
866
1147
  {
867
1148
  struct Draw_annotate_args *annotate = (struct Draw_annotate_args *)arg;
868
- Draw *draw = annotate->draw;
869
1149
 
870
- if (!rm_gvl_in_use(draw))
871
- {
872
- magick_free(draw->info->text);
873
- draw->info->text = NULL;
874
- draw->info->affine = annotate->keep;
875
- }
1150
+ restore_affine(annotate);
876
1151
 
877
1152
  #if defined(IMAGEMAGICK_7)
878
1153
  if (annotate->exception)
@@ -899,8 +1174,9 @@ static VALUE
899
1174
  annotate_body(VALUE arg)
900
1175
  {
901
1176
  struct Draw_annotate_args *annotate = (struct Draw_annotate_args *)arg;
902
- Draw *draw = annotate->draw;
1177
+ MagickDraw *draw = annotate->draw;
903
1178
  Image *image;
1179
+ DrawInfo *info;
904
1180
  unsigned long width, height;
905
1181
  long x, y;
906
1182
  char geometry_str[100];
@@ -932,24 +1208,28 @@ annotate_body(VALUE arg)
932
1208
  snprintf(geometry_str, sizeof(geometry_str), "%lux%lu%+ld%+ld", width, height, x, y);
933
1209
  }
934
1210
 
935
- // Store in Draw structure. The text is drawn as given: it is not run
1211
+ // Store in the MagickDraw struct. The text is drawn as given: it is not run
936
1212
  // through InterpretImageProperties(), so a `%[...]` or `%x` escape in it is
937
1213
  // not expanded. Everything those escapes provide is available directly from
938
1214
  // Ruby -- Image#columns, Image#filename, Image#artifact and so on.
1215
+ annotate->text = rb_str_new_frozen(StringValue(annotate->text));
939
1216
  embed_text = StringValueCStr(annotate->text);
940
1217
  image = rm_check_frozen(annotate->image_arg);
941
- rm_gvl_check_writable(draw);
1218
+ rm_gvl_check_readable(annotate->self);
942
1219
 
943
- magick_clone_string(&draw->info->geometry, geometry_str);
944
- draw->info->text = ConstantString(embed_text);
1220
+ info = clone_draw_info(annotate->self, draw, annotate->image_arg);
1221
+ restore_affine(annotate);
1222
+ magick_clone_string(&info->geometry, geometry_str);
1223
+ magick_clone_string(&info->text, embed_text);
945
1224
 
946
1225
  #if defined(IMAGEMAGICK_7)
947
1226
  annotate->exception = AcquireExceptionInfo();
948
- DECLARE_GVL_CALL(call, AnnotateImage, image, draw->info, annotate->exception);
1227
+ DECLARE_GVL_CALL(call, AnnotateImage, image, info, annotate->exception);
949
1228
  #else
950
- DECLARE_GVL_CALL(call, AnnotateImage, image, draw->info);
1229
+ DECLARE_GVL_CALL(call, AnnotateImage, image, info);
951
1230
  #endif
952
- call.update(annotate->image_arg).update(annotate->self).run<void>();
1231
+ mark_draw(call.update(annotate->image_arg), annotate->self, draw).release(info).run<void>();
1232
+ DestroyDrawInfo(info);
953
1233
 
954
1234
  #if defined(IMAGEMAGICK_7)
955
1235
  exception = annotate->exception;
@@ -994,7 +1274,7 @@ VALUE Draw_annotate(
994
1274
  // Save the affine matrix in case it is modified by
995
1275
  // Draw#rotation=
996
1276
  annotate.draw = get_draw(self);
997
- rm_gvl_check_writable(annotate.draw);
1277
+ rm_gvl_check_readable(self);
998
1278
  annotate.keep = annotate.draw->info->affine;
999
1279
 
1000
1280
  annotate.self = self;
@@ -1043,7 +1323,7 @@ VALUE Draw_annotate(
1043
1323
  VALUE
1044
1324
  Draw_composite(int argc, VALUE *argv, VALUE self)
1045
1325
  {
1046
- Draw *draw;
1326
+ MagickDraw *draw;
1047
1327
  const char *op;
1048
1328
  double x, y, width, height;
1049
1329
  CompositeOperator composite_op;
@@ -1080,16 +1360,19 @@ Draw_composite(int argc, VALUE *argv, VALUE self)
1080
1360
  rb_raise(rb_eArgError, "unknown composite operator (%d)", composite_op);
1081
1361
  }
1082
1362
 
1083
- TypedData_Get_Struct(self, Draw, &rm_draw_data_type, draw);
1363
+ rb_check_frozen(self);
1364
+ TypedData_Get_Struct(self, MagickDraw, &rm_draw_data_type, draw);
1365
+ comp_img = rm_check_readable(image);
1084
1366
 
1085
1367
  // Create a temp copy of the composite image
1086
1368
  rm_write_temp_image(comp_img, name, sizeof(name));
1087
1369
 
1088
1370
  // Add the temp filename to the filename array.
1089
1371
  // Use Magick storage since we need to keep the list around
1090
- // until destroy_Draw is called.
1372
+ // until the last Draw object that refers to it is freed.
1091
1373
  tmpfile_name = (struct TmpFile_Name *)magick_malloc(sizeof(struct TmpFile_Name) + rm_strnlen_s(name, sizeof(name)));
1092
1374
  strcpy(tmpfile_name->name, name);
1375
+ tmpfile_name->refs = 1;
1093
1376
  tmpfile_name->next = draw->tmpfile_ary;
1094
1377
  draw->tmpfile_ary = tmpfile_name;
1095
1378
 
@@ -1106,26 +1389,6 @@ Draw_composite(int argc, VALUE *argv, VALUE self)
1106
1389
  }
1107
1390
 
1108
1391
 
1109
- // Release what Draw#draw and get_type_metrics() set on the DrawInfo for one call
1110
- static void
1111
- free_primitive(void *ptr, intptr_t arg ATTRIBUTE_UNUSED)
1112
- {
1113
- Draw *draw = (Draw *)ptr;
1114
-
1115
- magick_free(draw->info->primitive);
1116
- draw->info->primitive = NULL;
1117
- }
1118
-
1119
- static void
1120
- free_text(void *ptr, intptr_t arg ATTRIBUTE_UNUSED)
1121
- {
1122
- Draw *draw = (Draw *)ptr;
1123
-
1124
- magick_free(draw->info->text);
1125
- draw->info->text = NULL;
1126
- }
1127
-
1128
-
1129
1392
  /**
1130
1393
  * Execute the stored drawing primitives on the current image.
1131
1394
  *
@@ -1136,8 +1399,10 @@ free_text(void *ptr, intptr_t arg ATTRIBUTE_UNUSED)
1136
1399
  VALUE
1137
1400
  Draw_draw(VALUE self, VALUE image_arg)
1138
1401
  {
1139
- Draw *draw;
1402
+ MagickDraw *draw;
1140
1403
  Image *image;
1404
+ DrawInfo *info;
1405
+ char *primitive;
1141
1406
  #if defined(IMAGEMAGICK_7)
1142
1407
  ExceptionInfo *exception;
1143
1408
  #endif
@@ -1146,26 +1411,25 @@ Draw_draw(VALUE self, VALUE image_arg)
1146
1411
  image = rm_check_frozen(image_arg);
1147
1412
 
1148
1413
  draw = get_draw(self);
1149
- rm_gvl_check_writable(draw);
1414
+ rm_gvl_check_readable(self);
1150
1415
  if (draw->primitives == 0)
1151
1416
  {
1152
1417
  rb_raise(rb_eArgError, "nothing to draw");
1153
1418
  }
1419
+ primitive = StringValueCStr(draw->primitives);
1154
1420
 
1155
- // Point the DrawInfo structure at the current set of primitives.
1156
- magick_clone_string(&(draw->info->primitive), StringValueCStr(draw->primitives));
1421
+ info = clone_draw_info(self, draw, image_arg);
1422
+ magick_clone_string(&info->primitive, primitive);
1157
1423
 
1158
1424
  #if defined(IMAGEMAGICK_7)
1159
1425
  exception = AcquireExceptionInfo();
1160
- DECLARE_GVL_CALL(call, DrawImage, image, draw->info, exception);
1426
+ DECLARE_GVL_CALL(call, DrawImage, image, info, exception);
1161
1427
  call.release(exception);
1162
1428
  #else
1163
- DECLARE_GVL_CALL(call, DrawImage, image, draw->info);
1429
+ DECLARE_GVL_CALL(call, DrawImage, image, info);
1164
1430
  #endif
1165
- call.update(image_arg).update(self).restore(free_primitive, draw).run<void>();
1166
-
1167
- magick_free(draw->info->primitive);
1168
- draw->info->primitive = NULL;
1431
+ mark_draw(call.update(image_arg), self, draw).release(info).run<void>();
1432
+ DestroyDrawInfo(info);
1169
1433
 
1170
1434
  #if defined(IMAGEMAGICK_7)
1171
1435
  CHECK_EXCEPTION();
@@ -1234,6 +1498,54 @@ Draw_get_multiline_type_metrics(
1234
1498
  }
1235
1499
 
1236
1500
 
1501
+ /**
1502
+ * Release a reference to a list of temporary images, and delete the images
1503
+ * that no other Draw object refers to.
1504
+ *
1505
+ * No Ruby usage (internal function)
1506
+ *
1507
+ * @param tmpfile_name the first entry of the list
1508
+ */
1509
+ static void
1510
+ release_tmpfiles(struct TmpFile_Name *tmpfile_name)
1511
+ {
1512
+ while (tmpfile_name && --tmpfile_name->refs == 0)
1513
+ {
1514
+ struct TmpFile_Name *next = tmpfile_name->next;
1515
+
1516
+ rm_delete_temp_image(tmpfile_name->name);
1517
+ magick_free(tmpfile_name);
1518
+ tmpfile_name = next;
1519
+ }
1520
+ }
1521
+
1522
+
1523
+ /**
1524
+ * Replace the DrawInfo of a Draw with another one.
1525
+ *
1526
+ * - A setter can keep a pointer into the DrawInfo while it calls Ruby code,
1527
+ * so the DrawInfo keeps its address and takes the contents of info.
1528
+ *
1529
+ * No Ruby usage (internal function)
1530
+ *
1531
+ * @param draw the MagickDraw struct
1532
+ * @param info the new DrawInfo, which this function destroys or keeps
1533
+ */
1534
+ static void
1535
+ replace_draw_info(MagickDraw *draw, DrawInfo *info)
1536
+ {
1537
+ if (draw->info)
1538
+ {
1539
+ std::swap(*draw->info, *info);
1540
+ DestroyDrawInfo(info);
1541
+ }
1542
+ else
1543
+ {
1544
+ draw->info = info;
1545
+ }
1546
+ }
1547
+
1548
+
1237
1549
  /**
1238
1550
  * Initialize clone, dup methods.
1239
1551
  *
@@ -1242,21 +1554,52 @@ Draw_get_multiline_type_metrics(
1242
1554
  */
1243
1555
  VALUE Draw_init_copy(VALUE self, VALUE orig)
1244
1556
  {
1245
- Draw *copy, *original;
1557
+ MagickDraw *copy, *original;
1558
+ DrawInfo *info;
1559
+ struct TmpFile_Name *tmpfile_ary;
1560
+ int state = 0;
1246
1561
 
1247
- TypedData_Get_Struct(orig, Draw, &rm_draw_data_type, original);
1248
- TypedData_Get_Struct(self, Draw, &rm_draw_data_type, copy);
1562
+ if (!rm_check_init_copy(self, orig))
1563
+ {
1564
+ return self;
1565
+ }
1566
+ rm_gvl_check_writable(self);
1567
+ rm_gvl_check_readable(orig);
1568
+ TypedData_Get_Struct(orig, MagickDraw, &rm_draw_data_type, original);
1569
+ TypedData_Get_Struct(self, MagickDraw, &rm_draw_data_type, copy);
1249
1570
 
1250
- copy->info = CloneDrawInfo(NULL, original->info);
1251
- if (!copy->info)
1571
+ info = CloneDrawInfo(NULL, original->info);
1572
+ if (!info)
1252
1573
  {
1253
1574
  rb_raise(rb_eNoMemError, "not enough memory to continue");
1254
1575
  }
1576
+ struct CopyPatterns_Args args = { info, orig };
1577
+ rb_protect(copy_patterns, (VALUE)&args, &state);
1578
+ if (state)
1579
+ {
1580
+ DestroyDrawInfo(info);
1581
+ rb_jump_tag(state);
1582
+ }
1583
+ // Copying the patterns releases the GVL, so other code can freeze self or
1584
+ // start a call that uses it meanwhile.
1585
+ if (OBJ_FROZEN(self) || rm_gvl_in_use(self))
1586
+ {
1587
+ DestroyDrawInfo(info);
1588
+ rb_check_frozen(self);
1589
+ rm_gvl_check_writable(self);
1590
+ }
1591
+ replace_draw_info(copy, info);
1592
+ copy->shadow_color = original->shadow_color;
1593
+
1594
+ copy->primitives = copy_primitives(original->primitives);
1255
1595
 
1256
- if (original->primitives)
1596
+ tmpfile_ary = copy->tmpfile_ary;
1597
+ copy->tmpfile_ary = original->tmpfile_ary;
1598
+ if (copy->tmpfile_ary)
1257
1599
  {
1258
- copy->primitives = rb_str_dup(original->primitives);
1600
+ copy->tmpfile_ary->refs++;
1259
1601
  }
1602
+ release_tmpfiles(tmpfile_ary);
1260
1603
 
1261
1604
  return self;
1262
1605
  }
@@ -1270,14 +1613,19 @@ VALUE Draw_init_copy(VALUE self, VALUE orig)
1270
1613
  VALUE
1271
1614
  Draw_initialize(VALUE self)
1272
1615
  {
1273
- Draw *draw, *draw_options;
1616
+ MagickDraw *draw, *draw_options;
1274
1617
  VALUE options;
1275
1618
 
1276
- TypedData_Get_Struct(self, Draw, &rm_draw_data_type, draw);
1619
+ rb_check_frozen(self);
1620
+ rm_gvl_check_writable(self);
1621
+ TypedData_Get_Struct(self, MagickDraw, &rm_draw_data_type, draw);
1277
1622
 
1278
1623
  options = new_DrawOptions();
1279
- TypedData_Get_Struct(options, Draw, &rm_draw_data_type, draw_options);
1280
- draw->info = draw_options->info;
1624
+ TypedData_Get_Struct(options, MagickDraw, &rm_draw_data_type, draw_options);
1625
+ // The block of Draw.new runs in new_DrawOptions.
1626
+ rb_check_frozen(self);
1627
+ rm_gvl_check_writable(self);
1628
+ replace_draw_info(draw, draw_options->info);
1281
1629
  draw_options->info = NULL;
1282
1630
 
1283
1631
  RB_GC_GUARD(options);
@@ -1295,10 +1643,10 @@ Draw_initialize(VALUE self)
1295
1643
  VALUE
1296
1644
  Draw_inspect(VALUE self)
1297
1645
  {
1298
- Draw *draw;
1646
+ MagickDraw *draw;
1299
1647
 
1300
- TypedData_Get_Struct(self, Draw, &rm_draw_data_type, draw);
1301
- return draw->primitives ? draw->primitives : rb_str_new2("(no primitives defined)");
1648
+ TypedData_Get_Struct(self, MagickDraw, &rm_draw_data_type, draw);
1649
+ return draw->primitives ? copy_primitives(draw->primitives) : rb_str_new2("(no primitives defined)");
1302
1650
  }
1303
1651
 
1304
1652
 
@@ -1309,11 +1657,10 @@ Draw_inspect(VALUE self)
1309
1657
  */
1310
1658
  VALUE Draw_alloc(VALUE klass)
1311
1659
  {
1312
- Draw *draw;
1660
+ MagickDraw *draw;
1313
1661
  VALUE draw_obj;
1314
1662
 
1315
- draw = ALLOC(Draw);
1316
- memset(draw, 0, sizeof(Draw));
1663
+ draw = ZALLOC(MagickDraw);
1317
1664
  draw_obj = TypedData_Wrap_Struct(klass, &rm_draw_data_type, draw);
1318
1665
 
1319
1666
  RB_GC_GUARD(draw_obj);
@@ -1331,11 +1678,12 @@ VALUE Draw_alloc(VALUE klass)
1331
1678
  VALUE
1332
1679
  Draw_primitive(VALUE self, VALUE primitive)
1333
1680
  {
1334
- Draw *draw;
1681
+ MagickDraw *draw;
1335
1682
 
1336
1683
  rb_check_frozen(self);
1337
1684
  StringValue(primitive);
1338
- TypedData_Get_Struct(self, Draw, &rm_draw_data_type, draw);
1685
+ rb_check_frozen(self);
1686
+ TypedData_Get_Struct(self, MagickDraw, &rm_draw_data_type, draw);
1339
1687
 
1340
1688
  if (draw->primitives == (VALUE)0)
1341
1689
  {
@@ -1355,12 +1703,12 @@ Draw_primitive(VALUE self, VALUE primitive)
1355
1703
  *
1356
1704
  * No Ruby usage (internal function)
1357
1705
  *
1358
- * @param drawptr pointer to a Draw object
1706
+ * @param drawptr pointer to a MagickDraw struct
1359
1707
  */
1360
1708
  static void
1361
1709
  Draw_compact(void *drawptr)
1362
1710
  {
1363
- Draw *draw = (Draw *)drawptr;
1711
+ MagickDraw *draw = (MagickDraw *)drawptr;
1364
1712
 
1365
1713
  if (draw->primitives != (VALUE)0)
1366
1714
  {
@@ -1373,12 +1721,12 @@ Draw_compact(void *drawptr)
1373
1721
  *
1374
1722
  * No Ruby usage (internal function)
1375
1723
  *
1376
- * @param drawptr pointer to a Draw object
1724
+ * @param drawptr pointer to a MagickDraw struct
1377
1725
  */
1378
1726
  static void
1379
1727
  Draw_mark(void *drawptr)
1380
1728
  {
1381
- Draw *draw = (Draw *)drawptr;
1729
+ MagickDraw *draw = (MagickDraw *)drawptr;
1382
1730
 
1383
1731
  if (draw->primitives != (VALUE)0)
1384
1732
  {
@@ -1392,12 +1740,12 @@ Draw_mark(void *drawptr)
1392
1740
  *
1393
1741
  * No Ruby usage (internal function)
1394
1742
  *
1395
- * @param drawptr pointer to a Draw object
1743
+ * @param drawptr pointer to a MagickDraw struct
1396
1744
  */
1397
1745
  static void
1398
1746
  Draw_destroy(void *drawptr)
1399
1747
  {
1400
- Draw *draw = (Draw *)drawptr;
1748
+ MagickDraw *draw = (MagickDraw *)drawptr;
1401
1749
 
1402
1750
  if (draw->info)
1403
1751
  {
@@ -1405,16 +1753,7 @@ Draw_destroy(void *drawptr)
1405
1753
  draw->info = NULL;
1406
1754
  }
1407
1755
 
1408
- // Erase any temporary image files.
1409
- while (draw->tmpfile_ary)
1410
- {
1411
- struct TmpFile_Name *tmpfile_name;
1412
-
1413
- tmpfile_name = draw->tmpfile_ary;
1414
- draw->tmpfile_ary = draw->tmpfile_ary->next;
1415
- rm_delete_temp_image(tmpfile_name->name);
1416
- magick_free(tmpfile_name);
1417
- }
1756
+ release_tmpfiles(draw->tmpfile_ary);
1418
1757
 
1419
1758
  xfree(drawptr);
1420
1759
  }
@@ -1424,12 +1763,12 @@ Draw_destroy(void *drawptr)
1424
1763
  *
1425
1764
  * No Ruby usage (internal function)
1426
1765
  *
1427
- * @param infoptr pointer to the Draw object
1766
+ * @param drawptr pointer to the MagickDraw struct
1428
1767
  */
1429
1768
  static size_t
1430
1769
  Draw_memsize(const void *drawptr)
1431
1770
  {
1432
- return sizeof(Draw);
1771
+ return sizeof(MagickDraw);
1433
1772
  }
1434
1773
 
1435
1774
  /**
@@ -1457,11 +1796,10 @@ new_DrawOptions(void)
1457
1796
  VALUE
1458
1797
  DrawOptions_alloc(VALUE klass)
1459
1798
  {
1460
- Draw *draw_options;
1799
+ MagickDraw *draw_options;
1461
1800
  VALUE draw_options_obj;
1462
1801
 
1463
- draw_options = ALLOC(Draw);
1464
- memset(draw_options, 0, sizeof(Draw));
1802
+ draw_options = ZALLOC(MagickDraw);
1465
1803
  draw_options_obj = TypedData_Wrap_Struct(klass, &rm_draw_data_type, draw_options);
1466
1804
 
1467
1805
  RB_GC_GUARD(draw_options_obj);
@@ -1478,14 +1816,18 @@ DrawOptions_alloc(VALUE klass)
1478
1816
  VALUE
1479
1817
  DrawOptions_initialize(VALUE self)
1480
1818
  {
1481
- Draw *draw_options;
1819
+ MagickDraw *draw_options;
1820
+ DrawInfo *info;
1482
1821
 
1483
- TypedData_Get_Struct(self, Draw, &rm_draw_data_type, draw_options);
1484
- draw_options->info = AcquireDrawInfo();
1485
- if (!draw_options->info)
1822
+ rb_check_frozen(self);
1823
+ rm_gvl_check_writable(self);
1824
+ TypedData_Get_Struct(self, MagickDraw, &rm_draw_data_type, draw_options);
1825
+ info = AcquireDrawInfo();
1826
+ if (!info)
1486
1827
  {
1487
1828
  rb_raise(rb_eNoMemError, "not enough memory to continue");
1488
1829
  }
1830
+ replace_draw_info(draw_options, info);
1489
1831
 
1490
1832
  if (rb_block_given_p())
1491
1833
  {
@@ -1509,12 +1851,11 @@ PolaroidOptions_alloc(VALUE klass)
1509
1851
  {
1510
1852
  VALUE polaroid_obj;
1511
1853
  ImageInfo *image_info;
1512
- Draw *draw;
1854
+ MagickDraw *draw;
1513
1855
 
1514
1856
  image_info = CloneImageInfo(NULL);
1515
1857
 
1516
- draw = ALLOC(Draw);
1517
- memset(draw, 0, sizeof(*draw));
1858
+ draw = ZALLOC(MagickDraw);
1518
1859
 
1519
1860
  draw->info = CloneDrawInfo(image_info, (DrawInfo *) NULL);
1520
1861
  (void) DestroyImageInfo(image_info);
@@ -1537,11 +1878,11 @@ PolaroidOptions_alloc(VALUE klass)
1537
1878
  VALUE
1538
1879
  PolaroidOptions_initialize(VALUE self)
1539
1880
  {
1540
- Draw *draw;
1881
+ MagickDraw *draw;
1541
1882
  ExceptionInfo *exception;
1542
1883
 
1543
1884
  // Default shadow color
1544
- draw = get_draw(self);
1885
+ draw = get_writable_draw(self);
1545
1886
 
1546
1887
  exception = AcquireExceptionInfo();
1547
1888
  QueryColorCompliance("gray75", AllCompliance, &draw->shadow_color, exception);
@@ -1582,11 +1923,13 @@ rm_polaroid_new(void)
1582
1923
  VALUE
1583
1924
  PolaroidOptions_shadow_color_eq(VALUE self, VALUE shadow)
1584
1925
  {
1585
- Draw *draw;
1926
+ MagickDraw *draw;
1927
+ PixelColor color;
1586
1928
 
1587
- rb_check_frozen(self);
1588
- TypedData_Get_Struct(self, Draw, &rm_draw_data_type, draw);
1589
- Color_to_PixelColor(&draw->shadow_color, shadow);
1929
+ get_writable_draw(self);
1930
+ Color_to_PixelColor(&color, shadow);
1931
+ draw = get_writable_draw(self);
1932
+ draw->shadow_color = color;
1590
1933
  return shadow;
1591
1934
  }
1592
1935
 
@@ -1600,52 +1943,84 @@ PolaroidOptions_shadow_color_eq(VALUE self, VALUE shadow)
1600
1943
  VALUE
1601
1944
  PolaroidOptions_border_color_eq(VALUE self, VALUE border)
1602
1945
  {
1603
- Draw *draw;
1946
+ MagickDraw *draw;
1947
+ PixelColor color;
1604
1948
 
1949
+ get_writable_draw(self);
1950
+ Color_to_PixelColor(&color, border);
1605
1951
  draw = get_writable_draw(self);
1606
- Color_to_PixelColor(&draw->info->border_color, border);
1952
+ draw->info->border_color = color;
1607
1953
  return border;
1608
1954
  }
1609
1955
 
1610
1956
 
1611
1957
  /**
1612
- * Create a dummy object of an image class.
1958
+ * Create the image that get_type_metrics measures on when no image is given.
1613
1959
  *
1614
1960
  * No Ruby usage (internal function)
1615
1961
  *
1616
- * @param klass the class for which to create a dummy
1617
- * @return the newly allocated dummy
1962
+ * @return the image
1618
1963
  */
1619
1964
  static VALUE
1620
- get_dummy_tm_img(VALUE klass)
1965
+ new_dummy_tm_img(void)
1621
1966
  {
1622
- #define DUMMY_IMG_CLASS_VAR "@@_dummy_img_"
1623
- VALUE dummy_img = 0;
1967
+ Info *info;
1968
+ Image *image;
1624
1969
 
1625
- if (rb_cvar_defined(klass, rb_intern(DUMMY_IMG_CLASS_VAR)) != Qtrue)
1970
+ info = CloneImageInfo(NULL);
1971
+ if (!info)
1626
1972
  {
1627
- Info *info;
1628
- Image *image;
1973
+ rb_raise(rb_eNoMemError, "not enough memory to continue");
1974
+ }
1975
+ image = rm_acquire_image(info);
1976
+ DestroyImageInfo(info);
1629
1977
 
1630
- info = CloneImageInfo(NULL);
1631
- if (!info)
1632
- {
1633
- rb_raise(rb_eNoMemError, "not enough memory to continue");
1634
- }
1635
- image = rm_acquire_image(info);
1636
- DestroyImageInfo(info);
1978
+ if (!image)
1979
+ {
1980
+ rb_raise(rb_eNoMemError, "not enough memory to continue");
1981
+ }
1982
+ return rm_image_new(image);
1983
+ }
1637
1984
 
1638
- if (!image)
1639
- {
1640
- rb_raise(rb_eNoMemError, "not enough memory to continue");
1641
- }
1642
- dummy_img = rm_image_new(image);
1643
1985
 
1644
- rb_cv_set(klass, DUMMY_IMG_CLASS_VAR, dummy_img);
1645
- }
1646
- dummy_img = rb_cv_get(klass, DUMMY_IMG_CLASS_VAR);
1986
+ /**
1987
+ * Set up where get_type_metrics keeps the image it measures on when no image
1988
+ * is given.
1989
+ *
1990
+ * No Ruby usage (internal function)
1991
+ */
1992
+ void
1993
+ rm_draw_init(void)
1994
+ {
1995
+ #if defined(HAVE_RB_RACTOR_LOCAL_STORAGE_VALUE_NEWKEY)
1996
+ dummy_img_key = rb_ractor_local_storage_value_newkey();
1997
+ #else
1998
+ rb_gc_register_address(&dummy_img);
1999
+ dummy_img = new_dummy_tm_img();
2000
+ #endif
2001
+ }
1647
2002
 
1648
- RB_GC_GUARD(dummy_img);
2003
+
2004
+ /**
2005
+ * Get the image that get_type_metrics measures on when no image is given,
2006
+ * creating it once in each Ractor.
2007
+ *
2008
+ * No Ruby usage (internal function)
2009
+ *
2010
+ * @return the image
2011
+ */
2012
+ static VALUE
2013
+ get_dummy_tm_img(void)
2014
+ {
2015
+ #if defined(HAVE_RB_RACTOR_LOCAL_STORAGE_VALUE_NEWKEY)
2016
+ VALUE dummy_img;
2017
+
2018
+ if (!rb_ractor_local_storage_value_lookup(dummy_img_key, &dummy_img))
2019
+ {
2020
+ dummy_img = new_dummy_tm_img();
2021
+ rb_ractor_local_storage_value_set(dummy_img_key, dummy_img);
2022
+ }
2023
+ #endif
1649
2024
 
1650
2025
  return dummy_img;
1651
2026
  }
@@ -1674,7 +2049,8 @@ static VALUE
1674
2049
  get_type_metrics(int argc, VALUE *argv, VALUE self, gvl_function_t fp)
1675
2050
  {
1676
2051
  Image *image;
1677
- Draw *draw;
2052
+ MagickDraw *draw;
2053
+ DrawInfo *info;
1678
2054
  VALUE t;
1679
2055
  VALUE text_arg = Qnil;
1680
2056
  TypeMetric metrics;
@@ -1690,13 +2066,15 @@ get_type_metrics(int argc, VALUE *argv, VALUE self, gvl_function_t fp)
1690
2066
  case 1: // use default image
1691
2067
  text_arg = argv[0];
1692
2068
  text = rm_str2cstr(&text_arg, &text_l);
1693
- TypedData_Get_Struct(get_dummy_tm_img(CLASS_OF(self)), Image, &rm_image_data_type, image);
2069
+ t = get_dummy_tm_img();
2070
+ image = rm_check_readable(t);
1694
2071
  break;
1695
2072
  case 2:
1696
2073
  t = rm_cur_image(argv[0]);
1697
2074
  image = rm_check_readable(t);
1698
2075
  text_arg = argv[1];
1699
2076
  text = rm_str2cstr(&text_arg, &text_l);
2077
+ image = rm_check_readable(t);
1700
2078
  break; // okay
1701
2079
  default:
1702
2080
  rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 2)", argc);
@@ -1709,26 +2087,25 @@ get_type_metrics(int argc, VALUE *argv, VALUE self, gvl_function_t fp)
1709
2087
  }
1710
2088
 
1711
2089
  draw = get_draw(self);
1712
- rm_gvl_check_writable(draw);
2090
+ rm_gvl_check_readable(self);
1713
2091
  // Measured as given: see the comment in Draw_annotate().
1714
- draw->info->text = ConstantString(text);
2092
+ info = CloneDrawInfo(NULL, draw->info);
2093
+ magick_clone_string(&info->text, text);
1715
2094
 
1716
2095
  #if defined(IMAGEMAGICK_7)
1717
2096
  exception = AcquireExceptionInfo();
1718
- DECLARE_GVL_CALL_FP(call, get_type_metrics, fp, image, draw->info, &metrics, exception);
2097
+ DECLARE_GVL_CALL_FP(call, get_type_metrics, fp, image, info, &metrics, exception);
1719
2098
  call.release(exception);
1720
2099
  #else
1721
- DECLARE_GVL_CALL_FP(call, get_type_metrics, fp, image, draw->info, &metrics);
2100
+ DECLARE_GVL_CALL_FP(call, get_type_metrics, fp, image, info, &metrics);
1722
2101
  #endif
1723
2102
  if (argc == 2)
1724
2103
  {
1725
2104
  call.read(t);
1726
2105
  }
1727
- void *ret = call.update(self).restore(free_text, draw).run<void *>();
2106
+ void *ret = mark_draw(call, self, draw).release(info).run<void *>();
1728
2107
  okay = static_cast<MagickBooleanType>(reinterpret_cast<intptr_t &>(ret));
1729
-
1730
- magick_free(draw->info->text);
1731
- draw->info->text = NULL;
2108
+ DestroyDrawInfo(info);
1732
2109
 
1733
2110
  if (!okay)
1734
2111
  {