link.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright (C) 2004-2023 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #include "mupdf/fitz.h"
  23. #include <math.h>
  24. fz_link *
  25. fz_new_link_of_size(fz_context *ctx, int size, fz_rect rect, const char *uri)
  26. {
  27. fz_link *link = Memento_label(fz_calloc(ctx, 1, size), "fz_link");
  28. link->refs = 1;
  29. link->rect = rect;
  30. fz_try(ctx)
  31. link->uri = fz_strdup(ctx, uri);
  32. fz_catch(ctx)
  33. {
  34. fz_drop_link(ctx, link);
  35. fz_rethrow(ctx);
  36. }
  37. return link;
  38. }
  39. fz_link *
  40. fz_keep_link(fz_context *ctx, fz_link *link)
  41. {
  42. return fz_keep_imp(ctx, link, &link->refs);
  43. }
  44. void
  45. fz_drop_link(fz_context *ctx, fz_link *link)
  46. {
  47. while (fz_drop_imp(ctx, link, &link->refs))
  48. {
  49. fz_link *next = link->next;
  50. if (link->drop)
  51. link->drop(ctx, link);
  52. fz_free(ctx, link->uri);
  53. fz_free(ctx, link);
  54. link = next;
  55. }
  56. }
  57. int
  58. fz_is_external_link(fz_context *ctx, const char *uri)
  59. {
  60. const char *mark;
  61. /* Basically, this function returns true, if the URI starts with
  62. * a valid 'scheme' followed by ':'. */
  63. if (!uri)
  64. return 0;
  65. /* All schemes must start with a letter; exit if we don't. */
  66. if ((*uri < 'a' || *uri > 'z') && (*uri < 'A' || *uri > 'Z'))
  67. return 0;
  68. uri++;
  69. mark = uri;
  70. /* Subsequent characters can be letters, digits, +, -, or . */
  71. while ((*uri >= 'a' && *uri <= 'z') ||
  72. (*uri >= 'A' && *uri <= 'Z') ||
  73. (*uri >= '0' && *uri <= '9') ||
  74. (*uri == '+') ||
  75. (*uri == '-') ||
  76. (*uri == '.'))
  77. ++uri;
  78. return uri[0] == ':' && (uri - mark) > 1;
  79. }
  80. fz_link_dest fz_make_link_dest_none(void)
  81. {
  82. fz_link_dest dest = { { -1, -1 }, FZ_LINK_DEST_XYZ, NAN, NAN, NAN, NAN, NAN };
  83. return dest;
  84. }
  85. fz_link_dest fz_make_link_dest_xyz(int chapter, int page, float x, float y, float z)
  86. {
  87. fz_link_dest dest = { { chapter, page }, FZ_LINK_DEST_XYZ, x, y, NAN, NAN, z };
  88. return dest;
  89. }