forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcustomprinter.cpp
More file actions
2171 lines (1808 loc) · 66.7 KB
/
customprinter.cpp
File metadata and controls
2171 lines (1808 loc) · 66.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include "prefix.h"
#include "globdefs.h"
#include "filedefs.h"
#include "objdefs.h"
#include "parsedef.h"
#include "osspec.h"
#include "uidc.h"
#include "mcerror.h"
#include "globals.h"
#include "exec.h"
#include "metacontext.h"
#include "printer.h"
#include "customprinter.h"
#include "license.h"
#include "path.h"
#include "pathprivate.h"
#include "gradient.h"
#include "textlayout.h"
#include "region.h"
#include "graphicscontext.h"
#include "font.h"
#include "graphics_util.h"
#ifdef _LINUX_DESKTOP
#include "flst.h"
#endif
////////////////////////////////////////////////////////////////////////////////
static bool MCCustomPrinterBlendModeFromInk(uint1 p_function, MCCustomPrinterBlendMode& r_mode)
{
switch(p_function)
{
case GXcopy: r_mode = kMCCustomPrinterBlendSrcOver; break;
case GXblendSrc: r_mode = kMCCustomPrinterBlendSrc; break;
case GXblendDst: r_mode = kMCCustomPrinterBlendDst; break;
case GXblendSrcOver: r_mode = kMCCustomPrinterBlendSrcOver; break;
case GXblendDstOver: r_mode = kMCCustomPrinterBlendDstOver; break;
case GXblendSrcIn: r_mode = kMCCustomPrinterBlendSrcIn; break;
case GXblendDstIn: r_mode = kMCCustomPrinterBlendDstIn; break;
case GXblendSrcOut: r_mode = kMCCustomPrinterBlendSrcOut; break;
case GXblendDstOut: r_mode = kMCCustomPrinterBlendDstOut; break;
case GXblendSrcAtop: r_mode = kMCCustomPrinterBlendSrcAtop; break;
case GXblendDstAtop: r_mode = kMCCustomPrinterBlendDstAtop; break;
case GXblendXor: r_mode = kMCCustomPrinterBlendXor; break;
case GXblendPlus: r_mode = kMCCustomPrinterBlendPlus; break;
case GXblendMultiply: r_mode = kMCCustomPrinterBlendMultiply; break;
case GXblendScreen: r_mode = kMCCustomPrinterBlendScreen; break;
case GXblendOverlay: r_mode = kMCCustomPrinterBlendOverlay; break;
case GXblendDarken: r_mode = kMCCustomPrinterBlendDarken; break;
case GXblendLighten: r_mode = kMCCustomPrinterBlendLighten; break;
case GXblendDodge: r_mode = kMCCustomPrinterBlendDodge; break;
case GXblendBurn: r_mode = kMCCustomPrinterBlendBurn; break;
case GXblendHardLight: r_mode = kMCCustomPrinterBlendHardLight; break;
case GXblendSoftLight: r_mode = kMCCustomPrinterBlendSoftLight; break;
case GXblendDifference: r_mode = kMCCustomPrinterBlendDifference; break;
case GXblendExclusion: r_mode = kMCCustomPrinterBlendExclusion; break;
default:
return false;
}
return true;
}
static bool MCCustomPrinterGradientFromMCGradient(MCGradientFillKind p_kind, MCCustomPrinterGradientType& r_type)
{
switch(p_kind)
{
case kMCGradientKindLinear: r_type = kMCCustomPrinterGradientLinear; break;
case kMCGradientKindRadial: r_type = kMCCustomPrinterGradientRadial; break;
case kMCGradientKindConical: r_type = kMCCustomPrinterGradientConical; break;
case kMCGradientKindDiamond: r_type = kMCCustomPrinterGradientDiamond; break;
case kMCGradientKindSpiral: r_type = kMCCustomPrinterGradientSpiral; break;
case kMCGradientKindXY: r_type = kMCCustomPrinterGradientXY; break;
case kMCGradientKindSqrtXY: r_type = kMCCustomPrinterGradientSqrtXY; break;
default:
return false;
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
static MCCustomPrinterTransform MCCustomPrinterTransformFromMCGAffineTransform(const MCGAffineTransform &p_transform)
{
MCCustomPrinterTransform t_transform;
t_transform.scale_x = p_transform.a;
t_transform.skew_x = p_transform.c;
t_transform.translate_x = p_transform.tx;
t_transform.skew_y = p_transform.b;
t_transform.scale_y = p_transform.d;
t_transform.translate_y = p_transform.ty;
return t_transform;
}
////////////////////////////////////////////////////////////////////////////////
bool MCCustomPrinterImageFromMCGImage(MCGImageRef p_image, MCCustomPrinterImage &r_image, void *&r_pixel_cache)
{
MCGRaster t_raster;
if (!MCGImageGetRaster(p_image, t_raster))
return false;
void *t_pixels;
t_pixels = nil;
void *t_pixel_cache;
t_pixel_cache = nil;
#if kMCCustomPrinterImagePixelFormat == kMCGPixelFormatNative
t_pixels = t_raster.pixels;
#else
if (!MCMemoryAllocate(t_raster.stride * t_raster.height, t_pixel_cache))
return false;
uint8_t *t_src;
t_src = (uint8_t*)t_raster.pixels;
uint8_t *t_dst;
t_dst = (uint8_t*)t_pixel_cache;
for (uint32_t i = 0; i < t_raster.height; i++)
{
uint32_t *t_src_row;
t_src_row = (uint32_t*)t_src;
uint32_t *t_dst_row;
t_dst_row = (uint32_t*)t_dst;
for (uint32_t j = 0; j < t_raster.width; j++)
*t_dst_row++ = MCGPixelFromNative(kMCCustomPrinterImagePixelFormat, *t_src_row++);
t_src += t_raster.stride;
t_dst += t_raster.stride;
}
t_pixels = t_pixel_cache;
#endif
// Fill in the printer image info
r_image.width = t_raster.width;
r_image.height = t_raster.height;
bool t_mask, t_alpha;
t_mask = !MCGImageIsOpaque(p_image);
t_alpha = t_mask && MCGImageHasPartialTransparency(p_image);
// IM-2014-06-26: [[ Bug 12699 ]] Set image type appropriately.
r_image . type = t_alpha ? kMCCustomPrinterImageRawARGB : (t_mask ? kMCCustomPrinterImageRawMRGB : kMCCustomPrinterImageRawXRGB);
r_image . id = (uint32_t)(intptr_t)p_image;
r_image . data = t_pixels;
r_image . data_size = t_raster.stride * t_raster.height;
r_pixel_cache = t_pixel_cache;
return true;
}
class MCCustomMetaContext: public MCMetaContext
{
public:
MCCustomMetaContext(const MCRectangle& page);
~MCCustomMetaContext(void);
bool render(MCCustomPrintingDevice *p_device, const MCPrinterRectangle& p_src_rect, const MCPrinterRectangle& p_dst_rect, const MCPrinterRectangle& p_page_rect);
protected:
bool candomark(MCMark *mark);
void domark(MCMark *mark);
bool begincomposite(const MCRectangle ®ion, MCGContextRef &r_context);
MCContext *begincomposite(const MCRectangle& region);
void endcomposite(MCRegionRef clip_region);
void dopathmark(MCMark *mark, MCPath *path);
void dorawpathmark(MCMark *mark, uint1 *commands, uint32_t command_count, int4 *ordinates, uint32_t ordinate_count, bool p_evenodd);
void dotextmark(MCMark *mark);
void doimagemark(MCMark *mark);
void dolinkmark(MCMark *mark);
private:
void transform_point(const MCPoint& in_point, MCCustomPrinterPoint& r_out_point);
void transform_rect(const MCRectangle& in_rect, MCCustomPrinterRectangle& r_out_rect);
void compute_clip(const MCRectangle& clip_rect, MCCustomPrinterRectangle& r_out_rect);
// The device we are targetting (only valid during the 'render' method).
MCCustomPrintingDevice *m_device;
// These are the transform factors to map the src to dst rect
double m_scale_x, m_scale_y;
double m_translate_x, m_translate_y;
// This is the rectangle that defines the maximum area in device space that
// can be used.
MCPrinterRectangle m_page_rect;
// If this is true, an error has occurred during execute
bool m_execute_error;
// The untransformed rect and scale of current composite region
MCRectangle m_composite_rect;
uint32_t m_composite_scale;
// The compositing graphics context used to render into
MCGContextRef m_composite_context;
};
MCCustomMetaContext::MCCustomMetaContext(const MCRectangle& p_page)
: MCMetaContext(p_page)
{
m_composite_context = nil;
}
MCCustomMetaContext::~MCCustomMetaContext(void)
{
MCGContextRelease(m_composite_context);
}
bool MCCustomMetaContext::render(MCCustomPrintingDevice *p_device, const MCPrinterRectangle& p_src_rect, const MCPrinterRectangle& p_dst_rect, const MCPrinterRectangle& p_page_rect)
{
m_execute_error = false;
m_device = p_device;
m_scale_x = (p_dst_rect . right - p_dst_rect . left) / (p_src_rect . right - p_src_rect . left);
m_scale_y = (p_dst_rect . bottom - p_dst_rect . top) / (p_src_rect . bottom - p_src_rect . top);
m_translate_x = -p_src_rect . left * m_scale_x + p_dst_rect . left;
m_translate_y = -p_src_rect . top * m_scale_y + p_dst_rect . top;
m_page_rect = p_page_rect;
// Run the display list
execute();
return !m_execute_error;
}
bool MCCustomMetaContext::candomark(MCMark *p_mark)
{
// If an error has occurred during this execution, just return true to minimize
// unnecessary rasterization (this is to make up for a lack of error handling
// during the super-classes execution process).
if (m_execute_error)
return true;
// This method is called for every mark to see if it needs rasterization.
switch(p_mark -> type)
{
case MARK_TYPE_LINE:
case MARK_TYPE_POLYGON:
case MARK_TYPE_TEXT:
case MARK_TYPE_RECTANGLE:
case MARK_TYPE_ROUND_RECTANGLE:
case MARK_TYPE_ARC:
case MARK_TYPE_PATH:
{
MCCustomPrinterPaint t_paint;
// Check to see if its a gradient
if (p_mark -> fill -> gradient != nil)
{
if (!MCCustomPrinterGradientFromMCGradient((MCGradientFillKind)p_mark -> fill -> gradient -> kind, t_paint . gradient . type))
return false;
t_paint . type = kMCCustomPrinterPaintGradient;
return m_device -> CanRenderPaint(t_paint);
}
// If its a solid color we are done
if (p_mark -> fill -> style == FillSolid)
return true;
// Check to see if its a pattern
if (p_mark -> fill -> style == FillTiled)
{
t_paint . type = kMCCustomPrinterPaintPattern;
t_paint . pattern . image . type = kMCCustomPrinterImageRawARGB;
return m_device -> CanRenderPaint(t_paint);
}
// Otherwise it is FillStippled or FillOpaqueStippled - neither of which
// we support for this kind of printing (yet?)
return true;
}
case MARK_TYPE_IMAGE:
{
// Devices have to support unmasked images (otherwise we couldn't
// rasterize!).
bool t_mask, t_alpha;
t_mask = !MCGImageIsOpaque(p_mark->image.descriptor.image);
t_alpha = t_mask && MCGImageHasPartialTransparency(p_mark->image.descriptor.image);
if (!t_mask)
return true;
// Now check to see if the appropriate type of masked (raw) image
// is supported.
MCCustomPrinterImage t_image;
if (t_alpha)
t_image . type = kMCCustomPrinterImageRawARGB;
else
t_image . type = kMCCustomPrinterImageRawMRGB;
return m_device -> CanRenderImage(t_image);
}
case MARK_TYPE_METAFILE:
case MARK_TYPE_EPS:
case MARK_TYPE_THEME:
// These all have to be rasterized!
return false;
case MARK_TYPE_GROUP:
{
// If the group has effects we can't do it (yet!)
if (p_mark -> group . effects != nil)
return false;
// If the blend mode isn't a custom printer supported one, we can't
// do it.
MCCustomPrinterBlendMode t_blend_mode;
if (!MCCustomPrinterBlendModeFromInk(p_mark -> group . function, t_blend_mode))
return false;
// Otherwise fill in the custom group struct and ask our device
MCCustomPrinterGroup t_group;
t_group . blend_mode = t_blend_mode;
t_group . opacity = p_mark -> group . opacity / 255.0;
return m_device -> CanRenderGroup(t_group);
}
case MARK_TYPE_LINK:
// We can always render links natively - even if this is a no-op.
return true;
case MARK_TYPE_END:
// Unknown mark so return false.
return false;
}
MCUnreachableReturn(false);
}
void MCCustomMetaContext::domark(MCMark *p_mark)
{
// If an error has occurred, we do nothing.
if (m_execute_error)
return;
switch(p_mark -> type)
{
case MARK_TYPE_LINE:
{
MCPath *t_path;
t_path = MCPath::create_line(p_mark -> line . start . x, p_mark -> line . start . y, p_mark -> line . end . x, p_mark -> line . end . y, true);
if (t_path != nil)
{
dopathmark(p_mark, t_path);
t_path -> release();
}
else
m_execute_error = true;
}
break;
case MARK_TYPE_POLYGON:
{
MCPath *t_path;
if (p_mark -> polygon . closed)
t_path = MCPath::create_polygon(p_mark -> polygon . vertices, p_mark -> polygon . count, true);
else
t_path = MCPath::create_polyline(p_mark -> polygon . vertices, p_mark -> polygon . count, true);
if (t_path != nil)
{
dopathmark(p_mark, t_path);
t_path -> release();
}
else
m_execute_error = true;
}
break;
case MARK_TYPE_TEXT:
dotextmark(p_mark);
break;
case MARK_TYPE_RECTANGLE:
{
// MM-2014-04-23: [[ Bug 11884 ]] Inset the bounds. Since MCPath only accepts ints, if the inset value is uneven,
// round up to the nearest even value, keeping behaviour as close to that of the graphics context as possible.
// SN-2014-10-17: [[ Bug 13351 ]] Only round up existing inset
if (p_mark -> rectangle . inset && !(p_mark -> rectangle . inset % 2))
p_mark -> rectangle . inset ++;
// SN-2014-10-17: [[ Bug 13351 ]] Be careful not to underflow the bounds
p_mark -> rectangle . bounds = MCRectangleMake(p_mark -> rectangle . bounds . x + p_mark -> rectangle . inset / 2,
p_mark -> rectangle . bounds . y + p_mark -> rectangle . inset / 2,
MCMin(p_mark -> rectangle . bounds . width, p_mark -> rectangle . bounds . width - p_mark -> rectangle . inset),
MCMin(p_mark -> rectangle . bounds . height, p_mark -> rectangle . bounds . height - p_mark -> rectangle . inset));
MCPath *t_path;
if (p_mark -> stroke != nil && p_mark -> rectangle . bounds . height == 1)
t_path = MCPath::create_line(p_mark -> rectangle . bounds . x, p_mark -> rectangle . bounds . y, p_mark -> rectangle . bounds . x + p_mark -> rectangle . bounds . width - 1, p_mark -> rectangle . bounds . y, true);
else if (p_mark -> stroke != nil && p_mark -> rectangle . bounds . width == 1)
t_path = MCPath::create_line(p_mark -> rectangle . bounds . x, p_mark -> rectangle . bounds . y, p_mark -> rectangle . bounds . x, p_mark -> rectangle . bounds . y + p_mark -> rectangle . bounds . height - 1, true);
else
t_path = MCPath::create_rectangle(p_mark -> rectangle . bounds, p_mark -> stroke != nil);
if (t_path != nil)
{
dopathmark(p_mark, t_path);
t_path -> release();
}
else
m_execute_error = true;
}
break;
case MARK_TYPE_ROUND_RECTANGLE:
{
// MM-2014-04-23: [[ Bug 11884 ]] Inset the bounds. Since MCPath only accepts ints, if the inset value is uneven,
// round up to the nearest even value, keeping behaviour as close to that of the graphics context as possible.
// SN-2014-10-17: [[ Bug 13351 ]] Only round up existing inset
if (!(p_mark -> round_rectangle . inset % 2))
p_mark -> round_rectangle . inset ++;
// SN-2014-10-17: [[ Bug 13351 ]] Be careful not to underflow the bounds
p_mark -> round_rectangle . bounds = MCRectangleMake(p_mark -> round_rectangle . bounds . x + p_mark -> round_rectangle . inset / 2,
p_mark -> round_rectangle . bounds . y + p_mark -> round_rectangle . inset / 2,
MCMin(p_mark -> round_rectangle . bounds . width, p_mark -> round_rectangle . bounds . width - p_mark -> round_rectangle . inset),
MCMin(p_mark -> round_rectangle . bounds . height, p_mark -> round_rectangle . bounds . height - p_mark -> round_rectangle . inset));
MCPath *t_path;
t_path = MCPath::create_rounded_rectangle(p_mark -> round_rectangle . bounds, p_mark -> round_rectangle . radius / 2, p_mark -> stroke != nil);
if (t_path != nil)
{
dopathmark(p_mark, t_path);
t_path -> release();
}
else
m_execute_error = true;
}
break;
case MARK_TYPE_ARC:
{
// MM-2014-04-23: [[ Bug 11884 ]] Inset the bounds. Since MCPath only accepts ints, if the inset value is uneven,
// round up to the nearest even value, keeping behaviour as close to that of the graphics context as possible.
// SN-2014-10-17: [[ Bug 13351 ]] Only round up existing inset
if (!(p_mark -> arc . inset % 2))
p_mark -> arc . inset ++;
// SN-2014-10-17: [[ Bug 13351 ]] Be careful not to underflow the bounds
p_mark -> arc . bounds = MCRectangleMake(p_mark -> arc . bounds . x + p_mark -> arc . inset / 2,
p_mark -> arc . bounds . y + p_mark -> arc . inset / 2,
MCMin(p_mark -> arc . bounds . width, p_mark -> arc . bounds . width - p_mark -> arc . inset),
MCMin(p_mark -> arc . bounds . height, p_mark -> arc . bounds . height - p_mark -> arc . inset));
MCPath *t_path;
if (p_mark -> arc . complete)
t_path = MCPath::create_segment(p_mark -> arc . bounds, p_mark -> arc . start, p_mark -> arc . angle, p_mark -> stroke != nil);
else
t_path = MCPath::create_arc(p_mark -> arc . bounds, p_mark -> arc . start, p_mark -> arc . angle, p_mark -> stroke != nil);
if (t_path != nil)
{
dopathmark(p_mark, t_path);
t_path -> release();
}
else
m_execute_error = true;
}
break;
case MARK_TYPE_PATH:
dorawpathmark(p_mark, p_mark -> path . commands, p_mark -> path . command_count, p_mark -> path . ordinates, p_mark -> path . ordinate_count, p_mark -> path . evenodd);
break;
case MARK_TYPE_IMAGE:
doimagemark(p_mark);
break;
case MARK_TYPE_GROUP:
{
// Work out the custom group properties
MCCustomPrinterGroup t_group;
MCCustomPrinterBlendModeFromInk(p_mark -> group . function, t_group . blend_mode);
t_group . opacity = p_mark -> group . opacity / 255.0;
compute_clip(p_mark -> clip, t_group . region);
// If the opacity if not 1, or the blend mode is not srcOver then we
// generate a group. Otherwise, there's no need.
if (t_group . opacity != 1.0 || t_group . blend_mode != kMCCustomPrinterBlendSrcOver)
{
if (!m_execute_error &&
!m_device -> BeginGroup(t_group))
m_execute_error = true;
if (!m_execute_error)
executegroup(p_mark);
if (!m_execute_error &&
!m_device -> EndGroup())
m_execute_error = true;
}
else
executegroup(p_mark);
}
break;
case MARK_TYPE_LINK:
dolinkmark(p_mark);
break;
default:
break;
}
}
void MCCustomMetaContext::doimagemark(MCMark *p_mark)
{
// See if we can render the image using the original text
MCCustomPrinterImageType t_image_type;
t_image_type = kMCCustomPrinterImageNone;
if (p_mark -> image . descriptor . data_type != kMCImageDataNone)
{
switch(p_mark -> image . descriptor . data_type)
{
case kMCImageDataGIF: t_image_type = kMCCustomPrinterImageGIF; break;
case kMCImageDataPNG: t_image_type = kMCCustomPrinterImagePNG; break;
case kMCImageDataJPEG: t_image_type = kMCCustomPrinterImageJPEG; break;
default: assert(false);
}
MCCustomPrinterImage t_image;
t_image . type = t_image_type;
if (!m_device -> CanRenderImage(t_image))
t_image_type = kMCCustomPrinterImageNone;
}
void *t_pixel_cache;
t_pixel_cache = nil;
// Fill in the printer image info
MCCustomPrinterImage t_image;
if (!m_execute_error)
{
if (t_image_type == kMCCustomPrinterImageNone)
{
if (!MCCustomPrinterImageFromMCGImage(p_mark -> image . descriptor . image, t_image, t_pixel_cache))
m_execute_error = true;
}
else
{
t_image . type = t_image_type;
t_image . id = (uint32_t)(intptr_t)p_mark -> image . descriptor . data_bits;
t_image . data = p_mark -> image . descriptor . data_bits;
t_image . data_size = p_mark -> image . descriptor . data_size;
t_image . width = MCGImageGetWidth(p_mark -> image . descriptor . image);
t_image . height = MCGImageGetHeight(p_mark -> image . descriptor . image);
}
}
if (!m_execute_error)
{
// Compute the transform that is needed - this transform goes from image
// space to page space.
// IM-2014-06-26: [[ Bug 12699 ]] Rework to ensure transforms are applied in the correct order - page transform -> image offset -> image transform
MCGAffineTransform t_transform;
t_transform = MCGAffineTransformMake(m_scale_x, 0, 0, m_scale_y, m_translate_x, m_translate_y);
t_transform = MCGAffineTransformConcat(t_transform, MCGAffineTransformMakeTranslation(p_mark -> image . dx - p_mark -> image . sx, p_mark -> image . dy - p_mark -> image . sy));
if (p_mark -> image . descriptor . has_transform)
t_transform = MCGAffineTransformConcat(t_transform, p_mark -> image . descriptor . transform);
// Compute the clip that is needed - the mark alreay has the appropriate clip set.
MCCustomPrinterRectangle t_clip;
compute_clip(p_mark -> clip, t_clip);
// Render the primitive
if (!m_device -> DrawImage(t_image, MCCustomPrinterTransformFromMCGAffineTransform(t_transform), t_clip))
m_execute_error = true;
}
if (t_pixel_cache != nil)
MCMemoryDeallocate(t_pixel_cache);
}
void MCCustomMetaContext::dolinkmark(MCMark *p_mark)
{
MCCustomPrinterRectangle t_region;
compute_clip(p_mark -> link . region, t_region);
if (!m_device -> MakeLink(t_region, p_mark -> link . text, kMCCustomPrinterLinkUnspecified))
m_execute_error = true;
}
#define SCALE 4
bool MCCustomMetaContext::begincomposite(const MCRectangle &p_mark_clip, MCGContextRef &r_context)
{
bool t_success = true;
MCGContextRef t_gcontext = nil;
// TODO: Make the rasterization scale depend in some way on current scaling,
// after all there's no point in making a large rasterized bitmap if it ends
// up being printed really really small!
uint4 t_scale;
t_scale = SCALE;
uint32_t t_width = p_mark_clip.width * t_scale;
uint32_t t_height = p_mark_clip.height * t_scale;
if (t_success)
t_success = MCGContextCreate(t_width, t_height, true, t_gcontext);
if (t_success)
{
MCGContextScaleCTM(t_gcontext, t_scale, t_scale);
MCGContextTranslateCTM(t_gcontext, -(MCGFloat)p_mark_clip . x, -(MCGFloat)p_mark_clip . y);
m_composite_context = t_gcontext;
m_composite_rect = p_mark_clip;
m_composite_scale = t_scale;
r_context = m_composite_context;
}
m_execute_error = !t_success;
return t_success;
}
static void surface_merge_with_mask_preserve(void *p_pixels, uint4 p_pixel_stride, void *p_mask, uint4 p_mask_stride, uint4 p_offset, uint4 p_width, uint4 p_height)
{
uint4 *t_pixel_ptr;
uint4 t_pixel_stride;
uint1 *t_mask_ptr;
uint4 t_mask_stride;
t_pixel_ptr = (uint4 *)p_pixels;
t_pixel_stride = p_pixel_stride >> 2;
t_mask_ptr = (uint1 *)p_mask;
t_mask_stride = p_mask_stride;
for(uint4 y = p_height; y > 0; --y, t_pixel_ptr += t_pixel_stride, t_mask_ptr += t_mask_stride)
{
uint4 t_byte, t_bit;
uint1 *t_bytes;
t_bytes = t_mask_ptr;
t_bit = 0x80 >> p_offset;
t_byte = *t_bytes++;
for(uint4 x = 0; x < p_width; ++x)
{
t_pixel_ptr[x] = t_pixel_ptr[x] & 0xFFFFFF;
if ((t_byte & t_bit) != 0)
t_pixel_ptr[x] |= 0xFF000000;
t_bit >>= 1;
if (!t_bit && x < p_width - 1)
t_bit = 0x80, t_byte = *t_bytes++;
}
}
}
void MCCustomMetaContext::endcomposite(MCRegionRef p_clip_region)
{
bool t_success = true;
MCGImageRef t_image;
t_image = nil;
t_success = nil != m_composite_context;
if (t_success)
t_success = MCGContextCopyImage(m_composite_context, t_image);
MCGContextRelease(m_composite_context);
m_composite_context = nil;
void *t_pixel_cache;
t_pixel_cache = nil;
if (t_success)
{
/* OVERHAUL - REVISIT: Disabling the mask stuff for now, just treat the composite image as ARGB */
// Make sure the region is in logical coords.
//MCRegionOffset(p_clip_region, -(signed)m_composite_rect . x * m_composite_scale, -(signed)m_composite_rect . y * m_composite_scale);
// We now need to merge the region into the surface as a 1-bit mask... So first
// get the region as such a mask
//MCBitmap *t_mask;
//if (MCRegionCalculateMask(p_clip_region, t_image -> width, t_image -> height, t_mask))
//{
// // And then merge in the mask - note we preserve the pixel data of the pixels
// // behind the mask... This is to eliminate rendering artifacts when the image
// // is resampled for display... Hmm - although the artifacts may be a cairo
// // problem - doing this doesn't seem to solve the issue!
// surface_merge_with_mask_preserve(t_image -> data, t_image -> bytes_per_line, t_mask -> data, t_mask -> bytes_per_line, 0, t_image -> width, t_image -> height);
// Now we have a masked image, issue an appropriate image rendering call to the
// device
MCCustomPrinterImage t_img_data;
t_success = MCCustomPrinterImageFromMCGImage(t_image, t_img_data, t_pixel_cache);
MCCustomPrinterTransform t_img_transform;
t_img_transform . scale_x = m_scale_x / m_composite_scale;
t_img_transform . scale_y = m_scale_y / m_composite_scale;
t_img_transform . skew_x = 0.0;
t_img_transform . skew_y = 0.0;
t_img_transform . translate_x = m_scale_x * m_composite_rect . x + m_translate_x;
t_img_transform . translate_y = m_scale_y * m_composite_rect . y + m_translate_y;
MCCustomPrinterRectangle t_img_clip;
compute_clip(m_composite_rect, t_img_clip);
t_success = m_device -> DrawImage(t_img_data, t_img_transform, t_img_clip);
// Destroy our mask image
// MCscreen -> destroyimage(t_mask);
//}
//else
// m_execute_error = true;
}
if (t_image != nil)
MCGImageRelease(t_image);
if (t_pixel_cache != nil)
MCMemoryDeallocate(t_pixel_cache);
m_execute_error = !t_success;
// Delete the region
MCRegionDestroy(p_clip_region);
}
void MCCustomMetaContext::dopathmark(MCMark *p_mark, MCPath *p_path)
{
uint32_t t_command_count, t_ordinate_count;
p_path -> get_lengths(t_command_count, t_ordinate_count);
dorawpathmark(p_mark, p_path -> get_commands(), t_command_count, p_path -> get_ordinates(), t_ordinate_count, false);
}
void MCCustomMetaContext::dorawpathmark(MCMark *p_mark, uint1 *p_commands, uint32_t p_command_count, int4 *p_ordinates, uint32_t p_ordinate_count, bool p_evenodd)
{
MCCustomPrinterPathCommand *t_out_commands;
t_out_commands = new (nothrow) MCCustomPrinterPathCommand[p_command_count];
MCCustomPrinterPoint *t_out_coords;
t_out_coords = new (nothrow) MCCustomPrinterPoint[p_ordinate_count];
if (t_out_commands != nil && t_out_coords != nil)
{
for(uint32_t i = 0, j = 0; i < p_command_count; i++)
switch(p_commands[i])
{
case PATH_COMMAND_END:
t_out_commands[i] = kMCCustomPrinterPathEnd;
break;
case PATH_COMMAND_MOVE_TO:
t_out_commands[i] = kMCCustomPrinterPathMoveTo;
t_out_coords[j] . x = p_ordinates[j * 2] / 256.0;
t_out_coords[j] . y = p_ordinates[j * 2 + 1] / 256.0;
j++;
break;
case PATH_COMMAND_LINE_TO:
t_out_commands[i] = kMCCustomPrinterPathLineTo;
t_out_coords[j] . x = p_ordinates[j * 2] / 256.0;
t_out_coords[j] . y = p_ordinates[j * 2 + 1] / 256.0;
j++;
break;
case PATH_COMMAND_CUBIC_TO:
t_out_commands[i] = kMCCustomPrinterPathCubicTo;
t_out_coords[j] . x = p_ordinates[j * 2] / 256.0;
t_out_coords[j] . y = p_ordinates[j * 2 + 1] / 256.0;
j++;
t_out_coords[j] . x = p_ordinates[j * 2] / 256.0;
t_out_coords[j] . y = p_ordinates[j * 2 + 1] / 256.0;
j++;
t_out_coords[j] . x = p_ordinates[j * 2] / 256.0;
t_out_coords[j] . y = p_ordinates[j * 2 + 1] / 256.0;
j++;
break;
case PATH_COMMAND_QUADRATIC_TO:
t_out_commands[i] = kMCCustomPrinterPathQuadraticTo;
t_out_coords[j] . x = p_ordinates[j * 2] / 256.0;
t_out_coords[j] . y = p_ordinates[j * 2 + 1] / 256.0;
j++;
t_out_coords[j] . x = p_ordinates[j * 2] / 256.0;
t_out_coords[j] . y = p_ordinates[j * 2 + 1] / 256.0;
j++;
break;
case PATH_COMMAND_CLOSE:
t_out_commands[i] = kMCCustomPrinterPathClose;
break;
default:
break;
}
// First construct the printer path.
MCCustomPrinterPath t_path;
t_path . commands = t_out_commands;
t_path . coords = t_out_coords;
// Now construct the printer paint.
MCCustomPrinterPaint t_paint;
t_paint . type = kMCCustomPrinterPaintNone;
// This is a temporary array containing the gradient stops (if any).
MCCustomPrinterGradientStop *t_paint_stops;
t_paint_stops = nil;
MCGImageRef t_image;
t_image = nil;
void *t_pixel_cache;
t_pixel_cache = nil;
// Note we have to check the fill in this order since 'gradient' is not
// a fill style and is indicated by the gradient field not being nil.
if (p_mark -> fill -> gradient != nil)
{
t_paint . type = kMCCustomPrinterPaintGradient;
t_paint . gradient . mirror = p_mark -> fill -> gradient -> mirror != 0;
t_paint . gradient . wrap = p_mark -> fill -> gradient -> wrap != 0;
t_paint . gradient . repeat = p_mark -> fill -> gradient -> repeat;
MCCustomPrinterGradientFromMCGradient((MCGradientFillKind)p_mark->fill->gradient->kind, t_paint.gradient.type);
// compute the affine transform of the gradient from the origin/primary/secondary points.
t_paint . gradient . transform . scale_x = p_mark->fill->gradient->primary.x - p_mark->fill->gradient->origin.x;
t_paint . gradient . transform . scale_y = p_mark->fill->gradient->secondary.y - p_mark->fill->gradient->origin.y;
t_paint . gradient . transform . skew_x = p_mark->fill->gradient->secondary.x - p_mark->fill->gradient->origin.x;
t_paint . gradient . transform . skew_y = p_mark->fill->gradient->primary.y - p_mark->fill->gradient->origin.y;
t_paint . gradient . transform . translate_x = p_mark->fill->gradient->origin.x;
t_paint . gradient . transform . translate_y = p_mark->fill->gradient->origin.y;
// Map the paint stops appropriately
t_paint_stops = new (nothrow) MCCustomPrinterGradientStop[p_mark -> fill -> gradient -> ramp_length];
if (t_paint_stops != nil)
{
for(uint32_t i = 0; i < p_mark -> fill -> gradient -> ramp_length; i++)
{
t_paint_stops[i] . color . red = ((p_mark -> fill -> gradient -> ramp[i] . color >> 16) & 0xff) / 255.0;
t_paint_stops[i] . color . green = ((p_mark -> fill -> gradient -> ramp[i] . color >> 8) & 0xff) / 255.0;
t_paint_stops[i] . color . blue = (p_mark -> fill -> gradient -> ramp[i] . color & 0xff) / 255.0;
t_paint_stops[i] . alpha = (p_mark -> fill -> gradient -> ramp[i] . color >> 24) / 255.0;
t_paint_stops[i] . offset = p_mark -> fill -> gradient -> ramp[i] . offset / 65535.0;
}
t_paint . gradient . stops = t_paint_stops;
t_paint . gradient . stop_count = p_mark -> fill -> gradient -> ramp_length;
}
else
m_execute_error = true;
}
else if (p_mark -> fill -> style == FillSolid)
{
t_paint . type = kMCCustomPrinterPaintSolid;
t_paint . solid . red = p_mark -> fill -> colour . red / 65535.0;
t_paint . solid . green = p_mark -> fill -> colour . green / 65535.0;
t_paint . solid . blue = p_mark -> fill -> colour . blue / 65535.0;
}
else if (p_mark -> fill -> style == FillTiled)
{
// Fetch the size of the tile, and its data.
MCGAffineTransform t_transform;
// IM-2014-05-13: [[ HiResPatterns ]] Update pattern access to use lock function
if (MCPatternLockForContextTransform(p_mark->fill->pattern, MCGAffineTransformMakeIdentity(), t_image, t_transform))
{
MCGRaster t_tile_raster;
/* UNCHECKED */ MCGImageGetRaster(t_image, t_tile_raster);
t_transform = MCGAffineTransformPreTranslate(t_transform, p_mark->fill->origin.x, p_mark->fill->origin.y);
// Construct the paint pattern.
t_paint . type = kMCCustomPrinterPaintPattern;
t_paint . pattern . transform = MCCustomPrinterTransformFromMCGAffineTransform(t_transform);
if (!MCCustomPrinterImageFromMCGImage(t_image, t_paint . pattern . image, t_pixel_cache))
m_execute_error = true;
MCGImageRetain(t_image);
MCPatternUnlock(p_mark->fill->pattern, t_image);
}
else
m_execute_error = true;
}
if (!m_execute_error)
{
// Compute the transform for the path. The path must be transformed by
// the printing device because it needs to happen *after* a stroke has
// been applied.
MCCustomPrinterTransform t_transform;
t_transform . scale_x = m_scale_x;
t_transform . scale_y = m_scale_y;
t_transform . skew_x = 0.0;
t_transform . skew_y = 0.0;
t_transform . translate_x = m_translate_x;
t_transform . translate_y = m_translate_y;
// Compute the clip for the path - the clip is in page-space, so we
// transform here.
MCCustomPrinterRectangle t_clip;
compute_clip(p_mark -> clip, t_clip);
if (p_mark -> stroke == nil)
{
if (!m_device -> FillPath(t_path, p_evenodd ? kMCCustomPrinterFillEvenOdd : kMCCustomPrinterFillNonZero, t_paint, t_transform, t_clip))
m_execute_error = true;
}
else
{
MCCustomPrinterStroke t_stroke;
// MW-2010-02-01: [[ Bug 8564 ]] If the width of the stroke is zero width, then the stroke
// parameters are: thickness == 1, cap == butt, join == miter.
if (p_mark -> stroke -> width != 0)
{
t_stroke . thickness = p_mark -> stroke -> width;
t_stroke . cap_style = p_mark -> stroke -> cap == CapButt ? kMCCustomPrinterCapButt :
(p_mark -> stroke -> cap == CapRound ? kMCCustomPrinterCapRound :
kMCCustomPrinterCapSquare);
t_stroke . join_style = p_mark -> stroke -> join == JoinBevel ? kMCCustomPrinterJoinBevel :
(p_mark -> stroke -> join == JoinRound ? kMCCustomPrinterJoinRound :
kMCCustomPrinterJoinMiter);
t_stroke . miter_limit = p_mark -> stroke -> miter_limit;
}
else
{
t_stroke . thickness = 1.0;
t_stroke . cap_style = kMCCustomPrinterCapButt;
t_stroke . join_style = kMCCustomPrinterJoinMiter;
t_stroke . miter_limit = 10;
}
if (p_mark -> stroke -> dash . length != 0)
{
t_stroke . dash_count = p_mark -> stroke -> dash . length;
t_stroke . dash_offset = p_mark -> stroke -> dash . offset;
t_stroke . dashes = new (nothrow) double[p_mark -> stroke -> dash . length];
if (t_stroke . dashes != nil)
{
for(uint32_t i = 0; i < p_mark -> stroke -> dash . length; i++)
t_stroke . dashes[i] = p_mark -> stroke -> dash . data[i];
}
else
m_execute_error = true;
}
else
{
t_stroke . dashes = nil;
t_stroke . dash_count = 0;
t_stroke . dash_offset = 0.0;
}
if (!m_execute_error)
if (!m_device -> StrokePath(t_path, t_stroke, t_paint, t_transform, t_clip))
m_execute_error = true;
delete[] t_stroke . dashes;
}
}
if (t_paint_stops != nil)
delete[] t_paint_stops;
if (t_image != nil)
MCGImageRelease(t_image);
if (t_pixel_cache != nil)
MCMemoryDeallocate(t_pixel_cache);
}
else
m_execute_error = true;
delete[] t_out_coords;
delete[] t_out_commands;
}
//////////
struct dotextmark_callback_state
{
MCCustomPrintingDevice *device;
MCCustomPrinterPaint paint;
MCCustomPrinterTransform transform;