1 | /* |
2 | * Copyright (C) 2009-2010, Pino Toscano <pino@kde.org> |
3 | * Copyright (C) 2013 Adrian Johnson <ajohnson@redneon.com> |
4 | * Copyright (C) 2014, Hans-Peter Deifel <hpdeifel@gmx.de> |
5 | * Copyright (C) 2016 Jakub Alba <jakubalba@gmail.com> |
6 | * Copyright (C) 2017-2019 Albert Astals Cid <aacid@kde.org> |
7 | * Copyright (C) 2018 Suzuki Toshiya <mpsuzuki@hiroshima-u.ac.jp> |
8 | * Copyright (C) 2020 Adam Reichold <adam.reichold@t-online.de> |
9 | * Copyright (C) 2024 Oliver Sander <oliver.sander@tu-dresden.de> |
10 | * |
11 | * This program is free software; you can redistribute it and/or modify |
12 | * it under the terms of the GNU General Public License as published by |
13 | * the Free Software Foundation; either version 2, or (at your option) |
14 | * any later version. |
15 | * |
16 | * This program is distributed in the hope that it will be useful, |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | * GNU General Public License for more details. |
20 | * |
21 | * You should have received a copy of the GNU General Public License |
22 | * along with this program; if not, write to the Free Software |
23 | * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. |
24 | */ |
25 | |
26 | #include "poppler-private.h" |
27 | |
28 | #include "GooString.h" |
29 | #include "Page.h" |
30 | #include "UTF.h" |
31 | |
32 | #include <ctime> |
33 | #include <iostream> |
34 | #include <sstream> |
35 | |
36 | using namespace poppler; |
37 | |
38 | static void stderr_debug_function(const std::string &msg, void * /*data*/) |
39 | { |
40 | std::cerr << "poppler/" << msg << std::endl; |
41 | } |
42 | |
43 | debug_func detail::user_debug_function = stderr_debug_function; |
44 | void *detail::debug_closure = nullptr; |
45 | |
46 | void detail::error_function(ErrorCategory /*category*/, Goffset pos, const char *msg) |
47 | { |
48 | std::ostringstream oss; |
49 | if (pos >= 0) { |
50 | oss << "error (" << pos << "): " ; |
51 | } else { |
52 | oss << "error: " ; |
53 | } |
54 | oss << msg; |
55 | detail::user_debug_function(oss.str(), detail::debug_closure); |
56 | } |
57 | |
58 | rectf detail::pdfrectangle_to_rectf(const PDFRectangle &pdfrect) |
59 | { |
60 | return rectf(pdfrect.x1, pdfrect.y1, pdfrect.x2 - pdfrect.x1, pdfrect.y2 - pdfrect.y1); |
61 | } |
62 | |
63 | ustring detail::unicode_GooString_to_ustring(const GooString *str) |
64 | { |
65 | const char *data = str->c_str(); |
66 | const int len = str->getLength(); |
67 | |
68 | const bool is_unicodeLE = hasUnicodeByteOrderMarkLE(s: str->toStr()); |
69 | const bool is_unicode = hasUnicodeByteOrderMark(s: str->toStr()) || is_unicodeLE; |
70 | int i = is_unicode ? 2 : 0; |
71 | ustring::size_type ret_len = len - i; |
72 | if (is_unicode) { |
73 | ret_len >>= 1; |
74 | } |
75 | ustring ret(ret_len, 0); |
76 | size_t ret_index = 0; |
77 | ustring::value_type u; |
78 | if (is_unicode) { |
79 | while (i < len) { |
80 | u = is_unicodeLE ? ((data[i + 1] & 0xff) << 8) | (data[i] & 0xff) : ((data[i] & 0xff) << 8) | (data[i + 1] & 0xff); |
81 | i += 2; |
82 | ret[ret_index++] = u; |
83 | } |
84 | } else { |
85 | while (i < len) { |
86 | u = data[i] & 0xff; |
87 | ++i; |
88 | ret[ret_index++] = u; |
89 | } |
90 | } |
91 | |
92 | return ret; |
93 | } |
94 | |
95 | ustring detail::unicode_to_ustring(const Unicode *u, int length) |
96 | { |
97 | ustring str(length, 0); |
98 | ustring::iterator it = str.begin(); |
99 | const Unicode *uu = u; |
100 | for (int i = 0; i < length; ++i) { |
101 | *it++ = ustring::value_type(*uu++ & 0xffff); |
102 | } |
103 | return str; |
104 | } |
105 | |
106 | GooString *detail::ustring_to_unicode_GooString(const ustring &str) |
107 | { |
108 | const size_t len = str.size() * 2 + 2; |
109 | const ustring::value_type *me = str.data(); |
110 | byte_array ba(len); |
111 | ba[0] = (char)0xfe; |
112 | ba[1] = (char)0xff; |
113 | for (size_t i = 0; i < str.size(); ++i, ++me) { |
114 | ba[i * 2 + 2] = ((*me >> 8) & 0xff); |
115 | ba[i * 2 + 3] = (*me & 0xff); |
116 | } |
117 | GooString *goo = new GooString(&ba[0], len); |
118 | return goo; |
119 | } |
120 | |