mirror of
https://github.com/shlomif/PySolFC.git
synced 2025-04-15 02:54:09 -04:00
hacky way to load svg fragments
does not work quite right - it is a start.
This commit is contained in:
parent
f5690a3285
commit
869bcece37
3 changed files with 41 additions and 25 deletions
|
@ -25,10 +25,13 @@
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from pysollib.mfxutil import Image, ImageTk, USE_PIL, print_err
|
from pysollib.mfxutil import Image, ImageTk, USE_PIL, print_err
|
||||||
|
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 copyImage, createBottom, createImage, loadImage
|
||||||
from pysollib.pysoltk import shadowImage
|
from pysollib.pysoltk import shadowImage
|
||||||
from pysollib.resource import CSI
|
from pysollib.resource import CSI
|
||||||
from pysollib.settings import TOOLKIT
|
from pysollib.settings import TOOLKIT
|
||||||
|
from pysollib.ui.tktile.svg import SVGManager
|
||||||
|
|
||||||
# ************************************************************************
|
# ************************************************************************
|
||||||
# * Images
|
# * Images
|
||||||
|
@ -76,16 +79,22 @@ class Images:
|
||||||
def destruct(self):
|
def destruct(self):
|
||||||
pass
|
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
|
# print '__loadCard:', filename
|
||||||
f = os.path.join(self.cs.dir, filename)
|
f = os.path.join(self.cs.dir, filename)
|
||||||
if not os.path.exists(f):
|
if rec and 'suit' in rec:
|
||||||
print_err('card image path %s does not exist' % f)
|
# img = PIL_Image(image=self.svg.render_fragment("6_heart"))
|
||||||
return None
|
img = self.svg.render_fragment(
|
||||||
try:
|
id_="6_heart", width=self.CARDW, height=self.CARDH)
|
||||||
img = loadImage(file=f)
|
img = PIL_Image(image=img)
|
||||||
except Exception:
|
else:
|
||||||
return None
|
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':
|
if TOOLKIT == 'kivy':
|
||||||
w = img.texture.size[0]
|
w = img.texture.size[0]
|
||||||
|
@ -170,8 +179,12 @@ class Images:
|
||||||
pstep += self.cs.nshadows + 1 # shadows & shade
|
pstep += self.cs.nshadows + 1 # shadows & shade
|
||||||
pstep = max(0, (80.0 - progress.percent) / pstep)
|
pstep = max(0, (80.0 - progress.percent) / pstep)
|
||||||
# load face cards
|
# load face cards
|
||||||
for n in self.cs.getFaceCardNames():
|
self.svg = SVGManager(
|
||||||
self._card.append(self.__loadCard(n + self.cs.ext))
|
'/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
|
self._card[-1].filename = n
|
||||||
if progress:
|
if progress:
|
||||||
progress.update(step=pstep)
|
progress.update(step=pstep)
|
||||||
|
|
|
@ -357,14 +357,15 @@ class Cardset(Resource):
|
||||||
names = []
|
names = []
|
||||||
for suit in self.suits:
|
for suit in self.suits:
|
||||||
for rank in self.ranks:
|
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:
|
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
|
assert len(names) == self.ncards
|
||||||
return names
|
return names
|
||||||
|
|
||||||
def getPreviewCardNames(self):
|
def getPreviewCardNames(self):
|
||||||
names = self.getFaceCardNames()
|
names = [x for x, _ in self.getFaceCardNames()]
|
||||||
pnames = []
|
pnames = []
|
||||||
ranks, suits = self.ranks, self.suits
|
ranks, suits = self.ranks, self.suits
|
||||||
lr, ls = len(ranks), len(suits)
|
lr, ls = len(ranks), len(suits)
|
||||||
|
|
|
@ -5,30 +5,32 @@
|
||||||
# https://stackoverflow.com/questions/22583035
|
# https://stackoverflow.com/questions/22583035
|
||||||
# Thanks!
|
# Thanks!
|
||||||
|
|
||||||
import Image
|
|
||||||
|
|
||||||
import ImageTk
|
|
||||||
|
|
||||||
import cairo
|
import cairo
|
||||||
|
|
||||||
import rsvg
|
from gi.repository import Rsvg
|
||||||
|
|
||||||
|
from pysollib.mfxutil import Image, ImageTk
|
||||||
|
|
||||||
|
|
||||||
class SVGManager:
|
class SVGManager:
|
||||||
"""docstring for SVGManager"""
|
"""docstring for SVGManager"""
|
||||||
def __init__(self, filename):
|
def __init__(self, filename):
|
||||||
self.filename = 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_"""
|
"""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,
|
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
|
||||||
int(width), int(height))
|
int(width_), int(height_))
|
||||||
context = cairo.Context(surface)
|
context = cairo.Context(surface)
|
||||||
self.svg.render_cairo_sub(context, id_)
|
self.svg.render_cairo_sub(context, id__)
|
||||||
tk_image = ImageTk.PhotoImage('RGBA')
|
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)
|
'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
|
return tk_image
|
||||||
|
|
Loading…
Add table
Reference in a new issue