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
4 changes: 2 additions & 2 deletions src/_backend_agg.h
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ class span_conv_alpha
void prepare()
{
}
void generate(color_type *span, int x, int y, unsigned len) const
void generate(color_type *span, [[maybe_unused]] int x, [[maybe_unused]] int y, unsigned len) const
{
do {
span->a = (agg::int8u)((double)span->a * m_alpha);
Expand Down Expand Up @@ -1054,7 +1054,7 @@ class QuadMeshGenerator
return 5;
}

inline bool should_simplify()
constexpr bool should_simplify()
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/_c_internal_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ static py::object
mpl_GetCurrentProcessExplicitAppUserModelID(void)
{
#ifdef _WIN32
wchar_t* appid = NULL;
wchar_t* appid = nullptr;
HRESULT hr = GetCurrentProcessExplicitAppUserModelID(&appid);
if (FAILED(hr)) {
PyErr_SetFromWindowsErr(hr);
Expand Down
2 changes: 1 addition & 1 deletion src/_image_resample.h
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ class span_conv_alpha

void prepare() {}

void generate(color_type* span, int x, int y, unsigned len) const
void generate(color_type* span, [[maybe_unused]] int x, [[maybe_unused]] int y, unsigned len) const
{
if (m_alpha != 1.0) {
do {
Expand Down
14 changes: 11 additions & 3 deletions src/_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void point_in_path_impl(PointArray &points, PathIterator &path, ResultArray &ins
bool all_done;

size_t n = safe_first_shape(points);
assert(safe_first_shape(inside_flag) >= n);
assert(static_cast<size_t>(safe_first_shape(inside_flag)) >= n);

std::vector<uint8_t> yflag0(n);
std::vector<uint8_t> subpath_flag(n);
Expand Down Expand Up @@ -238,13 +238,13 @@ void point_in_path_impl(PointArray &points, PathIterator &path, ResultArray &ins
}

template <class PathIterator, class PointArray, class ResultArray>
inline void points_in_path(PointArray &points,
constexpr void points_in_path(PointArray &points,
const double r,
PathIterator &path,
agg::trans_affine &trans,
ResultArray &result)
{
assert(safe_first_shape(result) >= safe_first_shape(points));
assert(static_cast<py::ssize_t>(safe_first_shape(result)) >= safe_first_shape(points));
for (auto i = 0; i < safe_first_shape(points); ++i) {
result[i] = false;
}
Expand Down Expand Up @@ -1021,6 +1021,14 @@ void __add_number(double val, char format_code, int precision,
{
char *str = PyOS_double_to_string(
val, format_code, precision, Py_DTSF_ADD_DOT_0, nullptr);
if (str == nullptr) {
const char* template_msg = "Cannot call PyOS_double_to_string within %s "
"with the following arguments: val=%f, format_code=%c, precision=%d";
int sz = std::snprintf(nullptr, 0, template_msg, __func__, val, format_code, precision);
std::vector<char> buf(sz + 1); // note +1 for null terminator
std::sprintf(buf.data(), template_msg, __func__, val, format_code, precision); // certain to fit
throw std::invalid_argument(buf.data());
}
// Delete trailing zeros and decimal point
char *c = str + strlen(str) - 1; // Start at last character.
// Rewind through all the zeros and, if present, the trailing decimal
Expand Down
5 changes: 5 additions & 0 deletions src/_qhull_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ class QhullInfo {
}
}

QhullInfo(QhullInfo& other) = delete;
QhullInfo(const QhullInfo& other) = delete;
QhullInfo& operator=(QhullInfo& other) = delete;
QhullInfo& operator=(const QhullInfo& other) = delete;

private:
FILE* error_file;
qhT* qh;
Expand Down
4 changes: 2 additions & 2 deletions src/_tkagg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ using namespace pybind11::literals;
* also.
*/
#define WIN32_DLL
static inline PyObject *PyErr_SetFromWindowsErr(int ierr) {
static constexpr PyObject *PyErr_SetFromWindowsErr(int ierr) {
PyErr_SetString(PyExc_OSError, "Call to EnumProcessModules failed");
return NULL;
return nullptr;
}
#endif

Expand Down
14 changes: 7 additions & 7 deletions src/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ class scalar
{
}

T &operator()(int i, int j = 0, int k = 0)
T &operator()([[maybe_unused]] int i, [[maybe_unused]] int j = 0, [[maybe_unused]] int k = 0)
{
return m_value;
}

const T &operator()(int i, int j = 0, int k = 0) const
const T &operator()([[maybe_unused]] int i, [[maybe_unused]] int j = 0, [[maybe_unused]] int k = 0) const
{
return m_value;
}

int shape(size_t i)
int shape([[maybe_unused]] size_t i)
{
return 1;
}
Expand All @@ -58,22 +58,22 @@ class empty

empty() = default;

T &operator()(int i, int j = 0, int k = 0)
T &operator()([[maybe_unused]] int i, [[maybe_unused]] int j = 0, [[maybe_unused]] int k = 0)
{
throw std::runtime_error("Accessed empty array");
}

const T &operator()(int i, int j = 0, int k = 0) const
const T &operator()([[maybe_unused]] int i, [[maybe_unused]] int j = 0, [[maybe_unused]] int k = 0) const
{
throw std::runtime_error("Accessed empty array");
}

sub_t operator[](int i) const
sub_t operator[]([[maybe_unused]] int i) const
{
return empty<T>();
}

int shape(size_t i) const
int shape([[maybe_unused]] size_t i) const
{
return 0;
}
Expand Down
26 changes: 20 additions & 6 deletions src/ft2font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
#include "mplutils.h"

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <iterator>
#include <limits>
#include <map>
#include <set>
#include <stdexcept>
Expand All @@ -17,8 +19,18 @@
#endif

FT2Image::FT2Image(unsigned long width, unsigned long height)
: m_buffer((unsigned char *)calloc(width * height, 1)), m_width(width), m_height(height)
: m_width(width), m_height(height)
{
size_t buffer_size = width * height;
if (buffer_size != 0 && buffer_size / width != height) {
const char* template_msg = "Cannot allocate a FT2Image "
"with the following arguments: width=%lu, height=%lu";
int sz = std::snprintf(nullptr, 0, template_msg, width, height);
std::vector<char> buf(sz + 1); // note +1 for null terminator
std::sprintf(buf.data(), template_msg, width, height); // certain to fit
throw std::overflow_error(buf.data());
}
m_buffer = static_cast<unsigned char *>(calloc(width * height, 1));
}

FT2Image::~FT2Image()
Expand Down Expand Up @@ -148,7 +160,10 @@ static FT_Outline_Funcs ft_outline_funcs = {
ft_outline_move_to,
ft_outline_line_to,
ft_outline_conic_to,
ft_outline_cubic_to};
ft_outline_cubic_to,
0,
0,
};

void
FT2Font::get_path(std::vector<double> &vertices, std::vector<unsigned char> &codes)
Expand All @@ -169,8 +184,7 @@ FT2Font::get_path(std::vector<double> &vertices, std::vector<unsigned char> &cod
codes.reserve(estimated_points);
if (FT_Error error = FT_Outline_Decompose(
&face->glyph->outline, &ft_outline_funcs, &decomposer)) {
throw std::runtime_error("FT_Outline_Decompose failed with error " +
std::to_string(error));
THROW_FT_ERROR("Decompose font outline", error);
}
if (vertices.empty()) { // Don't append CLOSEPOLY to null glyphs.
return;
Expand Down Expand Up @@ -474,12 +488,12 @@ void FT2Font::set_text(
FT_Error error;
error = FT_Load_Glyph(rglyph.ftface, rglyph.index, flags);
if (error) {
throw std::runtime_error("failed to load glyph");
THROW_FT_ERROR("Loading glyphs", error);
}
FT_Glyph thisGlyph;
error = FT_Get_Glyph(rglyph.ftface->glyph, &thisGlyph);
if (error) {
throw std::runtime_error("failed to get glyph");
THROW_FT_ERROR("Getting glyphs", error);
}

pen.x += rglyph.x_offset;
Expand Down
3 changes: 2 additions & 1 deletion src/ft2font.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
extern "C" {
#include <ft2build.h>
#include FT_BITMAP_H
#include FT_ERRORS_H
#include FT_FREETYPE_H
#include FT_GLYPH_H
#include FT_OUTLINE_H
Expand All @@ -37,7 +38,7 @@ namespace py = pybind11;
#define FIXED_MINOR(val) (unsigned short)(val & 0xffff)

// Error handling (error codes are loaded as described in fterror.h).
inline char const* ft_error_string(FT_Error error) {
constexpr char const* ft_error_string(FT_Error error) {
#undef __FTERRORS_H__
#define FT_ERROR_START_LIST switch (error) {
#define FT_ERRORDEF( e, v, s ) case v: return s;
Expand Down
32 changes: 15 additions & 17 deletions src/ft2font_wrapper.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <memory>
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <pybind11/pybind11.h>
#include <pybind11/native_enum.h>
Expand Down Expand Up @@ -298,11 +299,10 @@ class PyFT2Font final : public FT2Font

void ft_glyph_warn(FT_ULong charcode, std::set<FT_String*> family_names)
{
std::set<FT_String*>::iterator it = family_names.begin();
std::stringstream ss;
ss<< (*it ? *it : "unknown family name");
while(++it != family_names.end()){
ss<<", "<< (*it ? *it : "unknown family name");
std::ostringstream ss;
for (const auto& fname : family_names) {
ss << (fname != nullptr ? fname : "unknown family name");
if (fname != *family_names.rbegin()) ss << ", ";
}

auto text_helpers = py::module_::import("matplotlib._text_helpers");
Expand Down Expand Up @@ -366,16 +366,15 @@ read_from_file_callback(FT_Stream stream, unsigned long offset, unsigned char *b
static void
close_file_callback(FT_Stream stream)
{
PyObject *type, *value, *traceback;
PyErr_Fetch(&type, &value, &traceback);
PyObject* exc = PyErr_GetRaisedException();
PyFT2Font *self = (PyFT2Font *)stream->descriptor.pointer;
try {
self->py_file.attr("close")();
} catch (py::error_already_set &eas) {
eas.discard_as_unraisable(__func__);
}
self->py_file = py::object();
PyErr_Restore(type, value, traceback);
PyErr_SetRaisedException(exc);
}

const char *PyFT2Font_init__doc__ = R"""(
Expand All @@ -400,10 +399,9 @@ const char *PyFT2Font_init__doc__ = R"""(
This API is private: do not use it directly.
)""";

static PyFT2Font *
PyFT2Font_init(FT_Library ft2Library, py::object filename,
std::optional<long> hinting_factor = std::nullopt,
FT_Long face_index = 0,
static std::unique_ptr<PyFT2Font>
PyFT2Font_init(FT_Library ft2Library, py::object filename, std::optional<long> hinting_factor = std::nullopt,
FT_ULong face_index = 0,
std::optional<std::vector<PyFT2Font *>> fallback_list = std::nullopt,
std::optional<int> kerning_factor = std::nullopt,
bool warn_if_used = false)
Expand All @@ -421,7 +419,7 @@ PyFT2Font_init(FT_Library ft2Library, py::object filename,
kerning_factor = 0;
}

if (face_index < 0 || face_index > 0xffff) {
if (face_index > 0xffff) {
throw std::range_error("face_index must be between 0 and 65535, inclusive");
}

Expand All @@ -432,7 +430,7 @@ PyFT2Font_init(FT_Library ft2Library, py::object filename,
std::back_inserter(fallback_fonts));
}

auto self = new PyFT2Font(fallback_fonts, warn_if_used);
auto self = std::make_unique<PyFT2Font>(fallback_fonts, warn_if_used);
self->set_kerning_factor(*kerning_factor);

if (fallback_list) {
Expand All @@ -446,7 +444,7 @@ PyFT2Font_init(FT_Library ft2Library, py::object filename,
self->stream.base = nullptr;
self->stream.size = 0x7fffffff; // Unknown size.
self->stream.pos = 0;
self->stream.descriptor.pointer = self;
self->stream.descriptor.pointer = self.get();
self->stream.read = &read_from_file_callback;
FT_Open_Args open_args;
memset((void *)&open_args, 0, sizeof(FT_Open_Args));
Expand Down Expand Up @@ -1572,10 +1570,10 @@ PYBIND11_MODULE(ft2font, m, py::mod_gil_not_used())
[ft2Library](
py::object filename,
std::optional<long> hinting_factor = std::nullopt,
FT_Long face_index = 0,
FT_ULong face_index = 0,
std::optional<std::vector<PyFT2Font *>> fallback_list = std::nullopt,
std::optional<int> kerning_factor = std::nullopt,
bool warn_if_used = false) -> PyFT2Font *
bool warn_if_used = false) -> std::unique_ptr<PyFT2Font>
{
return PyFT2Font_init(ft2Library, filename, hinting_factor, face_index,
fallback_list, kerning_factor, warn_if_used);
Expand Down
8 changes: 4 additions & 4 deletions src/mplutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
#endif


inline int mpl_round_to_int(double v)
constexpr int mpl_round_to_int(double v)
{
return (int)(v + ((v >= 0.0) ? 0.5 : -0.5));
}

inline double mpl_round(double v)
constexpr double mpl_round(double v)
{
return (double)mpl_round_to_int(v);
}
Expand All @@ -60,7 +60,7 @@ namespace py = pybind11;
using namespace pybind11::literals;

template<typename T>
inline void check_trailing_shape(T array, char const* name, long d1)
constexpr void check_trailing_shape(T array, char const* name, long d1)
{
if (array.ndim() != 2) {
throw py::value_error(
Expand All @@ -79,7 +79,7 @@ inline void check_trailing_shape(T array, char const* name, long d1)
}

template<typename T>
inline void check_trailing_shape(T array, char const* name, long d1, long d2)
constexpr void check_trailing_shape(T array, char const* name, long d1, long d2)
{
if (array.ndim() != 3) {
throw py::value_error(
Expand Down
Loading
Loading