Showing posts with label text processing. Show all posts
Showing posts with label text processing. Show all posts

Monday, 1 July 2019

Creating PDF with links

Livecode permits you to print any part of your programs as a PDF (or on a real printer).
If you want, you can create a PDF with links. The command to use is print link.
For example on a window create a text (http://www.paypal.com) and the use Text-> Link menu. Like this:




For example the button code is:

on mouseUp
   set the printPaperSize to (item 3 to 4 of the rect of this stack)
   put the rect of field "example" into temp
   ask file "Save PDF as:" with "Example.pdf"   
   if it is empty then
      exit mouseUp
   end if
   open printing to PDF it   
   print card 1 of this stack   
   print link to URL "http://www.paypal.com/" with rect temp
   close printing
end mouseUp

This is the result in a PDF reader:


Tuesday, 21 May 2019

Text margins

Default margins are 8 pixel from the border:
We can change them in many ways: with the property margins, all to a single value:

on mouseUp
   set the margins of field 1 to 0
end mouseUp


or with margins and all four different values:

on mouseUp
   set the margins of field 1 to 2,10,10,0
end mouseUp


Or with the single properties: topMargin, rightMargin , bottomMargin , leftMargin.

Monday, 22 April 2019

Using external fonts

You can incorporate fonts in your app or program in livecode, you can use on Windows and Mac the start using font, example:

start using font file "c:/myFont.ttf"

but you have also to add in the list of the file to copy:
On linux is different, you need to copy the font in the folder "~/.fonts/" and then launch fc-cache -f, for example:

create folder "~/.fonts"
revCopyFile "DroidSansMono.ttf", "~/.fonts/DroidSansMono.ttf"
put shell("fc-cache -fv") into temp

Thursday, 11 April 2019

Animated scrolling text

Creating an animated scrolling text can be achieved by livecode in two ways:
  • text manipulation
  • graphic manipulation
We'll see both ways.

TEXT MANIPULATION

Just create a field and put this code in it:

on movingtext mytext, myspeed
   set the realtext of me to mytext
   set the realtext2 of me to mytext
   stripspaces
   set the text of me to empty
   createspaces
   put the realtext2 of me into temp
   put the text of me before temp
   #put the text of me after temp
   set the realtext2 of me to temp
   set the text of me to temp
   set the cyclet of me to true
   cycleText myspeed
end movingtext

on stopcycle
   set the cyclet of me to false
end stopcycle

on stripspaces
   put the realtext2 of me into temp
   if the last char of temp is space then
      delete the last char of me
      set the realtext2 of me to temp
      stripspaces
   end if
end stripspaces

on cycletext myspeed
   put the realtext2 of me into temp
   put the number of chars of temp into nt
   set the text of me to temp
   repeat for nt times
      delete char 1 of me
      if not the cyclet of me then exit repeat
      wait mySpeed sec with messages
   end repeat
   if the cyclet of me then send "cycletext " & myspeed to me in 0 sec
end cycletext

on createspaces
   set the dontwrap of me to true
   put space after me
   if the formattedwidth of me < the width of me then
      createspaces
   end if
end createspaces


Now you have the following messages to use:
  • movingtext TEXT,SPEED: you use this message to change text, start scrolling setting the scroll speed
  • stopcycle to stop scrolling text

GRAPHIC MANIPULATION

Create a field and put this code inside it:

on cycleText myText, mySpeed
   lock screen
   set the locktext of me to true
   set the dontwrap of me to true
   set the pcontinue of me to true
   set the text of me to myText
   clone me as "clonefield"
   put the ID of the last field into tID
   set the pcloneid of me to tID
   set the width of fld id tid to the formattedwidth of fld id tid
   set opaque of field id tid to false
   set showFocusBorder of field id tid to false
   set threeD of field id tid to false
   set showborder of field id tid to false
   set lockText of field id tid to true
   set the left of fld id tid to the right of me
   set the top of fld id tid to the top of me
   create graphic "cycleR"
   put the id of the last graphic into tID2
   set the pgr of me to tID2
   set the style of graphic id tid2 to "rectangle"
   set the rect of graphic id tid2 to the rect of fld id tid
   set the opaque of graphic id tid2 to true
   set the backgroundcolor of graphic id tid2 to the backgroundcolor of this card
   set the linesize of graphic id tid2 to 0
   clone graphic id tid2
   put the id of the last graphic into tid3
   set the pgL of me to tid3
   set the right of graphic id tid3 to the left of me
   set the top of the graphic id tid3 to the top of me
   set the prealText of me to the text of me
   set the text of me to empty
   set the pmyspeed of me to myspeed
   unlock screen
   cycle2 mySpeed
end cycleText

on cycle2 mySpeed
   put the pcloneid of me into temp
   if exists(field id temp) then   
      set the left of fld id temp to (the left of field id temp - 1)
      wait myspeed sec
      if the right of fld id temp < the left of me then
         set the left of fld id temp to the right of me
      end if
   end if
   if the pcontinue of me then send "cycle2 "& mySpeed to me in 0 sec
end cycle2

on StopCycle
   set the pcontinue of me to false
   set the text of me to the pRealText of me
   put the pcloneid of me into tid
   put the pgr of me into tid2
   put the pGL of me into tid3
   delete field id tid
   delete graphic id tid2
   delete graphic id tid3
end StopCycle


RESULTS

You can see results in the following video, the text mode is confinde in it's space but it sn't constant due the difference in size of each letter, on the other hand the gaphic mode is smother but needs on the left and on the righe no other object, because we have to cover the text:

Thursday, 4 April 2019

Fonts and links

Using the property inspector of livecode you can set many effects for your font:
Here the explanations:
  • textFont is the font used
  • textSize is the size of the fonr
  • textStyle can be:
    • bold
    • italic
    • underlined
    • boxed
    • 3D boxed
    • link
    • strikethrough
  • textAling is the alignof the text
  • fixedLineHeight is to permit or not the custom height of each line
  • textHeight is the distance between lines
  • margins is the distance of text from box around it
However you can mix  all styles using the htmlText property, for example if you have a field named label1 and put this code in a button:

on mouseUp
   put "<h1>Titolo 1</h1>" after temp
   put "<h2>Titolo 2</h2>" after temp   
   put "Questo e' un esempio di testo con piu' stili contemporaneamente. Ecco un elenco puntato:" after temp
   put "<ul>" after temp
   put "<li>primo elemento</li>" after temp
   put "<li>secondo elemento</li>" after temp
   put "</ul>" after temp
   put "Ecco un link: <a >www.livecode.com</a>" after temp
   set the htmltext of field "label1" to temp
end mouseUp

Clicking the button, you'll obtain this:

The link www.livecode.com is just text, to activate it you have to use the message linkCLicked, like this:

on linkClicked pLink
   launch url pLink
end linkClicked

The linkClicked will launch the browser to the url of the text of the link, so you can have different links on the same text and all will work correctly.

Wednesday, 3 April 2019

Justified text

One missing thing in livecode is the justified text. However there are some workaround.
The first option is to use the free hhtextEdit widget for livecode:
It's based on the browser widget, full of option. You can download from this link: http://livecodeshare.runrev.com/stack/860/HHTextEditBasic

Otherwise you can use this code in a field:

local finaltext

on closefield
   justifyme
end closefield

on justifyme
   lock screen
   put empty into finaltext
   stripspaces
   set the dontwrap of me to true
   put the text of me into temp
   set the text of me to empty
   replace return with (space & chartonum(1) & space ) in temp #we separete words with newlines attached, and much more, newline is a word now
   put the width of me into tw
   put the number of words of temp into nw
   repeat with i=1 to nw
      if word i of temp is chartonum(1) then
         put return after me
         put the text of me after finaltext
         put empty into me
         next repeat
      end if
      put word i of temp after me
      if the formattedwidth of me > tw then
         delete the last word of me
         addspaces 0
         put word i of temp & space into me         
      else
         put space after me
      end if
   end repeat
   put the text of me after finaltext
   set the text of me to finaltext
   set the dontwrap of me to false
   unlock screen
end justifyme

on addspaces temp
   if the number of words of me is 1 then
      put the text of me after finaltext
      set the text of me to empty
      exit addspaces
   end if
   
   put the width of me into tw
   put the number of words of me -1 into nspaces #the correct number of spaces between words is nspaces -1, but we'll use mod
   put temp mod nspaces into temp
   add 1 to temp
   put space after word temp of me
   if the formattedwidth of me > tw then
      put the number of chars of word 1 to temp of me into tc
      delete char (tc +1) of me
      put the text of me & space after finaltext
      set the text of me to empty
   else
      addspaces temp
   end if
end addspaces

on stripspaces
   put the text of me into temp
   repeat for each char tchar in temp
      if (tchar is space) and (the last char of t2 is space) then
         next repeat
      else
         put tchar after t2
      end if
   end repeat
   set the text of me to t2
end stripspaces

on resizeControl
   justifyme   
end resizeControl


The result is good: