Working with Styled text - a few questions

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

Working with Styled text - a few questions

Post by Simon Knight » Mon Dec 29, 2014 4:47 pm

Hi,

Although I have used Livecode for some time I have never needed to use "fancy" text and all my utility apps have just basic styles applied to a field.

I would like to learn some new techniques and would like to create a simple note taking application that allows various character level styles to be applied, retained and displayed.

My basic design requires that a note is comprises "Title", "BodyText" and "Status" fields with Title and BodyText allowing character level styles. The user will populate these three fields via text fields on a card then hit save which causes the record data to be stored.

This would be a simple process if I were using plain text : I would put the text of the three fields into a single variable delimited by tab characters. The record would then be added to the end of a data storage variable with a CR being used to indicate the end of a record.

Using character styles makes things more complicated. First off there are at least three different but related ways of accesses the style data : rtfText, htmlText and styledText. Other forum posts suggest that htmltext is preferred over rtfText and I have not found much about styledText. So until told otherwise I will plan to create my record by merging the htmltext of each of my data entry fields: please comment.

At some point I wish to filter and sort the records e.g. open notes sorted in alphabetical order, again this is simple with plain text but with fancy text it seems much more complicated. I will need to retrieve the plain text but I am unsure how to do this other than writing data back into a field and reading the plain text out Should I just plan to save the plain text in additional fields when the record is initially saved, to allow the records to be sorted and filtered? Should I be using (gulp) the styledText arrays instead?

Do you have any recommendations?

best wishes

Simon K
best wishes
Skids

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10354
Joined: Wed May 06, 2009 2:28 pm

Re: Working with Styled text - a few questions

Post by dunbarx » Mon Dec 29, 2014 5:25 pm

Simon.

I am no text expert, but I do not think such actions as sorting or filtering will bear at all on the styled text of your data. It is true that if you have styled text in a field 1, say, and:

put fld 1 into fld 2

then only plain text will appear. Whereas if you:

set the htmltext of fld 2 to the htmlText of fld 1

It comes over styled. But sort and filter are based on ASCII values, not style values. Have you run across a situation where the style affects certain text functions?

Craig

Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

Re: Working with Styled text - a few questions

Post by Simon Knight » Mon Dec 29, 2014 9:00 pm

Hi Craig,

I was thinking of storing the htmltext records as lines in a variable. I will probably have to remove any formatting using the replaceText command on an as needed basis e.g. (rough pseudo code..)

Code: Select all

 set itemdelimiter to tab
put WordToFind into tSeekMe
Loop through the data
put item n of tLine into tStyledData
replaceText (tStyledData, ("<" & * & ">"), "")

If tSeekme is among tStyledData then
  -- do something with the record.....
end if
next record


best wishes

Simon
best wishes
Skids

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10354
Joined: Wed May 06, 2009 2:28 pm

Re: Working with Styled text - a few questions

Post by dunbarx » Mon Dec 29, 2014 9:33 pm

Simon.

I would use a custom property. The htmlText is preserved in custom properties:

set the storeStyledText of this cd to the htmlText of fld "myStyledText"
answer the storeStyledText of this cd

Craig

Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

Re: Working with Styled text - a few questions

Post by Simon Knight » Mon Dec 29, 2014 11:45 pm

Hi,
I suppose what I am seeking is an idea of what the merits are of various methods that can be employed when working with styled text. Writing these posts has helped clarify some of the issues so all I have to do is write the code! I shall probably use a custom prop as you suggest especially as htmltext is just text.

However, I wonder if I should be looking at using the array styledText and savi the data as an encoded array. Adding data and style runs to the array look complex but no impossible and may offer greater flexibility in the future.

best wishes

Simon K
best wishes
Skids

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10127
Joined: Fri Feb 19, 2010 10:17 am

Re: Working with Styled text - a few questions

Post by richmond62 » Tue Dec 30, 2014 12:55 pm

"But sort and filter are based on ASCII values"

Um . . . I have a feeling they are (now) based on Unicode values.

Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

Re: Working with Styled text - a few questions

Post by Simon Knight » Tue Dec 30, 2014 1:27 pm

"But sort and filter are based on ASCII values"

Um . . . I have a feeling they are (now) based on Unicode values.
Probably either, but if the htmltext is saved and then reused it is also plain ASCII text (or unicode) so the sorts and filters have to remove the formatting commands.

However, it seems that things are a little more complex. If I create some fancy text in a field and then save the "effective htmltext" to a variable my variable is populated with my text data wrapped in html tags. If this is then loaded into a field most of the styles are correctly displayed (I am having problems with the backgroundcolor of the field but that is probably a bug in my code) but if I then attempt to modify a value that originally applied to the whole field e.g. textcolor the value loaded in the htmltext appears to take precedence.

I am setting font size, font colour and background colour of the field in my prototype stack by writing to the field values i.e. they apply to all the text in the field. When I save the data in the field using the effective htmltext these values are being picked up and saved (ignoring the problem with background colour for the moment). When the text is reloaded using

Code: Select all

set the htmltext of fld "myfield" to tFancyText
styled text is displayed but if I attempt to change the font size or colour the new settings are ignored. This is a surprise. The work around is to save the values as part of the record and then re-apply the values when the record is loaded, when I do this I will also stop using the "effective" keyword when saving the data which may simplify the html text somewhat.

My next step is to produce a list where each record /line has its background set according to a value in the data.
best wishes
Skids

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10058
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Working with Styled text - a few questions

Post by FourthWorld » Tue Dec 30, 2014 7:58 pm

HtmlText is designed to provide a plain text representation which can be used to faithfully reproduce the contents of a field, but not the field itself.

This can be very useful for having runs that are bold, italic, etc., but which use the current system font on each platform the app is run on, by just setting the field's textFont property, or other cases where you want field properties to be reflected across the text as a whole.

If you need to apply the field's textFont property to its contents, you could use:

Code: Select all

set the textFont of char 1 to -1 of fld "SomeField" to the effective textFont of fld "SomeField
Alternatively, you could store the field properties separately, applying them to any other field you want the text displayed in.

The "effective" keyword is useful there, since it allows for cases where the field doesn't have its own textFont property set, and will instead inherit the textFont of the card, and if the card doesn't have it set it will be inherited from the stack, and if not set in the stack it will inherit the engine defaults. Querying "the textFont" of a field may return empty if not set in the field itself, but "the effective textFont" will always return a value; "effective" can also be used with textSize, textHeight, textStyle, and possibly a few others.

Note that when any runs that have a given attribute assigned that are also field properties (textStyle, textFont, textSize, and a few others), those runs will override the values of the field properties. For example, if your field is set to use a "Helvetica" as its textFont and you have some runs that use "Times Roman", the runs with the textfont assigned will continue to render with Times Roman even as their textSize will reflect that of the field.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10058
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Working with Styled text - a few questions

Post by FourthWorld » Tue Dec 30, 2014 8:03 pm

And a tip for working with textStyles, introduced in v5.5.4 IIRC:

In that version forward you can query the textStyle of a chunk and it'll return either a comma-separate list of styles or "mixed" if the styles are not present across the chunk as a whole.

In some cases it's more convenient to isolate specific textStyles and work with them as Booleans, and now we can do that by using the array form of textStyle:

Code: Select all

set the textStyle["bold"] of the selectedChunk to (not the textStyle["bold"] of the selectedChunk)
See the Dictionary entry for textStyle for a complete list of available styles. Not as complete as CSS, but more than many word processors offer.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

Re: Working with Styled text - a few questions

Post by Simon Knight » Fri Jan 02, 2015 11:29 pm

Hi Richard,

Thanks for both your replies and I apologise for not seeing them earlier.

I am creating a simple (ha) note taking application for my own use. I'm doing it partly to replace an OmniOutliner File plus Applescripts that is creaking a bit and partly to have a go both with styled text and datagrid forms having not used either in the past.

I was wondering the best/approved method of storing and processing styled data given that I will want to sort and filter the data at some point. Things appear to get quite complex if for example I store the styled text of a number of records for display in a datagrid form and then want to sort the data. I have to admit that I have postponed working out how the dgdata might be sorted/filtered : dgdata[1]["MyStyledField"] [tstyledtextarray[1]]...... head begins to spin at this point ;-) , I guess a cop out method would be to store a plaintext copy of the data for sorting/filtering.

best wishes

Simon K.
best wishes
Skids

Post Reply