# Path: drawingDimensioning/core.py
# def debugPrint( level, msg ):
#     if level <= debugPrint.level:
#         App.Console.PrintMessage(msg + '\n')
# 
# Path: drawingDimensioning/svgLib.py
# class SvgTextParser:
#     def __init__(self, xml):
#         p_header_end = xml.find('>') 
#         self.header = xml[:p_header_end]
#         try:
#             self.text = unicode(xml[ p_header_end+1:-len('</text>') ],'utf8')
#         except TypeError:
#             self.text = unicode(xml[ p_header_end+1:-len('</text>') ])
#         #import FreeCAD
#         #FreeCAD.Console.PrintMessage(self.text)
#         self.parms = {}
#         h = self.header
#         p = h.find('=')
#         while p > -1:
#             i = p-1
#             key = ''
#             while key == '' or h[i] != ' ':
#                 if h[i] != ' ':
#                     key = h[i] + key
#                 i = i - 1
#             p1 = h.find('"', p)
#             p2 = h.find('"', p1+1)
#             self.parms[key] = h[p1+1:p2]
#             p = self.header.find('=', p+1)
#         self.x = float(self.parms['x'])
#         self.y = float(self.parms['y'])
#         self.font_family = self.parms.get('font-family', 'inherit')
#         self.font_size = self.parms.get('font-size','inherit')
#         if 'style' in self.parms: #for backwards compadiability
#             self.font_size = self.parms['style'][len('font-size:'):]
#         self.fill = self.parms.get('fill','rgb(0,0,0)')
#         self.transform = self.parms.get('transform')
#         if self.transform != None:
#             t = self.transform
#             if 'rotate(' in t:
#                 self.rotation = float(t[t.find('rotate(')+len('rotate('):].split()[0])
#             else:
#                 self.rotation = 0
#         else:
#             self.rotation = 0
#         self.text_anchor = self.parms.get('text-anchor','inherit')
#     def toXML(self):
#         XML = '''<text x="%f" y="%f" font-family="%s" font-size="%s" fill="%s" text-anchor="%s" %s >%s</text>'''  % (  self.x, self.y, self.font_family, self.font_size,  self.fill, self.text_anchor, 'transform="rotate(%f %f,%f)"' % (self.rotation,self.x,self.y) if self.rotation != 0 else '', self.text )
#         return XML
#     def convertUnits(self,value):
#         '''http://www.w3.org/TR/SVG/coords.html#Units
#         units do not seem to have on font size though:
#         8cm == 8pt == 8blah?'''
#         i = 0
#         while i < len(value) and value[i] in '0123456789.':
#             i = i + 1
#         v = value[:i]
#         factor = 1.0
#         return float(v)*factor
# 
#     def textRect(self):
#         sizeInPoints = self.convertUnits(self.font_size.strip())
#         font = QtGui.QFont(self.font_family, sizeInPoints)
#         fm = QtGui.QFontMetrics(font)
#         return fm.boundingRect(self.text)
# 
#     def width(self):
#         'return width of untransformed text'
#         return self.textRect().width() *0.63
#     def height(self):
#         'height of untransformed text'
#         return self.textRect().height() *0.6
# 
#     def __unicode__(self):
#         return u'<svgLib_dd.SvgTextParser family="%s" font_size="%s" fill="%s" rotation="%f" text="%s">' % (self.font_family, self.font_size, self.fill, self.rotation, self.text )
# 
#     def __repr__(self):
#         return u'<svgLib_dd.SvgTextParser family="%s" font_size="%s" fill="%s" rotation="%f">' % (self.font_family, self.font_size, self.fill, self.rotation )


import pickle
import base64
from numpy.linalg import norm
from numpy import log
from .core import debugPrint
from .svgLib import SvgTextParser
        from .recomputeDimensions import SvgElements
        from .recomputeDimensions import SvgElements
        from .recomputeDimensions import SvgElements
        from .recomputeDimensions import SvgElements
        for x,y,r in svg.circles:
            error1 = norm( pos_ref - new_vi.normalize_position ( x,y ) )
            error2 = abs( log( r_ref / (r/new_vi.scale) ) )
            error = error2 + error1 #giving radius and center position equal priority ...
            if min_error == None or error < min_error:
                self.x, self.y, self.r = x,y,r
        self.viewInfo = new_vi
        return True



class TextSelection( Dimensioning_Selection_prototype ):
    def init_for_svg_KWs( self, svg_KWs, svg_element ):
        self.svgText = SvgTextParser( svg_element.XML[svg_element.pStart:svg_element.pEnd])
    def svg_fun_args( self, args ):
        t = self.svgText
        args.extend([t.x, t.y, t.text, t.font_size, t.rotation, t.font_family, t.fill])
    def updateValues( self, doc ):
        if not self.viewInfo.changed( doc ):
            return False
        debugPrint(3,'textSelection: drawing %s has changed, updating values' % self.viewInfo.name )
        new_vi = self.viewInfo.get_up_to_date_version( doc )
        old_vi = self.viewInfo
        pos_ref = old_vi.normalize_position ( self.svgText.x, self.svgText.y )
        drawingObj = doc.getObject( self.viewInfo.name )
        svg = SvgElements( drawingObj.ViewResult, self.svg_element_tag)
        min_error = None
        for x,y,element in svg.texts:
            error = norm( pos_ref - new_vi.normalize_position ( x,y ) )
            if min_error == None or error < min_error:

# Path: drawingDimensioning/core.py
# def debugPrint( level, msg ):
#     if level <= debugPrint.level:
#         App.Console.PrintMessage(msg + '\n')
# 
# Path: drawingDimensioning/svgLib.py
# class SvgTextParser:
#     def __init__(self, xml):
#         p_header_end = xml.find('>') 
#         self.header = xml[:p_header_end]
#         try:
#             self.text = unicode(xml[ p_header_end+1:-len('</text>') ],'utf8')
#         except TypeError:
#             self.text = unicode(xml[ p_header_end+1:-len('</text>') ])
#         #import FreeCAD
#         #FreeCAD.Console.PrintMessage(self.text)
#         self.parms = {}
#         h = self.header
#         p = h.find('=')
#         while p > -1:
#             i = p-1
#             key = ''
#             while key == '' or h[i] != ' ':
#                 if h[i] != ' ':
#                     key = h[i] + key
#                 i = i - 1
#             p1 = h.find('"', p)
#             p2 = h.find('"', p1+1)
#             self.parms[key] = h[p1+1:p2]
#             p = self.header.find('=', p+1)
#         self.x = float(self.parms['x'])
#         self.y = float(self.parms['y'])
#         self.font_family = self.parms.get('font-family', 'inherit')
#         self.font_size = self.parms.get('font-size','inherit')
#         if 'style' in self.parms: #for backwards compadiability
#             self.font_size = self.parms['style'][len('font-size:'):]
#         self.fill = self.parms.get('fill','rgb(0,0,0)')
#         self.transform = self.parms.get('transform')
#         if self.transform != None:
#             t = self.transform
#             if 'rotate(' in t:
#                 self.rotation = float(t[t.find('rotate(')+len('rotate('):].split()[0])
#             else:
#                 self.rotation = 0
#         else:
#             self.rotation = 0
#         self.text_anchor = self.parms.get('text-anchor','inherit')
#     def toXML(self):
#         XML = '''<text x="%f" y="%f" font-family="%s" font-size="%s" fill="%s" text-anchor="%s" %s >%s</text>'''  % (  self.x, self.y, self.font_family, self.font_size,  self.fill, self.text_anchor, 'transform="rotate(%f %f,%f)"' % (self.rotation,self.x,self.y) if self.rotation != 0 else '', self.text )
#         return XML
#     def convertUnits(self,value):
#         '''http://www.w3.org/TR/SVG/coords.html#Units
#         units do not seem to have on font size though:
#         8cm == 8pt == 8blah?'''
#         i = 0
#         while i < len(value) and value[i] in '0123456789.':
#             i = i + 1
#         v = value[:i]
#         factor = 1.0
#         return float(v)*factor
# 
#     def textRect(self):
#         sizeInPoints = self.convertUnits(self.font_size.strip())
#         font = QtGui.QFont(self.font_family, sizeInPoints)
#         fm = QtGui.QFontMetrics(font)
#         return fm.boundingRect(self.text)
# 
#     def width(self):
#         'return width of untransformed text'
#         return self.textRect().width() *0.63
#     def height(self):
#         'height of untransformed text'
#         return self.textRect().height() *0.6
# 
#     def __unicode__(self):
#         return u'<svgLib_dd.SvgTextParser family="%s" font_size="%s" fill="%s" rotation="%f" text="%s">' % (self.font_family, self.font_size, self.fill, self.rotation, self.text )
# 
#     def __repr__(self):
#         return u'<svgLib_dd.SvgTextParser family="%s" font_size="%s" fill="%s" rotation="%f">' % (self.font_family, self.font_size, self.fill, self.rotation )


# Path: drawingDimensioning/proxies.py
import pickle
import base64
from numpy.linalg import norm
from numpy import log
from .core import debugPrint
from .svgLib import SvgTextParser
        from .recomputeDimensions import SvgElements
        from .recomputeDimensions import SvgElements
        from .recomputeDimensions import SvgElements
        from .recomputeDimensions import SvgElements

        for x,y,r in svg.circles:
            error1 = norm( pos_ref - new_vi.normalize_position ( x,y ) )
            error2 = abs( log( r_ref / (r/new_vi.scale) ) )
            error = error2 + error1 #giving radius and center position equal priority ...
            if min_error == None or error < min_error:
                self.x, self.y, self.r = x,y,r
        self.viewInfo = new_vi
        return True



class TextSelection( Dimensioning_Selection_prototype ):
    def init_for_svg_KWs( self, svg_KWs, svg_element ):
        self.svgText = SvgTextParser( svg_element.XML[svg_element.pStart:svg_element.pEnd])
    def svg_fun_args( self, args ):
        t = self.svgText
        args.extend([t.x, t.y, t.text, t.font_size, t.rotation, t.font_family, t.fill])
    def updateValues( self, doc ):
        if not self.viewInfo.changed( doc ):
            return False
        debugPrint(3,'textSelection: drawing %s has changed, updating values' % self.viewInfo.name )
        new_vi = self.viewInfo.get_up_to_date_version( doc )
        old_vi = self.viewInfo
        pos_ref = old_vi.normalize_position ( self.svgText.x, self.svgText.y )
        drawingObj = doc.getObject( self.viewInfo.name )
        svg = SvgElements( drawingObj.ViewResult, self.svg_element_tag)
        min_error = None
        for x,y,element in svg.texts:
            error = norm( pos_ref - new_vi.normalize_position ( x,y ) )
            if min_error == None or error < min_error:

                self.svgText = SvgTextParser( element.XML[element.pStart:element.pEnd] )