From 6ed8f5ba8dd42420927559599a25d099c43a8ef2 Mon Sep 17 00:00:00 2001
From: Jonathan Kew <jkew@mozilla.com>
Date: Wed, 22 Apr 2026 13:00:22 -0700
Subject: [PATCH 11/29] Add named-destination support to cairo-quartz-surface

---
 src/cairo-quartz-surface.c | 119 ++++++++++++++++++++++++++++++-------
 1 file changed, 98 insertions(+), 21 deletions(-)

diff --git a/src/cairo-quartz-surface.c b/src/cairo-quartz-surface.c
index 198c88160..6145d01ca 100644
--- a/src/cairo-quartz-surface.c
+++ b/src/cairo-quartz-surface.c
@@ -2192,24 +2192,13 @@ _cairo_quartz_surface_clipper_intersect_clip_path (cairo_surface_clipper_t *clip
 }
 
 static cairo_int_status_t
-_cairo_quartz_surface_tag (void			       *abstract_surface,
-			   cairo_bool_t                 begin,
-			   const char                  *tag_name,
-			   const char                  *attributes,
-			   const cairo_pattern_t       *source,
-			   const cairo_stroke_style_t  *style,
-			   const cairo_matrix_t	       *ctm,
-			   const cairo_matrix_t	       *ctm_inverse,
-			   const cairo_clip_t	       *clip)
+_cairo_quartz_surface_link (cairo_quartz_surface_t *surface,
+                            cairo_bool_t            begin,
+                            const char             *attributes)
 {
     cairo_link_attrs_t link_attrs;
     cairo_int_status_t status = CAIRO_STATUS_SUCCESS;
     int i, num_rects;
-    cairo_quartz_surface_t *surface = (cairo_quartz_surface_t *) abstract_surface;
-
-    /* Currently the only tag we support is "Link" */
-    if (strcmp (tag_name, "Link"))
-        return CAIRO_INT_STATUS_UNSUPPORTED;
 
     /* We only process the 'begin' tag, and expect a rect attribute;
        using the extents of the drawing operations enclosed by the begin/end
@@ -2223,11 +2212,24 @@ _cairo_quartz_surface_tag (void			       *abstract_surface,
 
     num_rects = _cairo_array_num_elements (&link_attrs.rects);
     if (num_rects > 0) {
-        CFURLRef url = CFURLCreateWithBytes (NULL,
-                                             (const UInt8 *) link_attrs.uri,
-                                             strlen (link_attrs.uri),
-                                             kCFStringEncodingUTF8,
-                                             NULL);
+        /* Create either a named destination or a URL, depending which is present
+           in the link attributes. */
+        CFURLRef url = NULL;
+        CFStringRef name = NULL;
+        if (link_attrs.uri && *link_attrs.uri)
+            url = CFURLCreateWithBytes (NULL,
+                                        (const UInt8 *) link_attrs.uri,
+                                        strlen (link_attrs.uri),
+                                        kCFStringEncodingUTF8,
+                                        NULL);
+        else if (link_attrs.dest && *link_attrs.dest)
+            name = CFStringCreateWithBytes (kCFAllocatorDefault,
+                                            (const UInt8 *) link_attrs.dest,
+                                            strlen (link_attrs.dest),
+                                            kCFStringEncodingUTF8,
+                                            FALSE);
+        else /* silently ignore link that doesn't have a usable target */
+            goto cleanup;
 
         for (i = 0; i < num_rects; i++) {
             CGRect link_rect;
@@ -2241,12 +2243,19 @@ _cairo_quartz_surface_tag (void			       *abstract_surface,
                             rectf.width,
                             rectf.height);
 
-            CGPDFContextSetURLForRect (surface->cgContext, url, link_rect);
+            if (url)
+                CGPDFContextSetURLForRect (surface->cgContext, url, link_rect);
+            else
+                CGPDFContextSetDestinationForRect (surface->cgContext, name, link_rect);
         }
 
-        CFRelease (url);
+        if (url)
+            CFRelease (url);
+        else
+            CFRelease (name);
     }
 
+cleanup:
     _cairo_array_fini (&link_attrs.rects);
     free (link_attrs.dest);
     free (link_attrs.uri);
@@ -2255,6 +2264,74 @@ _cairo_quartz_surface_tag (void			       *abstract_surface,
     return status;
 }
 
+static cairo_int_status_t
+_cairo_quartz_surface_dest (cairo_quartz_surface_t *surface,
+                            cairo_bool_t            begin,
+                            const char             *attributes)
+{
+    cairo_dest_attrs_t dest_attrs;
+    cairo_int_status_t status = CAIRO_STATUS_SUCCESS;
+    double x = 0, y = 0;
+
+    /* We only process the 'begin' tag, and expect 'x' and 'y' attributes. */
+    if (!begin)
+        return status;
+
+    status = _cairo_tag_parse_dest_attributes (attributes, &dest_attrs);
+    if (unlikely (status))
+	return status;
+
+    if (unlikely (!dest_attrs.name || !strlen (dest_attrs.name)))
+        goto cleanup;
+
+    CFStringRef name = CFStringCreateWithBytes (kCFAllocatorDefault,
+                                                (const UInt8 *) dest_attrs.name,
+                                                strlen (dest_attrs.name),
+                                                kCFStringEncodingUTF8,
+                                                FALSE);
+
+    if (dest_attrs.x_valid)
+        x = dest_attrs.x;
+    if (dest_attrs.y_valid)
+        y = dest_attrs.y;
+
+    CGPDFContextAddDestinationAtPoint (surface->cgContext,
+                                       name,
+                                       CGPointMake (x, surface->extents.height - y));
+    CFRelease (name);
+
+cleanup:
+    free (dest_attrs.name);
+
+    return status;
+}
+
+static cairo_int_status_t
+_cairo_quartz_surface_tag (void			       *abstract_surface,
+			   cairo_bool_t                 begin,
+			   const char                  *tag_name,
+			   const char                  *attributes,
+			   const cairo_pattern_t       *source,
+			   const cairo_stroke_style_t  *style,
+			   const cairo_matrix_t	       *ctm,
+			   const cairo_matrix_t	       *ctm_inverse,
+			   const cairo_clip_t	       *clip)
+{
+    cairo_link_attrs_t link_attrs;
+    int i, num_rects;
+    cairo_quartz_surface_t *surface = (cairo_quartz_surface_t *) abstract_surface;
+
+    /* Currently the only tags we support are CAIRO_TAG_LINK and CAIRO_TAG_DEST */
+    if (!strcmp (tag_name, CAIRO_TAG_LINK))
+        return _cairo_quartz_surface_link (surface, begin, attributes);
+
+    if (!strcmp (tag_name, CAIRO_TAG_DEST))
+        return _cairo_quartz_surface_dest (surface, begin, attributes);
+
+    /* Unknown tag names are silently ignored here. */
+    return CAIRO_INT_STATUS_SUCCESS;
+}
+
 // XXXtodo implement show_page; need to figure out how to handle begin/end
 
 static const cairo_surface_backend_t cairo_quartz_surface_backend = {
-- 
2.53.0

