Skip to content

Commit

Permalink
Merge pull request bash0#194 from AnEnglishmanInNorway/clipartframe
Browse files Browse the repository at this point in the history
Cliparts can now have frames
  • Loading branch information
AnEnglishmanInNorway authored Jan 5, 2025
2 parents b5b30b3 + 8b654fa commit d40a174
Showing 1 changed file with 40 additions and 29 deletions.
69 changes: 40 additions & 29 deletions cewe2pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand All @@ -1017,11 +1005,36 @@ 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'))

# 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
Expand All @@ -1048,6 +1061,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)

Expand Down Expand Up @@ -1103,15 +1118,11 @@ def processElements(additional_fonts, fotobook, imagedir,
# Clip-Art
# In the clipartarea there are two similar elements, the <designElementIDs> and the <clipart>.
# We are using the <clipart> 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)
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, alpha)
processAreaClipartTag(clipartElement, areaHeight, areaRot, areaWidth, pdf, transx, transy, decoration)
return


Expand Down

0 comments on commit d40a174

Please sign in to comment.