Paul McNett wrote:
> Paul McNett wrote:
>> They need to be registered with reportlab before you can use them. Sorry, I 
>> don't 
>> remember how to do it but I figured it out for you before, when we were 
>> working on 
>> displaying special unicode chars like that c with the squiggly.
>>
>> For some reason I'm not able to find that mail exchange, but I gave you code 
>> to 
>> register the needed font... if you find it, let me know!
> 
> Okay, check out:
> http://leafe.com/archives/showMsg/374228
> 
>  >> 9. what is the right way to "install a font"? currently I have this in my 
> code:
>  >>
>  >> pdfmetrics.registerFont(TTFont("FreeSans",
>  >> "/usr/share/fonts/truetype/freefont/FreeSans.ttf"))
>  >> rw = dReportWriter(OutputFile=buffer, ReportFormFile=xmlfile, Cursor=[ds])
>  >>
>  >> Do I need to do that every time?
>  >
>  >
>  > You don't need to do that every time you run the report, just the first
>  > time after reportlab is first imported.
> 
> So, somewhere in the initialization of your app:
> 
> from reportlab.pdfbase import pdfmetrics
> from reportlab.pdfbase.ttfonts import TTFont
> pdfmetrics.registerFont(TTFont("<font name>", "<abs_path_to_font_file>"))
> 
> Then, you should be able to refer to that font in your rfxml.

oh yeah, that... :)

ok, I hacked something together that 'works' but.. well.. it needs to be done 
right.  not sure what right is, but this ain't it.

What I did: pass the full pathname to the font file as parameters.  code looks 
at extension and does the best it can to figure out the FontName and registers 
it.  So because I use 3 fonts, I do this:

$ python /home/carl/dabo-trunk/ide/ReportDesigner.py \
-r /usr/share/fonts/type1/gsfonts/n019004l.afm  \
-r /usr/share/fonts/type1/gsfonts/n019003l.afm \
-r fonts/FreeSans.ttf \
badge.rfxml

.afm files contain the font name, so it looks it up.
.ttf, no clue, so I used the file name: FreeSans.

The design sucks, the code could be worse... the only thing it has going for it 
is Works For Me(tm).

I am really not sure what the right way to map font's to file names is.  I have 
a feeling a good first step would be to just store the full
(path/filename, fontname) in the .rfxml.  or maybe just 
(font_name,base_filename.ext)  and store the local paths to fonts in 
DaboPreferences.db or an environment var.  (and hope that there isn't more than 
one n019004l.afm or FreeSans.ttf in those dirs.)  Or maybe the OS has something 
to offer.

I haven't touched the web server code yet, but part of my design allows me to 
easily reuse this code.  (I think.)  So whatever the final code is, it should 
be 
able to work in that environment too.    mainly  dReportWriter.py should not 
require DaboPreferences.db - Not sure how much additional functionality I would 
want.  If anything, I am thinking a .initfonts([path1_to_font,path2_to_font...])

Carl K

Index: /home/carl/dabo-trunk/ide/ReportDesigner.py
===================================================================
--- /home/carl/dabo-trunk/ide/ReportDesigner.py (revision 4898)
+++ /home/carl/dabo-trunk/ide/ReportDesigner.py (working copy)
@@ -1,6 +1,10 @@
  #!/usr/bin/env python
  # -*- coding: utf-8 -*-
  import sys, os, copy
+import os.path
+
+from optparse import OptionParser
+
  import dabo, dabo.ui
  dabo.ui.loadUI("wx")
  import dabo.dEvents as dEvents
@@ -10,6 +14,8 @@
  from dabo.ui import dKeys
  import ClassDesignerPropSheet

+from reportlab.pdfbase import pdfmetrics
+from reportlab.pdfbase.ttfonts import TTFont

  rdc = None

@@ -2537,17 +2543,75 @@
                self.Form.onFilePreviewReport(None)
                dabo.ui.callAfter(self.Form.pgf._setSelectedPageNumber, 0)

-if __name__ == "__main__":
+def get_fontname(fontfile):
+       """
+       Retrieves the FontName from the .afm file.
+
+       c...@dell29:~/dabo-trunk/ide$ grep FontName 
/usr/share/fonts/type1/gsfonts/n019004l.afm
+       FontName NimbusSanL-Bold
+       """
+       for line in open(fontfile).readlines():
+               lline = line.split()
+               if lline[0]=='FontName': return lline[1]
+       return None
+
+def reg_font_afm(head,root):
+       afmFile = os.path.join(head,root+".afm")
+       pfbFile = os.path.join(head,root+".pfb")
+       faceName = get_fontname(afmFile) # pulled from AFM file
+       print faceName
+       justFace = pdfmetrics.EmbeddedType1Face(afmFile, pfbFile)
+       pdfmetrics.registerTypeFace(justFace)
+       justFont = pdfmetrics.Font(faceName, faceName, 'WinAnsiEncoding')
+       pdfmetrics.registerFont(justFont)
+       return
+
+def reg_font_ttf(fontname,fontfile):
+       pdfmetrics.registerFont(TTFont(fontname,fontfile ))
+       return
+
+def reg_font(fontpathname):
+       """
+       given full path/name.ext of font file,
+       split into head,root,ext and punt.
+       """
+
+       head,tail = os.path.split(fontpathname)
+       print head,tail
+       root,ext = os.path.splitext(tail)
+       print root,ext
+       if ext=='.ttf': reg_font_ttf(root,fontpathname)
+       if ext=='.afm': reg_font_afm(head,root)
+
+       return
+
+
+def run( options, args ):
+
        app = DesignerController()
        app.setup()

-       if len(sys.argv) > 1:
-               for fileSpec in sys.argv[1:]:
+       for fontfile in options.fonts:
+               reg_font(fontfile)
+
+       if args:
+               for fileSpec in args:
                        form = ReportDesignerForm()
-                       form.editor.openFile("%s" % fileSpec)
+                       form.editor.openFile(fileSpec)
                        form.Visible = True
        else:
                form = ReportDesignerForm()
                form.editor.newFile()
                form.Visible = True
        app.start()
+
+if __name__ == "__main__":
+       
+       parser = OptionParser()
+       parser.add_option("-r", "--register-font",
+               action="append", dest="fonts", metavar="path to font",
+               help="register the font.  supply the full path, one per -r.", )
+       (options, args) = parser.parse_args()
+
+       run( options, args )
+




--- StripMime Report -- processed MIME parts ---
multipart/mixed
  text/plain (text body -- kept)
  text/x-patch
---

_______________________________________________
Post Messages to: Dabo-users@leafe.com
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-users
Searchable Archives: http://leafe.com/archives/search/dabo-users
This message: http://leafe.com/archives/byMID/495974bf.9000...@personnelware.com

Reply via email to