Skip to content

Commit 723b46a

Browse files
committed
fix: trim problem; concat offsetlist param;
feat: glow() add mode param chore: bump version to 2.2.0
1 parent cb491fa commit 723b46a

9 files changed

Lines changed: 125258 additions & 41 deletions

File tree

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
# BDF Parser
1+
# BDF Parser Python library
22

33
[![PyPI package](https://img.shields.io/badge/pip%20install-bdfparser-brightgreen)](https://pypi.org/project/bdfparser/) [![version number](https://img.shields.io/pypi/v/bdfparser?color=green&label=version)](https://github.com/tomchen/bdfparser/releases) [![Actions Status](https://github.com/tomchen/bdfparser/workflows/Test/badge.svg)](https://github.com/tomchen/bdfparser/actions) [![codecov](https://codecov.io/gh/tomchen/bdfparser/branch/master/graph/badge.svg?token=IMVVQEC04H)](https://codecov.io/gh/tomchen/bdfparser) [![License](https://img.shields.io/github/license/tomchen/bdfparser)](https://github.com/tomchen/bdfparser/blob/master/LICENSE)
44

5-
BDF (Glyph Bitmap Distribution; [Wikipedia](https://en.wikipedia.org/wiki/Glyph_Bitmap_Distribution_Format); [Spec](https://font.tomchen.org/bdf_spec/)) format bitmap font file parser library in Python. It has [`Font`](https://font.tomchen.org/bdfparser_py/font), [`Glyph`](https://font.tomchen.org/bdfparser_py/glyph) and [`Bitmap`](https://font.tomchen.org/bdfparser_py/bitmap) classes providing more than 30 enriched API methods of parsing BDF fonts, getting their meta information, rendering text in any writing direction, adding special effects and manipulating bitmap images. It works seamlessly with [PIL / Pillow](https://pillow.readthedocs.io/en/stable/) and [NumPy](https://numpy.org/).
5+
BDF (Glyph Bitmap Distribution; [Wikipedia](https://en.wikipedia.org/wiki/Glyph_Bitmap_Distribution_Format); [Spec](https://font.tomchen.org/bdf_spec/)) format bitmap font file parser library in Python. It has [`Font`](https://font.tomchen.org/bdfparser_js/font), [`Glyph`](https://font.tomchen.org/bdfparser_js/glyph) and [`Bitmap`](https://font.tomchen.org/bdfparser_js/bitmap) classes providing more than 30 chainable API methods of parsing BDF fonts, getting their meta information, rendering text in any writing direction, adding special effects and manipulating bitmap images. It works seamlessly with [PIL / Pillow](https://pillow.readthedocs.io/en/stable/) and [NumPy](https://numpy.org/), and has detailed documentation / tutorials / API reference.
6+
7+
**BDF Parser TypeScript (JavaScript) library** ([documentation](https://font.tomchen.org/bdfparser_js/); [GitHub page](https://github.com/tomchen/bdfparser-js); [npm page](https://www.npmjs.com/package/bdfparser); `npm i bdfparser`) is a port of **BDF Parser Python library** ([documentation](https://font.tomchen.org/bdfparser_py/); [GitHub page](https://github.com/tomchen/bdfparser); [PyPI page](https://pypi.org/project/bdfparser/); `pip install bdfparser`). Both are written by [Tom Chen](https://github.com/tomchen/) and under the MIT License.
8+
9+
The BDF Parser TypeScript (JavaScript) library has a [**Live Demo & Editor**](https://font.tomchen.org/bdfparser_js/editor) you can try.
610

711
Below I'll show you some quick examples, but it is still strongly recommended you go to [**BDF Parser Python Library's official website to read the detailed documentation / tutorials / API reference**](https://font.tomchen.org/bdfparser_py/).
812

9-
Install bdfparser library with [pip](https://pip.pypa.io/en/stable/installing/#do-i-need-to-install-pip):
13+
Install bdfparser Python library with [pip](https://pip.pypa.io/en/stable/installing/#do-i-need-to-install-pip):
1014

1115
```bash
1216
pip install bdfparser
@@ -62,4 +66,5 @@ You probably understand what I did in these examples. Whether you do or not, go
6266
<img src="https://font.tomchen.org/img/bdfparser_py/plot.png" /><br>
6367
<img src="https://font.tomchen.org/img/bdfparser_py/font_preview_part.png" />
6468
</a>
69+
<a href="https://font.tomchen.org/bdfparser_js/editor" title="BDF Parser Live Demo & Code Editor"><img src="https://font.tomchen.org/img/bdfparser_js/bdfparser_live_editor_demo.gif" width="700" alt="BDF Parser Live Demo & Code Editor"></a>
6570
</p>

src/bdfparser/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
https://font.tomchen.org/bdfparser_py/
55
'''
66

7-
__version__ = "2.1.0"
7+
__version__ = "2.2.0"
88

99
from .bdfparser import *

src/bdfparser/bdfparser.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def __parse_headers(self):
149149
comment = 'comment'
150150
if comment not in self.headers:
151151
self.headers[comment] = []
152-
self.headers[comment].append(value)
152+
self.headers[comment].append(value.strip(' "\'\t\r\n'))
153153
elif key == 'SWIDTH':
154154
nlist = value.split()
155155
self.headers['swx0'] = int(nlist[0])
@@ -213,7 +213,7 @@ def __parse_props(self):
213213
comment = 'comment'
214214
if comment not in self.props:
215215
self.props[comment] = []
216-
self.props[comment].append(value)
216+
self.props[comment].append(value.strip(' "\'\t\r\n'))
217217
else:
218218
self.props[key.lower()] = value
219219
elif l == 1:
@@ -420,7 +420,7 @@ def glyphbycp(self, codepoint):
420420
str(codepoint) + ") does not exist in the font. Will return `None`"
421421
)
422422
# Use old style for Python 3.5 support. For 3.6+:
423-
# f"Glyph \"{chr(codepoint)}\" (codepoint {str(codepoint)}) does not exist in the font"
423+
# f"Glyph \"{chr(codepoint)}\" (codepoint {str(codepoint)}) does not exist in the font. Will return `None`"
424424
return None
425425
return Glyph(dict(zip(self.__META_TITLES, self.glyphs[codepoint])), self)
426426

@@ -955,15 +955,15 @@ def __add__(self, bitmap):
955955

956956
return self.__class__.concatall([self, bitmap])
957957

958-
def concat(self, bitmap, direction=1, align=1, offsetlist=None):
958+
def concat(self, bitmap, direction=1, align=1, offset=0):
959959
'''
960960
Concatenate another `Bitmap` objects to the current one.
961961
962962
https://font.tomchen.org/bdfparser_py/bitmap#concat
963963
'''
964964

965965
self.bindata = self.__class__.concatall(
966-
[self, bitmap], direction, align, offsetlist).bindata
966+
[self, bitmap], direction, align, [offset]).bindata
967967
return self
968968

969969
@classmethod
@@ -1050,11 +1050,11 @@ def shadow(self, xoff=1, yoff=-1):
10501050
self.bindata = bitmap_shadow.bindata
10511051
return self
10521052

1053-
def glow(self):
1053+
def glow(self, mode=0):
10541054
'''
10551055
Add glow effect to the shape in the bitmap.
10561056
1057-
The glowing area is one pixel up, right, bottom and left to the original pixels, and will be filled by `'2'`s.
1057+
The glowing area is one pixel up, right, bottom and left to the original pixels (corners will not be filled in default mode 0 but will in mode 1), and will be filled by `'2'`s.
10581058
10591059
https://font.tomchen.org/bdfparser_py/bitmap#glow
10601060
'''
@@ -1072,6 +1072,15 @@ def glow(self):
10721072
b[i_line][i_pixel + 1] = (b[i_line][i_pixel + 1] or 2)
10731073
b[i_line - 1][i_pixel] = (b[i_line - 1][i_pixel] or 2)
10741074
b[i_line + 1][i_pixel] = (b[i_line + 1][i_pixel] or 2)
1075+
if mode == 1:
1076+
b[i_line - 1][i_pixel -
1077+
1] = (b[i_line - 1][i_pixel - 1] or 2)
1078+
b[i_line - 1][i_pixel +
1079+
1] = (b[i_line - 1][i_pixel + 1] or 2)
1080+
b[i_line + 1][i_pixel -
1081+
1] = (b[i_line + 1][i_pixel - 1] or 2)
1082+
b[i_line + 1][i_pixel +
1083+
1] = (b[i_line + 1][i_pixel + 1] or 2)
10751084
self.bindata = [''.join(str(p) for p in l) for l in b]
10761085
return self
10771086

tests/fonts/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,9 @@
88
* U+0001 (1) is before U+0000 (0, originally the first one)
99
* U+06FF (1791, "ۿ", originally the last one in the Arabic range) is before U+06FE (1790, "۾")
1010
* U+7684 (30340, Chinese character "的") is inserted before U+06FF (1791)
11+
12+
**unifont-reduced.bdf** is a reduced version of **unifont-13.0.04.bdf** (GNU Unifont v13.0.04 released on 2020-11-21). **unifont-reduced.bdf** includes 5441 glyphs in total:
13+
14+
* U+0000-U+13FF
15+
* U+3040-U+309F (Hiragana), U+30A0-U+30FF (Katakana)
16+
* The most commonly used Simplified and Traditional Chinese characters which are 的, 一, 是, 不, 了, 人, 我, 在, 有, 他, 这, 为, 之, 大, 来, 以, 个, 中, 上, 们, 到, 说, 国, 和, 地, 也, 子, 时, 道, 出, 而, 要, 于, 就, 下, 得, 可, 你, 年, 生, 自, 会, 那, 后, 能, 对, 着, 事, 其, 里, 所, 去, 行, 过, 家, 十, 用, 发, 天, 如, 然, 作, 方, 成, 者, 多, 日, 都, 三, 小, 军, 二, 无, 同, 么, 经, 法, 当, 起, 与, 好, 看, 学, 进, 种, 将, 还, 分, 此, 心, 前, 面, 又, 定, 见, 只, 主, 没, 公, 从, 這, 爲, 來, 個, 們, 說, 國, 時, 於, 會, 後, 對, 裏, 過, 發, 軍, 無, 麼, 經, 當, 與, 學, 進, 種, 將, 還, 見, 沒, 從

tests/fonts/unifont-13.0.04-for-test.bdf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ STARTFONT 2.1
22
FONT -gnu-Unifont-Medium-R-Normal-Sans-16-160-75-75-c-80-iso10646-1
33
SIZE 16 75 75
44
FONTBOUNDINGBOX 16 16 0 -2
5+
COMMENT "Generated by fontforge, http://fontforge.sourceforge.net"
6+
COMMENT "(C)Copyright"
57
STARTPROPERTIES 24
68
COPYRIGHT "Copyright (C) 1998-2020 Roman Czyborra, Paul Hardy, Qianqian Fang, Andrew Miller, Johnnie Weaver, David Corbett, Rebecca Bettencourt, et al. License: SIL Open Font License version 1.1 and GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html> with the GNU Font Embedding Exception."
79
FONT_VERSION "13.0.04"

0 commit comments

Comments
 (0)