diff --git a/lib/matplotlib/ft2font.pyi b/lib/matplotlib/ft2font.pyi index fb74fd676f5b..6a0716e993a5 100644 --- a/lib/matplotlib/ft2font.pyi +++ b/lib/matplotlib/ft2font.pyi @@ -1,4 +1,4 @@ -from typing import BinaryIO, Literal +from typing import BinaryIO, Literal, TypedDict, overload import numpy as np from numpy.typing import NDArray @@ -42,6 +42,122 @@ SCALABLE: int SFNT: int VERTICAL: int +class _SfntHeadDict(TypedDict): + version: tuple[int, int] + fontRevision: tuple[int, int] + checkSumAdjustment: int + magicNumber: int + flags: int + unitsPerEm: int + created: tuple[int, int] + modified: tuple[int, int] + xMin: int + yMin: int + xMax: int + yMax: int + macStyle: int + lowestRecPPEM: int + fontDirectionHint: int + indexToLocFormat: int + glyphDataFormat: int + +class _SfntMaxpDict(TypedDict): + version: tuple[int, int] + numGlyphs: int + maxPoints: int + maxContours: int + maxComponentPoints: int + maxComponentContours: int + maxZones: int + maxTwilightPoints: int + maxStorage: int + maxFunctionDefs: int + maxInstructionDefs: int + maxStackElements: int + maxSizeOfInstructions: int + maxComponentElements: int + maxComponentDepth: int + +class _SfntOs2Dict(TypedDict): + version: int + xAvgCharWidth: int + usWeightClass: int + usWidthClass: int + fsType: int + ySubscriptXSize: int + ySubscriptYSize: int + ySubscriptXOffset: int + ySubscriptYOffset: int + ySuperscriptXSize: int + ySuperscriptYSize: int + ySuperscriptXOffset: int + ySuperscriptYOffset: int + yStrikeoutSize: int + yStrikeoutPosition: int + sFamilyClass: int + panose: bytes + ulCharRange: tuple[int, int, int, int] + achVendID: bytes + fsSelection: int + fsFirstCharIndex: int + fsLastCharIndex: int + +class _SfntHheaDict(TypedDict): + version: tuple[int, int] + ascent: int + descent: int + lineGap: int + advanceWidthMax: int + minLeftBearing: int + minRightBearing: int + xMaxExtent: int + caretSlopeRise: int + caretSlopeRun: int + caretOffset: int + metricDataFormat: int + numOfLongHorMetrics: int + +class _SfntVheaDict(TypedDict): + version: tuple[int, int] + vertTypoAscender: int + vertTypoDescender: int + vertTypoLineGap: int + advanceHeightMax: int + minTopSideBearing: int + minBottomSizeBearing: int + yMaxExtent: int + caretSlopeRise: int + caretSlopeRun: int + caretOffset: int + metricDataFormat: int + numOfLongVerMetrics: int + +class _SfntPostDict(TypedDict): + format: tuple[int, int] + italicAngle: tuple[int, int] + underlinePosition: int + underlineThickness: int + isFixedPitch: int + minMemType42: int + maxMemType42: int + minMemType1: int + maxMemType1: int + +class _SfntPcltDict(TypedDict): + version: tuple[int, int] + fontNumber: int + pitch: int + xHeight: int + style: int + typeFamily: int + capHeight: int + symbolSet: int + typeFace: bytes + characterComplement: bytes + strokeWeight: int + widthType: int + serifStyle: int + class FT2Font: ascender: int bbox: tuple[int, int, int, int] @@ -92,9 +208,20 @@ class FT2Font: self, ) -> tuple[str, str, str, str, str, int, int, int, int]: ... def get_sfnt(self) -> dict[tuple[int, int, int, int], bytes]: ... - def get_sfnt_table( - self, name: Literal["head", "maxp", "OS/2", "hhea", "vhea", "post", "pclt"] - ) -> dict[str, tuple[int, int, int, int] | tuple[int, int] | int | bytes]: ... + @overload + def get_sfnt_table(self, name: Literal["head"]) -> _SfntHeadDict | None: ... + @overload + def get_sfnt_table(self, name: Literal["maxp"]) -> _SfntMaxpDict | None: ... + @overload + def get_sfnt_table(self, name: Literal["OS/2"]) -> _SfntOs2Dict | None: ... + @overload + def get_sfnt_table(self, name: Literal["hhea"]) -> _SfntHheaDict | None: ... + @overload + def get_sfnt_table(self, name: Literal["vhea"]) -> _SfntVheaDict | None: ... + @overload + def get_sfnt_table(self, name: Literal["post"]) -> _SfntPostDict | None: ... + @overload + def get_sfnt_table(self, name: Literal["pclt"]) -> _SfntPcltDict | None: ... def get_width_height(self) -> tuple[int, int]: ... def get_xys(self, antialiased: bool = ...) -> NDArray[np.float64]: ... def load_char(self, charcode: int, flags: int = ...) -> Glyph: ... @@ -122,4 +249,5 @@ class Glyph: vertBearingY: int vertAdvance: int + @property def bbox(self) -> tuple[int, int, int, int]: ... diff --git a/lib/matplotlib/tests/test_mathtext.py b/lib/matplotlib/tests/test_mathtext.py index 5a9e8a8b9264..7ebc76dabc1d 100644 --- a/lib/matplotlib/tests/test_mathtext.py +++ b/lib/matplotlib/tests/test_mathtext.py @@ -281,6 +281,7 @@ def test_fontinfo(): fontpath = mpl.font_manager.findfont("DejaVu Sans") font = mpl.ft2font.FT2Font(fontpath) table = font.get_sfnt_table("head") + assert table is not None assert table['version'] == (1, 0) diff --git a/src/ft2font_wrapper.cpp b/src/ft2font_wrapper.cpp index 872f9c0a6023..7888a9c212a5 100644 --- a/src/ft2font_wrapper.cpp +++ b/src/ft2font_wrapper.cpp @@ -1118,44 +1118,23 @@ static PyObject *PyFT2Font_get_sfnt_table(PyFT2Font *self, PyObject *args) "s:(l,l), s:(l,l), s:h, s:h, s:h, s:h, s:H, s:H, s:h, s:h, s:h}"; TT_Header *t = (TT_Header *)table; return Py_BuildValue(head_dict, - "version", - FIXED_MAJOR(t->Table_Version), - FIXED_MINOR(t->Table_Version), - "fontRevision", - FIXED_MAJOR(t->Font_Revision), - FIXED_MINOR(t->Font_Revision), - "checkSumAdjustment", - t->CheckSum_Adjust, - "magicNumber", - t->Magic_Number, - "flags", - t->Flags, - "unitsPerEm", - t->Units_Per_EM, - "created", - t->Created[0], - t->Created[1], - "modified", - t->Modified[0], - t->Modified[1], - "xMin", - t->xMin, - "yMin", - t->yMin, - "xMax", - t->xMax, - "yMax", - t->yMax, - "macStyle", - t->Mac_Style, - "lowestRecPPEM", - t->Lowest_Rec_PPEM, - "fontDirectionHint", - t->Font_Direction, - "indexToLocFormat", - t->Index_To_Loc_Format, - "glyphDataFormat", - t->Glyph_Data_Format); + "version", FIXED_MAJOR(t->Table_Version), FIXED_MINOR(t->Table_Version), + "fontRevision", FIXED_MAJOR(t->Font_Revision), FIXED_MINOR(t->Font_Revision), + "checkSumAdjustment", t->CheckSum_Adjust, + "magicNumber", t->Magic_Number, + "flags", t->Flags, + "unitsPerEm", t->Units_Per_EM, + "created", t->Created[0], t->Created[1], + "modified", t->Modified[0], t->Modified[1], + "xMin", t->xMin, + "yMin", t->yMin, + "xMax", t->xMax, + "yMax", t->yMax, + "macStyle", t->Mac_Style, + "lowestRecPPEM", t->Lowest_Rec_PPEM, + "fontDirectionHint", t->Font_Direction, + "indexToLocFormat", t->Index_To_Loc_Format, + "glyphDataFormat", t->Glyph_Data_Format); } case 1: { char maxp_dict[] = @@ -1163,37 +1142,21 @@ static PyObject *PyFT2Font_get_sfnt_table(PyFT2Font *self, PyObject *args) "s:H, s:H, s:H, s:H, s:H, s:H, s:H, s:H}"; TT_MaxProfile *t = (TT_MaxProfile *)table; return Py_BuildValue(maxp_dict, - "version", - FIXED_MAJOR(t->version), - FIXED_MINOR(t->version), - "numGlyphs", - t->numGlyphs, - "maxPoints", - t->maxPoints, - "maxContours", - t->maxContours, - "maxComponentPoints", - t->maxCompositePoints, - "maxComponentContours", - t->maxCompositeContours, - "maxZones", - t->maxZones, - "maxTwilightPoints", - t->maxTwilightPoints, - "maxStorage", - t->maxStorage, - "maxFunctionDefs", - t->maxFunctionDefs, - "maxInstructionDefs", - t->maxInstructionDefs, - "maxStackElements", - t->maxStackElements, - "maxSizeOfInstructions", - t->maxSizeOfInstructions, - "maxComponentElements", - t->maxComponentElements, - "maxComponentDepth", - t->maxComponentDepth); + "version", FIXED_MAJOR(t->version), FIXED_MINOR(t->version), + "numGlyphs", t->numGlyphs, + "maxPoints", t->maxPoints, + "maxContours", t->maxContours, + "maxComponentPoints", t->maxCompositePoints, + "maxComponentContours", t->maxCompositeContours, + "maxZones", t->maxZones, + "maxTwilightPoints", t->maxTwilightPoints, + "maxStorage", t->maxStorage, + "maxFunctionDefs", t->maxFunctionDefs, + "maxInstructionDefs", t->maxInstructionDefs, + "maxStackElements", t->maxStackElements, + "maxSizeOfInstructions", t->maxSizeOfInstructions, + "maxComponentElements", t->maxComponentElements, + "maxComponentDepth", t->maxComponentDepth); } case 2: { char os_2_dict[] = @@ -1202,55 +1165,28 @@ static PyObject *PyFT2Font_get_sfnt_table(PyFT2Font *self, PyObject *args) "s:y#, s:H, s:H, s:H}"; TT_OS2 *t = (TT_OS2 *)table; return Py_BuildValue(os_2_dict, - "version", - t->version, - "xAvgCharWidth", - t->xAvgCharWidth, - "usWeightClass", - t->usWeightClass, - "usWidthClass", - t->usWidthClass, - "fsType", - t->fsType, - "ySubscriptXSize", - t->ySubscriptXSize, - "ySubscriptYSize", - t->ySubscriptYSize, - "ySubscriptXOffset", - t->ySubscriptXOffset, - "ySubscriptYOffset", - t->ySubscriptYOffset, - "ySuperscriptXSize", - t->ySuperscriptXSize, - "ySuperscriptYSize", - t->ySuperscriptYSize, - "ySuperscriptXOffset", - t->ySuperscriptXOffset, - "ySuperscriptYOffset", - t->ySuperscriptYOffset, - "yStrikeoutSize", - t->yStrikeoutSize, - "yStrikeoutPosition", - t->yStrikeoutPosition, - "sFamilyClass", - t->sFamilyClass, - "panose", - t->panose, - Py_ssize_t(10), - "ulCharRange", - t->ulUnicodeRange1, - t->ulUnicodeRange2, - t->ulUnicodeRange3, - t->ulUnicodeRange4, - "achVendID", - t->achVendID, - Py_ssize_t(4), - "fsSelection", - t->fsSelection, - "fsFirstCharIndex", - t->usFirstCharIndex, - "fsLastCharIndex", - t->usLastCharIndex); + "version", t->version, + "xAvgCharWidth", t->xAvgCharWidth, + "usWeightClass", t->usWeightClass, + "usWidthClass", t->usWidthClass, + "fsType", t->fsType, + "ySubscriptXSize", t->ySubscriptXSize, + "ySubscriptYSize", t->ySubscriptYSize, + "ySubscriptXOffset", t->ySubscriptXOffset, + "ySubscriptYOffset", t->ySubscriptYOffset, + "ySuperscriptXSize", t->ySuperscriptXSize, + "ySuperscriptYSize", t->ySuperscriptYSize, + "ySuperscriptXOffset", t->ySuperscriptXOffset, + "ySuperscriptYOffset", t->ySuperscriptYOffset, + "yStrikeoutSize", t->yStrikeoutSize, + "yStrikeoutPosition", t->yStrikeoutPosition, + "sFamilyClass", t->sFamilyClass, + "panose", t->panose, Py_ssize_t(10), + "ulCharRange", t->ulUnicodeRange1, t->ulUnicodeRange2, t->ulUnicodeRange3, t->ulUnicodeRange4, + "achVendID", t->achVendID, Py_ssize_t(4), + "fsSelection", t->fsSelection, + "fsFirstCharIndex", t->usFirstCharIndex, + "fsLastCharIndex", t->usLastCharIndex); } case 3: { char hhea_dict[] = @@ -1258,33 +1194,19 @@ static PyObject *PyFT2Font_get_sfnt_table(PyFT2Font *self, PyObject *args) "s:h, s:h, s:h, s:h, s:H}"; TT_HoriHeader *t = (TT_HoriHeader *)table; return Py_BuildValue(hhea_dict, - "version", - FIXED_MAJOR(t->Version), - FIXED_MINOR(t->Version), - "ascent", - t->Ascender, - "descent", - t->Descender, - "lineGap", - t->Line_Gap, - "advanceWidthMax", - t->advance_Width_Max, - "minLeftBearing", - t->min_Left_Side_Bearing, - "minRightBearing", - t->min_Right_Side_Bearing, - "xMaxExtent", - t->xMax_Extent, - "caretSlopeRise", - t->caret_Slope_Rise, - "caretSlopeRun", - t->caret_Slope_Run, - "caretOffset", - t->caret_Offset, - "metricDataFormat", - t->metric_Data_Format, - "numOfLongHorMetrics", - t->number_Of_HMetrics); + "version", FIXED_MAJOR(t->Version), FIXED_MINOR(t->Version), + "ascent", t->Ascender, + "descent", t->Descender, + "lineGap", t->Line_Gap, + "advanceWidthMax", t->advance_Width_Max, + "minLeftBearing", t->min_Left_Side_Bearing, + "minRightBearing", t->min_Right_Side_Bearing, + "xMaxExtent", t->xMax_Extent, + "caretSlopeRise", t->caret_Slope_Rise, + "caretSlopeRun", t->caret_Slope_Run, + "caretOffset", t->caret_Offset, + "metricDataFormat", t->metric_Data_Format, + "numOfLongHorMetrics", t->number_Of_HMetrics); } case 4: { char vhea_dict[] = @@ -1292,58 +1214,33 @@ static PyObject *PyFT2Font_get_sfnt_table(PyFT2Font *self, PyObject *args) "s:h, s:h, s:h, s:h, s:H}"; TT_VertHeader *t = (TT_VertHeader *)table; return Py_BuildValue(vhea_dict, - "version", - FIXED_MAJOR(t->Version), - FIXED_MINOR(t->Version), - "vertTypoAscender", - t->Ascender, - "vertTypoDescender", - t->Descender, - "vertTypoLineGap", - t->Line_Gap, - "advanceHeightMax", - t->advance_Height_Max, - "minTopSideBearing", - t->min_Top_Side_Bearing, - "minBottomSizeBearing", - t->min_Bottom_Side_Bearing, - "yMaxExtent", - t->yMax_Extent, - "caretSlopeRise", - t->caret_Slope_Rise, - "caretSlopeRun", - t->caret_Slope_Run, - "caretOffset", - t->caret_Offset, - "metricDataFormat", - t->metric_Data_Format, - "numOfLongVerMetrics", - t->number_Of_VMetrics); + "version", FIXED_MAJOR(t->Version), FIXED_MINOR(t->Version), + "vertTypoAscender", t->Ascender, + "vertTypoDescender", t->Descender, + "vertTypoLineGap", t->Line_Gap, + "advanceHeightMax", t->advance_Height_Max, + "minTopSideBearing", t->min_Top_Side_Bearing, + "minBottomSizeBearing", t->min_Bottom_Side_Bearing, + "yMaxExtent", t->yMax_Extent, + "caretSlopeRise", t->caret_Slope_Rise, + "caretSlopeRun", t->caret_Slope_Run, + "caretOffset", t->caret_Offset, + "metricDataFormat", t->metric_Data_Format, + "numOfLongVerMetrics", t->number_Of_VMetrics); } case 5: { char post_dict[] = "{s:(h,H), s:(h,H), s:h, s:h, s:k, s:k, s:k, s:k, s:k}"; TT_Postscript *t = (TT_Postscript *)table; return Py_BuildValue(post_dict, - "format", - FIXED_MAJOR(t->FormatType), - FIXED_MINOR(t->FormatType), - "italicAngle", - FIXED_MAJOR(t->italicAngle), - FIXED_MINOR(t->italicAngle), - "underlinePosition", - t->underlinePosition, - "underlineThickness", - t->underlineThickness, - "isFixedPitch", - t->isFixedPitch, - "minMemType42", - t->minMemType42, - "maxMemType42", - t->maxMemType42, - "minMemType1", - t->minMemType1, - "maxMemType1", - t->maxMemType1); + "format", FIXED_MAJOR(t->FormatType), FIXED_MINOR(t->FormatType), + "italicAngle", FIXED_MAJOR(t->italicAngle), FIXED_MINOR(t->italicAngle), + "underlinePosition", t->underlinePosition, + "underlineThickness", t->underlineThickness, + "isFixedPitch", t->isFixedPitch, + "minMemType42", t->minMemType42, + "maxMemType42", t->maxMemType42, + "minMemType1", t->minMemType1, + "maxMemType1", t->maxMemType1); } case 6: { char pclt_dict[] = @@ -1351,35 +1248,19 @@ static PyObject *PyFT2Font_get_sfnt_table(PyFT2Font *self, PyObject *args) "s:b, s:b}"; TT_PCLT *t = (TT_PCLT *)table; return Py_BuildValue(pclt_dict, - "version", - FIXED_MAJOR(t->Version), - FIXED_MINOR(t->Version), - "fontNumber", - t->FontNumber, - "pitch", - t->Pitch, - "xHeight", - t->xHeight, - "style", - t->Style, - "typeFamily", - t->TypeFamily, - "capHeight", - t->CapHeight, - "symbolSet", - t->SymbolSet, - "typeFace", - t->TypeFace, - Py_ssize_t(16), - "characterComplement", - t->CharacterComplement, - Py_ssize_t(8), - "strokeWeight", - t->StrokeWeight, - "widthType", - t->WidthType, - "serifStyle", - t->SerifStyle); + "version", FIXED_MAJOR(t->Version), FIXED_MINOR(t->Version), + "fontNumber", t->FontNumber, + "pitch", t->Pitch, + "xHeight", t->xHeight, + "style", t->Style, + "typeFamily", t->TypeFamily, + "capHeight", t->CapHeight, + "symbolSet", t->SymbolSet, + "typeFace", t->TypeFace, Py_ssize_t(16), + "characterComplement", t->CharacterComplement, Py_ssize_t(8), + "strokeWeight", t->StrokeWeight, + "widthType", t->WidthType, + "serifStyle", t->SerifStyle); } default: Py_RETURN_NONE;