File indexing completed on 2024-04-28 07:50:11

0001 /*  This file is part of the KDE libraries
0002     SPDX-FileCopyrightText: 2006 Jacob R Rideout <kde@jacobrideout.net>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef TEXTBREAKS_H
0008 #define TEXTBREAKS_H
0009 
0010 class QString;
0011 
0012 #include "sonnetcore_export.h"
0013 
0014 #include <memory>
0015 
0016 namespace Sonnet
0017 {
0018 class TextBreaksPrivate;
0019 
0020 /**
0021  * @short TextBreaks determines the barriers between linguistic structures in any given text.
0022  *
0023  * TextBreaks is a class that determines the boundaries between graphemes
0024  * (characters as per the unicode definition,) words and sentences. The
0025  * default implementation conforms to Unicode Standard Annex #29 https://unicode.org/reports/tr29/.
0026  * You can subclass TextBreaks to create the correct behaviour for languages that require it.
0027  *
0028  * @author Jacob Rideout <kde@jacobrideout.net>
0029  * @since 4.3
0030  */
0031 class SONNETCORE_EXPORT TextBreaks
0032 {
0033 public:
0034     struct Position {
0035         int start, length;
0036     };
0037 
0038     /**
0039      * This structure abstracts the positions of breaks in the test. As per the
0040      * unicode annex, both the start and end of the text are returned.
0041      */
0042     typedef QList<Position> Positions;
0043 
0044     /** Constructor
0045      * Creates a new TextBreaks instance. If @p text is specified,
0046      * it sets the text to be checked.
0047      * @param text the text that is to be checked
0048      */
0049     explicit TextBreaks(const QString &text = QString());
0050 
0051     /** Virtual Destructor
0052      */
0053     virtual ~TextBreaks();
0054 
0055     /**
0056      * Returns the text to be checked
0057      * @return text
0058      */
0059     QString text() const;
0060 
0061     /**
0062      * Sets the text to @p text
0063      * @param text to be set
0064      * @return true if the word is misspelled. false otherwise
0065      */
0066     void setText(const QString &text);
0067 
0068     /**
0069      * Return the Positions of each word for the given  @p text.
0070      * @param text to be checked
0071      * @return positions of breaks
0072      */
0073     static Positions wordBreaks(const QString &text);
0074 
0075     /**
0076      * Return the Positions of each sentence for the given  @p text.
0077      * @param text to be checked
0078      * @return positions of breaks
0079      */
0080     static Positions sentenceBreaks(const QString &text);
0081 
0082     /**
0083      * Return the Positions of each word for the text previously set.
0084      * @return positions of breaks
0085      */
0086     virtual Positions wordBreaks() const;
0087 
0088     /**
0089      * Return the Positions of each sentence for the text previously set.
0090      * @return positions of breaks
0091      */
0092     virtual Positions sentenceBreaks() const;
0093 
0094 private:
0095     std::unique_ptr<TextBreaksPrivate> const d;
0096 };
0097 }
0098 
0099 Q_DECLARE_TYPEINFO(Sonnet::TextBreaks::Position, Q_PRIMITIVE_TYPE);
0100 
0101 #endif