Movatterモバイル変換


[0]ホーム

URL:


ContentsMenuExpandLight modeDark modeAuto light/dark, in light modeAuto light/dark, in dark modeSkip to content
Pillow (PIL Fork) 12.0.0 documentation
Light LogoDark Logo
Pillow (PIL Fork) 12.0.0 documentation
Back to top

Source code for PIL.BdfFontFile

## The Python Imaging Library# $Id$## bitmap distribution font (bdf) file parser## history:# 1996-05-16 fl   created (as bdf2pil)# 1997-08-25 fl   converted to FontFile driver# 2001-05-25 fl   removed bogus __init__ call# 2002-11-20 fl   robustification (from Kevin Cazabon, Dmitry Vasiliev)# 2003-04-22 fl   more robustification (from Graham Dumpleton)## Copyright (c) 1997-2003 by Secret Labs AB.# Copyright (c) 1997-2003 by Fredrik Lundh.## See the README file for information on usage and redistribution.#"""Parse X Bitmap Distribution Format (BDF)"""from__future__importannotationsfromtypingimportBinaryIOfrom.importFontFile,Image
[docs]defbdf_char(f:BinaryIO,)->(tuple[str,int,tuple[tuple[int,int],tuple[int,int,int,int],tuple[int,int,int,int]],Image.Image,]|None):# skip to STARTCHARwhileTrue:s=f.readline()ifnots:returnNoneifs.startswith(b"STARTCHAR"):breakid=s[9:].strip().decode("ascii")# load symbol propertiesprops={}whileTrue:s=f.readline()ifnotsors.startswith(b"BITMAP"):breaki=s.find(b" ")props[s[:i].decode("ascii")]=s[i+1:-1].decode("ascii")# load bitmapbitmap=bytearray()whileTrue:s=f.readline()ifnotsors.startswith(b"ENDCHAR"):breakbitmap+=s[:-1]# The word BBX# followed by the width in x (BBw), height in y (BBh),# and x and y displacement (BBxoff0, BByoff0)# of the lower left corner from the origin of the character.width,height,x_disp,y_disp=(int(p)forpinprops["BBX"].split())# The word DWIDTH# followed by the width in x and y of the character in device pixels.dwx,dwy=(int(p)forpinprops["DWIDTH"].split())bbox=((dwx,dwy),(x_disp,-y_disp-height,width+x_disp,-y_disp),(0,0,width,height),)try:im=Image.frombytes("1",(width,height),bitmap,"hex","1")exceptValueError:# deal with zero-width charactersim=Image.new("1",(width,height))returnid,int(props["ENCODING"]),bbox,im
[docs]classBdfFontFile(FontFile.FontFile):"""Font file plugin for the X11 BDF format."""def__init__(self,fp:BinaryIO)->None:super().__init__()s=fp.readline()ifnots.startswith(b"STARTFONT 2.1"):msg="not a valid BDF file"raiseSyntaxError(msg)props={}comments=[]whileTrue:s=fp.readline()ifnotsors.startswith(b"ENDPROPERTIES"):breaki=s.find(b" ")props[s[:i].decode("ascii")]=s[i+1:-1].decode("ascii")ifs[:i]in[b"COMMENT",b"COPYRIGHT"]:ifs.find(b"LogicalFontDescription")<0:comments.append(s[i+1:-1].decode("ascii"))whileTrue:c=bdf_char(fp)ifnotc:breakid,ch,(xy,dst,src),im=cif0<=ch<len(self.glyph):self.glyph[ch]=xy,dst,src,im
Copyright © 1995-2011 Fredrik Lundh and contributors, 2010 Jeffrey A. Clark and contributors.
Made withSphinx and@pradyunsg'sFuro

[8]
ページ先頭

©2009-2025 Movatter.jp