| 1 |
""" |
| 2 |
A custom graphic renderer for the '.plain' files produced by dot. |
| 3 |
|
| 4 |
""" |
| 5 |
|
| 6 |
from __future__ import absolute_import, print_function |
| 7 |
import re, os, math |
| 8 |
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide" |
| 9 |
import pygame |
| 10 |
from pygame.locals import * |
| 11 |
|
| 12 |
from dotviewer.strunicode import forcestr, forceunicode |
| 13 |
|
| 14 |
|
| 15 |
this_dir = os.path.dirname(os.path.abspath(__file__)) |
| 16 |
FONT = os.path.join(this_dir, 'font', 'FiraMath-Regular.otf') |
| 17 |
FIXEDFONT = os.path.join(this_dir, 'font', 'DroidSansMono.ttf') |
| 18 |
COLOR = { |
| 19 |
'aliceblue': (240, 248, 255), |
| 20 |
'antiquewhite': (250, 235, 215), |
| 21 |
'antiquewhite1': (255, 239, 219), |
| 22 |
'antiquewhite2': (238, 223, 204), |
| 23 |
'antiquewhite3': (205, 192, 176), |
| 24 |
'antiquewhite4': (139, 131, 120), |
| 25 |
'aquamarine': (127, 255, 212), |
| 26 |
'aquamarine1': (127, 255, 212), |
| 27 |
'aquamarine2': (118, 238, 198), |
| 28 |
'aquamarine3': (102, 205, 170), |
| 29 |
'aquamarine4': (69, 139, 116), |
| 30 |
'azure': (240, 255, 255), |
| 31 |
'azure1': (240, 255, 255), |
| 32 |
'azure2': (224, 238, 238), |
| 33 |
'azure3': (193, 205, 205), |
| 34 |
'azure4': (131, 139, 139), |
| 35 |
'beige': (245, 245, 220), |
| 36 |
'bisque': (255, 228, 196), |
| 37 |
'bisque1': (255, 228, 196), |
| 38 |
'bisque2': (238, 213, 183), |
| 39 |
'bisque3': (205, 183, 158), |
| 40 |
'bisque4': (139, 125, 107), |
| 41 |
'black': (0, 0, 0), |
| 42 |
'blanchedalmond': (255, 235, 205), |
| 43 |
'blue': (0, 0, 255), |
| 44 |
'blue1': (0, 0, 255), |
| 45 |
'blue2': (0, 0, 238), |
| 46 |
'blue3': (0, 0, 205), |
| 47 |
'blue4': (0, 0, 139), |
| 48 |
'blueviolet': (138, 43, 226), |
| 49 |
'brown': (165, 42, 42), |
| 50 |
'brown1': (255, 64, 64), |
| 51 |
'brown2': (238, 59, 59), |
| 52 |
'brown3': (205, 51, 51), |
| 53 |
'brown4': (139, 35, 35), |
| 54 |
'burlywood': (222, 184, 135), |
| 55 |
'burlywood1': (255, 211, 155), |
| 56 |
'burlywood2': (238, 197, 145), |
| 57 |
'burlywood3': (205, 170, 125), |
| 58 |
'burlywood4': (139, 115, 85), |
| 59 |
'cadetblue': (95, 158, 160), |
| 60 |
'cadetblue1': (152, 245, 255), |
| 61 |
'cadetblue2': (142, 229, 238), |
| 62 |
'cadetblue3': (122, 197, 205), |
| 63 |
'cadetblue4': (83, 134, 139), |
| 64 |
'chartreuse': (127, 255, 0), |
| 65 |
'chartreuse1': (127, 255, 0), |
| 66 |
'chartreuse2': (118, 238, 0), |
| 67 |
'chartreuse3': (102, 205, 0), |
| 68 |
'chartreuse4': (69, 139, 0), |
| 69 |
'chocolate': (210, 105, 30), |
| 70 |
'chocolate1': (255, 127, 36), |
| 71 |
'chocolate2': (238, 118, 33), |
| 72 |
'chocolate3': (205, 102, 29), |
| 73 |
'chocolate4': (139, 69, 19), |
| 74 |
'coral': (255, 127, 80), |
| 75 |
'coral1': (255, 114, 86), |
| 76 |
'coral2': (238, 106, 80), |
| 77 |
'coral3': (205, 91, 69), |
| 78 |
'coral4': (139, 62, 47), |
| 79 |
'cornflowerblue': (100, 149, 237), |
| 80 |
'cornsilk': (255, 248, 220), |
| 81 |
'cornsilk1': (255, 248, 220), |
| 82 |
'cornsilk2': (238, 232, 205), |
| 83 |
'cornsilk3': (205, 200, 177), |
| 84 |
'cornsilk4': (139, 136, 120), |
| 85 |
'crimson': (220, 20, 60), |
| 86 |
'cyan': (0, 255, 255), |
| 87 |
'cyan1': (0, 255, 255), |
| 88 |
'cyan2': (0, 238, 238), |
| 89 |
'cyan3': (0, 205, 205), |
| 90 |
'cyan4': (0, 139, 139), |
| 91 |
'darkgoldenrod': (184, 134, 11), |
| 92 |
'darkgoldenrod1': (255, 185, 15), |
| 93 |
'darkgoldenrod2': (238, 173, 14), |
| 94 |
'darkgoldenrod3': (205, 149, 12), |
| 95 |
'darkgoldenrod4': (139, 101, 8), |
| 96 |
'darkgreen': (0, 100, 0), |
| 97 |
'darkkhaki': (189, 183, 107), |
| 98 |
'darkolivegreen': (85, 107, 47), |
| 99 |
'darkolivegreen1': (202, 255, 112), |
| 100 |
'darkolivegreen2': (188, 238, 104), |
| 101 |
'darkolivegreen3': (162, 205, 90), |
| 102 |
'darkolivegreen4': (110, 139, 61), |
| 103 |
'darkorange': (255, 140, 0), |
| 104 |
'darkorange1': (255, 127, 0), |
| 105 |
'darkorange2': (238, 118, 0), |
| 106 |
'darkorange3': (205, 102, 0), |
| 107 |
'darkorange4': (139, 69, 0), |
| 108 |
'darkorchid': (153, 50, 204), |
| 109 |
'darkorchid1': (191, 62, 255), |
| 110 |
'darkorchid2': (178, 58, 238), |
| 111 |
'darkorchid3': (154, 50, 205), |
| 112 |
'darkorchid4': (104, 34, 139), |
| 113 |
'darksalmon': (233, 150, 122), |
| 114 |
'darkseagreen': (143, 188, 143), |
| 115 |
'darkseagreen1': (193, 255, 193), |
| 116 |
'darkseagreen2': (180, 238, 180), |
| 117 |
'darkseagreen3': (155, 205, 155), |
| 118 |
'darkseagreen4': (105, 139, 105), |
| 119 |
'darkslateblue': (72, 61, 139), |
| 120 |
'darkslategray': (47, 79, 79), |
| 121 |
'darkslategray1': (151, 255, 255), |
| 122 |
'darkslategray2': (141, 238, 238), |
| 123 |
'darkslategray3': (121, 205, 205), |
| 124 |
'darkslategray4': (82, 139, 139), |
| 125 |
'darkslategrey': (47, 79, 79), |
| 126 |
'darkturquoise': (0, 206, 209), |
| 127 |
'darkviolet': (148, 0, 211), |
| 128 |
'deeppink': (255, 20, 147), |
| 129 |
'deeppink1': (255, 20, 147), |
| 130 |
'deeppink2': (238, 18, 137), |
| 131 |
'deeppink3': (205, 16, 118), |
| 132 |
'deeppink4': (139, 10, 80), |
| 133 |
'deepskyblue': (0, 191, 255), |
| 134 |
'deepskyblue1': (0, 191, 255), |
| 135 |
'deepskyblue2': (0, 178, 238), |
| 136 |
'deepskyblue3': (0, 154, 205), |
| 137 |
'deepskyblue4': (0, 104, 139), |
| 138 |
'dimgray': (105, 105, 105), |
| 139 |
'dimgrey': (105, 105, 105), |
| 140 |
'dodgerblue': (30, 144, 255), |
| 141 |
'dodgerblue1': (30, 144, 255), |
| 142 |
'dodgerblue2': (28, 134, 238), |
| 143 |
'dodgerblue3': (24, 116, 205), |
| 144 |
'dodgerblue4': (16, 78, 139), |
| 145 |
'firebrick': (178, 34, 34), |
| 146 |
'firebrick1': (255, 48, 48), |
| 147 |
'firebrick2': (238, 44, 44), |
| 148 |
'firebrick3': (205, 38, 38), |
| 149 |
'firebrick4': (139, 26, 26), |
| 150 |
'floralwhite': (255, 250, 240), |
| 151 |
'forestgreen': (34, 139, 34), |
| 152 |
'gainsboro': (220, 220, 220), |
| 153 |
'ghostwhite': (248, 248, 255), |
| 154 |
'gold': (255, 215, 0), |
| 155 |
'gold1': (255, 215, 0), |
| 156 |
'gold2': (238, 201, 0), |
| 157 |
'gold3': (205, 173, 0), |
| 158 |
'gold4': (139, 117, 0), |
| 159 |
'goldenrod': (218, 165, 32), |
| 160 |
'goldenrod1': (255, 193, 37), |
| 161 |
'goldenrod2': (238, 180, 34), |
| 162 |
'goldenrod3': (205, 155, 29), |
| 163 |
'goldenrod4': (139, 105, 20), |
| 164 |
'gray': (192, 192, 192), |
| 165 |
'gray0': (0, 0, 0), |
| 166 |
'gray1': (3, 3, 3), |
| 167 |
'gray10': (26, 26, 26), |
| 168 |
'gray100': (255, 255, 255), |
| 169 |
'gray11': (28, 28, 28), |
| 170 |
'gray12': (31, 31, 31), |
| 171 |
'gray13': (33, 33, 33), |
| 172 |
'gray14': (36, 36, 36), |
| 173 |
'gray15': (38, 38, 38), |
| 174 |
'gray16': (41, 41, 41), |
| 175 |
'gray17': (43, 43, 43), |
| 176 |
'gray18': (46, 46, 46), |
| 177 |
'gray19': (48, 48, 48), |
| 178 |
'gray2': (5, 5, 5), |
| 179 |
'gray20': (51, 51, 51), |
| 180 |
'gray21': (54, 54, 54), |
| 181 |
'gray22': (56, 56, 56), |
| 182 |
'gray23': (59, 59, 59), |
| 183 |
'gray24': (61, 61, 61), |
| 184 |
'gray25': (64, 64, 64), |
| 185 |
'gray26': (66, 66, 66), |
| 186 |
'gray27': (69, 69, 69), |
| 187 |
'gray28': (71, 71, 71), |
| 188 |
'gray29': (74, 74, 74), |
| 189 |
'gray3': (8, 8, 8), |
| 190 |
'gray30': (77, 77, 77), |
| 191 |
'gray31': (79, 79, 79), |
| 192 |
'gray32': (82, 82, 82), |
| 193 |
'gray33': (84, 84, 84), |
| 194 |
'gray34': (87, 87, 87), |
| 195 |
'gray35': (89, 89, 89), |
| 196 |
'gray36': (92, 92, 92), |
| 197 |
'gray37': (94, 94, 94), |
| 198 |
'gray38': (97, 97, 97), |
| 199 |
'gray39': (99, 99, 99), |
| 200 |
'gray4': (10, 10, 10), |
| 201 |
'gray40': (102, 102, 102), |
| 202 |
'gray41': (105, 105, 105), |
| 203 |
'gray42': (107, 107, 107), |
| 204 |
'gray43': (110, 110, 110), |
| 205 |
'gray44': (112, 112, 112), |
| 206 |
'gray45': (115, 115, 115), |
| 207 |
'gray46': (117, 117, 117), |
| 208 |
'gray47': (120, 120, 120), |
| 209 |
'gray48': (122, 122, 122), |
| 210 |
'gray49': (125, 125, 125), |
| 211 |
'gray5': (13, 13, 13), |
| 212 |
'gray50': (127, 127, 127), |
| 213 |
'gray51': (130, 130, 130), |
| 214 |
'gray52': (133, 133, 133), |
| 215 |
'gray53': (135, 135, 135), |
| 216 |
'gray54': (138, 138, 138), |
| 217 |
'gray55': (140, 140, 140), |
| 218 |
'gray56': (143, 143, 143), |
| 219 |
'gray57': (145, 145, 145), |
| 220 |
'gray58': (148, 148, 148), |
| 221 |
'gray59': (150, 150, 150), |
| 222 |
'gray6': (15, 15, 15), |
| 223 |
'gray60': (153, 153, 153), |
| 224 |
'gray61': (156, 156, 156), |
| 225 |
'gray62': (158, 158, 158), |
| 226 |
'gray63': (161, 161, 161), |
| 227 |
'gray64': (163, 163, 163), |
| 228 |
'gray65': (166, 166, 166), |
| 229 |
'gray66': (168, 168, 168), |
| 230 |
'gray67': (171, 171, 171), |
| 231 |
'gray68': (173, 173, 173), |
| 232 |
'gray69': (176, 176, 176), |
| 233 |
'gray7': (18, 18, 18), |
| 234 |
'gray70': (179, 179, 179), |
| 235 |
'gray71': (181, 181, 181), |
| 236 |
'gray72': (184, 184, 184), |
| 237 |
'gray73': (186, 186, 186), |
| 238 |
'gray74': (189, 189, 189), |
| 239 |
'gray75': (191, 191, 191), |
| 240 |
'gray76': (194, 194, 194), |
| 241 |
'gray77': (196, 196, 196), |
| 242 |
'gray78': (199, 199, 199), |
| 243 |
'gray79': (201, 201, 201), |
| 244 |
'gray8': (20, 20, 20), |
| 245 |
'gray80': (204, 204, 204), |
| 246 |
'gray81': (207, 207, 207), |
| 247 |
'gray82': (209, 209, 209), |
| 248 |
'gray83': (212, 212, 212), |
| 249 |
'gray84': (214, 214, 214), |
| 250 |
'gray85': (217, 217, 217), |
| 251 |
'gray86': (219, 219, 219), |
| 252 |
'gray87': (222, 222, 222), |
| 253 |
'gray88': (224, 224, 224), |
| 254 |
'gray89': (227, 227, 227), |
| 255 |
'gray9': (23, 23, 23), |
| 256 |
'gray90': (229, 229, 229), |
| 257 |
'gray91': (232, 232, 232), |
| 258 |
'gray92': (235, 235, 235), |
| 259 |
'gray93': (237, 237, 237), |
| 260 |
'gray94': (240, 240, 240), |
| 261 |
'gray95': (242, 242, 242), |
| 262 |
'gray96': (245, 245, 245), |
| 263 |
'gray97': (247, 247, 247), |
| 264 |
'gray98': (250, 250, 250), |
| 265 |
'gray99': (252, 252, 252), |
| 266 |
'green': (0, 255, 0), |
| 267 |
'green1': (0, 255, 0), |
| 268 |
'green2': (0, 238, 0), |
| 269 |
'green3': (0, 205, 0), |
| 270 |
'green4': (0, 139, 0), |
| 271 |
'greenyellow': (173, 255, 47), |
| 272 |
'grey': (192, 192, 192), |
| 273 |
'grey0': (0, 0, 0), |
| 274 |
'grey1': (3, 3, 3), |
| 275 |
'grey10': (26, 26, 26), |
| 276 |
'grey100': (255, 255, 255), |
| 277 |
'grey11': (28, 28, 28), |
| 278 |
'grey12': (31, 31, 31), |
| 279 |
'grey13': (33, 33, 33), |
| 280 |
'grey14': (36, 36, 36), |
| 281 |
'grey15': (38, 38, 38), |
| 282 |
'grey16': (41, 41, 41), |
| 283 |
'grey17': (43, 43, 43), |
| 284 |
'grey18': (46, 46, 46), |
| 285 |
'grey19': (48, 48, 48), |
| 286 |
'grey2': (5, 5, 5), |
| 287 |
'grey20': (51, 51, 51), |
| 288 |
'grey21': (54, 54, 54), |
| 289 |
'grey22': (56, 56, 56), |
| 290 |
'grey23': (59, 59, 59), |
| 291 |
'grey24': (61, 61, 61), |
| 292 |
'grey25': (64, 64, 64), |
| 293 |
'grey26': (66, 66, 66), |
| 294 |
'grey27': (69, 69, 69), |
| 295 |
'grey28': (71, 71, 71), |
| 296 |
'grey29': (74, 74, 74), |
| 297 |
'grey3': (8, 8, 8), |
| 298 |
'grey30': (77, 77, 77), |
| 299 |
'grey31': (79, 79, 79), |
| 300 |
'grey32': (82, 82, 82), |
| 301 |
'grey33': (84, 84, 84), |
| 302 |
'grey34': (87, 87, 87), |
| 303 |
'grey35': (89, 89, 89), |
| 304 |
'grey36': (92, 92, 92), |
| 305 |
'grey37': (94, 94, 94), |
| 306 |
'grey38': (97, 97, 97), |
| 307 |
'grey39': (99, 99, 99), |
| 308 |
'grey4': (10, 10, 10), |
| 309 |
'grey40': (102, 102, 102), |
| 310 |
'grey41': (105, 105, 105), |
| 311 |
'grey42': (107, 107, 107), |
| 312 |
'grey43': (110, 110, 110), |
| 313 |
'grey44': (112, 112, 112), |
| 314 |
'grey45': (115, 115, 115), |
| 315 |
'grey46': (117, 117, 117), |
| 316 |
'grey47': (120, 120, 120), |
| 317 |
'grey48': (122, 122, 122), |
| 318 |
'grey49': (125, 125, 125), |
| 319 |
'grey5': (13, 13, 13), |
| 320 |
'grey50': (127, 127, 127), |
| 321 |
'grey51': (130, 130, 130), |
| 322 |
'grey52': (133, 133, 133), |
| 323 |
'grey53': (135, 135, 135), |
| 324 |
'grey54': (138, 138, 138), |
| 325 |
'grey55': (140, 140, 140), |
| 326 |
'grey56': (143, 143, 143), |
| 327 |
'grey57': (145, 145, 145), |
| 328 |
'grey58': (148, 148, 148), |
| 329 |
'grey59': (150, 150, 150), |
| 330 |
'grey6': (15, 15, 15), |
| 331 |
'grey60': (153, 153, 153), |
| 332 |
'grey61': (156, 156, 156), |
| 333 |
'grey62': (158, 158, 158), |
| 334 |
'grey63': (161, 161, 161), |
| 335 |
'grey64': (163, 163, 163), |
| 336 |
'grey65': (166, 166, 166), |
| 337 |
'grey66': (168, 168, 168), |
| 338 |
'grey67': (171, 171, 171), |
| 339 |
'grey68': (173, 173, 173), |
| 340 |
'grey69': (176, 176, 176), |
| 341 |
'grey7': (18, 18, 18), |
| 342 |
'grey70': (179, 179, 179), |
| 343 |
'grey71': (181, 181, 181), |
| 344 |
'grey72': (184, 184, 184), |
| 345 |
'grey73': (186, 186, 186), |
| 346 |
'grey74': (189, 189, 189), |
| 347 |
'grey75': (191, 191, 191), |
| 348 |
'grey76': (194, 194, 194), |
| 349 |
'grey77': (196, 196, 196), |
| 350 |
'grey78': (199, 199, 199), |
| 351 |
'grey79': (201, 201, 201), |
| 352 |
'grey8': (20, 20, 20), |
| 353 |
'grey80': (204, 204, 204), |
| 354 |
'grey81': (207, 207, 207), |
| 355 |
'grey82': (209, 209, 209), |
| 356 |
'grey83': (212, 212, 212), |
| 357 |
'grey84': (214, 214, 214), |
| 358 |
'grey85': (217, 217, 217), |
| 359 |
'grey86': (219, 219, 219), |
| 360 |
'grey87': (222, 222, 222), |
| 361 |
'grey88': (224, 224, 224), |
| 362 |
'grey89': (227, 227, 227), |
| 363 |
'grey9': (23, 23, 23), |
| 364 |
'grey90': (229, 229, 229), |
| 365 |
'grey91': (232, 232, 232), |
| 366 |
'grey92': (235, 235, 235), |
| 367 |
'grey93': (237, 237, 237), |
| 368 |
'grey94': (240, 240, 240), |
| 369 |
'grey95': (242, 242, 242), |
| 370 |
'grey96': (245, 245, 245), |
| 371 |
'grey97': (247, 247, 247), |
| 372 |
'grey98': (250, 250, 250), |
| 373 |
'grey99': (252, 252, 252), |
| 374 |
'honeydew': (240, 255, 240), |
| 375 |
'honeydew1': (240, 255, 240), |
| 376 |
'honeydew2': (224, 238, 224), |
| 377 |
'honeydew3': (193, 205, 193), |
| 378 |
'honeydew4': (131, 139, 131), |
| 379 |
'hotpink': (255, 105, 180), |
| 380 |
'hotpink1': (255, 110, 180), |
| 381 |
'hotpink2': (238, 106, 167), |
| 382 |
'hotpink3': (205, 96, 144), |
| 383 |
'hotpink4': (139, 58, 98), |
| 384 |
'indianred': (205, 92, 92), |
| 385 |
'indianred1': (255, 106, 106), |
| 386 |
'indianred2': (238, 99, 99), |
| 387 |
'indianred3': (205, 85, 85), |
| 388 |
'indianred4': (139, 58, 58), |
| 389 |
'indigo': (75, 0, 130), |
| 390 |
'invis': (255, 255, 254), |
| 391 |
'ivory': (255, 255, 240), |
| 392 |
'ivory1': (255, 255, 240), |
| 393 |
'ivory2': (238, 238, 224), |
| 394 |
'ivory3': (205, 205, 193), |
| 395 |
'ivory4': (139, 139, 131), |
| 396 |
'khaki': (240, 230, 140), |
| 397 |
'khaki1': (255, 246, 143), |
| 398 |
'khaki2': (238, 230, 133), |
| 399 |
'khaki3': (205, 198, 115), |
| 400 |
'khaki4': (139, 134, 78), |
| 401 |
'lavender': (230, 230, 250), |
| 402 |
'lavenderblush': (255, 240, 245), |
| 403 |
'lavenderblush1': (255, 240, 245), |
| 404 |
'lavenderblush2': (238, 224, 229), |
| 405 |
'lavenderblush3': (205, 193, 197), |
| 406 |
'lavenderblush4': (139, 131, 134), |
| 407 |
'lawngreen': (124, 252, 0), |
| 408 |
'lemonchiffon': (255, 250, 205), |
| 409 |
'lemonchiffon1': (255, 250, 205), |
| 410 |
'lemonchiffon2': (238, 233, 191), |
| 411 |
'lemonchiffon3': (205, 201, 165), |
| 412 |
'lemonchiffon4': (139, 137, 112), |
| 413 |
'lightblue': (173, 216, 230), |
| 414 |
'lightblue1': (191, 239, 255), |
| 415 |
'lightblue2': (178, 223, 238), |
| 416 |
'lightblue3': (154, 192, 205), |
| 417 |
'lightblue4': (104, 131, 139), |
| 418 |
'lightcoral': (240, 128, 128), |
| 419 |
'lightcyan': (224, 255, 255), |
| 420 |
'lightcyan1': (224, 255, 255), |
| 421 |
'lightcyan2': (209, 238, 238), |
| 422 |
'lightcyan3': (180, 205, 205), |
| 423 |
'lightcyan4': (122, 139, 139), |
| 424 |
'lightgoldenrod': (238, 221, 130), |
| 425 |
'lightgoldenrod1': (255, 236, 139), |
| 426 |
'lightgoldenrod2': (238, 220, 130), |
| 427 |
'lightgoldenrod3': (205, 190, 112), |
| 428 |
'lightgoldenrod4': (139, 129, 76), |
| 429 |
'lightgoldenrodyellow': (250, 250, 210), |
| 430 |
'lightgray': (211, 211, 211), |
| 431 |
'lightgrey': (211, 211, 211), |
| 432 |
'lightpink': (255, 182, 193), |
| 433 |
'lightpink1': (255, 174, 185), |
| 434 |
'lightpink2': (238, 162, 173), |
| 435 |
'lightpink3': (205, 140, 149), |
| 436 |
'lightpink4': (139, 95, 101), |
| 437 |
'lightsalmon': (255, 160, 122), |
| 438 |
'lightsalmon1': (255, 160, 122), |
| 439 |
'lightsalmon2': (238, 149, 114), |
| 440 |
'lightsalmon3': (205, 129, 98), |
| 441 |
'lightsalmon4': (139, 87, 66), |
| 442 |
'lightseagreen': (32, 178, 170), |
| 443 |
'lightskyblue': (135, 206, 250), |
| 444 |
'lightskyblue1': (176, 226, 255), |
| 445 |
'lightskyblue2': (164, 211, 238), |
| 446 |
'lightskyblue3': (141, 182, 205), |
| 447 |
'lightskyblue4': (96, 123, 139), |
| 448 |
'lightslateblue': (132, 112, 255), |
| 449 |
'lightslategray': (119, 136, 153), |
| 450 |
'lightslategrey': (119, 136, 153), |
| 451 |
'lightsteelblue': (176, 196, 222), |
| 452 |
'lightsteelblue1': (202, 225, 255), |
| 453 |
'lightsteelblue2': (188, 210, 238), |
| 454 |
'lightsteelblue3': (162, 181, 205), |
| 455 |
'lightsteelblue4': (110, 123, 139), |
| 456 |
'lightyellow': (255, 255, 224), |
| 457 |
'lightyellow1': (255, 255, 224), |
| 458 |
'lightyellow2': (238, 238, 209), |
| 459 |
'lightyellow3': (205, 205, 180), |
| 460 |
'lightyellow4': (139, 139, 122), |
| 461 |
'limegreen': (50, 205, 50), |
| 462 |
'linen': (250, 240, 230), |
| 463 |
'magenta': (255, 0, 255), |
| 464 |
'magenta1': (255, 0, 255), |
| 465 |
'magenta2': (238, 0, 238), |
| 466 |
'magenta3': (205, 0, 205), |
| 467 |
'magenta4': (139, 0, 139), |
| 468 |
'maroon': (176, 48, 96), |
| 469 |
'maroon1': (255, 52, 179), |
| 470 |
'maroon2': (238, 48, 167), |
| 471 |
'maroon3': (205, 41, 144), |
| 472 |
'maroon4': (139, 28, 98), |
| 473 |
'mediumaquamarine': (102, 205, 170), |
| 474 |
'mediumblue': (0, 0, 205), |
| 475 |
'mediumorchid': (186, 85, 211), |
| 476 |
'mediumorchid1': (224, 102, 255), |
| 477 |
'mediumorchid2': (209, 95, 238), |
| 478 |
'mediumorchid3': (180, 82, 205), |
| 479 |
'mediumorchid4': (122, 55, 139), |
| 480 |
'mediumpurple': (147, 112, 219), |
| 481 |
'mediumpurple1': (171, 130, 255), |
| 482 |
'mediumpurple2': (159, 121, 238), |
| 483 |
'mediumpurple3': (137, 104, 205), |
| 484 |
'mediumpurple4': (93, 71, 139), |
| 485 |
'mediumseagreen': (60, 179, 113), |
| 486 |
'mediumslateblue': (123, 104, 238), |
| 487 |
'mediumspringgreen': (0, 250, 154), |
| 488 |
'mediumturquoise': (72, 209, 204), |
| 489 |
'mediumvioletred': (199, 21, 133), |
| 490 |
'midnightblue': (25, 25, 112), |
| 491 |
'mintcream': (245, 255, 250), |
| 492 |
'mistyrose': (255, 228, 225), |
| 493 |
'mistyrose1': (255, 228, 225), |
| 494 |
'mistyrose2': (238, 213, 210), |
| 495 |
'mistyrose3': (205, 183, 181), |
| 496 |
'mistyrose4': (139, 125, 123), |
| 497 |
'moccasin': (255, 228, 181), |
| 498 |
'navajowhite': (255, 222, 173), |
| 499 |
'navajowhite1': (255, 222, 173), |
| 500 |
'navajowhite2': (238, 207, 161), |
| 501 |
'navajowhite3': (205, 179, 139), |
| 502 |
'navajowhite4': (139, 121, 94), |
| 503 |
'navy': (0, 0, 128), |
| 504 |
'navyblue': (0, 0, 128), |
| 505 |
'none': (255, 255, 254), |
| 506 |
'oldlace': (253, 245, 230), |
| 507 |
'olivedrab': (107, 142, 35), |
| 508 |
'olivedrab1': (192, 255, 62), |
| 509 |
'olivedrab2': (179, 238, 58), |
| 510 |
'olivedrab3': (154, 205, 50), |
| 511 |
'olivedrab4': (105, 139, 34), |
| 512 |
'orange': (255, 165, 0), |
| 513 |
'orange1': (255, 165, 0), |
| 514 |
'orange2': (238, 154, 0), |
| 515 |
'orange3': (205, 133, 0), |
| 516 |
'orange4': (139, 90, 0), |
| 517 |
'orangered': (255, 69, 0), |
| 518 |
'orangered1': (255, 69, 0), |
| 519 |
'orangered2': (238, 64, 0), |
| 520 |
'orangered3': (205, 55, 0), |
| 521 |
'orangered4': (139, 37, 0), |
| 522 |
'orchid': (218, 112, 214), |
| 523 |
'orchid1': (255, 131, 250), |
| 524 |
'orchid2': (238, 122, 233), |
| 525 |
'orchid3': (205, 105, 201), |
| 526 |
'orchid4': (139, 71, 137), |
| 527 |
'palegoldenrod': (238, 232, 170), |
| 528 |
'palegreen': (152, 251, 152), |
| 529 |
'palegreen1': (154, 255, 154), |
| 530 |
'palegreen2': (144, 238, 144), |
| 531 |
'palegreen3': (124, 205, 124), |
| 532 |
'palegreen4': (84, 139, 84), |
| 533 |
'paleturquoise': (175, 238, 238), |
| 534 |
'paleturquoise1': (187, 255, 255), |
| 535 |
'paleturquoise2': (174, 238, 238), |
| 536 |
'paleturquoise3': (150, 205, 205), |
| 537 |
'paleturquoise4': (102, 139, 139), |
| 538 |
'palevioletred': (219, 112, 147), |
| 539 |
'palevioletred1': (255, 130, 171), |
| 540 |
'palevioletred2': (238, 121, 159), |
| 541 |
'palevioletred3': (205, 104, 137), |
| 542 |
'palevioletred4': (139, 71, 93), |
| 543 |
'papayawhip': (255, 239, 213), |
| 544 |
'peachpuff': (255, 218, 185), |
| 545 |
'peachpuff1': (255, 218, 185), |
| 546 |
'peachpuff2': (238, 203, 173), |
| 547 |
'peachpuff3': (205, 175, 149), |
| 548 |
'peachpuff4': (139, 119, 101), |
| 549 |
'peru': (205, 133, 63), |
| 550 |
'pink': (255, 192, 203), |
| 551 |
'pink1': (255, 181, 197), |
| 552 |
'pink2': (238, 169, 184), |
| 553 |
'pink3': (205, 145, 158), |
| 554 |
'pink4': (139, 99, 108), |
| 555 |
'plum': (221, 160, 221), |
| 556 |
'plum1': (255, 187, 255), |
| 557 |
'plum2': (238, 174, 238), |
| 558 |
'plum3': (205, 150, 205), |
| 559 |
'plum4': (139, 102, 139), |
| 560 |
'powderblue': (176, 224, 230), |
| 561 |
'purple': (160, 32, 240), |
| 562 |
'purple1': (155, 48, 255), |
| 563 |
'purple2': (145, 44, 238), |
| 564 |
'purple3': (125, 38, 205), |
| 565 |
'purple4': (85, 26, 139), |
| 566 |
'red': (255, 0, 0), |
| 567 |
'red1': (255, 0, 0), |
| 568 |
'red2': (238, 0, 0), |
| 569 |
'red3': (205, 0, 0), |
| 570 |
'red4': (139, 0, 0), |
| 571 |
'rosybrown': (188, 143, 143), |
| 572 |
'rosybrown1': (255, 193, 193), |
| 573 |
'rosybrown2': (238, 180, 180), |
| 574 |
'rosybrown3': (205, 155, 155), |
| 575 |
'rosybrown4': (139, 105, 105), |
| 576 |
'royalblue': (65, 105, 225), |
| 577 |
'royalblue1': (72, 118, 255), |
| 578 |
'royalblue2': (67, 110, 238), |
| 579 |
'royalblue3': (58, 95, 205), |
| 580 |
'royalblue4': (39, 64, 139), |
| 581 |
'saddlebrown': (139, 69, 19), |
| 582 |
'salmon': (250, 128, 114), |
| 583 |
'salmon1': (255, 140, 105), |
| 584 |
'salmon2': (238, 130, 98), |
| 585 |
'salmon3': (205, 112, 84), |
| 586 |
'salmon4': (139, 76, 57), |
| 587 |
'sandybrown': (244, 164, 96), |
| 588 |
'seagreen': (46, 139, 87), |
| 589 |
'seagreen1': (84, 255, 159), |
| 590 |
'seagreen2': (78, 238, 148), |
| 591 |
'seagreen3': (67, 205, 128), |
| 592 |
'seagreen4': (46, 139, 87), |
| 593 |
'seashell': (255, 245, 238), |
| 594 |
'seashell1': (255, 245, 238), |
| 595 |
'seashell2': (238, 229, 222), |
| 596 |
'seashell3': (205, 197, 191), |
| 597 |
'seashell4': (139, 134, 130), |
| 598 |
'sienna': (160, 82, 45), |
| 599 |
'sienna1': (255, 130, 71), |
| 600 |
'sienna2': (238, 121, 66), |
| 601 |
'sienna3': (205, 104, 57), |
| 602 |
'sienna4': (139, 71, 38), |
| 603 |
'skyblue': (135, 206, 235), |
| 604 |
'skyblue1': (135, 206, 255), |
| 605 |
'skyblue2': (126, 192, 238), |
| 606 |
'skyblue3': (108, 166, 205), |
| 607 |
'skyblue4': (74, 112, 139), |
| 608 |
'slateblue': (106, 90, 205), |
| 609 |
'slateblue1': (131, 111, 255), |
| 610 |
'slateblue2': (122, 103, 238), |
| 611 |
'slateblue3': (105, 89, 205), |
| 612 |
'slateblue4': (71, 60, 139), |
| 613 |
'slategray': (112, 128, 144), |
| 614 |
'slategray1': (198, 226, 255), |
| 615 |
'slategray2': (185, 211, 238), |
| 616 |
'slategray3': (159, 182, 205), |
| 617 |
'slategray4': (108, 123, 139), |
| 618 |
'slategrey': (112, 128, 144), |
| 619 |
'snow': (255, 250, 250), |
| 620 |
'snow1': (255, 250, 250), |
| 621 |
'snow2': (238, 233, 233), |
| 622 |
'snow3': (205, 201, 201), |
| 623 |
'snow4': (139, 137, 137), |
| 624 |
'springgreen': (0, 255, 127), |
| 625 |
'springgreen1': (0, 255, 127), |
| 626 |
'springgreen2': (0, 238, 118), |
| 627 |
'springgreen3': (0, 205, 102), |
| 628 |
'springgreen4': (0, 139, 69), |
| 629 |
'steelblue': (70, 130, 180), |
| 630 |
'steelblue1': (99, 184, 255), |
| 631 |
'steelblue2': (92, 172, 238), |
| 632 |
'steelblue3': (79, 148, 205), |
| 633 |
'steelblue4': (54, 100, 139), |
| 634 |
'tan': (210, 180, 140), |
| 635 |
'tan1': (255, 165, 79), |
| 636 |
'tan2': (238, 154, 73), |
| 637 |
'tan3': (205, 133, 63), |
| 638 |
'tan4': (139, 90, 43), |
| 639 |
'thistle': (216, 191, 216), |
| 640 |
'thistle1': (255, 225, 255), |
| 641 |
'thistle2': (238, 210, 238), |
| 642 |
'thistle3': (205, 181, 205), |
| 643 |
'thistle4': (139, 123, 139), |
| 644 |
'tomato': (255, 99, 71), |
| 645 |
'tomato1': (255, 99, 71), |
| 646 |
'tomato2': (238, 92, 66), |
| 647 |
'tomato3': (205, 79, 57), |
| 648 |
'tomato4': (139, 54, 38), |
| 649 |
'transparent': (255, 255, 254), |
| 650 |
'turquoise': (64, 224, 208), |
| 651 |
'turquoise1': (0, 245, 255), |
| 652 |
'turquoise2': (0, 229, 238), |
| 653 |
'turquoise3': (0, 197, 205), |
| 654 |
'turquoise4': (0, 134, 139), |
| 655 |
'violet': (238, 130, 238), |
| 656 |
'violetred': (208, 32, 144), |
| 657 |
'violetred1': (255, 62, 150), |
| 658 |
'violetred2': (238, 58, 140), |
| 659 |
'violetred3': (205, 50, 120), |
| 660 |
'violetred4': (139, 34, 82), |
| 661 |
'wheat': (245, 222, 179), |
| 662 |
'wheat1': (255, 231, 186), |
| 663 |
'wheat2': (238, 216, 174), |
| 664 |
'wheat3': (205, 186, 150), |
| 665 |
'wheat4': (139, 126, 102), |
| 666 |
'white': (255, 255, 255), |
| 667 |
'whitesmoke': (245, 245, 245), |
| 668 |
'yellow': (255, 255, 0), |
| 669 |
'yellow1': (255, 255, 0), |
| 670 |
'yellow2': (238, 238, 0), |
| 671 |
'yellow3': (205, 205, 0), |
| 672 |
'yellow4': (139, 139, 0), |
| 673 |
'yellowgreen': (154, 205, 50), |
| 674 |
} |
| 675 |
|
| 676 |
GONS = { |
| 677 |
b'triangle': 3, |
| 678 |
b'diamond': 4, |
| 679 |
b'pentagon': 5, |
| 680 |
b'hexagon': 6, |
| 681 |
b'septagon': 7, |
| 682 |
b'octagon': 8, |
| 683 |
} |
| 684 |
|
| 685 |
re_nonword=re.compile(r'([^0-9a-zA-Z_.]+)') |
| 686 |
re_linewidth=re.compile(forcestr(r'setlinewidth\((\d+(\.\d*)?|\.\d+)\)')) |
| 687 |
|
| 688 |
def combine(color1, color2, alpha): |
| 689 |
r1, g1, b1 = color1 |
| 690 |
r2, g2, b2 = color2 |
| 691 |
beta = 1.0 - alpha |
| 692 |
return (int(r1 * alpha + r2 * beta), |
| 693 |
int(g1 * alpha + g2 * beta), |
| 694 |
int(b1 * alpha + b2 * beta)) |
| 695 |
|
| 696 |
|
| 697 |
def highlight_color(color): |
| 698 |
if color == (0, 0, 0): |
| 699 |
return (255, 0, 255) |
| 700 |
elif color == (255, 255, 255): |
| 701 |
return (255, 255, 0) |
| 702 |
intensity = sum(color) |
| 703 |
if intensity > 191 * 3: |
| 704 |
return combine(color, (128, 192, 0), 0.2) |
| 705 |
else: |
| 706 |
return combine(color, (255, 255, 0), 0.2) |
| 707 |
|
| 708 |
def getcolor(name, default): |
| 709 |
if name in COLOR: |
| 710 |
return COLOR[name] |
| 711 |
elif name.startswith('#') and len(name) == 7: |
| 712 |
rval = COLOR[name] = (int(name[1:3],16), int(name[3:5],16), int(name[5:7],16)) |
| 713 |
return rval |
| 714 |
else: |
| 715 |
return default |
| 716 |
|
| 717 |
def ensure_readable(fgcolor, bgcolor): |
| 718 |
if bgcolor is None: |
| 719 |
return fgcolor |
| 720 |
r, g, b = bgcolor |
| 721 |
|
| 722 |
l = 0.2627 * r + 0.6780 * g + 0.0593 * b |
| 723 |
if l < 70: |
| 724 |
return (255, 255, 255) |
| 725 |
return fgcolor |
| 726 |
|
| 727 |
class GraphLayout: |
| 728 |
fixedfont = False |
| 729 |
|
| 730 |
def __init__(self, scale, width, height): |
| 731 |
self.scale = scale |
| 732 |
self.boundingbox = width, height |
| 733 |
self.nodes = {} |
| 734 |
self.edges = [] |
| 735 |
self.links = {} |
| 736 |
|
| 737 |
def add_node(self, *args): |
| 738 |
n = Node(*args) |
| 739 |
self.nodes[n.name] = n |
| 740 |
|
| 741 |
def add_edge(self, *args): |
| 742 |
self.edges.append(Edge(self.nodes, *args)) |
| 743 |
|
| 744 |
def get_display(self): |
| 745 |
from dotviewer.graphdisplay import GraphDisplay |
| 746 |
return GraphDisplay(self) |
| 747 |
|
| 748 |
def display(self): |
| 749 |
self.get_display().run() |
| 750 |
|
| 751 |
def reload(self): |
| 752 |
return self |
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
def display_async_quit(): |
| 757 |
pygame.event.post(pygame.event.Event(QUIT)) |
| 758 |
|
| 759 |
def display_async_cmd(**kwds): |
| 760 |
pygame.event.post(pygame.event.Event(USEREVENT, **kwds)) |
| 761 |
|
| 762 |
EventQueue = [] |
| 763 |
|
| 764 |
def wait_for_events(): |
| 765 |
if not EventQueue: |
| 766 |
EventQueue.append(pygame.event.wait()) |
| 767 |
EventQueue.extend(pygame.event.get()) |
| 768 |
|
| 769 |
def wait_for_async_cmd(): |
| 770 |
|
| 771 |
while True: |
| 772 |
wait_for_events() |
| 773 |
e = EventQueue.pop(0) |
| 774 |
if e.type in (USEREVENT, QUIT): |
| 775 |
break |
| 776 |
EventQueue.insert(0, e) |
| 777 |
|
| 778 |
|
| 779 |
class Node: |
| 780 |
def __init__(self, name, x, y, w, h, label, style, shape, color, fillcolor): |
| 781 |
self.name = forceunicode(name) |
| 782 |
self.x = float(x) |
| 783 |
self.y = float(y) |
| 784 |
self.w = float(w) |
| 785 |
self.h = float(h) |
| 786 |
self.label = forceunicode(label) |
| 787 |
self.style = style |
| 788 |
self.shape = shape |
| 789 |
self.color = forceunicode(color) |
| 790 |
self.fillcolor = forceunicode(fillcolor) |
| 791 |
self.highlight = False |
| 792 |
|
| 793 |
def sethighlight(self, which): |
| 794 |
self.highlight = bool(which) |
| 795 |
|
| 796 |
class Edge: |
| 797 |
label = None |
| 798 |
|
| 799 |
def __init__(self, nodes, tail, head, cnt, *rest): |
| 800 |
self.tail = nodes[forceunicode(tail)] |
| 801 |
self.head = nodes[forceunicode(head)] |
| 802 |
cnt = int(cnt) |
| 803 |
self.points = [(float(rest[i]), float(rest[i+1])) |
| 804 |
for i in range(0, cnt*2, 2)] |
| 805 |
rest = rest[cnt*2:] |
| 806 |
if len(rest) > 2: |
| 807 |
label, xl, yl = rest[:3] |
| 808 |
self.label = forceunicode(label) |
| 809 |
self.xl = float(xl) |
| 810 |
self.yl = float(yl) |
| 811 |
rest = rest[3:] |
| 812 |
self.style, self.color = rest |
| 813 |
self.color = forceunicode(self.color) |
| 814 |
linematch = re_linewidth.match(self.style) |
| 815 |
if linematch: |
| 816 |
num = linematch.group(1) |
| 817 |
self.linewidth = int(round(float(num))) |
| 818 |
self.style = self.style[linematch.end(0):] |
| 819 |
else: |
| 820 |
self.linewidth = 1 |
| 821 |
self.highlight = False |
| 822 |
self.cachedbezierpoints = None |
| 823 |
self.cachedarrowhead = None |
| 824 |
self.cachedlimits = None |
| 825 |
|
| 826 |
def sethighlight(self, which): |
| 827 |
self.highlight = bool(which) |
| 828 |
|
| 829 |
def limits(self): |
| 830 |
result = self.cachedlimits |
| 831 |
if result is None: |
| 832 |
points = self.bezierpoints() |
| 833 |
xs = [point[0] for point in points] |
| 834 |
ys = [point[1] for point in points] |
| 835 |
self.cachedlimits = result = (min(xs), max(ys), max(xs), min(ys)) |
| 836 |
return result |
| 837 |
|
| 838 |
def bezierpoints(self): |
| 839 |
result = self.cachedbezierpoints |
| 840 |
if result is None: |
| 841 |
result = [] |
| 842 |
pts = self.points |
| 843 |
for i in range(0, len(pts)-3, 3): |
| 844 |
result += beziercurve(pts[i], pts[i+1], pts[i+2], pts[i+3]) |
| 845 |
self.cachedbezierpoints = result |
| 846 |
return result |
| 847 |
|
| 848 |
def arrowhead(self): |
| 849 |
result = self.cachedarrowhead |
| 850 |
if result is None: |
| 851 |
|
| 852 |
|
| 853 |
def dist(node, pt): |
| 854 |
return abs(node.x - pt[0]) + abs(node.y - pt[1]) |
| 855 |
|
| 856 |
error_if_direct = (dist(self.head, self.points[-1]) + |
| 857 |
dist(self.tail, self.points[0])) |
| 858 |
error_if_reversed = (dist(self.tail, self.points[-1]) + |
| 859 |
dist(self.head, self.points[0])) |
| 860 |
if error_if_direct > error_if_reversed: |
| 861 |
head = 0 |
| 862 |
dir = 1 |
| 863 |
else: |
| 864 |
head = -1 |
| 865 |
dir = -1 |
| 866 |
n = 1 |
| 867 |
while True: |
| 868 |
try: |
| 869 |
x0, y0 = self.points[head] |
| 870 |
x1, y1 = self.points[head+n*dir] |
| 871 |
except IndexError: |
| 872 |
result = [] |
| 873 |
break |
| 874 |
vx = x0-x1 |
| 875 |
vy = y0-y1 |
| 876 |
try: |
| 877 |
f = 0.12 / math.sqrt(vx*vx + vy*vy) |
| 878 |
vx *= f |
| 879 |
vy *= f |
| 880 |
result = [(x0 + 0.9*vx, y0 + 0.9*vy), |
| 881 |
(x0 + 0.4*vy, y0 - 0.4*vx), |
| 882 |
(x0 - 0.4*vy, y0 + 0.4*vx)] |
| 883 |
break |
| 884 |
except (ZeroDivisionError, ValueError): |
| 885 |
n += 1 |
| 886 |
self.cachedarrowhead = result |
| 887 |
return result |
| 888 |
|
| 889 |
def beziercurve(p0, p1, p2, p3, resolution=8): |
| 890 |
(x0, y0) = p0 |
| 891 |
(x1, y1) = p1 |
| 892 |
(x2, y2) = p2 |
| 893 |
(x3, y3) = p3 |
| 894 |
result = [] |
| 895 |
f = 1.0/(resolution-1) |
| 896 |
append = result.append |
| 897 |
for i in range(resolution): |
| 898 |
t = f*i |
| 899 |
t0 = (1-t)*(1-t)*(1-t) |
| 900 |
t1 = t *(1-t)*(1-t) * 3.0 |
| 901 |
t2 = t * t *(1-t) * 3.0 |
| 902 |
t3 = t * t * t |
| 903 |
append((x0*t0 + x1*t1 + x2*t2 + x3*t3, |
| 904 |
y0*t0 + y1*t1 + y2*t2 + y3*t3)) |
| 905 |
return result |
| 906 |
|
| 907 |
def segmentdistance(p0, p1, p): |
| 908 |
"Distance between the point (x,y) and the segment (x0,y0)-(x1,y1)." |
| 909 |
(x0, y0) = p0 |
| 910 |
(x1, y1) = p1 |
| 911 |
(x, y) = p |
| 912 |
vx = x1 - x0 |
| 913 |
vy = y1 - y0 |
| 914 |
try: |
| 915 |
l = math.hypot(vx, vy) |
| 916 |
vx /= l |
| 917 |
vy /= l |
| 918 |
dlong = vx*(x-x0) + vy*(y-y0) |
| 919 |
except (ZeroDivisionError, ValueError): |
| 920 |
dlong = -1 |
| 921 |
if dlong < 0.0: |
| 922 |
return math.hypot(x-x0, y-y0) |
| 923 |
elif dlong > l: |
| 924 |
return math.hypot(x-x1, y-y1) |
| 925 |
else: |
| 926 |
return abs(vy*(x-x0) - vx*(y-y0)) |
| 927 |
|
| 928 |
def ellipse(t, center, a, b): |
| 929 |
""" compute points on an elliplse, with t running from 0 to 2*math.pi, a |
| 930 |
and b being the radii and center as the origin of the ellipse. """ |
| 931 |
xcenter, ycenter = center |
| 932 |
return int(xcenter - a / 2.0 * math.sin(t)), int(ycenter - b / 2.0 * math.cos(t)) |
| 933 |
|
| 934 |
class GraphRenderer: |
| 935 |
MARGIN = 0.6 |
| 936 |
FONTCACHE = {} |
| 937 |
|
| 938 |
def __init__(self, screen, graphlayout, scale=75, highdpi=False): |
| 939 |
if highdpi: |
| 940 |
self.SCALEMIN = 10 |
| 941 |
self.SCALEMAX = 200 |
| 942 |
else: |
| 943 |
self.SCALEMIN = 3 |
| 944 |
self.SCALEMAX = 100 |
| 945 |
self.graphlayout = graphlayout |
| 946 |
self.setscale(scale) |
| 947 |
self.setoffset(0, 0) |
| 948 |
self.screen = screen |
| 949 |
self.textzones = [] |
| 950 |
self.highlightwords = graphlayout.links |
| 951 |
self.highlight_word = None |
| 952 |
self.visiblenodes = [] |
| 953 |
self.visibleedges = [] |
| 954 |
|
| 955 |
def wordcolor(self, word): |
| 956 |
info = self.highlightwords[word] |
| 957 |
if isinstance(info, tuple) and len(info) >= 2: |
| 958 |
color = info[1] |
| 959 |
else: |
| 960 |
color = None |
| 961 |
if color is None: |
| 962 |
color = (128,0,0) |
| 963 |
if word == self.highlight_word: |
| 964 |
return ((255,255,80), color) |
| 965 |
else: |
| 966 |
return (color, None) |
| 967 |
|
| 968 |
def setscale(self, scale): |
| 969 |
scale = max(min(scale, self.SCALEMAX), self.SCALEMIN) |
| 970 |
self.scale = float(scale) |
| 971 |
w, h = self.graphlayout.boundingbox |
| 972 |
self.margin = int(self.MARGIN * scale) |
| 973 |
self.width = int(w * scale) + (2 * self.margin) |
| 974 |
self.height = int(h * scale) + (2 * self.margin) |
| 975 |
self.bboxh = h |
| 976 |
size = int(15 * (scale-10) / 75) |
| 977 |
self.font = self.getfont(size) |
| 978 |
|
| 979 |
def getfont(self, size): |
| 980 |
if size in self.FONTCACHE: |
| 981 |
return self.FONTCACHE[size] |
| 982 |
elif size < 5: |
| 983 |
self.FONTCACHE[size] = None |
| 984 |
return None |
| 985 |
else: |
| 986 |
if self.graphlayout.fixedfont: |
| 987 |
filename = FIXEDFONT |
| 988 |
else: |
| 989 |
filename = FONT |
| 990 |
font = self.FONTCACHE[size] = pygame.font.Font(filename, size) |
| 991 |
return font |
| 992 |
|
| 993 |
def setoffset(self, offsetx, offsety): |
| 994 |
"Set the (x,y) origin of the rectangle where the graph will be rendered." |
| 995 |
self.ofsx = offsetx - self.margin |
| 996 |
self.ofsy = offsety - self.margin |
| 997 |
|
| 998 |
def shiftoffset(self, dx, dy): |
| 999 |
self.ofsx += dx |
| 1000 |
self.ofsy += dy |
| 1001 |
|
| 1002 |
def getcenter(self): |
| 1003 |
w, h = self.screen.get_size() |
| 1004 |
return self.revmap(w//2, h//2) |
| 1005 |
|
| 1006 |
def setcenter(self, x, y): |
| 1007 |
w, h = self.screen.get_size() |
| 1008 |
x, y = self.map(x, y) |
| 1009 |
self.shiftoffset(x-w//2, y-h//2) |
| 1010 |
|
| 1011 |
def shiftscale(self, factor, fix=None): |
| 1012 |
if fix is None: |
| 1013 |
fixx, fixy = self.screen.get_size() |
| 1014 |
fixx //= 2 |
| 1015 |
fixy //= 2 |
| 1016 |
else: |
| 1017 |
fixx, fixy = fix |
| 1018 |
x, y = self.revmap(fixx, fixy) |
| 1019 |
self.setscale(self.scale * factor) |
| 1020 |
newx, newy = self.map(x, y) |
| 1021 |
self.shiftoffset(newx - fixx, newy - fixy) |
| 1022 |
|
| 1023 |
def reoffset(self, swidth, sheight): |
| 1024 |
offsetx = noffsetx = self.ofsx |
| 1025 |
offsety = noffsety = self.ofsy |
| 1026 |
width = self.width |
| 1027 |
height = self.height |
| 1028 |
|
| 1029 |
|
| 1030 |
if width <= swidth: |
| 1031 |
noffsetx = (width - swidth) // 2 |
| 1032 |
else: |
| 1033 |
noffsetx = min(max(0, offsetx), width - swidth) |
| 1034 |
|
| 1035 |
if height <= sheight: |
| 1036 |
noffsety = (height - sheight) // 2 |
| 1037 |
else: |
| 1038 |
noffsety = min(max(0, offsety), height - sheight) |
| 1039 |
|
| 1040 |
self.ofsx = noffsetx |
| 1041 |
self.ofsy = noffsety |
| 1042 |
|
| 1043 |
def getboundingbox(self): |
| 1044 |
"Get the rectangle where the graph will be rendered." |
| 1045 |
return (-self.ofsx, -self.ofsy, self.width, self.height) |
| 1046 |
|
| 1047 |
def visible(self, x1, y1, x2, y2): |
| 1048 |
"""Is any part of the box visible (i.e. within the bounding box)? |
| 1049 |
|
| 1050 |
We have to perform clipping ourselves because with big graphs the |
| 1051 |
coordinates may sometimes become longs and cause OverflowErrors |
| 1052 |
within pygame. |
| 1053 |
""" |
| 1054 |
w, h = self.screen.get_size() |
| 1055 |
return x1 < w and x2 > 0 and y1 < h and y2 > 0 |
| 1056 |
|
| 1057 |
def computevisible(self): |
| 1058 |
del self.visiblenodes[:] |
| 1059 |
del self.visibleedges[:] |
| 1060 |
w, h = self.screen.get_size() |
| 1061 |
for node in self.graphlayout.nodes.values(): |
| 1062 |
x, y = self.map(node.x, node.y) |
| 1063 |
nw2 = int(node.w * self.scale)//2 |
| 1064 |
nh2 = int(node.h * self.scale)//2 |
| 1065 |
if x-nw2 < w and x+nw2 > 0 and y-nh2 < h and y+nh2 > 0: |
| 1066 |
self.visiblenodes.append(node) |
| 1067 |
for edge in self.graphlayout.edges: |
| 1068 |
if edge.style == b"invis": |
| 1069 |
continue |
| 1070 |
x1, y1, x2, y2 = edge.limits() |
| 1071 |
x1, y1 = self.map(x1, y1) |
| 1072 |
if x1 < w and y1 < h: |
| 1073 |
x2, y2 = self.map(x2, y2) |
| 1074 |
if x2 > 0 and y2 > 0: |
| 1075 |
self.visibleedges.append(edge) |
| 1076 |
|
| 1077 |
def map(self, x, y): |
| 1078 |
return (int(x*self.scale) - (self.ofsx - self.margin), |
| 1079 |
int((self.bboxh-y)*self.scale) - (self.ofsy - self.margin)) |
| 1080 |
|
| 1081 |
def revmap(self, px, py): |
| 1082 |
return ((px + (self.ofsx - self.margin)) / self.scale, |
| 1083 |
self.bboxh - (py + (self.ofsy - self.margin)) / self.scale) |
| 1084 |
|
| 1085 |
def draw_node_commands(self, node): |
| 1086 |
if node.shape in (b"record", b'Mrecord'): |
| 1087 |
return self.draw_record_commands(node) |
| 1088 |
|
| 1089 |
xcenter, ycenter = self.map(node.x, node.y) |
| 1090 |
boxwidth = int(node.w * self.scale) |
| 1091 |
boxheight = int(node.h * self.scale) |
| 1092 |
fgcolor = getcolor(node.color, (0,0,0)) |
| 1093 |
bgcolor = getcolor(node.fillcolor, (255,255,255)) |
| 1094 |
if node.highlight: |
| 1095 |
fgcolor = highlight_color(fgcolor) |
| 1096 |
bgcolor = highlight_color(bgcolor) |
| 1097 |
|
| 1098 |
text = node.label |
| 1099 |
lines = text.replace('\\l','\\l\n').replace('\r','\r\n').split('\n') |
| 1100 |
|
| 1101 |
if not lines[-1]: |
| 1102 |
del lines[-1] |
| 1103 |
wmax = 0 |
| 1104 |
hmax = 0 |
| 1105 |
commands = [] |
| 1106 |
bkgndcommands = [] |
| 1107 |
if self.font is None: |
| 1108 |
if lines: |
| 1109 |
raw_line = lines[0].replace('\\l','').replace('\r','') |
| 1110 |
if raw_line: |
| 1111 |
for size in (12, 10, 8, 6, 4): |
| 1112 |
font = self.getfont(size) |
| 1113 |
img = TextSnippet(self, raw_line, (0, 0, 0), bgcolor, font=font) |
| 1114 |
w, h = img.get_size() |
| 1115 |
if (w >= boxwidth or h >= boxheight): |
| 1116 |
continue |
| 1117 |
else: |
| 1118 |
if w>wmax: wmax = w |
| 1119 |
def cmd(img=img, y=hmax, w=w): |
| 1120 |
img.draw(xcenter-w//2, ytop+y) |
| 1121 |
commands.append(cmd) |
| 1122 |
hmax += h |
| 1123 |
break |
| 1124 |
else: |
| 1125 |
for line in lines: |
| 1126 |
raw_line = line.replace('\\l','').replace('\r','') or ' ' |
| 1127 |
if '\f' in raw_line: |
| 1128 |
imgs = [] |
| 1129 |
graytext = True |
| 1130 |
h = 16 |
| 1131 |
w_total = 0 |
| 1132 |
for linepart in raw_line.split('\f'): |
| 1133 |
graytext = not graytext |
| 1134 |
if not linepart.strip(): |
| 1135 |
continue |
| 1136 |
if graytext: |
| 1137 |
fgcolor = (128, 160, 160) |
| 1138 |
else: |
| 1139 |
fgcolor = (0, 0, 0) |
| 1140 |
img = TextSnippet(self, linepart, fgcolor, bgcolor) |
| 1141 |
imgs.append((w_total, img)) |
| 1142 |
w, h = img.get_size() |
| 1143 |
w_total += w |
| 1144 |
if w_total > wmax: wmax = w_total |
| 1145 |
def cmd(imgs=imgs, y=hmax): |
| 1146 |
for x, img in imgs: |
| 1147 |
img.draw(xleft+x, ytop+y) |
| 1148 |
commands.append(cmd) |
| 1149 |
else: |
| 1150 |
img = TextSnippet(self, raw_line, (0, 0, 0), bgcolor) |
| 1151 |
w, h = img.get_size() |
| 1152 |
if w>wmax: wmax = w |
| 1153 |
if raw_line.strip(): |
| 1154 |
if line.endswith('\\l'): |
| 1155 |
def cmd(img=img, y=hmax): |
| 1156 |
img.draw(xleft, ytop+y) |
| 1157 |
elif line.endswith('\r'): |
| 1158 |
def cmd(img=img, y=hmax, w=w): |
| 1159 |
img.draw(xright-w, ytop+y) |
| 1160 |
else: |
| 1161 |
def cmd(img=img, y=hmax, w=w): |
| 1162 |
img.draw(xcenter-w//2, ytop+y) |
| 1163 |
commands.append(cmd) |
| 1164 |
hmax += h |
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
xleft = xcenter - wmax//2 |
| 1170 |
xright = xcenter + wmax//2 |
| 1171 |
ytop = ycenter - hmax//2 |
| 1172 |
x = xcenter-boxwidth//2 |
| 1173 |
y = ycenter-boxheight//2 |
| 1174 |
center = (xcenter, ycenter) |
| 1175 |
|
| 1176 |
if node.shape in (b'box', b'rect', b'rectangle'): |
| 1177 |
rect = (x-1, y-1, boxwidth+2, boxheight+2) |
| 1178 |
if rect[0] < 0: |
| 1179 |
rect = (0, y-1, boxwidth+2 + x-1, boxheight+2) |
| 1180 |
if rect[1] < 0: |
| 1181 |
rect = (rect[0], 0, rect[2], boxheight+2 + y-1) |
| 1182 |
def cmd(): |
| 1183 |
self.screen.fill(bgcolor, rect) |
| 1184 |
bkgndcommands.append(cmd) |
| 1185 |
def cmd(): |
| 1186 |
pygame.draw.rect(self.screen, fgcolor, rect, 1) |
| 1187 |
commands.append(cmd) |
| 1188 |
elif node.shape == b'square': |
| 1189 |
width = max(boxwidth+2, boxheight+2) |
| 1190 |
rect = (x-1, y-1, width, width) |
| 1191 |
def cmd(): |
| 1192 |
self.screen.fill(bgcolor, rect) |
| 1193 |
bkgndcommands.append(cmd) |
| 1194 |
def cmd(): |
| 1195 |
pygame.draw.rect(self.screen, fgcolor, rect, 1) |
| 1196 |
commands.append(cmd) |
| 1197 |
|
| 1198 |
elif node.shape == b'ellipse': |
| 1199 |
rect = (x-1, y-1, boxwidth+2, boxheight+2) |
| 1200 |
def cmd(): |
| 1201 |
pygame.draw.ellipse(self.screen, bgcolor, rect, 0) |
| 1202 |
bkgndcommands.append(cmd) |
| 1203 |
def cmd(): |
| 1204 |
pygame.draw.ellipse(self.screen, fgcolor, rect, 1) |
| 1205 |
commands.append(cmd) |
| 1206 |
elif node.shape == b'circle': |
| 1207 |
radius = max(boxwidth+2, boxheight+2) // 2 |
| 1208 |
def cmd(): |
| 1209 |
pygame.draw.circle(self.screen, bgcolor, center, radius, 0) |
| 1210 |
bkgndcommands.append(cmd) |
| 1211 |
def cmd(): |
| 1212 |
pygame.draw.circle(self.screen, fgcolor, center, radius, 1) |
| 1213 |
commands.append(cmd) |
| 1214 |
elif node.shape == b'doublecircle': |
| 1215 |
radius = max(boxwidth+2, boxheight+2) // 2 |
| 1216 |
bigradius = int(radius * 1.05) |
| 1217 |
def cmd(): |
| 1218 |
pygame.draw.circle(self.screen, bgcolor, center, bigradius, 0) |
| 1219 |
bkgndcommands.append(cmd) |
| 1220 |
def cmd(): |
| 1221 |
pygame.draw.circle(self.screen, fgcolor, center, radius, 1) |
| 1222 |
pygame.draw.circle(self.screen, fgcolor, center, bigradius, 1) |
| 1223 |
commands.append(cmd) |
| 1224 |
elif node.shape == b'octagon': |
| 1225 |
step = 1-math.sqrt(2)/2 |
| 1226 |
points = [(int(x+boxwidth*fx), int(y+boxheight*fy)) |
| 1227 |
for fx, fy in [(step,0), (1-step,0), |
| 1228 |
(1,step), (1,1-step), |
| 1229 |
(1-step,1), (step,1), |
| 1230 |
(0,1-step), (0,step)]] |
| 1231 |
def cmd(): |
| 1232 |
pygame.draw.polygon(self.screen, bgcolor, points, 0) |
| 1233 |
bkgndcommands.append(cmd) |
| 1234 |
def cmd(): |
| 1235 |
pygame.draw.polygon(self.screen, fgcolor, points, 1) |
| 1236 |
commands.append(cmd) |
| 1237 |
elif node.shape == b'doubleoctagon' or node.shape == b'tripleoctagon': |
| 1238 |
radius = max(boxwidth+2, boxheight+2) |
| 1239 |
|
| 1240 |
width = int(radius * 1.05) - radius |
| 1241 |
count = 8 |
| 1242 |
points = [ellipse((2 * math.pi) / float(count) * (i + 0.5), |
| 1243 |
center, boxwidth, boxheight) |
| 1244 |
for i in range(count)] |
| 1245 |
points2 = [ellipse((2 * math.pi) / float(count) * (i + 0.5), |
| 1246 |
center, boxwidth + width, boxheight + width) |
| 1247 |
for i in range(count)] |
| 1248 |
points3 = [ellipse((2 * math.pi) / float(count) * (i + 0.5), |
| 1249 |
center, boxwidth + 2 * width, boxheight + 2 * width) |
| 1250 |
for i in range(count)] |
| 1251 |
if node.shape != b'tripleoctagon': |
| 1252 |
points3 = points2 |
| 1253 |
def cmd(): |
| 1254 |
pygame.draw.polygon(self.screen, bgcolor, points3, 0) |
| 1255 |
bkgndcommands.append(cmd) |
| 1256 |
def cmd(): |
| 1257 |
pygame.draw.polygon(self.screen, fgcolor, points, 1) |
| 1258 |
pygame.draw.polygon(self.screen, fgcolor, points2, 1) |
| 1259 |
pygame.draw.polygon(self.screen, fgcolor, points3, 1) |
| 1260 |
commands.append(cmd) |
| 1261 |
elif node.shape in GONS: |
| 1262 |
count = GONS[node.shape] |
| 1263 |
points = [ellipse((2 * math.pi) / float(count) * i, center, boxwidth, boxheight) |
| 1264 |
for i in range(count)] |
| 1265 |
def cmd(): |
| 1266 |
pygame.draw.polygon(self.screen, bgcolor, points, 0) |
| 1267 |
bkgndcommands.append(cmd) |
| 1268 |
def cmd(): |
| 1269 |
pygame.draw.polygon(self.screen, fgcolor, points, 1) |
| 1270 |
commands.append(cmd) |
| 1271 |
|
| 1272 |
elif node.shape in (b'none', b'plain', b'plaintext'): |
| 1273 |
pass |
| 1274 |
return bkgndcommands, commands |
| 1275 |
|
| 1276 |
def draw_commands(self): |
| 1277 |
nodebkgndcmd = [] |
| 1278 |
nodecmd = [] |
| 1279 |
for node in self.visiblenodes: |
| 1280 |
cmd1, cmd2 = self.draw_node_commands(node) |
| 1281 |
nodebkgndcmd += cmd1 |
| 1282 |
nodecmd += cmd2 |
| 1283 |
|
| 1284 |
edgebodycmd = [] |
| 1285 |
edgeheadcmd = [] |
| 1286 |
for edge in self.visibleedges: |
| 1287 |
|
| 1288 |
fgcolor = getcolor(edge.color, (0,0,0)) |
| 1289 |
if edge.highlight: |
| 1290 |
fgcolor = highlight_color(fgcolor) |
| 1291 |
points = [self.map(*xy) for xy in edge.bezierpoints()] |
| 1292 |
|
| 1293 |
def drawedgebody(points=points, fgcolor=fgcolor, width=edge.linewidth): |
| 1294 |
pygame.draw.lines(self.screen, fgcolor, False, points, width) |
| 1295 |
edgebodycmd.append(drawedgebody) |
| 1296 |
|
| 1297 |
points = [self.map(*xy) for xy in edge.arrowhead()] |
| 1298 |
if points: |
| 1299 |
def drawedgehead(points=points, fgcolor=fgcolor): |
| 1300 |
pygame.draw.polygon(self.screen, fgcolor, points, 0) |
| 1301 |
edgeheadcmd.append(drawedgehead) |
| 1302 |
|
| 1303 |
if edge.label: |
| 1304 |
x, y = self.map(edge.xl, edge.yl) |
| 1305 |
img = TextSnippet(self, edge.label, (0, 0, 0)) |
| 1306 |
w, h = img.get_size() |
| 1307 |
if self.visible(x-w//2, y-h//2, x+w//2, y+h//2): |
| 1308 |
def drawedgelabel(img=img, x1=x-w//2, y1=y-h//2): |
| 1309 |
img.draw(x1, y1) |
| 1310 |
edgeheadcmd.append(drawedgelabel) |
| 1311 |
|
| 1312 |
return edgebodycmd + nodebkgndcmd + edgeheadcmd + nodecmd |
| 1313 |
|
| 1314 |
def draw_record_commands(self, node): |
| 1315 |
xcenter, ycenter = self.map(node.x, node.y) |
| 1316 |
boxwidth = int(node.w * self.scale) |
| 1317 |
boxheight = int(node.h * self.scale) |
| 1318 |
fgcolor = getcolor(node.color, (0,0,0)) |
| 1319 |
bgcolor = getcolor(node.fillcolor, (255,255,255)) |
| 1320 |
if node.highlight: |
| 1321 |
fgcolor = highlight_color(fgcolor) |
| 1322 |
bgcolor = highlight_color(bgcolor) |
| 1323 |
|
| 1324 |
text = node.label |
| 1325 |
|
| 1326 |
img = TextSnippetRecordLR.make(self, text, (boxwidth, boxheight), fgcolor, bgcolor) |
| 1327 |
wmax, hmax = img.get_size() |
| 1328 |
xleft = xcenter - boxwidth//2 |
| 1329 |
ytop = ycenter - boxheight//2 |
| 1330 |
def cmd(img=img, y=0): |
| 1331 |
img.draw(xleft, ytop+y) |
| 1332 |
commands = [cmd] |
| 1333 |
bkgndcommands = [] |
| 1334 |
|
| 1335 |
x = xcenter-boxwidth//2 |
| 1336 |
y = ycenter-boxheight//2 |
| 1337 |
|
| 1338 |
|
| 1339 |
rect = (x-1, y-1, boxwidth+2, boxheight+2) |
| 1340 |
if rect[0] < 0: |
| 1341 |
rect = (0, y-1, boxwidth+2 + x-1, boxheight+2) |
| 1342 |
if rect[1] < 0: |
| 1343 |
rect = (rect[0], 0, rect[2], boxheight+2 + y-1) |
| 1344 |
def cmd(): |
| 1345 |
self.screen.fill(bgcolor, rect) |
| 1346 |
bkgndcommands.append(cmd) |
| 1347 |
def cmd(): |
| 1348 |
pygame.draw.rect(self.screen, fgcolor, rect, 1) |
| 1349 |
commands.append(cmd) |
| 1350 |
return bkgndcommands, commands |
| 1351 |
|
| 1352 |
def render(self): |
| 1353 |
self.computevisible() |
| 1354 |
|
| 1355 |
bbox = self.getboundingbox() |
| 1356 |
ox, oy, width, height = bbox |
| 1357 |
dpy_width, dpy_height = self.screen.get_size() |
| 1358 |
|
| 1359 |
|
| 1360 |
if ox < 0: |
| 1361 |
width += ox |
| 1362 |
ox = 0 |
| 1363 |
if oy < 0: |
| 1364 |
height += oy |
| 1365 |
oy = 0 |
| 1366 |
if width > dpy_width: |
| 1367 |
width = dpy_width |
| 1368 |
if height > dpy_height: |
| 1369 |
height = dpy_height |
| 1370 |
self.screen.fill((224, 255, 224), (ox, oy, width, height)) |
| 1371 |
|
| 1372 |
|
| 1373 |
gray = (128, 128, 128) |
| 1374 |
if ox > 0: |
| 1375 |
self.screen.fill(gray, (0, 0, ox, dpy_height)) |
| 1376 |
if oy > 0: |
| 1377 |
self.screen.fill(gray, (0, 0, dpy_width, oy)) |
| 1378 |
w = dpy_width - (ox + width) |
| 1379 |
if w > 0: |
| 1380 |
self.screen.fill(gray, (dpy_width-w, 0, w, dpy_height)) |
| 1381 |
h = dpy_height - (oy + height) |
| 1382 |
if h > 0: |
| 1383 |
self.screen.fill(gray, (0, dpy_height-h, dpy_width, h)) |
| 1384 |
|
| 1385 |
|
| 1386 |
del self.textzones[:] |
| 1387 |
for cmd in self.draw_commands(): |
| 1388 |
cmd() |
| 1389 |
|
| 1390 |
def findall(self, searchstr): |
| 1391 |
"""Return an iterator for all nodes and edges that contain a searchstr. |
| 1392 |
""" |
| 1393 |
for item in self.graphlayout.nodes.values(): |
| 1394 |
if item.label and searchstr in item.label: |
| 1395 |
yield item |
| 1396 |
for item in self.graphlayout.edges: |
| 1397 |
if item.label and searchstr in item.label: |
| 1398 |
yield item |
| 1399 |
|
| 1400 |
def at_position(self, p): |
| 1401 |
"""Figure out the word under the cursor.""" |
| 1402 |
x, y = p |
| 1403 |
for rx, ry, rw, rh, word in self.textzones: |
| 1404 |
if rx <= x < rx+rw and ry <= y < ry+rh: |
| 1405 |
return word |
| 1406 |
return None |
| 1407 |
|
| 1408 |
def node_at_position(self, p): |
| 1409 |
"""Return the Node under the cursor.""" |
| 1410 |
x, y = p |
| 1411 |
x, y = self.revmap(x, y) |
| 1412 |
for node in self.visiblenodes: |
| 1413 |
if 2.0*abs(x-node.x) <= node.w and 2.0*abs(y-node.y) <= node.h: |
| 1414 |
return node |
| 1415 |
return None |
| 1416 |
|
| 1417 |
def edge_at_position(self, p, distmax=14): |
| 1418 |
"""Return the Edge near the cursor.""" |
| 1419 |
x, y = p |
| 1420 |
|
| 1421 |
distmax /= self.scale |
| 1422 |
xy = self.revmap(x, y) |
| 1423 |
closest_edge = None |
| 1424 |
for edge in self.visibleedges: |
| 1425 |
pts = edge.bezierpoints() |
| 1426 |
for i in range(1, len(pts)): |
| 1427 |
d = segmentdistance(pts[i-1], pts[i], xy) |
| 1428 |
if d < distmax: |
| 1429 |
distmax = d |
| 1430 |
closest_edge = edge |
| 1431 |
return closest_edge |
| 1432 |
|
| 1433 |
|
| 1434 |
class TextSnippet: |
| 1435 |
|
| 1436 |
def __init__(self, renderer, text, fgcolor, bgcolor=None, font=None): |
| 1437 |
fgcolor = ensure_readable(fgcolor, bgcolor) |
| 1438 |
self.renderer = renderer |
| 1439 |
self.imgs = [] |
| 1440 |
self.parts = [] |
| 1441 |
if font is None: |
| 1442 |
font = renderer.font |
| 1443 |
if font is None: |
| 1444 |
return |
| 1445 |
parts = self.parts |
| 1446 |
for word in re_nonword.split(text): |
| 1447 |
if not word: |
| 1448 |
continue |
| 1449 |
if word in renderer.highlightwords: |
| 1450 |
fg, bg = renderer.wordcolor(word) |
| 1451 |
bg = bg or bgcolor |
| 1452 |
else: |
| 1453 |
fg, bg = fgcolor, bgcolor |
| 1454 |
parts.append((word, fg, bg)) |
| 1455 |
|
| 1456 |
for i in range(len(parts)-2, -1, -1): |
| 1457 |
if parts[i][1:] == parts[i+1][1:]: |
| 1458 |
word, fg, bg = parts[i] |
| 1459 |
parts[i] = word + parts[i+1][0], fg, bg |
| 1460 |
del parts[i+1] |
| 1461 |
|
| 1462 |
for i in range(len(parts)): |
| 1463 |
if parts[i][2] is None: |
| 1464 |
parts[i] = parts[i][:2] |
| 1465 |
|
| 1466 |
i = 0 |
| 1467 |
while i < len(parts): |
| 1468 |
part = parts[i] |
| 1469 |
word = part[0] |
| 1470 |
try: |
| 1471 |
img = font.render(word, True, *part[1:]) |
| 1472 |
except pygame.error: |
| 1473 |
del parts[i] |
| 1474 |
else: |
| 1475 |
self.imgs.append(img) |
| 1476 |
i += 1 |
| 1477 |
|
| 1478 |
def get_size(self): |
| 1479 |
if self.imgs: |
| 1480 |
sizes = [img.get_size() for img in self.imgs] |
| 1481 |
return sum([w for w,h in sizes]), max([h for w,h in sizes]) |
| 1482 |
else: |
| 1483 |
return 0, 0 |
| 1484 |
|
| 1485 |
def draw(self, x, y): |
| 1486 |
for part, img in zip(self.parts, self.imgs): |
| 1487 |
word = part[0] |
| 1488 |
self.renderer.screen.blit(img, (x, y)) |
| 1489 |
w, h = img.get_size() |
| 1490 |
self.renderer.textzones.append((x, y, w, h, word)) |
| 1491 |
x += w |
| 1492 |
|
| 1493 |
|
| 1494 |
def record_to_nested_lists(s): |
| 1495 |
try: |
| 1496 |
|
| 1497 |
from HTMLParser import HTMLParser |
| 1498 |
unescape = HTMLParser().unescape |
| 1499 |
except ImportError: |
| 1500 |
|
| 1501 |
from html import unescape |
| 1502 |
def delimit_string(): |
| 1503 |
curr.append(unescape("".join(curr_string).strip("\n"))) |
| 1504 |
del curr_string[:] |
| 1505 |
curr = [] |
| 1506 |
stack = [] |
| 1507 |
curr_string = [] |
| 1508 |
index = 0 |
| 1509 |
prev = '' |
| 1510 |
while index < len(s): |
| 1511 |
char = s[index] |
| 1512 |
if char == "|": |
| 1513 |
if prev != '}': |
| 1514 |
delimit_string() |
| 1515 |
elif char == "{": |
| 1516 |
if prev == "\\": |
| 1517 |
curr_string[-1] = "{" |
| 1518 |
else: |
| 1519 |
stack.append(curr) |
| 1520 |
curr = [] |
| 1521 |
elif char == "}": |
| 1522 |
if prev == "\\": |
| 1523 |
curr_string[-1] = "}" |
| 1524 |
else: |
| 1525 |
delimit_string() |
| 1526 |
stack[-1].append(curr) |
| 1527 |
curr = stack.pop() |
| 1528 |
elif char == "<": |
| 1529 |
index = s.find(">", index) + 1 |
| 1530 |
if index == 0: |
| 1531 |
assert 0 |
| 1532 |
else: |
| 1533 |
prev = ">" |
| 1534 |
continue |
| 1535 |
else: |
| 1536 |
curr_string.append(char) |
| 1537 |
index += 1 |
| 1538 |
prev = char |
| 1539 |
if curr_string or prev == "|": |
| 1540 |
delimit_string() |
| 1541 |
assert not stack |
| 1542 |
return curr |
| 1543 |
|
| 1544 |
|
| 1545 |
class TextSnippetRecordLR: |
| 1546 |
def __init__(self, renderer, snippets, fgcolor, bgcolor=None, font=None): |
| 1547 |
if font is None: |
| 1548 |
font = renderer.font |
| 1549 |
self.snippets = snippets |
| 1550 |
self.renderer = renderer |
| 1551 |
self.fgcolor = fgcolor |
| 1552 |
|
| 1553 |
@staticmethod |
| 1554 |
def make(renderer, s, bbox, fgcolor, bgcolor=None, font=None): |
| 1555 |
fgcolor = ensure_readable(fgcolor, bgcolor) |
| 1556 |
l = record_to_nested_lists(s) |
| 1557 |
res = TextSnippetRecordLR.make_from_list(renderer, l, fgcolor, bgcolor, font) |
| 1558 |
res._compute_offsets(bbox) |
| 1559 |
return res |
| 1560 |
|
| 1561 |
@classmethod |
| 1562 |
def make_from_list(cls, renderer, l, fgcolor, bgcolor, font): |
| 1563 |
snippets = [] |
| 1564 |
for element in l: |
| 1565 |
if isinstance(element, list): |
| 1566 |
snippets.append(cls.othercls.make_from_list(renderer, element, fgcolor, bgcolor, font)) |
| 1567 |
else: |
| 1568 |
snippets.append(TextSnippet(renderer, element, fgcolor, bgcolor, font)) |
| 1569 |
return cls(renderer, snippets, fgcolor, bgcolor, font) |
| 1570 |
|
| 1571 |
def get_size(self): |
| 1572 |
if self.snippets: |
| 1573 |
sizes = [img.get_size() for img in self.snippets] |
| 1574 |
return sum([w for w,h in sizes]), max([h for w,h in sizes]) |
| 1575 |
else: |
| 1576 |
return 0, 0 |
| 1577 |
|
| 1578 |
def draw(self, x, y): |
| 1579 |
for i, img in enumerate(self.snippets): |
| 1580 |
w, h = img.get_size() |
| 1581 |
if not isinstance(img, TextSnippetRecordLR): |
| 1582 |
img.draw(x + self.offsets[i] + self.spacing, y + (self.bbox[1] - h) // 2) |
| 1583 |
else: |
| 1584 |
img.draw(x + self.offsets[i], y) |
| 1585 |
if i != 0: |
| 1586 |
start = x + self.offsets[i], y |
| 1587 |
end = x + self.offsets[i], y + self.bbox[1] |
| 1588 |
pygame.draw.line(self.renderer.screen, self.fgcolor, start, end) |
| 1589 |
|
| 1590 |
def _compute_offsets(self, bbox): |
| 1591 |
self.bbox = bbox |
| 1592 |
width_content, height_content = self.get_size() |
| 1593 |
width, height = bbox |
| 1594 |
spacing = (width - width_content) / 2.0 / len(self.snippets) |
| 1595 |
offset = 0 |
| 1596 |
offsets = [] |
| 1597 |
for i, img in enumerate(self.snippets): |
| 1598 |
w, h = img.get_size() |
| 1599 |
offsets.append(int(offset)) |
| 1600 |
offset += w + 2 * spacing |
| 1601 |
if not isinstance(img, TextSnippet): |
| 1602 |
img._compute_offsets((w + 2 * spacing, height)) |
| 1603 |
self.offsets = offsets |
| 1604 |
self.spacing = spacing |
| 1605 |
|
| 1606 |
class TextSnippetRecordTD(TextSnippetRecordLR): |
| 1607 |
othercls = TextSnippetRecordLR |
| 1608 |
|
| 1609 |
def get_size(self): |
| 1610 |
if self.snippets: |
| 1611 |
sizes = [img.get_size() for img in self.snippets] |
| 1612 |
return max([w for w,h in sizes]), sum([h for w,h in sizes]) |
| 1613 |
else: |
| 1614 |
return 0, 0 |
| 1615 |
|
| 1616 |
def draw(self, x, y): |
| 1617 |
for i, img in enumerate(self.snippets): |
| 1618 |
w, h = img.get_size() |
| 1619 |
if not isinstance(img, TextSnippetRecordLR): |
| 1620 |
img.draw(x + (self.bbox[0] - w) // 2, y + self.offsets[i] + self.spacing) |
| 1621 |
else: |
| 1622 |
img.draw(x, y + self.offsets[i]) |
| 1623 |
if i != 0: |
| 1624 |
start = x, y + self.offsets[i] |
| 1625 |
end = x + self.bbox[0], y + self.offsets[i] |
| 1626 |
pygame.draw.line(self.renderer.screen, self.fgcolor, start, end) |
| 1627 |
|
| 1628 |
def _compute_offsets(self, bbox): |
| 1629 |
self.bbox = bbox |
| 1630 |
width_content, height_content = self.get_size() |
| 1631 |
width, height = bbox |
| 1632 |
spacing = (height - height_content) / 2.0 / len(self.snippets) |
| 1633 |
offset = 0 |
| 1634 |
offsets = [] |
| 1635 |
for i, img in enumerate(self.snippets): |
| 1636 |
w, h = img.get_size() |
| 1637 |
offsets.append(int(offset)) |
| 1638 |
offset += h + 2 * spacing |
| 1639 |
if not isinstance(img, TextSnippet): |
| 1640 |
img._compute_offsets((width, h + 2 * spacing)) |
| 1641 |
self.offsets = offsets |
| 1642 |
self.spacing = spacing |
| 1643 |
|
| 1644 |
|
| 1645 |
TextSnippetRecordLR.othercls = TextSnippetRecordTD |