pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/matplotlib/matplotlib/pull/31434/files

://github.githubassets.com/assets/primer-primitives-10bf9dd67e3d70bd.css" /> ft2font: Add internal API for accessing font variations by QuLogic · Pull Request #31434 · matplotlib/matplotlib · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions lib/matplotlib/ft2font.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,34 @@ class _SfntPcltDict(TypedDict):
widthType: int
serifStyle: int

class VarAxisFlags(Flag):
DEFAULT = cast(int, ...)
HIDDEN = cast(int, ...)

@final
class VariationAxis:
@property
def name(self) -> str: ...
@property
def minimum(self) -> float: ...
@property
def default(self) -> float: ...
@property
def maximum(self) -> float: ...
@property
def tag(self) -> int: ...
@property
def names(self) -> dict[tuple[int, int, int], str | bytes]: ...
@property
def flags(self) -> VarAxisFlags: ...

@final
class VariationNamedStyle:
@property
def names(self) -> dict[tuple[int, int, int], str | bytes]: ...
@property
def psnames(self) -> dict[tuple[int, int, int], str | bytes] | None: ...

@final
class LayoutItem:
@property
Expand Down Expand Up @@ -299,6 +327,10 @@ class FT2Font(Buffer):
features: tuple[str] | None = ...,
language: str | list[tuple[str, int, int]] | None = ...,
) -> NDArray[np.float64]: ...
def get_variation_descriptor(self) -> tuple[list[VariationAxis], list[VariationNamedStyle]]: ...
def get_default_variation_style(self) -> int: ...
def get_variations(self) -> tuple[float, ...]: ...
def set_variations(self, variations: tuple[float, ...] | int) -> None: ...
@property
def ascender(self) -> int: ...
@property
Expand Down
14 changes: 14 additions & 0 deletions lib/matplotlib/tests/test_ft2font.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,20 @@ def test_ft2font_loading():
assert font.get_bitmap_offset() == (0, 0)


def test_ft2font_variations_invalid():
# Smoke test as we don't have a font that has variations built in.
file = fm.findfont('DejaVu Sans')
font = ft2font.FT2Font(file)
with pytest.raises(RuntimeError):
font.get_variation_descriptor()
with pytest.raises(RuntimeError):
font.get_variations()
with pytest.raises(RuntimeError):
font.get_default_variation_style()
with pytest.raises(RuntimeError):
font.set_variations([0.0])


def test_ft2font_drawing():
expected_str = (
' ',
Expand Down
70 changes: 70 additions & 0 deletions src/ft2font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -780,3 +780,73 @@ long FT2Font::get_name_index(char *name)
{
return FT_Get_Name_Index(face, (FT_String *)name);
}

FT2Font::VariationInfo FT2Font::get_variation_descriptor()
{
FT_MM_Var *master;
FT_CHECK(FT_Get_MM_Var, face, &master);

std::vector<VariationAxis> axes;
axes.reserve(master->num_axis);
for (FT_UInt axis_index = 0; axis_index < master->num_axis; axis_index++) {
FT_UInt flags = 0;
FT_CHECK(FT_Get_Var_Axis_Flags, master, axis_index, &flags);
const auto &axis = master->axis[axis_index];
axes.emplace_back(axis.name,
float(axis.minimum) / (1<<16),
float(axis.def) / (1<<16),
float(axis.maximum) / (1<<16),
axis.tag,
axis.strid,
flags);
}

std::vector<VariationNamedStyle> styles;
styles.reserve(master->num_namedstyles);
for (FT_UInt instance = 0; instance < master->num_namedstyles; instance++) {
const auto &style = master->namedstyle[instance];
styles.emplace_back(style.strid, style.psid);
}

FT_CHECK(FT_Done_MM_Var, _ft2Library, master);

return {axes, styles};
}

FT_UInt FT2Font::get_default_variation_style() {
FT_UInt result;
FT_CHECK(FT_Get_Default_Named_Instance, face, &result);
return result;
}

std::vector<double> FT2Font::get_variations() {
FT_MM_Var *master;
FT_CHECK(FT_Get_MM_Var, face, &master);
std::vector<FT_Fixed> fixed_coords(master->num_axis);
FT_CHECK(FT_Done_MM_Var, _ft2Library, master);

FT_CHECK(FT_Get_Var_Design_Coordinates, face, fixed_coords.size(),
fixed_coords.data());

std::vector<double> coords;
coords.reserve(fixed_coords.size());
for (auto const &c : fixed_coords) {
coords.emplace_back(static_cast<double>(c) / (1 << 16));
}

return coords;
}

void FT2Font::set_variations(std::vector<double> coords) {
std::vector<FT_Fixed> fixed_coords;
fixed_coords.reserve(coords.size());
for (auto const &c : coords) {
fixed_coords.emplace_back(static_cast<FT_Fixed>(c * (1 << 16)));
}
FT_CHECK(FT_Set_Var_Design_Coordinates, face, fixed_coords.size(),
fixed_coords.data());
}

void FT2Font::set_variations(FT_UInt instance_index) {
FT_CHECK(FT_Set_Named_Instance, face, instance_index);
}
19 changes: 19 additions & 0 deletions src/ft2font.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ extern "C" {
#include FT_BITMAP_H
#include FT_FREETYPE_H
#include FT_GLYPH_H
#include FT_MULTIPLE_MASTERS_H
#include FT_OUTLINE_H
#include FT_SFNT_NAMES_H
#include FT_TYPE1_TABLES_H
#include FT_TRUETYPE_IDS_H
#include FT_TRUETYPE_TABLES_H
}

Expand Down Expand Up @@ -183,6 +185,23 @@ class FT2Font
return FT_HAS_KERNING(face);
}

using VariationAxis = std::tuple<
std::string, // name
FT_Fixed, // minimum
FT_Fixed, // default
FT_Fixed, // maximum
FT_ULong, // tag
FT_UInt, // name ID
FT_UInt>; // flags
using VariationNamedStyle = std::tuple<FT_UInt, FT_UInt>; // name ID, postscript ID
using VariationInfo = std::tuple<std::vector<VariationAxis>,
std::vector<VariationNamedStyle>>;
VariationInfo get_variation_descriptor();
FT_UInt get_default_variation_style();
std::vector<double> get_variations();
void set_variations(std::vector<double> coords);
void set_variations(FT_UInt instance_index);

protected:
virtual void ft_glyph_warn(FT_ULong charcode, std::set<FT_String*> family_names) = 0;
private:
Expand Down
Loading
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy