`
zuroc
  • 浏览: 1291756 次
  • 性别: Icon_minigender_1
  • 来自: 江苏
社区版块
存档分类
最新评论

视觉长度截断函数

阅读更多
#长度的来源见附件中的html

_visable_width=0, 24, 24, 24, 24, 24, 24, 24, 24, 0, 0, 0, 0, 0, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
12, 12, 16, 28, 28, 44, 32, 8, 16, 16, 20, 28, 12, 16, 12, 12, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 12, 12, 28, 28, 28, 28, 48, 28, 32,
36, 36, 32, 28, 36, 36, 12, 24, 32, 28, 36, 36, 36, 32, 36, 36, 32,
28, 36, 28, 44, 28, 28, 28, 12, 12, 12, 20, 28, 16, 28, 28, 24, 28,
28, 12, 28, 28, 12, 12, 24, 12, 44, 28, 28, 28, 28, 16, 28, 12, 28,
20, 36, 20, 20, 20, 16, 12, 16, 28, 24
#中文长48

def visable_length(word):
   length=len(word)
   width=0
   i=0
   while i<length:
       c=word[i]
       if c>"\xE0" and c<="\xEF":
           i+=3
           width+=48
       else:
           if c<"\xC0":
               i+=1
               width+=_visable_width[ord(c)]
           else:
               width+=48
               if c<="\xDF":i+=2
               elif c<="\xF7":i+=4
               elif c<="\xFB":i+=5
               else:i+=6
   return width

def visable_cut(word,limit,etc="..."):
   i=0
   length=len(word)
   if length<=limit:return word
   limit = limit*48
   width = 0
   while i<length:
       c=word[i]
       if c>"\xE0" and c<="\xEF":
           offset=3
           width+=48
       else:
           if c<"\xC0":
               offset=1
               width+=_visable_width[ord(c)]
           else:
               width+=48
               if c<="\xDF":offset=2
               elif c<="\xF7":offset=4
               elif c<="\xFB":offset=5
               else:offset=6
       if width>limit:
           etc_length=(48+visable_length(etc))/48
           return word[:i-etc_length]+etc
       else:
           i+=offset
   return word
1
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics