1
0
Fork 0
mirror of https://github.com/shlomif/PySolFC.git synced 2025-04-05 00:02:29 -04:00

hacky way to load svg fragments

does not work quite right - it is a start.
This commit is contained in:
Shlomi Fish 2019-04-28 01:42:38 +03:00
parent 0a3b56021a
commit 28024ff6e6
3 changed files with 40 additions and 25 deletions

View file

@ -25,10 +25,12 @@
import os
from pysollib.mfxutil import Image, ImageTk, USE_PIL
from pysollib.pysoltk import PIL_Image
from pysollib.pysoltk import copyImage, createBottom, createImage, loadImage
from pysollib.pysoltk import shadowImage
from pysollib.resource import CSI
from pysollib.settings import TOOLKIT
from pysollib.ui.tktile.svg import SVGManager
# ************************************************************************
# * Images
@ -76,16 +78,22 @@ class Images:
def destruct(self):
pass
def __loadCard(self, filename, check_w=1, check_h=1):
def __loadCard(self, filename, check_w=1, check_h=1, rec=None):
# print '__loadCard:', filename
f = os.path.join(self.cs.dir, filename)
if not os.path.exists(f):
print('card image path %s does not exist' % (f))
return None
try:
img = loadImage(file=f)
except Exception:
return None
if rec and 'suit' in rec:
# img = PIL_Image(image=self.svg.render_fragment("6_heart"))
img = self.svg.render_fragment(
id_="6_heart", width=self.CARDW, height=self.CARDH)
img = PIL_Image(image=img)
else:
if not os.path.exists(f):
print('card image path %s does not exist' % (f))
return None
try:
img = loadImage(file=f)
except Exception:
return None
if TOOLKIT == 'kivy':
w = img.texture.size[0]
@ -170,8 +178,12 @@ class Images:
pstep += self.cs.nshadows + 1 # shadows & shade
pstep = max(0, (80.0 - progress.percent) / pstep)
# load face cards
for n in self.cs.getFaceCardNames():
self._card.append(self.__loadCard(n + self.cs.ext))
self.svg = SVGManager(
'/usr/share/carddecks/' +
'svg-ancient-egyptians/Ancient_Egyptians.svgz')
for n, rec in self.cs.getFaceCardNames():
print(n)
self._card.append(self.__loadCard(n + self.cs.ext, rec=rec))
self._card[-1].filename = n
if progress:
progress.update(step=pstep)

View file

@ -355,14 +355,15 @@ class Cardset(Resource):
names = []
for suit in self.suits:
for rank in self.ranks:
names.append("%02d%s" % (rank + 1, suit))
names.append(("%02d%s" % (rank + 1, suit),
{'rank': rank, 'suit': suit}))
for trump in self.trumps:
names.append("%02d%s" % (trump + 1, "z"))
names.append(("%02d%s" % (trump + 1, "z"), {'trump': trump}))
assert len(names) == self.ncards
return names
def getPreviewCardNames(self):
names = self.getFaceCardNames()
names = [x for x, _ in self.getFaceCardNames()]
pnames = []
ranks, suits = self.ranks, self.suits
lr, ls = len(ranks), len(suits)

View file

@ -5,30 +5,32 @@
# https://stackoverflow.com/questions/22583035
# Thanks!
import Image
import ImageTk
import cairo
import rsvg
from gi.repository import Rsvg
from pysollib.mfxutil import Image, ImageTk
class SVGManager:
"""docstring for SVGManager"""
def __init__(self, filename):
self.filename = filename
self.svg = rsvg.Handle(filename)
self.svg = Rsvg.Handle().new_from_file(filename)
def render_fragment(self, id_):
def render_fragment(self, id_, width, height):
id__ = '#' + id_
"""docstring for render_"""
width, height = self.svg.get_sub_dimension_data(id_)[:2]
dims = self.svg.get_dimensions_sub(id__)[1]
width_, height_ = dims.width, dims.height
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
int(width), int(height))
int(width_), int(height_))
context = cairo.Context(surface)
self.svg.render_cairo_sub(context, id_)
self.svg.render_cairo_sub(context, id__)
tk_image = ImageTk.PhotoImage('RGBA')
image = Image.frombuffer('RGBA', (width, height), surface.get_data(),
image = Image.frombuffer('RGBA', (width_, height_),
bytes(surface.get_data()),
'raw', 'BGRA', 0, 1)
tk_image.paste(image)
return image.crop((0, 0, width, height))
tk_image = ImageTk.PhotoImage(image.crop((0, 0, width, height)))
return tk_image