Skip to content

Commit

Permalink
fix: trim problem; concat offsetlist param;
Browse files Browse the repository at this point in the history
feat: glow() add mode param
chore: bump version to 2.2.0
  • Loading branch information
tomchen committed Feb 6, 2021
1 parent cb491fa commit 723b46a
Show file tree
Hide file tree
Showing 9 changed files with 125,258 additions and 41 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# BDF Parser
# BDF Parser Python library

[![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)

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/).
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.

**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.

The BDF Parser TypeScript (JavaScript) library has a [**Live Demo & Editor**](https://font.tomchen.org/bdfparser_js/editor) you can try.

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/).

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

```bash
pip install bdfparser
Expand Down Expand Up @@ -62,4 +66,5 @@ You probably understand what I did in these examples. Whether you do or not, go
<img src="https://font.tomchen.org/img/bdfparser_py/plot.png" /><br>
<img src="https://font.tomchen.org/img/bdfparser_py/font_preview_part.png" />
</a>
<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>
</p>
2 changes: 1 addition & 1 deletion src/bdfparser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
https://font.tomchen.org/bdfparser_py/
'''

__version__ = "2.1.0"
__version__ = "2.2.0"

from .bdfparser import *
23 changes: 16 additions & 7 deletions src/bdfparser/bdfparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def __parse_headers(self):
comment = 'comment'
if comment not in self.headers:
self.headers[comment] = []
self.headers[comment].append(value)
self.headers[comment].append(value.strip(' "\'\t\r\n'))
elif key == 'SWIDTH':
nlist = value.split()
self.headers['swx0'] = int(nlist[0])
Expand Down Expand Up @@ -213,7 +213,7 @@ def __parse_props(self):
comment = 'comment'
if comment not in self.props:
self.props[comment] = []
self.props[comment].append(value)
self.props[comment].append(value.strip(' "\'\t\r\n'))
else:
self.props[key.lower()] = value
elif l == 1:
Expand Down Expand Up @@ -420,7 +420,7 @@ def glyphbycp(self, codepoint):
str(codepoint) + ") does not exist in the font. Will return `None`"
)
# Use old style for Python 3.5 support. For 3.6+:
# f"Glyph \"{chr(codepoint)}\" (codepoint {str(codepoint)}) does not exist in the font"
# f"Glyph \"{chr(codepoint)}\" (codepoint {str(codepoint)}) does not exist in the font. Will return `None`"
return None
return Glyph(dict(zip(self.__META_TITLES, self.glyphs[codepoint])), self)

Expand Down Expand Up @@ -955,15 +955,15 @@ def __add__(self, bitmap):

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

def concat(self, bitmap, direction=1, align=1, offsetlist=None):
def concat(self, bitmap, direction=1, align=1, offset=0):
'''
Concatenate another `Bitmap` objects to the current one.
https://font.tomchen.org/bdfparser_py/bitmap#concat
'''

self.bindata = self.__class__.concatall(
[self, bitmap], direction, align, offsetlist).bindata
[self, bitmap], direction, align, [offset]).bindata
return self

@classmethod
Expand Down Expand Up @@ -1050,11 +1050,11 @@ def shadow(self, xoff=1, yoff=-1):
self.bindata = bitmap_shadow.bindata
return self

def glow(self):
def glow(self, mode=0):
'''
Add glow effect to the shape in the bitmap.
The glowing area is one pixel up, right, bottom and left to the original pixels, and will be filled by `'2'`s.
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.
https://font.tomchen.org/bdfparser_py/bitmap#glow
'''
Expand All @@ -1072,6 +1072,15 @@ def glow(self):
b[i_line][i_pixel + 1] = (b[i_line][i_pixel + 1] or 2)
b[i_line - 1][i_pixel] = (b[i_line - 1][i_pixel] or 2)
b[i_line + 1][i_pixel] = (b[i_line + 1][i_pixel] or 2)
if mode == 1:
b[i_line - 1][i_pixel -
1] = (b[i_line - 1][i_pixel - 1] or 2)
b[i_line - 1][i_pixel +
1] = (b[i_line - 1][i_pixel + 1] or 2)
b[i_line + 1][i_pixel -
1] = (b[i_line + 1][i_pixel - 1] or 2)
b[i_line + 1][i_pixel +
1] = (b[i_line + 1][i_pixel + 1] or 2)
self.bindata = [''.join(str(p) for p in l) for l in b]
return self

Expand Down
6 changes: 6 additions & 0 deletions tests/fonts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@
* U+0001 (1) is before U+0000 (0, originally the first one)
* U+06FF (1791, "ۿ", originally the last one in the Arabic range) is before U+06FE (1790, "۾")
* U+7684 (30340, Chinese character "的") is inserted before U+06FF (1791)

**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:

* U+0000-U+13FF
* U+3040-U+309F (Hiragana), U+30A0-U+30FF (Katakana)
* The most commonly used Simplified and Traditional Chinese characters which are 的, 一, 是, 不, 了, 人, 我, 在, 有, 他, 这, 为, 之, 大, 来, 以, 个, 中, 上, 们, 到, 说, 国, 和, 地, 也, 子, 时, 道, 出, 而, 要, 于, 就, 下, 得, 可, 你, 年, 生, 自, 会, 那, 后, 能, 对, 着, 事, 其, 里, 所, 去, 行, 过, 家, 十, 用, 发, 天, 如, 然, 作, 方, 成, 者, 多, 日, 都, 三, 小, 军, 二, 无, 同, 么, 经, 法, 当, 起, 与, 好, 看, 学, 进, 种, 将, 还, 分, 此, 心, 前, 面, 又, 定, 见, 只, 主, 没, 公, 从, 這, 爲, 來, 個, 們, 說, 國, 時, 於, 會, 後, 對, 裏, 過, 發, 軍, 無, 麼, 經, 當, 與, 學, 進, 種, 將, 還, 見, 沒, 從
2 changes: 2 additions & 0 deletions tests/fonts/unifont-13.0.04-for-test.bdf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ STARTFONT 2.1
FONT -gnu-Unifont-Medium-R-Normal-Sans-16-160-75-75-c-80-iso10646-1
SIZE 16 75 75
FONTBOUNDINGBOX 16 16 0 -2
COMMENT "Generated by fontforge, http://fontforge.sourceforge.net"
COMMENT "(C)Copyright"
STARTPROPERTIES 24
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."
FONT_VERSION "13.0.04"
Expand Down
Loading

0 comments on commit 723b46a

Please sign in to comment.