I'm relatively new to programming and recently I've started playing around with pygame (set of modules to write games in python). I'm looking to create a program/game in which some of the labels, strings, buttons, etc are in Arabic. I'm guessing that pygame has to support Arabic letters and it probably doesn't? Or could I possibly use another GUI library that does support Arabic and use that in unison with pygame? Any direction would be much appreciated!
Well Python itself uses Unicode for everything so that's not the problem. A quick googling also shows that PyGame should be able to render Unicode fonts just fine. So I assume the problem is more that it can't find fonts for the specific language to use for rendering.
Here is a short example for PyGame and especially this link should be useful.
This is the important library - so specifying a font that can render your language and using it to render it should work fine. Probably a good idea to write a small wrapper
Nb: Haven't used PyGame myself so this is based on speculation and some quick search about how PyGame renders fonts.
PS: If you want the game to work reliably for all of your users, it's probably a good idea to include an Open Source font in your release, otherwise you need some methodology to check if the user has some fonts installed that will work fine - a probably non-trivial problem if you want Xplattform support.
Python does support unicode-coded source.
Set the coding of your source file to the right type with a line of the form # coding: [yourCoding] at the very beginning of your file. I think # coding: utf-8 works for Arabic.
Then prepend your string literals with a u, like so:
u'アク'
(Sorry if you don't have a Japanese font installed, it's the only one I had handy!)
This makes python treat them as unicode characters. There's further information specific to Arabic on this site.
Both previous answers are great.
There is also a great built-in python function called unicode. It's very easy to use.
I wanted to write Hebrew text so I wrote a function:
def hebrew(text):
# Encoding that supports hebrew + punctuation marks
return unicode(text, "Windows-1255")
Then you can call it using:
hebrew("<Hebrew text>")
And it will return your text Hebrew encoded.
Related
When I open a file which has multilanguage character contents arabic contents are not rendering correctly, I setup encoding to utf-8 but did not help. How do you solve this?
On windows the process for getting code to display correctly includes all of:
including a comment on the second line of each source file with #coding:utf-8, the first should really be the shebang #!/usr/bin/env python
ensure that you have a font installed with good Unicode support, including the character page that you are going to be using - Consolas (the default for Wing-IDE) is usually a good choice but AFAIK does not include the full Arabic character ranges however Lucida Console should provide this.
Ensure that Wing-IDE has the Edit->Preferences->User Interface->Fonts->Editor Font/Size set to the selected font, i.e. Licida Console.
Is there a way to change which character is used for QT's QTextOption.ShowTabsAndSpaces flag?
I find that the default character that's used for viewing whitespace (specifically spaces) stands out a little too much. I'd like to change the font or character used so that it's less distinct.
It looks like the character used is unicode "Middle Dot", · (U+00B7) and I'd like to use, say, U+02D1 ˑ.
Ideally I'd like to be able to set it to whatever the user wants.
I've been searching through the Qt docs and have only been able to find how to turn this flag on (here).
EDIT:
I guess I should show some code... Here's how I'm currently adding the whitespace indicators:
opts = self.document().defaultTextOption()
opts.setFlags(opts.flags() | QTextOption.ShowTabsAndSpaces)
self.document().setDefaultTextOption(opts)
Running Python 3.4 and PyQt4, but should be able to port C++ code over.
EDIT2:
Thanks to Andrei Shikalev's answer below, I've posted a feature request for this on the QT tracker: https://bugreports.qt.io/browse/QTBUG-46072
Currently we could not change characters for tabs and white space. This characters hardcoded in Qt source for QTextLayout:
QChar visualTab(0x2192);
...
QChar visualSpace((ushort)0xb7);
More info in source for QTextLayout on GitHub.
You can create feature request for tabs and white spaces custom characters. IMHO this feature will be useful for custom-looking editors, based on Qt.
Before I get to the actual question I will say that altough I'm currently working in Python I will accept a solution in ANY language. I'm mostly a Java programmer but since Java is pretty limited to its JVM I didn't think it would be possible to create this in Java.
Goal:
I'm trying to make a program that will intercept keyboard events (I've already done this part using pyHook, this is one of the main reasons I am programming this in Python). Based on these events and the context I need to write unicode characters (ancient-greek) into any focused window (Currently only on Windows OS but an uniform solution that will work on all OS's seems ideal). Basically this is a program that allows me (Classical Language Student) to type Ancient Greek.
Problems:
Everything is working great up until the point where I need to send unicode characters, like an alpha, delta or omega, using sendKeys. The hook works perfectly and SendKeys works perfectly with normal ASCII characters. I've tried the following libraries all to no avail: (Code example at the bottom)
SendKeysCtypes (contrary to what the blog says it does NOT support unicode)
win32com.client using the shell and SendKeys.
SendKeys (Another library doing basically the same thing)
Now that I've outlined my current situation I've got the following questions:
Questions
1. Is it at all possible to use unicode characters with SendKeys? (google searches thus far seem to indicate that it is impossible).
Since this is likely not the case I wonder:
2. Is there any other library capable of sending unicode characters to the focused window?
Another thing that has crossed my mind is that I might be using the wrong method altogether (the whole simulating keypress events thing). Any other solution that will help me reach, or at least get closer to, my goal are VERY welcome.
#coding: utf-8
import time
import win32com
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shell.Run('notepad')
time.sleep(0.1)
shell.AppActivate('kladblok')
shell.SendKeys("When Unicode characters are pasted here, errors ensue", 0)
shell.SendKeys(u"When Unicode characters are pasted here, harmony shall hopefully ensue".encode("utf-16le"), 0)
You have not followed up on questions in comments, so this is necessarily speculative.
From a scripting language (Python or Ruby, say) on a Debian-based system, I would like to find either one of:
All the Unicode codepoints that a particular font has glyphs for
All the fonts that have glyphs for a particular Unicode codepoint
(Obviously either 1 or 2 can be derived form the other, so whatever is easier would be great.) I have done this in the past by running:
fc-list : file charset
... and parsing the output at the end of each line, based on this code from fontconfig
but it seems to me that there ought to be a much simpler way of doing this.
(I'm not completely sure this is the right StackExchange site for this question, but I am looking for an answer that can be used programmatically.)
I would try any of the FreeType 2 language bindings. Here's a Perl solution to list the Unicode code points of a font using Font::FreeType:
use Font::FreeType;
Font::FreeType->new->face('DejaVuSans.ttf')->foreach_char(sub {
printf("%04X\n", $_->char_code);
});
I've recently listed the mapping from unicode codepoints to glypths in a TTF using TTX/FontTools. That tool is written in Python, so it matches the Python tag in your post. The command
ttx -t cmap foo.ttf
will generate an XML file foo.ttx which describes that mapping, for various environments and encodings. See e.g. this reference for a description of what the platform and encoding identifiers actually mean. I assume that the package can be used as a library as well as a command line tool, but I have no experience there.
I want to write a little IDE for Cython using PyQT, but I don't have any idea how to implement Syntax Highlighting.
I know how to parse the Python-source, but I don't know how I can set the color for different words within the Textfield in PyQT.
I could use HTML for this, but how does it work in realtime ? I mean when the user edits the text I need to be able to immediately change the text's format, etc.
Do you know how I can achieve this ?
Have you considered using QScintilla?
"As well as features found in standard text editing components, QScintilla includes features especially useful when editing and debugging source code. These include support for syntax styling, error indicators, code completion and call tips."
I would recommend checking out the code in KhtEditor which is written in Python using PyQt. I believe the author is also working on a port from QWidget to QML.