| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent c508c35 commit 13f925e
21 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,12 +5,12 @@ | |||
| 5 | 5 | import matplotlib | |
| 6 | 6 | import matplotlib.pyplot | |
| 7 | 7 | import matplotlib._cntr | |
| 8 | + import matplotlib._ft2 | ||
| 8 | 9 | import matplotlib._image | |
| 9 | 10 | import matplotlib._path | |
| 10 | 11 | import matplotlib._png | |
| 11 | 12 | import matplotlib._tri | |
| 12 | 13 | import matplotlib.backends._backend_agg | |
| 13 | - import matplotlib.ft2font | ||
| 14 | 14 | import matplotlib.ttconv | |
| 15 | 15 | if platform.system() not in ['Windows']: | |
| 16 | 16 | import matplotlib.backends._tkagg | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,13 +9,12 @@ | |||
| 9 | 9 | """ | |
| 10 | 10 | from __future__ import print_function | |
| 11 | 11 | import matplotlib | |
| 12 | - from matplotlib.ft2font import FT2Font, KERNING_DEFAULT, KERNING_UNFITTED, KERNING_UNSCALED | ||
| 12 | + from matplotlib import _ft2 | ||
| 13 | 13 | ||
| 14 | 14 | ||
| 15 | - #fname = '/usr/share/fonts/sfd/FreeSans.ttf' | ||
| 16 | 15 | fname = matplotlib.get_data_path() + '/fonts/ttf/DejaVuSans.ttf' | |
| 17 | - font = FT2Font(fname) | ||
| 18 | - font.set_charmap(0) | ||
| 16 | + font = _ft2.Face(fname) | ||
| 17 | + font.set_charmap(0) # FIXME | ||
| 19 | 18 | ||
| 20 | 19 | codes = font.get_charmap().items() | |
| 21 | 20 | #dsu = [(ccode, glyphind) for ccode, glyphind in codes] | |
@@ -34,11 +33,8 @@ | |||
| 34 | 33 | coded[name] = ccode | |
| 35 | 34 | glyphd[name] = glyphind | |
| 36 | 35 | ||
| 37 | - code = coded['A'] | ||
| 38 | - glyph = font.load_char(code) | ||
| 39 | - #print(glyph.bbox) | ||
| 40 | 36 | print(glyphd['A'], glyphd['V'], coded['A'], coded['V']) | |
| 41 | - print('AV', font.get_kerning(glyphd['A'], glyphd['V'], KERNING_DEFAULT)) | ||
| 42 | - print('AV', font.get_kerning(glyphd['A'], glyphd['V'], KERNING_UNFITTED)) | ||
| 43 | - print('AV', font.get_kerning(glyphd['A'], glyphd['V'], KERNING_UNSCALED)) | ||
| 44 | - print('AV', font.get_kerning(glyphd['A'], glyphd['T'], KERNING_UNSCALED)) | ||
| 37 | + print('AV', font.get_kerning(glyphd['A'], glyphd['V'], _ft2.Kerning.DEFAULT)) | ||
| 38 | + print('AV', font.get_kerning(glyphd['A'], glyphd['V'], _ft2.Kerning.UNFITTED)) | ||
| 39 | + print('AV', font.get_kerning(glyphd['A'], glyphd['V'], _ft2.Kerning.UNSCALED)) | ||
| 40 | + print('AV', font.get_kerning(glyphd['A'], glyphd['T'], _ft2.Kerning.UNSCALED)) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,68 +1,54 @@ | |||
| 1 | 1 | """ | |
| 2 | - ============ | ||
| 3 | - Ftface Props | ||
| 4 | - ============ | ||
| 5 | - | ||
| 6 | - This is a demo script to show you how to use all the properties of an | ||
| 7 | - FT2Font object. These describe global font properties. For | ||
| 8 | - individual character metrics, use the Glyph object, as returned by | ||
| 9 | - load_char | ||
| 2 | + =============== | ||
| 3 | + Face properties | ||
| 4 | + =============== | ||
| 5 | + | ||
| 6 | + This is a demo script to show you how to use all the properties of a Face | ||
| 7 | + object. These describe global font properties. For individual character | ||
| 8 | + metrics, use the Glyph object, as loaded from the glyph attribute after calling | ||
| 9 | + load_char. | ||
| 10 | 10 | """ | |
| 11 | 11 | from __future__ import print_function | |
| 12 | 12 | import matplotlib | |
| 13 | - import matplotlib.ft2font as ft | ||
| 14 | - | ||
| 15 | - | ||
| 16 | - #fname = '/usr/local/share/matplotlib/VeraIt.ttf' | ||
| 17 | - fname = matplotlib.get_data_path() + '/fonts/ttf/DejaVuSans-Oblique.ttf' | ||
| 18 | - #fname = '/usr/local/share/matplotlib/cmr10.ttf' | ||
| 19 | - | ||
| 20 | - font = ft.FT2Font(fname) | ||
| 21 | - | ||
| 22 | - print('Num faces :', font.num_faces) # number of faces in file | ||
| 23 | - print('Num glyphs :', font.num_glyphs) # number of glyphs in the face | ||
| 24 | - print('Family name :', font.family_name) # face family name | ||
| 25 | - print('Style name :', font.style_name) # face style name | ||
| 26 | - print('PS name :', font.postscript_name) # the postscript name | ||
| 27 | - print('Num fixed :', font.num_fixed_sizes) # number of embedded bitmap in face | ||
| 28 | - | ||
| 29 | - # the following are only available if face.scalable | ||
| 30 | - if font.scalable: | ||
| 31 | - # the face global bounding box (xmin, ymin, xmax, ymax) | ||
| 32 | - print('Bbox :', font.bbox) | ||
| 33 | - # number of font units covered by the EM | ||
| 34 | - print('EM :', font.units_per_EM) | ||
| 35 | - # the ascender in 26.6 units | ||
| 36 | - print('Ascender :', font.ascender) | ||
| 37 | - # the descender in 26.6 units | ||
| 38 | - print('Descender :', font.descender) | ||
| 39 | - # the height in 26.6 units | ||
| 40 | - print('Height :', font.height) | ||
| 41 | - # maximum horizontal cursor advance | ||
| 42 | - print('Max adv width :', font.max_advance_width) | ||
| 43 | - # same for vertical layout | ||
| 44 | - print('Max adv height :', font.max_advance_height) | ||
| 45 | - # vertical position of the underline bar | ||
| 46 | - print('Underline pos :', font.underline_position) | ||
| 47 | - # vertical thickness of the underline | ||
| 48 | - print('Underline thickness :', font.underline_thickness) | ||
| 49 | - | ||
| 50 | - for style in ('Italic', | ||
| 51 | - 'Bold', | ||
| 52 | - 'Scalable', | ||
| 53 | - 'Fixed sizes', | ||
| 54 | - 'Fixed width', | ||
| 55 | - 'SFNT', | ||
| 56 | - 'Horizontal', | ||
| 57 | - 'Vertical', | ||
| 58 | - 'Kerning', | ||
| 59 | - 'Fast glyphs', | ||
| 60 | - 'Multiple masters', | ||
| 61 | - 'Glyph names', | ||
| 62 | - 'External stream'): | ||
| 63 | - bitpos = getattr(ft, style.replace(' ', '_').upper()) - 1 | ||
| 64 | - print('%-17s:' % style, bool(font.style_flags & (1 << bitpos))) | ||
| 65 | - | ||
| 66 | - print(dir(font)) | ||
| 67 | - | ||
| 68 | - print(font.get_kerning) | ||
| 13 | + from matplotlib import font_manager, _ft2 | ||
| 14 | + | ||
| 15 | + | ||
| 16 | + fname = matplotlib.get_data_path() + "/fonts/ttf/DejaVuSans-Oblique.ttf" | ||
| 17 | + font = font_manager.get_font(fname) | ||
| 18 | + | ||
| 19 | + print("Faces in file :", font.num_faces) | ||
| 20 | + print("Glyphs in face :", font.num_glyphs) | ||
| 21 | + print("Family name :", font.family_name) | ||
| 22 | + print("Style name :", font.style_name) | ||
| 23 | + print("Postscript name :", font.get_postscript_name()) | ||
| 24 | + print("Embedded bitmap strikes:", font.num_fixed_sizes) | ||
| 25 | + | ||
| 26 | + if font.face_flags & _ft2.FACE_FLAG_SCALABLE: | ||
| 27 | + print('Global bbox (xmin, ymin, xmax, ymax):', font.bbox) | ||
| 28 | + print('Font units per EM :', font.units_per_EM) | ||
| 29 | + print('Ascender (pixels) :', font.ascender) | ||
| 30 | + print('Descender (pixels) :', font.descender) | ||
| 31 | + print('Height (pixels) :', font.height) | ||
| 32 | + print('Max horizontal advance :', font.max_advance_width) | ||
| 33 | + print('Max vertical advance :', font.max_advance_height) | ||
| 34 | + print('Underline position :', font.underline_position) | ||
| 35 | + print('Underline thickness :', font.underline_thickness) | ||
| 36 | + | ||
| 37 | + for style in ['Style flag italic', | ||
| 38 | + 'Style flag bold']: | ||
| 39 | + flag = getattr(_ft2, style.replace(' ', '_').upper()) - 1 | ||
| 40 | + print('%-26s:' % style, bool(font.style_flags & flag)) | ||
| 41 | + | ||
| 42 | + for style in ['Face flag scalable', | ||
| 43 | + 'Face flag fixed sizes', | ||
| 44 | + 'Face flag fixed width', | ||
| 45 | + 'Face flag SFNT', | ||
| 46 | + 'Face flag horizontal', | ||
| 47 | + 'Face flag vertical', | ||
| 48 | + 'Face flag kerning', | ||
| 49 | + 'Face flag fast glyphs', | ||
| 50 | + 'Face flag multiple masters', | ||
| 51 | + 'Face flag glyph names', | ||
| 52 | + 'Face flag external stream']: | ||
| 53 | + flag = getattr(_ft2, style.replace(' ', '_').upper()) | ||
| 54 | + print('%-26s:' % style, bool(font.face_flags & flag)) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,7 +15,7 @@ | |||
| 15 | 15 | import os | |
| 16 | 16 | ||
| 17 | 17 | import matplotlib | |
| 18 | - from matplotlib.ft2font import FT2Font | ||
| 18 | + from matplotlib import _ft2 | ||
| 19 | 19 | from matplotlib.font_manager import FontProperties | |
| 20 | 20 | import matplotlib.pyplot as plt | |
| 21 | 21 | ||
@@ -35,8 +35,8 @@ | |||
| 35 | 35 | fontname = os.path.join(matplotlib.get_data_path(), | |
| 36 | 36 | 'fonts', 'ttf', 'DejaVuSans.ttf') | |
| 37 | 37 | ||
| 38 | - font = FT2Font(fontname) | ||
| 39 | - codes = sorted(font.get_charmap().items()) | ||
| 38 | + font = _ft2.Face(fontname) | ||
| 39 | + codes = sorted(font.get_charmap().items()) # FIXME | ||
| 40 | 40 | ||
| 41 | 41 | # a 16,16 array of character strings | |
| 42 | 42 | chars = [['' for c in range(16)] for r in range(16)] | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,7 +12,7 @@ | |||
| 12 | 12 | * alpha blending | |
| 13 | 13 | * DPI scaling properly - everything scales properly (dashes, linewidths, etc) | |
| 14 | 14 | * draw polygon | |
| 15 | - * freetype2 w/ ft2font | ||
| 15 | + * freetype2 | ||
| 16 | 16 | ||
| 17 | 17 | TODO: | |
| 18 | 18 | ||
@@ -28,20 +28,16 @@ | |||
| 28 | 28 | import numpy as np | |
| 29 | 29 | from collections import OrderedDict | |
| 30 | 30 | from math import radians, cos, sin | |
| 31 | - from matplotlib import rcParams, __version__ | ||
| 31 | + | ||
| 32 | + from matplotlib import ( | ||
| 33 | + _ft2, _png, colors as mcolors, font_manager, rcParams, __version__) | ||
| 32 | 34 | from matplotlib.backend_bases import ( | |
| 33 | 35 | _Backend, FigureCanvasBase, FigureManagerBase, RendererBase, cursors) | |
| 36 | + from matplotlib.backends._backend_agg import RendererAgg as _RendererAgg | ||
| 34 | 37 | from matplotlib.figure import Figure | |
| 35 | - from matplotlib.font_manager import findfont, get_font | ||
| 36 | - from matplotlib.ft2font import (LOAD_FORCE_AUTOHINT, LOAD_NO_HINTING, | ||
| 37 | - LOAD_DEFAULT, LOAD_NO_AUTOHINT) | ||
| 38 | 38 | from matplotlib.mathtext import MathTextParser | |
| 39 | 39 | from matplotlib.path import Path | |
| 40 | 40 | from matplotlib.transforms import Bbox, BboxBase | |
| 41 | - from matplotlib import colors as mcolors | ||
| 42 | - | ||
| 43 | - from matplotlib.backends._backend_agg import RendererAgg as _RendererAgg | ||
| 44 | - from matplotlib import _png | ||
| 45 | 41 | ||
| 46 | 42 | try: | |
| 47 | 43 | from PIL import Image | |
@@ -53,12 +49,12 @@ | |||
| 53 | 49 | ||
| 54 | 50 | def get_hinting_flag(): | |
| 55 | 51 | mapping = { | |
| 56 | - True: LOAD_FORCE_AUTOHINT, | ||
| 57 | - False: LOAD_NO_HINTING, | ||
| 58 | - 'either': LOAD_DEFAULT, | ||
| 59 | - 'native': LOAD_NO_AUTOHINT, | ||
| 60 | - 'auto': LOAD_FORCE_AUTOHINT, | ||
| 61 | - 'none': LOAD_NO_HINTING | ||
| 52 | + True: _ft2.LOAD_FORCE_AUTOHINT, | ||
| 53 | + False: _ft2.LOAD_NO_HINTING, | ||
| 54 | + 'either': _ft2.LOAD_DEFAULT, | ||
| 55 | + 'native': _ft2.LOAD_NO_AUTOHINT, | ||
| 56 | + 'auto': _ft2.LOAD_FORCE_AUTOHINT, | ||
| 57 | + 'none': _ft2.LOAD_NO_HINTING | ||
| 62 | 58 | } | |
| 63 | 59 | return mapping[rcParams['text.hinting']] | |
| 64 | 60 | ||
@@ -106,9 +102,9 @@ def __setstate__(self, state): | |||
| 106 | 102 | ||
| 107 | 103 | def _get_hinting_flag(self): | |
| 108 | 104 | if rcParams['text.hinting']: | |
| 109 | - return LOAD_FORCE_AUTOHINT | ||
| 105 | + return _ft2.LOAD_FORCE_AUTOHINT | ||
| 110 | 106 | else: | |
| 111 | - return LOAD_NO_HINTING | ||
| 107 | + return _ft2.LOAD_NO_HINTING | ||
| 112 | 108 | ||
| 113 | 109 | # for filtering to work with rasterization, methods needs to be wrapped. | |
| 114 | 110 | # maybe there is better way to do it. | |
@@ -177,7 +173,7 @@ def draw_mathtext(self, gc, x, y, s, prop, angle): | |||
| 177 | 173 | yd = descent * cos(radians(angle)) | |
| 178 | 174 | x = np.round(x + ox + xd) | |
| 179 | 175 | y = np.round(y - oy + yd) | |
| 180 | - self._renderer.draw_text_image(font_image, x, y + 1, angle, gc) | ||
| 176 | + self._renderer.draw_text_image(font_image, x, y, angle, gc) | ||
| 181 | 177 | ||
| 182 | 178 | def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): | |
| 183 | 179 | """ | |
@@ -191,24 +187,15 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): | |||
| 191 | 187 | ||
| 192 | 188 | if font is None: | |
| 193 | 189 | return None | |
| 194 | - if len(s) == 1 and ord(s) > 127: | ||
| 195 | - font.load_char(ord(s), flags=flags) | ||
| 196 | - else: | ||
| 197 | - # We pass '0' for angle here, since it will be rotated (in raster | ||
| 198 | - # space) in the following call to draw_text_image). | ||
| 199 | - font.set_text(s, 0, flags=flags) | ||
| 200 | - font.draw_glyphs_to_bitmap(antialiased=rcParams['text.antialiased']) | ||
| 201 | - d = font.get_descent() / 64.0 | ||
| 190 | + layout = _ft2.Layout.simple(s, font, flags) | ||
| 191 | + d = -np.floor(layout.yMin) | ||
| 202 | 192 | # The descent needs to be adjusted for the angle | |
| 203 | - xo, yo = font.get_bitmap_offset() | ||
| 204 | - xo /= 64.0 | ||
| 205 | - yo /= 64.0 | ||
| 206 | 193 | xd = -d * sin(radians(angle)) | |
| 207 | 194 | yd = d * cos(radians(angle)) | |
| 208 | 195 | ||
| 209 | - #print x, y, int(x), int(y), s | ||
| 210 | 196 | self._renderer.draw_text_image( | |
| 211 | - font, np.round(x - xd + xo), np.round(y + yd + yo) + 1, angle, gc) | ||
| 197 | + layout.render(), # FIXME Antialiasing. | ||
| 198 | + np.round(x - xd), np.round(y + yd), angle, gc) | ||
| 212 | 199 | ||
| 213 | 200 | def get_text_width_height_descent(self, s, prop, ismath): | |
| 214 | 201 | """ | |
@@ -232,13 +219,10 @@ def get_text_width_height_descent(self, s, prop, ismath): | |||
| 232 | 219 | ||
| 233 | 220 | flags = get_hinting_flag() | |
| 234 | 221 | font = self._get_agg_font(prop) | |
| 235 | - font.set_text(s, 0.0, flags=flags) # the width and height of unrotated string | ||
| 236 | - w, h = font.get_width_height() | ||
| 237 | - d = font.get_descent() | ||
| 238 | - w /= 64.0 # convert from subpixels | ||
| 239 | - h /= 64.0 | ||
| 240 | - d /= 64.0 | ||
| 241 | - return w, h, d | ||
| 222 | + layout = _ft2.Layout.simple(s, font, flags) | ||
| 223 | + return (layout.xMax - layout.xMin, | ||
| 224 | + layout.yMax - layout.yMin, | ||
| 225 | + -layout.yMin) | ||
| 242 | 226 | ||
| 243 | 227 | def draw_tex(self, gc, x, y, s, prop, angle, ismath='TeX!', mtext=None): | |
| 244 | 228 | # todo, handle props, angle, origins | |
@@ -265,13 +249,10 @@ def _get_agg_font(self, prop): | |||
| 265 | 249 | """ | |
| 266 | 250 | Get the font for text instance t, cacheing for efficiency | |
| 267 | 251 | """ | |
| 268 | - fname = findfont(prop) | ||
| 269 | - font = get_font(fname) | ||
| 270 | - | ||
| 271 | - font.clear() | ||
| 252 | + fname = font_manager.findfont(prop) | ||
| 253 | + font = font_manager.get_font(fname) | ||
| 272 | 254 | size = prop.get_size_in_points() | |
| 273 | - font.set_size(size, self.dpi) | ||
| 274 | - | ||
| 255 | + font.set_char_size(size, self.dpi) | ||
| 275 | 256 | return font | |
| 276 | 257 | ||
| 277 | 258 | def points_to_pixels(self, points): | |
| Back | FazBrowse Home | New Git URL |
0 commit comments