Skip to content

Commit 1814b6e

Browse files
committed
[feat]: image processing update
1 parent e8a7b50 commit 1814b6e

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

Package.resolved

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/ScriptToolkit/NSImageExtension.swift

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public extension NSImage {
9393
return finalResult
9494
}
9595

96-
func image(withText text: String, attributes: [NSAttributedString.Key: Any], position: CGFloat) -> NSImage {
96+
func image(withText text: String, attributes: [NSAttributedString.Key: Any], horizontalPosition: CGFloat, verticalPosition: CGFloat) -> NSImage {
9797
let image = self
9898
let text = text as NSString
9999
let options: NSString.DrawingOptions = [.usesLineFragmentOrigin, .usesFontLeading]
@@ -102,7 +102,7 @@ public extension NSImage {
102102

103103
let x = (image.size.width - textSize.width) / 2
104104
let y = (image.size.height - textSize.height) / 2
105-
let point = NSMakePoint(x, y * position)
105+
let point = NSMakePoint(x * horizontalPosition, y * verticalPosition)
106106

107107
image.lockFocus()
108108
text.draw(at: point, withAttributes: attributes)
@@ -111,18 +111,44 @@ public extension NSImage {
111111
return image
112112
}
113113

114-
func annotate(text: String, font: String, size: CGFloat, position: CGFloat, fill: NSColor, stroke: NSColor, strokeWidth: CGFloat) throws -> NSImage {
114+
func annotate(
115+
text: String,
116+
font: String,
117+
size: CGFloat,
118+
horizontalTitlePosition: CGFloat,
119+
verticalTitlePosition: CGFloat,
120+
titleAlignment: String,
121+
fill: NSColor,
122+
stroke: NSColor,
123+
strokeWidth: CGFloat
124+
) throws -> NSImage {
115125

116126
guard let titleFont = NSFont(name: font, size: size) else { throw ScriptError.argumentError(message: "Unable to find font \(font)") }
117127

128+
var alignmentMode: CATextLayerAlignmentMode
129+
130+
switch titleAlignment {
131+
case "left":
132+
alignmentMode = .left
133+
134+
case "center":
135+
alignmentMode = .center
136+
137+
case "right":
138+
alignmentMode = .right
139+
}
140+
118141
// Solid color text
119142
let fillText = image(
120143
withText: text,
121144
attributes: [
122145
.foregroundColor: fill,
123-
.font: titleFont
146+
.font: titleFont,
147+
.alignmentMode: alignmentMode
124148
],
125-
position: position)
149+
horizontalTitlePosition: horizontalTitlePosition,
150+
verticalTitlePosition: verticalTitlePosition
151+
)
126152

127153
// Add strokes
128154
return fillText.image(
@@ -131,9 +157,11 @@ public extension NSImage {
131157
.foregroundColor: NSColor.clear,
132158
.strokeColor: stroke,
133159
.strokeWidth: strokeWidth * size,
134-
.font: titleFont
160+
.font: titleFont,
161+
.alignmentMode: alignmentMode
135162
],
136-
position: position)
163+
horizontalTitlePosition: horizontalTitlePosition,
164+
verticalTitlePosition: verticalTitlePosition)
137165
}
138166
}
139167

0 commit comments

Comments
 (0)