How to use the scour.scour.SVGLength function in scour

To help you get started, we’ve selected a few scour examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github scour-project / scour / scour / scour.py View on Github external
def properlySizeDoc(docElement, options):
   # get doc width and height
   w = SVGLength(docElement.getAttribute('width'))
   h = SVGLength(docElement.getAttribute('height'))

   # if width/height are not unitless or px then it is not ok to rewrite them into a viewBox.
   # well, it may be OK for Web browsers and vector editors, but not for librsvg.
   if options.renderer_workaround:
      if ((w.units != Unit.NONE and w.units != Unit.PX) or
         (h.units != Unit.NONE and h.units != Unit.PX)):
          return

   # else we have a statically sized image and we should try to remedy that

   # parse viewBox attribute
   vbSep = re.split("\\s*\\,?\\s*", docElement.getAttribute('viewBox'), 3)
   # if we have a valid viewBox we need to check it
   vbWidth,vbHeight = 0,0
   if len(vbSep) == 4:
github scour-project / scour / scour / scour.py View on Github external
def properlySizeDoc(docElement, options):
   # get doc width and height
   w = SVGLength(docElement.getAttribute('width'))
   h = SVGLength(docElement.getAttribute('height'))

   # if width/height are not unitless or px then it is not ok to rewrite them into a viewBox.
   # well, it may be OK for Web browsers and vector editors, but not for librsvg.
   if options.renderer_workaround:
      if ((w.units != Unit.NONE and w.units != Unit.PX) or
         (h.units != Unit.NONE and h.units != Unit.PX)):
          return

   # else we have a statically sized image and we should try to remedy that

   # parse viewBox attribute
   vbSep = re.split("\\s*\\,?\\s*", docElement.getAttribute('viewBox'), 3)
   # if we have a valid viewBox we need to check it
   vbWidth,vbHeight = 0,0
   if len(vbSep) == 4:
      try:
github scour-project / scour / scour / scour.py View on Github external
if node.getAttribute('cy') != '':
      cy = SVGLength(node.getAttribute('cy'))
      if node.nodeName in ['circle', 'ellipse']:
         if cy.value == 0:
            node.removeAttribute('cy')
            num += 1
      elif node.nodeName == "radialGradient":
         if ( (cy.value == 50  and cy.units == Unit.PCT) or
              (cy.value == 0.5 and cy.units == Unit.NONE and not node.getAttribute('gradientUnits') == 'userSpaceOnUse') ):
            node.removeAttribute('cy')
            num += 1

   # r - radialGradient: 50% (r="0.5" usually equals "0.5px" which only equals "50%" if gradientUnits="objectBoundingBox")
   if node.getAttribute('r') != '':
      if node.nodeName == 'radialGradient':
         r = SVGLength(node.getAttribute('r'))
         if ( (r.value == 50  and r.units == Unit.PCT) or
              (r.value == 0.5 and r.units == Unit.NONE and not node.getAttribute('gradientUnits') == 'userSpaceOnUse') ):
            node.removeAttribute('r')
            num += 1

   # Summarily get rid of some more attributes
   attributes = [node.attributes.item(i).nodeName
              for i in range(node.attributes.length)]
   for attribute in attributes:
      if attribute not in tainted:
         if attribute in list(default_attributes.keys()):
            if node.getAttribute(attribute) == default_attributes[attribute]:
               node.removeAttribute(attribute)
               num += 1
            else:
               tainted = taint(tainted, attribute)
github scour-project / scour / scour / scour.py View on Github external
if node.getAttribute('x2') != '':
      x2 = SVGLength(node.getAttribute('x2'))
      if node.nodeName == 'line':
         if x2.value == 0:
           node.removeAttribute('x2')
           num += 1
      elif node.nodeName == 'linearGradient':
         if ( (x2.value == 100 and x2.units == Unit.PCT) or
              (x2.value == 1   and x2.units == Unit.NONE and not node.getAttribute('gradientUnits') == 'userSpaceOnUse') ):
            node.removeAttribute('x2')
            num += 1

   # y2 - line: 0
   #      linearGradient: 0%
   if node.getAttribute('y2') != '':
      y2 = SVGLength(node.getAttribute('y2'))
      if y2.value == 0:
         node.removeAttribute('y2')
         num += 1

   # fx: equal to rx
   if node.getAttribute('fx') != '':
      if node.getAttribute('fx') == node.getAttribute('cx'):
         node.removeAttribute('fx')
         num += 1

   # fy: equal to ry
   if node.getAttribute('fy') != '':
      if node.getAttribute('fy') == node.getAttribute('cy'):
         node.removeAttribute('fy')
         num += 1
github scour-project / scour / scour / scour.py View on Github external
def scourLength(length):
   """
   Scours a length. Accepts units.
   """
   length = SVGLength(length)

   return scourUnitlessLength(length.value) + Unit.str(length.units)
github scour-project / scour / scour / scour.py View on Github external
if node.getAttribute('cx') != '':
      cx = SVGLength(node.getAttribute('cx'))
      if node.nodeName in ['circle', 'ellipse']:
         if cx.value == 0:
            node.removeAttribute('cx')
            num += 1
      elif node.nodeName == "radialGradient":
         if ( (cx.value == 50  and cx.units == Unit.PCT) or
              (cx.value == 0.5 and cx.units == Unit.NONE and not node.getAttribute('gradientUnits') == 'userSpaceOnUse') ):
            node.removeAttribute('cx')
            num += 1

   # cy - circle / ellipse: 0
   #      radialGradient: 50% (cy="0.5" usually equals "0.5px" which only equals "50%" if gradientUnits="objectBoundingBox")
   if node.getAttribute('cy') != '':
      cy = SVGLength(node.getAttribute('cy'))
      if node.nodeName in ['circle', 'ellipse']:
         if cy.value == 0:
            node.removeAttribute('cy')
            num += 1
      elif node.nodeName == "radialGradient":
         if ( (cy.value == 50  and cy.units == Unit.PCT) or
              (cy.value == 0.5 and cy.units == Unit.NONE and not node.getAttribute('gradientUnits') == 'userSpaceOnUse') ):
            node.removeAttribute('cy')
            num += 1

   # r - radialGradient: 50% (r="0.5" usually equals "0.5px" which only equals "50%" if gradientUnits="objectBoundingBox")
   if node.getAttribute('r') != '':
      if node.nodeName == 'radialGradient':
         r = SVGLength(node.getAttribute('r'))
         if ( (r.value == 50  and r.units == Unit.PCT) or
              (r.value == 0.5 and r.units == Unit.NONE and not node.getAttribute('gradientUnits') == 'userSpaceOnUse') ):
github scour-project / scour / scour / scour.py View on Github external
def removeDuplicateGradientStops(doc):
   global numElemsRemoved
   num = 0

   for gradType in ['linearGradient', 'radialGradient']:
      for grad in doc.getElementsByTagName(gradType):
         stops = {}
         stopsToRemove = []
         for stop in grad.getElementsByTagName('stop'):
            # convert percentages into a floating point number
            offsetU = SVGLength(stop.getAttribute('offset'))
            if offsetU.units == Unit.PCT:
               offset = offsetU.value / 100.0
            elif offsetU.units == Unit.NONE:
               offset = offsetU.value
            else:
               offset = 0
            # set the stop offset value to the integer or floating point equivalent
            if int(offset) == offset: stop.setAttribute('offset', str(int(offset)))
            else: stop.setAttribute('offset', str(offset))

            color = stop.getAttribute('stop-color')
            opacity = stop.getAttribute('stop-opacity')
            style = stop.getAttribute('style')
            if offset in stops :
               oldStop = stops[offset]
               if oldStop[0] == color and oldStop[1] == opacity and oldStop[2] == style:
github scour-project / scour / scour / scour.py View on Github external
Also checks for the attributes actually being lengths, not 'inherit', 'none'
   or anything that isn't an SVGLength.

   Returns the number of bytes saved after performing these reductions.
   """
   num = 0

   styles = _getStyle(element)
   for lengthAttr in ['opacity', 'flood-opacity', 'fill-opacity',
                  'stroke-opacity', 'stop-opacity', 'stroke-miterlimit',
                  'stroke-dashoffset', 'letter-spacing', 'word-spacing',
                  'kerning', 'font-size-adjust', 'font-size',
                  'stroke-width']:
      val = element.getAttribute(lengthAttr)
      if val != '':
         valLen = SVGLength(val)
         if valLen.units != Unit.INVALID: # not an absolute/relative size or inherit, can be % though
            newVal = scourLength(val)
            if len(newVal) < len(val):
               num += len(val) - len(newVal)
               element.setAttribute(lengthAttr, newVal)
      # repeat for attributes hidden in styles
      if lengthAttr in list(styles.keys()):
         val = styles[lengthAttr]
         valLen = SVGLength(val)
         if valLen.units != Unit.INVALID:
            newVal = scourLength(val)
            if len(newVal) < len(val):
               num += len(val) - len(newVal)
               styles[lengthAttr] = newVal
   _setStyle(element, styles)