-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Add FontFile.to_imagefont() #9419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fjhenigman
wants to merge
13
commits into
python-pillow:main
Choose a base branch
from
fjhenigman:usepcf
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+70
−12
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a90075a
Add FontFile.to_imagefont().
fjhenigman ea9baaf
Merge branch 'main' into usepcf
radarhere 3e14bea
Use `assert_image_equal_tofile` when similarity is zero
radarhere 0604d6a
Remove unused argument
radarhere 612e3c2
Remove temporary buffer
radarhere 723e764
Improved coverage
radarhere 57be9dc
Merge pull request #1 from radarhere/usepcf
fjhenigman 97673f4
Merge branch 'python-pillow:main' into usepcf
fjhenigman 0ce21f9
Updated documentation
radarhere a18a62c
Merge pull request #2 from radarhere/usepcf
fjhenigman 04470d5
Removed unused argument
radarhere f7ee265
Avoid shadowing built-in
radarhere f7582b8
Updated documentation terms
radarhere File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -18,7 +18,7 @@ | |||||||||||||||||||||||||
| import os | ||||||||||||||||||||||||||
| from typing import BinaryIO | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| from . import Image, _binary | ||||||||||||||||||||||||||
| from . import Image, ImageFont, _binary | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| WIDTH = 800 | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
@@ -110,6 +110,22 @@ def compile(self) -> None: | |||||||||||||||||||||||||
| self.bitmap.paste(im.crop(src), s) | ||||||||||||||||||||||||||
| self.metrics[i] = d, dst, s | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def _encode_metrics(self) -> bytes: | ||||||||||||||||||||||||||
| values: tuple[int, ...] = () | ||||||||||||||||||||||||||
| for i in range(256): | ||||||||||||||||||||||||||
| m = self.metrics[i] | ||||||||||||||||||||||||||
| if m: | ||||||||||||||||||||||||||
| values += m[0] + m[1] + m[2] | ||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||
| values += (0,) * 10 | ||||||||||||||||||||||||||
|
Comment on lines
+118
to
+120
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ... we can avoid repeatedly creating new tuples:
Suggested change
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| metrics = b"" | ||||||||||||||||||||||||||
| for v in values: | ||||||||||||||||||||||||||
| if v < 0: | ||||||||||||||||||||||||||
| v += 65536 | ||||||||||||||||||||||||||
| metrics += _binary.o16be(v) | ||||||||||||||||||||||||||
| return metrics | ||||||||||||||||||||||||||
|
Comment on lines
+122
to
+127
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, mutable
Suggested change
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def save(self, filename: str) -> None: | ||||||||||||||||||||||||||
| """Save font""" | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
@@ -126,9 +142,18 @@ def save(self, filename: str) -> None: | |||||||||||||||||||||||||
| fp.write(b"PILfont\n") | ||||||||||||||||||||||||||
| fp.write(f";;;;;;{self.ysize};\n".encode("ascii")) # HACK!!! | ||||||||||||||||||||||||||
| fp.write(b"DATA\n") | ||||||||||||||||||||||||||
| for id in range(256): | ||||||||||||||||||||||||||
| m = self.metrics[id] | ||||||||||||||||||||||||||
| if not m: | ||||||||||||||||||||||||||
| puti16(fp, (0,) * 10) | ||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||
| puti16(fp, m[0] + m[1] + m[2]) | ||||||||||||||||||||||||||
| fp.write(self._encode_metrics()) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def to_imagefont(self) -> ImageFont.ImageFont: | ||||||||||||||||||||||||||
| """Convert to ImageFont""" | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| self.compile() | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # font data | ||||||||||||||||||||||||||
| if not self.bitmap: | ||||||||||||||||||||||||||
| msg = "No bitmap created" | ||||||||||||||||||||||||||
| raise ValueError(msg) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| imagefont = ImageFont.ImageFont() | ||||||||||||||||||||||||||
| imagefont._load(self.bitmap, self._encode_metrics()) | ||||||||||||||||||||||||||
| return imagefont | ||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we use a list...