Page 1 of 1
[Solved] Random font and size for letters
Posted: Tue Apr 16, 2013 7:32 pm
by Bondrenko
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)
Re: Random font and size for letters
Posted: Wed Apr 17, 2013 12:17 am
by mauriciobaeza
Hi...
In Calc or Writer?... in Basic or Python?...
Best regards
Re: Random font and size for letters
Posted: Wed Apr 17, 2013 7:46 am
by Bondrenko
Hi! Thanks for your reply!
Writer. Basic is more preferable.
Re: Random font and size for letters
Posted: Thu Apr 18, 2013 8:02 pm
by Charlie Young
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
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
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.
Re: Random font and size for letters
Posted: Fri Apr 19, 2013 11:07 am
by Bondrenko
Charlie Young, thanks a lot! It works great!
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?
Re: Random font and size for letters
Posted: Fri Apr 19, 2013 5:14 pm
by Charlie Young
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
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.
Re: Random font and size for letters
Posted: Mon Apr 22, 2013 12:15 am
by Bondrenko
Charlie Young you are my savior.
Exactly what i need! Thank you very much! =)