| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -523,6 +523,10 @@ def get_familyname(self): | |||
| 523 | 523 | br'light|ultralight|extra|condensed))+$') | |
| 524 | 524 | return re.sub(extras, '', name) | |
| 525 | 525 | ||
| 526 | + @property | ||
| 527 | + def family_name(self): | ||
| 528 | + return self.get_familyname() | ||
| 529 | + | ||
| 526 | 530 | def get_weight(self): | |
| 527 | 531 | "Return the font weight, e.g., 'Bold' or 'Roman'" | |
| 528 | 532 | return self._header[b'Weight'] | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1240,58 +1240,115 @@ def get_underline_thickness(self, font, fontsize, dpi): | |||
| 1240 | 1240 | # The number of different sizes of chars to use, beyond which they will not | |
| 1241 | 1241 | # get any smaller | |
| 1242 | 1242 | NUM_SIZE_LEVELS = 6 | |
| 1243 | - # Percentage of x-height of additional horiz. space after sub/superscripts | ||
| 1244 | - SCRIPT_SPACE = {'cm': 0.075, | ||
| 1245 | - 'stix': 0.10, | ||
| 1246 | - 'stixsans': 0.05, | ||
| 1247 | - 'dejavuserif': 0.05, | ||
| 1248 | - 'dejavusans': 0.05} | ||
| 1249 | - ## Percentage of x-height that sub/superscripts drop below the baseline | ||
| 1250 | - SUBDROP = {'cm': 0.2, | ||
| 1251 | - 'stix': 0.4, | ||
| 1252 | - 'stixsans': 0.4, | ||
| 1253 | - 'dejavuserif': 0.4, | ||
| 1254 | - 'dejavusans': 0.4} | ||
| 1255 | - # Percentage of x-height that superscripts are raised from the baseline | ||
| 1256 | - SUP1 = {'cm': 0.45, | ||
| 1257 | - 'stix': 0.8, | ||
| 1258 | - 'stixsans': 0.8, | ||
| 1259 | - 'dejavuserif': 0.7, | ||
| 1260 | - 'dejavusans': 0.7} | ||
| 1261 | - # Percentage of x-height that subscripts drop below the baseline | ||
| 1262 | - SUB1 = {'cm': 0.2, | ||
| 1263 | - 'stix': 0.3, | ||
| 1264 | - 'stixsans': 0.3, | ||
| 1265 | - 'dejavuserif': 0.3, | ||
| 1266 | - 'dejavusans': 0.3} | ||
| 1267 | - # Percentage of x-height that subscripts drop below the baseline when a | ||
| 1268 | - # superscript is present | ||
| 1269 | - SUB2 = {'cm': 0.3, | ||
| 1270 | - 'stix': 0.6, | ||
| 1271 | - 'stixsans': 0.5, | ||
| 1272 | - 'dejavuserif': 0.5, | ||
| 1273 | - 'dejavusans': 0.5} | ||
| 1274 | - # Percentage of x-height that sub/supercripts are offset relative to the | ||
| 1275 | - # nucleus edge for non-slanted nuclei | ||
| 1276 | - DELTA = {'cm': 0.075, | ||
| 1277 | - 'stix': 0.05, | ||
| 1278 | - 'stixsans': 0.025, | ||
| 1279 | - 'dejavuserif': 0.025, | ||
| 1280 | - 'dejavusans': 0.025} | ||
| 1281 | - # Additional percentage of last character height above 2/3 of the x-height that | ||
| 1282 | - # supercripts are offset relative to the subscript for slanted nuclei | ||
| 1283 | - DELTASLANTED = {'cm': 0.3, | ||
| 1284 | - 'stix': 0.3, | ||
| 1285 | - 'stixsans': 0.6, | ||
| 1286 | - 'dejavuserif': 0.2, | ||
| 1287 | - 'dejavusans': 0.2} | ||
| 1288 | - # Percentage of x-height that supercripts and subscripts are offset for | ||
| 1289 | - # integrals | ||
| 1290 | - DELTAINTEGRAL = {'cm': 0.3, | ||
| 1291 | - 'stix': 0.3, | ||
| 1292 | - 'stixsans': 0.3, | ||
| 1293 | - 'dejavuserif': 0.1, | ||
| 1294 | - 'dejavusans': 0.1} | ||
| 1243 | + | ||
| 1244 | + | ||
| 1245 | + class FontConstantsBase(object): | ||
| 1246 | + """ | ||
| 1247 | + A set of constants that controls how certain things, such as sub- | ||
| 1248 | + and superscripts are laid out. These are all metrics that can't | ||
| 1249 | + be reliably retrieved from the font metrics in the font itself. | ||
| 1250 | + """ | ||
| 1251 | + # Percentage of x-height of additional horiz. space after sub/superscripts | ||
| 1252 | + script_space = 0.05 | ||
| 1253 | + | ||
| 1254 | + # Percentage of x-height that sub/superscripts drop below the baseline | ||
| 1255 | + subdrop = 0.4 | ||
| 1256 | + | ||
| 1257 | + # Percentage of x-height that superscripts are raised from the baseline | ||
| 1258 | + sup1 = 0.7 | ||
| 1259 | + | ||
| 1260 | + # Percentage of x-height that subscripts drop below the baseline | ||
| 1261 | + sub1 = 0.3 | ||
| 1262 | + | ||
| 1263 | + # Percentage of x-height that subscripts drop below the baseline when a | ||
| 1264 | + # superscript is present | ||
| 1265 | + sub2 = 0.5 | ||
| 1266 | + | ||
| 1267 | + # Percentage of x-height that sub/supercripts are offset relative to the | ||
| 1268 | + # nucleus edge for non-slanted nuclei | ||
| 1269 | + delta = 0.025 | ||
| 1270 | + | ||
| 1271 | + # Additional percentage of last character height above 2/3 of the | ||
| 1272 | + # x-height that supercripts are offset relative to the subscript | ||
| 1273 | + # for slanted nuclei | ||
| 1274 | + delta_slanted = 0.2 | ||
| 1275 | + | ||
| 1276 | + # Percentage of x-height that supercripts and subscripts are offset for | ||
| 1277 | + # integrals | ||
| 1278 | + delta_integral = 0.1 | ||
| 1279 | + | ||
| 1280 | + | ||
| 1281 | + class ComputerModernFontConstants(FontConstantsBase): | ||
| 1282 | + script_space = 0.075 | ||
| 1283 | + subdrop = 0.2 | ||
| 1284 | + sup1 = 0.45 | ||
| 1285 | + sub1 = 0.2 | ||
| 1286 | + sub2 = 0.3 | ||
| 1287 | + delta = 0.075 | ||
| 1288 | + delta_slanted = 0.3 | ||
| 1289 | + delta_integral = 0.3 | ||
| 1290 | + | ||
| 1291 | + | ||
| 1292 | + class STIXFontConstants(FontConstantsBase): | ||
| 1293 | + script_space = 0.1 | ||
| 1294 | + sup1 = 0.8 | ||
| 1295 | + sub2 = 0.6 | ||
| 1296 | + delta = 0.05 | ||
| 1297 | + delta_slanted = 0.3 | ||
| 1298 | + delta_integral = 0.3 | ||
| 1299 | + | ||
| 1300 | + | ||
| 1301 | + class STIXSansFontConstants(FontConstantsBase): | ||
| 1302 | + script_space = 0.05 | ||
| 1303 | + sup1 = 0.8 | ||
| 1304 | + delta_slanted = 0.6 | ||
| 1305 | + delta_integral = 0.3 | ||
| 1306 | + | ||
| 1307 | + | ||
| 1308 | + class DejaVuSerifFontConstants(FontConstantsBase): | ||
| 1309 | + pass | ||
| 1310 | + | ||
| 1311 | + | ||
| 1312 | + class DejaVuSansFontConstants(FontConstantsBase): | ||
| 1313 | + pass | ||
| 1314 | + | ||
| 1315 | + | ||
| 1316 | + # Maps font family names to the FontConstantBase subclass to use | ||
| 1317 | + _font_constant_mapping = { | ||
| 1318 | + 'DejaVu Sans': DejaVuSansFontConstants, | ||
| 1319 | + 'DejaVu Sans Mono': DejaVuSansFontConstants, | ||
| 1320 | + 'DejaVu Serif': DejaVuSerifFontConstants, | ||
| 1321 | + 'cmb10': ComputerModernFontConstants, | ||
| 1322 | + 'cmex10': ComputerModernFontConstants, | ||
| 1323 | + 'cmmi10': ComputerModernFontConstants, | ||
| 1324 | + 'cmr10': ComputerModernFontConstants, | ||
| 1325 | + 'cmss10': ComputerModernFontConstants, | ||
| 1326 | + 'cmsy10': ComputerModernFontConstants, | ||
| 1327 | + 'cmtt10': ComputerModernFontConstants, | ||
| 1328 | + 'STIXGeneral': STIXFontConstants, | ||
| 1329 | + 'STIXNonUnicode': STIXFontConstants, | ||
| 1330 | + 'STIXSizeFiveSym': STIXFontConstants, | ||
| 1331 | + 'STIXSizeFourSym': STIXFontConstants, | ||
| 1332 | + 'STIXSizeThreeSym': STIXFontConstants, | ||
| 1333 | + 'STIXSizeTwoSym': STIXFontConstants, | ||
| 1334 | + 'STIXSizeOneSym': STIXFontConstants, | ||
| 1335 | + # Map the fonts we used to ship, just for good measure | ||
| 1336 | + 'Bitstream Vera Sans': DejaVuSansFontConstants, | ||
| 1337 | + 'Bitstream Vera': DejaVuSansFontConstants, | ||
| 1338 | + } | ||
| 1339 | + | ||
| 1340 | + | ||
| 1341 | + def _get_font_constant_set(state): | ||
| 1342 | + constants = _font_constant_mapping.get( | ||
| 1343 | + state.font_output._get_font(state.font).family_name, | ||
| 1344 | + FontConstantsBase) | ||
| 1345 | + # STIX sans isn't really its own fonts, just different code points | ||
| 1346 | + # in the STIX fonts, so we have to detect this one separately. | ||
| 1347 | + if (constants is STIXFontConstants and | ||
| 1348 | + isinstance(state.font_output, StixSansFonts)): | ||
| 1349 | + return STIXSansFontConstants | ||
| 1350 | + return constants | ||
| 1351 | + | ||
| 1295 | 1352 | ||
| 1296 | 1353 | class MathTextWarning(Warning): | |
| 1297 | 1354 | pass | |
@@ -2873,25 +2930,24 @@ def subsuper(self, s, loc, toks): | |||
| 2873 | 2930 | nucleus = Hlist([nucleus]) | |
| 2874 | 2931 | ||
| 2875 | 2932 | # Handle regular sub/superscripts | |
| 2876 | - | ||
| 2877 | - fs = rcParams['mathtext.fontset'] | ||
| 2878 | - if fs == 'custom': | ||
| 2879 | - fs = 'dejavusans' | ||
| 2880 | - | ||
| 2933 | + constants = _get_font_constant_set(state) | ||
| 2881 | 2934 | lc_height = last_char.height | |
| 2882 | 2935 | lc_baseline = 0 | |
| 2883 | 2936 | if self.is_dropsub(last_char): | |
| 2884 | 2937 | lc_baseline = last_char.depth | |
| 2885 | 2938 | ||
| 2886 | 2939 | # Compute kerning for sub and super | |
| 2887 | - superkern = DELTA[fs] * xHeight | ||
| 2888 | - subkern = DELTA[fs] * xHeight | ||
| 2940 | + superkern = constants.delta * xHeight | ||
| 2941 | + subkern = constants.delta * xHeight | ||
| 2889 | 2942 | if self.is_slanted(last_char): | |
| 2890 | - superkern += DELTA[fs] * xHeight | ||
| 2891 | - superkern += DELTASLANTED[fs] * (lc_height - xHeight * 2. / 3.) | ||
| 2943 | + superkern += constants.delta * xHeight | ||
| 2944 | + superkern += (constants.delta_slanted * | ||
| 2945 | + (lc_height - xHeight * 2. / 3.)) | ||
| 2892 | 2946 | if self.is_dropsub(last_char): | |
| 2893 | - subkern = (3 * DELTA[fs] - DELTAINTEGRAL[fs]) * lc_height | ||
| 2894 | - superkern = (3 * DELTA[fs] + DELTAINTEGRAL[fs]) * lc_height | ||
| 2947 | + subkern = (3 * constants.delta - | ||
| 2948 | + constants.delta_integral) * lc_height | ||
| 2949 | + superkern = (3 * constants.delta + | ||
| 2950 | + constants.delta_integral) * lc_height | ||
| 2895 | 2951 | else: | |
| 2896 | 2952 | subkern = 0 | |
| 2897 | 2953 | ||
@@ -2900,26 +2956,26 @@ def subsuper(self, s, loc, toks): | |||
| 2900 | 2956 | x = Hlist([Kern(subkern), sub]) | |
| 2901 | 2957 | x.shrink() | |
| 2902 | 2958 | if self.is_dropsub(last_char): | |
| 2903 | - shift_down = lc_baseline + SUBDROP[fs] * xHeight | ||
| 2959 | + shift_down = lc_baseline + constants.subdrop * xHeight | ||
| 2904 | 2960 | else: | |
| 2905 | - shift_down = SUB1[fs] * xHeight | ||
| 2961 | + shift_down = constants.sub1 * xHeight | ||
| 2906 | 2962 | x.shift_amount = shift_down | |
| 2907 | 2963 | else: | |
| 2908 | 2964 | x = Hlist([Kern(superkern), super]) | |
| 2909 | 2965 | x.shrink() | |
| 2910 | 2966 | if self.is_dropsub(last_char): | |
| 2911 | - shift_up = lc_height - SUBDROP[fs] * xHeight | ||
| 2967 | + shift_up = lc_height - constants.subdrop * xHeight | ||
| 2912 | 2968 | else: | |
| 2913 | - shift_up = SUP1[fs] * xHeight | ||
| 2969 | + shift_up = constants.sup1 * xHeight | ||
| 2914 | 2970 | if sub is None: | |
| 2915 | 2971 | x.shift_amount = -shift_up | |
| 2916 | 2972 | else: # Both sub and superscript | |
| 2917 | 2973 | y = Hlist([Kern(subkern),sub]) | |
| 2918 | 2974 | y.shrink() | |
| 2919 | 2975 | if self.is_dropsub(last_char): | |
| 2920 | - shift_down = lc_baseline + SUBDROP[fs] * xHeight | ||
| 2976 | + shift_down = lc_baseline + constants.subdrop * xHeight | ||
| 2921 | 2977 | else: | |
| 2922 | - shift_down = SUB2[fs] * xHeight | ||
| 2978 | + shift_down = constants.sub2 * xHeight | ||
| 2923 | 2979 | # If sub and superscript collide, move super up | |
| 2924 | 2980 | clr = (2.0 * rule_thickness - | |
| 2925 | 2981 | ((shift_up - x.depth) - (y.height - shift_down))) | |
@@ -2931,8 +2987,9 @@ def subsuper(self, s, loc, toks): | |||
| 2931 | 2987 | x.shift_amount = shift_down | |
| 2932 | 2988 | ||
| 2933 | 2989 | if not self.is_dropsub(last_char): | |
| 2934 | - x.width += SCRIPT_SPACE[fs] * xHeight | ||
| 2990 | + x.width += constants.script_space * xHeight | ||
| 2935 | 2991 | result = Hlist([nucleus, x]) | |
| 2992 | + | ||
| 2936 | 2993 | return [result] | |
| 2937 | 2994 | ||
| 2938 | 2995 | def _genfrac(self, ldelim, rdelim, rule, style, num, den): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -76,11 +76,11 @@ font.stretch : normal | |||
| 76 | 76 | # relative to font.size, using the following values: xx-small, x-small, | |
| 77 | 77 | # small, medium, large, x-large, xx-large, larger, or smaller | |
| 78 | 78 | font.size : 12.0 | |
| 79 | - font.serif : Bitstream Vera Serif, DejaVu Serif, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif | ||
| 80 | - font.sans-serif: Bitstream Vera Sans, DejaVu Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif | ||
| 79 | + font.serif : DejaVu Serif, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif | ||
| 80 | + font.sans-serif: DejaVu Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif | ||
| 81 | 81 | font.cursive : Apple Chancery, Textile, Zapf Chancery, Sand, Script MT, Felipa, cursive | |
| 82 | 82 | font.fantasy : Comic Sans MS, Chicago, Charcoal, ImpactWestern, Humor Sans, fantasy | |
| 83 | - font.monospace : Bitstream Vera Sans Mono, DejaVu Sans Mono, Andale Mono, Nimbus Mono L, Courier New, Courier, Fixed, Terminal, monospace | ||
| 83 | + font.monospace : DejaVu Sans Mono, Andale Mono, Nimbus Mono L, Courier New, Courier, Fixed, Terminal, monospace | ||
| 84 | 84 | ||
| 85 | 85 | ### TEXT | |
| 86 | 86 | # text properties used by text.Text. See | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,6 +22,7 @@ | |||
| 22 | 22 | from matplotlib import ticker | |
| 23 | 23 | from matplotlib import pyplot as plt | |
| 24 | 24 | from matplotlib import ft2font | |
| 25 | + from matplotlib import rcParams | ||
| 25 | 26 | from matplotlib.testing.noseclasses import KnownFailureTest, \ | |
| 26 | 27 | KnownFailureDidNotFailTest, ImageComparisonFailure | |
| 27 | 28 | from matplotlib.testing.compare import comparable_formats, compare_images, \ | |
@@ -109,18 +110,27 @@ def tearDownClass(cls): | |||
| 109 | 110 | cls.original_settings) | |
| 110 | 111 | ||
| 111 | 112 | ||
| 112 | - def cleanup(func): | ||
| 113 | - @functools.wraps(func) | ||
| 114 | - def wrapped_function(*args, **kwargs): | ||
| 115 | - original_units_registry = matplotlib.units.registry.copy() | ||
| 116 | - original_settings = mpl.rcParams.copy() | ||
| 117 | - try: | ||
| 118 | - func(*args, **kwargs) | ||
| 119 | - finally: | ||
| 120 | - _do_cleanup(original_units_registry, | ||
| 121 | - original_settings) | ||
| 113 | + def cleanup(style=None): | ||
| 114 | + def make_cleanup(func): | ||
| 115 | + @functools.wraps(func) | ||
| 116 | + def wrapped_function(*args, **kwargs): | ||
| 117 | + original_units_registry = matplotlib.units.registry.copy() | ||
| 118 | + original_settings = mpl.rcParams.copy() | ||
| 119 | + matplotlib.style.use(style) | ||
| 120 | + try: | ||
| 121 | + func(*args, **kwargs) | ||
| 122 | + finally: | ||
| 123 | + _do_cleanup(original_units_registry, | ||
| 124 | + original_settings) | ||
| 122 | 125 | ||
| 123 | - return wrapped_function | ||
| 126 | + return wrapped_function | ||
| 127 | + | ||
| 128 | + if isinstance(style, six.string_types): | ||
| 129 | + return make_cleanup | ||
| 130 | + else: | ||
| 131 | + result = make_cleanup(style) | ||
| 132 | + style = 'classic' | ||
| 133 | + return result | ||
| 124 | 134 | ||
| 125 | 135 | ||
| 126 | 136 | def check_freetype_version(ver): | |
@@ -138,6 +148,7 @@ def check_freetype_version(ver): | |||
| 138 | 148 | class ImageComparisonTest(CleanupTest): | |
| 139 | 149 | @classmethod | |
| 140 | 150 | def setup_class(cls): | |
| 151 | + CleanupTest.setup_class() | ||
| 141 | 152 | cls._initial_settings = mpl.rcParams.copy() | |
| 142 | 153 | try: | |
| 143 | 154 | matplotlib.style.use(cls._style) | |
@@ -146,11 +157,8 @@ def setup_class(cls): | |||
| 146 | 157 | mpl.rcParams.clear() | |
| 147 | 158 | mpl.rcParams.update(cls._initial_settings) | |
| 148 | 159 | raise | |
| 149 | - # Because the setup of a CleanupTest might involve | ||
| 150 | - # modifying a few rcparams, this setup should come | ||
| 151 | - # last prior to running the image test. | ||
| 152 | - CleanupTest.setup_class() | ||
| 153 | 160 | cls.original_settings = cls._initial_settings | |
| 161 | + matplotlib.tests.set_font_settings_for_testing() | ||
| 154 | 162 | cls._func() | |
| 155 | 163 | ||
| 156 | 164 | @classmethod | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,6 +22,12 @@ | |||
| 22 | 22 | 'test data.') | |
| 23 | 23 | ||
| 24 | 24 | ||
| 25 | + def set_font_settings_for_testing(): | ||
| 26 | + rcParams['font.family'] = 'DejaVu Sans' | ||
| 27 | + rcParams['text.hinting'] = False | ||
| 28 | + rcParams['text.hinting_factor'] = 8 | ||
| 29 | + | ||
| 30 | + | ||
| 25 | 31 | def setup(): | |
| 26 | 32 | # The baseline images are created in this locale, so we should use | |
| 27 | 33 | # it during all of the tests. | |
@@ -45,9 +51,8 @@ def setup(): | |||
| 45 | 51 | # tests and are not necessarily the default values as specified in | |
| 46 | 52 | # rcsetup.py | |
| 47 | 53 | rcdefaults() # Start with all defaults | |
| 48 | - rcParams['font.family'] = 'DejaVu Sans' | ||
| 49 | - rcParams['text.hinting'] = False | ||
| 50 | - rcParams['text.hinting_factor'] = 8 | ||
| 54 | + | ||
| 55 | + set_font_settings_for_testing() | ||
| 51 | 56 | ||
| 52 | 57 | ||
| 53 | 58 | def assert_str_equal(reference_str, test_str, | |
| Back | FazBrowse Home | New Git URL |
0 commit comments