MCPcopy Create free account
hub / github.com/PageBot/PageBot / widthMatch

Method widthMatch

Lib/pagebot/fonttoolbox/objects/font.py:732–776  ·  view source on GitHub ↗

Answers level of matching for the (abbreviated) width name or number with font. Currently there is only no-match (0) and full-match (1). Future implementations may give a float indicator for the level of matching, so the calling function can decide on the level of threshold.

(self, width)

Source from the content-addressed store, hash-verified

730 return 0 # No match
731
732 def widthMatch(self, width):
733 """Answers level of matching for the (abbreviated) width name or number
734 with font. Currently there is only no-match (0) and full-match (1).
735 Future implementations may give a float indicator for the level of
736 matching, so the calling function can decide on the level of threshold.
737
738 >>> from pagebot.fonttoolbox.fontpaths import getTestFontsPath
739 >>> path = getTestFontsPath() + '/google/roboto/Roboto-Black.ttf' # We know this exists in the PageBot repository
740 >>> font = getFont(path)
741 >>> font.info.widthClass
742 5
743 >>> font.widthMatch(0) # Bad match
744 0
745 >>> font.widthMatch(4) # Close match fails
746 0
747 >>> font.widthMatch(5) # Exact match
748 1.0
749 >>> font.widthMatch(6) # Close match fails
750 0
751 >>> font.widthMatch(10) # Bad match
752 0
753 >>> path = getTestFontsPath() + '/google/roboto/Roboto-Bold.ttf' # We know this exists in the PageBot repository
754 >>> font = Font(path)
755 >>> font.info.widthClass
756 5
757 >>> font.widthMatch(5) # Wrong exact match --> 1000 due to wrong font.info.widthClass
758 1.0
759 >>> font.widthMatch('Wide') # No match on "Wide"
760 0
761 >>> #font.widthMatch('Cond') # Exact match on "Cond"
762 1.0
763 """
764 if isinstance(width, (float, int)):
765 # Compare the width as number as max difference to what we already have.
766 w = self.info.widthClass
767 if w <= 100: # Normalize to 1000
768 w *= 100
769 if w in FONT_WIDTH_MATCHES.get(width, []):
770 return 1.0
771 else: # Comparing by string
772 fileName = path2FontName(self.path)
773 for w in FONT_WIDTH_MATCHES.get(width, []):
774 if not isinstance(w, (float, int)) and (w in fileName or w in self.info.styleName):
775 return 1.0
776 return 0
777
778 def isItalic(self):
779 """Answers if this font should be considered to be italic. Currently

Callers 1

matchMethod · 0.95

Calls 2

path2FontNameFunction · 0.90
getMethod · 0.45

Tested by

no test coverage detected