@@ -27,6 +27,7 @@ along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
2727#include " cdata.h"
2828#include " mcerror.h"
2929#include " execpt.h"
30+ #include " exec.h"
3031#include " util.h"
3132#include " block.h"
3233
@@ -459,8 +460,35 @@ MCParagraph *MCField::parsestyledtextappendparagraph(MCArrayRef p_style, MCNameR
459460 return t_new_paragraph;
460461}
461462
463+ static bool convert_array_value_to_number_if_non_empty (MCExecContext& ctxt, MCArrayRef p_array, MCNameRef p_key, MCNumberRef &r_value)
464+ {
465+ MCValueRef t_value;
466+ if (!MCArrayFetchValue (p_array, false , p_key, t_value))
467+ return false ;
468+ if (MCValueIsEmpty (t_value))
469+ return false ;
470+ if (!ctxt . ConvertToNumber (t_value, r_value))
471+ return false ;
472+ return true ;
473+ }
474+
475+ static bool copy_element_as_color_if_non_empty (MCExecContext& ctxt, MCArrayRef array, MCNameRef key, MCColor& r_value)
476+ {
477+ MCValueRef t_value;
478+ if (!MCArrayFetchValue (array, false , key, t_value))
479+ return false ;
480+ if (MCValueIsEmpty (t_value))
481+ return false ;
482+ MCAutoStringRef t_string;
483+ if (!ctxt . ConvertToString (t_value, &t_string))
484+ return false ;
485+ return MCscreen -> parsecolor (*t_string, r_value, nil);
486+ }
487+
462488void MCField::parsestyledtextappendblock (MCParagraph *p_paragraph, MCArrayRef p_style, const char *p_initial, const char *p_final, MCStringRef p_metadata, bool p_is_unicode)
463489{
490+ MCExecPoint ep (NULL , NULL , NULL );
491+ MCExecContext ctxt (ep);
464492 // Make sure we don't try and append any more than 64K worth of bytes.
465493 uint32_t t_text_length;
466494 t_text_length = MCMin (p_final - p_initial, 65534 - p_paragraph -> textsize);
@@ -523,95 +551,93 @@ void MCField::parsestyledtextappendblock(MCParagraph *p_paragraph, MCArrayRef p_
523551 MCValueRef t_valueref;
524552
525553 // Set foreground
526- if (MCArrayFetchValue (p_style, false , MCNAME (" textColor" ), t_valueref))
527554 {
528555 MCColor t_color;
529- if (MCscreen -> parsecolor ((MCStringRef)t_valueref , t_color, nil ))
556+ if (copy_element_as_color_if_non_empty (ctxt, p_style, MCNAME ( " textColor " ) , t_color))
530557 t_block -> setcolor (&t_color);
531- MCValueRelease (t_valueref);
532558 }
533559
534560 // Set background
535- if (MCArrayFetchValue (p_style, false , MCNAME (" backgroundColor" ), t_valueref))
536561 {
537562 MCColor t_color;
538- if (MCscreen -> parsecolor ((MCStringRef)t_valueref , t_color, nil ))
563+ if (copy_element_as_color_if_non_empty (ctxt, p_style, MCNAME ( " backgroundColor " ) , t_color))
539564 t_block -> setbackcolor (&t_color);
540- MCValueRelease (t_valueref);
541565 }
542-
543566 // Set textshift
544- if (MCArrayFetchValue (p_style, false , MCNAME (" textShift" ), t_valueref))
545567 {
546- t_block -> setshift (MCNumberFetchAsInteger ((MCNumberRef)t_valueref));
547- MCValueRelease (t_valueref);
568+ MCAutoNumberRef t_number;
569+ convert_array_value_to_number_if_non_empty (ctxt, p_style, MCNAME (" textShift" ), &t_number);
570+ t_block -> setshift (MCNumberFetchAsInteger (*t_number));
548571 }
549-
572+
550573 // If the block is unicode then we always have to set textFont. In either
551574 // case if there is a textFont key, strip any ,unicode tag first.
552- if (MCArrayFetchValue (p_style, false , MCNAME (" textFont" ), t_valueref))
575+ if (MCArrayFetchValue (p_style, false , MCNAME (" textFont" ), t_valueref) && ! MCValueIsEmpty (t_valueref) )
553576 {
577+ MCAutoStringRef t_string;
578+ /* UNCHECKED */ ctxt . ConvertToString (t_valueref, &t_string);
554579 uindex_t t_comma;
555- if (MCStringFirstIndexOfChar ((MCStringRef)t_valueref , ' ,' , 0 , kMCCompareExact , t_comma))
580+ if (MCStringFirstIndexOfChar (*t_string , ' ,' , 0 , kMCCompareExact , t_comma))
556581 {
557582 MCAutoStringRef t_substring;
558- MCStringCopySubstringAndRelease ((MCStringRef)t_valueref , MCRangeMake (0 , t_comma), &t_substring);
583+ MCStringCopySubstringAndRelease (*t_string , MCRangeMake (0 , t_comma), &t_substring);
559584 t_valueref = MCValueRetain (*t_substring);
560585 }
561- else
562- MCValueRelease (t_valueref);
563586 }
564587
565588 // Now if we have unicode, or a textFont style set it.
566- if (!MCStringIsEmpty ((MCStringRef) t_valueref))
589+ if (!MCValueIsEmpty ( t_valueref))
567590 {
568- t_block -> setatts (P_TEXT_FONT , (void *)MCStringGetCString ((MCStringRef)t_valueref));
591+ MCAutoStringRef t_string;
592+ /* UNCHECKED */ ctxt . ConvertToString (t_valueref, &t_string);
593+ t_block -> setatts (P_TEXT_FONT , (void *)MCStringGetCString (*t_string));
569594 MCValueRelease (t_valueref);
570595 }
571596
572597 // Set textsize
573- if (MCArrayFetchValue (p_style, false , MCNAME (" textSize" ), t_valueref))
574598 {
575- t_block -> setatts (P_TEXT_SIZE , (void *)MCNumberFetchAsInteger ((MCNumberRef)t_valueref));
576- MCValueRelease (t_valueref);
599+ MCAutoNumberRef t_number;
600+ convert_array_value_to_number_if_non_empty (ctxt, p_style, MCNAME (" textSize" ), &t_number);
601+ t_block -> setatts (P_TEXT_SIZE , (void *)MCNumberFetchAsInteger (*t_number));
577602 }
578603
579604 // Set textstyle
580- if (MCArrayFetchValue (p_style, false , MCNAME (" textStyle" ), t_valueref))
605+ if (MCArrayFetchValue (p_style, false , MCNAME (" textStyle" ), t_valueref) && ! MCValueIsEmpty (t_valueref) )
581606 {
607+ MCAutoStringRef t_string;
608+ /* UNCHECKED */ ctxt . ConvertToString (t_valueref, &t_string);
582609 uint4 flags;
583610 MCAutoStringRef fname;
584611 uint2 height;
585612 uint2 size;
586613 uint2 style;
587614
588- MCF_parsetextatts (P_TEXT_STYLE , (MCStringRef)t_valueref , flags, &fname, height, size, style);
615+ MCF_parsetextatts (P_TEXT_STYLE , *t_string , flags, &fname, height, size, style);
589616
590617 t_block -> setatts (P_TEXT_STYLE , (void *)style);
591- MCValueRelease (t_valueref);
592618 }
593619
594620 // Set linktext
595621 if (MCArrayFetchValue (p_style, false , MCNAME (" linkText" ), t_valueref))
596622 {
597623 t_block -> setatts (P_LINK_TEXT , (void *)t_valueref);
598- MCValueRelease (t_valueref);
599624 }
600625
601626 // Set imagesource
602627 if (MCArrayFetchValue (p_style, false , MCNAME (" imageSource" ), t_valueref))
603628 {
604629 t_block -> setatts (P_IMAGE_SOURCE , (void *)t_valueref);
605- MCValueRelease (t_valueref);
606630 }
607631
608- if (!MCStringIsEmpty ((MCStringRef) t_valueref))
632+ if (!MCValueIsEmpty ( t_valueref))
609633 MCValueRelease (t_valueref);
610634
611635}
612636
613637void MCField::parsestyledtextblockarray (MCArrayRef p_block_value, MCParagraph*& x_paragraphs)
614638{
639+ MCExecPoint ep (NULL , NULL , NULL );
640+ MCExecContext ctxt (ep);
615641 // If the value is a sequence, recurse for each element.
616642 if (MCArrayIsSequence (p_block_value))
617643 {
@@ -622,28 +648,32 @@ void MCField::parsestyledtextblockarray(MCArrayRef p_block_value, MCParagraph*&
622648 continue ;
623649
624650 if (!MCValueIsArray (t_block_entry))
625- continue ;
651+ {
652+ MCArrayRef t_array;
653+ /* UNCHECKED */ ctxt . ConvertToArray (t_block_entry, &t_array);
626654
627- parsestyledtextblockarray ((MCArrayRef)t_block_entry , x_paragraphs);
655+ parsestyledtextblockarray (*t_array , x_paragraphs);
628656 }
629-
630657 return ;
631658 }
632659
633660 MCValueRef t_valueref;
634- MCAutoArrayRef t_style_entry;
661+ MCAutoArrayRef t_style_entry;
635662
636663 // Set foreground
637- if (MCArrayFetchValue (p_block_value, false , MCNAME (" style" ), t_valueref))
664+ if (MCArrayFetchValue (p_block_value, false , MCNAME (" style" ), t_valueref) && ! MCValueIsEmpty (t_valueref) )
638665 {
639- /* UNCHECKED */ MCArrayCopyAndRelease ((MCArrayRef)t_valueref, &t_style_entry);
666+ MCArrayRef t_array;
667+ /* UNCHECKED */ ctxt . ConvertToArray (t_valueref, t_array);
668+ /* UNCHECKED */ MCArrayCopyAndRelease (t_array, &t_style_entry);
640669 }
641670 // Get the metadata (if any)
642671 MCStringRef t_metadata;
643- if (MCArrayFetchValue (p_block_value, false , MCNAME (" metadata" ), t_valueref))
672+ if (MCArrayFetchValue (p_block_value, false , MCNAME (" metadata" ), t_valueref) && ! MCValueIsEmpty (t_valueref) )
644673 {
645- t_metadata = (MCStringRef)MCValueRetain (t_valueref);
646- MCValueRelease (t_valueref);
674+ MCAutoStringRef t_string;
675+ /* UNCHECKED */ ctxt . ConvertToString (t_valueref, &t_string);
676+ t_metadata = MCValueRetain (*t_string);
647677 }
648678 // If there are no paragraphs yet, create one.
649679 MCParagraph *t_paragraph;
@@ -658,16 +688,18 @@ void MCField::parsestyledtextblockarray(MCArrayRef p_block_value, MCParagraph*&
658688 bool t_is_unicode;
659689 t_is_unicode = false ;
660690
661- if (!MCArrayFetchValue (p_block_value, false , MCNAME (" text" ), t_valueref))
691+ if (!MCArrayFetchValue (p_block_value, false , MCNAME (" text" ), t_valueref) || MCValueIsEmpty (t_valueref) )
662692 {
663693 /* UNCKECKED */ MCArrayFetchValue (p_block_value, false , MCNAME (" unicodeText" ), t_valueref);
664694 t_is_unicode = true ;
665695 }
666- if (MCStringIsEmpty ((MCStringRef) t_valueref) || MCValueGetTypeCode (t_valueref) == kMCValueTypeCodeArray )
696+ if (MCValueIsEmpty ( t_valueref) || MCValueGetTypeCode (t_valueref) == kMCValueTypeCodeArray )
667697 return ;
668698
669- t_text_ptr = MCStringGetCString ((MCStringRef)t_valueref);
670- t_text_length = MCStringGetLength ((MCStringRef)t_valueref);
699+ MCAutoStringRef t_temp;
700+ /* UNCHECKED */ ctxt . ConvertToString (t_valueref, &t_temp);
701+ t_text_ptr = MCStringGetCString (*t_temp);
702+ t_text_length = MCStringGetLength (*t_temp);
671703 while (t_text_length != 0 )
672704 {
673705 bool t_add_paragraph;
@@ -696,7 +728,7 @@ void MCField::parsestyledtextblockarray(MCArrayRef p_block_value, MCParagraph*&
696728 parsestyledtextappendblock (t_paragraph, *t_style_entry, t_text_initial_ptr, t_text_final_ptr, t_metadata, t_is_unicode);
697729
698730 MCValueRelease (t_metadata);
699- if (!MCStringIsEmpty ((MCStringRef) t_valueref))
731+ if (!MCValueIsEmpty ( t_valueref))
700732 MCValueRelease (t_valueref);
701733
702734 // And, if we need a new paragraph, add it.
0 commit comments