def check_svg_size(size): """Raises exception if invalid size for SVG node.""" if size[0] <= 0: raise ifaint.LoadError("SVG element has negative width") elif size[1] <= 0: raise ifaint.LoadError("SVG element has negative height") return size
def parse(file_path, image_props): """Build an image from the pdf file at file_path""" f = codecs.open(file_path, "rb", "ascii") # Fixme: ascii will fail for values > 127 text = f.read() page_sizes = _find_page_sizes(text) if page_sizes == 0: raise ifaint.LoadError("No pages found.") stream_list = _find_streams(text) if len(stream_list) == 0: raise ifaint.LoadError("No data streams found.") if len(stream_list) != len(page_sizes): raise ifaint.LoadError("Data stream count does not match page count.") meta_data_list = _find_faint_meta(text) if len(meta_data_list) != 0 and len(meta_data_list) != len(page_sizes): raise ifaint.LoadError( "Faint meta data count does not match page count.") if len(meta_data_list) == 0: meta_data_list = [{} for num in page_sizes] for stream, size, meta_data in zip(stream_list, page_sizes, meta_data_list): frame_props = image_props.add_frame(size) _parse_stream(stream, frame_props, size, meta_data)
def parse_svg_string(xml_string, image_props, system_language="en"): """Parses the SVG document in the given string, using the image_props to build an image. """ try: ET.register_namespace("svg", "{http://www.w3.org/2000/svg}") root = ET.fromstring(xml_string) if root.tag == ns_svg("svg"): parse_svg_root_node(root, image_props, system_language) else: raise ifaint.LoadError("Root element was not <svg>.") except svg_error as e: raise ifaint.LoadError(str(e))
def parse_doc(path, image_props, system_language="en"): """Parses the SVG document at the given file path, using the image_props to build an image. """ try: ET.register_namespace("svg", "{http://www.w3.org/2000/svg}") tree = ET.parse(path) root = tree.getroot() if root.tag == ns_svg("svg"): parse_svg_root_node(root, image_props, system_language) else: raise ifaint.LoadError("Root element was not <svg>.") except svg_error as e: raise ifaint.LoadError(str(e))
def wrapper(*args, **kwArgs): try: return func(*args, **kwArgs) except ParseError as e: if _expat.is_document_related(e): raise _ifaint.LoadError("Error in file:\n" + str(e)) else: # Re-raise as internal error raise wrapper.__doc__ = func.__doc__
def parse_doc(path, props): """Use the ImageProps to build a Faint Image from the svg file at the specified path. """ try: dom = minidom.parse(path) except expat.ExpatError, err: if expat.is_document_related(err): err_str = "Error in file:\n" + str(err) raise ifaint.LoadError(err_str) else: raise # Re-raise as internal error
def parse_color_stop_offset(svg_string): """Parses an offset for a gradient color stop""" match = re.match(svg_re.percentage, svg_string) if match: return float(match.group(0)) / 100 match = re.match(svg_re.number_attr, svg_string) if match: # Ratio offset # Fixme: I think standard says clamp to 0.0-1.0 return float(match.group(0)) else: raise ifaint.LoadError("Failed parsing gradient offset")
def parse_transform(s): """Parses an entry in a transform-list.""" def _parse_args(s): assert s.startswith("(") assert s.endswith(")") # Fixme: parse number (i.e. incl. Ee etc) str_args = [ arg for arg in s.replace(" ", ",")[1:-1].split(",") if len(arg) != 0 ] return [float(arg) for arg in str_args] op, args = s.split("(") args = _parse_args('(' + args) if op == "skewX": return Matrix.skewX(deg2rad(*args)) elif op == "skewY": return Matrix.skewY(deg2rad(*args)) elif op == "rotate": rad = deg2rad(args[0]) pivot = args[1:] if len(args) == 3 else None return Matrix.rotation(rad, pivot) elif op == "translate": x = args[0] y = args[1] if len(args) == 2 else 0 return Matrix.translation(x, y) elif op == "matrix": return Matrix(*args) elif op == "scale": sx = args[0] sy = args[1] if len(args) == 2 else sx return Matrix.scale(sx, sy) else: raise ifaint.LoadError("Unsupported transform: %s" % op)
def _io_load_error(io_error, file_name): """Load equivalent of _io_save_error""" err_str = 'Faint could not read from "%s".\n\nError code: %d (%s).' % ( file_name, io_error.errno, io_error.strerror) return ifaint.LoadError(err_str)