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

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_ENCODERLATEX_H
0021 #define KBIBTEX_IO_ENCODERLATEX_H
0022 
0023 #include <QIODevice>
0024 
0025 #include <Encoder>
0026 
0027 #ifdef HAVE_KF
0028 #include "kbibtexio_export.h"
0029 #endif // HAVE_KF
0030 
0031 /**
0032  * Base class for that convert between different textual representations
0033  * for non-ASCII characters, specialized for LaTeX. For example, this
0034  * class can "translate" between \"a and its UTF-8 representation.
0035  * @author Thomas Fischer <fischer@unix-ag.uni-kl.de>
0036  */
0037 class KBIBTEXIO_EXPORT EncoderLaTeX : public Encoder
0038 {
0039 public:
0040     QString decode(const QString &text) const override;
0041     QString encode(const QString &text, const TargetEncoding targetEncoding) const override;
0042 
0043     static const EncoderLaTeX &instance();
0044     ~EncoderLaTeX() override;
0045 
0046 protected:
0047     EncoderLaTeX();
0048 
0049     /**
0050      * This data structure keeps individual characters that have
0051      * a special purpose in LaTeX and therefore needs to be escaped
0052      * both in text and in math mode by prefixing with a backlash.
0053      */
0054     static const QChar encoderLaTeXProtectedSymbols[];
0055 
0056     /**
0057      * This data structure keeps individual characters that have
0058      * a special purpose in LaTeX in text mode and therefore needs
0059      * to be escaped by prefixing with a backlash. In math mode,
0060      * those have a different purpose and may not be escaped there.
0061      */
0062     static const QChar encoderLaTeXProtectedTextOnlySymbols[];
0063 
0064     /**
0065      * Check if input data contains a verbatim command like \url{...},
0066      * append it to output, and update the position to point to the next
0067      * character after the verbatim command.
0068      * @return 'true' if a verbatim command has been copied, otherwise 'false'.
0069      */
0070     bool testAndCopyVerbatimCommands(const QString &input, int &pos, QString &output) const;
0071 
0072 private:
0073     Q_DISABLE_COPY(EncoderLaTeX)
0074 
0075     /**
0076      * Check if character c represents a modifier like
0077      * a quotation mark in \"a. Return the modifier's
0078      * index in the lookup table or -1 if not a known
0079      * modifier.
0080      */
0081     int modifierInLookupTable(const QChar modifier) const;
0082 
0083     /**
0084      * Return a string that represents the part of
0085      * the base string starting from startFrom contains
0086      * only alpha characters (a-z,A-Z).
0087      * Return value may be an empty string.
0088      */
0089     QString readAlphaCharacters(const QString &base, int startFrom) const;
0090 };
0091 
0092 #endif // KBIBTEX_IO_ENCODERLATEX_H