Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b3e6500

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 a0ee711 commit b3e6500

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
@@ -5,25 +5,13 @@
55
#include "ft2font.h"
66
#include "numpy/arrayobject.h"
77

8-
#include <cstddef>
98
#include <set>
109
#include <sstream>
1110
#include <unordered_map>
1211

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

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

347335
static py::array_t<double>
348-
PyFT2Font_set_text(PyFT2Font *self, std::u32string text, double angle = 0.0,
336+
PyFT2Font_set_text(PyFT2Font *self, std::u32string_view text, double angle = 0.0,
349337
FT_Int32 flags = FT_LOAD_FORCE_AUTOHINT)
350338
{
351339
std::vector<double> xys;
352-
std::vector<uint32_t> codepoints;
353-
size_t size;
354340

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

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

370351
const char *PyFT2Font_get_num_glyphs__doc__ =

0 commit comments

Comments
 (0)