1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- # -*- coding:utf-8 -*-
- ##
- # This file is part of connectSVG.
- #
- # connectSVG is free software: you can redistribute it and/or modify it
- # under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License,
- # or (at your option) any later version.
- #
- # connectSVG is distributed in the hope that it will be useful, but WITHOUT ANY
- # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- # details.
- #
- # You should have received a copy of the GNU General Public License along
- # with connectSVG. If not, see <https://www.gnu.org/licenses/>.
- ##
- import logging
- log = logging.getLogger("connectsvg")
- TOLERANCE = 0.0000000000005
- def connect(path, other) :
- if abs(path.end - other.start) <= TOLERANCE :
- log.debug("Appending path %s--%s", other.start, other.end)
- other.start = path.end
- path.extend(other)
- return path, True
- elif abs(path.end - other.end) <= TOLERANCE:
- log.debug("Appending path %s--%s", other.start, other.end)
- other.end = path.end;
- path.extend(other.reversed())
- return path, True
- elif abs(path.start - other.start) <= TOLERANCE :
- log.debug("Appending path %s--%s", other.start, other.end)
- other.start = path.start
- path = path.reversed()
- path.extend(other)
- return path, True
- elif abs(path.start - other.end) <= TOLERANCE :
- log.debug("Appending path %s--%s", other.start, other.end)
- other.end = path.start
- path = path.reversed()
- path.extend(other.reversed())
- return path, True
- return path, False
- def connectPaths(paths, attributes) :
- log.info("Starting with %d paths", len(paths))
- processedPaths = []
- processedAttributes = []
- while len(paths) > 0 :
- path = paths.pop()
- attr = attributes.pop()
- log.debug("Processing path %s--%s", path.start, path.end)
- i = 0
- while i < len(paths) :
- if attr['style'] == attributes[i]['style'] :
- path, used = connect(path, paths[i])
-
- if used :
- paths.pop(i)
- attributes.pop(i)
- i = -1
- i += 1
- processedPaths.append(path)
- processedAttributes.append({
- 'style': attr['style'],
- 'id': 'path'+str(len(processedPaths))
- })
- log.info("Ending with %d paths", len(processedPaths))
- return processedPaths, processedAttributes
|