Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2015
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. # encoding=utf-8
  2.  
  3. from __future__ import print_function, unicode_literals
  4.  
  5. # Copyright (c) 2010, Lee Sun-yeon <lee_soonyeon@gmail.com>
  6.  
  7. LEAD_BASE = 0x1100 # lead consonant, choseong: 초성
  8. VOWEL_BASE = 0x1161 # medial vowel, chungseong:중성
  9. TAIL_BASE = 0x11A7 # tail consonant, chongseong:종성
  10. SYLLABLE_BASE = 0xAC00 # syllable, 44032
  11. VOWEL_RANGE = [VOWEL_BASE, 0x1175]
  12.  
  13. LEAD_COUNT = 19
  14. VOWEL_COUNT = 21
  15. TAIL_COUNT = 28
  16. N_COUNT = VOWEL_COUNT * TAIL_COUNT # 588
  17. SYLLABLE_COUNT = LEAD_COUNT * N_COUNT # 11172
  18.  
  19. JAMO_LEAD_TABLE = ['g', 'gg', 'n', 'd', 'dd', 'r', 'm', 'b', 'bb', 's', 'ss', '', 'j', 'jj', 'c', 'k', 't', 'p', 'h']
  20. JAMO_VOWEL_TABLE = ['a', 'ae', 'ya', 'yae', 'eo', 'e', 'yeo', 'ye', 'o', 'wa', 'wae', 'oe', 'yo', 'u', 'weo', 'we', 'wi', 'yu', 'eu', 'yi', 'i']
  21. JAMO_TAIL_TABLE = ['', 'g', 'gg', 'gs', 'n', 'nj', 'nh', 'd', 'l', 'lg', 'lm', 'lb', 'ls', 'lt', 'lp', 'lh', 'm', 'b', 'bs', 's', 'ss', 'ng', 'j', 'c', 'k', 't', 'p', 'h']
  22.  
  23. def decompose(s):
  24. code_point = ord(s)
  25.  
  26. index = code_point - SYLLABLE_BASE
  27. if index not in xrange(0, SYLLABLE_COUNT):
  28. return [(0, 0, 1, 1, s)]
  29.  
  30. initial = index / N_COUNT
  31. vowel = (index % N_COUNT) / TAIL_COUNT
  32. final = index % TAIL_COUNT
  33.  
  34. return boxes(JAMO_LEAD_TABLE[initial], JAMO_VOWEL_TABLE[vowel], JAMO_TAIL_TABLE[final])
  35.  
  36. # Copyright (c) 2013, David Kreuter <dkreuter@gmail.com>
  37.  
  38. def boxes(lx, ly, lz):
  39. side = ("a", "i", "e", "eo", "yeo", "ae")
  40. if ly in side: c, j = 0, 0.5
  41. elif ly[0] == "w" or ly == "yi": c, j = 2, 0.65
  42. else: c, j = 1, 0.65
  43. if not lz: j = 1.0
  44. a = (
  45. [(0.0, 0.0, 0.5, j, lx), (0.5, 0.0 , 1.0, j, ly)],
  46. [(0.0, 0.0, 1.0, 0.5*j, lx), (0.0, 0.5*j, 1.0, j, ly)],
  47. [(0.0, 0.0, 0.5, 0.5*j, lx), (0.0, 0.5*j, 0.5, j, ly[0]), (0.5, 0.0, 1.0, j, ly[1:])]
  48. )[c]
  49. if len(lz)==2:
  50. a.append((0.0, j, 0.5, 1.0, lz[0]))
  51. a.append((0.5, j, 1.0, 1.0, lz[1]))
  52. elif lz:
  53. a.append((0.0, j, 1.0, 1.0, lz))
  54. return a
  55.  
  56.  
  57. def mkboxy(x): return unichr(ord(x)+0xFEC0)
  58.  
  59. import cairo
  60.  
  61. bs = 38
  62.  
  63. if "short version":
  64. text = "위키백과는 위키를 이용하여 전 세계 사람들이 함께 만들어가는 웹 기반의 다언어 백과사전입니다."
  65. col = 15
  66. else:
  67. text = "위키백과는 위키를 이용하여 전 세계 사람들이 함께 만들어가는 웹 기반의 다언어 백과사전입니다. 위키백과는 중립적이고 검증 가능한 자유 콘텐츠 백과사전의 제공을 목적으로 하는 프로젝트로, 누구나 참여하여 문서를 수정하고 발전시킬 수 있습니다. 위키백과는 다섯 가지 기본 원칙에 따라 운영됩니다. 모든 문서는 크리에이티브 커먼즈 저작자표시-동일조건변경허락 3.0에 따라 사용할 수 있으며, 복사, 수정과 배포가 자유롭고 상업적 목적의 사용도 가능합니다."
  68. col = 30
  69. lines = []
  70. while text:
  71. lines.append(text[:col])
  72. text = text[col:]
  73.  
  74. WIDTH, HEIGHT = col*bs, len(lines)*2*bs
  75.  
  76. surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
  77. ctx = cairo.Context(surface)
  78. ctx.set_source_rgb(0.0, 0.0, 0.0)
  79. ctx.paint()
  80.  
  81. ctx.set_line_width(0.05)
  82.  
  83. def lboxed(a, b, c, d, e):
  84. if not e: return
  85. uu = ctx.text_extents(e)
  86. #pprint(uu)
  87. if uu[2]*uu[3] == 0: return
  88. ctx.save()
  89. ctx.translate(a, b)
  90. ctx.scale((c-a), (d-b))
  91. ctx.scale(1.0/uu[2], 1.0/uu[3])
  92. ctx.translate(-uu[0], -uu[1])
  93. ctx.move_to(0, 0)
  94. ctx.show_text(e)
  95. ctx.restore()
  96.  
  97. def p(l):
  98. ctx.save()
  99. ctx.set_operator(cairo.OPERATOR_ADD)
  100. ctx.set_font_size(1.0)
  101. ctx.select_font_face("Century SchoolBook", 0, 0)
  102. xx = decompose(l)
  103.  
  104. #for a, b, c, d, e in xx:
  105. # ctx.set_source_rgb(1.0, 0.0, 0.0)
  106. # ctx.rectangle(a, b, c-a, d-b)
  107. # ctx.stroke()
  108.  
  109. for a, b, c, d, e in xx:
  110. if not e: continue
  111. ctx.save()
  112. #e = "".join(map(mkboxy, e))
  113. e = e.upper()
  114. ctx.set_source_rgb(0.1, 0.3, 1.0)
  115. lboxed(a, b, c, d, e)
  116. ctx.restore()
  117.  
  118. ctx.set_source_rgb(1.0, 0.0, 0.0)
  119. ctx.select_font_face("SourceHanSansKR" or "NanumMyeongjo", 0, 0)
  120. lboxed(0, 1, 1, 2, l)
  121. ctx.restore()
  122.  
  123. ctx.scale(bs, bs)
  124.  
  125. import translit
  126.  
  127. for i, line in enumerate(lines):
  128. print(translit.romanize(line, from_enc=None))
  129. for j, l in enumerate(line):
  130. ctx.save()
  131. ctx.translate(j, 2*i)
  132. p(l)
  133. ctx.restore()
  134.  
  135. surface.write_to_png("output.png")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement