summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortroido <troido@protonmail.com>2020-09-22 14:07:43 +0200
committertroido <troido@protonmail.com>2020-09-22 14:07:43 +0200
commit5baa21f79f3a2fbee8470a54e81ab2aa99770936 (patch)
tree298884dbf16cd1916bb94fd5ccc09dabe5c9b830
parent2397f225381bafeb87262fef4979ee9eae516717 (diff)
charmap with character_width 2 will automatically convert halfwidth to fullwidth
-rw-r--r--asciifarmclient/charmap.py25
-rw-r--r--asciifarmclient/display.py12
-rw-r--r--asciifarmclient/loaders.py3
-rw-r--r--charmaps/fullwidth.json60
-rw-r--r--charmaps/halfwidth.json2
5 files changed, 36 insertions, 66 deletions
diff --git a/asciifarmclient/charmap.py b/asciifarmclient/charmap.py
index 43ae006..593364c 100644
--- a/asciifarmclient/charmap.py
+++ b/asciifarmclient/charmap.py
@@ -2,9 +2,26 @@
from .utils import get
from ratuil.textstyle import TextStyle
+from ratuil import strwidth
ALPHABET = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
+
+def make_text_wide(char):
+ if len(char) != 1 or strwidth.strwidth(char) != 1:
+ return char
+ o = ord(char)
+ if o >= ord('!') and o <= ord('~'): # printable ascii character
+ return chr(o - ord("!") + ord('!')) # fullwidth ascii block
+ if char == " ":
+ return chr(12288) # ideographic space
+ return char + char
+
+def make_sprite_wide(sprite):
+ (char, fg, bg) = sprite
+ return (make_text_wide(char), fg, bg)
+
+
def parseSprite(sprite):
if isinstance(sprite, str):
return (sprite, None, None)
@@ -47,4 +64,12 @@ class CharMap:
for name, colour in jsonmap.get("msgcolours", {}).items():
self.message_styles[name] = TextStyle(*colour)
self.character_width = jsonmap.get("charwidth", self.character_width)
+
+ def make_wide(self):
+ self.default = make_sprite_wide(self.default)
+ mapping = {}
+ for key, sprite in self.mapping.items():
+ mapping[key] = make_sprite_wide(sprite)
+ self.mapping = mapping
+ self.alphabet = "".join(make_text_wide(char) for char in self.alphabet)
diff --git a/asciifarmclient/display.py b/asciifarmclient/display.py
index 3928f99..597e502 100644
--- a/asciifarmclient/display.py
+++ b/asciifarmclient/display.py
@@ -30,8 +30,6 @@ class Display:
self.layout.set_target(self.screen)
self.layout.update()
-
-
# temporary, until these have a better place
self.inventory = ListSelector(self.getWidget("inventory"))
self.inventory._debug_name = "inventory"
@@ -61,13 +59,13 @@ class Display:
for cell in cells:
(x, y), spriteNames = cell
if not len(spriteNames):
- char, fg, bg = self.getChar(' ')
+ char, fg, bg = self.charmap.get(' ')
else:
- char, fg, bg = self.getChar(spriteNames[0])
+ char, fg, bg = self.charmap.get(spriteNames[0])
for spriteName in spriteNames[1:]:
if bg is not None:
break
- _char, _fg, bg = self.getChar(spriteName)
+ _char, _fg, bg = self.charmap.get(spriteName)
field.change_cell(x, y, char, TextStyle(fg, bg))
@@ -136,10 +134,6 @@ class Display:
self.layout.update()
self.screen.update()
- def getChar(self, sprite):
- """This returns the character belonging to some spritename. This does not read a character"""
- return self.charmap.get(sprite)
-
def update_size(self):
self.screen.reset()
diff --git a/asciifarmclient/loaders.py b/asciifarmclient/loaders.py
index 790f71a..1694a75 100644
--- a/asciifarmclient/loaders.py
+++ b/asciifarmclient/loaders.py
@@ -67,4 +67,7 @@ def loadCharmap(name):
for template in templates:
charmap.apply_json(template)
+ if charmap.character_width == 2:
+ charmap.make_wide()
+
return charmap
diff --git a/charmaps/fullwidth.json b/charmaps/fullwidth.json
index 37ad20e..191b1bb 100644
--- a/charmaps/fullwidth.json
+++ b/charmaps/fullwidth.json
@@ -1,60 +1,8 @@
{
"templates": ["halfwidth"],
- "mapping":{
- "tree": ["T",0,2],
- "wall": ["#",7,8],
- "builtwall": ["+",7,8],
- "quarry": ["Q",7,8],
- "rock": ["#",8,8],
- "stone": ["o",7],
- "pebble": ["*",7],
- "player": ["@",15],
- "ground": [".",3],
- "grass1": [",",2],
- "grass2": ["'",2],
- "grass3": ["`",10],
- "rabbit": ["b",15],
- "water": ["~",4,4],
- "floor": [".",8],
- "portal": ["$", 5],
- "stairdown": [">",7],
- "stairup": ["<",7],
- "dummy": ["d",3],
- "spikes": ["^",7],
- "goblin": ["g",2],
- "seed": [":",10],
- "plantedseed": [".",10],
- "seedling": [",",10],
- "youngplant": ["v",10],
- "smallplant": ["v",2],
- "plant": ["Y",2],
- "food": ["8",13],
- "troll": ["T",1],
- "sword": ["/", 7],
- "club": ["!", 3],
- "house": ["^", 3, 1],
- "bridge": ["=", 9,3],
- "smallbridge": ["=", 3,4],
- "entry": ["%", 7],
- "fence": ["#", 3],
- "armour": ["[", 7],
- "wound": [" ", 7, 1],
- "rat": ["r",7],
- "closeddoor": ["+",1 ,3],
- "opendoor": [""",3],
- "raindrop": [":",12],
- "snowflake": ["*",15],
- "human": ["U", 7],
- "sign": ["?", 3],
- "spinningwheel": ["5", 3],
- "loom": ["4", 3],
- "cotton": ["8", 15],
- "cottonyarn": ["0", 15],
- "cottoncloth": ["H", 15],
- " ": [" ", 7]
- },
- "default": ["?", 7],
"charwidth": 2,
- "alphabet":
- "!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
+ "mapping": {
+ "house": ["^^", 3, 1],
+ "spikes": ["^^",7]
+ }
}
diff --git a/charmaps/halfwidth.json b/charmaps/halfwidth.json
index a17b925..74d067c 100644
--- a/charmaps/halfwidth.json
+++ b/charmaps/halfwidth.json
@@ -49,7 +49,7 @@
"loom": ["4", 3],
"cotton": ["8", 15],
"cottonyarn": ["0", 15],
- "cottoncloth": ["H", 15],
+ "cottoncloth": ["H", 7],
" ": [" ", 7]
},
"writable": {