[Solved] Random font and size for letters

Keyboard macros or custom scripts

[Solved] Random font and size for letters

Postby Bondrenko » Tue Apr 16, 2013 7:32 pm

Hi!

I want to write script that randomly (from range i specify) changes size and font for every letter in the document. But i have no experience of writing such scripts at all :( . Please can someone help with this idea? (and sorry my English) :)
Last edited by Bondrenko on Mon Apr 22, 2013 12:16 am, edited 1 time in total.
Debian 6.0. OOo 3.2.1
Bondrenko
 
Posts: 4
Joined: Tue Apr 16, 2013 7:21 pm

Re: Random font and size for letters

Postby mauriciobaeza » Wed Apr 17, 2013 12:17 am

Hi...

In Calc or Writer?... in Basic or Python?...

Best regards
______________________________________________
Everything not given is lost
AOO 4.1 / LibO 4.3 on ArchLinux with Gnome3
Please, I do not answer private questions, you use the forum
mauriciobaeza
 
Posts: 56
Joined: Thu Apr 22, 2010 5:03 am

Re: Random font and size for letters

Postby Bondrenko » Wed Apr 17, 2013 7:46 am

Hi! Thanks for your reply!
Writer. Basic is more preferable.
Debian 6.0. OOo 3.2.1
Bondrenko
 
Posts: 4
Joined: Tue Apr 16, 2013 7:21 pm

Re: Random font and size for letters

Postby Charlie Young » Thu Apr 18, 2013 8:02 pm

I'm not clear on what you mean by a specified range, but, despite some problems to be mentioned, this might be a start.

This gets all the installed fonts, as an array of FontDescriptors. This will include such things as math and symbol fonts, and non-Latin alphabets, so it may include many more things than you want.

Code: Select all   Expand viewCollapse view
Function getFontList()

   Dim w As Object
   Dim dev As Object
   Dim FontList
      
   w = ThisComponent.CurrentController.Frame.ComponentWindow
   dev = w.createDevice(100,100) 'Size (100, 100) is arbitrary
   FontList = dev.getFontDescriptors()
   
   getFontList = FontList
End Function


Then we can scan a text character-by-character, and randomly change the size and font of each

Code: Select all   Expand viewCollapse view
Sub JumbleLetters()
   Dim oDoc As Object
   Dim oText As Object
   Dim oCursor As Object
   Dim MoreText As Boolean
   Dim u As Long
   Dim FontList
   
   oDoc = ThisComponent
   oText = oDoc.Text
   FontList = getFontList()
   u = UBound(FontList)
   oCursor = oText.createTextCursor()
   oCursor.gotoStart(False)
   MoreText = oCursor.goRight(1,True)
   do while MoreText
      oCursor.CharHeight = Rnd()*24
      oCursor.CharFontName = FontList(Int(Rnd()* u)).Name
      oCursor.collapseToEnd()
      MoreText = oCursor.goRight(1,True)
   loop
   
End Sub



Apart from the use of the horrible Basic Rnd() function, the main problem I'm having with this is that the cursor seems to move extremely slowly as it proceeds into a paragraph, so this takes a long time to run on even a fairly short document. I might be missing something that others could spot.

This is roughly based on something I've done before, and I want to try it in another language to see if it has the same problems.
Apache OpenOffice 4.1.1
Windows XP
User avatar
Charlie Young
Volunteer
 
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Random font and size for letters

Postby Bondrenko » Fri Apr 19, 2013 11:07 am

Charlie Young, thanks a lot! It works great! :D
But is there a way to select not all but some fonts i only need? for example only Times new roman, Arial and Dejavy sans and variety of their sizes between 12 and 17?
Debian 6.0. OOo 3.2.1
Bondrenko
 
Posts: 4
Joined: Tue Apr 16, 2013 7:21 pm

Re: Random font and size for letters

Postby Charlie Young » Fri Apr 19, 2013 5:14 pm

Bondrenko wrote:
But is there a way to select not all but some fonts i only need? for example only Times new roman, Arial and Dejavy sans and variety of their sizes between 12 and 17?


That is simpler, I think. There are various ways to deal with the sizes in this case, but I'll just apply the same thing I'm doing with the fonts. Put as many font names and sizes as you want in the Array() statements. There need not be the same number of each.

Code: Select all   Expand viewCollapse view
Sub JumbleLetters2()
   Dim oDoc As Object
   Dim oText As Object
   Dim oCursor As Object
   Dim MoreText As Boolean
   Dim nList As Long, nSize As Long
   Dim FontList, FontSize
   
   oDoc = ThisComponent
   oText = oDoc.Text
   FontList = Array("Arial","Times New Roman","DejaVu Sans","Century Gothic")
   FontSize = Array(12,13,13.5,14,16.2,17)
   nList = UBound(FontList)
   nSize = UBound(FontSize)
   MoreText = True
   oCursor = oText.createTextCursor()
   oCursor.gotoStart(False)
   MoreText = oCursor.goRight(1,True)
   do while MoreText
      oCursor.CharHeight = FontSize(Int(Rnd()* nSize))
      oCursor.CharFontName = FontList(Int(Rnd()* nList))
      oCursor.collapseToEnd()
      MoreText = oCursor.goRight(1,True)
   loop
   
End Sub



Unexpectedly, this runs much faster.
Apache OpenOffice 4.1.1
Windows XP
User avatar
Charlie Young
Volunteer
 
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Random font and size for letters

Postby Bondrenko » Mon Apr 22, 2013 12:15 am

Charlie Young you are my savior.
Exactly what i need! Thank you very much! =)
Debian 6.0. OOo 3.2.1
Bondrenko
 
Posts: 4
Joined: Tue Apr 16, 2013 7:21 pm


Return to OpenOffice Basic, Python, BeanShell, JavaScript

Who is online

Users browsing this forum: No registered users and 5 guests