poppler-private.cpp 3.41 KB
Newer Older
1
/*
Pino Toscano's avatar
Pino Toscano committed
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>
Jakub Alba's avatar
Jakub Alba committed
5
 * Copyright (C) 2016 Jakub Alba <jakubalba@gmail.com>
Albert Astals Cid's avatar
Albert Astals Cid committed
6
 * Copyright (C) 2017, 2018 Albert Astals Cid <aacid@kde.org>
Albert Astals Cid's avatar
Albert Astals Cid committed
7
 * Copyright (C) 2018 Suzuki Toshiya <mpsuzuki@hiroshima-u.ac.jp>
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * 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 "poppler-private.h"

#include "GooString.h"
#include "Page.h"

29
#include <ctime>
30 31 32 33 34
#include <iostream>
#include <sstream>

using namespace poppler;

35 36
static void stderr_debug_function(const std::string &msg, void * /*data*/)
{
37
    std::cerr << "poppler/" << msg << std::endl;
38 39 40
}

debug_func detail::user_debug_function = stderr_debug_function;
41
void *detail::debug_closure = nullptr;
42

43
void detail::error_function(void * /*data*/, ErrorCategory /*category*/, Goffset pos, const char *msg)
44 45 46
{
    std::ostringstream oss;
    if (pos >= 0) {
47
        oss << "error (" << pos << "): ";
48
    } else {
49
        oss << "error: ";
50
    }
51
    oss << msg;
52
    detail::user_debug_function(oss.str(), detail::debug_closure);
53 54 55 56 57 58 59
}

rectf detail::pdfrectangle_to_rectf(const PDFRectangle &pdfrect)
{
    return rectf(pdfrect.x1, pdfrect.y1, pdfrect.x2 - pdfrect.x1, pdfrect.y2 - pdfrect.y1);
}

Albert Astals Cid's avatar
Albert Astals Cid committed
60
ustring detail::unicode_GooString_to_ustring(const GooString *str)
61
{
62
    const char *data = str->c_str();
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
    const int len = str->getLength();

    int i = 0;
    bool is_unicode = false;
    if ((data[0] & 0xff) == 0xfe && (len > 1 && (data[1] & 0xff) == 0xff)) {
        is_unicode = true;
        i = 2;
    }
    ustring::size_type ret_len = len - i;
    if (is_unicode) {
        ret_len >>= 1;
    }
    ustring ret(ret_len, 0);
    size_t ret_index = 0;
    ustring::value_type u;
    if (is_unicode) {
        while (i < len) {
            u = ((data[i] & 0xff) << 8) | (data[i + 1] & 0xff);
            i += 2;
            ret[ret_index++] = u;
        }
    } else {
        while (i < len) {
            u = data[i] & 0xff;
            ++i;
            ret[ret_index++] = u;
        }
    }

    return ret;
93 94 95 96
}

ustring detail::unicode_to_ustring(const Unicode *u, int length)
{
97
    ustring str(length, 0);
98
    ustring::iterator it = str.begin();
99
    const Unicode *uu = u;
100 101
    for (int i = 0; i < length; ++i) {
        *it++ = ustring::value_type(*uu++ & 0xffff);
102 103 104 105 106 107
    }
    return str;
}

GooString* detail::ustring_to_unicode_GooString(const ustring &str)
{
108 109 110
    const size_t len = str.size() * 2 + 2;
    const ustring::value_type *me = str.data();
    byte_array ba(len);
Albert Astals Cid's avatar
Albert Astals Cid committed
111 112
    ba[0] = (char)0xfe;
    ba[1] = (char)0xff;
113 114 115 116
    for (size_t i = 0; i < str.size(); ++i, ++me) {
        ba[i * 2 + 2] = ((*me >> 8) & 0xff);
        ba[i * 2 + 3] = (*me & 0xff);
    }
117
    GooString *goo = new GooString(&ba[0], len);
118 119
    return goo;
}