     pin.y = y
     pin.length = pinLength
     pin.orientation = 'right'
-    if pin.name.length > leftTextWidth then leftTextWidth = pin.name.length
     leftPins.push pin
+
+    w = pin.name.length*settings.fontSize.pinName + space
+    y1 = y - dy
+    y2 = y + dy
+    if x > -w - space then x = -w - space
+    # Check whether pin rectangle intersects other rectangles
+    for r in rects
+      if ((y1 > r.y1) and (y1 < r.y2)) or ((y2 > r.y1) and (y2 < r.y2))
+        x1 = r.x1 - w - space
+        if x > x1 then x = x1 # Make symbol wider
     y += step
-  leftTextWidth *= settings.fontSize.pinName
+
+  leftX = Math.round x
 
   # Pins on the right side
+  x = width/2
   rightPins = []
-  rightTextWidth = 0
   if top.length > 0 # Center aligned symbol
     y = -step * right.length/2 + step/2
   else # Top aligned symbol

  height = step * (Math.max(left.length, right.length) + 2)
  space = settings.space.pinName

  # Pins on the left side
  leftPins = []
  leftTextWidth = 0
  if top.length > 0 # Center aligned symbol
    y = -step * left.length/2 + step/2
  else # Top aligned symbol

    pin.y = y
    pin.length = pinLength
    pin.orientation = 'left'
    rightPins.push pin

    w = pin.name.length*settings.fontSize.pinName + space
    y1 = y - dy
    y2 = y + dy
    if x < w + space then x = w + space
    # Check whether pin rectangle intersects other rectangles
    for r in rects
      if ((y1 > r.y1) and (y1 < r.y2)) or ((y2 > r.y1) and (y2 < r.y2))
        x2 = r.x2 + w + space
        if x < x2 then x = x2 # Make symbol wider
    y += step

  rightX = Math.round x

  # Update box size
  width = rightX - leftX
  height = bottomY - topY

  # Box
  y = 0
  if top.length > 0 then y = topY # Center aligned symbol
  symbol.addRectangle
    x: leftX
    y: y
    width: width
    height: height
    fill: 'foreground'

  for pin in leftPins
    pin.x = leftX - pinLength
    symbol.addPin pin

  for pin in rightPins
    pin.x = rightX + pinLength
    symbol.addPin pin

  for pin in topPins
    pin.y = topY - pinLength
    symbol.addPin pin

  for pin in bottomPins
    pin.y = bottomY + pinLength
    symbol.addPin pin
