Archive for the ‘pyGtk’ Category

cpConverter: a code page converter for the Linux environment

cpConverter

cpConverter

cpConverter is a simple utility to change the code page of plain text based files (text files, subtitle files, source code files etc.) aimed for the Linux gnome environment.

You can convert to and from any of the 84 code pages the program supports (UTF, Latin 1, Greek, Japanese, Chinese, Russian, etc.)

It supports multiple files selection, making a backup of your files before converting, and preview  of the files in a separate multitab window to check if you have selected the proper encoding.

It is released under the Gnu GPL lisense, that means it is free software.

For more info and download link, head to the program’s page here.

Advertisements

How to find if a specific font is installed in a user’s system (with PyGtk)

After 10 years of working on Windows I recently switched to Linux and especially Ubuntu (‘working’ is not the right word ’cause actually I don’t work with a pc and I’m not a professional programmer, I learned programming by myself and for the fun of it. I just use my pc for fun, entertainment and to broaden my knowledge on various fields.)
But I found myself that so far in Ubuntu I’m missing a lot of things from Windows. No, I don’t miss Windows as an OS, not at all, but I do miss a lot of applications written for that specific OS (and I don’t want to use Wine to run them in Ubuntu, I want native applications.)
So, I found that Linux lacks a decent .nfo and ascii art viewer. Well I know that there is NFO Viewer, but you must download it first and then compile it and then install it. Ok, I did that, it’s a very descent program, but I’m not fully satisfied with it. So what can I do? Write an .nfo viewer myself of course! Even for the fun of it and for learning a lot of things in the process, both for pyGtk and Linux (and Python.)
So, I started. But I came across my first problem. That is, .nfo files and ascii art in general use codepage cp437 (or IBM437) for the encoding and in Windows to view it properly you must select the Terminal font in your application, a fixed-size font used in the Windows command line with special characters that an .nfo file use. But there isn’t in Ubuntu a font that is close to the Windows Terminal font. All the native Ubuntu fixed-size fonts are way too far from the Windows one. I think the most close native font is DejaVu Sans Mono, but still it lacks a lot to view .nfo and ascii art properly.
But I searched… and found at last the Terminus font. A font that I must say is almost identicall to the Windows one. But you must download it and install it if you wanna use it. And as far as regarding my program, I don’t want to force the user to download it and install it (a hypothetical story of course, if I ever gonna release that program.)
So back to code and to my problem, I wanted to check if the user has that specific font in his/her system and then load it. If I don’t find it then set another font as the default one so as to keep the user close to see what ascii art is. And all of these because the default font that gtk+ and pango use is not a fixed-size one (I think it uses one from the Sans family).
But how to check if the user has that font? I didn’t know, so I started googling first and reading the docs, both pyGtk ones and Python. I posted also in a couple of forums my question. But I didn’t get any answer. Something was missing. What was that? Well, the answer was simple after all, and it was lying inside the pango docs that I didn’t read carefully at first. So I read it all over again and found the answer (and realized for once more that first I must read the docs carefully.)

And here it is if you ever need to search for a specific font in the user’s system and set it as the default for your application if found, or otherwise set a secondary as default:


# first you must import pango of course
import pango

# The get_pango_context() method returns the pango.Context with
# the appropriate colormap, font description and base direction
# for the selected widget (text_view in our case, assuming
# text_view is your gtk.TextView)

context = text_view.get_pango_context()

# list_families() returns a tuple containing a set of the fonts
# that pango can use with the selected widget

fonts = context.list_families()

# so we iterate in the font tuple
for font in fonts:
    # if our font is found (get_name returns a string)
    if font.get_name() == 'Terminus':
        # set that font for our text_view and break the loop
        text_view.modify_font(pango.FontDescription('Terminus 8'))
        break
    else:
        # else set our second choice font
        text_view.modify_font(pango.FontDescription('DejaVu Sans Mono 8'))

Pretty simple, isn’t it?

Hope it’ll help someone as it did to me.

[PS: First I must apologize for the long, boring and tiring story just to present 9 lines of code and second I must apologize for my bad english. But you see, english is not my native language.]