politza / pdf-tools

Emacs support library for PDF files.
GNU General Public License v3.0
1.57k stars 162 forks source link

Support retina display on OSX #51

Closed brabalan closed 2 years ago

brabalan commented 9 years ago

Documents viewed with pdf-view are much fuzzier than when viewed with doc-view (I have set doc-view-resolution to 300). How can I increase the resolution for pfd-view? (I looked at the pdf-tools and pdf-view customize group but could not find a relevant option.)

politza commented 9 years ago

Alan Schmitt notifications@github.com writes:

Documents viewed with pdf-view are much fuzzier than when viewed with doc-view (I have set doc-view-resolution to 300).

Please post a comparison screenshot ?

brabalan commented 9 years ago

doc-view: screen shot 2015-02-14 at 10 31 55 pfd-view: screen shot 2015-02-14 at 10 31 12

politza commented 9 years ago

Alan Schmitt notifications@github.com writes:

doc-view: screen s pfd-view: screen s

Could this be some bad anti-aliasing ?

As far as I know there is no need for a resolution in poppler, since all PDF objects (images unless embedded, fonts) are vector-based, i.e. can be, and are, rendered arbitrary large. Anyway, there are no knobs for this. The resolution in doc-view simply determines the size of image, but this is done automatically in pdf-view.

Would you mind uploading the PDF page ?

gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dFirstPage=17 -dLastPage=17 -sOutputFile=page_17.pdf file.pdf

-ap

brabalan commented 9 years ago

Here is the page: https://transfert.inria.fr/fichiers/038db3f7d96457232984b2f9ccae2355/page_17.pdf

This may be an emacs + retina macbook issue. I use this emacs https://github.com/railwaycat/emacs-mac-port which may do something special to display png. I'll open an issue there.

Edit: this probably explains why docview is not blury: https://github.com/railwaycat/emacs-mac-port/blob/7be6d9e8dbd122b060b058c0a345091debc52975/doc/emacs/macport.texi#L401

politza commented 9 years ago

Alan Schmitt notifications@github.com writes:

Here is the page:

They look the same to me in doc-view and pdf-view.

-ap

politza commented 9 years ago

Does this advice do the trick ?

(defadvice pdf-info-renderpage (before double-width-arg activate)
  (ad-set-arg 1 (* 2 (ad-get-arg 1))))
brabalan commented 9 years ago

Unfortunately, no. (It also breaks the function that deal with resizing: fit to page is broken, and shrink-pdf and enlarge-pdf both enlarge it.)

PS: I don't think the blurry aspect is a bug on your side, it's more of a feature missing of emacs for OS X on Macs with retina display. I just tried increasing the resolution, and the picture becomes sharper. Unfortunately I don't know how to get pixel resolution to do a conclusive test.

brabalan commented 9 years ago

I received an answer from YAMAMOTO Mitsuharu and he suggests an approach:

Multiple-resolution bitmap feature is only provided for files (i.e., "@2x" convention) or via TIFF format that can contain multiple bitmaps. So, it is not directly applicable to the pdf-tools case where PNG data is read from memory.

I skimmed over the pdf-tools source code, and I thought another solution would be possible. The Mac port can render PDF data natively in a resolution-independent way as described in the "Mac Images and Colors" node in Emacs info:

The image-io image type also provides rasterization of several document formats such as PDF, RTF, HTML, WEBARCHIVE, DOC, etc. The list of supported formats can be obtained by evaluating (image-io-types). Because the image-io image type accepts the same image descriptor properties as the imagemagick image type does (*note (elisp)ImageMagick Images::), you can specify the page number (0 for the first page) of the document by the :index property and get the number of pages by the image-metadata function.

So, it would be possible to make epdfinfo generate "distilled" PDF data using cairo PDF surface (rather than rasterized PNG data using cairo image surface), and then let the Mac port rasterize the PDF data using the image-io image type. This way, the Mac port can automatically re-render the generated PDF data even on resolution changes.

As SVG images are inherently resolution-independent, the images are automatically rendered with appropriate scaling for high-resolution environment. Rasterization of documents by the image-io image type mentioned above also takes account of resolution. Note that resolution can change dynamically on Mac OS X 10.7 and later even for the same frame, when it is moved from one monitor to another for example. The Mac port detects such a resolution change and re-render images or reload files automatically.

Do you think this could work?

politza commented 9 years ago

I might (w/o to much trouble) if image-io behaves like an image, e.g. works with image-mode.

brabalan commented 9 years ago

My understanding is that it works like imagemagick. Don't you already have support for it?

politza commented 9 years ago

Alan Schmitt notifications@github.com writes:

My understanding is that it works like imagemagick. Don't you already have support for it?

Yes...

brabalan commented 9 years ago

Thank you for doing it. I'll test it as soon as it's propagated on Melpa.

brabalan commented 9 years ago

I gave it a test and I do not see any change. Is there something special I need to do to activate it?

politza commented 9 years ago

Alan Schmitt notifications@github.com writes:

I gave it a test and I do not see any change. Is there something special I need to do to activate it?

I closed the issue because in my view it was resolved. I'm not going to implement anything Mac specific.

brabalan commented 9 years ago

OK, I updated the issue title to reflect this.

dbrumley commented 9 years ago

I know the issue is closed, but I also would like retina resolution on pdf's on my mac. Putting this message here in case someone has an alternate solution/setup for those looking at this issue. (I don't get retina inside docview, either though).

brabalan commented 9 years ago

@dbrumley To get retina support in docview, you need to use this emacs version: https://github.com/railwaycat/emacs-mac-port.

In the meantime, I have found a workaround: I run at full retina resolution (and not half resolution by default). I have to increase the font size in emacs of course, but everything is super sharp.

dbrumley commented 9 years ago

I'm sure this is a dumb question, but I couldn't find the answer googling. How do you run it at full resolution? Do you mean you run your mac itself at a non-default resolution, or there is an emacs setting?

brabalan commented 9 years ago

It's a mac thing: you need an utility that gives you access to a "scaled" resolution that is actually not scaled (the preference panel does not let you use the full resolution of the screen).

I went back to the normal "double" resolution because the interface is really not meant to run at such a high resolution. So I'm back to square 1.

dbrumley commented 9 years ago

I'm posting here for posterity. If you are using macports, you can port install emacs-mac-app which installs a version with retina support. Now to view a pdf in retina mode you should name the file "foo@2x.pdf". I picked this up from https://github.com/railwaycat/emacs-mac-port/blob/7be6d9e8dbd122b060b058c0a345091debc52975/doc/emacs/macport.texi#L401

chuchana commented 9 years ago

@dbrumley In PDFView mode, too?

srustamo commented 8 years ago

I'm using 'emacs-mac-port', DocView renders pdfs in retina resolution. 'pdf-tools' does not. I prefer 'pdf-tools' becasue of many pdf tools it offers (outline mode etc) and hope that it will be possible to render in retina resolution with 'pdf-tools'.

plaidfinch commented 8 years ago

This issue is really important to me, and I suspect to other users as well. I'm willing to work on providing a fix, if given a pointer in the right direction. Where in the codebase should I start poking about?

dbrumley commented 8 years ago

On OSX:

$ sudo port install emacs-mac-app -imagemagick -rsvg

I believe, but do not know, that this is a fork of https://github.com/railwaycat/emacs-mac-port.git

politza commented 8 years ago

Try to look into the image-io imagetype as suggested here:

https://github.com/politza/pdf-tools/issues/51#issuecomment-75914942

seanfarley commented 8 years ago

Just wanted to chime in that this would be really, really nice to have. I was hoping @kwf made some progress to see if I could help but couldn't find any.

Also, @dbrumley, I'm one of the maintainers of the emacs-mac-app in MacPorts. The default variants shouldn't have affected retina support. If it's still a problem for you, feel free to open a bug with MacPorts.

plaidfinch commented 8 years ago

@seanfarley, I haven't had much time to take a look at this, but I'll certainly let you know if I get a chance to make any progress.

LordZorgoth commented 7 years ago

I'd just like to point out that there is a very ugly workaround to this issue, which certainly is not a good fix, but works for me until someone implements a patch.

The workaround is to use a utility that enables running the display at full Retina resolution, and then ensure that the full Retina resolution is only used when emacs is focused. The resolution transition is ugly, but if, like me, you primarily use emacs in fullscreen mode, it is bearable. Note that in order to enable full resolution on El Capitan and newer versions of OS X, you will need to disable system integrity protection (SIP). You only need to do this once; once the setting is enabled, you can switch to and from that resolution dynamically with system integrity protection on.

You will need to increase the default font height to keep emacs readable. I find that 256 works well on my 15" 2880x1800 display. I am using a paid, closed source piece of software because I figured I just needed something that worked and it wasn't worth an hour or three right then to find another piece of software.

I imagine that it would not be too difficult to write an open-source script to do a similar operation, and perhaps to improve on it by only changing the resolution only when emacs is fullscreen, if you only work with pdfs in fullscreen windows). I am not about to write one because I don't have time.

The closed-source tool I am currently using is SwitchResX. It has a trial version that works for 10 days, then costs $16. You'd want to create two "display sets," one called Full Resolution that is set to the native resolution of your monitor, and one called Native Resolution to the resolution you prefer to work in for other applications. In SetResX Preferences > Applications, set All Applications to Normal Resolution and Emacs to Full Resolution, checking the boxes Reset Display Settings when Application Ends and Apply again display settings when application comes to front. Before it will actually work, you'll need to disable SIP and move into full resolution. You can then reenable SIP. More details on SIP here: http://www.madrau.com/support/support/srx_1011.html

hisnawi commented 7 years ago

Any update on this?

m-falconi commented 7 years ago

It has been a while, but @politza can you develop a bit more on how one could implement the suggestion in Yamamoto comment above? I.e. how to make epdfinfo produce a pdf and then raster it with image-io?

Alternatively, is someone already working on a different fix?

LuhengStat commented 6 years ago

@m-falconi I also want to know the answer, do you have some suggestions now?

m-falconi commented 6 years ago

@LuhengStat Unfortunately I have no suggestions. I adopted the most radical workaround of working with my retina display at native resolution (2880x1800). Since for my workflow I use only emacs and chrome, this solution is acceptable for me (and I can switch to more usable resolutions in other contexts). But for sure this is not a solution at all.

vancleve commented 6 years ago

I'm on mac os and would also very much appreciate a fix here. I imagine though that high DPI screens are common in linux too.

I don't have much experience in lisp, but can help if necessary.

martinweiss commented 6 years ago

I am crossing my fingers that someone came up with a solution for this. Anyone?

martinweiss commented 6 years ago

I'm posting a screenshot to show what it looks like on my setup:

screen shot 2018-08-28 at 12 47 03
jsks commented 5 years ago

In case this helps anyone, if you're using emacs-mac, Yamamoto Mitsuharu's patch to use image-io still works.

https://lists.gnu.org/archive/html/emacs-devel/2016-04/msg00648.html

srustamo commented 5 years ago

@jsks Did you need to compile emacs-mac with some image-io swtiches? If yes, which ones?

jsks commented 5 years ago

Defaults are fine. Imagemagick seems to work as well with the patch given that it's the same code using backing-scale-factor, but I've never tested it extensively.

Note, the patch doesn't apply cleanly anymore so it requires some manual intervention.

srustamo commented 5 years ago

So, you applied the patch and then installed with no additional switches?

We're there many diffs? I mean how involved is the patching?

On Thu, Jan 3, 2019, 19:39 jsks <notifications@github.com wrote:

Defaults are fine. Imagemagick seems to work as well with the patch given that it's the same code using backing-scale-factor, but I've never tested it extensively.

Note, the patch doesn't apply cleanly anymore so it requires some manual intervention.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/politza/pdf-tools/issues/51#issuecomment-451161939, or mute the thread https://github.com/notifications/unsubscribe-auth/AAtw9Uc3tKJ6TLzY_o6G0ZYBzRuSkP36ks5u_hY2gaJpZM4DgefO .

jsks commented 5 years ago

@srustamo If it's easier here's the clean patch as a gist

Clone pdf-tools, download the gist, and apply it using git apply retina.patch. After that follow the instructions in pdf-tools README as normal.

srustamo commented 5 years ago

Cheers!

On Thu, Jan 3, 2019 at 21:14 jsks notifications@github.com wrote:

@srustamo https://github.com/srustamo If it's easier here's the clean patch as a gist https://gist.github.com/jsks/056a4e950c8bc410aabe3c219fbd4b25

Clone pdf-tools, download the gist, and apply it using git apply retina.patch. After that follow the instructions in pdf-tools README as normal.

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/politza/pdf-tools/issues/51#issuecomment-451192152, or mute the thread https://github.com/notifications/unsubscribe-auth/AAtw9UjmqsMkkICW8SLA44ZdnsXswnCdks5u_ixOgaJpZM4DgefO .

carlosgeos commented 5 years ago

I can confirm the patch is working with emacs-mac (other versions of Emacs do not work). I had to tweak it a little bit though cause one hunk failed --> this my version

I ran patch < retina.patch directly on my .emacs.d/elpa/pdf-tools-20181221.1913 folder as a temporary solution. Remember to remove the .elc files !

seanfarley commented 5 years ago

I've forked this repo so I can just keep rebasing the patch: https://github.com/seanfarley/pdf-tools/commits/retina

Personally, I use quelpa to install this.

@politza Is there any way we can get this merged somehow upstream? Maybe even behind a flag? Or will we continually keep having to rebase this until the svg work is done?

P.S. the title of this issue should probably be changed since it applies to linux hi-dpi equally as well as macos retina

fuxialexander commented 5 years ago

I made another PR, utilizing the native image scaling on Emacs NS-port master branch: https://github.com/politza/pdf-tools/pull/501

surfcao commented 5 years ago

I made another PR, utilizing the native image scaling on Emacs NS-port master branch:

501

Tried this in Emacs 26.3, it doesn't seem to working. Pdf still shows as blurry.

politza commented 5 years ago

It's hard-coded to only work in a cvs-build.

taquangtrung commented 4 years ago

I made another PR, utilizing the native image scaling on Emacs NS-port master branch:

501

Tried this in Emacs 26.3, it doesn't seem to working. Pdf still shows as blurry.

It doesn't work with Emacs 26.3 in my mac either.

chuxubank commented 4 years ago

For those who still don't know how to enable hidpi support, thx to @dzop , now it can easily be enabled with

  (setq pdf-view-use-scaling t)

but with a large cost of memory.

Detail

taquangtrung commented 4 years ago

@chuxubank Thanks a lot. It works for my case.

guoqinglei commented 4 years ago

@chuxubank I have tried your method with Emacs 26.3 on macOS Catalina , but it still doesn't work for me.

Anyone has some other methods ?

Thanks a lot.