Skip to content

Commit ad1576d

Browse files
committed
v241218.1
1 parent 9dbdad6 commit ad1576d

File tree

8 files changed

+53
-37
lines changed

8 files changed

+53
-37
lines changed

dist/easycoder-241215.1.tar.gz

-40 KB
Binary file not shown.

dist/easycoder-241218.1.tar.gz

2.51 MB
Binary file not shown.

easycoder/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
from .ec_value import *
1111
from .ec_graphics import *
1212

13-
__version__ = "241215.1"
13+
__version__ = "241218.1"

easycoder/ec_renderer.py

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import sys, json
44
import tkinter as tk
5-
from PIL import *
5+
from PIL import Image, ImageTk
66

77
elements = {}
88
zlist = []
@@ -170,38 +170,33 @@ def renderText(values, parent, args):
170170
screenTop = top + getScreenTop(parent)
171171
width = getValue(args, values['width']) if 'width' in values else 100
172172
height = getValue(args, values['height']) if 'height' in values else 100
173-
right = screenLeft + width
174-
bottom = screenTop + height
175173
shape = getValue(args, values['shape']) if 'shape' in values else 'rectangle'
176-
fill = getValue(args, values['fill']) if 'fill' in values else None
177174
outline = getValue(args, values['outline']) if 'outline' in values else None
178-
outlineWidth = getValue(args, values['outlineWidth']) if 'outlineWidth' in values else 0 if outline == None else 1
179175
color = getValue(args, values['color']) if 'color' in values else None
180176
text = getValue(args, values['text']) if 'text' in values else ''
181177
fontFace = getValue(args, values['fontFace']) if 'fontFace' in values else 'Helvetica'
182178
fontWeight = getValue(args, values['fontWeight']) if 'fontWeight' in values else 'normal'
183-
fontSize = round(height*2/5) if shape == 'ellipse' else round(height*3/5)
184-
fontTop = screenTop + height/2
179+
fontTop = int(round(screenTop + height/2))
185180
if 'fontSize' in values:
186181
fontSize = getValue(args, values['fontSize'])
187-
fontTop = top + round(fontSize * 5 / 4)
188-
adjust = round(fontSize/5) if shape == 'ellipse' else 0
182+
fontTop = int(round(screenTop + height/2 - fontSize/4))
183+
else:
184+
fontSize = int(round(height*2/5) if shape == 'ellipse' else round(height*3/5))
185+
fontTop -= int(round(screenTop + height/2 - fontSize/5))
186+
adjust = int(round(fontSize/5)) if shape == 'ellipse' else 0
189187
align = getValue(args, values['align']) if 'align' in values else 'center'
190188
if align == 'left':
191-
xoff = round(fontSize/5)
189+
xoff = int(round(fontSize/5))
192190
anchor = 'w'
193191
elif align == 'right':
194-
xoff = width - round(fontSize/5)
192+
xoff = width - int(round(fontSize/5))
195193
anchor = 'e'
196194
else:
197-
xoff = width/2
195+
xoff = int(round(width/2))
198196
anchor = 'center'
199197
if xoff < 3:
200198
xoff = 3
201-
if shape == 'ellipse':
202-
containerId = getCanvas().create_oval(screenLeft, screenTop, right, bottom, fill=fill, outline=outline, width=outlineWidth)
203-
else:
204-
containerId = getCanvas().create_rectangle(screenLeft, screenTop, right, bottom, fill=fill, outline=outline, width=outlineWidth)
199+
xoff -= int(round(fontSize/4))
205200
textId = canvas.create_text(screenLeft + xoff, fontTop + adjust, fill=color, font=f'"{fontFace}" {fontSize} {fontWeight}', text=text, anchor=anchor)
206201
if 'name' in values:
207202
widgetName = getValue(args, values['name'])
@@ -211,7 +206,6 @@ def renderText(values, parent, args):
211206
"type": "text",
212207
"name": widgetName,
213208
"id": textId,
214-
"containerId": containerId,
215209
"left": left,
216210
"top": top,
217211
"width": width,
@@ -229,34 +223,31 @@ def renderImage(values, parent, args):
229223
top = getValue(args, values['top']) if 'top' in values else 10
230224
screenTop = top + getScreenTop(parent)
231225
width = getValue(args, values['width']) if 'width' in values else 100
232-
height = getValue(args, values['height']) if 'height' in values else 100
233-
right = screenLeft + width
234-
bottom = screenTop + height
235-
src = getValue(args, values['src']) if 'src' in values else None
236-
containerId = getCanvas().create_rectangle(screenLeft, screenTop, right, bottom, width=0)
226+
source = getValue(args, values['source']) if 'source' in values else None
237227
if 'name' in values:
238228
widgetName = values['name']
239229
else:
240230
widgetName = None
231+
if source == None:
232+
raise(Exception(f'No image source given for \'{id}\''))
233+
img = (Image.open(source))
234+
height = int(round(img.height * width / img.width))
235+
resized_image= img.resize((width, height), Image.LANCZOS)
236+
new_image= ImageTk.PhotoImage(resized_image)
237+
imageid = getCanvas().create_image(screenLeft, screenTop, anchor='nw', image=new_image)
238+
images[widgetName] = {'id': imageid, "image": new_image}
241239
widgetSpec = {
242240
"type": "image",
243241
"nme": widgetName,
244-
"id": containerId,
245242
"left": left,
246243
"top": top,
247244
"width": width,
248245
"height": height,
246+
"source": source,
249247
"parent": parent
250248
}
251249
elements[widgetName] = widgetSpec
252250
zlist.append({widgetName: widgetSpec})
253-
if src == None:
254-
raise(Exception(f'No image source given for \'{id}\''))
255-
img = (Image.open(src))
256-
resized_image= img.resize((width, height), Image.ANTIALIAS)
257-
new_image= ImageTk.PhotoImage(resized_image)
258-
imageid = getCanvas().create_image(screenLeft, screenTop, anchor='nw', image=new_image)
259-
images[containerId] = {'id': imageid, "image": new_image}
260251
return widgetSpec
261252

262253
# Create a canvas or render a widget

images/Semoigo Dawn.jpg

2.47 MB
Loading

scripts/graphics-demo.json renamed to json/graphics-demo.json

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"#": [
33
"BlueRect",
4-
"RedEllipse"
4+
"RedEllipse",
5+
"Photo"
56
],
67
"BlueRect": {
78
"type": "rectangle",
@@ -27,10 +28,33 @@
2728
"RedEllipse": {
2829
"type": "ellipse",
2930
"left": 300,
30-
"top": 200,
31+
"top": 100,
3132
"width": 200,
3233
"height": 100,
3334
"fill": "red",
34-
"name": "redellipse"
35+
"name": "redellipse",
36+
"#": [
37+
"WhiteText"
38+
],
39+
"WhiteText": {
40+
"type": "text",
41+
"left": 25,
42+
"top": 40,
43+
"width": 160,
44+
"height": 32,
45+
"color": "white",
46+
"text": "Graphics demo",
47+
"fontSize": 20,
48+
"name": "mytext"
49+
}
50+
},
51+
52+
"Photo": {
53+
"type": "image",
54+
"left": 150,
55+
"top": 250,
56+
"width": 300,
57+
"source": "images/Semoigo Dawn.jpg",
58+
"name": "dawn"
3559
}
3660
}

scripts/graphics-demo.ecs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
rectangle BlueRect
66
ellipse RedEllipse
77
ellipse GreenCircle
8+
text WhiteText
89
file File
910
variable Spec
1011
variable UpDown
@@ -16,7 +17,7 @@
1617

1718
create screen at 300 300 size 640 480 fill color yellow
1819

19-
open File `scripts/graphics-demo.json` for reading
20+
open File `json/graphics-demo.json` for reading
2021
read Spec from File
2122
close File
2223

@@ -48,7 +49,7 @@
4849
clear UpDown
4950
end
5051
move RedEllipse to Left Top
51-
take 5 from Left
52+
add 5 to Left
5253
end
5354
else
5455
begin
@@ -62,7 +63,7 @@
6263
set UpDown
6364
end
6465
move RedEllipse to Left Top
65-
add 5 to Left
66+
take 5 from Left
6667
end
6768
increment N
6869
end

0 commit comments

Comments
 (0)