Skip to content

Commit abd9b8a

Browse files
committed
Fix matplotlib#253. Use advance width to calculate bounding box.
Also, fixes a long-standing bug in kerning.
1 parent 41efb46 commit abd9b8a

3 files changed

Lines changed: 7 additions & 13 deletions

File tree

‎lib/matplotlib/backends/backend_pdf.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2022,7 +2022,7 @@ def draw_text_woven(chunks):
20222022
else:
20232023
kern = 0
20242024
lastgind = gind
2025-
newx += kern/64.0 + glyph.linearHoriAdvance/65536.0
2025+
newx += -kern/64.0 + glyph.linearHoriAdvance/65536.0
20262026

20272027
if mode == 1:
20282028
self.file.output(Op.end_text)

‎lib/matplotlib/backends/backend_ps.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
802802
else:
803803
kern = 0
804804
lastgind = gind
805-
thisx += kern/64.0
805+
thisx -= kern/64.0
806806

807807
lines.append('%f %f m /%s glyphshow'%(thisx, thisy, name))
808808
thisx += glyph.linearHoriAdvance/65536.0

‎src/ft2font.cpp‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -586,15 +586,9 @@ FT_BBox FT2Font::compute_string_bbox()
586586
if (glyph_bbox.yMin < bbox.yMin) {
587587
bbox.yMin = glyph_bbox.yMin;
588588
}
589-
if (glyph_bbox.xMin == glyph_bbox.xMax) {
590-
right_side += glyphs[n]->advance.x >> 10;
591-
if (right_side > bbox.xMax) {
592-
bbox.xMax = right_side;
593-
}
594-
} else {
595-
if (glyph_bbox.xMax > bbox.xMax) {
596-
bbox.xMax = glyph_bbox.xMax;
597-
}
589+
right_side += glyphs[n]->advance.x >> 10;
590+
if (right_side > bbox.xMax) {
591+
bbox.xMax = right_side;
598592
}
599593
if (glyph_bbox.yMax > bbox.yMax)
600594
bbox.yMax = glyph_bbox.yMax;
@@ -617,7 +611,7 @@ int FT2Font::get_kerning(int left, int right, int mode)
617611
FT_Vector delta;
618612

619613
if (!FT_Get_Kerning(face, left, right, mode, &delta)) {
620-
return (int)(delta.x / hinting_factor);
614+
return (int)(delta.x) / (hinting_factor << 16);
621615
} else {
622616
return 0;
623617
}
@@ -651,7 +645,7 @@ void FT2Font::set_text(
651645
if (use_kerning && previous && glyph_index) {
652646
FT_Vector delta;
653647
FT_Get_Kerning(face, previous, glyph_index, FT_KERNING_DEFAULT, &delta);
654-
pen.x += delta.x / hinting_factor;
648+
pen.x -= (delta.x << 10) / (hinting_factor << 16);
655649
}
656650
error = FT_Load_Glyph(face, glyph_index, flags);
657651
if (error) {

0 commit comments

Comments
 (0)