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

Skip to content

Commit 29371f9

Browse files
committed
some font tweaks
svn path=/trunk/matplotlib/; revision=631
1 parent 9fac26f commit 29371f9

File tree

6 files changed

+61
-26
lines changed

6 files changed

+61
-26
lines changed

lib/matplotlib/backends/backend_agg.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,13 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath):
192192

193193
font = self._get_agg_font(prop)
194194
if font is None: return None
195-
font.set_text(s, angle)
195+
if len(s)==1 and ord(s)>127:
196+
197+
font.load_char(ord(s))
198+
else:
199+
font.set_text(s, angle)
196200
font.draw_glyphs_to_bitmap()
197-
201+
198202
self._renderer.draw_text(font, int(x), int(y), gc)
199203

200204

lib/matplotlib/backends/backend_template.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False):
107107
"""
108108
Render the matplotlib.text.Text instance at x, y in window
109109
coords using GraphicsContext gc
110+
111+
**backend implementers note** When you are trying to determine
112+
if you have gotten your bounding box right (which is what
113+
enables the text layout/alignment to work properly), it helps to change the line in text.py
114+
115+
if 0: bbox_artist(self, renderer)
116+
117+
to if 1, and then the actual bounding box will be blotted
118+
along with your text.
119+
110120
"""
111121
pass
112122

lib/matplotlib/font_manager.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ def __init__(self,
548548
weight = None,
549549
stretch= None,
550550
size = None,
551+
fname = None, # if this is set, it's a hardcoded filename to use
551552
):
552553

553554

@@ -567,12 +568,13 @@ def __init__(self,
567568
self.__stretch = stretch
568569
self.__size = size
569570
self.__parent_size = fontManager.get_default_size()
570-
571+
self.fname = fname
572+
571573
def __hash__(self):
572574
return hash( (
573575
tuple(self.__family), self.__style, self.__variant,
574576
self.__weight, self.__stretch, self.__size,
575-
self.__parent_size))
577+
self.__parent_size, self.fname))
576578

577579
def __str__(self):
578580
return str((self.__family, self.__style, self.__variant,
@@ -844,7 +846,11 @@ def findfont(self, prop, fontext='ttf'):
844846
Delete this file to have matplotlib rebuild the cache."""
845847

846848
debug = False
847-
849+
if prop.fname is not None:
850+
fname = prop.fname
851+
verbose.report('findfont returning %s'%fname, 'debug')
852+
return fname
853+
848854
if fontext == 'ttf':
849855
fontdict = self.ttfdict
850856
elif fontext == 'afm':

lib/matplotlib/table.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,15 @@ def draw(self, renderer):
9898
Rectangle.draw(self, renderer)
9999

100100
# position the text
101-
self._set_text_position()
101+
self._set_text_position(renderer)
102102
self._text.draw(renderer)
103103

104-
def _set_text_position(self):
104+
def _set_text_position(self, renderer):
105105
""" Set text up so it draws in the right place.
106106
107107
Currently support 'left', 'center' and 'right'
108108
"""
109-
bbox = self.get_window_extent()
109+
bbox = self.get_window_extent(renderer)
110110
l, b, w, h = bbox.get_bounds()
111111

112112
# draw in center vertically
@@ -138,6 +138,9 @@ def get_required_width(self, renderer):
138138
return w * (1.0 + (2.0 * self.PAD))
139139

140140

141+
def set_text_props(self, **kwargs):
142+
'update the text properties with kwargs'
143+
self._text.update_properties(kwargs)
141144

142145
class Table(Artist):
143146
"""
@@ -239,7 +242,7 @@ def get_child_artists(self):
239242

240243
return self._cells.values()
241244

242-
def get_window_extent(self, renderer=None):
245+
def get_window_extent(self, renderer):
243246

244247
boxes = [c.get_window_extent(renderer) for c in self._cells]
245248
return bbox_all(boxes)
@@ -396,3 +399,7 @@ def _update_positions(self, renderer):
396399
self._offset(ox, oy)
397400

398401

402+
def get_celld(self):
403+
'return a dict of cells in the table'
404+
return self._cells
405+

lib/matplotlib/text.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ def __init__(self,
5252
self._text = text
5353
self._verticalalignment = verticalalignment
5454
self._horizontalalignment = horizontalalignment
55-
self._multialignment = multialignment
56-
57-
58-
55+
self._multialignment = multialignment
5956
self._rotation = rotation
6057
self._fontproperties = fontproperties
6158

@@ -469,6 +466,7 @@ def update_properties(self, d):
469466

470467
def is_math_text(self):
471468
if not matplotlib._havemath: return False
469+
if len(self._text)<2: return False
472470
return ( self._text.startswith('$') and
473471
self._text.endswith('$') )
474472

src/ft2font.cpp

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ FT2Font::clear(const Py::Tuple & args) {
263263
image.height = 0;
264264
image.offsetx = 0;
265265
image.offsety = 0;
266-
266+
267267
text = "";
268268
angle = 0.0;
269269

@@ -277,12 +277,16 @@ FT2Font::clear(const Py::Tuple & args) {
277277
for (size_t i=0; i<gms.size(); i++) {
278278
Py_DECREF(gms[i]);
279279
}
280+
280281
glyphs.resize(0);
281282
gms.resize(0);
282283

283284
return Py::Object();
284285
}
285286

287+
288+
289+
286290
char FT2Font::set_size__doc__[] =
287291
"set_size(ptsize, dpi)\n"
288292
"\n"
@@ -516,6 +520,7 @@ FT2Font::draw_bitmap( FT_Bitmap* bitmap,
516520
if ( i >= width || j >= height )
517521
continue;
518522
image.buffer[i + j*width] |= bitmap->buffer[q*bitmap->width + p];
523+
519524
}
520525
}
521526
}
@@ -614,9 +619,10 @@ char FT2Font::draw_glyphs_to_bitmap__doc__[] =
614619
;
615620
Py::Object
616621
FT2Font::draw_glyphs_to_bitmap(const Py::Tuple & args) {
622+
617623
_VERBOSE("FT2Font::draw_glyphs_to_bitmap");
618624
args.verify_length(0);
619-
625+
620626
FT_BBox string_bbox = compute_string_bbox();
621627

622628
image.width = (string_bbox.xMax-string_bbox.xMin) / 64+2;
@@ -627,17 +633,19 @@ FT2Font::draw_glyphs_to_bitmap(const Py::Tuple & args) {
627633
image.offsety = -image.height;
628634
else
629635
image.offsety = (int)(-string_bbox.yMax/64.0);
630-
636+
637+
631638
size_t numBytes = image.width*image.height;
632639
delete [] image.buffer;
633640
image.buffer = new unsigned char [numBytes];
634641
for (size_t n=0; n<numBytes; n++)
635642
image.buffer[n] = 0;
643+
644+
636645

637646
for ( size_t n = 0; n < glyphs.size(); n++ )
638647
{
639648
FT_BBox bbox;
640-
641649
FT_Glyph_Get_CBox(glyphs[n], ft_glyph_bbox_pixels, &bbox);
642650

643651
error = FT_Glyph_To_Bitmap(&glyphs[n],
@@ -648,17 +656,20 @@ FT2Font::draw_glyphs_to_bitmap(const Py::Tuple & args) {
648656
);
649657
if (error)
650658
throw Py::RuntimeError("Could not convert glyph to bitmap");
651-
659+
652660
FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyphs[n];
653-
/* now, draw to our target surface (convert position) */
661+
// now, draw to our target surface (convert position)
654662

655663
//bitmap left and top in pixel, string bbox in subpixel
656-
657-
draw_bitmap( &bitmap->bitmap,
658-
bitmap->left-string_bbox.xMin/64,
659-
string_bbox.yMax/64-bitmap->top+1
660-
);
664+
FT_Int x = (FT_Int)(bitmap->left-string_bbox.xMin/64.);
665+
FT_Int y = (FT_Int)(string_bbox.yMax/64.-bitmap->top+1);
666+
//make sure the index is non-neg
667+
x = x<0?0:x;
668+
y = y<0?0:y;
669+
670+
draw_bitmap( &bitmap->bitmap, x, y);
661671
}
672+
662673
return Py::Object();
663674
}
664675

@@ -744,8 +755,7 @@ FT2Font::get_charmap(const Py::Tuple & args) {
744755
Py::Dict charmap;
745756

746757
FT_ULong code = FT_Get_First_Char(face, &index);
747-
while (code != 0) {
748-
//charmap[Py::Long((long) code)] = Py::Int((int) index);
758+
while (index != 0) {
749759
charmap[Py::Int((int) index)] = Py::Long((long) code);
750760
code = FT_Get_Next_Char(face, code, &index);
751761
}

0 commit comments

Comments
 (0)