Permalink
Browse files

Respect DPI of loaded images optionally. Modify LaTeX Preview mode.

Preview now generates double-resolution images; Emacs will respect DPI in this case.
  • Loading branch information...
1 parent 8331e67 commit 0303dcd6c9f05afe4a548c3fd3488626a9cea74d @davidswelt committed Jul 4, 2012
Showing with 110 additions and 20 deletions.
  1. +1 −1 aquamacs/doc/latex/changelog.tex
  2. +12 −6 aquamacs/src/site-lisp/edit-modes/auctex/preview.el
  3. +74 −12 src/nsimage.m
  4. +23 −1 src/nsterm.m
@@ -8,7 +8,7 @@ \subsection{Changes--- dev}
\item S-tab no longer mapped to C-y (ASCII 25, end of medium). Now properly handled as backtab. Patch by Gracjan Polak.
\item Full-screen now supports the standard Mac OS X 10.7 ``Lion'' full-screen mode: The frame takes up the full (main) display and uses its own screen. Users may switch between screens as usual (mouse swipe or C-left/right, for instance). The old-style fullscreen mode is still available, however. To maximize a frame on the current space, allowing other frames on this space, type C-u A-S-Return, or prepend any of the commands that enter fullscreen mode with C-u.
Thanks for research: Sandy Patterson and Daisuke Murase.
-\item Retina displays are now supported (higher-resolution artwork is included in most cases).
+\item Retina displays are now supported (high-resolution artwork is included in most cases). LaTeX Preview mode (AUCTeX) has been modified to show high-resolution previews. (Technical note: other packages can be updated by providing TIFFs with multiple resolutions, or by generating high-DPI images, setting their DPI meta information correctly, and setting the new `ns-true-dpi-images-filename-string' variable while loading images.)
\item Aquamacs now provides experimental session persistency: you can load and save sessions. Use the new {\em Load Session, Save Session As} menu entries in the {\em File} menu to use these new functions. Note: Unlike the formerly available functions from the `desktop' package, all frames, windows, tabs, buffers and some customizations are saved. Buffers not linked to a file, such as those showing processes, will not be restored, and frames may not end up on the same space at this time. Aquamacs now includes an adapted version of the {\tt revive.el} package by Hirose Yuuji. We recommend that you do not load a different version of this package yourself.
% , and under Mac OS X 10.7 or later, sessions are reguarily saved and restored in case of unexpected program termination or reboot
% and the new customization variable `revive-desktop-after-launching' to turn the behavior off, or enable automatic session persistency even on pre-Lion operating systems
@@ -1118,6 +1118,7 @@ RUN-BUFFER is the buffer of the TeX process,
TEMPDIR is the correct copy of `TeX-active-tempdir',
PS-FILE is a copy of `preview-ps-file', IMAGETYPE is the image type
for the file extension."
+ (let ((ns-true-dpi-images-filename-string "pr"))
(overlay-put ov 'filenames
(unless (eq ps-file t)
(list
@@ -1130,7 +1131,7 @@ for the file extension."
(overlay-put ov 'preview-image
(list (preview-icon-copy preview-nonready-icon)))
(preview-add-urgentization #'preview-gs-urgentize ov run-buffer)
- (list ov))
+ (list ov)))
(defun preview-mouse-open-error (string)
"Display STRING in a new view buffer on click."
@@ -1892,6 +1893,7 @@ is already selected and unnarrowed."
(defun preview-dvipng-place-all ()
"Place all images dvipng has created, if any.
Deletes the dvi file when finished."
+ (let ((ns-true-dpi-images-filename-string "pr"))
(let (filename queued oldfiles snippet)
(dolist (ov (prog1 preview-gs-queue (setq preview-gs-queue nil)))
(when (and (setq queued (overlay-get ov 'queued))
@@ -1939,7 +1941,7 @@ Deletes the dvi file when finished."
(delete-file
(with-current-buffer TeX-command-buffer
(funcall (car gsfile) "dvi"))))
- (file-error nil)))))
+ (file-error nil))))))
(defun preview-active-string (ov)
"Generate before-string for active image overlay OV."
@@ -1956,6 +1958,7 @@ and TEMPDIR as CDR. TEMPDIR is a copy of `TeX-active-tempdir'
with the directory name, the reference count and its top directory
name elements. If FILE is already in that form, the file name itself
gets converted into a CONS-cell with a name and a reference count."
+ (print (format "make filename %s\n" file))
(if (consp file)
(progn
(if (consp (car file))
@@ -2975,17 +2978,20 @@ name(\\([^)]+\\))\\)\\|\
snippet)) "Parser"))))))))
(preview-call-hook 'close (car open-data) close-data))))))
+(defvar preview-resolution-factor 2.0 "")
(defun preview-get-geometry ()
"Transfer display geometry parameters from current display.
Returns list of scale, resolution and colors. Calculation
is done in current buffer."
(condition-case err
(let* ((geometry
(list (preview-hook-enquiry preview-scale-function)
- (cons (/ (* 25.4 (display-pixel-width))
- (display-mm-width))
- (/ (* 25.4 (display-pixel-height))
- (display-mm-height)))
+ (cons (* preview-resolution-factor
+ (/ (* 25.4 (display-pixel-width))
+ (display-mm-width)))
+ (* preview-resolution-factor
+ (/ (* 25.4 (display-pixel-height))
+ (display-mm-height))))
(preview-get-colors)))
(preview-min-spec
(* (cdr (nth 1 geometry))
View
@@ -106,6 +106,7 @@ Updated by Christian Limpach (chris@nice.ch)
return 0;
}
+ /* Pixels = Points in Emacs world... */
size = [eImg size];
img->width = size.width;
img->height = size.height;
@@ -119,12 +120,34 @@ Updated by Christian Limpach (chris@nice.ch)
int
ns_image_width (void *img)
{
+ /*
+ NSImageRep *imgRep;
+
+ if ([(id)img respondsToSelector: @selector (bestRepresentationForRect:context:hints:)])
+ {
+ imgRep = [img bestRepresentationForRect: NSMakeRect(100,100,30,30) context:nil hints: nil];
+
+ if (imgRep)
+ return [imgRep pixelsWide];
+ }
+ */
return [(id)img size].width;
}
int
ns_image_height (void *img)
{
+ /*
+ NSImageRep *imgRep;
+
+ if ([(id)img respondsToSelector: @selector (bestRepresentationForRect:context:hints:)])
+ {
+ imgRep = [img bestRepresentationForRect: NSMakeRect(100,100,30,30) context:nil hints: nil];
+
+ if (imgRep)
+ return [imgRep pixelsHigh];
+ }
+ */
return [(id)img size].height;
}
@@ -157,6 +180,10 @@ Updated by Christian Limpach (chris@nice.ch)
========================================================================== */
+// extern Lisp_Object Vns_true_dpi_images_filename_string;
+/* defined in nsterm.m */
+
+
@implementation EmacsImage
static EmacsImage *ImageList = nil;
@@ -167,7 +194,6 @@ @implementation EmacsImage
NSImageRep *imgRep;
Lisp_Object found;
-
#if 1
image = ImageList;
/* look for an existing image of the same name */
@@ -207,27 +233,63 @@ @implementation EmacsImage
// image = [NSImage imageNamed: [NSString stringWithUTF8String: SDATA (found)]];
+#if 0
#if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
imgRep = [NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]];
#else
imgRep = [image bestRepresentationForDevice: nil];
#endif
+#else
+
+ // we'll optimize for the main screen.
+ // needed to pick the right representation e.g., when HiDPI image is provided.
+ imgRep = [image bestRepresentationForRect: NSMakeRect(100,100,30,30) context:nil hints: nil];
+
+#endif
+
if (imgRep == nil)
{
[image release];
return nil;
}
-
- /* Do not ignore the DPI of the image - in case of multiple representations
- (e.g., TIFFs with images for multiple resolutions), retain image size and
- let NSImage choose the right representation. */
-
+
+ // Respect DPI?
+
/* The next two lines cause the DPI of the image to be ignored.
This seems to be the behavior users expect. */
- // [image setScalesWhenResized: YES];
- // [image setSize: NSMakeSize([imgRep pixelsWide], [imgRep pixelsHigh])];
- // this would retain the size:
- // [image setSize: NSMakeSize([image size].width, [image size].height)];
+
+ if (EQ (Qt, Vns_true_dpi_images_filename_string) ||
+ (STRINGP (Vns_true_dpi_images_filename_string) &&
+ strstr (SDATA (file), SDATA (Vns_true_dpi_images_filename_string))))
+ {
+ // this would retain the size:
+ // [image setSize: NSMakeSize([image size].width, [image size].height)];
+
+ // points = pixels for this port
+ // so we need to scale the image.
+
+ // NS assumes 72dpi= 72/25.4 pix/mm
+ // So we adjust according to the CoreGraphics DPI information
+
+ // use userSpaceScaleFactor?
+
+ NSScreen *screen = [NSScreen mainScreen];
+ CGDirectDisplayID displayID = (CGDirectDisplayID)[[[screen deviceDescription] objectForKey:@"NSScreenNumber"] unsignedIntValue];
+ CGSize physicalSize = CGDisplayScreenSize (displayID);
+ CGRect bounds = CGDisplayBounds (displayID);
+ float resx = bounds.size.width / physicalSize.width; // pixels per mm
+ float resy = bounds.size.height / physicalSize.height;
+
+
+ [image setScalesWhenResized: YES];
+ [image setSize: NSMakeSize([image size].width*resx / (72.0/25.4), [image size].height*resy / (72.0/25.4))];
+
+ }
+ else
+ {
+ [image setScalesWhenResized: YES];
+ [image setSize: NSMakeSize([imgRep pixelsWide], [imgRep pixelsHigh])];
+ }
[image setName: [NSString stringWithUTF8String: SDATA (file)]];
[image reference];
@@ -434,8 +496,8 @@ - (void) setPixmapData
/* The next two lines cause the DPI of the image to be ignored.
This seems to be the behavior users expect. */
- [self setScalesWhenResized: YES];
- [self setSize: NSMakeSize([bmRep pixelsWide], [bmRep pixelsHigh])];
+ [self setScalesWhenResized: YES];
+ [self setSize: NSMakeSize([bmRep pixelsWide], [bmRep pixelsHigh])];
break;
}
View
@@ -3707,7 +3707,21 @@ Convert modifier code (see lisp.h) to lisp symbol
NSScreen *screen = [NSScreen mainScreen];
NSWindowDepth depth = [screen depth];
- dpyinfo->resx = 72.27; /* used 75.0, but this makes pt == pixel, expected */
+
+#if 0
+ /* Use correct DPI for main screen*/
+ CGDirectDisplayID displayID = (CGDirectDisplayID)[[[screen deviceDescription] objectForKey:@"NSScreenNumber"] unsignedIntValue];
+ CGSize physicalSize = CGDisplayScreenSize (displayID);
+ CGRect bounds = CGDisplayBounds (displayID);
+ float resx = bounds.size.width / physicalSize.width;
+ float resy = bounds.size.height / physicalSize.height;
+
+
+ dpyinfo->resx = resx * 25.4;
+ dpyinfo->resy = resy * 25.4;
+#endif
+
+ dpyinfo->resx = 72.27; /* used to be 75.0, but this makes pt == pixel, expected */
dpyinfo->resy = 72.27;
dpyinfo->color_p = ![NSDeviceWhiteColorSpace isEqualToString:
NSColorSpaceFromDepth (depth)]
@@ -7568,6 +7582,14 @@ function key (on laptops). Set to control, meta, alt, super, or hyper
baseline level. The default value is nil. */);
x_underline_at_descent_line = 0;
+ DEFVAR_LISP ("ns-true-dpi-images-filename-string", Vns_true_dpi_images_filename_string,
+ doc: /* String to recognize images to be displayed in their true size.
+If set to a string, Emacs (NS only) will look for this string in the filename of
+each image to be loaded, and if the string is found, will scale the image so that
+it is sized correctly according to the image's resolution (DPI) and the main screen's
+resolution. If set to `t`, all images to be loaded are scaled.*/);
+ Vns_true_dpi_images_filename_string = Qnil;
+
/* Tell emacs about this window system. */
Fprovide (intern ("ns"), Qnil);
}

0 comments on commit 0303dcd

Please sign in to comment.