10000 Simplify FT2Font.get_font · matplotlib/matplotlib@a9d4633 · GitHub
[go: up one dir, main page]

Skip to content

Commit a9d4633

Browse files
committed
Simplify FT2Font.get_font
Inline `convert_xys_to_array` and modify the arguments to take a C++ container, so we don't need a less-safe pointer, and we don't need to copy another time over.
1 parent b50bd8b commit a9d4633

File tree

3 files changed

+15
-34
lines changed

3 files changed

+15
-34
lines changed

src/ft2font.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ void FT2Font::set_kerning_factor(int factor)
397397
}
398398

399399
void FT2Font::set_text(
400-
size_t N, uint32_t *codepoints, double angle, FT_Int32 flags, std::vector<double> &xys)
400+
std::u32string_view text, double angle, FT_Int32 flags, std::vector<double> &xys)
401401
{
402402
FT_Matrix matrix; /* transformation matrix */
403403

@@ -420,7 +420,7 @@ void FT2Font::set_text(
420420
FT_UInt previous = 0;
421421
FT2Font *previous_ft_object = NULL;
422422

423-
for (size_t n = 0; n < N; n++) {
423+
for (auto codepoint : text) {
424424
FT_UInt glyph_index = 0;
425425
FT_BBox glyph_bbox;
426426
FT_Pos last_advance;
@@ -429,14 +429,14 @@ void FT2Font::set_text(
429429
std::set<FT_String*> glyph_seen_fonts;
430430
FT2Font *ft_object_with_glyph = this;
431431
bool was_found = load_char_with_fallback(ft_object_with_glyph, glyph_index, glyphs,
432-
char_to_font, glyph_to_font, codepoints[n], flags,
432+
char_to_font, glyph_to_font, codepoint, flags,
433433
charcode_error, glyph_error, glyph_seen_fonts, false);
434434
if (!was_found) {
435-
ft_glyph_warn((FT_ULong)codepoints[n], glyph_seen_fonts);
435+
ft_glyph_warn((FT_ULong)codepoint, glyph_seen_fonts);
436436
// render missing glyph tofu
437437
// come back to top-most font
438438
ft_object_with_glyph = this;
439-
char_to_font[codepoints[n]] = ft_object_with_glyph;
439+
char_to_font[codepoint] = ft_object_with_glyph;
440440
glyph_to_font[glyph_index] = ft_object_with_glyph;
441441
ft_object_with_glyph->load_glyph(glyph_index, flags, ft_object_with_glyph, false);
442442
}

src/ft2font.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
#ifndef MPL_FT2FONT_H
77
#define MPL_FT2FONT_H
88

9-
#include <cstdint>
109
#include <set>
1110
#include <string>
11+
#include <string_view>
1212
#include <unordered_map>
1313
#include <vector>
1414

@@ -77,8 +77,8 @@ class FT2Font
7777
void set_size(double ptsize, double dpi);
7878
void set_charmap(int i);
7979
void select_charmap(unsigned long i);
80-
void set_text(
81-
size_t N, uint32_t *codepoints, double angle, FT_Int32 flags, std::vector<double> &xys);
80+
void set_text(std::u32string_view codepoints, double angle, FT_Int32 flags,
81+
std::vector<double> &xys);
8282
int get_kerning(FT_UInt left, FT_UInt right, FT_UInt mode, bool fallback);
8383
int get_kerning(FT_UInt left, FT_UInt right, FT_UInt mode, FT_Vector &delta);
8484
void set_kerning_factor(int factor);

src/ft2font_wrapper.cpp

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,13 @@
66
#include "ft2font.h"
77
#include "numpy/arrayobject.h"
88

9-
#include <cstddef>
109
#include <set>
1110
#include <sstream>
1211
#include <unordered_map>
1312

1413
namespace py = pybind11;
1514
using namespace pybind11::literals;
1615

17-
static py::array_t<double>
18-
convert_xys_to_array(std::vector<double> &xys)
19-
{
20-
py::ssize_t dims[] = { static_cast<py::ssize_t>(xys.size()) / 2, 2 };
21-
py::array_t<double> result(dims);
22-
if (xys.size() > 0) {
23-
memcpy(result.mutable_data(), xys.data(), result.nbytes());
24-
}
25-
return result;
26-
}
27-
2816
/**********************************************************************
2917
* FT2Image
3018
* */
@@ -346,26 +334,19 @@ const char *PyFT2Font_set_text__doc__ =
346334
"A sequence of x,y positions in 26.6 subpixels is returned; divide by 64 for pixels.\n";
347335

348336
static py::array_t<double>
349-
PyFT2Font_set_text(PyFT2Font *self, std::u32string text, double angle = 0.0,
337+
PyFT2Font_set_text(PyFT2Font *self, std::u32string_view text, double angle = 0.0,
350338
FT_Int32 flags = FT_LOAD_FORCE_AUTOHINT)
351339
{
352340
std::vector<double> xys;
353-
std::vector<uint32_t> codepoints;
354-
size_t size;
355341

356-
size = text.size();
357-
codepoints.resize(size);
358-
for (size_t i = 0; i < size; ++i) {
359-
codepoints[i] = text[i];
360-
}
342+
self->x->set_text(text, angle, flags, xys);
361343

362-
uint32_t* codepoints_array = NULL;
363-
if (size > 0) {
364-
codepoints_array = &codepoints[0];
344+
py::ssize_t dims[] = { static_cast<py::ssize_t>(xys.size()) / 2, 2 };
345+
py::array_t<double> result(dims);
346+
if (xys.size() > 0) {
347+
memcpy(result.mutable_data(), xys.data(), result.nbytes());
365348
}
366-
self->x->set_text(size, codepoints_array, angle, flags, xys);
367-
368-
return convert_xys_to_array(xys);
349+
return result;
369350
}
370351

371352
const char *PyFT2Font_get_num_glyphs__doc__ =

0 commit comments

Comments
 (0)
0