File indexing completed on 2024-05-19 05:05:31

0001 /***************************************************************************
0002  *   SPDX-License-Identifier: GPL-2.0-or-later
0003  *                                                                         *
0004  *   SPDX-FileCopyrightText: 2004-2019 Thomas Fischer <fischer@unix-ag.uni-kl.de>
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  *   This program is distributed in the hope that it will be useful,       *
0012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0014  *   GNU General Public License for more details.                          *
0015  *                                                                         *
0016  *   You should have received a copy of the GNU General Public License     *
0017  *   along with this program; if not, see <https://www.gnu.org/licenses/>. *
0018  ***************************************************************************/
0019 
0020 #ifndef KBIBTEX_IO_ENCODER_H
0021 #define KBIBTEX_IO_ENCODER_H
0022 
0023 #include <QString>
0024 
0025 #ifdef HAVE_KF
0026 #include "kbibtexio_export.h"
0027 #endif // HAVE_KF
0028 
0029 /**
0030  * Base class for that convert between different textual representations
0031  * for non-ASCII characters. Examples for external textual representations
0032  * are \"a in LaTeX and &auml; in XML.
0033  * @author Thomas Fischer <fischer@unix-ag.uni-kl.de>
0034  */
0035 class KBIBTEXIO_EXPORT Encoder
0036 {
0037 public:
0038     enum class TargetEncoding {RAW, ASCII, UTF8};
0039 
0040     static const Encoder &instance();
0041     virtual ~Encoder();
0042 
0043     /**
0044      * Decode from external textual representation to internal (UTF-8) representation.
0045      * @param text text in external textual representation
0046      * @return text in internal (UTF-8) representation
0047      */
0048     virtual QString decode(const QString &text) const;
0049 
0050     /**
0051      * Encode from internal (UTF-8) representation to external textual representation.
0052      * Output may be restricted to ASCII (non-ASCII characters will be rewritten depending
0053      * on concrete Encoder class, for example as '&#228;' as XML or '\"a' for LaTeX)
0054      * or UTF-8 (all characters allowed, only 'special ones' rewritten, for example
0055      * '&amp;' for XML and '\&' for LaTeX).
0056      * @param text in internal (UTF-8) representation
0057      * @param targetEncoding allow either only ASCII output or UTF-8 output.
0058      * @return text text in external textual representation
0059      */
0060     virtual QString encode(const QString &text, const TargetEncoding targetEncoding) const;
0061 
0062     QString convertToPlainAscii(const QString &input) const;
0063     static bool containsOnlyAscii(const QString &text);
0064 
0065 protected:
0066     Encoder();
0067 
0068 private:
0069     Q_DISABLE_COPY(Encoder)
0070 
0071     class Private;
0072     Private *const d;
0073 };
0074 
0075 #endif // KBIBTEX_IO_ENCODER_H