OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Creates a text document with a sample of each font

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
AndrewZ
Moderator
Moderator


Joined: 21 Jun 2004
Posts: 4148
Location: Colorado, USA

PostPosted: Tue Dec 07, 2004 6:33 am    Post subject: Creates a text document with a sample of each font Reply with quote

It creates a text document with a sample of each font. The output is suitable for printing.

font sheet.sxw
Back to top
View user's profile Send private message Visit poster's website
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 4021
Location: Lawrence, Kansas, USA

PostPosted: Tue Dec 07, 2004 7:40 am    Post subject: Reply with quote

Nice.

Could you please also post the entire source code here, since this is the Code Snippets section. Be sure to bracket it in a Code block.

End the block with...
[/code]

Begin the block with....
[code]
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
AndrewZ
Moderator
Moderator


Joined: 21 Jun 2004
Posts: 4148
Location: Colorado, USA

PostPosted: Tue Dec 07, 2004 6:29 pm    Post subject: Reply with quote

Code:

REM  *****  BASIC  *****

'A BASIC program for OpenOffice.org that creates a text document with a sample of each available fonts
'Copyright (C) 2004 Andrew Ziem

'Some of this was derived from code to enumerate fonts from Paul Sobolik (psobolik@lycos.com)
'found in <http://www.pitonyak.org/AndrewMacro.sxw>.

'This program is free software; you can redistribute it and/or
'modify it under the terms of the GNU General Public License
'as published by the Free Software Foundation; either version 2
'of the License, or (at your option) any later version.

'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details.

'You should have received a copy of the GNU General Public License
'along with this program; if not, write to the Free Software
'Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.


Sub Main
Dim mNoArgs()
Dim oTextDocument As Object
Dim oDesktop As Object
Dim sUrl As String
Dim oText As Object
Dim oCursor As Object

oDesktop = createUnoService("com.sun.star.frame.Desktop")
sUrl = "private:factory/swriter"
oTextDocument = oDesktop.loadComponentFromURL(sURL,"_blank", 0, mNoArgs())

oText = oTextDocument.Text
oCursor = oText.createTextCursor()


  Dim oToolkit as Object
  oToolkit = CreateUnoService("com.sun.star.awt.Toolkit")

  Dim oDevice as Variant
  oDevice = oToolkit.createScreenCompatibleDevice(0, 0)

  Dim oFontDescriptors As Variant
  oFontDescriptors = oDevice.FontDescriptors

  Dim oFontDescriptor As Object

  Dim sFontList as String
  Dim iIndex as Integer, iStart As Integer, iTotal As Integer, iAdjust As Integer
  iTotal = UBound(oFontDescriptors) - LBound(oFontDescriptors) + 1
  iStart = 1
  iAdjust = iStart - LBound(oFontDescriptors)
  For iIndex = LBound(oFontDescriptors) To UBound(oFontDescriptors)
    oFontDescriptor = oFontDescriptors(iIndex)
    oCursor.charFontName = oFontDescriptor.Name
    oCursor.CharFontPitch = oFontDescriptor.Pitch
    oCursor.CharFontFamily = oFontDescriptor.Family
    oCursor.CharWeight = oFontDescriptor.Weight
    oCursor.CharPosture = oFontDescriptor.Slant   
    oText.insertString(oCursor, oFontDescriptor.Name, FALSE)
    oCursor.charFontName = "Arial"
    oCursor.CharWeight = 0
    oCursor.CharPosture = 0
    oText.insertString(oCursor, " (" & oFontDescriptor.Name, FALSE)
'    oText.insertString(oCursor, " b:" & oFontDescriptor.Weight & " s:" & oFontDescriptor.Slant, FALSE)
    If (100 < oFontDescriptor.Weight) Then
        oText.insertString(oCursor, " bold", FALSE)
        End If
    If (com.sun.star.awt.FontSlant.OBLIQUE = oFontDescriptor.Slant) Then
        oText.insertString(oCursor, " oblique" , FALSE)
        End If       
    If (com.sun.star.awt.FontSlant.ITALIC = oFontDescriptor.Slant) Then
        oText.insertString(oCursor, " italic", FALSE)
        End If       
       
    oText.insertString(oCursor, ")", FALSE)       
    oCursor.charFontName = oFontDescriptor.Name
    oCursor.CharWeight = oFontDescriptor.Weight
    oCursor.CharPosture = oFontDescriptor.Slant       
    oText.insertControlCharacter(oCursor, LINE_BREAK, 0)
    oText.insertString(oCursor, "The quick brown fox jumps over the lazy dog.", FALSE)
    oText.insertControlCharacter(oCursor, APPEND_PARAGRAPH, 0)   
   Next iIndex

End Sub


Back to top
View user's profile Send private message Visit poster's website
cwchia
Super User
Super User


Joined: 09 Jan 2003
Posts: 1063
Location: Malaysia

PostPosted: Sat Apr 16, 2005 11:44 pm    Post subject: Reply with quote

I tried to use the macro in 1.9.93 but the resulted page is not sorted alphabetically like what happened in 1.1.4.

Why? Rolling Eyes
Back to top
View user's profile Send private message
AndrewZ
Moderator
Moderator


Joined: 21 Jun 2004
Posts: 4148
Location: Colorado, USA

PostPosted: Wed Apr 20, 2005 7:04 am    Post subject: Reply with quote

cwchia wrote:
I tried to use the macro in 1.9.93 but the resulted page is not sorted alphabetically like what happened in 1.1.4.

Why? Rolling Eyes


I can confirm that.

It seems that in OOo 1.1 the font list is provided sorted and this behavior changed in OOo 2. It would be necessary add function to sort the font list (but I don't plan more work on this script).
Back to top
View user's profile Send private message Visit poster's website
foxcole
Super User
Super User


Joined: 19 Jan 2006
Posts: 2771
Location: Minneapolis, Minnesota

PostPosted: Tue Apr 04, 2006 8:15 pm    Post subject: Re: Creates a text document with a sample of each font Reply with quote

ahz wrote:
It creates a text document with a sample of each font. The output is suitable for printing.

font sheet.sxw


I like the fact that I can scan a few sheets of paper to find what I need. I rarely know the exact name of the font I'm looking for, so alphabetic sorting isn't all that critical for me.

Very nice snippet! Thank you!
_________________
Cheers!
---Fox

WinXP Pro SP2, OOo Portable 2.3.1, OOo local 2.4 RC4
New OpenOffice forum: http://user.services.openoffice.org/en/forum/
Manuals: http://documentation.openoffice.org/manuals/index.html
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
jrh
Newbie
Newbie


Joined: 22 Apr 2006
Posts: 3
Location: Stokesley, N Yorks, UK

PostPosted: Sat Apr 22, 2006 10:26 am    Post subject: Reply with quote

I am new to OOo macros (but familiar with VB) and decided to improve this code, as an exercise.

- The font names are sorted. (Illustrates sorting an array by sorting an auxiliary array of indices.) Actually, this is not perfect, eg if you change the font weight description " bold" to " Bold", it breaks collation between Lucida Sans and Lucida Sans Typewriter.

- Spell checking is disabled

- More use of subroutines to make the code clearer (at least, I think it helps)

- Blank line output between each font for clarity

Code:

REM  *****  BASIC  *****

'A BASIC program for OpenOffice.org that creates a text document with a sample of each available font
'Copyright (C) 2006 John Hunt

'Much of this was derived from code (C) 2004 Andrew Ziem found at
'<http://www.oooforum.org/forum/viewtopic.phtml?t=14900>.
'Some his code was derived from code to enumerate fonts from Paul Sobolik (psobolik@lycos.com)
'found in <http://www.pitonyak.org/AndrewMacro.sxw>.

'This program is free software; you can redistribute it and/or
'modify it under the terms of the GNU General Public License
'as published by the Free Software Foundation; either version 2
'of the License, or (at your option) any later version.

'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details.

'You should have received a copy of the GNU General Public License
'along with this program; if not, write to the Free Software
'Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

' Tested in OOo 2.0 under Mandriva Linux 2006

option explicit

Sub Main
   const LINE_BREAK = 1
   const APPEND_PARAGRAPH = 5
   
   Dim oTextDocument As Object
   Dim oDesktop As Object
   Dim sUrl As String
   Dim oText As Object
   Dim oCursor As Object
   dim sFullName as string, sNames as variant, vSortedIndex as variant, sLastFullName as string
   dim sFontName as string, sLastFontName as string
   
   oDesktop = createUnoService("com.sun.star.frame.Desktop")
   sUrl = "private:factory/swriter"
   oTextDocument = oDesktop.loadComponentFromURL(sURL, "_blank", 0, array())
   
   oText = oTextDocument.Text
   oCursor = oText.createTextCursor()
   oCursor.CharHeight = 10
   
   Dim oFontDescriptors As Variant
   oFontDescriptors = getFontDescriptors()
   
   Dim oFontDescriptor As Object
   
   ' disable spell checking
   dim aLocale as new com.sun.star.lang.Locale
   aLocale.Language = ""
   aLocale.Country = ""
   oCursor.CharLocale = aLocale
   
   Dim i as Integer
 
     ' list all the font names
   sNames = DimArray(UBound(oFontDescriptors) - LBound(oFontDescriptors))   ' zero based

   For i = LBound(oFontDescriptors) To UBound(oFontDescriptors)
      oFontDescriptor = oFontDescriptors(i)
       with oFontDescriptor
          sFullName =  "  (" & .Name
          If (100 < .Weight) Then sFullName = sFullName & " bold"
          If (com.sun.star.awt.FontSlant.OBLIQUE = .Slant) Then sFullName = sFullName & " oblique"
          If (com.sun.star.awt.FontSlant.ITALIC = .Slant) Then sFullName = sFullName & " italic"
           sFullName = sFullName & ")"
      end with   
      sNames(i - LBound(oFontDescriptors)) = sFullName
   next
 
     ' sort the font names
   vSortedIndex = ArraySortIndexed(sNames)

   ' output font names and sample text in sorted order
   For i = LBound(vSortedIndex) To UBound(vSortedIndex)
        sFullName = sNames(vSortedIndex(i))
        if sFullName <> sLastFullName then   ' my Linux system lists some fonts twice
           sLastFullName = sFullName
          oFontDescriptor = oFontDescriptors(vSortedIndex(i))
         
          with oFontDescriptor
             sFontName = .Name
             if sLastFontName <> sFontName and sLastFontName<>"" then
                ' leave some white space between fonts for legibility
                SetArial(oCursor)
               oText.insertControlCharacter(oCursor, APPEND_PARAGRAPH, 0)
             endif
             sLastFontName = sFontName
             
             SetFont(oCursor, oFontDescriptor)
             oText.insertString(oCursor, sFontName, FALSE)
         
            SetArial(oCursor)
             oText.insertString(oCursor, sFullName, FALSE)
             oText.insertControlCharacter(oCursor, LINE_BREAK, 0)
            
             SetFont(oCursor, oFontDescriptor)
             oText.insertString(oCursor, TestChars(), FALSE)
            
            SetArial(oCursor)
             oText.insertControlCharacter(oCursor, APPEND_PARAGRAPH, 0)   
            
         end with
      endif
   Next i
End Sub

function getFontDescriptors() as variant
   Dim oToolkit as Object
   Dim oDevice as Variant

   oToolkit = CreateUnoService("com.sun.star.awt.Toolkit")
   oDevice = oToolkit.createScreenCompatibleDevice(0, 0)
   getFontDescriptors = oDevice.FontDescriptors
end function   

private sub SetArial(oCursor as object)
   with oCursor
       .charFontName = "Arial"
       .CharWeight = 0
       .CharPosture = 0
   end with
end sub

private sub SetFont(oCursor as object, oFontDescriptors As Variant)
   with oCursor
      .charFontName = oFontDescriptors.Name
      .CharWeight = oFontDescriptors.Weight
      .CharPosture = oFontDescriptors.Slant
   end with
end sub

private function TestChars() as string
   static rslt as string
   dim i as long
   
   if rslt="" then
      for i = 33 to 126
         if chr(i) = "0" or ucase(chr(i)) = "A" then rslt = rslt & " "
         rslt = rslt & chr(i)
         if chr(i) = "9" or ucase(chr(i)) = "Z" then rslt = rslt & " "
      next
      rslt = rslt &  " $£€ ßäéç§¿©®"   ' some example specials
   endif

   TestChars = rslt
end function   


' Sort an array.  Array elements can be anything for which
' the > operator works. Returns an array of indices that when
' used to access the array put the array elements in order.
' The input array need not be zero based. The returned array is
' always zero based.
' Uses Shell sort; simple and much quicker than bubble sort.

function ArraySortIndexed(vaArray As Variant)as variant

    Dim i As Long, iGap As Long, bNoExch As Boolean, iTemp As long
    dim iUB as long, iLB as long
   dim rslt as variant

   iUB = UBound(vaArray, 1)
   iLB = LBound(vaArray, 1)

   if iUB - iLB + 1 = 0 then
      rslt = array()
   else
      ' dimarray cannot create an empty array, and is always zero based
      rslt = DimArray(iUB - iLB)   
   endif
   
   for i = iLB to iUB
      rslt(i - iLB) = i
   next
   
    iGap = (iUB - iLB + 1) \ 2

    Do
        Do
            bNoExch = True
            For i = 0 To UBound(rslt) - iGap
                If vaArray(rslt(i)) > vaArray(rslt(i + iGap)) Then
                    iTemp = rslt(i)
                    rslt(i) = rslt(i + iGap)
                    rslt(i + iGap) = iTemp
                    bNoExch = False
                End If
            Next
        Loop Until bNoExch
        iGap = iGap \ 2
    Loop Until iGap = 0
   
    ArraySortIndexed = rslt
   
End function
Back to top
View user's profile Send private message
andrewk8
Newbie
Newbie


Joined: 28 Mar 2008
Posts: 1

PostPosted: Fri Mar 28, 2008 6:32 pm    Post subject: Reply with quote

I tried both versions (Andrew Ziem's original and John Hunt's improved version). They both run and show the font names in their respective fonts. But what neither does is show the test string in the same font. In both cases, the test strings are shown in Arial.

I'm not a BASIC programmer, but I can figure out that the line "SetFont(oCursor, oFontDescriptor)" just before the test string should be setting the font, but it is obviously not working. What am I missing?

OpenOffice 2.3.0 under Ubuntu 7.10
_________________
--ACK
Back to top
View user's profile Send private message
dysmas
Power User
Power User


Joined: 14 Apr 2007
Posts: 59
Location: Grenoble, France

PostPosted: Sat Mar 29, 2008 9:30 am    Post subject: Reply with quote

it is a strange bug.

A workout is to change setFont to SetArial on line 96. Thus, the name of the font will be written in Arial, which is not the best thing, but the examples which are more important, will be written in the right font.


Code:
             SetArial(oCursor, oFontDescriptor)
             oText.insertString(oCursor, sFontName, FALSE)
         
             SetArial(oCursor)
             oText.insertString(oCursor, sFullName, FALSE)
             oText.insertControlCharacter(oCursor, LINE_BREAK, 0)
           
             SetFont(oCursor, oFontDescriptor)
             oText.insertString(oCursor, TestChars(), FALSE)
           
             SetArial(oCursor)
             oText.insertControlCharacter(oCursor, APPEND_PARAGRAPH, 0)


I don't understand why this happens.

Tested on OO 2.3.0, windows 2000 Pro
_________________
Dysmas
Back to top
View user's profile Send private message
whitejohn29
Newbie
Newbie


Joined: 05 Jul 2008
Posts: 3

PostPosted: Wed Jul 09, 2008 2:00 am    Post subject: Reply with quote

I studied very carefully your code but I do not understand why this line - rslt = rslt & " $�� ���秿��" is used. Can you explain me why you use such strange symbols?.
_________________
John, my web site is here
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group