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

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

问题描述

我们有一个小型网络应用程序,我们希望将其转换为原生应用程序.现在,它有很多活动部件(后端、浏览器等),我们想将它转换成一个单一的紧凑应用程序.我们决定使用 PyGame 来做到这一点,到目前为止一切都很好,除了字体渲染问题.

我想渲染的字符串是कोझिकोड.这个,正确渲染看起来像 .

具体码位为u0915u094bu091du093fu0915u094b和u0921

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

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

我对 unicode 没有太多经验,所以我不知道如何解决这个问题.我能做些什么来解决这个问题吗?

以防万一,我正在使用

第一个单词正确呈现,但我们通过反转元音和字母位置来完成,正如我在粗略修复中提到的那样.第二个写得正确但没有正确呈现.

更新 2:在没有其他任何东西的情况下,我决定尝试使用外部程序将字符串渲染成图像,然后将该图像 blit 到 PyGame Surface 上.我尝试过 imagemagick,但它以同样的方式让我们感到困惑.Gimp 工作正常,所以我打算使用批处理模式来完成我的工作.

解决方案

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

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 .

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

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.

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.

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?

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

This is what it looks like

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.

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.

解决方案

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天全站免登陆