Skip to content

Commit 477bef2

Browse files
committed
Add support for saving CMYK images
1 parent f115a0e commit 477bef2

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

drawBot/context/baseContext.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,10 @@ def textBox(self, txt, box, font=_FALLBACKFONT, fontSize=10, align=None, hyphena
393393
path, (x, y) = context._getPathForFrameSetter(box)
394394
attributedString = context.attributedString(txt, align)
395395

396+
print("hip", hyphenation)
397+
if hyphenation:
398+
attributedString = context.hyphenateAttributedString(attributedString, path)
399+
396400
setter = CoreText.CTFramesetterCreateWithAttributedString(attributedString)
397401
frame = CoreText.CTFramesetterCreateFrame(setter, (0, 0), path, None)
398402
ctLines = CoreText.CTFrameGetLines(frame)

drawBot/context/imageContext.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ def _tiffCompressionConverter(value):
3131
return t.get(value.lower(), AppKit.NSTIFFCompressionNone)
3232

3333

34+
def _colorSpaceConverter(value):
35+
if value == "CMYK":
36+
return AppKit.NSDeviceCMYKColorSpace
37+
return AppKit.NSCalibratedRGBColorSpace
38+
39+
3440
_nsImageOptions = {
3541
# DrawBot Key NSImage property key converter or None doc
3642
"imageColorSyncProfileData": (AppKit.NSImageColorSyncProfileData, _nsDataConverter, "A bytes or NSData object containing the ColorSync profile data."),
@@ -70,6 +76,7 @@ class ImageContext(PDFContext):
7076
("antiAliasing", "Indicate if a the image should be rendedered with anti-aliasing. Default is True."),
7177
("fontSubpixelQuantization", "A Boolean value that specifies whether subpixel quantization of glyphs is allowed. Default is True."),
7278
("multipage", "Output a numbered image for each page or frame in the document."),
79+
("colorSpace", "Set color space (RGB, CMYK). Default is RGB."),
7380
]
7481

7582
ensureEvenPixelDimensions = False
@@ -90,6 +97,10 @@ def _writeDataToFile(self, data, path, options):
9097
imageResolution = options.get("imageResolution", 72.0)
9198
antiAliasing = options.get("antiAliasing", True)
9299
fontSubpixelQuantization = options.get("fontSubpixelQuantization", True)
100+
101+
colorSpace = options.get("colorSpace", "RGB")
102+
colorSpaceName = _colorSpaceConverter(colorSpace)
103+
93104
properties = {}
94105
for key, value in options.items():
95106
if key in _nsImageOptions:
@@ -105,13 +116,21 @@ def _writeDataToFile(self, data, path, options):
105116
pdfPage=page,
106117
antiAliasing=antiAliasing,
107118
fontSubpixelQuantization=fontSubpixelQuantization,
108-
imageResolution=imageResolution
119+
imageResolution=imageResolution,
120+
colorSpaceName=colorSpaceName,
109121
)
122+
123+
if "imageColorSyncProfileData" in options:
124+
imageRep.setProperty_withValue_(AppKit.NSImageColorSyncProfileData, options["imageColorSyncProfileData"]) # doing this with representationUsingType_properties_ does not work
125+
110126
if self.ensureEvenPixelDimensions:
111127
if imageRep.pixelsWide() % 2 or imageRep.pixelsHigh() % 2:
112128
raise DrawBotError("Exporting to %s doesn't support odd pixel dimensions for width and height." % (", ".join(self.fileExtensions)))
129+
113130
imageData = imageRep.representationUsingType_properties_(self._saveImageFileTypes[ext], properties)
131+
114132
imagePath = fileName + pathAdd + fileExt
133+
115134
self._storeImageData(imageData, imagePath)
116135
pathAdd = "_%s" % (index + 2)
117136
del page, imageRep, imageData
@@ -133,13 +152,17 @@ def _makeBitmapImageRep(nsImage=None, pdfPage=None, imageResolution=72.0, antiAl
133152
elif nsImage is not None:
134153
width, height = nsImage.size()
135154

155+
hasAlpha = True
156+
if colorSpaceName == AppKit.NSDeviceCMYKColorSpace:
157+
hasAlpha = False # Quartz doesn’t support alpha for CMYK bitmaps.
158+
136159
rep = AppKit.NSBitmapImageRep.alloc().initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_(
137160
None, # planes
138161
int(width * scaleFactor), # pixelsWide
139162
int(height * scaleFactor), # pixelsHigh
140163
8, # bitsPerSample
141164
4, # samplesPerPixel
142-
True, # hasAlpha
165+
hasAlpha, # hasAlpha
143166
False, # isPlanar
144167
colorSpaceName, # colorSpaceName
145168
0, # bytesPerRow

0 commit comments

Comments
 (0)