(path, viewbox)
return new_path
Normalizes all args in a path to a standard 24x24 viewbox.
Normalizes all args in a path to a standard 24x24 viewbox.
Normalizes all args in a path to a standard 24x24 viewbox .
def normalize_based_on_viewbox(path, viewbox):
  """Normalizes all args in a path to a standard 24x24 viewbox."""
  # Each SVG lives in a 2D plane. The viewbox determines the region of that
  # plane that gets rendered. For instance, some designers may work with a
  # viewbox that's 24x24, others with one that's 100x100, etc.

  # Suppose I design the the letter "h" in the Arial style using a 100x100
  # viewbox (let's call it icon A). Let's suppose the icon has height 75. Then,
  # I design the same character using a 20x20 viewbox (call this icon B), with
  # height 15 (=75% of 20). This means that, when rendered, both icons with look
  # exactly the same, but the scale of the commands each icon is using is
  # different. For instance, if icon A has a command like "lineTo 100 100", the
  # equivalent command in icon B will be "lineTo 20 20".

  # In order to avoid this problem and bring all real values to the same scale,
  # I scale all icons' commands to use a 24x24 viewbox. This function does this:
  # it converts a path that exists in the given viewbox into a standard 24x24
  # viewbox.
  viewbox = viewbox.split(' ')
  norm = max(int(viewbox[-1]), int(viewbox[-2]))

  if int(viewbox[-1]) > int(viewbox[-2]):
    add_to_y = 0
    add_to_x = abs(int(viewbox[-1]) - int(viewbox[-2])) / 2
  else:
    add_to_y = abs(int(viewbox[-1]) - int(viewbox[-2])) / 2
    add_to_x = 0

  new_path = []
  for command in path:
    if command[0] == 'a':
      new_path.append([command[0]] +
                      _normalize_args(command[1:3], norm) +
                      command[3:6] +
                      _normalize_args(command[6:], norm))
    elif command[0] == 'A':
      new_path.append(
          [command[0]] +
          _normalize_args(command[1:3], norm) +
          command[3:6] +
          _normalize_args(command[6:], norm, add=(add_to_x, add_to_y)))
    elif command[0] == 'V':
      new_path.append(
          [command[0]] +
          _normalize_args(command[1:], norm, add=(add_to_x, add_to_y),
                          flip=True))
    elif command[0] == command[0].upper():
      new_path.append(
          [command[0]] +
          _normalize_args(command[1:], norm, add=(add_to_x, add_to_y)))
    elif command[0] in 'zZ':
      new_path.append([command[0]])
    else:
      new_path.append([command[0]] + _normalize_args(command[1:], norm))

  return new_path
def normalize_based_on_viewbox ( path , viewbox ) : # Each SVG lives in a 2D plane. The viewbox determines the region of that # plane that gets rendered. For instance, some designers may work with a # viewbox that's 24x24, others with one that's 100x100, etc. # Suppose I design the the letter "h" in the Arial style using a 100x100 # viewbox (let's call it icon A). Let's suppose the icon has height 75. Then, # I design the same character using a 20x20 viewbox (call this icon B), with # height 15 (=75% of 20). This means that, when rendered, both icons with look # exactly the same, but the scale of the commands each icon is using is # different. For instance, if icon A has a command like "lineTo 100 100", the # equivalent command in icon B will be "lineTo 20 20". # In order to avoid this problem and bring all real values to the same scale, # I scale all icons' commands to use a 24x24 viewbox. This function does this: # it converts a path that exists in the given viewbox into a standard 24x24 # viewbox. viewbox = viewbox . split ( ' ' ) norm = max ( int ( viewbox [ - 1 ] ) , int ( viewbox [ - 2 ] ) ) if int ( viewbox [ - 1 ] ) > int ( viewbox [ - 2 ] ) : add_to_y = 0 add_to_x = abs ( int ( viewbox [ - 1 ] ) - int ( viewbox [ - 2 ] ) ) / 2 else : add_to_y = abs ( int ( viewbox [ - 1 ] ) - int ( viewbox [ - 2 ] ) ) / 2 add_to_x = 0 new_path = [ ] for command in path : if command [ 0 ] == 'a' : new_path . append ( [ command [ 0 ] ] + _normalize_args ( command [ 1 : 3 ] , norm ) + command [ 3 : 6 ] + _normalize_args ( command [ 6 : ] , norm ) ) elif command [ 0 ] == 'A' : new_path . append ( [ command [ 0 ] ] + _normalize_args ( command [ 1 : 3 ] , norm ) + command [ 3 : 6 ] + _normalize_args ( command [ 6 : ] , norm , add = ( add_to_x , add_to_y ) ) ) elif command [ 0 ] == 'V' : new_path . append ( [ command [ 0 ] ] + _normalize_args ( command [ 1 : ] , norm , add = ( add_to_x , add_to_y ) , flip = True ) ) elif command [ 0 ] == command [ 0 ] . upper ( ) : new_path . append ( [ command [ 0 ] ] + _normalize_args ( command [ 1 : ] , norm , add = ( add_to_x , add_to_y ) ) ) elif command [ 0 ] in 'zZ' : new_path . append ( [ command [ 0 ] ] ) else : new_path . append ( [ command [ 0 ] ] + _normalize_args ( command [ 1 : ] , norm ) ) return new_path