def display_text( string, bgcolor=bgcolor, dfont=pygame.font.Font(myexpefont, round(2 / 3 * myexpefontsize))): # Display text font = dfont # Font name and size if textdirection == 'RTL': text = font.render( pyfribidi.log2vis(string), 1, fgcolor) # Set text to display, antialiasing and color if DEBUG: print(string, pyfribidi.log2vis(string)) else: text = font.render( string, 1, fgcolor) # Set text to display, antialiasing and color if DEBUG: print(string) textpos = text.get_rect() # Get coordinates of the surface # needed for text display textsize = text.get_size() textpos.centerx = background.get_rect().centerx # set text # x-position # centered textpos.centery = background.get_rect().centery # set text # y-position # centered background.fill(bgcolor) # Blank the background surface window.blit(background, (0, 0)) # Blit it window.blit(text, textpos) # Blits the text to the coordinates pygame.display.flip() # Flip the display return (textsize, textpos)
def format(self, item): if item.price_include_lawyer == None: return '---' elif item.price_include_lawyer == False: return log2vis(gettext('no')) elif item.price_include_lawyer == True: return log2vis(gettext('yes'))
def testCp1255NaturalRTL(self): """ other encodings: cp1255 """ charset = 'cp1255' self.assertEqual( pyfribidi.log2vis(u"שלום - hello".encode(charset), encoding=charset), u"hello - םולש".encode(charset))
def testUTF16NaturalRTL(self): """ other encodings: utf-16 """ charset = 'utf-16' self.assertEqual( pyfribidi.log2vis(u"שלום - hello".encode(charset), encoding=charset), u"hello - םולש".encode(charset))
def testIso8859_8NaturalRTL(self): """ other encodings: iso8859-8 """ charset = 'iso8859-8' self.assertEqual( pyfribidi.log2vis(u"שלום - hello".encode(charset), encoding=charset), u"hello - םולש".encode(charset))
def biditext_cb(data, modifier, modifier_data, line): # We assume a tab separates the nick from the text text = line.split('\t', 1) if len(text) > 1: text[1] = log2vis(text[1], ON) return '\t'.join(text) return line
def format(self, item): clients = item.clients str2 = '' parts = clients.strip().split('\r\n') parts.reverse() for s in parts: str2 += log2vis(s) return Paragraph(str2, styleRow10)
def format(self, item): ''' item is of type EmployeeSalary ''' val = '' sales = item.sales for project, sales in sales: val += log2vis("%s - %s" % (project, len(sales))) + '<br/>' return val
def testBigString(self): """ unicode: big string It does not make sense to order such big strings, this just checks that there are no size limits in pyfribidi. """ # About 2MB string for default python build (ucs2) big = (u'א' * 1024) * 1024 self.assertEqual(pyfribidi.log2vis(big), big)
def testBigString(self): """ utf8: big string It does not make sense to order such big strings, this just checks that there are no size limits in pyfribidi. """ # About 2MB string big = ('א' * 1024) * 1024 self.assertEqual(pyfribidi.log2vis(big), big)
def display_text(string, direction): pygame.font.init() font = pygame.font.Font(myexpefont, myexpefontsize) # Font name and size #text = font.render(string, 1, (250, 250, 250)) # Set text to display, antialiasing and color if direction == 'RTL': text = font.render( pyfribidi.log2vis(string), 1, fgcolor) # Set text to display, antialiasing and color print string, pyfribidi.log2vis(string) else: text = font.render( string, 1, fgcolor) # Set text to display, antialiasing and color print string textpos = text.get_rect() # Get surface size information textpos.centerx = background.get_rect().centerx # set x position textpos.centery = background.get_rect().centery # set y position screen.fill(bgcolor) screen.blit(text, textpos) pygame.display.flip()
def _formatText(self, text): "Generates PDF text output operator(s)" text = pyfribidi.log2vis(text, RTL) if self._dynamicFont: results = [] font = pdfmetrics.getFont(self._fontname) for subset, chunk in font.splitString(text, self._canvas._doc): if subset != self._curSubset: pdffontname = font.getSubsetInternalName(subset, self._canvas._doc) results.append("%s %s Tf %s TL" % (pdffontname, fp_str(self._fontsize), fp_str(self._leading))) self._curSubset = subset chunk = self._canvas._escape(chunk) results.append("(%s) Tj" % chunk) return string.join(results, ' ') else: text = self._canvas._escape(text) return "(%s) Tj" % text
def on_button_clicked(obj): inText = clip.wait_for_text() outText = pyfribidi.log2vis(inText, pyfribidi.ON) clip.set_text(outText)
def testNaturalLTR(self): """ unicode: reorder LTR line by natural order """ self.assertEqual(pyfribidi.log2vis(u"hello - שלום", ON), u"hello - םולש")
def biditext_cb(data, modifier, modifier_data, line): return log2vis(line, LTR)
def testEmpty(self): """ utf8: empty string """ self.assertEqual(pyfribidi.log2vis(''), '')
def test_glibc_free_invalid_next_size(self): # *** glibc detected *** /home/ralf/py27/bin/python2: free(): invalid next size (fast): 0x00000000011cff00 *** pyfribidi.log2vis( '\xf0\x90\x8e\xa2\xf0\x90\x8e\xaf\xf0\x90\x8e\xb4\xf0\x90\x8e\xa1\xf0\x90\x8f\x83' )
def test_glibc_free_invalid_next_size(self): # *** glibc detected *** /home/ralf/py27/bin/python2: free(): invalid next size (fast): 0x00000000011cff00 *** pyfribidi.log2vis('\xf0\x90\x8e\xa2\xf0\x90\x8e\xaf\xf0\x90\x8e\xb4\xf0\x90\x8e\xa1\xf0\x90\x8f\x83')
def testReorderNonSpacingMarks(self): """unicode: reorder non spacing marks""" self.assertEqual(pyfribidi.log2vis(u"חַיְפַא", RTL), u"אפַיְחַ" )
def testNoReorderNonSpacingMarks(self): """unicode: reorder non spacing marks""" self.assertEqual(pyfribidi.log2vis(u"חַיְפַא", RTL, reordernsm=False), u"אַפְיַח")
def testReorderNonSpacingMarks(self): """unicode: reorder non spacing marks""" self.assertEqual(pyfribidi.log2vis(u"חַיְפַא", RTL), u"אפַיְחַ")
def testNaturalRTL(self): """ unicode: reorder RTL line by natural order """ self.assertEqual(pyfribidi.log2vis(u"שלום - hello", ON), u"hello - םולש")
def testAsLTR(self): """ unicode: reorder line as LTR """ self.assertEqual(pyfribidi.log2vis(u"hello - שלום", LTR), u"hello - םולש")
def testAsRTL(self): """ unicode: reorder line as RTL """ self.assertEqual(pyfribidi.log2vis(u"hello - שלום", RTL), u"םולש - hello")
def testDefaultDirection(self): """ unicode: use RTL default """ self.assertEqual(pyfribidi.log2vis(u"hello - שלום"), pyfribidi.log2vis(u"hello - שלום", RTL))
def prep_arabic(string): '''Prepares strings with arabic in them for being printed to the terminal ''' return pyfribidi.log2vis(string)
def testIso8859_8NaturalRTL(self): """ other encodings: iso8859-8 """ charset = 'iso8859-8' self.assertEqual(pyfribidi.log2vis(u"שלום - hello".encode(charset), encoding=charset), u"hello - םולש".encode(charset))
text = text.encode('%(encoding)s') """ % locals() if encoding in ['unicode', 'utf-8']: # utf-8 is the default encoding for strings code = "pyfribidi.log2vis(text)" else: # other encodings require encoding parameter code = "pyfribidi.log2vis(text, encoding='%s')" % encoding timer = timeit.Timer(code, setup) seconds = timer.timeit(number=tests) microseconds = 1000000 * seconds / tests print("%12s: %.8f seconds (%.2f usec/pass)" % (encoding, seconds, microseconds)) # warm up caches for i in xrange(100000): pyfribidi.log2vis(u'Some text to warm up the caches') lines = 50 # typical screen of text print('\ntime to reorder %s lines:\n' % lines) for encoding in hebrew_encodings: timeEncoding(encoding, lines) lines = 100000 print('\ntime to reorder %s lines:\n' % lines) for encoding in hebrew_encodings: timeEncoding(encoding, lines)
def testUTF16NaturalRTL(self): """ other encodings: utf-16 """ charset = 'utf-16' self.assertEqual(pyfribidi.log2vis(u"שלום - hello".encode(charset), encoding=charset), u"hello - םולש".encode(charset))
def testNoReorderNonSpacingMarks(self): """unicode: reorder non spacing marks""" self.assertEqual(pyfribidi.log2vis(u"חַיְפַא", RTL, reordernsm=False), u"אַפְיַח" )
# utf-8 is the default encoding for strings code = "pyfribidi.log2vis(text)" else: # other encodings require encoding parameter code = "pyfribidi.log2vis(text, encoding='%s')" % encoding timer = timeit.Timer(code, setup) seconds = timer.timeit(number=tests) microseconds = 1000000 * seconds / tests print "%12s: %.8f seconds (%.2f usec/pass)" % (encoding, seconds, microseconds) # warm up caches for i in xrange(100000): pyfribidi.log2vis(u'Some text to warm up the caches') lines = 50 # typical screen of text print print 'time to rerorder %s lines:' % lines print for encoding in hebrew_encodings: timeEncoding(encoding, lines) lines = 100000 print print 'time to rerorder %s lines:' % lines print for encoding in hebrew_encodings: timeEncoding(encoding, lines)
def simple_render(text): result = log2vis(text) return result
def testEmpty(self): """ unicode: empty string """ self.assertEqual(pyfribidi.log2vis(u''), u'')
def testCp1255NaturalRTL(self): """ other encodings: cp1255 """ charset = 'cp1255' self.assertEqual(pyfribidi.log2vis(u"שלום - hello".encode(charset), encoding=charset), u"hello - םולש".encode(charset))
def get_extents(font_name ,text, font_size): result = log2vis(text) result = shape(font_name, text, font_size) x_advance = sum([char['x_advance'] for char in result]) y_advance = 0 return x_advance, y_advance
def _getFragWords(frags, use_bidi=False): ''' given a Parafrag list return a list of fragwords [[size, (f00,w00), ..., (f0n,w0n)],....,[size, (fm0,wm0), ..., (f0n,wmn)]] each pair f,w represents a style and some string each sublist represents a word ''' R = [] # the collection of frag information W = [] # a frag per word n = 0 # width of the frag hangingStrip = False for f in frags: text = f.text if use_bidi and pyfribidi: text = pyfribidi.log2vis(text) # PDF renderers commonly mishandle uFEFF, but it's safe to delete it, so do that now. if u'\ufeff' in text: text = u''.join([c for c in text if c != u'\ufeff']) else: is_indic = False for c in text: if 2304 <= ord(c) <= 3071 or 3398 <= ord(c) <= 3415 or 4096 <= ord(c) <= 4191: is_indic = True break if is_indic: dependent_vowels = (u'\u0a3f', u'\u0bc6', u'\u0bc7', u'\u0bc8', u'\u09bf', u'\u09c7', u'\u09c8', u'\u0d46', u'\u0d47', u'\u0d48', u'\u093f', u'\u0b47', u'\u0abf', u'\u1031', u'\u0dd9', u'\u0ddb') text = [c for c in text] k = 1 for c in text[1:]: if c in dependent_vowels: text[k-1], text[k] = text[k], text[k-1] k += 1 text = u''.join(text) #del f.text # we can't do this until we sort out splitting # of paragraphs if text!='': if hangingStrip: hangingStrip = False text = text.lstrip() #if type(text) is str: # text = text.decode('utf8') S = split(text) if S==[]: S = [''] # not sure what's going on here yet if W!=[] and text[0] in whitespace: W.insert(0,n) R.append(W) W = [] n = 0 # loop through all but the last word for w in S[:-1]: W.append((f,w)) n += stringWidth(w, f.fontName, f.fontSize) W.insert(0,n) R.append(W) W = [] n = 0 # handle the last word # WTF IS ALL OF THIS. # set the word to the last word, because we skipped it in the for loop above w = S[-1] W.append((f,w)) n += stringWidth(w, f.fontName, f.fontSize) # if the last character in the text is whitespace # then go ahead and add the wordfrag structure # otherwise it will be caught by the last if W != [] # and the exact same thing will happen.. WTF # ^ but only if this is the last frag # otherwise this frag gets included in with something else # W is not always length = 2 when it is appended if text and text[-1] in whitespace: W.insert(0,n) R.append(W) W = [] n = 0 #elif hasattr(f,'cbDefn'): # w = getattr(f.cbDefn,'width',0) # if w: # if W!=[]: # W.insert(0,n) # R.append(W) # W = [] # n = 0 # R.append([w,(f,'')]) # else: # W.append((f,'')) elif hasattr(f, 'lineBreak'): #pass the frag through. The line breaker will scan for it. if W!=[]: W.insert(0,n) R.append(W) W = [] n = 0 R.append([0,(f,'')]) hangingStrip = True if W!=[]: W.insert(0,n) R.append(W) return R
def test_log2vis(self): result = pyfribidi.log2vis(self.text) self.assertEqual(self.result, result)
def format(self, item): return log2vis(item.project.initiator)
def visualizeIfUnicode(s): return pyfribidi.log2vis(s, base_direction=pyfribidi.ON ).decode('utf-8')
def format(self, item): return log2vis(item.project.name_and_city)
def bidi(s,enabled=DEFAULT_BIDI): if not enabled: return s return log2vis(s,base_direction=LTR)
def on_button_clicked(obj): inText = clip.wait_for_text() outText = pyfribidi.log2vis (inText, pyfribidi.ON) clip.set_text(outText)