Skip to content
This repository has been archived by the owner on May 18, 2019. It is now read-only.

Commit

Permalink
put base64 data into svg image
Browse files Browse the repository at this point in the history
Belonging to [master]:
  - #2695
  • Loading branch information
hkiel authored and OpenModelica-Hudson committed Oct 5, 2018
1 parent 09d7769 commit e6854b5
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions Examples/generate_icons.py
Expand Up @@ -93,7 +93,7 @@ def classToFileName(cl):

# example: Bitmap(true, {0.0, 0.0}, 0, {{-98, 98}, {98, -98}}, "modelica://Modelica/Resources/Images/Mechanics/MultiBody/Visualizers/TorusIcon.png"
# TODO: where is the imageSource?
regex_bitmap = re.compile('Bitmap\(([\w ]+), {('+exp_float+'), ('+exp_float+')}, ('+exp_float+'), ({{'+exp_float+', '+exp_float+'}(?:, {'+exp_float+', '+exp_float+'})*}), ("[^"]*")')
regex_bitmap = re.compile('Bitmap\(([\w ]+), {('+exp_float+'), ('+exp_float+')}, ('+exp_float+'), {{('+exp_float+'), ('+exp_float+')}, {('+exp_float+'), ('+exp_float+')}}, ("[^"]*")(?:, ("[^"]*"))?')

# anything unknown that produces output should look like this: Trash(...
regex_any = re.compile('(\w+)\(')
Expand Down Expand Up @@ -292,13 +292,13 @@ def getGraphicsForClass(modelicaClass):
graphicsObj['visible'] = g[0]
graphicsObj['origin'] = [float(g[1]), float(g[2])]
graphicsObj['rotation'] = float(g[3])

points = []
gg = re.findall(regex_points, g[4])
for i in range(0, len(gg)):
points.append([float(gg[i][0]), float(gg[i][1])])
graphicsObj['points'] = points
graphicsObj['href'] = g[5].strip('"')
graphicsObj['extent'] = [[float(g[4]), float(g[5])], [float(g[6]), float(g[7])]]
print icon_line
if g[9] is not None:
graphicsObj['href'] = "data:image;base64,"+g[9].strip('"')
else:
logger.warning('Not yet supported: {0} with URL'.format(graphics['type']))
graphicsObj['href'] = g[8].strip('"')

if not 'type' in graphicsObj:
r = regex_any.search(icon_line)
Expand Down Expand Up @@ -528,7 +528,7 @@ def getSvgFromGraphics(dwg, graphics, minX, maxY, includeInvisibleText, transfor

origin = graphics['origin']

if graphics['type'] == 'Rectangle' or graphics['type'] == 'Ellipse' or graphics['type'] == 'Text':
if graphics['type'] == 'Rectangle' or graphics['type'] == 'Ellipse' or graphics['type'] == 'Text' or graphics['type'] == "Bitmap":
(x0, y0) = getCoordinates(graphics['extent'][0], graphics, minX, maxY, transformation, coordinateSystem)
(x1, y1) = getCoordinates(graphics['extent'][1], graphics, minX, maxY, transformation, coordinateSystem)

Expand Down Expand Up @@ -684,9 +684,18 @@ def getSvgFromGraphics(dwg, graphics, minX, maxY, includeInvisibleText, transfor
shape.add(svgwrite.text.TSpan(graphics['textString'], **extra))

elif graphics['type'] == 'Bitmap':
logger.warning('Not supported: {0}'.format(graphics['type']))
shape = dwg.image(graphics['href']) # put in correct URL or base64 data "data:image/png;base64,", need width/height?
return shape, definitions
xmin = x0
ymin = y0
xmax = x1
ymax = y1

if x0 > x1:
xmin = x1
xmax = x0
if y0 > y1:
ymin = y1
ymax = y0
shape = dwg.image(graphics['href'], x=xmin,y=ymin,width=xmax-xmin,height=ymax-ymin) # put in correct URL or base64 data "data:image;base64,"

elif graphics['type'] == 'Empty':
return None
Expand Down Expand Up @@ -1020,7 +1029,8 @@ def getSvgFromGraphics(dwg, graphics, minX, maxY, includeInvisibleText, transfor

definitions.add(gradient)
else:
shape.fill('none', opacity=0)
if graphics['type'] != 'Bitmap':
shape.fill('none', opacity=0)

return shape, definitions

Expand Down

0 comments on commit e6854b5

Please sign in to comment.