1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
|
/*
* Copyright (C) 2009 Carlos Garcia Campos
* Copyright (C) 2004 Marco Pesenti Gritti
* Copyright © 2018 Christian Persch
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include "ev-document.h"
#include "ev-document-misc.h"
#include "synctex_parser.h"
enum {
PROP_0,
PROP_MODIFIED
};
typedef struct _EvPageSize
{
gdouble width;
gdouble height;
} EvPageSize;
struct _EvDocumentPrivate
{
gchar *uri;
guint64 file_size;
gboolean cache_loaded;
gint n_pages;
gboolean modified;
gboolean uniform;
gdouble uniform_width;
gdouble uniform_height;
gdouble max_width;
gdouble max_height;
gdouble min_width;
gdouble min_height;
gint max_label;
gchar **page_labels;
EvPageSize *page_sizes;
EvDocumentInfo *info;
synctex_scanner_p synctex_scanner;
};
static guint64 _ev_document_get_size_gfile (GFile *file);
static guint64 _ev_document_get_size (const char *uri);
static gint _ev_document_get_n_pages (EvDocument *document);
static void _ev_document_get_page_size (EvDocument *document,
EvPage *page,
double *width,
double *height);
static gchar *_ev_document_get_page_label (EvDocument *document,
EvPage *page);
static EvDocumentInfo *_ev_document_get_info (EvDocument *document);
static gboolean _ev_document_support_synctex (EvDocument *document);
static GMutex ev_doc_mutex;
static GMutex ev_fc_mutex;
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (EvDocument, ev_document, G_TYPE_OBJECT)
GQuark
ev_document_error_quark (void)
{
static GQuark q = 0;
if (q == 0)
q = g_quark_from_static_string ("ev-document-error-quark");
return q;
}
static EvPage *
ev_document_impl_get_page (EvDocument *document,
gint index)
{
return ev_page_new (index);
}
static EvDocumentInfo *
ev_document_impl_get_info (EvDocument *document)
{
return ev_document_info_new ();
}
static void
ev_document_finalize (GObject *object)
{
EvDocument *document = EV_DOCUMENT (object);
g_clear_pointer (&document->priv->uri, g_free);
g_clear_pointer (&document->priv->page_sizes, g_free);
g_clear_pointer (&document->priv->page_labels, g_strfreev);
g_clear_pointer (&document->priv->info, ev_document_info_free);
g_clear_pointer (&document->priv->synctex_scanner, synctex_scanner_free);
G_OBJECT_CLASS (ev_document_parent_class)->finalize (object);
}
static void
ev_document_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
switch (prop_id) {
case PROP_MODIFIED:
ev_document_set_modified (EV_DOCUMENT (object),
g_value_get_boolean (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
ev_document_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
EvDocument *document = EV_DOCUMENT (object);
switch (prop_id) {
case PROP_MODIFIED:
g_value_set_boolean (value, document->priv->modified);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
ev_document_init (EvDocument *document)
{
document->priv = ev_document_get_instance_private (document);
/* Assume all pages are the same size until proven otherwise */
document->priv->uniform = TRUE;
}
static void
ev_document_class_init (EvDocumentClass *klass)
{
GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
klass->get_page = ev_document_impl_get_page;
klass->get_info = ev_document_impl_get_info;
klass->get_backend_info = NULL;
g_object_class->get_property = ev_document_get_property;
g_object_class->set_property = ev_document_set_property;
g_object_class->finalize = ev_document_finalize;
g_object_class_install_property (g_object_class,
PROP_MODIFIED,
g_param_spec_boolean ("modified",
"Is modified",
"Whether the document has been modified",
FALSE,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
}
/**
* ev_document_get_modified:
* @document: an #EvDocument
*
* Returns: %TRUE iff the document has been modified.
*
* You can monitor changes to the modification state by connecting to the
* notify::modified signal on @document.
*
* Since: 3.28
*/
gboolean
ev_document_get_modified (EvDocument *document)
{
g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
return document->priv->modified;
}
/**
* ev_document_set_modified:
* @document: an #EvDocument
* @modified: a boolean value to set the document as modified or not.
*
* Set the @document modification state as @modified.
*
* Since: 3.28
*/
void
ev_document_set_modified (EvDocument *document,
gboolean modified)
{
g_return_if_fail (EV_IS_DOCUMENT (document));
if (document->priv->modified != modified) {
document->priv->modified = modified;
g_object_notify (G_OBJECT (document), "modified");
}
}
void
ev_document_doc_mutex_lock (void)
{
g_mutex_lock (&ev_doc_mutex);
}
void
ev_document_doc_mutex_unlock (void)
{
g_mutex_unlock (&ev_doc_mutex);
}
gboolean
ev_document_doc_mutex_trylock (void)
{
return g_mutex_trylock (&ev_doc_mutex);
}
void
ev_document_fc_mutex_lock (void)
{
g_mutex_lock (&ev_fc_mutex);
}
void
ev_document_fc_mutex_unlock (void)
{
g_mutex_unlock (&ev_fc_mutex);
}
gboolean
ev_document_fc_mutex_trylock (void)
{
return g_mutex_trylock (&ev_fc_mutex);
}
static void
ev_document_setup_cache (EvDocument *document)
{
EvDocumentPrivate *priv = document->priv;
gboolean custom_page_labels = FALSE;
gint i;
/* Cache some info about the document to avoid
* going to the backends since it requires locks
*/
priv->cache_loaded = TRUE;
for (i = 0; i < priv->n_pages; i++) {
EvPage *page = ev_document_get_page (document, i);
gdouble page_width = 0;
gdouble page_height = 0;
EvPageSize *page_size;
gchar *page_label;
_ev_document_get_page_size (document, page, &page_width, &page_height);
if (i == 0) {
priv->uniform_width = page_width;
priv->uniform_height = page_height;
priv->max_width = priv->uniform_width;
priv->max_height = priv->uniform_height;
priv->min_width = priv->uniform_width;
priv->min_height = priv->uniform_height;
} else if (priv->uniform &&
(priv->uniform_width != page_width ||
priv->uniform_height != page_height)) {
/* It's a different page size. Backfill the array. */
int j;
/* Check for potential integer overflow in allocation - Issue #2094 */
if ((gsize)priv->n_pages > G_MAXSIZE / sizeof(EvPageSize))
g_error ("Exiting program due to abnormal page count detected: %d", priv->n_pages);
priv->page_sizes = g_new0 (EvPageSize, priv->n_pages);
for (j = 0; j < i; j++) {
page_size = &(priv->page_sizes[j]);
page_size->width = priv->uniform_width;
page_size->height = priv->uniform_height;
}
priv->uniform = FALSE;
}
if (!priv->uniform) {
page_size = &(priv->page_sizes[i]);
page_size->width = page_width;
page_size->height = page_height;
if (page_width > priv->max_width)
priv->max_width = page_width;
if (page_width < priv->min_width)
priv->min_width = page_width;
if (page_height > priv->max_height)
priv->max_height = page_height;
if (page_height < priv->min_height)
priv->min_height = page_height;
}
page_label = _ev_document_get_page_label (document, page);
if (page_label) {
if (!priv->page_labels)
priv->page_labels = g_new0 (gchar *, priv->n_pages + 1);
if (!custom_page_labels) {
gchar *real_page_label;
real_page_label = g_strdup_printf ("%d", i + 1);
custom_page_labels = g_strcmp0 (real_page_label, page_label) != 0;
g_free (real_page_label);
}
priv->page_labels[i] = page_label;
priv->max_label = MAX (priv->max_label,
g_utf8_strlen (page_label, 256));
}
g_object_unref (page);
}
if (!custom_page_labels)
g_clear_pointer (&priv->page_labels, g_strfreev);
}
static void
ev_document_initialize_synctex (EvDocument *document,
const gchar *uri)
{
EvDocumentPrivate *priv = document->priv;
if (_ev_document_support_synctex (document)) {
gchar *filename;
filename = g_filename_from_uri (uri, NULL, NULL);
if (filename != NULL) {
priv->synctex_scanner =
synctex_scanner_new_with_output_file (filename, NULL, 1);
g_free (filename);
}
}
}
/**
* ev_document_load_full:
* @document: a #EvDocument
* @uri: the document's URI
* @flags: flags from #EvDocumentLoadFlags
* @error: a #GError location to store an error, or %NULL
*
* Loads @document from @uri.
*
* On failure, %FALSE is returned and @error is filled in.
* If the document is encrypted, EV_DEFINE_ERROR_ENCRYPTED is returned.
* If the backend cannot load the specific document, EV_DOCUMENT_ERROR_INVALID
* is returned. Other errors are possible too, depending on the backend
* used to load the document and the URI, e.g. #GIOError, #GFileError, and
* #GConvertError.
*
* Returns: %TRUE on success, or %FALSE on failure.
*/
gboolean
ev_document_load_full (EvDocument *document,
const char *uri,
EvDocumentLoadFlags flags,
GError **error)
{
EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
gboolean retval;
GError *err = NULL;
retval = klass->load (document, uri, &err);
if (!retval) {
if (err) {
g_propagate_error (error, err);
} else {
g_warning ("%s::EvDocument::load returned FALSE but did not fill in @error; fix the backend!\n",
G_OBJECT_TYPE_NAME (document));
/* So upper layers don't crash */
g_set_error_literal (error,
EV_DOCUMENT_ERROR,
EV_DOCUMENT_ERROR_INVALID,
"Internal error in backend");
}
} else {
document->priv->info = _ev_document_get_info (document);
document->priv->n_pages = _ev_document_get_n_pages (document);
if (!(flags & EV_DOCUMENT_LOAD_FLAG_NO_CACHE))
ev_document_setup_cache (document);
document->priv->uri = g_strdup (uri);
document->priv->file_size = _ev_document_get_size (uri);
ev_document_initialize_synctex (document, uri);
}
return retval;
}
/**
* ev_document_load:
* @document: a #EvDocument
* @uri: the document's URI
* @error: a #GError location to store an error, or %NULL
*
* Loads @document from @uri.
*
* On failure, %FALSE is returned and @error is filled in.
* If the document is encrypted, EV_DEFINE_ERROR_ENCRYPTED is returned.
* If the backend cannot load the specific document, EV_DOCUMENT_ERROR_INVALID
* is returned. If the backend does not support the format for the document's
* contents, EV_DOCUMENT_ERROR_UNSUPPORTED_CONTENT is returned. Other errors
* are possible too, depending on the backend used to load the document and
* the URI, e.g. #GIOError, #GFileError, and #GConvertError.
*
* Returns: %TRUE on success, or %FALSE on failure.
*/
gboolean
ev_document_load (EvDocument *document,
const char *uri,
GError **error)
{
return ev_document_load_full (document, uri,
EV_DOCUMENT_LOAD_FLAG_NONE, error);
}
/**
* ev_document_load_stream:
* @document: a #EvDocument
* @stream: a #GInputStream
* @flags: flags from #EvDocumentLoadFlags
* @cancellable: (allow-none): a #GCancellable, or %NULL
* @error: (allow-none): a #GError location to store an error, or %NULL
*
* Synchronously loads the document from @stream.
* See ev_document_load() for more information.
*
* Returns: %TRUE if loading succeeded, or %FALSE on error with @error filled in
*
* Since: 3.6
*/
gboolean
ev_document_load_stream (EvDocument *document,
GInputStream *stream,
EvDocumentLoadFlags flags,
GCancellable *cancellable,
GError **error)
{
EvDocumentClass *klass;
g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
klass = EV_DOCUMENT_GET_CLASS (document);
if (!klass->load_stream) {
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
"Backend does not support loading from stream");
return FALSE;
}
if (!klass->load_stream (document, stream, flags, cancellable, error))
return FALSE;
document->priv->info = _ev_document_get_info (document);
document->priv->n_pages = _ev_document_get_n_pages (document);
if (!(flags & EV_DOCUMENT_LOAD_FLAG_NO_CACHE))
ev_document_setup_cache (document);
return TRUE;
}
/**
* ev_document_load_gfile:
* @document: a #EvDocument
* @file: a #GFile
* @flags: flags from #EvDocumentLoadFlags
* @cancellable: (allow-none): a #GCancellable, or %NULL
* @error: (allow-none): a #GError location to store an error, or %NULL
*
* Synchronously loads the document from @file.
* See ev_document_load() for more information.
*
* Returns: %TRUE if loading succeeded, or %FALSE on error with @error filled in
*
* Since: 3.6
*/
gboolean
ev_document_load_gfile (EvDocument *document,
GFile *file,
EvDocumentLoadFlags flags,
GCancellable *cancellable,
GError **error)
{
EvDocumentClass *klass;
g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
g_return_val_if_fail (G_IS_FILE (file), FALSE);
g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
klass = EV_DOCUMENT_GET_CLASS (document);
if (!klass->load_gfile) {
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
"Backend does not support loading from GFile");
return FALSE;
}
if (!klass->load_gfile (document, file, flags, cancellable, error))
return FALSE;
document->priv->info = _ev_document_get_info (document);
document->priv->n_pages = _ev_document_get_n_pages (document);
if (!(flags & EV_DOCUMENT_LOAD_FLAG_NO_CACHE))
ev_document_setup_cache (document);
document->priv->uri = g_file_get_uri (file);
document->priv->file_size = _ev_document_get_size_gfile (file);
ev_document_initialize_synctex (document, document->priv->uri);
return TRUE;
}
/**
* ev_document_load_fd:
* @document: a #EvDocument
* @fd: a file descriptor
* @flags: flags from #EvDocumentLoadFlags
* @cancellable: (allow-none): a #GCancellable, or %NULL
* @error: (allow-none): a #GError location to store an error, or %NULL
*
* Synchronously loads the document from @fd, which must refer to
* a regular file.
*
* Note that this function takes ownership of @fd; you must not ever
* operate on it again. It will be closed automatically if the document
* is destroyed, or if this function returns %NULL.
*
* See ev_document_load() for more information.
*
* Returns: %TRUE if loading succeeded, or %FALSE on error with @error filled in
*
* Since: 42.0
*/
gboolean
ev_document_load_fd (EvDocument *document,
int fd,
EvDocumentLoadFlags flags,
GCancellable *cancellable,
GError **error)
{
EvDocumentClass *klass;
struct stat statbuf;
int fd_flags;
g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
g_return_val_if_fail (fd != -1, FALSE);
g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
klass = EV_DOCUMENT_GET_CLASS (document);
if (!klass->load_fd) {
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
"Backend does not support loading from file descriptor");
close (fd);
return FALSE;
}
if (fstat(fd, &statbuf) == -1 ||
(fd_flags = fcntl (fd, F_GETFL, &flags)) == -1) {
int errsv = errno;
g_set_error_literal (error, G_FILE_ERROR,
g_file_error_from_errno (errsv),
g_strerror (errsv));
close (fd);
return FALSE;
}
if (!S_ISREG(statbuf.st_mode)) {
g_set_error_literal (error, G_FILE_ERROR, G_FILE_ERROR_BADF,
"Not a regular file.");
close (fd);
return FALSE;
}
switch (fd_flags & O_ACCMODE) {
case O_RDONLY:
case O_RDWR:
break;
case O_WRONLY:
default:
g_set_error_literal (error, G_FILE_ERROR, G_FILE_ERROR_BADF,
"Not a readable file descriptor.");
close (fd);
return FALSE;
}
if (!klass->load_fd (document, fd, flags, cancellable, error))
return FALSE;
document->priv->info = _ev_document_get_info (document);
document->priv->n_pages = _ev_document_get_n_pages (document);
if (!(flags & EV_DOCUMENT_LOAD_FLAG_NO_CACHE))
ev_document_setup_cache (document);
return TRUE;
}
/**
* ev_document_save:
* @document: a #EvDocument
* @uri: the target URI
* @error: a #GError location to store an error, or %NULL
*
* Saves @document to @uri.
*
* Returns: %TRUE on success, or %FALSE on error with @error filled in
*/
gboolean
ev_document_save (EvDocument *document,
const char *uri,
GError **error)
{
EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
return klass->save (document, uri, error);
}
/**
* ev_document_get_page:
* @document: a #EvDocument
* @index: index of page
*
* Returns: (transfer full): Newly created #EvPage for the given index.
*/
EvPage *
ev_document_get_page (EvDocument *document,
gint index)
{
EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
return klass->get_page (document, index);
}
static gboolean
_ev_document_support_synctex (EvDocument *document)
{
EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
return klass->support_synctex ? klass->support_synctex (document) : FALSE;
}
gboolean
ev_document_has_synctex (EvDocument *document)
{
g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
return document->priv->synctex_scanner != NULL;
}
/**
* ev_document_synctex_backward_search:
* @document: a #EvDocument
* @page_index: the target page
* @x: X coordinate
* @y: Y coordinate
*
* Peforms a Synctex backward search to obtain the TeX input file, line and
* (possibly) column corresponding to the position (@x,@y) (in 72dpi
* coordinates) in the @page of @document.
*
* Returns: A pointer to the EvSourceLink structure that holds the result. @NULL if synctex
* is not enabled for the document or no result is found.
* The EvSourceLink pointer should be freed with g_free after it is used.
*/
EvSourceLink *
ev_document_synctex_backward_search (EvDocument *document,
gint page_index,
gfloat x,
gfloat y)
{
EvSourceLink *result = NULL;
synctex_scanner_p scanner;
g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
scanner = document->priv->synctex_scanner;
if (!scanner)
return NULL;
if (synctex_edit_query (scanner, page_index + 1, x, y) > 0) {
synctex_node_p node;
/* We assume that a backward search returns either zero or one result_node */
node = synctex_scanner_next_result (scanner);
if (node != NULL) {
const gchar *filename;
filename = synctex_scanner_get_name (scanner, synctex_node_tag (node));
if (filename) {
result = ev_source_link_new (filename,
synctex_node_line (node),
synctex_node_column (node));
}
}
}
return result;
}
/**
* ev_document_synctex_forward_search: (skip)
* @document: a #EvDocument
* @source_link: a #EvSourceLink
*
* Peforms a Synctex forward search to obtain the area in the document
* corresponding to the position (line and column in @source_link) in
* the source Tex file.
*
* Returns: An EvMapping with the page number and area corresponding to
* the given line in the source file. It must be free with g_free when done
*/
EvMapping *
ev_document_synctex_forward_search (EvDocument *document,
EvSourceLink *link)
{
EvMapping *result = NULL;
synctex_scanner_p scanner;
g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
scanner = document->priv->synctex_scanner;
if (!scanner)
return NULL;
/* Since 1.19, synctex_display_query has a fourth parameter,
* page-hint, which we set into a dummy number to not break the
* API. In synctex it is used to set the best results first
* given the page-hint
*/
if (synctex_display_query (scanner, link->filename, link->line, link->col, 0) > 0) {
synctex_node_p node;
gint page;
if ((node = synctex_scanner_next_result (scanner))) {
result = g_new (EvMapping, 1);
page = synctex_node_page (node) - 1;
result->data = GINT_TO_POINTER (page);
result->area.x1 = synctex_node_box_visible_h (node);
result->area.y1 = synctex_node_box_visible_v (node) -
synctex_node_box_visible_height (node);
result->area.x2 = synctex_node_box_visible_width (node) + result->area.x1;
result->area.y2 = synctex_node_box_visible_depth (node) +
synctex_node_box_visible_height (node) + result->area.y1;
}
}
return result;
}
static guint64
_ev_document_get_size_gfile (GFile *file)
{
goffset size = 0;
GFileInfo *info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_SIZE,
G_FILE_QUERY_INFO_NONE, NULL, NULL);
if (info) {
size = g_file_info_get_size (info);
g_object_unref (info);
}
return size;
}
static guint64
_ev_document_get_size (const char *uri)
{
GFile *file = g_file_new_for_uri (uri);
guint64 size = _ev_document_get_size_gfile (file);
g_object_unref (file);
return size;
}
static gint
_ev_document_get_n_pages (EvDocument *document)
{
EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
return klass->get_n_pages (document);
}
gint
ev_document_get_n_pages (EvDocument *document)
{
g_return_val_if_fail (EV_IS_DOCUMENT (document), 0);
return document->priv->n_pages;
}
static void
_ev_document_get_page_size (EvDocument *document,
EvPage *page,
double *width,
double *height)
{
EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
klass->get_page_size (document, page, width, height);
}
/**
* ev_document_get_page_size:
* @document: a #EvDocument
* @page_index: index of page
* @width: (out) (allow-none): return location for the width of the page, or %NULL
* @height: (out) (allow-none): return location for the height of the page, or %NULL
*/
void
ev_document_get_page_size (EvDocument *document,
gint page_index,
double *width,
double *height)
{
EvDocumentPrivate *priv;
g_return_if_fail (EV_IS_DOCUMENT (document));
g_return_if_fail (page_index >= 0 || page_index < document->priv->n_pages);
priv = document->priv;
if (priv->cache_loaded) {
if (width)
*width = priv->uniform ?
priv->uniform_width :
priv->page_sizes[page_index].width;
if (height)
*height = priv->uniform ?
priv->uniform_height :
priv->page_sizes[page_index].height;
} else {
EvPage *page;
g_mutex_lock (&ev_doc_mutex);
page = ev_document_get_page (document, page_index);
_ev_document_get_page_size (document, page, width, height);
g_object_unref (page);
g_mutex_unlock (&ev_doc_mutex);
}
}
static gchar *
_ev_document_get_page_label (EvDocument *document,
EvPage *page)
{
EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
return klass->get_page_label ?
klass->get_page_label (document, page) : NULL;
}
gchar *
ev_document_get_page_label (EvDocument *document,
gint page_index)
{
g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
g_return_val_if_fail (page_index >= 0 || page_index < document->priv->n_pages, NULL);
if (!document->priv->cache_loaded) {
EvPage *page;
gchar *page_label;
g_mutex_lock (&ev_doc_mutex);
page = ev_document_get_page (document, page_index);
page_label = _ev_document_get_page_label (document, page);
g_object_unref (page);
g_mutex_unlock (&ev_doc_mutex);
return page_label ? page_label : g_strdup_printf ("%d", page_index + 1);
}
return (document->priv->page_labels && document->priv->page_labels[page_index]) ?
g_strdup (document->priv->page_labels[page_index]) :
g_strdup_printf ("%d", page_index + 1);
}
static EvDocumentInfo *
_ev_document_get_info (EvDocument *document)
{
EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
return klass->get_info (document);
}
/**
* ev_document_get_info:
* @document: a #EvDocument
*
* Returns the #EvDocumentInfo for the document.
*
* Returns: (transfer none): a #EvDocumentInfo
*/
EvDocumentInfo *
ev_document_get_info (EvDocument *document)
{
g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
return document->priv->info;
}
gboolean
ev_document_get_backend_info (EvDocument *document, EvDocumentBackendInfo *info)
{
EvDocumentClass *klass;
g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
klass = EV_DOCUMENT_GET_CLASS (document);
if (klass->get_backend_info == NULL)
return FALSE;
return klass->get_backend_info (document, info);
}
cairo_surface_t *
ev_document_render (EvDocument *document,
EvRenderContext *rc)
{
EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
return klass->render (document, rc);
}
static GdkPixbuf *
_ev_document_get_thumbnail (EvDocument *document,
EvRenderContext *rc)
{
cairo_surface_t *surface;
GdkPixbuf *pixbuf = NULL;
surface = ev_document_render (document, rc);
if (surface != NULL) {
pixbuf = ev_document_misc_pixbuf_from_surface (surface);
cairo_surface_destroy (surface);
}
return pixbuf;
}
/**
* ev_document_get_thumbnail:
* @document: an #EvDocument
* @rc: an #EvRenderContext
*
* Returns: (transfer full): a #GdkPixbuf
*/
GdkPixbuf *
ev_document_get_thumbnail (EvDocument *document,
EvRenderContext *rc)
{
EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
if (klass->get_thumbnail)
return klass->get_thumbnail (document, rc);
return _ev_document_get_thumbnail (document, rc);
}
/**
* ev_document_get_thumbnail_surface:
* @document: an #EvDocument
* @rc: an #EvRenderContext
*
* Returns: (transfer full): a #cairo_surface_t
*
* Since: 3.14
*/
cairo_surface_t *
ev_document_get_thumbnail_surface (EvDocument *document,
EvRenderContext *rc)
{
EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
if (klass->get_thumbnail_surface)
return klass->get_thumbnail_surface (document, rc);
return ev_document_render (document, rc);
}
const gchar *
ev_document_get_uri (EvDocument *document)
{
g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
return document->priv->uri;
}
const gchar *
ev_document_get_title (EvDocument *document)
{
g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
return (document->priv->info->fields_mask & EV_DOCUMENT_INFO_TITLE) ?
document->priv->info->title : NULL;
}
gboolean
ev_document_is_page_size_uniform (EvDocument *document)
{
g_return_val_if_fail (EV_IS_DOCUMENT (document), TRUE);
if (!document->priv->cache_loaded) {
g_mutex_lock (&ev_doc_mutex);
ev_document_setup_cache (document);
g_mutex_unlock (&ev_doc_mutex);
}
return document->priv->uniform;
}
void
ev_document_get_max_page_size (EvDocument *document,
gdouble *width,
gdouble *height)
{
g_return_if_fail (EV_IS_DOCUMENT (document));
if (!document->priv->cache_loaded) {
g_mutex_lock (&ev_doc_mutex);
ev_document_setup_cache (document);
g_mutex_unlock (&ev_doc_mutex);
}
if (width)
*width = document->priv->max_width;
if (height)
*height = document->priv->max_height;
}
void
ev_document_get_min_page_size (EvDocument *document,
gdouble *width,
gdouble *height)
{
g_return_if_fail (EV_IS_DOCUMENT (document));
if (!document->priv->cache_loaded) {
g_mutex_lock (&ev_doc_mutex);
ev_document_setup_cache (document);
g_mutex_unlock (&ev_doc_mutex);
}
if (width)
*width = document->priv->min_width;
if (height)
*height = document->priv->min_height;
}
gboolean
ev_document_check_dimensions (EvDocument *document)
{
g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
if (!document->priv->cache_loaded) {
g_mutex_lock (&ev_doc_mutex);
ev_document_setup_cache (document);
g_mutex_unlock (&ev_doc_mutex);
}
return (document->priv->max_width > 0 && document->priv->max_height > 0);
}
guint64
ev_document_get_size (EvDocument *document)
{
g_return_val_if_fail (EV_IS_DOCUMENT (document), 0);
return document->priv->file_size;
}
gint
ev_document_get_max_label_len (EvDocument *document)
{
g_return_val_if_fail (EV_IS_DOCUMENT (document), -1);
if (!document->priv->cache_loaded) {
g_mutex_lock (&ev_doc_mutex);
ev_document_setup_cache (document);
g_mutex_unlock (&ev_doc_mutex);
}
return document->priv->max_label;
}
gboolean
ev_document_has_text_page_labels (EvDocument *document)
{
g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
if (!document->priv->cache_loaded) {
g_mutex_lock (&ev_doc_mutex);
ev_document_setup_cache (document);
g_mutex_unlock (&ev_doc_mutex);
}
return document->priv->page_labels != NULL;
}
gboolean
ev_document_find_page_by_label (EvDocument *document,
const gchar *page_label,
gint *page_index)
{
gint i, page;
glong value;
gchar *endptr = NULL;
EvDocumentPrivate *priv = document->priv;
g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
g_return_val_if_fail (page_label != NULL, FALSE);
g_return_val_if_fail (page_index != NULL, FALSE);
if (!document->priv->cache_loaded) {
g_mutex_lock (&ev_doc_mutex);
ev_document_setup_cache (document);
g_mutex_unlock (&ev_doc_mutex);
}
/* First, look for a literal label match */
for (i = 0; priv->page_labels && i < priv->n_pages; i ++) {
if (priv->page_labels[i] != NULL &&
! strcmp (page_label, priv->page_labels[i])) {
*page_index = i;
return TRUE;
}
}
/* Second, look for a match with case insensitively */
for (i = 0; priv->page_labels && i < priv->n_pages; i++) {
if (priv->page_labels[i] != NULL &&
! strcasecmp (page_label, priv->page_labels[i])) {
*page_index = i;
return TRUE;
}
}
/* Next, parse the label, and see if the number fits */
value = strtol (page_label, &endptr, 10);
if (endptr[0] == '\0') {
/* Page number is an integer */
page = MIN (G_MAXINT, value);
/* convert from a page label to a page offset */
page --;
if (page >= 0 && page < priv->n_pages) {
*page_index = page;
return TRUE;
}
}
return FALSE;
}
/* EvSourceLink */
G_DEFINE_BOXED_TYPE (EvSourceLink, ev_source_link, ev_source_link_copy, ev_source_link_free)
EvSourceLink *
ev_source_link_new (const gchar *filename,
gint line,
gint col)
{
EvSourceLink *link = g_slice_new (EvSourceLink);
link->filename = g_strdup (filename);
link->line = line;
link->col = col;
return link;
}
EvSourceLink *
ev_source_link_copy (EvSourceLink *link)
{
EvSourceLink *copy;
g_return_val_if_fail (link != NULL, NULL);
copy = g_slice_new (EvSourceLink);
*copy = *link;
copy->filename = g_strdup (link->filename);
return copy;
}
void
ev_source_link_free (EvSourceLink *link)
{
if (link == NULL)
return;
g_free (link->filename);
g_slice_free (EvSourceLink, link);
}
/* EvRectangle */
G_DEFINE_BOXED_TYPE (EvRectangle, ev_rectangle, ev_rectangle_copy, ev_rectangle_free)
EvRectangle *
ev_rectangle_new (void)
{
return g_new0 (EvRectangle, 1);
}
EvRectangle *
ev_rectangle_copy (EvRectangle *rectangle)
{
EvRectangle *new_rectangle;
g_return_val_if_fail (rectangle != NULL, NULL);
new_rectangle = g_new (EvRectangle, 1);
*new_rectangle = *rectangle;
return new_rectangle;
}
void
ev_rectangle_free (EvRectangle *rectangle)
{
g_free (rectangle);
}
/* Compares two rects. returns 0 if they're equal */
#define EPSILON 0.0000001
gint
ev_rect_cmp (EvRectangle *a,
EvRectangle *b)
{
if (a == b)
return 0;
if (a == NULL || b == NULL)
return 1;
return ! ((ABS (a->x1 - b->x1) < EPSILON) &&
(ABS (a->y1 - b->y1) < EPSILON) &&
(ABS (a->x2 - b->x2) < EPSILON) &&
(ABS (a->y2 - b->y2) < EPSILON));
}
|