Line numbers for soft-wrapped styled text?

Alex Tweedly alex at tweedly.net
Sat Mar 25 19:00:32 EDT 2017


Never done it, but I had a quick play ....

and found a way to do it  - but  whether it would count as "efficiently" 
or not is debatable :-(

It does about 4 lines of text per millisecond on my elderly MacBook Pro 
(with LC 9.0.0 DP6)

If you have a way of determining which line(s) are being changed, then 
you could run this only from that line onward  to save some time ....


Here's the code snippet I was playing with; it assumes

field "F1" - the text

field "f2" - the line numbers, these two fields should have matching 
base, height, etc.

Basically, it runs through the original field finding the formatted 
height of each line. I can't find any way to get that except as the 
formattedHeight of an entire field, so it has to loop through each line 
in turn. The easy way to do that (simply adding each line) is painfully 
slow, so instead it loops through the lines, add each one in turn *as 
the second line*, so it get the height difference between that and a 
single line. [ hmmm - not very clear ! Feel free to ask me to explain 
myself better :-)  ]

and finally restores the original field "F1"

Then it runs through the lines of field "F2" setting the 'spacebelow' of 
each line so that it matches the height of the styled & soft-wrapped 
line of the text field.

Like I said - it works (at least for my simple testing), but it ain't 
superfast ....

-- Alex.

local  sHeights
on mouseUp pK
    local tA, tB
    local temp, t1, t2, t3, tt, tBase

    put the millisecs into t1
    set the lockscreen to true
    set the textHeight of fld "f2" to 12
    put the styledtext of fld "f1" into tA
    put empty into tB
    put empty into sHeights
    put 0 into sHeights[0]
    put tA[1] into tB[1]
    -- and make it single line
    put empty into tB[1]["runs"]
    put "abc" into tB[1]["runs"][1]["text"]

    -- and then get the differential height of one simple line
    put tB[1] into tB[2]
    set the styledText of fld "f1" to tB
    put the  formattedHeight of fld "f1"  into tBase

    repeat with i = 1 to the number of lines in the keys of tA
       put tA[i] into tB[2]
       set the styledText of fld "f1" to tB
       put the  formattedHeight of fld "f1" into sHeights[i]
       --       put i && the number of lines in fld "f1" && sHeights[i] 
&CR after msg
    end repeat
    set the styledText of fld "f1" to tA
    put empty into fld "f2"
    repeat with i = 1 to the number of lines in fld "f1"
       put i &CR after tt
    end repeat
    set the text of fld "f2" to tt
    put the millisecs into t2
    repeat with i = 1 to the number of lines in fld "f1"
       put sHeights[i] - tBase into temp
       if temp > 0 then
          set the spacebelow of line i of fld "f2" to temp
       end if
    end repeat
    put the millisecs into t3
    set the lockscreen to false
    put t2 - t1 && t3 - t2 && t3 - t1 &CR after msg
    --put sLineNum && the formattedheight of fld "f2" && tR

end mouseUp




On 25/03/2017 15:36, Richard Gaskin via use-livecode wrote:
> I have an app that allows the user to write styled text, in which any 
> portion of that text may have a variety of styles and sizes, and may 
> be long enough to soft-wrap.
>
> I'd like to add a line-number field next to it, but I haven't come up 
> with an efficient way of setting up those line numbers so that they 
> align vertically with the lines next to them.
>
> Ideally the line numbers would reflect true lines, separated by CRs, 
> as opposed to any soft-wrapped lines.
>
> What I'm after is common in many apps that support line numbers for 
> style text, but alas I don't have a ready solution.
>
> Anyone here done this?  Any tips you can share for doing this 
> efficiently?
>





More information about the use-livecode mailing list