'Tries to convert html units in C{val} to pixels.
@param pts: If True return 10*pts instead of pixels.
@return: The number of pixels (an int) if successful. Otherwise, returns None.'

def unit_convert(self, val, pts=False, base_length='10pt'):

    dpi = self.profile.dpi
    result = None
    try:
        result = int(val)
    except ValueError:
        pass
    m = re.search('\\s*(-*[0-9]*\\.?[0-9]*)\\s*(%|em|px|mm|cm|in|dpt|pt|pc)', val)
    if ((m is not None) and m.group(1)):
        unit = float(m.group(1))
        if (m.group(2) == '%'):
            normal = self.unit_convert(base_length)
            result = ((unit / 100.0) * normal)
        elif (m.group(2) == 'px'):
            result = unit
        elif (m.group(2) == 'in'):
            result = (unit * dpi)
        elif (m.group(2) == 'pt'):
            result = ((unit * dpi) / 72.0)
        elif (m.group(2) == 'dpt'):
            result = ((unit * dpi) / 720.0)
        elif (m.group(2) == 'em'):
            normal = self.unit_convert(base_length)
            result = (unit * normal)
        elif (m.group(2) == 'pc'):
            result = ((unit * (dpi / 72.0)) * 12)
        elif (m.group(2) == 'mm'):
            result = ((unit * 0.04) * dpi)
        elif (m.group(2) == 'cm'):
            result = ((unit * 0.4) * dpi)
    if (result is not None):
        if pts:
            result = int(round((result * (720.0 / dpi))))
        else:
            result = int(round(result))
    return result
