在PyGame中梵文文本渲染不正确 [英] Devanagari text rendering improperly in PyGame

查看:159
本文介绍了在PyGame中梵文文本渲染不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个小型Web应用程序,我们希望将其转换为本地应用程序.现在,它有很多移动部件(后端,浏览器等),我们希望将其转换为一个紧凑的应用程序.我们决定使用PyGame进行此操作,到目前为止,除了字体渲染问题外,一切都很好.

We have a small web app that we want to convert into something native. Right now, it's got a lot of moving parts (the backend, the browser etc.) and we'd like to convert it into a single tight application. We decided to use PyGame to do this and it's been fine so far except for a font rendering issue.

我要呈现的字符串为.正确呈现的外观类似于.

The string I'd like to render is कोझिकोड. This, correctly rendered looks like .

具体的代码点是\ u0915 \ u094b \ u091d \ u093f \ u0915 \ u094b和\ u0921

The specific code points are \u0915 \u094b \u091d \u093f \u0915 \u094b and \u0921

现在,在我的编辑器和浏览器中看起来不错,但是当我尝试在PyGame中渲染它时,我得到了这个.基本上,元音符号(\ u093fि)应该位于झ的左侧,但它出现在其右侧(和南部)的左侧,从而将其完全弄乱了.这在浏览器或文本编辑器(具有相同的输入字符串)中不会发生,因此我猜测它是PyGame中的渲染器问题.

Now, this looks fine in my editor and my browser but when I try to render it in PyGame, I get this . Basically, the vowel sign (\u093f ि) should have been on the left of the झ but it appears to its right (and to the left of the क) thereby messing it up completely. This doesn't happen in a browser or a text editor (with the same input string) so I'm guessing it a renderer problem in PyGame.

有一个仅适用于此特定情况的粗略解决方案,即将ि(\ u093f)放在झ(\ u091d)之前.在这种情况下,它可以像.这依赖于我对语言有所了解并将该逻辑放入代码中.我必须在这里处理多种语言,所以这实际上是不可行的.

There is one crude fix which works only in this specific case which i s to put the ि (\u093f) before the झ (\u091d). In that case, it renders properly like so . This relies on me knowing something about the language and putting that logic into the code. I have to deal with multiple languages here so that's not really feasible.

我对Unicode没有太多经验,所以我不知道如何解决这个问题.有什么我可以解决的吗?

I don't have much experience with unicode so I don't know how to approach this problem. Is there something I can do to fix this?

如果有问题,我使用的是 freesans 字体在Debian上,它具有必要的字形来呈现此图形.

In case it matters, I'm using the freesans font which is there on Debian and which has the necessary glyphs to render this.

更新: 实际呈现此代码的代码如下

Update: The code to actually render this is as follows

# -*- coding: utf-8 -*-
import time

import pygame

# Pygame setup and create root window
pygame.font.init()
screen = pygame.display.set_mode((320, 200))
empty = pygame.Surface((320, 200))


font_file = pygame.font.match_font("freesans")  # Select and
font = pygame.font.Font(font_file, 30)          # open the font
writing = font.render(u"कोिझकोड  कोझिकोड", True, (0, 0, 0))  # Render text on a surface
screen.fill((255, 255, 255)) # Clear the background
screen.blit(writing, (10, 10)) # Blit the text surface on the background
pygame.display.flip()  # Refresh the display

input() # Wait for input before quitting

这是

第一个单词正确呈现,但我们已通过反转原始拼音中提到的元音和字母位置来做到这一点.第二个写正确,但呈现不正确.

The first word is rendered correctly but we've done it by inverting the vowel and the letter positions as I mentioned in the crude fix. The second is written properly but not rendered correctly.

更新2 : 在没有其他条件的情况下,我决定尝试使用外部程序将字符串渲染为图像,然后将此图像转换为PyGame Surface.我尝试了imagemagick,但它以这种方式使我们感到困惑. Gimp可以正常工作,因此我计划使用批处理模式来完成我的工作.

Update 2: In the absence of anything else, I've decided to try to render the string into an image using an external program and then blit this image onto the PyGame Surface. I tried imagemagick but it messes us in the same way as this. Gimp works fine and so I'm planning to use the batch mode to get my work done.

推荐答案

对于我自己的情况,我最终不得不求助于一个非常丑陋但可用的解决方法.我写了一个script-fu插件,它使用文件名和一段文本作为参数.然后写出文本,并使用gimp将其保存为png文件.然后,我的程序将其加载并直接将png蒙皮到表面上.

I had to finally resort to a really ugly but usable workaround for my own situation. I wrote a script-fu plugin which takes a filename and a piece of text as arguments. It then writes out the text and saves it a png file using gimp. My program then loads this up and blits the png directly onto the surface.

这篇关于在PyGame中梵文文本渲染不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆