Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # encoding=utf-8
- from __future__ import print_function, unicode_literals
- # Copyright (c) 2010, Lee Sun-yeon <lee_soonyeon@gmail.com>
- LEAD_BASE = 0x1100 # lead consonant, choseong: 초성
- VOWEL_BASE = 0x1161 # medial vowel, chungseong:중성
- TAIL_BASE = 0x11A7 # tail consonant, chongseong:종성
- SYLLABLE_BASE = 0xAC00 # syllable, 44032
- VOWEL_RANGE = [VOWEL_BASE, 0x1175]
- LEAD_COUNT = 19
- VOWEL_COUNT = 21
- TAIL_COUNT = 28
- N_COUNT = VOWEL_COUNT * TAIL_COUNT # 588
- SYLLABLE_COUNT = LEAD_COUNT * N_COUNT # 11172
- JAMO_LEAD_TABLE = ['g', 'gg', 'n', 'd', 'dd', 'r', 'm', 'b', 'bb', 's', 'ss', '', 'j', 'jj', 'c', 'k', 't', 'p', 'h']
- JAMO_VOWEL_TABLE = ['a', 'ae', 'ya', 'yae', 'eo', 'e', 'yeo', 'ye', 'o', 'wa', 'wae', 'oe', 'yo', 'u', 'weo', 'we', 'wi', 'yu', 'eu', 'yi', 'i']
- 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']
- def decompose(s):
- code_point = ord(s)
- index = code_point - SYLLABLE_BASE
- if index not in xrange(0, SYLLABLE_COUNT):
- return [(0, 0, 1, 1, s)]
- initial = index / N_COUNT
- vowel = (index % N_COUNT) / TAIL_COUNT
- final = index % TAIL_COUNT
- return boxes(JAMO_LEAD_TABLE[initial], JAMO_VOWEL_TABLE[vowel], JAMO_TAIL_TABLE[final])
- # Copyright (c) 2013, David Kreuter <dkreuter@gmail.com>
- def boxes(lx, ly, lz):
- side = ("a", "i", "e", "eo", "yeo", "ae")
- if ly in side: c, j = 0, 0.5
- elif ly[0] == "w" or ly == "yi": c, j = 2, 0.65
- else: c, j = 1, 0.65
- if not lz: j = 1.0
- a = (
- [(0.0, 0.0, 0.5, j, lx), (0.5, 0.0 , 1.0, j, ly)],
- [(0.0, 0.0, 1.0, 0.5*j, lx), (0.0, 0.5*j, 1.0, j, ly)],
- [(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:])]
- )[c]
- if len(lz)==2:
- a.append((0.0, j, 0.5, 1.0, lz[0]))
- a.append((0.5, j, 1.0, 1.0, lz[1]))
- elif lz:
- a.append((0.0, j, 1.0, 1.0, lz))
- return a
- def mkboxy(x): return unichr(ord(x)+0xFEC0)
- import cairo
- bs = 38
- if "short version":
- text = "위키백과는 위키를 이용하여 전 세계 사람들이 함께 만들어가는 웹 기반의 다언어 백과사전입니다."
- col = 15
- else:
- text = "위키백과는 위키를 이용하여 전 세계 사람들이 함께 만들어가는 웹 기반의 다언어 백과사전입니다. 위키백과는 중립적이고 검증 가능한 자유 콘텐츠 백과사전의 제공을 목적으로 하는 프로젝트로, 누구나 참여하여 문서를 수정하고 발전시킬 수 있습니다. 위키백과는 다섯 가지 기본 원칙에 따라 운영됩니다. 모든 문서는 크리에이티브 커먼즈 저작자표시-동일조건변경허락 3.0에 따라 사용할 수 있으며, 복사, 수정과 배포가 자유롭고 상업적 목적의 사용도 가능합니다."
- col = 30
- lines = []
- while text:
- lines.append(text[:col])
- text = text[col:]
- WIDTH, HEIGHT = col*bs, len(lines)*2*bs
- surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
- ctx = cairo.Context(surface)
- ctx.set_source_rgb(0.0, 0.0, 0.0)
- ctx.paint()
- ctx.set_line_width(0.05)
- def lboxed(a, b, c, d, e):
- if not e: return
- uu = ctx.text_extents(e)
- #pprint(uu)
- if uu[2]*uu[3] == 0: return
- ctx.save()
- ctx.translate(a, b)
- ctx.scale((c-a), (d-b))
- ctx.scale(1.0/uu[2], 1.0/uu[3])
- ctx.translate(-uu[0], -uu[1])
- ctx.move_to(0, 0)
- ctx.show_text(e)
- ctx.restore()
- def p(l):
- ctx.save()
- ctx.set_operator(cairo.OPERATOR_ADD)
- ctx.set_font_size(1.0)
- ctx.select_font_face("Century SchoolBook", 0, 0)
- xx = decompose(l)
- #for a, b, c, d, e in xx:
- # ctx.set_source_rgb(1.0, 0.0, 0.0)
- # ctx.rectangle(a, b, c-a, d-b)
- # ctx.stroke()
- for a, b, c, d, e in xx:
- if not e: continue
- ctx.save()
- #e = "".join(map(mkboxy, e))
- e = e.upper()
- ctx.set_source_rgb(0.1, 0.3, 1.0)
- lboxed(a, b, c, d, e)
- ctx.restore()
- ctx.set_source_rgb(1.0, 0.0, 0.0)
- ctx.select_font_face("SourceHanSansKR" or "NanumMyeongjo", 0, 0)
- lboxed(0, 1, 1, 2, l)
- ctx.restore()
- ctx.scale(bs, bs)
- import translit
- for i, line in enumerate(lines):
- print(translit.romanize(line, from_enc=None))
- for j, l in enumerate(line):
- ctx.save()
- ctx.translate(j, 2*i)
- p(l)
- ctx.restore()
- surface.write_to_png("output.png")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement