Skip to content

Commit

Permalink
Fix image scaling issue when rendering svg onto webview
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-pod committed Apr 9, 2022
1 parent 56d19df commit 66e17e6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
12 changes: 12 additions & 0 deletions Sources/SVGConverterCore/Internal/PixelScaleWindow.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// PixelScaleWindow.swift
//
//
// Created by Alexandre Podlewski on 09/04/2022.
//

import AppKit

class PixelScaleWindow: NSWindow {
override var backingScaleFactor: CGFloat { return 1 }
}
8 changes: 1 addition & 7 deletions Sources/SVGConverterCore/Internal/WebViewSVGRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,8 @@ final class WebViewSVGRenderer: WKWebView, WKNavigationDelegate {
}
isRendering = true
self.completion = completion
let webViewScale = layer?.contentsScale ?? 1
self.inPixelSize = size
self.inPointSize = NSSize(
width: size.width / webViewScale,
height: size.height / webViewScale
)
self.inPointSize = self.convertFromBacking(size)
do {
let resizedSVGData = try resizeSVG(svgData, to: self.inPointSize)
guard let svgString = String(data: resizedSVGData, encoding: .utf8) else {
Expand Down Expand Up @@ -119,8 +115,6 @@ final class WebViewSVGRenderer: WKWebView, WKNavigationDelegate {
)
)

webView.layer?.contentsScale = 1.0
webView.layer?.rasterizationScale = 0.1
webView.frame.size = config.rect.size
webView.takeSnapshot(with: config) { [weak self] image, error in
guard let self = self else { return }
Expand Down
10 changes: 10 additions & 0 deletions Sources/SVGConverterCore/SVGRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import Foundation
import AppKit

@available(macOS 10.15, *)
@MainActor
Expand Down Expand Up @@ -51,6 +52,7 @@ public final class SVGRenderer {

// MARK: - Private Properties

private let rendererWindow: NSWindow
private let renderer: WebViewSVGRenderer

// MARK: - Life cycle
Expand All @@ -70,6 +72,14 @@ public final class SVGRenderer {
}
self.renderer = WebViewSVGRenderer(options: options)

// ???: (Alexandre Podlewski) 09/04/2022 Without forcing the backing scale factor it is not possible to control
// the output of screeshoting the web view. Without window issues could also occurred when changing of
// focused screen with a different scale factor. An issue occurred when focused on a x1 screen, with
// the webview outside of a window it had a default backing scale factor of x2 but the snapshot was taken
// as x1, this causing invalid size generated images.
self.rendererWindow = PixelScaleWindow()
self.rendererWindow.contentView = self.renderer

self.warningHandler = warningHandler
self.renderer.warningHandler = warningHandler
}
Expand Down

0 comments on commit 66e17e6

Please sign in to comment.