forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfieldrtf.cpp
More file actions
836 lines (720 loc) · 28.3 KB
/
fieldrtf.cpp
File metadata and controls
836 lines (720 loc) · 28.3 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
/* Copyright (C) 2003-2013 Runtime Revolution 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 "core.h"
#include "globdefs.h"
#include "filedefs.h"
#include "objdefs.h"
#include "parsedef.h"
#include "field.h"
#include "paragraf.h"
#include "unicode.h"
#include "text.h"
#include "osspec.h"
#include "execpt.h"
#include "mcstring.h"
#include "textbuffer.h"
////////////////////////////////////////////////////////////////////////////////
// Platform-specific ifdefs (mac is different from linux/windows in some ways).
#ifdef _MACOSX
#define EXPORT_RTF_PLATFORM_TAG "\\mac"
#define EXPORT_RTF_PLATFORM_CHARSET "77"
#define EXPORT_RTF_PLATFORM_DPI 72
#else
#define EXPORT_RTF_PLATFORM_TAG "\\ansi"
#define EXPORT_RTF_PLATFORM_CHARSET "0"
#define EXPORT_RTF_PLATFORM_DPI 96
#endif
// Simple class managing a set of unique (indexed) values.
template<class T> class export_rtf_table_t
{
public:
export_rtf_table_t(void)
{
m_count = m_capacity = 0;
m_array = nil;
}
~export_rtf_table_t(void)
{
MCMemoryDeleteArray(m_array);
}
uint32_t count(void) const
{
return m_count;
}
void add(T p_entry)
{
uint32_t t_index;
if (find(p_entry, t_index))
return;
if (m_count + 1 > m_capacity)
if (!MCMemoryResizeArray(m_capacity == 0 ? 16 : m_capacity * 2, m_array, m_capacity))
return;
if (m_count - t_index > 0)
memmove(m_array + t_index + 1, m_array + t_index, sizeof(T) * (m_count - t_index));
m_array[t_index] = p_entry;
m_count += 1;
}
uint32_t lookup(T p_entry) const
{
uint32_t t_index;
if (!find(p_entry, t_index))
t_index = 0;
return t_index;
}
T operator[] (uint32_t p_index) const
{
return m_array[p_index];
}
private:
bool find(T p_entry, uint32_t& r_index) const
{
for(r_index = 0; r_index < m_count; r_index++)
if (p_entry == m_array[r_index])
return true;
return false;
}
uint32_t m_count;
uint32_t m_capacity;
T *m_array;
};
// Simple class managing an array of values.
template<class T> class export_rtf_array_t
{
public:
export_rtf_array_t(void)
{
m_count = m_capacity = 0;
m_array = nil;
}
~export_rtf_array_t(void)
{
MCMemoryDeleteArray(m_array);
}
uint32_t count(void) const
{
return m_count;
}
void append(T p_entry)
{
if (m_count + 1 > m_capacity)
if (!MCMemoryResizeArray(m_capacity == 0 ? 16 : m_capacity * 2, m_array, m_capacity))
return;
m_array[m_count++] = p_entry;
}
T operator[] (uint32_t p_index) const
{
return m_array[p_index];
}
private:
uint32_t m_count;
uint32_t m_capacity;
T *m_array;
};
// A collection of char styles that are applied to a run.
struct export_rtf_char_style_t
{
bool italic_on : 1;
bool bold_on : 1;
bool strike_on : 1;
bool underline_on : 1;
bool superscript_on : 1;
bool subscript_on : 1;
bool link_on : 1;
int32_t font_index;
int32_t font_size;
int32_t text_color_index;
int32_t background_color_index;
MCNameRef link_text;
MCNameRef metadata;
};
// The rtf export context structure.
struct export_rtf_t
{
// The list of fonts that are in the font table.
export_rtf_table_t<MCNameRef> fonts;
// The list of colors that are in the color table.
export_rtf_table_t<uint32_t> colors;
// The list of list styles that are in the list table.
export_rtf_table_t<uint32_t> lists;
// The mapping from paragraph index to list style id (if an entry is zero
// then the paragraph has no list styling).
export_rtf_array_t<uint32_t> list_ids;
// The buffer containing the output text.
text_buffer_t buffer;
// The styles we inherit from the field (used for list labels).
export_rtf_char_style_t parent_style;
// The stack of styles currently in play. This will have at most three
// levels at the moment, as the maximum nesting is (link, metadata, bgcolor).
export_rtf_char_style_t styles[4];
// The current 'top' style in the stack.
uint32_t style_index;
// The 0-based index of the paragraph (used to get list_ids).
uint32_t paragraph_index;
// The encoded list style in effect at this point.
uint32_t list_style;
// The current paragraph had metadata attached.
bool has_metadata;
};
////////////////////////////////////////////////////////////////////////////////
// Convert pixels to twentieth's of a point.
static int32_t pixels_to_twips(int32_t p_pixels)
{
return p_pixels * 20;
}
// Convert pixels to half points.
static int32_t pixels_to_half_points(int32_t p_pixels)
{
return (p_pixels * 144) / EXPORT_RTF_PLATFORM_DPI;
}
// Fetch the depth of an encoded list style.
static uint32_t export_rtf_get_list_style_depth(uint32_t p_style)
{
return p_style & 0xf;
}
// Get the type of list at level p_level in the given encoded list style.
static uint32_t export_rtf_get_list_style_level(uint32_t p_style, uint32_t p_level)
{
return (((p_style >> 4) >> (p_level * 3)) & 0x7) + 1;
}
// Set the depth of the encoded list style.
static void export_rtf_set_list_style_depth(uint32_t& x_style, uint32_t p_depth)
{
x_style = (x_style & ~0xf) | (p_depth & 0xf);
}
// Set the level of the encoded list style to the given style.
static void export_rtf_set_list_style_level(uint32_t& x_style, uint32_t p_depth, uint32_t p_list_type)
{
x_style = (x_style & ~(0x7 << (4 + p_depth * 3))) | ((p_list_type - 1) << (4 + p_depth * 3));
}
// Compute a new encoded list style when presented with a new depth/style
// for an additional paragraph. It returns 'true' if the style has changed,
// 'false' otherwise. (If it returns true, it means 'base_style' should be
// recorded and a new list is beginning as 'new_style').
static bool export_rtf_process_list_style(uint32_t p_base_style, uint32_t p_list_style, uint32_t p_list_depth, uint32_t& r_new_style)
{
// If this is a skip style, then do nothing.
if (p_list_style == kMCParagraphListStyleSkip)
{
r_new_style = p_base_style;
return false;
}
// If this paragraph has no list style set, then that terminates the list.
if (p_list_style == kMCParagraphListStyleNone)
{
r_new_style = 0;
// If the base style is 0, then this isn't terminating anything so return
// false.
return p_base_style != 0;
}
// We can emit a maximum of 9 levels into RTF.
if (p_list_depth > 8)
p_list_depth = 8;
// Get the current number of levels assigned.
uint32_t t_base_depth;
t_base_depth = p_base_style & 0xf;
// If this paragraph is at greater depth, then add the style in to the existing
// list and return.
if (p_list_depth + 1 > t_base_depth)
{
r_new_style = p_base_style;
export_rtf_set_list_style_depth(r_new_style, p_list_depth + 1);
export_rtf_set_list_style_level(r_new_style, p_list_depth, p_list_style);
return false;
}
// Otherwise, we must be adding another paragraph to an existing level so check
// the style is the same.
if (export_rtf_get_list_style_level(p_base_style, p_list_depth) != p_list_style)
{
r_new_style = 0;
return true;
}
// If we get here, no changes have been made to the style, so continue.
r_new_style = p_base_style;
return false;
}
// Accumulate all the tables needed to export the rtf.
static bool export_rtf_build_tables(void *p_context, MCFieldExportEventType p_event_type, const MCFieldExportEventData& p_event_data)
{
export_rtf_t& ctxt = *(export_rtf_t *)p_context;
if (p_event_type == kMCFieldExportEventNativeRun || p_event_type == kMCFieldExportEventUnicodeRun)
{
ctxt . fonts . add(p_event_data . character_style . text_font);
ctxt . colors . add(p_event_data . character_style . text_color);
if (p_event_data . character_style . has_background_color)
ctxt . colors . add(p_event_data . character_style . background_color);
}
else if (p_event_type == kMCFieldExportEventBeginParagraph)
{
ctxt . colors . add(p_event_data . paragraph_style . border_color);
if (p_event_data . paragraph_style . has_background_color)
ctxt . colors . add(p_event_data . paragraph_style . background_color);
uint32_t t_new_list_style;
if (export_rtf_process_list_style(ctxt . list_style, p_event_data . paragraph_style . list_style, p_event_data . paragraph_style . list_depth, t_new_list_style))
ctxt . lists . add(ctxt . list_style);
ctxt . list_style = t_new_list_style;
if (p_event_data . paragraph_style . list_style != kMCParagraphListStyleNone)
ctxt . list_ids . append(ctxt . lists . count() + 1);
else
ctxt . list_ids . append(0);
}
return true;
}
// Emit a run of native text, taking care to encode appropriate chars
// (in particular, line break - 0x0b).
static void export_rtf_emit_native_text(text_buffer_t& buffer, const uint8_t *p_chars, uint32_t p_char_count)
{
for(;;)
{
uint32_t t_end;
for(t_end = 0; t_end < p_char_count; t_end++)
if (p_chars[t_end] < 0x20 ||
p_chars[t_end] >= 0x80 ||
p_chars[t_end] == 0x5c ||
p_chars[t_end] == 0x7b ||
p_chars[t_end] == 0x7d ||
p_chars[t_end] == 0x0b)
break;
if (t_end > 0)
{
buffer . appendtext(p_chars, t_end, false);
p_char_count -= t_end;
p_chars += t_end;
}
if (p_char_count == 0)
break;
if (*p_chars != 0x0b)
buffer . appendtextf("\\'%02X", *p_chars);
else
buffer . appendcstring("\\line ");
p_chars += 1;
p_char_count -= 1;
}
}
// Clear an rtf char style.
static void export_rtf_clear_char_style(export_rtf_char_style_t& x_style)
{
x_style . italic_on = false;
x_style . bold_on = false;
x_style . underline_on = false;
x_style . strike_on = false;
x_style . superscript_on = false;
x_style . subscript_on = false;
x_style . font_index = -1;
x_style . font_size = -1;
x_style . text_color_index = -1;
x_style . background_color_index = -1;
x_style . link_text = nil;
x_style . metadata = nil;
}
// Fetch an rtf char style from a field char style.
static void export_rtf_fetch_char_style(export_rtf_t& ctxt, export_rtf_char_style_t& r_style, const MCFieldCharacterStyle& p_field_style)
{
r_style . italic_on = (p_field_style . text_style & (FA_ITALIC | FA_OBLIQUE)) != 0;
r_style . bold_on = (p_field_style . text_style & FA_WEIGHT) == (FA_BOLD & FA_WEIGHT);
r_style . strike_on = (p_field_style . text_style & FA_STRIKEOUT) != 0;
r_style . underline_on = (p_field_style . text_style & FA_UNDERLINE) != 0;
r_style . superscript_on = p_field_style . text_shift > 0;
r_style . subscript_on = p_field_style . text_shift < 0;
r_style . link_on = (p_field_style . text_style & FA_LINK) != 0;
r_style . font_index = ctxt . fonts . lookup(p_field_style . text_font);
r_style . font_size = pixels_to_half_points(p_field_style . text_size);
r_style . text_color_index = ctxt . colors . lookup(p_field_style . text_color);
if (p_field_style . has_link_text)
r_style . link_text = p_field_style . link_text;
else
r_style . link_text = nil;
if (p_field_style . has_metadata)
r_style . metadata = p_field_style . metadata;
else
r_style . metadata = nil;
if (p_field_style . has_background_color)
r_style . background_color_index = ctxt . colors . lookup(p_field_style . background_color);
else
r_style . background_color_index = -1;
}
// Emit the changes in char style from old to new.
static void export_rtf_emit_char_style_changes(text_buffer_t& buffer, export_rtf_char_style_t& p_old, export_rtf_char_style_t& p_new)
{
if (p_new . italic_on != p_old . italic_on)
buffer . appendcstring(p_new . italic_on ? "\\i " : "\\i0 ");
if (p_new . bold_on != p_old . bold_on)
buffer . appendcstring(p_new . bold_on ? "\\b " : "\\b0 ");
if (p_new . strike_on != p_old . strike_on)
buffer . appendcstring(p_new . strike_on ? "\\strike " : "\\strike0 ");
// MW-2012-03-13: [[ Bug ]] Incorrect tag used for underline - should be 'ul' not 'u'.
if (p_new . underline_on != p_old . underline_on)
buffer . appendcstring(p_new . underline_on ? "\\ul " : "\\ul0 ");
if (p_new . superscript_on != p_old . superscript_on || p_new . subscript_on != p_old . subscript_on)
buffer . appendcstring(p_new . subscript_on ? "\\sub " : (p_new . subscript_on ? "\\super " : "\\nosupersub "));
if (p_new . font_index != p_old . font_index)
buffer . appendtextf("\\f%d ", p_new . font_index);
if (p_new . font_size != p_old . font_size)
buffer . appendtextf("\\fs%d ", p_new . font_size);
if (p_new . text_color_index != p_old . text_color_index)
buffer . appendtextf("\\cf%d ", p_new . text_color_index + 1);
if (p_new . background_color_index != p_old . background_color_index && p_new . background_color_index != -1)
buffer . appendtextf("\\cb%d\\chcbpat%d ", p_new . background_color_index + 1, p_new . background_color_index + 1);
}
// Emit the paragraph content. This is the second pass of rtf generation, the
// first pass constructs the necessary tables.
static bool export_rtf_emit_paragraphs(void *p_context, MCFieldExportEventType p_event_type, const MCFieldExportEventData& p_event_data)
{
export_rtf_t& ctxt = *(export_rtf_t *)p_context;
// If this is the first paragraph, then emit the tables first.
if (p_event_type == kMCFieldExportEventBeginParagraph && p_event_data . is_first_paragraph)
{
// Output the inital info.
ctxt . buffer . appendcstring("{\\rtf1" EXPORT_RTF_PLATFORM_TAG " ");
// Output the font table.
if (ctxt . fonts . count() > 0)
{
ctxt . buffer . appendcstring("{\\fonttbl");
for(uint32_t i = 0; i < ctxt . fonts . count(); i++)
ctxt . buffer . appendtextf("{\\f%d\\fnil \\fcharset" EXPORT_RTF_PLATFORM_CHARSET " %s;}", i, MCNameGetCString(ctxt . fonts[i]));
ctxt . buffer . appendcstring("}\n");
}
// Output the color table.
if (ctxt . colors . count() > 0)
{
ctxt . buffer . appendcstring("{\\colortbl;");
for(uint32_t i = 0; i < ctxt . colors . count(); i++)
ctxt . buffer . appendtextf("\\red%d\\green%d\\blue%d;", (ctxt . colors[i] >> 16) & 0xff, (ctxt . colors[i] >> 8) & 0xff, (ctxt . colors[i] >> 0) & 0xff);
ctxt . buffer . appendcstring("}\n");
}
// Output the list table.
if (ctxt . lists . count() > 0)
{
// Fetch the char rtf styles that are inherited (for listtext labels).
export_rtf_fetch_char_style(ctxt, ctxt . parent_style, p_event_data . character_style);
ctxt . buffer . appendcstring("{\\*\\listtable");
for(uint32_t i = 0; i < ctxt . lists . count(); i++)
{
ctxt . buffer . appendtextf("{\\list\n", i + 1);
for(uint32_t j = 0; j < export_rtf_get_list_style_depth(ctxt . lists[i]); j++)
{
uint32_t t_list_style;
t_list_style = export_rtf_get_list_style_level(ctxt . lists[i], j);
if (t_list_style < kMCParagraphListStyleNumeric)
{
int32_t t_char;
const char *t_marker;
if (t_list_style == kMCParagraphListStyleDisc)
t_char = 0x2022, t_marker = "disc";
else if (t_list_style == kMCParagraphListStyleSquare)
t_char = 0x25AA, t_marker = "square";
else if (t_list_style == kMCParagraphListStyleCircle)
t_char = 0x25E6, t_marker = "circle";
ctxt . buffer . appendtextf("{\\listlevel\\levelnfc23\\leveljc0\\levelstartat1\\levelfollow0{\\*\\levelmarker \\{%s\\}}{\\leveltext\\'01\\u%d.;}{\\levelnumbers;}",
t_marker, t_char);
}
else
{
int32_t t_type;
switch(t_list_style)
{
case kMCParagraphListStyleNumeric: t_type = 0; break;
case kMCParagraphListStyleLowerCase: t_type = 4; break;
case kMCParagraphListStyleUpperCase: t_type = 3; break;
case kMCParagraphListStyleLowerRoman: t_type = 2; break;
case kMCParagraphListStyleUpperRoman: t_type = 1; break;
}
ctxt . buffer . appendtextf("{\\listlevel\\levelnfc%d\\leveljc0\\levelstartat1\\levelfollow0{\\leveltext\'02\'0%d.;}{\\levelnumbers\'01;}", t_type, j);
}
export_rtf_emit_char_style_changes(ctxt . buffer, ctxt . styles[0], ctxt . parent_style);
ctxt . buffer . appendcstring("}\n");
}
ctxt . buffer . appendtextf("\\listid%d}\n", i + 1);
}
ctxt . buffer . appendcstring("}\n");
ctxt . buffer . appendcstring("{\\*\\listoverridetable");
for(uint32_t i = 0; i < ctxt . lists . count(); i++)
ctxt . buffer . appendtextf("{\\listoverride\\listid%d\\listoverridecount0\\ls%d}\n", i + 1, i + 1);
ctxt . buffer . appendcstring("}\n");
}
}
if (p_event_type == kMCFieldExportEventBeginParagraph)
{
// If we aren't on the first paragraph, then emit a paragraph separator.
if (!p_event_data . is_first_paragraph)
ctxt . buffer . appendcstring("\\par ");
// Clear all the paragraph properties.
ctxt . buffer . appendcstring("\\pard ");
// If we have paragraph metadata set, then emit that field.
if (p_event_data . paragraph_style . has_metadata)
{
// TODO: paragraph metadata roundtrip - this clause breaks things at the moment :(
//ctxt . buffer . appendtextf("{{\\field{\\*\\fldinst LCLINEMETADATA \"%s\"}}", MCNameGetCString(p_event_data . paragraph_style . metadata));
//ctxt . has_metadata = true;
// MW-2014-03-12: [[ Bug 11769 ]] We haven't emitted any metadata (as it doesn't work right yet) so
// best not to emit an end brace!
ctxt . has_metadata = false;
}
else
ctxt . has_metadata = false;
// If we have a background color set, then emit a 'cbpat' control. Note that
// putting this control later on breaks in openoffice :(
if (p_event_data . paragraph_style . has_background_color)
ctxt . buffer . appendtextf("\\cbpat%d ", ctxt . colors . lookup(p_event_data . paragraph_style . background_color) + 1);
// If we have a border set, then emit the border controls.
if (p_event_data . paragraph_style . border_width != 0)
{
ctxt . buffer . appendtextf("\\box\\brdrw%d\\brdrcf%d\\brdsp%d ",
pixels_to_twips(p_event_data . paragraph_style . border_width),
ctxt . colors . lookup(p_event_data . paragraph_style . border_color) + 1,
pixels_to_twips(p_event_data . paragraph_style . padding));
}
// Compute the left / first indent depending on whether the paragraph has a list
// style or not.
int32_t t_left_indent, t_first_indent;
t_left_indent = 0;
t_first_indent = 0;
if (p_event_data . paragraph_style . list_style == kMCParagraphListStyleNone)
{
// If no list style, then just use the (effective) settings.
t_left_indent = p_event_data . paragraph_style . left_indent;
t_first_indent = p_event_data . paragraph_style . first_indent;
}
else
{
if (p_event_data . paragraph_style . has_list_indent)
{
// If there is a list indent, then left indent is a multiple of it (based on
// depth).
t_left_indent = p_event_data . paragraph_style . left_indent + (p_event_data . paragraph_style . list_depth + 1) * p_event_data . paragraph_style . list_indent;
t_first_indent = -(signed)MCAbs(p_event_data . paragraph_style . list_indent);
}
else
{
// Otherwise, the user has overriden things, so use left / first indent
// directly.
t_left_indent = p_event_data . paragraph_style . left_indent;
t_first_indent = -(signed)MCAbs(p_event_data . paragraph_style . first_indent);
}
}
// Emit the controls for first / left / right indent.
if (t_first_indent != 0)
ctxt . buffer . appendtextf("\\fi%d ", pixels_to_twips(t_first_indent));
if (t_left_indent != 0)
ctxt . buffer . appendtextf("\\li%d ", pixels_to_twips(t_left_indent));
if (p_event_data . paragraph_style . right_indent != 0)
ctxt . buffer . appendtextf("\\ri%d ", pixels_to_twips(p_event_data . paragraph_style . right_indent));
// If we have a list style assigned to this paragraph, then process it.
if (ctxt . list_ids[ctxt . paragraph_index] != 0)
{
// Emit a tab-stop at the text indent level.
ctxt . buffer . appendtextf("\\tx%d", pixels_to_twips(-t_first_indent));
// Emit the level and list style controls.
ctxt . buffer . appendtextf("\\ls%d", ctxt . list_ids[ctxt . paragraph_index]);
ctxt . buffer . appendtextf("\\ilvl%d", MCMin(p_event_data . paragraph_style . list_depth, 8U));
// Emit the tag prefix and styling.
ctxt . buffer . appendcstring("{\\listtext\\tab");
export_rtf_emit_char_style_changes(ctxt . buffer, ctxt . styles[ctxt . style_index], ctxt . parent_style);
// Now fetch the list style of the current paragraph and output the
// appropriate 'listtext' tag.
uint32_t t_list_style;
t_list_style = p_event_data . paragraph_style . list_style;
switch(t_list_style)
{
case kMCParagraphListStyleDisc:
case kMCParagraphListStyleCircle:
case kMCParagraphListStyleSquare:
{
// Emit the correct bullet char in the 'listtext' section.
uint32_t t_char;
t_char = (t_list_style == kMCParagraphListStyleDisc ? 0x2022 : (t_list_style == kMCParagraphListStyleCircle ? 0x25E6 : 0x25AA));
ctxt . buffer . appendtextf("\\u%d.\\tab}", t_char); }
break;
case kMCParagraphListStyleNumeric:
case kMCParagraphListStyleUpperRoman:
case kMCParagraphListStyleLowerRoman:
case kMCParagraphListStyleUpperCase:
case kMCParagraphListStyleLowerCase:
{
// Format the label using the paragraph number supplied by the export process
// and emit it in the 'listtext' section.
char t_label_buffer[PG_MAX_INDEX_SIZE];
const char *t_label_string;
uint32_t t_label_length;
MCParagraph::formatliststyleindex(t_list_style, p_event_data . paragraph_number, t_label_buffer, t_label_string, t_label_length);
ctxt . buffer . appendtextf(" %.*s\\tab}", t_label_length, t_label_string);
}
break;
case kMCParagraphListStyleSkip:
// Just emit a tab in the listtext section. This causes the paragraph to be
// aligned after an (invisible) number.
ctxt . buffer . appendcstring("\\tab}");
break;
}
}
// Emit the appropriate alignment tag.
switch(p_event_data . paragraph_style . text_align)
{
case kMCParagraphTextAlignLeft:
case kMCParagraphTextAlignJustify:
break;
case kMCParagraphTextAlignRight:
ctxt . buffer . appendcstring("\\qr ");
break;
case kMCParagraphTextAlignCenter:
ctxt . buffer . appendcstring("\\qc ");
break;
}
// Emit the spacing above and below controls.
if (p_event_data . paragraph_style . space_above != 0)
ctxt . buffer . appendtextf("\\sb%d ", pixels_to_twips(p_event_data . paragraph_style . space_above));
if (p_event_data . paragraph_style . space_below != 0)
ctxt . buffer . appendtextf("\\sa%d ", pixels_to_twips(p_event_data . paragraph_style . space_below));
// Increment the paragraph index.
ctxt . paragraph_index += 1;
}
else if (p_event_type == kMCFieldExportEventEndParagraph)
{
// Make sure any nested styles are finished.
if (ctxt . styles[ctxt . style_index] . link_text != nil)
ctxt . buffer . appendcstring("}");
while(ctxt . style_index > 0)
{
ctxt . buffer . appendcstring("}");
ctxt . style_index -= 1;
}
if (ctxt . has_metadata)
ctxt . buffer . appendcstring("}");
// For neatness, emit a newline after each paragraph.
if (!p_event_data . is_last_paragraph)
ctxt . buffer . appendcstring("\n");
}
else if (p_event_type == kMCFieldExportEventNativeRun || p_event_type == kMCFieldExportEventUnicodeRun)
{
// Fetch the new (basic) textstyles - these are ones which can be turned on and off
// with tags.
export_rtf_char_style_t t_new_style;
export_rtf_fetch_char_style(ctxt, t_new_style, p_event_data . character_style);
// Handle a change in link text.
if (t_new_style . link_text != ctxt . styles[ctxt . style_index] . link_text)
{
if (ctxt . styles[ctxt . style_index] . link_text != nil)
ctxt . buffer . appendcstring("}");
while(ctxt . style_index > 0)
{
ctxt . buffer . appendcstring("}");
ctxt . style_index -= 1;
}
if (t_new_style . link_text != nil)
{
ctxt . buffer . appendtextf("{\\field{\\*\\fldinst %s \"%s\"}{\\fldrslt ", t_new_style . link_on ? "HYPERLINK" : "LCANCHOR", MCNameGetCString(t_new_style . link_text));
ctxt . styles[ctxt . style_index] = ctxt . styles[ctxt . style_index];
ctxt . style_index += 1;
}
}
// Handle a change in metadata.
if (t_new_style . metadata != ctxt . styles[ctxt . style_index] . metadata)
{
if (ctxt . styles[ctxt . style_index] . metadata != nil)
ctxt . buffer . appendcstring("}");
while(ctxt . style_index > 0 && ctxt . styles[ctxt . style_index] . link_text == nil)
{
ctxt . buffer . appendcstring("}");
ctxt . style_index -= 1;
}
if (t_new_style . metadata != nil)
{
ctxt . buffer . appendtextf("{\\field{\\*\\fldinst LCMETADATA \"%s\"}{\\fldrslt ", MCNameGetCString(t_new_style . metadata));
ctxt . styles[ctxt . style_index + 1] = ctxt . styles[ctxt . style_index];
ctxt . style_index += 1;
}
}
// Handle a change in background color.
if (t_new_style . background_color_index != ctxt . styles[ctxt . style_index] . background_color_index)
{
if (t_new_style . background_color_index == -1)
{
ctxt . buffer . appendcstring("}");
ctxt . style_index -= 1;
}
else if (ctxt . styles[ctxt . style_index] . background_color_index == -1)
{
ctxt . buffer . appendcstring("{");
ctxt . styles[ctxt . style_index + 1] = ctxt . styles[ctxt . style_index];
ctxt . style_index += 1;
}
}
// Emit any style changes.
export_rtf_emit_char_style_changes(ctxt . buffer, ctxt . styles[ctxt . style_index], t_new_style);
ctxt . styles[ctxt . style_index] = t_new_style;
// Now emit the text, if native its easy, otherwise we must process.
if (p_event_type == kMCFieldExportEventNativeRun)
export_rtf_emit_native_text(ctxt . buffer, (const uint8_t *)p_event_data . bytes, p_event_data . byte_count);
else if (p_event_type == kMCFieldExportEventUnicodeRun)
{
const uint16_t *t_chars;
uint32_t t_char_count;
t_chars = (const uint16_t *)p_event_data . bytes;
t_char_count = p_event_data . byte_count / 2;
// A temporary buffer to store any native converted text in.
uint8_t *t_native_text;
t_native_text = new uint8_t[t_char_count];
// Loop until we are done.
while(t_char_count > 0)
{
// Attempt to convert as much as possible to native text. This returns
// the number of unicode chars consumed in 'used', and the number of native
// chars created in made.
uint32_t t_made, t_used;
MCTextRunnify(t_chars, t_char_count, t_native_text, t_used, t_made);
// If made is 0 then we have 'used' unicode chars to emit; otherwise we
// have 'made' native chars to emit.
if (t_made == 0)
for(uint32_t i = 0; i < t_used; i++)
ctxt . buffer . appendtextf("\\u%d?", t_chars[i]);
else
export_rtf_emit_native_text(ctxt . buffer, t_native_text, t_made);
// Update the char / char_count.
t_chars += t_used;
t_char_count -= t_used;
}
// Delete the temporary buffer.
delete t_native_text;
}
}
return true;
}
// MW-2012-02-29: [[ FieldExport ]] New RTF export method.
void MCField::exportasrtftext(MCExecPoint& ep, MCParagraph *p_paragraphs, int32_t p_start_index, int32_t p_finish_index)
{
export_rtf_t ctxt;
// Reset the list-style info.
ctxt . list_style = 0;
// Execute the first pass which collects all the data that's needed in the header - the font table,
// color table and list table.
doexport(kMCFieldExportParagraphs | kMCFieldExportRuns | kMCFieldExportParagraphStyles | kMCFieldExportCharacterStyles | kMCFieldExportFlattenStyles, p_paragraphs, p_start_index, p_finish_index, export_rtf_build_tables, &ctxt);
// Add the final list style, if any.
if (ctxt . list_style != 0)
ctxt . lists . add(ctxt . list_style);
// Setup the initial format settings.
for(uint32_t i = 0; i < 4; i++)
export_rtf_clear_char_style(ctxt . styles[i]);
ctxt . style_index = 0;
ctxt . list_style = 0;
ctxt . paragraph_index = 0;
// Now execute the second pass which generates all the paragraph content.
doexport(kMCFieldExportParagraphs | kMCFieldExportRuns | kMCFieldExportParagraphStyles | kMCFieldExportCharacterStyles | kMCFieldExportFlattenStyles | kMCFieldExportNumbering, p_paragraphs, p_start_index, p_finish_index, export_rtf_emit_paragraphs, &ctxt);
// Output the final info.
ctxt . buffer . appendcstring("\n}");
// Return the buffer in the ep.
ctxt . buffer . givetoep(ep);
}
void MCField::exportasrtftext(uint32_t p_part_id, MCExecPoint& ep, int32_t p_start_index, int32_t p_finish_index)
{
exportasrtftext(ep, resolveparagraphs(p_part_id), p_start_index, p_finish_index);
}
////////////////////////////////////////////////////////////////////////////////