changeset 0:f19f72547e25 draft

Initial commit
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Mon, 25 Sep 2017 15:31:21 +0200
parents
children 0c9d7f3334a0
files font454.py src/font544.png src/generate.py src/micropython.py src/test.png src/test.py
diffstat 6 files changed, 133 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/font454.py	Mon Sep 25 15:31:21 2017 +0200
@@ -0,0 +1,54 @@
+from micropython import const
+
+
+_FONT = (
+    b'{{{{{{wws{w{HY{{{{YDYDY{sUtGU{H[wyH{uHgHE{ws{{{{vyxyv{g[K[g{{]f]{{{wDw{{'
+    b'{{{wy{{{D{{{{{{{w{K_w}x{VHHHe{wuwww{`KfyD{UKgKU{w}HDK{DxTKT{VxUHU{D[wyx{'
+    b'UHfHU{UHEKe{{w{w{{{w{wy{KwxwK{{D{D{{xwKwx{eKg{w{VIHyB{fYH@H{dHdHd{FyxyF{'
+    b'`XHX`{DxtxD{Dxtxx{FyxIF{HHDHH{wwwww{KKKHU{HXpXH{xxxxD{Y@DLH{IL@LX{fYHYf{'
+    b'`HH`x{fYHIF{`HH`H{UxUKU{Dwwww{HHHIR{HHH]w{HHLD@{HYsYH{HYbww{D[wyD{txxxt{'
+    b'x}w_K{GKKKG{wLY{{{{{{{{Dxs{{{{{BIIB{x`XX`{{ByyB{KBIIB{{WIpF{OwUwww{`YB[`'
+    b'x`XHH{w{vwC{K{OKHUxHpXH{vwws_{{dD@H{{`XHH{{fYYf{{`XX`x{bYIBK{Ipxx{{B}_d{'
+    b'wUws_{{HHIV{{HH]s{{HLD@{{HbbH{{HHV[a{D_}D{Cw|wC{wwwwwwpwOwp{uxfKW{@YYY@{'
+)
+_SALT = const(132)
+
+
+def text(buffer, string, x0=0, y0=0, color=0xffff, bgcolor=0, colors=None):
+    """
+    Render the ``string`` as text onto the ``buffer`` starting at
+    (``x0``, ``y0``) and using the specified colors.
+
+    The ``colors`` parameter must be a sequence of length four, representing
+    the colors to be used for rendering, starting from the foreground color,
+    through two shades for antialiasing, and ending with the background color.
+    If ``None`` is used, that color will be transparent. If ``colors`` is not
+    specified, ``color`` and ``bgcolor`` will be used, without antialiasing.
+
+    Characters outside the ASCII range are replaced with a box.  Most control
+    characters (except for the newline), are ignored.
+    """
+
+    font = memoryview(_FONT)
+    if colors is None:
+        colors = (color, color, bgcolor, bgcolor)
+    x = x0
+    for c in string:
+        if c == '\n':
+            y0 += 6
+            x = x0
+            continue
+        index = min(95, ord(c) - 0x20)
+        if index < 0:
+            continue
+        row = y0
+        index *= 6
+        for byte in font[index:index + 6]:
+            unsalted = byte ^ _SALT
+            for col in range(x, x + 4):
+                color = colors[unsalted & 0x03]
+                if color is not None:
+                    buffer.pixel(col, row, color)
+                unsalted >>= 2
+            row += 1
+        x += 4
Binary file src/font544.png has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/generate.py	Mon Sep 25 15:31:21 2017 +0200
@@ -0,0 +1,34 @@
+import pygame
+
+
+def pack(images, salt):
+    font = bytearray()
+    for image in images:
+        font.extend(
+            (salt ^ sum(colors[tuple(image.get_at((x, y)))] << (x * 2)
+             for x in range(4)))
+            for y in range(6))
+    return font
+
+
+colors = {
+    (0, 0, 0, 0xff): 0,
+    (0x66, 0x66, 0x66, 0xff): 1,
+    (0x99, 0x99, 0x99, 0xff): 2,
+    (0xff, 0xff, 0xff, 0xff): 3,
+}
+image = pygame.image.load("font544.png")
+images = []
+for tile_x in range(0, image.get_size()[0] // 4):
+    rect = (tile_x * 4, 0, 4, 6)
+    images.append(image.subsurface(rect))
+
+
+current = (1000, 0, '')
+for salt in range(0, 255):
+    font = pack(images, salt)
+    current = min(current, (len(repr(font)), salt, font))
+
+print("_SALT = const(%d)" % current[1])
+print("_FONT = %r" % current[2])
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/micropython.py	Mon Sep 25 15:31:21 2017 +0200
@@ -0,0 +1,1 @@
+const = lambda x:x
Binary file src/test.png has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/test.py	Mon Sep 25 15:31:21 2017 +0200
@@ -0,0 +1,44 @@
+import sys
+import os
+import pygame
+
+
+sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
+
+
+import font454
+
+
+COLORS = (
+    (0x00, 0x00, 0x00),
+    (0x44, 0x44, 0x44),
+    (0x88, 0x88, 0x88),
+    (0xff, 0xff, 0xff),
+)
+LOREM_IPSUM = """Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed
+do eiusmod tempor incididunt ut labore et dolore magna aliqua? Ut enim ad minim
+veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
+consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
+cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
+proident: sunt in culpa qui officia deserunt mollit anim id est laborum;
+<@>^(#01+{23})-45=[$6*7/8|9]%~\xff"""
+
+
+class Buffer(object):
+    def __init__(self):
+        self.surface = pygame.Surface((320, 200))
+        self.surface.fill((0x88, 0x88, 0x88))
+
+    def pixel(self, x, y, color):
+        self.surface.set_at((x, y), color)
+
+    def save(self, filename):
+        pygame.image.save(self.surface, filename)
+
+
+buffer = Buffer()
+font454.text(buffer, LOREM_IPSUM, 0, 0, colors=COLORS)
+font454.text(buffer, LOREM_IPSUM, 0, 50, color=COLORS[0], bgcolor=COLORS[-1])
+font454.text(buffer, LOREM_IPSUM, 0, 100, colors=list(reversed(COLORS)))
+font454.text(buffer, LOREM_IPSUM, 0, 150, color=COLORS[-1], bgcolor=COLORS[0])
+buffer.save("test.png")