CMS 3D CMS Logo

List of all members | Public Member Functions | Public Attributes | Static Public Attributes
svgfig.Path Class Reference

Public Member Functions

def __init__ (self, d=[], attr)
 
def __repr__ (self)
 
def parse (self, pathdata)
 
def parse_boolean (self, index, pathdata)
 
def parse_command (self, index, pathdata)
 
def parse_number (self, index, pathdata)
 
def parse_whitespace (self, index, pathdata)
 
def SVG (self, trans=None)
 

Public Attributes

 attr
 
 d
 

Static Public Attributes

dictionary defaults = {}
 

Detailed Description

Path represents an SVG path, an arbitrary set of curves and
straight segments. Unlike SVG("path", d="..."), Path stores
coordinates as a list of numbers, rather than a string, so that it is
transformable in a Fig.

Path(d, attribute=value)

d                       required        path data
attribute=value pairs   keyword list    SVG attributes

See http://www.w3.org/TR/SVG/paths.html for specification of paths
from text.

Internally, Path data is a list of tuples with these definitions:

    * ("Z/z",): close the current path
    * ("H/h", x) or ("V/v", y): a horizontal or vertical line
      segment to x or y
    * ("M/m/L/l/T/t", x, y, global): moveto, lineto, or smooth
      quadratic curveto point (x, y). If global=True, (x, y) should
      not be transformed.
    * ("S/sQ/q", cx, cy, cglobal, x, y, global): polybezier or
      smooth quadratic curveto point (x, y) using (cx, cy) as a
      control point. If cglobal or global=True, (cx, cy) or (x, y)
      should not be transformed.
    * ("C/c", c1x, c1y, c1global, c2x, c2y, c2global, x, y, global):
      cubic curveto point (x, y) using (c1x, c1y) and (c2x, c2y) as
      control points. If c1global, c2global, or global=True, (c1x, c1y),
      (c2x, c2y), or (x, y) should not be transformed.
    * ("A/a", rx, ry, rglobal, x-axis-rotation, angle, large-arc-flag,
      sweep-flag, x, y, global): arcto point (x, y) using the
      aforementioned parameters.
    * (",/.", rx, ry, rglobal, angle, x, y, global): an ellipse at
      point (x, y) with radii (rx, ry). If angle is 0, the whole
      ellipse is drawn; otherwise, a partial ellipse is drawn.

Definition at line 1020 of file svgfig.py.

Constructor & Destructor Documentation

def svgfig.Path.__init__ (   self,
  d = [],
  attr 
)

Definition at line 1062 of file svgfig.py.

1062  def __init__(self, d=[], **attr):
1063  if isinstance(d, basestring): self.d = self.parse(d)
1064  else: self.d = list(d)
1065 
1066  self.attr = dict(self.defaults)
1067  self.attr.update(attr)
1068 
dictionary defaults
Definition: svgfig.py:1057
def parse(self, pathdata)
Definition: svgfig.py:1117
def __init__(self, d=[], attr)
Definition: svgfig.py:1062
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger list("!*","!HLTx*"if it matches 2 triggers or more) will accept the event if all the matching triggers are FAIL.It will reject the event if any of the triggers are PASS or EXCEPTION(this matches the behavior of"!*"before the partial wildcard feature was incorporated).Triggers which are in the READY state are completely ignored.(READY should never be returned since the trigger paths have been run

Member Function Documentation

def svgfig.Path.__repr__ (   self)

Definition at line 1059 of file svgfig.py.

References svgfig.SVG.attr, svgfig.Path.attr, svgfig.Fig.d, svgfig.Plot.d, svgfig.Frame.d, and svgfig.Path.d.

Referenced by data_sources.json_file.__str__().

1059  def __repr__(self):
1060  return "<Path (%d nodes) %s>" % (len(self.d), self.attr)
1061 
def __repr__(self)
Definition: svgfig.py:1059
def svgfig.Path.parse (   self,
  pathdata 
)
Parses text-commands, converting them into a list of tuples.
Called by the constructor.

Definition at line 1117 of file svgfig.py.

References svgfig.Path.parse_boolean(), svgfig.Path.parse_command(), svgfig.Path.parse_number(), and svgfig.Path.parse_whitespace().

1117  def parse(self, pathdata):
1118  """Parses text-commands, converting them into a list of tuples.
1119  Called by the constructor."""
1120  output = []
1121  index = 0
1122  while True:
1123  command, index, pathdata = self.parse_command(index, pathdata)
1124  index, pathdata = self.parse_whitespace(index, pathdata)
1125 
1126  if command == None and index == len(pathdata): break # this is the normal way out of the loop
1127  if command in ("Z", "z"):
1128  output.append((command,))
1129 
1130  ######################
1131  elif command in ("H", "h", "V", "v"):
1132  errstring = "Path command \"%s\" requires a number at index %d" % (command, index)
1133  num1, index, pathdata = self.parse_number(index, pathdata)
1134  if num1 == None: raise ValueError(errstring)
1135 
1136  while num1 != None:
1137  output.append((command, num1))
1138  num1, index, pathdata = self.parse_number(index, pathdata)
1139 
1140  ######################
1141  elif command in ("M", "m", "L", "l", "T", "t"):
1142  errstring = "Path command \"%s\" requires an x,y pair at index %d" % (command, index)
1143  num1, index, pathdata = self.parse_number(index, pathdata)
1144  num2, index, pathdata = self.parse_number(index, pathdata)
1145 
1146  if num1 == None: raise ValueError(errstring)
1147 
1148  while num1 != None:
1149  if num2 == None: raise ValueError(errstring)
1150  output.append((command, num1, num2, False))
1151 
1152  num1, index, pathdata = self.parse_number(index, pathdata)
1153  num2, index, pathdata = self.parse_number(index, pathdata)
1154 
1155  ######################
1156  elif command in ("S", "s", "Q", "q"):
1157  errstring = "Path command \"%s\" requires a cx,cy,x,y quadruplet at index %d" % (command, index)
1158  num1, index, pathdata = self.parse_number(index, pathdata)
1159  num2, index, pathdata = self.parse_number(index, pathdata)
1160  num3, index, pathdata = self.parse_number(index, pathdata)
1161  num4, index, pathdata = self.parse_number(index, pathdata)
1162 
1163  if num1 == None: raise ValueError(errstring)
1164 
1165  while num1 != None:
1166  if num2 == None or num3 == None or num4 == None: raise ValueError(errstring)
1167  output.append((command, num1, num2, False, num3, num4, False))
1168 
1169  num1, index, pathdata = self.parse_number(index, pathdata)
1170  num2, index, pathdata = self.parse_number(index, pathdata)
1171  num3, index, pathdata = self.parse_number(index, pathdata)
1172  num4, index, pathdata = self.parse_number(index, pathdata)
1173 
1174  ######################
1175  elif command in ("C", "c"):
1176  errstring = "Path command \"%s\" requires a c1x,c1y,c2x,c2y,x,y sextuplet at index %d" % (command, index)
1177  num1, index, pathdata = self.parse_number(index, pathdata)
1178  num2, index, pathdata = self.parse_number(index, pathdata)
1179  num3, index, pathdata = self.parse_number(index, pathdata)
1180  num4, index, pathdata = self.parse_number(index, pathdata)
1181  num5, index, pathdata = self.parse_number(index, pathdata)
1182  num6, index, pathdata = self.parse_number(index, pathdata)
1183 
1184  if num1 == None: raise ValueError(errstring)
1185 
1186  while num1 != None:
1187  if num2 == None or num3 == None or num4 == None or num5 == None or num6 == None: raise ValueError(errstring)
1188 
1189  output.append((command, num1, num2, False, num3, num4, False, num5, num6, False))
1190 
1191  num1, index, pathdata = self.parse_number(index, pathdata)
1192  num2, index, pathdata = self.parse_number(index, pathdata)
1193  num3, index, pathdata = self.parse_number(index, pathdata)
1194  num4, index, pathdata = self.parse_number(index, pathdata)
1195  num5, index, pathdata = self.parse_number(index, pathdata)
1196  num6, index, pathdata = self.parse_number(index, pathdata)
1197 
1198  ######################
1199  elif command in ("A", "a"):
1200  errstring = "Path command \"%s\" requires a rx,ry,angle,large-arc-flag,sweep-flag,x,y septuplet at index %d" % (command, index)
1201  num1, index, pathdata = self.parse_number(index, pathdata)
1202  num2, index, pathdata = self.parse_number(index, pathdata)
1203  num3, index, pathdata = self.parse_number(index, pathdata)
1204  num4, index, pathdata = self.parse_boolean(index, pathdata)
1205  num5, index, pathdata = self.parse_boolean(index, pathdata)
1206  num6, index, pathdata = self.parse_number(index, pathdata)
1207  num7, index, pathdata = self.parse_number(index, pathdata)
1208 
1209  if num1 == None: raise ValueError(errstring)
1210 
1211  while num1 != None:
1212  if num2 == None or num3 == None or num4 == None or num5 == None or num6 == None or num7 == None: raise ValueError(errstring)
1213 
1214  output.append((command, num1, num2, False, num3, num4, num5, num6, num7, False))
1215 
1216  num1, index, pathdata = self.parse_number(index, pathdata)
1217  num2, index, pathdata = self.parse_number(index, pathdata)
1218  num3, index, pathdata = self.parse_number(index, pathdata)
1219  num4, index, pathdata = self.parse_boolean(index, pathdata)
1220  num5, index, pathdata = self.parse_boolean(index, pathdata)
1221  num6, index, pathdata = self.parse_number(index, pathdata)
1222  num7, index, pathdata = self.parse_number(index, pathdata)
1223 
1224  return output
1225 
def parse_number(self, index, pathdata)
Definition: svgfig.py:1086
def parse(self, pathdata)
Definition: svgfig.py:1117
def parse_boolean(self, index, pathdata)
Definition: svgfig.py:1104
def parse_whitespace(self, index, pathdata)
Definition: svgfig.py:1069
def parse_command(self, index, pathdata)
Definition: svgfig.py:1074
def svgfig.Path.parse_boolean (   self,
  index,
  pathdata 
)
Part of Path's text-command parsing algorithm; used internally.

Definition at line 1104 of file svgfig.py.

References createfilelist.int, and svgfig.Path.parse_whitespace().

Referenced by svgfig.Path.parse().

1104  def parse_boolean(self, index, pathdata):
1105  """Part of Path's text-command parsing algorithm; used internally."""
1106  index, pathdata = self.parse_whitespace(index, pathdata)
1107 
1108  if index >= len(pathdata): return None, index, pathdata
1109  first_digit = pathdata[index]
1110 
1111  if first_digit in ("0", "1"):
1112  index += 1
1113  return int(first_digit), index, pathdata
1114  else:
1115  return None, index, pathdata
1116 
def parse_boolean(self, index, pathdata)
Definition: svgfig.py:1104
def parse_whitespace(self, index, pathdata)
Definition: svgfig.py:1069
def svgfig.Path.parse_command (   self,
  index,
  pathdata 
)
Part of Path's text-command parsing algorithm; used internally.

Definition at line 1074 of file svgfig.py.

References svgfig.Path.parse_whitespace().

Referenced by svgfig.Path.parse().

1074  def parse_command(self, index, pathdata):
1075  """Part of Path's text-command parsing algorithm; used internally."""
1076  index, pathdata = self.parse_whitespace(index, pathdata)
1077 
1078  if index >= len(pathdata): return None, index, pathdata
1079  command = pathdata[index]
1080  if "A" <= command <= "Z" or "a" <= command <= "z":
1081  index += 1
1082  return command, index, pathdata
1083  else:
1084  return None, index, pathdata
1085 
def parse_whitespace(self, index, pathdata)
Definition: svgfig.py:1069
def parse_command(self, index, pathdata)
Definition: svgfig.py:1074
def svgfig.Path.parse_number (   self,
  index,
  pathdata 
)
Part of Path's text-command parsing algorithm; used internally.

Definition at line 1086 of file svgfig.py.

References objects.autophobj.float, and svgfig.Path.parse_whitespace().

Referenced by svgfig.Path.parse().

1086  def parse_number(self, index, pathdata):
1087  """Part of Path's text-command parsing algorithm; used internally."""
1088  index, pathdata = self.parse_whitespace(index, pathdata)
1089 
1090  if index >= len(pathdata): return None, index, pathdata
1091  first_digit = pathdata[index]
1092 
1093  if "0" <= first_digit <= "9" or first_digit in ("-", "+", "."):
1094  start = index
1095  while index < len(pathdata) and ("0" <= pathdata[index] <= "9" or pathdata[index] in ("-", "+", ".", "e", "E")):
1096  index += 1
1097  end = index
1098 
1099  index = end
1100  return float(pathdata[start:end]), index, pathdata
1101  else:
1102  return None, index, pathdata
1103 
def parse_number(self, index, pathdata)
Definition: svgfig.py:1086
def parse_whitespace(self, index, pathdata)
Definition: svgfig.py:1069
def svgfig.Path.parse_whitespace (   self,
  index,
  pathdata 
)
Part of Path's text-command parsing algorithm; used internally.

Definition at line 1069 of file svgfig.py.

Referenced by svgfig.Path.parse(), svgfig.Path.parse_boolean(), svgfig.Path.parse_command(), and svgfig.Path.parse_number().

1069  def parse_whitespace(self, index, pathdata):
1070  """Part of Path's text-command parsing algorithm; used internally."""
1071  while index < len(pathdata) and pathdata[index] in (" ", "\t", "\r", "\n", ","): index += 1
1072  return index, pathdata
1073 
def parse_whitespace(self, index, pathdata)
Definition: svgfig.py:1069
def svgfig.Path.SVG (   self,
  trans = None 
)
Apply the transformation "trans" and return an SVG object.

Definition at line 1226 of file svgfig.py.

References svgfig.SVG.attr, svgfig.Path.attr, svgfig.Fig.d, svgfig.Plot.d, svgfig.Frame.d, svgfig.Path.d, join(), and svgfig.totrans().

1226  def SVG(self, trans=None):
1227  """Apply the transformation "trans" and return an SVG object."""
1228  if isinstance(trans, basestring): trans = totrans(trans)
1229 
1230  x, y, X, Y = None, None, None, None
1231  output = []
1232  for datum in self.d:
1233  if not isinstance(datum, (tuple, list)):
1234  raise TypeError("pathdata elements must be tuples/lists")
1235 
1236  command = datum[0]
1237 
1238  ######################
1239  if command in ("Z", "z"):
1240  x, y, X, Y = None, None, None, None
1241  output.append("Z")
1242 
1243  ######################
1244  elif command in ("H", "h", "V", "v"):
1245  command, num1 = datum
1246 
1247  if command == "H" or (command == "h" and x == None): x = num1
1248  elif command == "h": x += num1
1249  elif command == "V" or (command == "v" and y == None): y = num1
1250  elif command == "v": y += num1
1251 
1252  if trans == None: X, Y = x, y
1253  else: X, Y = trans(x, y)
1254 
1255  output.append("L%g %g" % (X, Y))
1256 
1257  ######################
1258  elif command in ("M", "m", "L", "l", "T", "t"):
1259  command, num1, num2, isglobal12 = datum
1260 
1261  if trans == None or isglobal12:
1262  if command.isupper() or X == None or Y == None:
1263  X, Y = num1, num2
1264  else:
1265  X += num1
1266  Y += num2
1267  x, y = X, Y
1268 
1269  else:
1270  if command.isupper() or x == None or y == None:
1271  x, y = num1, num2
1272  else:
1273  x += num1
1274  y += num2
1275  X, Y = trans(x, y)
1276 
1277  COMMAND = command.capitalize()
1278  output.append("%s%g %g" % (COMMAND, X, Y))
1279 
1280  ######################
1281  elif command in ("S", "s", "Q", "q"):
1282  command, num1, num2, isglobal12, num3, num4, isglobal34 = datum
1283 
1284  if trans == None or isglobal12:
1285  if command.isupper() or X == None or Y == None:
1286  CX, CY = num1, num2
1287  else:
1288  CX = X + num1
1289  CY = Y + num2
1290 
1291  else:
1292  if command.isupper() or x == None or y == None:
1293  cx, cy = num1, num2
1294  else:
1295  cx = x + num1
1296  cy = y + num2
1297  CX, CY = trans(cx, cy)
1298 
1299  if trans == None or isglobal34:
1300  if command.isupper() or X == None or Y == None:
1301  X, Y = num3, num4
1302  else:
1303  X += num3
1304  Y += num4
1305  x, y = X, Y
1306 
1307  else:
1308  if command.isupper() or x == None or y == None:
1309  x, y = num3, num4
1310  else:
1311  x += num3
1312  y += num4
1313  X, Y = trans(x, y)
1314 
1315  COMMAND = command.capitalize()
1316  output.append("%s%g %g %g %g" % (COMMAND, CX, CY, X, Y))
1317 
1318  ######################
1319  elif command in ("C", "c"):
1320  command, num1, num2, isglobal12, num3, num4, isglobal34, num5, num6, isglobal56 = datum
1321 
1322  if trans == None or isglobal12:
1323  if command.isupper() or X == None or Y == None:
1324  C1X, C1Y = num1, num2
1325  else:
1326  C1X = X + num1
1327  C1Y = Y + num2
1328 
1329  else:
1330  if command.isupper() or x == None or y == None:
1331  c1x, c1y = num1, num2
1332  else:
1333  c1x = x + num1
1334  c1y = y + num2
1335  C1X, C1Y = trans(c1x, c1y)
1336 
1337  if trans == None or isglobal34:
1338  if command.isupper() or X == None or Y == None:
1339  C2X, C2Y = num3, num4
1340  else:
1341  C2X = X + num3
1342  C2Y = Y + num4
1343 
1344  else:
1345  if command.isupper() or x == None or y == None:
1346  c2x, c2y = num3, num4
1347  else:
1348  c2x = x + num3
1349  c2y = y + num4
1350  C2X, C2Y = trans(c2x, c2y)
1351 
1352  if trans == None or isglobal56:
1353  if command.isupper() or X == None or Y == None:
1354  X, Y = num5, num6
1355  else:
1356  X += num5
1357  Y += num6
1358  x, y = X, Y
1359 
1360  else:
1361  if command.isupper() or x == None or y == None:
1362  x, y = num5, num6
1363  else:
1364  x += num5
1365  y += num6
1366  X, Y = trans(x, y)
1367 
1368  COMMAND = command.capitalize()
1369  output.append("%s%g %g %g %g %g %g" % (COMMAND, C1X, C1Y, C2X, C2Y, X, Y))
1370 
1371  ######################
1372  elif command in ("A", "a"):
1373  command, num1, num2, isglobal12, angle, large_arc_flag, sweep_flag, num3, num4, isglobal34 = datum
1374 
1375  oldx, oldy = x, y
1376  OLDX, OLDY = X, Y
1377 
1378  if trans == None or isglobal34:
1379  if command.isupper() or X == None or Y == None:
1380  X, Y = num3, num4
1381  else:
1382  X += num3
1383  Y += num4
1384  x, y = X, Y
1385 
1386  else:
1387  if command.isupper() or x == None or y == None:
1388  x, y = num3, num4
1389  else:
1390  x += num3
1391  y += num4
1392  X, Y = trans(x, y)
1393 
1394  if x != None and y != None:
1395  centerx, centery = (x + oldx)/2., (y + oldy)/2.
1396  CENTERX, CENTERY = (X + OLDX)/2., (Y + OLDY)/2.
1397 
1398  if trans == None or isglobal12:
1399  RX = CENTERX + num1
1400  RY = CENTERY + num2
1401 
1402  else:
1403  rx = centerx + num1
1404  ry = centery + num2
1405  RX, RY = trans(rx, ry)
1406 
1407  COMMAND = command.capitalize()
1408  output.append("%s%g %g %g %d %d %g %g" % (COMMAND, RX - CENTERX, RY - CENTERY, angle, large_arc_flag, sweep_flag, X, Y))
1409 
1410  elif command in (",", "."):
1411  command, num1, num2, isglobal12, angle, num3, num4, isglobal34 = datum
1412  if trans == None or isglobal34:
1413  if command == "." or X == None or Y == None:
1414  X, Y = num3, num4
1415  else:
1416  X += num3
1417  Y += num4
1418  x, y = None, None
1419 
1420  else:
1421  if command == "." or x == None or y == None:
1422  x, y = num3, num4
1423  else:
1424  x += num3
1425  y += num4
1426  X, Y = trans(x, y)
1427 
1428  if trans == None or isglobal12:
1429  RX = X + num1
1430  RY = Y + num2
1431 
1432  else:
1433  rx = x + num1
1434  ry = y + num2
1435  RX, RY = trans(rx, ry)
1436 
1437  RX, RY = RX - X, RY - Y
1438 
1439  X1, Y1 = X + RX * math.cos(angle*math.pi/180.), Y + RX * math.sin(angle*math.pi/180.)
1440  X2, Y2 = X + RY * math.sin(angle*math.pi/180.), Y - RY * math.cos(angle*math.pi/180.)
1441  X3, Y3 = X - RX * math.cos(angle*math.pi/180.), Y - RX * math.sin(angle*math.pi/180.)
1442  X4, Y4 = X - RY * math.sin(angle*math.pi/180.), Y + RY * math.cos(angle*math.pi/180.)
1443 
1444  output.append("M%g %gA%g %g %g 0 0 %g %gA%g %g %g 0 0 %g %gA%g %g %g 0 0 %g %gA%g %g %g 0 0 %g %g" \
1445  % (X1, Y1, RX, RY, angle, X2, Y2, RX, RY, angle, X3, Y3, RX, RY, angle, X4, Y4, RX, RY, angle, X1, Y1))
1446 
1447  return SVG("path", d="".join(output), **self.attr)
1448 
def SVG(self, trans=None)
Definition: svgfig.py:1226
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def totrans(expr, vars=("x","y"), globals=None, locals=None)
Definition: svgfig.py:597

Member Data Documentation

svgfig.Path.attr
svgfig.Path.d
dictionary svgfig.Path.defaults = {}
static

Definition at line 1057 of file svgfig.py.

Referenced by tree.Tree.reset(), and tree.Tree.var().