Skip to content

Commit

Permalink
added FontDB
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanielfernandes committed Jan 25, 2023
1 parent 92c2bac commit 40d87b5
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 77 deletions.
27 changes: 4 additions & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
[package]
name = "imagetext_py"
version = "0.1.0"
version = "2.0.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
name = "imagetext_py"
crate-type = ["cdylib"]

[dependencies]
pyo3 = { version = "0.18.0", features = ["extension-module"] }
imagetext = { path = "./imagetext", features = ["emoji", "default-emoji-resolver"]}
imagetext = { path = "./imagetext", features = ["emoji", "default-resolver", "fontdb"]}
image = "0.24.5"
2 changes: 1 addition & 1 deletion imagetext
79 changes: 78 additions & 1 deletion imagetext_py/imagetext_py.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,89 @@ class Font:
Args:
path (str): The path to the font.
fallbacks (list[str], optional): The fallback fonts. Defaults to None.
emoji_options (EmojiOptions, optional): The emoji options. Defaults to None.
emoji_options (EmojiOptions, optional): The emoji options. Defaults to the default emoji options.
Returns:
Font: The font.
"""

def set_emoji_options(self, emoji_options: EmojiOptions) -> None:
"""Set the emoji options of the font.
Args:
emoji_options (EmojiOptions): The emoji options.
"""


class FontDB:
@staticmethod
def LoadFromPath(name: str, path: str) -> None:
"""Load a font from a path. The font will be available by name.
Args:
name (str): The inputted name of the font.
path (str): The path to the font.
"""

@staticmethod
def LoadFromDir(path: str) -> None:
"""Recursively Load all fonts from a directory.
Args:
path (str): The path to the directory.
"""

@staticmethod
def LoadSystemFonts() -> None:
"""Load all system found fonts."""

@staticmethod
def Query(names: str) -> Font:
"""Query a font by names. ex. 'Segoe-UI Segoe-UI-Emoji Segoe-UI-Symbol'
A font with fallbacks and using default emoji options will be returned.
Args:
names (str): The name of the font.
Returns:
Font: The font.
"""

@staticmethod
def QueryWithEmoji(names: str, emoji_options: EmojiOptions) -> Font:
"""Query a font by names. ex. 'Segoe-UI Segoe-UI-Emoji Segoe-UI-Symbol'
A font with fallbacks will be returned.
Args:
names (str): The name of the font.
emoji_options (EmojiOptions): The emoji options.
Returns:
Font: The font.
"""


@staticmethod
def Remove(name: str) -> None:
"""Remove a font from the database.
Args:
name (str): The name of the font.
"""

@staticmethod
def SetDefaultEmojiOptions(emoji_options: EmojiOptions) -> None:
"""Set the default emoji options.
Args:
emoji_options (EmojiOptions): The emoji options.
"""




class EmojiSource(Enum):
Twitter = 0
Apple = 1
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "maturin"
[project]
name = "imagetext_py"
description = "Python bindings for imagetext"
version = "2.0.0"
version = "2.0.1"
authors = [
{ name = "Nathaniel Fernande", email = "[email protected]" },
]
Expand Down
52 changes: 26 additions & 26 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ imagetext makes use of [rusttype](https://github.com/redox-os/rusttype) for font
- Font fallbacks
- Text stroke
- Gradient fills
- Emojis! (almost every platform supported)
- Global Font Database with css-like font querying

## Installation

Expand Down Expand Up @@ -76,34 +78,32 @@ produces this image:
##### took `6ms` to draw this on my machine



## Pillow Usage
## Pillow and FontDB Usage
```python
from PIL import Image
from imagetext_py import *

font = Font("coolvetica.ttf", fallbacks=["emojis.ttf", "japanese.otf"])

black = Paint.Color((0, 0, 0, 255))
rainbow = Paint.Rainbow((0.0,0.0), (256.0,256.0))

# images must be converted to RGBA
im = Image.open("unknown.png").convert("RGBA")

# note: drawing operations are only applied after the context manager exits
with Writer(im) as w:
w.draw_text_wrapped(
text="hello my 😓 n🐢ame i☕s 会のすべ aての構成員 nathan and i drink soup boop coop, the quick brown fox jumps over the lazy dog",
x=256, y=256,
ax=0.5, ay=0.5,
width=512,
size=67,
font=font,
fill=black,
align=TextAlign.Center,
stroke=2.0,
stroke_color=rainbow
)

im.save("test.png")
FontDB.SetDefaultEmojiOptions(EmojiOptions(allow_discord=True))
FontDB.LoadFromDir(".")

font = FontDB.Query("coolvetica japanese")

with Image.new("RGBA", (512, 512), "white") as im:
with Writer(im) as w:
w.draw_text_wrapped(
text="hello from python 😓 lol, <:blobpain:739614945045643447> " \
"ほまみ <:chad:682819256173461522><:bigbrain:744344773229543495> " \
"emojis workin",
x=256, y=256,
ax=0.5, ay=0.5,
width=512,
size=90,
font=font,
fill=Paint.Color((0, 0, 0, 255)),
align=TextAlign.Center,
stroke=2.0,
stroke_color=Paint.Rainbow((0.0,0.0), (256.0,256.0)),
draw_emojis=True
)
im.save("test.png")
```
16 changes: 8 additions & 8 deletions src/drawing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn draw_text(
x,
y,
scale(size),
&font.superfont(),
&font.superfont,
DefaultEmojiResolver,
text,
)
Expand All @@ -50,7 +50,7 @@ pub fn draw_text(
x,
y,
scale(size),
&font.superfont(),
&font.superfont,
text,
)
.map_err(|e| {
Expand Down Expand Up @@ -87,7 +87,7 @@ pub fn draw_text_anchored(
ax,
ay,
scale(size),
&font.superfont(),
&font.superfont,
DefaultEmojiResolver,
text,
)
Expand All @@ -107,7 +107,7 @@ pub fn draw_text_anchored(
ax,
ay,
scale(size),
&font.superfont(),
&font.superfont,
text,
)
.map_err(|e| {
Expand Down Expand Up @@ -148,7 +148,7 @@ pub fn draw_text_multiline(
ay,
width,
scale(size),
&font.superfont(),
&font.superfont,
DefaultEmojiResolver,
&lines,
line_spacing.unwrap_or(1.0),
Expand All @@ -171,7 +171,7 @@ pub fn draw_text_multiline(
ay,
width,
scale(size),
&font.superfont(),
&font.superfont,
&lines,
line_spacing.unwrap_or(1.0),
align.unwrap_or(&TextAlign::Left).to_align(),
Expand Down Expand Up @@ -214,7 +214,7 @@ pub fn draw_text_wrapped(
ay,
width,
scale(size),
&font.superfont(),
&font.superfont,
DefaultEmojiResolver,
text,
line_spacing.unwrap_or(1.0),
Expand All @@ -237,7 +237,7 @@ pub fn draw_text_wrapped(
ay,
width,
scale(size),
&font.superfont(),
&font.superfont,
text,
line_spacing.unwrap_or(1.0),
align.unwrap_or(&TextAlign::Left).to_align(),
Expand Down
Loading

0 comments on commit 40d87b5

Please sign in to comment.