|
|
@@ -116,7 +116,7 @@ enum PrintService {
|
|
|
guard let operation = document.printOperation(
|
|
|
for: printInfo,
|
|
|
scalingMode: .pageScaleToFit,
|
|
|
- autoRotate: true
|
|
|
+ autoRotate: false
|
|
|
) else {
|
|
|
showPrintFailedAlert()
|
|
|
return
|
|
|
@@ -178,7 +178,7 @@ enum PrintService {
|
|
|
printInfo.horizontalPagination = .fit
|
|
|
printInfo.verticalPagination = .fit
|
|
|
printInfo.isHorizontallyCentered = true
|
|
|
- printInfo.isVerticallyCentered = true
|
|
|
+ printInfo.isVerticallyCentered = false
|
|
|
printInfo.topMargin = 36
|
|
|
printInfo.bottomMargin = 36
|
|
|
printInfo.leftMargin = 36
|
|
|
@@ -192,9 +192,14 @@ enum PrintService {
|
|
|
let pageSize = printInfo.paperSize
|
|
|
guard pageSize.width > 0, pageSize.height > 0 else { return nil }
|
|
|
|
|
|
+ let paragraphStyle = NSMutableParagraphStyle()
|
|
|
+ paragraphStyle.alignment = .left
|
|
|
+ paragraphStyle.lineBreakMode = .byWordWrapping
|
|
|
+
|
|
|
let attributes: [NSAttributedString.Key: Any] = [
|
|
|
.font: NSFont.systemFont(ofSize: 12),
|
|
|
.foregroundColor: NSColor.black,
|
|
|
+ .paragraphStyle: paragraphStyle,
|
|
|
]
|
|
|
let attributed = NSAttributedString(string: text, attributes: attributes)
|
|
|
|
|
|
@@ -214,8 +219,7 @@ enum PrintService {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
- var glyphIndex = 0
|
|
|
- while glyphIndex < layoutManager.numberOfGlyphs {
|
|
|
+ while true {
|
|
|
let textContainer = NSTextContainer(size: containerSize)
|
|
|
textContainer.lineFragmentPadding = 0
|
|
|
layoutManager.addTextContainer(textContainer)
|
|
|
@@ -223,24 +227,57 @@ enum PrintService {
|
|
|
let glyphRange = layoutManager.glyphRange(for: textContainer)
|
|
|
guard glyphRange.length > 0 else { break }
|
|
|
|
|
|
- context.beginPDFPage(nil)
|
|
|
+ let charRange = layoutManager.characterRange(forGlyphRange: glyphRange, actualGlyphRange: nil)
|
|
|
+ let pageText = attributed.attributedSubstring(from: charRange)
|
|
|
|
|
|
- NSGraphicsContext.saveGraphicsState()
|
|
|
- NSGraphicsContext.current = NSGraphicsContext(cgContext: context, flipped: false)
|
|
|
- layoutManager.drawGlyphs(
|
|
|
- forGlyphRange: glyphRange,
|
|
|
- at: NSPoint(x: printInfo.leftMargin, y: printInfo.bottomMargin)
|
|
|
+ context.beginPDFPage(nil)
|
|
|
+ drawTextPage(
|
|
|
+ pageText,
|
|
|
+ in: context,
|
|
|
+ pageSize: pageSize,
|
|
|
+ printInfo: printInfo,
|
|
|
+ textWidth: textWidth,
|
|
|
+ textHeight: textHeight
|
|
|
)
|
|
|
- NSGraphicsContext.restoreGraphicsState()
|
|
|
-
|
|
|
context.endPDFPage()
|
|
|
- glyphIndex = NSMaxRange(glyphRange)
|
|
|
+
|
|
|
+ if NSMaxRange(glyphRange) >= layoutManager.numberOfGlyphs { break }
|
|
|
}
|
|
|
|
|
|
context.closePDF()
|
|
|
return PDFDocument(data: pdfData as Data)
|
|
|
}
|
|
|
|
|
|
+ private static func drawTextPage(
|
|
|
+ _ text: NSAttributedString,
|
|
|
+ in context: CGContext,
|
|
|
+ pageSize: NSSize,
|
|
|
+ printInfo: NSPrintInfo,
|
|
|
+ textWidth: CGFloat,
|
|
|
+ textHeight: CGFloat
|
|
|
+ ) {
|
|
|
+ context.saveGState()
|
|
|
+ context.translateBy(x: 0, y: pageSize.height)
|
|
|
+ context.scaleBy(x: 1, y: -1)
|
|
|
+
|
|
|
+ let drawRect = CGRect(
|
|
|
+ x: printInfo.leftMargin,
|
|
|
+ y: printInfo.topMargin,
|
|
|
+ width: textWidth,
|
|
|
+ height: textHeight
|
|
|
+ )
|
|
|
+
|
|
|
+ NSGraphicsContext.saveGraphicsState()
|
|
|
+ NSGraphicsContext.current = NSGraphicsContext(cgContext: context, flipped: true)
|
|
|
+ text.draw(
|
|
|
+ with: drawRect,
|
|
|
+ options: [.usesLineFragmentOrigin, .usesFontLeading]
|
|
|
+ )
|
|
|
+ NSGraphicsContext.restoreGraphicsState()
|
|
|
+
|
|
|
+ context.restoreGState()
|
|
|
+ }
|
|
|
+
|
|
|
private static func pdfDocument(from image: NSImage) -> PDFDocument? {
|
|
|
let printInfo = configuredPrintInfo()
|
|
|
let pageSize = printInfo.paperSize
|