This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathblock.cpp
More file actions
executable file
·2357 lines (2028 loc) · 72 KB
/
block.cpp
File metadata and controls
executable file
·2357 lines (2028 loc) · 72 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 "parsedef.h"
#include "objdefs.h"
#include "mcio.h"
#include "globals.h"
#include "stack.h"
#include "field.h"
#include "image.h"
#include "paragraf.h"
#include "line.h"
#include "MCBlock.h"
#include "util.h"
#include "context.h"
#include "mctheme.h"
#include "font.h"
#include "path.h"
#include "segment.h"
#include "exec-interface.h"
#include "stackfileformat.h"
#define IMAGE_BLOCK_LEADING 2
static MCRectangle
MCBlockMakeRectangle(double x, double y,
double width, double height)
{
MCRectangle p_result;
p_result.x = int2(x);
p_result.y = int2(y);
p_result.width = uint2(width);
p_result.height = uint2(height);
return p_result;
}
// Default MCBlock constructor - makes a block with everything initialized
// to zero.
MCBlock::MCBlock(void)
{
parent = NULL;
flags = F_CLEAR|F_HAS_UNICODE;
atts = NULL;
m_index = m_size = 0;
width = 0;
opened = 0;
origin = 0;
tabpos = 0;
direction_level = 0;
// SN-2014-12-16: [[ Bug 14227 ]] Make sure to initialise the segment to nil.
segment = nil;
// MW-2012-02-14: [[ FontRefs ]] The font for the block starts off nil.
m_font = nil;
}
// MCBlock copy constructor - copy an existing block.
// Note that this copies everything except the linkage and opened state.
MCBlock::MCBlock(const MCBlock &bref) : MCDLlist(bref)
{
parent = bref.parent;
flags = bref.flags;
if (flags & F_HAS_ATTS)
{
atts = new (nothrow) Blockatts;
if (flags & F_HAS_COLOR)
{
atts->color = new (nothrow) MCColor;
*atts->color = *bref.atts->color;
}
if (flags & F_HAS_BACK_COLOR)
{
atts->backcolor = new (nothrow) MCColor;
*atts->backcolor = *bref.atts->backcolor;
}
// MW-2012-02-17: [[ SplitTextAttrs ]] Copy across the font attrs the other
// block has.
if ((flags & F_HAS_FNAME) != 0)
atts->fontname = MCValueRetain(bref.atts->fontname);
if ((flags & F_HAS_FSIZE) != 0)
atts -> fontsize = bref . atts -> fontsize;
if ((flags & F_HAS_FSTYLE) != 0)
atts -> fontstyle = bref . atts -> fontstyle;
atts->shift = bref.atts->shift;
// MW-2012-05-04: [[ Values ]] linkText / imageSource / metaData are now uniqued
// strings.
if (flags & F_HAS_LINK)
atts -> linktext = MCValueRetain(bref . atts -> linktext);
if (flags & F_HAS_IMAGE)
atts -> imagesource = MCValueRetain(bref . atts -> imagesource);
if (flags & F_HAS_METADATA)
atts -> metadata = MCValueRetain(bref . atts -> metadata);
}
else
atts = NULL;
m_index = bref.m_index;
m_size = bref.m_size;
width = 0;
opened = 0;
origin = 0;
tabpos = 0;
direction_level = bref.direction_level;
// SN-2014-12-16: [[ Bug 14227 ]] Make sure to initialise the segment to nil.
segment = nil;
// MW-2012-02-14: [[ FontRefs ]] The font for the block starts off nil.
m_font = nil;
}
// MCBlock deconstructor - frees all associated data structures.
MCBlock::~MCBlock()
{
while (opened)
close();
if (atts != NULL)
freeatts();
}
bool MCBlock::visit(MCObjectVisitorOptions p_options, uint32_t p_part, MCObjectVisitor *p_visitor)
{
return p_visitor -> OnBlock(this);
}
// IM-2016-07-06: [[ Bug 17690 ]] Test if block sizes or offsets require 32bit
// values to store (stack file format v8.1).
uint32_t MCBlock::getminimumstackfileversion(void)
{
// paragraph text is always unicode when saving as version 7.0 or greater.
// since we can't know which version will be used at this point, the
// best we can do is assume unicode text.
uint32_t t_index_size;
t_index_size = sizeof(unichar_t);
if (m_index * t_index_size > UINT16_MAX || m_size * t_index_size > UINT16_MAX)
return kMCStackFileFormatVersion_8_1;
else
return kMCStackFileFormatMinimumExportVersion;
}
// MW-2012-03-04: [[ StackFile5500 ]] If 'is_ext' is true then the record is an extended
// record.
IO_stat MCBlock::load(IO_handle stream, uint32_t version, bool is_ext)
{
IO_stat stat;
// MW-2012-03-04: [[ StackFile5500 ]] If this is an extended block, then work out
// where to skip to when all the attrs currently recognized have been read.
int64_t t_attr_end = 0;
if (is_ext)
{
// Read the size.
uint32_t t_size;
if ((stat = IO_read_uint2or4(&t_size, stream)) != IO_NORMAL)
return stat;
// The end is the current stream position + the size of the attrs.
t_attr_end = MCS_tell(stream) + t_size;
}
if ((stat = IO_read_uint4(&flags, stream)) != IO_NORMAL)
return checkloadstat(stat);
// MW-2012-03-04: [[ StackFile5500 ]] If this isn't an extended block, then strip the
// metadata flag.
if (!is_ext)
flags &= ~F_HAS_METADATA;
// MW-2012-01-26: [[ FlaggedField ]] Make sure we ignore the setting of FLAGGED flag.
flags &= ~F_FLAGGED;
if (atts == NULL)
atts = new (nothrow) Blockatts;
// MW-2012-02-17: [[ SplitTextAttrs ]] If the font flag is present, it means there
// is a font record to read.
if (flags & F_FONT)
{
if (version > kMCStackFileFormatVersion_1_3)
{
uint2 t_font_index;
if ((stat = IO_read_uint2(&t_font_index, stream)) != IO_NORMAL)
return checkloadstat(stat);
// MW-2012-02-17: [[ LogFonts ]] Map the font index we have to the font attrs.
// Note that we ignore the 'unicode' tag since that is encoded in flags.
MCNameRef t_fontname;
uint2 t_fontstyle, t_fontsize;
bool t_is_unicode;
MCLogicalFontTableLookup(t_font_index, t_fontname, t_fontstyle, t_fontsize, t_is_unicode);
// MW-2012-02-17: [[ SplitTextAttrs ]] Only set the font attrs if they are
// not inherited.
if (!getflag(F_INHERIT_FNAME))
atts->fontname = MCValueRetain(t_fontname);
if (!getflag(F_INHERIT_FSIZE))
atts -> fontsize = t_fontsize;
if (!getflag(F_INHERIT_FSTYLE))
atts -> fontstyle = t_fontstyle;
}
else
{
// MW-2012-02-17: [[ LogFonts ]] Read a nameref directly.
// MW-2013-11-19: [[ UnicodeFileFormat ]] This path only happens sfv < 1300
// so is legacy.
if ((stat = IO_read_nameref_legacy(atts->fontname, stream, false)) != IO_NORMAL)
return checkloadstat(stat);
if ((stat = IO_read_uint2(&atts->fontsize, stream)) != IO_NORMAL)
return checkloadstat(stat);
if ((stat = IO_read_uint2(&atts->fontstyle, stream)) != IO_NORMAL)
return checkloadstat(stat);
// MW-2012-02-17; [[ SplitTextAttrs ]] All the font attrs are set.
flags |= F_FATTR_MASK;
}
}
if (flags & F_HAS_COLOR)
{
atts->color = new (nothrow) MCColor;
if ((stat = IO_read_mccolor(*atts->color, stream)) != IO_NORMAL)
return checkloadstat(stat);
if (flags & F_HAS_COLOR_NAME)
{
// MW-2012-01-06: [[ Block Changes ]] We no longer use the color name
// so load, delete and unset the flag.
// MW-2013-11-19: [[ UnicodeFileFormat ]] The storage of this is ignored,
// so is legacy,
char *colorname;
if ((stat = IO_read_cstring_legacy(colorname, stream, 2)) != IO_NORMAL)
return checkloadstat(stat);
MCCStringFree(colorname);
flags &= ~F_HAS_COLOR_NAME;
}
}
if (flags & F_HAS_BACK_COLOR)
{
atts->backcolor = new (nothrow) MCColor;
if ((stat = IO_read_mccolor(*atts->backcolor, stream)) != IO_NORMAL)
return checkloadstat(stat);
if (version < kMCStackFileFormatVersion_2_0 || flags & F_HAS_BACK_COLOR_NAME)
{
// MW-2012-01-06: [[ Block Changes ]] We no longer use the backcolor name
// so load, delete and unset the flag.
// MW-2013-11-19: [[ UnicodeFileFormat ]] The storage of this is ignored,
// so is legacy,
if ((stat = IO_discard_cstring_legacy(stream, 2)) != IO_NORMAL)
return checkloadstat(stat);
flags &= ~F_HAS_BACK_COLOR_NAME;
}
}
if (flags & F_HAS_SHIFT)
if ((stat = IO_read_int2(&atts->shift, stream)) != IO_NORMAL)
return checkloadstat(stat);
// MW-2012-05-04: [[ Values ]] linkText / imageSource / metaData are now uniqued
// strings.
if (flags & F_HAS_LINK)
{
// MW-2013-11-19: [[ UnicodeFileFormat ]] If sfv >= 7000, use unicode.
if ((stat = IO_read_stringref_new(atts->linktext, stream, version >= kMCStackFileFormatVersion_7_0)) != IO_NORMAL)
return checkloadstat(stat);
/* UNCHECKED */ MCValueInterAndRelease(atts -> linktext, atts -> linktext);
}
if (flags & F_HAS_IMAGE)
{
// MW-2013-11-19: [[ UnicodeFileFormat ]] If sfv >= 7000, use unicode.
if ((stat = IO_read_stringref_new(atts->imagesource, stream, version >= kMCStackFileFormatVersion_7_0)) != IO_NORMAL)
return checkloadstat(stat);
/* UNCHECKED */ MCValueInterAndRelease(atts -> imagesource, atts -> imagesource);
}
// MW-2012-03-04: [[ StackFile5500 ]] If there is a metadata attr then read
// it in.
if (flags & F_HAS_METADATA)
{
// MW-2013-11-19: [[ UnicodeFileFormat ]] If sfv >= 7000, use unicode.
if ((stat = IO_read_stringref_new(atts->metadata, stream, version >= kMCStackFileFormatVersion_7_0)) != IO_NORMAL)
return checkloadstat(stat);
/* UNCHECKED */ MCValueInterAndRelease(atts -> metadata, atts -> metadata);
}
// MW-2012-03-04: [[ StackFile5500 ]] If this is an extended block, then skip
// to the end of the attrs record.
if (is_ext)
if ((stat = MCS_seek_set(stream, t_attr_end)) != IO_NORMAL)
return checkloadstat(stat);
// ***** IMPORTANT *****
// The "index" and "size" values loaded below are byte indices into the
// parent paragraph's text buffer and are almost certainly wrong if the
// paragraph contains any Unicode text.
//
// Things are this way to maintain file format compatibility with engines
// that do not support the full Unicode refactor.
//
// Helpfully, the paragraph loading code makes a SetRanges call to inform
// the block of the correct offsets as soon as it knows them.
// IM-2016-07-11: [[ Bug 17690 ]] change storage format for index & size from
// 16bit to 32bit in stack file format v8.1.
if (version >= kMCStackFileFormatVersion_8_1)
{
uint32_t t_index, t_size;
stat = IO_read_uint4(&t_index, stream);
if (stat != IO_NORMAL)
return checkloadstat(stat);
stat = IO_read_uint4(&t_size, stream);
if (stat != IO_NORMAL)
return checkloadstat(stat);
m_index = t_index;
m_size = t_size;
}
else
{
uint2 index, size;
if ((stat = IO_read_uint2(&index, stream)) != IO_NORMAL)
return checkloadstat(stat);
if ((stat = IO_read_uint2(&size, stream)) != IO_NORMAL)
return checkloadstat(stat);
m_index = index;
m_size = size;
}
// MW-2012-02-17: [[ SplitTextAttrs ]] Adjust the flags to their in-memory
// representation. We ditch F_FONT because it is superceeded by the HAS_F*
// flags.
if (getflag(F_FONT))
{
flags &= ~F_FONT;
flags = (flags & ~F_FATTR_MASK) | (~(flags & F_FATTR_MASK) & F_FATTR_MASK);
}
else
flags &= ~F_FATTR_MASK;
// MW-2012-02-17: [[ SplitTextAttrs ]] If the block has no atts flags set
// then clear the atts struct.
if ((flags & F_HAS_ATTS) == 0)
{
delete atts;
atts = nil;
}
return IO_NORMAL;
}
IO_stat MCBlock::save(IO_handle stream, uint4 p_part, uint32_t p_version)
{
IO_stat stat;
// MW-2012-03-04: [[ StackFile5500 ]] If the block has metadata and 5.5 stackfile
// format has been requested then this is an extended block.
bool t_is_ext;
if (p_version >= kMCStackFileFormatVersion_5_5 && getflag(F_HAS_METADATA))
t_is_ext = true;
else
t_is_ext = false;
// MW-2012-03-04: [[ StackFile5500 ]] If this is an extended block then use the EXT
// tag.
if ((stat = IO_write_uint1(t_is_ext ? OT_BLOCK_EXT : OT_BLOCK, stream)) != IO_NORMAL)
return stat;
// MW-2012-03-04: [[ StackFile5500 ]] If this is an extended block then write out
// the length of the attrs.
if (t_is_ext)
if ((stat = IO_write_uint2or4(measureattrs(p_version), stream)) != IO_NORMAL)
return stat;
uint4 oldflags = flags;
// MW-2012-01-06: [[ StackFile5500 ]] If this isn't an extended block, then remove
// the metadata flag.
if (!t_is_ext)
flags &= ~F_HAS_METADATA;
flags &= ~F_VISITED;
// MW-2012-01-26: [[ FlaggedField ]] Make sure we don't save the flagged flag.
flags &= ~F_FLAGGED;
// The "has unicode" flag depends on whether the paragraph is native
bool t_is_unicode;
if (p_version < kMCStackFileFormatVersion_7_0 && MCStringIsNative(parent->GetInternalStringRef()))
{
t_is_unicode = false;
flags &= ~F_HAS_UNICODE;
}
else
{
t_is_unicode = true;
flags |= F_HAS_UNICODE;
}
// SN-2014-12-04: [[ Bug 14149 ]] Add the F_HAS_TAB flag, for legacy saving
if (p_version < kMCStackFileFormatVersion_7_0)
{
if (segment && segment != segment -> next())
flags |= F_HAS_TAB;
}
// MW-2012-02-17: [[ SplitTextAttrs ]] If we have unicode, or one of the font attr are
// set then we must serialize a font.
bool t_need_font;
if ((flags & (F_FATTR_MASK | F_HAS_UNICODE)) != 0)
{
// Add in the font record flag.
flags |= F_FONT;
// Invert the font attr flags (from has to inherit).
flags = (flags & ~F_FATTR_MASK) | (~(flags & F_FATTR_MASK) & F_FATTR_MASK);
t_need_font = true;
}
else
t_need_font = false;
if ((stat = IO_write_uint4(flags, stream)) != IO_NORMAL)
return stat;
flags = oldflags;
// MW-2012-02-17: [[ SplitTextAttrs ]] If any one of the font attrs are set, or we
// are unicode we must serialize a font.
if (t_need_font)
{
// MW-2012-02-17: [[ SplitTextAttrs ]] Compute the attrs to write out.
MCNameRef t_fontname;
uint2 t_fontstyle, t_fontsize;
getfontattrs(nil, t_fontname, t_fontsize, t_fontstyle);
// MW-2012-02-17: [[ LogFonts ]] Map the font attrs to the appropriate font
// index and write.
uint2 t_font_index;
t_font_index = MCLogicalFontTableMap(t_fontname, t_fontstyle, t_fontsize, true);
if ((stat = IO_write_uint2(t_font_index, stream)) != IO_NORMAL)
return stat;
}
if (flags & F_HAS_COLOR)
if ((stat = IO_write_mccolor(*atts->color, stream)) != IO_NORMAL)
return stat;
if (flags & F_HAS_BACK_COLOR)
if ((stat = IO_write_mccolor(*atts->backcolor, stream)) != IO_NORMAL)
return stat;
if (flags & F_HAS_SHIFT)
if ((stat = IO_write_int2(atts->shift, stream)) != IO_NORMAL)
return stat;
// MW-2012-05-04: [[ Values ]] linkText / imageSource / metaData are now uniqued
// strings.
// MW-2013-11-19: [[ UnicodeFileFormat ]] If sfv >= 7000, use unicode.
if (flags & F_HAS_LINK)
if ((stat = IO_write_stringref_new(atts->linktext, stream, p_version >= kMCStackFileFormatVersion_7_0)) != IO_NORMAL)
return stat;
// MW-2013-11-19: [[ UnicodeFileFormat ]] If sfv >= 7000, use unicode.
if (flags & F_HAS_IMAGE)
if ((stat = IO_write_stringref_new(atts->imagesource, stream, p_version >= kMCStackFileFormatVersion_7_0)) != IO_NORMAL)
return stat;
// MW-2012-03-04: [[ StackFile5500 ]] If this is an extended block then emit the
// new attributes.
if (t_is_ext)
{
// MW-2013-11-19: [[ UnicodeFileFormat ]] If sfv >= 7000, use unicode.
if (flags & F_HAS_METADATA)
if ((stat = IO_write_stringref_new(atts -> metadata, stream, p_version >= kMCStackFileFormatVersion_7_0)) != IO_NORMAL)
return stat;
}
uint32_t t_index_size;
t_index_size = t_is_unicode ? sizeof(unichar_t) : sizeof(char_t);
// IM-2016-07-11: [[ Bug 17690 ]] change storage format for index & size from
// 16bit to 32bit in stack file format v8.1.
if (p_version >= kMCStackFileFormatVersion_8_1)
{
stat = IO_write_uint4(m_index * t_index_size, stream);
if (stat != IO_NORMAL)
return checkloadstat(stat);
stat = IO_write_uint4(m_size * t_index_size, stream);
if (stat != IO_NORMAL)
return checkloadstat(stat);
}
else
{
if ((stat = IO_write_uint2(m_index * t_index_size, stream)) != IO_NORMAL)
return stat;
if ((stat = IO_write_uint2(m_size * t_index_size, stream)) != IO_NORMAL)
return stat;
}
return IO_NORMAL;
}
void MCBlock::open(MCFontRef p_parent_font)
{
if (opened++ != 0)
return;
// MW-2012-02-14: [[ FontRefs ]] Map the font for the block.
mapfont(p_parent_font);
openimage();
width = 0;
}
void MCBlock::close()
{
if (opened == 0 || --opened != 0)
return;
closeimage();
// MW-2012-02-14: [[ FontRefs ]] Unmap the font for the block.
unmapfont();
}
// MW-2012-02-14: [[ FontRefs ]] This method computes the fontref for the block.
void MCBlock::mapfont(MCFontRef p_parent_font)
{
// If we have no font attrs set, then we just copy the parent font.
if (!hasfontattrs())
{
m_font = MCFontRetain(p_parent_font);
return;
}
// MW-2012-02-17: [[ SplitTextAttrs ]] Compute the attrs to write out. If we don't
// have all of the attrs, fetch the inherited ones.
MCNameRef t_textfont;
uint2 t_textstyle, t_textsize;
getfontattrs(nil, t_textfont, t_textsize, t_textstyle);
// Compute the font style from the text style.
MCFontStyle t_font_style;
t_font_style = MCFontStyleFromTextStyle(t_textstyle);
// If the parent font had printer metrics, then make sure we have them too.
if (MCFontHasPrinterMetrics(p_parent_font))
t_font_style |= kMCFontStylePrinterMetrics;
// Create the fontref.
/* UNCHECKED */ MCFontCreate(t_textfont, t_font_style, t_textsize, m_font);
}
// MW-2012-02-14: [[ FontRefs ]] This method delete the fontref associated with the block.
void MCBlock::unmapfont(void)
{
MCFontRelease(m_font);
m_font = nil;
}
// MW-2012-02-14: [[ FontRefs ]] This method updates the current fontref associated with the
// block, returning 'true' if it has changed.
bool MCBlock::recomputefonts(MCFontRef p_parent_font)
{
// If there are no font attributes, then we are updating to the parent font.
if (!hasfontattrs())
{
// If the font is the same as the parent font already then there is nothing to do.
if (m_font == p_parent_font)
return false;
// Otherwise change the current fontref for a copy of the parent fontref.
MCFontRelease(m_font);
m_font = MCFontRetain(p_parent_font);
width = 0;
// As we changed fontref, return 'true'.
return true;
}
// Take a copy of the current font.
MCFontRef t_current_font;
t_current_font = MCFontRetain(m_font);
// Unmap the current fontref, then map again.
unmapfont();
mapfont(p_parent_font);
// We've only changed if the new font is different from the old fontref.
bool t_changed;
t_changed = m_font != t_current_font;
// Get rid of the temporary copy of the old font we took.
MCFontRelease(t_current_font);
if (t_changed)
width = 0;
// Return the result of the operation.
return t_changed;
}
// MW-2012-02-06: [[ Bug ]] The 'flagged' property was causing multiple (identical)
// runs in the styledText format. To rectify this, the 'persistent_only' param
// indicates whether the comparison is for persistence (styledText) or for in-
// memory processing (defrag).
Boolean MCBlock::sameatts(MCBlock *bptr, bool p_persistent_only)
{
// Check to see if a subset of the flags are the same, if they aren't the
// blocks are not the same. Notice that in 'persistent' mode (used by
// styledText generation) we exclude the transient flags. We need to keep
// the latter in the case of 'defrag()' as technically they are different
// runs from that point of view.
// MW-2012-02-13: [[ Block Unicode ]] Make sure both blocks have the same
// 'unicode' flag.
// MW-2012-02-17: [[ SplitTextAttrs ]] Check for all the fattr rather than
// font.
uint32_t t_flags;
t_flags = F_HAS_ALL_FATTR | F_HAS_COLOR | F_HAS_METADATA | F_HAS_SHIFT | F_HAS_BACK_COLOR | F_HAS_LINK | F_HAS_UNICODE;
if (!p_persistent_only)
t_flags |= F_HILITED | F_FLAGGED | F_VISITED;
// If the flags don't match, we can't be the same.
if ((flags & t_flags) != (bptr -> flags & t_flags))
return False;
// If either block has an image, we treat them as different blocks.
if (flags & F_HAS_IMAGE || bptr->flags & F_HAS_IMAGE)
return False;
// MW-2012-02-17: [[ SplitTextAttrs ]] CHeck to see if all the font attrs
// match in turn.
if ((flags & F_HAS_FNAME) != 0 && (atts -> fontname != bptr -> atts -> fontname))
return False;
if ((flags & F_HAS_FSTYLE) != 0 && (atts -> fontstyle != bptr -> atts -> fontstyle))
return False;
if ((flags & F_HAS_FSIZE) != 0 && (atts -> fontsize != bptr -> atts -> fontsize))
return False;
// If the linkTexts are not the same, the blocks are different. Notice that
// we do a pointer comparison - these are 'Names' and as such are identical
// iff they are the same pointer.
if (getlinktext() != bptr -> getlinktext())
return False;
// If the metadatas are not the same, the blocks are different. Notice that
// we do a pointer comparison - the metadata fields are unique values so this
// is sufficient to check for equality.
if (getmetadata() != bptr -> getmetadata())
return False;
// If we have a color set, then make sure it matches the other block.
if ((flags & F_HAS_COLOR) != 0 &&
(bptr->atts->color->red != atts->color->red ||
bptr->atts->color->green != atts->color->green ||
bptr->atts->color->blue != atts->color->blue))
return False;
// If we have a backColor set, then make sure it matches the other block.
if ((flags & F_HAS_BACK_COLOR) != 0 &&
(bptr->atts->backcolor->red != atts->backcolor->red ||
bptr->atts->backcolor->green != atts->backcolor->green ||
bptr->atts->backcolor->blue != atts->backcolor->blue))
return False;
// If we have a shift set, then make sure it matches the other block.
if ((flags & F_HAS_SHIFT) != 0 &&
(bptr -> atts -> shift != atts -> shift))
return False;
// Ensure that the direction level matches
if (direction_level != bptr -> direction_level)
return False;
// Everything matches, so these two blocks must be the same.
return True;
}
static bool MCUnicodeCanBreakBetween(uint2 x, uint2 y)
{
// MW-2013-12-19: [[ Bug 11606 ]] We only check for breaks between chars and spaces
// where the space follows the char. This is because a break will consume all space
// chars after it thus we want to measure up to but not including the spaces.
bool t_x_isspace, t_y_isspace;
// AL-2014-09-03: [[ Bug 13332 ]] Don't break before a non-breaking space
t_x_isspace = MCUnicodeIsWhitespace(x);
t_y_isspace = y != 0x00A0 && MCUnicodeIsWhitespace(y);
if (t_x_isspace && t_y_isspace)
return false;
if (t_y_isspace)
return true;
bool t_xid;
t_xid = MCUnicodeCodepointIsIdeographic(x);
bool t_yid;
t_yid = MCUnicodeCodepointIsIdeographic(y);
if (!t_xid && !t_yid)
return false;
bool t_prohibit_break_after_x;
t_prohibit_break_after_x = (MCUnicodeCodepointGetBreakClass(x) & 2) != 0;
bool t_prohibit_break_before_y;
t_prohibit_break_before_y = (MCUnicodeCodepointGetBreakClass(y) & 1) != 0;
return !t_prohibit_break_after_x && !t_prohibit_break_before_y;
}
bool MCBlock::fit(coord_t x, coord_t maxwidth, findex_t& r_break_index, bool& r_break_fits)
{
// If the block of zero length, then it can't be broken and must be kept with previous blocks.
if (m_size == 0)
{
if (next() != parent -> getblocks())
r_break_fits = false;
else
r_break_fits = true;
r_break_index = m_index;
return true;
}
if ((flags & F_HAS_IMAGE) && atts -> image != NULL)
{
r_break_index = m_index + m_size;
r_break_fits = getwidth(NULL, x) <= maxwidth;
return r_break_fits;
}
// Fetch the first character of the next block
codepoint_t t_next_block_char;
MCBlock *t_next_block;
t_next_block = next();
if (t_next_block != parent -> getblocks())
{
if (t_next_block -> GetLength() == 0)
t_next_block_char = CODEPOINT_NONE-1;
else
t_next_block_char = parent->GetCodepointAtIndex(t_next_block -> m_index);
}
else
t_next_block_char = CODEPOINT_NONE;
// FG-2013-10-21 [[ Field speedups ]]
// Previously, we used to calculate the length of the entire block here in order
// to determine if splitting was required. Unfortunately, this tends to bypass
// the text layout cache resulting in very slow laying out now that the "proper"
// Unicode text layout APIs are in use.
//
// Now, all text is laid out word-at-a-time.
// We don't completely fit within maxwidth, so compute the last break point in
// the block by measuring
// MW-2013-12-19: [[ Bug 11606 ]] Track the width of the text within the block as a float
// but use the integer width to break. This ensures measure(a & b) == measure(a) + measure(b)
// (otherwise you get drift as the accumulated width the block calculates is different
// from the width of the text that is drawn).
coord_t t_width_float;
t_width_float = 0;
findex_t i;
i = m_index;
codepoint_t t_next_char;
t_next_char = parent->GetCodepointAtIndex(i);
findex_t t_break_index;
t_break_index = m_index;
bool t_can_fit;
t_can_fit = false;
// MW-2013-08-01: [[ Bug 10932 ]] Optimized loop only measuring between potential break
// points, rather than char by char.
bool t_whole_block;
t_whole_block = false;
while(i < m_index + m_size)
{
findex_t initial_i;
initial_i = i;
bool t_can_break;
t_can_break = false;
codepoint_t t_this_char;
bool t_end_of_block;
t_end_of_block = false;
// If this is the first block of a segment that was created by a tab,
// we can use the first position in the block as a break position,
// unless this is the first segment on a line.
if (!t_can_fit
&& this == segment->GetFirstBlock()
&& i == m_index
&& segment->GetParent()->GetFirstSegment() != segment
&& parent->GetCodepointAtIndex(i - 1) == '\t')
{
t_can_fit = t_can_break = true;
}
else
{
while(i < m_index + m_size)
{
t_this_char = t_next_char;
i = parent->IncrementIndex(i);
if (i < m_index + m_size)
t_next_char = parent->GetCodepointAtIndex(i);
else
{
t_next_char = t_next_block_char;
t_end_of_block = true;
}
if (t_next_char == CODEPOINT_NONE ||
MCUnicodeCanBreakBetween(t_this_char, t_next_char))
{
t_can_break = true;
break;
}
}
}
// MW-2013-11-07: [[ Bug 11393 ]] Previous per-platform implementations all fold into the optimized
// case now (previously iOS / Windows printer were measuring break by break, which is what we do
// generally now).
// FG-2014-04-30: [[ TabAlignments ]] Blocks no longer contain tabs
/*if (t_this_char == '\t')
{
twidth += gettabwidth(x + twidth, initial_i);
twidth_float = (MCGFloat)twidth;
t_last_break_width = twidth;
t_last_break_i = i;
}
else*/
{
MCRange t_range;
t_range = MCRangeMakeMinMax(initial_i, i);
// MM-2014-04-16: [[ Bug 11964 ]] Pass through the transform of the stack to make sure the measurment is correct for scaled text.
t_width_float += MCFontMeasureTextSubstringFloat(m_font, parent->GetInternalStringRef(), t_range, parent -> getparent() -> getstack() -> getdevicetransform());
}
if (t_can_fit && t_width_float > maxwidth)
break;
if (t_can_break)
t_break_index = i;
if (t_width_float <= maxwidth)
{
t_can_fit = true;
t_whole_block = t_end_of_block;
}
if (t_width_float >= maxwidth)
break;
}
// We now have a suitable break point in t_break_index. This could be index if
// there are none in the block. We now loop forward until we get to the end of
// any suitable run of spaces.
while(t_break_index < m_index + m_size && parent->TextIsWordBreak(parent->GetCodepointAtIndex(t_break_index)))
t_break_index = parent->IncrementIndex(t_break_index);
r_break_fits = t_can_fit;
r_break_index = t_break_index;
// If we found a break, was it before the end of the block?
return t_whole_block;
}
void MCBlock::split(findex_t p_index)
{
MCBlock *bptr = new (nothrow) MCBlock(*this);
findex_t newlength = m_size - (p_index - m_index);
bptr->SetRange(p_index, newlength);
m_size -= newlength;
width = 0;
// MW-2012-02-13: [[ Block Unicode ]] Only open the new block if the original
// was already open.
// MW-2012-02-14: [[ FontRefs ]] Pass in the parent fontref so the block can
// compute its fontref.
if (opened)
bptr->open(parent -> getparent() -> getfontref());
append(bptr);
}
// Compute the distance between x and the next tab stop position.
// FG-2014-04-30: [[ TabAlignments ]] Blocks no longer contain tabs
/*coord_t MCBlock::gettabwidth(coord_t x, findex_t i)
{
uint2 *tabs;
uint2 ntabs;
Boolean fixed;
// MW-2012-01-25: [[ ParaStyles ]] Fetch the tab-stops from the owning paragraph.
parent->gettabs(tabs, ntabs, fixed);
// The tabs are 'fixed' if VGRID is set - I presume this also prevents wrapping
// but who can really say!
if (fixed)
{
findex_t ctab = 0;
findex_t cindex = 0;
// Count the number of preceeding tabs in the paragraph.
MCBlock *t_block;
t_block = parent -> getblocks();
findex_t j;
j = 0;
while(j < i)
{
if (t_block -> getflag(F_HAS_TAB))
{
findex_t k;
k = t_block -> GetOffset() + t_block -> GetLength();
while(j < k && j < i)
{
if (parent->GetCodepointAtIndex(j) == '\t')
ctab++;
j = parent->IncrementIndex(j);
}
}
else
j = t_block -> GetOffset() + t_block -> GetLength();
t_block = t_block -> next();
}
// At this point 'ctab' contains the number of tab characters before
// the one gettabwidth was called with - i.e. the 0-based index
// of the tab we started with.
int2 newloc;
if (ctab < ntabs)
{
// The tab array stores absolute positions of each tab from the
// left of the field.
newloc = tabs[ctab];
}
else
{
// We have requested a tab beyond the end of the tab array, so
// we use the length of the last tab as the length of every successive
// tab.
uint2 diff;
if (ntabs == 1)
diff = tabs[0];
else
diff = tabs[ntabs - 1] - tabs[ntabs - 2];
newloc = tabs[ntabs - 1] + diff * (ctab - ntabs + 1);
}
// If the position of the current tab co-incides with the last x position then
// adjust upward by one to ensure we don't overwrite anything (legacy behavior).
if (newloc == x)
return 1;
// The width of the tab is absolute position of the tab minus the x position of
// the left hand-side of the passed in tab character.
return newloc - x;
}
else
{
int2 lasttab = 0;
uint2 i = 0;
// Loop through the tab array and find the first tab to the right of x.
while (i < ntabs && x >= lasttab)
{
lasttab = tabs[i];
i++;
}
if (x >= lasttab && ntabs != 0)
{
// If x is further right than (or is at) the last tab then the tab position