-
-
Notifications
You must be signed in to change notification settings - Fork 785
[widget audit] toga.Font #1903
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
Merged
Merged
[widget audit] toga.Font #1903
Changes from 71 commits
Commits
Show all changes
76 commits
Select commit
Hold shift + click to select a range
adaf069
Add a ctypes wrapper for fontconfig.
freakboy3742 cd0fcb2
Add Changenote.
freakboy3742 f706ade
Improved error reporting for unknown fonts.
freakboy3742 d2afb95
Fonts core API to 100%. Also fixes some stray canvas tests dependent …
freakboy3742 2732de1
Add an extra test for registering font variants.
freakboy3742 dca074d
Removed measure as a public API on font.
freakboy3742 11777d3
Removed a now-redundant test.
freakboy3742 1395706
Add fonts to testbed
studioph da65018
Quote font family when creating CSS stylesheet
studioph bc2823d
Use dict to convert Pango enums to Toga
studioph c4778d7
Add widget_probe fixture to testbed conftest
studioph 0d31ea0
Add font tests to testbed
studioph d20ea5b
Move font measure to canvas
studioph 32b18e9
Add changenote
studioph 2e27817
Fix app path in tests
studioph 8451948
Use app fixture
studioph 6f3325f
Merge branch 'main' into fontconfig
freakboy3742 0ae230e
Merge branch 'main' into fontconfig
freakboy3742 3cd10a8
Corrected a spelling error.
freakboy3742 2822635
Merge branch 'fontconfig' into gtk-font-tests
freakboy3742 67e17c1
Remove redundant platform font test.
freakboy3742 30e638b
Minor cleanup of GTK probe implementation.
freakboy3742 bf78924
Modify the not-a-font file to make the purpose explicit.
freakboy3742 d1ea0e7
Minor code format cleanup.
freakboy3742 93a5874
Small tweaks to tests.
freakboy3742 707a0b8
Tweaked the GTK font tests.
freakboy3742 1405606
Merge branch 'main' into gtk-font-tests
freakboy3742 c8134fc
Ensure webkit2 libraries are available for testing.
freakboy3742 fc9ada3
Update Winforms to get 100% font test coverage.
freakboy3742 3a814ad
Update docs to reflect support status and Winforms limitations.
freakboy3742 6572d97
Clarify comment around coverage case.
freakboy3742 9d644e4
Clarify description of default font size.
freakboy3742 ffc3424
Restructured code to avoid false KeyError hits.
freakboy3742 16c4930
Clarified comment about FontConfig singleton.
freakboy3742 510abc9
Check for additional failure modes when loading custom fonts.
freakboy3742 a58e376
Rework font tests to test all variants.
freakboy3742 a267763
Restructure winforms font loading to isolate keyerror failures.
freakboy3742 5fa3011
Moved all Winforms font handling into the font class to allow for cus…
freakboy3742 6fa4f6f
Made the winforms font test more complete.
freakboy3742 58b9039
Interpret OBLIQUE as ITALIC.
freakboy3742 a27be03
Correct typo.
freakboy3742 bc832e9
Merge branch 'main' into gtk-font-tests
freakboy3742 c4d8a57
Refactor app path into interface layer.
freakboy3742 95d6b3c
Merge branch 'main' into gtk-font-tests
freakboy3742 e53c7ea
Corrections to Winforms and testbed tests.
freakboy3742 34cc18d
Try a different approach to avoid path discrepancy issues.
freakboy3742 c6b63c1
Merge remote-tracking branch 'upstream/main' into gtk-font-tests
freakboy3742 f863568
Add 100% font coverage for Cocoa.
freakboy3742 0d32913
Restructure font probe into a standalone mixin.
freakboy3742 d246db4
100% coverage for iOS fonts.
freakboy3742 c5836d2
Android fonts working for all but custom fonts.
freakboy3742 dca6219
Correct GTK test for custom font.
freakboy3742 dbafa75
Add safety rail when using Dummy backend.
freakboy3742 424014d
Small tweak to box child creation to ensure custom fonts work on GTK.
freakboy3742 c58d20e
Merge branch 'main' into gtk-font-tests
freakboy3742 6281eb0
Merge branch 'main' into gtk-font-tests
freakboy3742 c67cb76
Merge branch 'main' into gtk-font-tests
freakboy3742 69fab4f
Merge branch 'main' into gtk-font-tests
freakboy3742 7e2c327
Merge branch 'main' into gtk-font-tests
freakboy3742 da83ddf
Merge branch 'main' into gtk-font-tests
freakboy3742 131503d
Correct spelling errors.
freakboy3742 0c07e4c
Merge branch 'main' into gtk-font-tests
freakboy3742 a978bc7
Merge branch 'main' into gtk-font-tests
freakboy3742 3531f9f
Bump PyGObject version to incorporate bugfix for PyGObject bug 119.
freakboy3742 d22539a
Clarify that Android Selection doesn't support font, color or alignment
mhsmith 5821c65
Documentation cleanups
mhsmith 86015f4
Complete transition to keyword-only arguments
mhsmith 86bb969
Fix Android Table font tests, and add expected font sizes to probes
mhsmith 026f957
Add an example of adding a label with a font after initial construction.
freakboy3742 3862545
100% coverage for Android fonts
mhsmith 4b9c84a
Documentation fixes
mhsmith a0933e9
Revert the child creation order.
freakboy3742 e69d97e
Clarify an android requirements as a test requirement.
freakboy3742 dce0223
Reorganize docs for Font.
freakboy3742 7036ec4
Correct test case for child creation order.
freakboy3742 33931fa
Complete reversion of child creation order
mhsmith 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| from concurrent.futures import ThreadPoolExecutor | ||
|
|
||
| from fontTools.ttLib import TTFont | ||
| from java import jint | ||
| from java.lang import Integer, Long | ||
|
|
||
| from android.graphics import Typeface | ||
| from android.graphics.fonts import FontFamily | ||
| from android.util import TypedValue | ||
| from toga.fonts import ( | ||
| BOLD, | ||
| ITALIC, | ||
| MESSAGE, | ||
| NORMAL, | ||
| OBLIQUE, | ||
| SMALL_CAPS, | ||
| SYSTEM, | ||
| SYSTEM_DEFAULT_FONT_SIZE, | ||
| ) | ||
|
|
||
| SYSTEM_FONTS = {} | ||
| nativeGetFamily = new_FontFamily = None | ||
|
|
||
|
|
||
| def load_fontmap(): | ||
| field = Typeface.getClass().getDeclaredField("sSystemFontMap") | ||
| field.setAccessible(True) | ||
| fontmap = field.get(None) | ||
|
|
||
| for name in fontmap.keySet().toArray(): | ||
| typeface = fontmap.get(name) | ||
| SYSTEM_FONTS[typeface] = name | ||
| for native_style in [ | ||
| Typeface.BOLD, | ||
| Typeface.ITALIC, | ||
| Typeface.BOLD | Typeface.ITALIC, | ||
| ]: | ||
| SYSTEM_FONTS[Typeface.create(typeface, native_style)] = name | ||
|
|
||
|
|
||
| def reflect_font_methods(): | ||
| global nativeGetFamily, new_FontFamily | ||
|
|
||
| # Bypass non-SDK interface restrictions by looking them up on a background thread | ||
| # with no Java stack frames (https://stackoverflow.com/a/61600526). | ||
|
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. Based on the StackOverflow discussion, I'm utterly petrified about what will happen to this as Android releases new versions... but until then, it's a nice hack. (well, it's an utterly ugly hack, but it's a nice utterly ugly hack :-) ) |
||
| with ThreadPoolExecutor() as executor: | ||
| nativeGetFamily = executor.submit( | ||
| Typeface.getClass().getDeclaredMethod, | ||
| "nativeGetFamily", | ||
| Long.TYPE, | ||
| Integer.TYPE, | ||
| ).result() | ||
| nativeGetFamily.setAccessible(True) | ||
|
|
||
| new_FontFamily = executor.submit( | ||
| FontFamily.getClass().getConstructor, Long.TYPE | ||
| ).result() | ||
|
|
||
|
|
||
| class FontMixin: | ||
| supports_custom_fonts = True | ||
|
|
||
| def assert_font_options(self, weight=NORMAL, style=NORMAL, variant=NORMAL): | ||
| assert (BOLD if self.typeface.isBold() else NORMAL) == weight | ||
|
|
||
| if style == OBLIQUE: | ||
| print("Interpreting OBLIQUE font as ITALIC") | ||
| assert self.typeface.isItalic() | ||
| else: | ||
| assert (ITALIC if self.typeface.isItalic() else NORMAL) == style | ||
|
|
||
| if variant == SMALL_CAPS: | ||
| print("Ignoring SMALL CAPS font test") | ||
| else: | ||
| assert NORMAL == variant | ||
|
|
||
| def assert_font_size(self, expected): | ||
| if expected == SYSTEM_DEFAULT_FONT_SIZE: | ||
| expected = self.default_font_size | ||
| assert round(self.text_size) == round( | ||
| TypedValue.applyDimension( | ||
| TypedValue.COMPLEX_UNIT_SP, | ||
| expected, | ||
| self.native.getResources().getDisplayMetrics(), | ||
| ) | ||
| ) | ||
|
|
||
| def assert_font_family(self, expected): | ||
| if not SYSTEM_FONTS: | ||
| load_fontmap() | ||
|
|
||
| if actual := SYSTEM_FONTS.get(self.typeface): | ||
| assert actual == { | ||
| SYSTEM: self.default_font_family, | ||
| MESSAGE: "sans-serif", | ||
| }.get(expected, expected) | ||
| else: | ||
| if not nativeGetFamily: | ||
| reflect_font_methods() | ||
| family_ptr = nativeGetFamily.invoke( | ||
| None, self.typeface.native_instance, jint(0) | ||
| ) | ||
| family = new_FontFamily.newInstance(family_ptr) | ||
| assert family.getSize() == 1 | ||
|
|
||
| font = TTFont(family.getFont(0).getFile().getPath()) | ||
| assert font["name"].getDebugName(1) == expected | ||
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
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 |
|---|---|---|
| @@ -1,13 +1,5 @@ | ||
| from toga.fonts import SYSTEM | ||
|
|
||
| from .textinput import TextInputProbe | ||
|
|
||
|
|
||
| class PasswordInputProbe(TextInputProbe): | ||
| # In password mode, the EditText defaults to monospace. | ||
| def assert_font_family(self, expected): | ||
| actual = self.font.family | ||
| if expected == SYSTEM: | ||
| assert actual == "monospace" | ||
| else: | ||
| assert actual == expected | ||
| default_font_family = "monospace" |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
This had no effect because the gravity we actually need to change is that of the TextView within the
R__layout.simple_spinner_item. This can be fixed later, along with color and font (#1758).