SimPILFont


NameSimPILFont JSON
Version 24.5.17 PyPI version JSON
download
home_pageNone
SummaryA "family size style" request system that simplifies loading remote fonts with PIL
upload_time2024-05-17 16:36:47
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseNone
keywords pil pillow font imagefont
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # SimPILFont

A simple `"family size style"` request system for `PIL.ImageFont.truetype(...)`. 

## Basic Usage
```python3
from PIL import Image, ImageDraw
from simpil import SimPILFont

# instance with zero or more paths to *non-obvious font directories
#  *platform-specific font directories are already known
sf = SimPILFont('./fonts', './some_other/fonts')

# get ImageFont and dimensions of text
text_32   = "Hello World"
djvu_32   = sf("DejaVu Sans 32 bold").font  # 'DejaVu Sans 32 bold'
_,_,w1,h1 = sf.max_bbox(text_32)

# get ImageFont and dimensions of text
text_27   = "Goodbye World"
djvu_27   = sf("27 book").font  # 'DejaVu Sans 27 book'
_,_,w2,h2 = sf.max_bbox(text_27)

img  = Image.new("RGB", (max(w1, w2), h1+h2), color="black")
dctx = ImageDraw.Draw(img)

dctx.text((0, 0) , text_32, font=djvu_32, fill="white")
dctx.text((0, h1), text_27, font=djvu_27, fill="red")

img.show()
del dctx
```

## Font Requests

A font request has the signature `"family size style"` ex: `"Verdana 16 bold italic"`. Requests are explicit. Any part that you do not explicitly change, will not change. Subsequent requests for a family will receive cached data that was memoized when the family was first requested. Font encoding is determined internally. You never need to consider it.

```python3
from simpil import SimPILFont

sf = SimPILFont()

print(sf('Verdana 16 bold'))        # 'Verdana 16 bold'
print(sf('DejaVu Sans'))            # 'DejaVu Sans 16 bold'
print(sf('12'))                     # 'DejaVu Sans 12 bold'
print(sf('condensed bold oblique')) # 'DejaVu Sans 12 condensed bold oblique'
print(sf('Impact regular'))         # 'Impact 12 regular'
```

Font requests can also be formatted as `args`, and the order of your arguments, in either style font request, don't actually matter.

```python3
from simpil import SimPILFont

sf = SimPILFont()

print(sf('Verdana', 'bold', 16))  # 'Verdana 16 bold'
print(sf('bold DejaVu Sans 22'))  # 'DejaVu Sans 22 bold'

# technically, even this would work. it's an unintended byproduct of my parse method and is not a recommended format 
print(sf('condensed DejaVu bold 22 oblique Sans'))  # 'DejaVu Sans 22 condensed bold oblique'
```

Every part of `family.split(' ')` must include one or more capital letters. Every part of `style.split(' ')` must be entirely lowercase. As long as you mind the rules you can make some mistakes.

```python3
from simpil import SimPILFont

sf = SimPILFont()

#printing always returns the font request as the "perfect" request 
print(sf('De Javu Sans 16 extra light')) # 'DejaVu Sans 16 extralight'
print(sf.styles)                         # ('bold', 'bold oblique', 'extralight', 'oblique', 'book', 'condensed bold', 'condensed bold oblique', 'condensed oblique', 'condensed')
```

## Font Data

| property| description                    | default    |
|---------|--------------------------------|------------|
|`.family`| family name                    | "Arial"    |
|`.style` | style name                     | "regular"  |
|`.size`  | font size                      | 12         |
|`.path`  | path to font file              | None       |
|`.font`  | ImageFont.FreeTypeFont instance| None       |
|`.styles`| tuple of supported styles      | None       |

```python3
from simpil import SimPILFont

sf = SimPILFont()

# font request constants
HELVETICA_22 = 'Helvetica 22 regular'

# once you make a font request, the SimPILFont instance retains all of the metadata until you make a new font request
# a font request is the only way to affect these properties
helvetica_22 = sf(HELVETICA_22).font  # ImageFont.FreeTypeFont instance
styles       = sf.styles              # ('regular', 'bold', 'italic', etc...)
path         = sf.path                # "path/to/regular/helvetica.ttf"
family       = sf.family              # "Helvetica"
style        = sf.style               # "regular"
size         = sf.size                # 22
```

You can call the inline `.export()` method to save a json file, containing all compatible fonts, categorized by their encoding.

```python3
from simpil import SimPILFont

#export returns the SimPILFont instance
sf = SimPILFont().export()    # saved to app_directory/fonts.json
```

## BBox Variations
```python3
from simpil import SimPILFont

# you can use this shortcut if you want to instance SimPILFont and request a "start-up" font
# this makes more sense if you only intend to use one font - like this example
sf  = SimPILFont()("Verdana 20 regular")

text = "Hello World"

# proxy for ttf.getbbox(text)
x1, y1, w1, h1 = sf.bbox(text)

# the smallest possible bbox
x2, y2, w2, h2 = sf.min_bbox(text)

# (right/bottom) margins mirror (left/top) margins, respectively
x3, y3, w3, h3 = sf.max_bbox(text)
```


            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "SimPILFont",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "OneMadGypsy <onemadgypsy@gmail.com>",
    "keywords": "PIL, Pillow, font, ImageFont",
    "author": null,
    "author_email": "OneMadGypsy <onemadgypsy@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/5e/f6/fa33d70e21eaddd0024cb2f866873be3ca0c56838829cbc5e8c75bc3e75d/simpilfont-24.5.17.tar.gz",
    "platform": null,
    "description": "# SimPILFont\r\n\r\nA simple `\"family size style\"` request system for `PIL.ImageFont.truetype(...)`. \r\n\r\n## Basic Usage\r\n```python3\r\nfrom PIL import Image, ImageDraw\r\nfrom simpil import SimPILFont\r\n\r\n# instance with zero or more paths to *non-obvious font directories\r\n#  *platform-specific font directories are already known\r\nsf = SimPILFont('./fonts', './some_other/fonts')\r\n\r\n# get ImageFont and dimensions of text\r\ntext_32   = \"Hello World\"\r\ndjvu_32   = sf(\"DejaVu Sans 32 bold\").font  # 'DejaVu Sans 32 bold'\r\n_,_,w1,h1 = sf.max_bbox(text_32)\r\n\r\n# get ImageFont and dimensions of text\r\ntext_27   = \"Goodbye World\"\r\ndjvu_27   = sf(\"27 book\").font  # 'DejaVu Sans 27 book'\r\n_,_,w2,h2 = sf.max_bbox(text_27)\r\n\r\nimg  = Image.new(\"RGB\", (max(w1, w2), h1+h2), color=\"black\")\r\ndctx = ImageDraw.Draw(img)\r\n\r\ndctx.text((0, 0) , text_32, font=djvu_32, fill=\"white\")\r\ndctx.text((0, h1), text_27, font=djvu_27, fill=\"red\")\r\n\r\nimg.show()\r\ndel dctx\r\n```\r\n\r\n## Font Requests\r\n\r\nA font request has the signature `\"family size style\"` ex: `\"Verdana 16 bold italic\"`. Requests are explicit. Any part that you do not explicitly change, will not change. Subsequent requests for a family will receive cached data that was memoized when the family was first requested. Font encoding is determined internally. You never need to consider it.\r\n\r\n```python3\r\nfrom simpil import SimPILFont\r\n\r\nsf = SimPILFont()\r\n\r\nprint(sf('Verdana 16 bold'))        # 'Verdana 16 bold'\r\nprint(sf('DejaVu Sans'))            # 'DejaVu Sans 16 bold'\r\nprint(sf('12'))                     # 'DejaVu Sans 12 bold'\r\nprint(sf('condensed bold oblique')) # 'DejaVu Sans 12 condensed bold oblique'\r\nprint(sf('Impact regular'))         # 'Impact 12 regular'\r\n```\r\n\r\nFont requests can also be formatted as `args`, and the order of your arguments, in either style font request, don't actually matter.\r\n\r\n```python3\r\nfrom simpil import SimPILFont\r\n\r\nsf = SimPILFont()\r\n\r\nprint(sf('Verdana', 'bold', 16))  # 'Verdana 16 bold'\r\nprint(sf('bold DejaVu Sans 22'))  # 'DejaVu Sans 22 bold'\r\n\r\n# technically, even this would work. it's an unintended byproduct of my parse method and is not a recommended format \r\nprint(sf('condensed DejaVu bold 22 oblique Sans'))  # 'DejaVu Sans 22 condensed bold oblique'\r\n```\r\n\r\nEvery part of `family.split(' ')` must include one or more capital letters. Every part of `style.split(' ')` must be entirely lowercase. As long as you mind the rules you can make some mistakes.\r\n\r\n```python3\r\nfrom simpil import SimPILFont\r\n\r\nsf = SimPILFont()\r\n\r\n#printing always returns the font request as the \"perfect\" request \r\nprint(sf('De Javu Sans 16 extra light')) # 'DejaVu Sans 16 extralight'\r\nprint(sf.styles)                         # ('bold', 'bold oblique', 'extralight', 'oblique', 'book', 'condensed bold', 'condensed bold oblique', 'condensed oblique', 'condensed')\r\n```\r\n\r\n## Font Data\r\n\r\n| property| description                    | default    |\r\n|---------|--------------------------------|------------|\r\n|`.family`| family name                    | \"Arial\"    |\r\n|`.style` | style name                     | \"regular\"  |\r\n|`.size`  | font size                      | 12         |\r\n|`.path`  | path to font file              | None       |\r\n|`.font`  | ImageFont.FreeTypeFont instance| None       |\r\n|`.styles`| tuple of supported styles      | None       |\r\n\r\n```python3\r\nfrom simpil import SimPILFont\r\n\r\nsf = SimPILFont()\r\n\r\n# font request constants\r\nHELVETICA_22 = 'Helvetica 22 regular'\r\n\r\n# once you make a font request, the SimPILFont instance retains all of the metadata until you make a new font request\r\n# a font request is the only way to affect these properties\r\nhelvetica_22 = sf(HELVETICA_22).font  # ImageFont.FreeTypeFont instance\r\nstyles       = sf.styles              # ('regular', 'bold', 'italic', etc...)\r\npath         = sf.path                # \"path/to/regular/helvetica.ttf\"\r\nfamily       = sf.family              # \"Helvetica\"\r\nstyle        = sf.style               # \"regular\"\r\nsize         = sf.size                # 22\r\n```\r\n\r\nYou can call the inline `.export()` method to save a json file, containing all compatible fonts, categorized by their encoding.\r\n\r\n```python3\r\nfrom simpil import SimPILFont\r\n\r\n#export returns the SimPILFont instance\r\nsf = SimPILFont().export()    # saved to app_directory/fonts.json\r\n```\r\n\r\n## BBox Variations\r\n```python3\r\nfrom simpil import SimPILFont\r\n\r\n# you can use this shortcut if you want to instance SimPILFont and request a \"start-up\" font\r\n# this makes more sense if you only intend to use one font - like this example\r\nsf  = SimPILFont()(\"Verdana 20 regular\")\r\n\r\ntext = \"Hello World\"\r\n\r\n# proxy for ttf.getbbox(text)\r\nx1, y1, w1, h1 = sf.bbox(text)\r\n\r\n# the smallest possible bbox\r\nx2, y2, w2, h2 = sf.min_bbox(text)\r\n\r\n# (right/bottom) margins mirror (left/top) margins, respectively\r\nx3, y3, w3, h3 = sf.max_bbox(text)\r\n```\r\n\r\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A \"family size style\" request system that simplifies loading remote fonts with PIL",
    "version": "24.5.17",
    "project_urls": {
        "Homepage": "https://github.com/OneMadGypsy/SimPIL-Font/",
        "Issues": "https://github.com/OneMadGypsy/SimPIL-Font/issues"
    },
    "split_keywords": [
        "pil",
        " pillow",
        " font",
        " imagefont"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2d3546efcc61c93ce1e2b860b64bbdd0ddef9ae3028e81f2fbf2471d48f66417",
                "md5": "58b6ca1d2f27bf83430b2db44e1e3ac9",
                "sha256": "9eeba920c5243a1f11273b42141f07a47ec3ee62eb24f3b669f6db26cf74ea30"
            },
            "downloads": -1,
            "filename": "SimPILFont-24.5.17-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "58b6ca1d2f27bf83430b2db44e1e3ac9",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 6239,
            "upload_time": "2024-05-17T16:36:45",
            "upload_time_iso_8601": "2024-05-17T16:36:45.099236Z",
            "url": "https://files.pythonhosted.org/packages/2d/35/46efcc61c93ce1e2b860b64bbdd0ddef9ae3028e81f2fbf2471d48f66417/SimPILFont-24.5.17-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5ef6fa33d70e21eaddd0024cb2f866873be3ca0c56838829cbc5e8c75bc3e75d",
                "md5": "e13f226c314d81dba9df33ded43aaec2",
                "sha256": "446a5422f2a09bcbfc6b0ad923f04b18eaff34fbfe4bd53f29e7f4d60261ee6d"
            },
            "downloads": -1,
            "filename": "simpilfont-24.5.17.tar.gz",
            "has_sig": false,
            "md5_digest": "e13f226c314d81dba9df33ded43aaec2",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 6221,
            "upload_time": "2024-05-17T16:36:47",
            "upload_time_iso_8601": "2024-05-17T16:36:47.468214Z",
            "url": "https://files.pythonhosted.org/packages/5e/f6/fa33d70e21eaddd0024cb2f866873be3ca0c56838829cbc5e8c75bc3e75d/simpilfont-24.5.17.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-17 16:36:47",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "OneMadGypsy",
    "github_project": "SimPIL-Font",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "simpilfont"
}
        
Elapsed time: 0.24726s