Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions bubbles/bubbles.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ def __init__(self,
grad_offset=30.0,
font_family='Latin Modern Sans', font_weight='normal'):
self.pars = locals(); self.pars.pop('self')
self.bubble = {}
self._bubbles = []
self.bubble = {} # bubble definitions
self._bubbles = [] # bubble placements (id, x, y)
self._liaisons = []
self._texts = []
self._colors = []
self._colorsid = []
self._colors = [] # color definition

def def_bubble(self, key, fill=0xFFFFFF, r=50, stroke=0x990000, stroke_w=10):

Expand Down Expand Up @@ -140,12 +139,17 @@ def put_liaison_equal(self, r0, x, y0, y1, h):
'''

l = abs(y1-y0)
assert 0.5*h <= r0 , f"bad h parameter, should be less or equal to {2*r0}"

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"bad" parameter actually works -- gives a weird looking bond but I thought it might be not useless

if 0.5*l < r0:
assert 0.25*(l**2+h**2) >= r0**2, f"bad h parameter, should be greater or equal to {2*math.sqrt(r0**2-l**2/4)}"

R = (0.25*l**2 + 0.25*h**2 - r0**2) / (2.0*r0 - h)

dx = 0.5*h + R
dy = 0.5*l
dr = math.hypot(dx, dy)

# position of a tangent point wrt C0's center
xx = dx*r0 / dr
yy = dy*r0 / dr
return R, x+xx, y0+yy, 0, 2.0*(dy-yy), -2.0*xx
Expand Down Expand Up @@ -270,6 +274,7 @@ def get_canvsize(self):


def _gold(f, eps, bra, ket):
"""Golden ratio optimisation: find a local minimum for f between bra and key, with tolerence eps"""
phi = (math.sqrt(5.0)-1.0)*0.5
d = ket-bra
x1 = ket-d*phi
Expand Down
38 changes: 32 additions & 6 deletions inkscape-ext/bubblebond.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@
from itertools import count
import inkex
from lxml import etree
import bubbles

try:
import bubbles
except ImportError:
# try and detect the bubbles.py file next to this one
import sys, os
sys.path.append(os.path.split(__file__)[0])
import bubbles
del sys.path[-1]
del sys,os


class BubbleBond(inkex.Effect):
Expand Down Expand Up @@ -36,7 +45,19 @@ def effect(self):
raise inkex.AbortExtension("These are not circles.")
h = width * 2.0*min(r0, r1)

colors = (st0['stroke'], st1['stroke'])
if 'stroke' in st0 and st0['stroke'] != 'none':
col0 = st0['stroke']
elif 'fill' in st0:
col0 = st0['fill']
else:
col0 = 'none'
if 'stroke' in st1 and st1['stroke'] != 'none':
col1 = st1['stroke']
elif 'fill' in st0:
col1 = st1['fill']
else:
col1 = 'none'
colors = (col0, col1)
x0 = float(c0.attrib['cx'])
y0 = float(c0.attrib['cy'])
x1 = float(c1.attrib['cx'])
Expand Down Expand Up @@ -98,14 +119,19 @@ def get_radius(self, c0, st0):
r0 = c0.radius[0]
else:
return None
return r0 + float(st0['stroke-width'])/2
if 'stroke' in st0 and st0['stroke'] != 'none':
return r0 + float(st0['stroke-width'])/2
else:
return r0

def get_style_dict(self, c0):
st0_str = c0.attrib['style']
st0_obj = inkex.styles.Style.parse_str(st0_str)
st0_dict = {}
for key, val in st0_obj:
st0_dict[key] = val
if hasattr(st0_obj, 'items'):
st0_dict = { k:v for k,v in st0_obj.items() }
else:
# fallback that only works on old versions of inkscape (before which one?)
st0_dict = { k:v for k,v in st0_obj }
return st0_dict


Expand Down