Skip to content

Instantly share code, notes, and snippets.

@ltlapy
Last active April 10, 2023 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ltlapy/75d31653436855476c8535ec194d504e to your computer and use it in GitHub Desktop.
Save ltlapy/75d31653436855476c8535ec194d504e to your computer and use it in GitHub Desktop.
VTX 벡터 그래픽
# 먼저 실행하시오(기반 함수들)
def str_chunk(data, size):
return [data[i:i+size] for i in range(0, len(data), size)]
# 주어진 정수를 n비트 단위로 쪼갬
def bit_chunk(data: int, bit, size=0, significant='MSB') -> list:
res = []
while data > 0:
res.append(data % (2 ** bit))
data = data // (2 ** bit)
while len(res) < size:
res.append(0)
if significant == 'MSB':
res.reverse()
return res
# 주어진 정수를 바이트 단위로 쪼개서 bytes로 만듬 (bit_chunk의 바이트 단위 문법 설탕)
def byte_chunk(data, size=2, significant='MSB') -> bytes:
return bytes(bit_chunk(data, 8, size, significant))
# PDI 연산값(파라미터) 생성기. 단수형과 2,3차원 복수형을 지원
def param(a, chunk=3, upper=True) -> bytes:
if type(a) == int:
a = [a]
baah = []
if len(a) == 1:
chunk_bits = 6
else: chunk_bits = (8-2)//len(a) # by default, 2-dimension is by 2 values in 3 chunks of *3 bits*(total of 8 + a signbit)
for c in a:
# sign bit
signbit = False
if c < 0:
signbit = True
c = 2**(chunk_bits*chunk-1)-1 + c + 1
# if c > 127:
# signbit = not signbit
# c %= 128
# bit chunk
chunks = bit_chunk(c, chunk_bits, chunk, 'MSB')
if signbit:
chunks[0] += signbit << (chunk_bits - 1)
baah.append(chunks)
out = b''
for ba in zip(*baah):
base = 0x40 + (0x80 if upper else 0)
for i, n in enumerate(ba):
# 0b00xx-xyyy or 0x00xx-yyzz
base += n << (len(a)-1-i)*chunk_bits
out += byte_chunk(base, 1)
return out
# 좌표값을 위한 PDI 연산값(파라미터) 생성기. 0-1의 실수로 입력
def xy(x, y, w=255, h=199):
# KS X 3057, 48p : x[0-1] y[0-.7825]
x = round(x * w)
y = round(y * h)
return param((x, y))
s.write(b'\x1B\x25\x41') # SVTX
s.write(b'\x1B\x63') # RIS. VTX-NSR-8비트-화면-키보드로 초기화
s.write(b'\x9F') # NSR, 개방영역 시작
# s.write(b'\xB8') # FIELD,
# s.write(xy(.0, .0) + xy(1,1)) # xy-dxy: (.20,.20,.60,.60)
# s.write(b'\xA1\xC8' + param((1,1))) # DOMAIN, 단1복3, line=(.02,.02)
s.write(b'\xB2'+ xy(.00,.00) + xy(1,1))
s.write(b'\xB6')
for pos in (
( .50, .80), # Start Point
(-.10, -.60), # Delta Point 01
( .30, .40), # Delta Point ...
(-.40, .00),
( .30, -.40),
):
s.write(xy(*pos))
s.write(b'\xA4' + xy(.23, .1)) # POINT SET (비가시)
s.write(b'baaaaa... ')
s.write(b'\x1B\x24\x2A\x43' # KSC 5601 한글 문자집합 => G2
+ b'\x1B\x7D') # LS2R: G2 -> GR
s.write('냐가 해냈다냥...'.encode('euc-kr', 'replace'))
s.write(b'\x1B\x7E') # LS1R: G1 -> GR
# s.write(b'\x1B\x7D') # LS2R: 보조문자(G2)를 GR로 할당
# s.write(b'\x1B\x7E') # LS1R: G1 -> GR
# s.write(b'\x85') # C1@END, 영역 종료
s.write(b'\x1A') # SDC, 개방영역 종료
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment