From 3637177271c20465390eb45c90e6679aea1b434e Mon Sep 17 00:00:00 2001 From: Peter John Story Date: Sun, 5 Jan 2025 10:16:09 +0100 Subject: [PATCH 1/2] Allow frame on cliparts --- cewe2pdf.py | 65 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 27 deletions(-) diff --git a/cewe2pdf.py b/cewe2pdf.py index 207250b..fbcb0d0 100755 --- a/cewe2pdf.py +++ b/cewe2pdf.py @@ -504,7 +504,11 @@ def processAreaImageTag(imageTag, area, areaHeight, areaRot, areaWidth, imagedir if frameClipartFileName is not None: # we set the transx, transy, and areaRot for the clipart to zero, because our current pdf object # already has these transformations applied. So don't do it twice. - insertClipartFile(frameClipartFileName, [], 0, areaWidth, areaHeight, frameAlpha, pdf, 0, 0, False, False) + # flipX and flipY are also set to false because it cause an exception in PIL + # therefore, even if the CEWE software offers the possibility to flip the clipart frame, cewe2pdf + # remains unable to render it + colorreplacements, flipX, flipY = getClipConfig(imageTag) + insertClipartFile(frameClipartFileName, colorreplacements, 0, areaWidth, areaHeight, frameAlpha, pdf, 0, 0, False, False, None) for decorationTag in area.findall('decoration'): processAreaDecorationTag(decorationTag, areaHeight, areaWidth, pdf) @@ -983,27 +987,11 @@ def loadClipart(fileName) -> ClpFile: return newClpFile -def processAreaClipartTag(clipartElement, areaHeight, areaRot, areaWidth, pdf, transx, transy, alpha): - clipartID = int(clipartElement.get('designElementId')) - # print("Warning: clip-art elements are not supported. (designElementId = {})".format(clipartID)) - - # designElementId 0 seems to be a special empty placeholder - if clipartID == 0: - return - - # Load the clipart - fileName = None - if clipartID in clipartDict: - fileName = clipartDict[clipartID] - # verify preconditions to avoid exception loading the clip art file, which would break the page count - if not fileName: - logging.error(f"Problem getting file name for clipart ID: {clipartID}") - return - +def getClipConfig(Element): colorreplacements = [] flipX = False flipY = False - for clipconfig in clipartElement.findall('ClipartConfiguration'): + for clipconfig in Element.findall('ClipartConfiguration'): for clipcolors in clipconfig.findall('colors'): for clipcolor in clipcolors.findall('color'): source = '#'+clipcolor.get('source').upper()[1:7] @@ -1017,11 +1005,37 @@ def processAreaClipartTag(clipartElement, areaHeight, areaRot, areaWidth, pdf, t flipX = True if mirror in ('x', 'both'): flipY = True + return colorreplacements, flipX, flipY + + +def processAreaClipartTag(clipartElement, areaHeight, areaRot, areaWidth, pdf, transx, transy, clipArtDecoration): + clipartID = int(clipartElement.get('designElementId')) + # print("Warning: clip-art elements are not supported. (designElementId = {})".format(clipartID)) + + # designElementId 0 seems to be a special empty placeholder + if clipartID == 0: + return + + # Load the clipart + fileName = None + if clipartID in clipartDict: + fileName = clipartDict[clipartID] + # verify preconditions to avoid exception loading the clip art file, which would break the page count + if not fileName: + logging.error(f"Problem getting file name for clipart ID: {clipartID}") + return + + alpha = 255 + if clipArtDecoration is not None: + alphatext = clipArtDecoration.get('alpha') # alpha attribute + if alphatext is not None: + alpha = int((float(alphatext)) * 255) - insertClipartFile(fileName, colorreplacements, transx, areaWidth, areaHeight, alpha, pdf, transy, areaRot, flipX, flipY) + colorreplacements, flipX, flipY = getClipConfig(clipartElement) + insertClipartFile(fileName, colorreplacements, transx, areaWidth, areaHeight, alpha, pdf, transy, areaRot, flipX, flipY, clipArtDecoration) -def insertClipartFile(fileName:str, colorreplacements, transx, areaWidth, areaHeight, alpha, pdf, transy, areaRot, flipX, flipY): +def insertClipartFile(fileName:str, colorreplacements, transx, areaWidth, areaHeight, alpha, pdf, transy, areaRot, flipX, flipY, decoration): img_transx = transx res = image_res # use the foreground resolution setting for clipart @@ -1048,6 +1062,8 @@ def insertClipartFile(fileName:str, colorreplacements, transx, areaWidth, areaHe pdf.drawImage(ImageReader(clipart.pngMemFile), f * -0.5 * areaWidth, f * -0.5 * areaHeight, width=f * areaWidth, height=f * areaHeight, mask='auto') + if decoration is not None: + processAreaDecorationTag(decoration, areaHeight, areaWidth, pdf) pdf.rotate(areaRot) pdf.translate(-img_transx, -transy) @@ -1104,14 +1120,9 @@ def processElements(additional_fonts, fotobook, imagedir, # In the clipartarea there are two similar elements, the and the . # We are using the element here if area.get('areatype') == 'clipartarea': # only look for alpha and clipart within clipartarea tags - alpha = 255 decoration = area.find('decoration') # decoration tag - if decoration is not None: - alphatext = decoration.get('alpha') # alpha attribute - if alphatext is not None: - alpha = int((float(alphatext)) * 255) for clipartElement in area.findall('clipart'): - processAreaClipartTag(clipartElement, areaHeight, areaRot, areaWidth, pdf, transx, transy, alpha) + processAreaClipartTag(clipartElement, areaHeight, areaRot, areaWidth, pdf, transx, transy, decoration) return From 8b654fa91b70bd38fee3117e8618f34efff5a869 Mon Sep 17 00:00:00 2001 From: Peter John Story Date: Sun, 5 Jan 2025 10:17:33 +0100 Subject: [PATCH 2/2] Correct comments on the clipart frame code --- cewe2pdf.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cewe2pdf.py b/cewe2pdf.py index fbcb0d0..39f2024 100755 --- a/cewe2pdf.py +++ b/cewe2pdf.py @@ -1010,7 +1010,6 @@ def getClipConfig(Element): def processAreaClipartTag(clipartElement, areaHeight, areaRot, areaWidth, pdf, transx, transy, clipArtDecoration): clipartID = int(clipartElement.get('designElementId')) - # print("Warning: clip-art elements are not supported. (designElementId = {})".format(clipartID)) # designElementId 0 seems to be a special empty placeholder if clipartID == 0: @@ -1119,8 +1118,9 @@ def processElements(additional_fonts, fotobook, imagedir, # Clip-Art # In the clipartarea there are two similar elements, the and the . # We are using the element here - if area.get('areatype') == 'clipartarea': # only look for alpha and clipart within clipartarea tags - decoration = area.find('decoration') # decoration tag + if area.get('areatype') == 'clipartarea': + # within clipartarea tags we need the decoration for alpha and border information + decoration = area.find('decoration') for clipartElement in area.findall('clipart'): processAreaClipartTag(clipartElement, areaHeight, areaRot, areaWidth, pdf, transx, transy, decoration) return