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

## The Python Imaging Library# $Id$## base class for raster font file parsers## history:# 1997-06-05 fl   created# 1997-08-19 fl   restrict image width## Copyright (c) 1997-1998 by Secret Labs AB# Copyright (c) 1997-1998 by Fredrik Lundh## See the README file for information on usage and redistribution.#from__future__importannotationsimportosfromtypingimportBinaryIOfrom.importImage,_binaryWIDTH=800
[docs]defputi16(fp:BinaryIO,values:tuple[int,int,int,int,int,int,int,int,int,int])->None:"""Write network order (big-endian) 16-bit sequence"""forvinvalues:ifv<0:v+=65536fp.write(_binary.o16be(v))
[docs]classFontFile:"""Base class for raster font file handlers."""bitmap:Image.Image|None=Nonedef__init__(self)->None:self.info:dict[bytes,bytes|int]={}self.glyph:list[tuple[tuple[int,int],tuple[int,int,int,int],tuple[int,int,int,int],Image.Image,]|None]=[None]*256def__getitem__(self,ix:int)->(tuple[tuple[int,int],tuple[int,int,int,int],tuple[int,int,int,int],Image.Image,]|None):returnself.glyph[ix]
[docs]defcompile(self)->None:"""Create metrics and bitmap"""ifself.bitmap:return# create bitmap large enough to hold all datah=w=maxwidth=0lines=1forglyphinself.glyph:ifglyph:d,dst,src,im=glyphh=max(h,src[3]-src[1])w=w+(src[2]-src[0])ifw>WIDTH:lines+=1w=src[2]-src[0]maxwidth=max(maxwidth,w)xsize=maxwidthysize=lines*hifxsize==0andysize==0:returnself.ysize=h# paste glyphs into bitmapself.bitmap=Image.new("1",(xsize,ysize))self.metrics:list[tuple[tuple[int,int],tuple[int,int,int,int],tuple[int,int,int,int]]|None]=[None]*256x=y=0foriinrange(256):glyph=self[i]ifglyph:d,dst,src,im=glyphxx=src[2]-src[0]x0,y0=x,yx=x+xxifx>WIDTH:x,y=0,y+hx0,y0=x,yx=xxs=src[0]+x0,src[1]+y0,src[2]+x0,src[3]+y0self.bitmap.paste(im.crop(src),s)self.metrics[i]=d,dst,s
[docs]defsave(self,filename:str)->None:"""Save font"""self.compile()# font dataifnotself.bitmap:msg="No bitmap created"raiseValueError(msg)self.bitmap.save(os.path.splitext(filename)[0]+".pbm","PNG")# font metricswithopen(os.path.splitext(filename)[0]+".pil","wb")asfp:fp.write(b"PILfont\n")fp.write(f";;;;;;{self.ysize};\n".encode("ascii"))# HACK!!!fp.write(b"DATA\n")foridinrange(256):m=self.metrics[id]ifnotm:puti16(fp,(0,)*10)else:puti16(fp,m[0]+m[1]+m[2])
Copyright © 1995-2011 Fredrik Lundh and contributors, 2010 Jeffrey A. Clark and contributors.
Made withSphinx and@pradyunsg'sFuro

[8]
ページ先頭

©2009-2025 Movatter.jp