Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5199773

Browse filesBrowse files
committed
Implement new/modify previous FT2Font functions
1 parent f6f2fd7 commit 5199773
Copy full SHA for 5199773

File tree

Expand file treeCollapse file tree

1 file changed

+133
-17
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+133
-17
lines changed

‎src/ft2font.cpp

Copy file name to clipboardExpand all lines: src/ft2font.cpp
+133-17Lines changed: 133 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,8 @@ FT2Image::draw_rect_filled(unsigned long x0, unsigned long y0, unsigned long x1,
169169
m_dirty = true;
170170
}
171171

172-
static FT_UInt ft_get_char_index_or_warn(FT_Face face, FT_ULong charcode)
172+
static FT_UInt ft_glyph_warn(FT_ULong charcode)
173173
{
174-
FT_UInt glyph_index = FT_Get_Char_Index(face, charcode);
175-
if (glyph_index) {
176-
return glyph_index;
177-
}
178174
PyObject *text_helpers = NULL, *tmp = NULL;
179175
if (!(text_helpers = PyImport_ImportModule("matplotlib._text_helpers")) ||
180176
!(tmp = PyObject_CallMethod(text_helpers, "warn_on_missing_glyph", "k", charcode))) {
@@ -189,6 +185,14 @@ static FT_UInt ft_get_char_index_or_warn(FT_Face face, FT_ULong charcode)
189185
return 0;
190186
}
191187

188+
static FT_UInt ft_get_char_index_or_warn(FT_Face face, FT_ULong charcode)
189+
{
190+
FT_UInt glyph_index = FT_Get_Char_Index(face, charcode);
191+
if (glyph_index) {
192+
return glyph_index;
193+
}
194+
return ft_glyph_warn(charcode);
195+
}
192196

193197
// ft_outline_decomposer should be passed to FT_Outline_Decompose. On the
194198
// first pass, vertices and codes are set to NULL, and index is simply
@@ -319,7 +323,10 @@ FT2Font::get_path()
319323
return Py_BuildValue("NN", vertices.pyobj(), codes.pyobj());
320324
}
321325

322-
FT2Font::FT2Font(FT_Open_Args &open_args, long hinting_factor_) : image(), face(NULL)
326+
FT2Font::FT2Font(FT_Open_Args &open_args,
327+
long hinting_factor_,
328+
std::vector<FT2Font *> &fallback_list)
329+
: image(), face(NULL)
323330
{
324331
clear();
325332

@@ -353,6 +360,9 @@ FT2Font::FT2Font(FT_Open_Args &open_args, long hinting_factor_) : image(), face(
353360

354361
FT_Matrix transform = { 65536 / hinting_factor, 0, 0, 65536 };
355362
FT_Set_Transform(face, &transform, 0);
363+
364+
// Set fallbacks
365+
set_fallbacks(fallback_list);
356366
}
357367

358368
FT2Font::~FT2Font()
@@ -364,6 +374,10 @@ FT2Font::~FT2Font()
364374
if (face) {
365375
FT_Done_Face(face);
366376
}
377+
378+
for (uint i = 0; i < fallbacks.size(); i ++) {
379+
fallbacks[i]->~FT2Font();
380+
}
367381
}
368382

369383
void FT2Font::clear()
@@ -376,6 +390,17 @@ void FT2Font::clear()
376390
}
377391

378392
glyphs.clear();
393+
394+
for (uint i = 0; i < fallbacks.size(); i ++) {
395+
fallbacks[i]->clear();
396+
}
397+
}
398+
399+
void FT2Font::check() {
400+
printf("Fallback num: -1; Numface: %lu\n", get_face()->num_glyphs);
401+
for (uint i = 0; i < fallbacks.size(); i++) {
402+
printf("Fallback num: %u; Numface: %lu\n", i, fallbacks[i]->get_face()->num_glyphs);
403+
}
379404
}
380405

381406
void FT2Font::set_size(double ptsize, double dpi)
@@ -387,8 +412,18 @@ void FT2Font::set_size(double ptsize, double dpi)
387412
}
388413
FT_Matrix transform = { 65536 / hinting_factor, 0, 0, 65536 };
389414
FT_Set_Transform(face, &transform, 0);
415+
416+
for (uint i = 0; i < fallbacks.size(); i++) {
417+
fallbacks[i]->set_size(ptsize, dpi);
418+
}
390419
}
391420

421+
void FT2Font::set_fallbacks(std::vector<FT2Font *> &fallback_list)
422+
{
423+
fallbacks.assign(fallback_list.begin(), fallback_list.end());
424+
}
425+
426+
392427
void FT2Font::set_charmap(int i)
393428
{
394429
if (i >= face->num_charmaps) {
@@ -421,15 +456,32 @@ int FT2Font::get_kerning(FT_UInt left, FT_UInt right, FT_UInt mode)
421456
}
422457
}
423458

459+
int FT2Font::get_kerning(FT_UInt left, FT_UInt right, FT_UInt mode, FT_Vector &delta)
460+
{
461+
if (!FT_HAS_KERNING(face)) {
462+
return 0;
463+
}
464+
465+
if (!FT_Get_Kerning(face, left, right, mode, &delta)) {
466+
return (int)(delta.x) / (hinting_factor << kerning_factor);
467+
} else {
468+
return 0;
469+
}
470+
}
471+
424472
void FT2Font::set_kerning_factor(int factor)
425473
{
426474
kerning_factor = factor;
475+
for (uint i = 0; i < fallbacks.size(); i ++){
476+
fallbacks[i]->set_kerning_factor(factor);
477+
}
427478
}
428479

429480
void FT2Font::set_text(
430481
size_t N, uint32_t *codepoints, double angle, FT_Int32 flags, std::vector<double> &xys)
431482
{
432483
FT_Matrix matrix; /* transformation matrix */
484+
check();
433485

434486
angle = angle / 360.0 * 2 * M_PI;
435487

@@ -452,28 +504,47 @@ void FT2Font::set_text(
452504
FT_BBox glyph_bbox;
453505
FT_Pos last_advance;
454506

455-
glyph_index = ft_get_char_index_or_warn(face, codepoints[n]);
507+
FT_UInt final_glyph_index;
508+
FT_Error charcode_error, glyph_error;
509+
FT2Font *ft_object_with_glyph = this;
510+
printf("\nBefore loading char! \n");
511+
load_char_with_fallback(ft_object_with_glyph, final_glyph_index, glyphs, codepoints[n],
512+
flags, charcode_error, glyph_error);
513+
printf("Final ft2font: %lu\n", ft_object_with_glyph->get_face()->num_glyphs);
514+
if (charcode_error || glyph_error) {
515+
ft_glyph_warn((FT_ULong)codepoints[n]);
516+
return;
517+
}
518+
519+
glyph_index = final_glyph_index;
456520

457521
// retrieve kerning distance and move pen position
458522
if (use_kerning && previous && glyph_index) {
459523
FT_Vector delta;
460-
FT_Get_Kerning(face, previous, glyph_index, FT_KERNING_DEFAULT, &delta);
524+
ft_object_with_glyph->get_kerning(previous, glyph_index, FT_KERNING_DEFAULT, delta);
461525
pen.x += delta.x / (hinting_factor << kerning_factor);
462526
}
463-
if (FT_Error error = FT_Load_Glyph(face, glyph_index, flags)) {
464-
throw_ft_error("Could not load glyph", error);
465-
}
527+
528+
// DONE with load_char_with_fallback
529+
// if (FT_Error error = FT_Load_Glyph(face, glyph_index, flags)) {
530+
// throw_ft_error("Could not load glyph", error);
531+
// }
532+
466533
// ignore errors, jump to next glyph
467534

468535
// extract glyph image and store it in our table
469536

470-
FT_Glyph thisGlyph;
471-
if (FT_Error error = FT_Get_Glyph(face->glyph, &thisGlyph)) {
472-
throw_ft_error("Could not get glyph", error);
473-
}
537+
// DONE with load_char_with_fallback
538+
// FT_Glyph thisGlyph;
539+
// if (FT_Error error = FT_Get_Glyph(face->glyph, &thisGlyph)) {
540+
// throw_ft_error("Could not get glyph", error);
541+
// }
474542
// ignore errors, jump to next glyph
475543

476-
last_advance = face->glyph->advance.x;
544+
FT_Glyph &thisGlyph = glyphs[glyphs.size() - 1];
545+
546+
// Compare with thisGlyph?
547+
last_advance = ft_object_with_glyph->get_face()->glyph->advance.x;
477548
FT_Glyph_Transform(thisGlyph, 0, &pen);
478549
FT_Glyph_Transform(thisGlyph, &matrix, 0);
479550
xys.push_back(pen.x);
@@ -489,7 +560,10 @@ void FT2Font::set_text(
489560
pen.x += last_advance;
490561

491562
previous = glyph_index;
492-
glyphs.push_back(thisGlyph);
563+
564+
printf("Set_Text complete! \n");
565+
// DONE with load_char_with_fallback
566+
// glyphs.push_back(thisGlyph);
493567
}
494568

495569
FT_Vector_Transform(&pen, &matrix);
@@ -513,6 +587,48 @@ void FT2Font::load_char(long charcode, FT_Int32 flags)
513587
glyphs.push_back(thisGlyph);
514588
}
515589

590+
void FT2Font::load_char_with_fallback(FT2Font* &ft_object_with_glyph,
591+
FT_UInt &final_glyph_index,
592+
std::vector<FT_Glyph> &parent_glyphs,
593+
long charcode,
594+
FT_Int32 flags,
595+
FT_Error &charcode_error,
596+
FT_Error &glyph_error)
597+
{
598+
printf("loading char!\n");
599+
FT_UInt glyph_index = FT_Get_Char_Index(face, charcode);
600+
601+
if (glyph_index) {
602+
printf("glyph found!\n");
603+
charcode_error = FT_Load_Glyph(face, glyph_index, flags);
604+
// throw_ft_error("Could not load charcode", error);
605+
FT_Glyph thisGlyph;
606+
glyph_error = FT_Get_Glyph(face->glyph, &thisGlyph);
607+
// throw_ft_error("Could not get glyph", error);
608+
final_glyph_index = glyph_index;
609+
parent_glyphs.push_back(thisGlyph);
610+
}
611+
612+
else {
613+
printf("glyph not found! Fallback size: %lu\n", fallbacks.size());
614+
for (uint i = 0; i < fallbacks.size(); ++i) {
615+
uint current_size = parent_glyphs.size();
616+
printf("Fallback %u: %u\n", i, current_size);
617+
fallbacks[i]->load_char_with_fallback(ft_object_with_glyph, final_glyph_index,
618+
parent_glyphs, charcode, flags, charcode_error,
619+
glyph_error);
620+
printf("Got back from fallback load char!\n");
621+
// jump out if glyph size increased
622+
if (parent_glyphs.size() == current_size + 1) {
623+
printf("size increased!\n");
624+
ft_object_with_glyph = fallbacks[i];
625+
printf("Ft object assigned!\n");
626+
return;
627+
}
628+
}
629+
}
630+
}
631+
516632
void FT2Font::load_glyph(FT_UInt glyph_index, FT_Int32 flags)
517633
{
518634
if (FT_Error error = FT_Load_Glyph(face, glyph_index, flags)) {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.