summaryrefslogtreecommitdiff
path: root/vcl/unx/gtk4/gtkaccessibletext.cxx
blob: 51091e7353273b19c6d94912725d7a15f1853e51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <vector>

#include <com/sun/star/accessibility/AccessibleTextType.hpp>
#include <com/sun/star/accessibility/TextSegment.hpp>
#include <com/sun/star/accessibility/XAccessibleText.hpp>
#include <com/sun/star/accessibility/XAccessibleTextAttributes.hpp>
#include <o3tl/any.hxx>
#include <sal/log.hxx>

#include "a11y.hxx"
#include "gtkaccessibletext.hxx"

#if GTK_CHECK_VERSION(4, 14, 0)

namespace
{
sal_Int16 lcl_GtkTextGranularityToUNOBoundaryType(GtkAccessibleTextGranularity eGranularity)
{
    switch (eGranularity)
    {
        case GTK_ACCESSIBLE_TEXT_GRANULARITY_CHARACTER:
            return com::sun::star::accessibility::AccessibleTextType::CHARACTER;
        case GTK_ACCESSIBLE_TEXT_GRANULARITY_WORD:
            return com::sun::star::accessibility::AccessibleTextType::WORD;
        case GTK_ACCESSIBLE_TEXT_GRANULARITY_SENTENCE:
            return com::sun::star::accessibility::AccessibleTextType::SENTENCE;
        case GTK_ACCESSIBLE_TEXT_GRANULARITY_LINE:
            return com::sun::star::accessibility::AccessibleTextType::LINE;
        case GTK_ACCESSIBLE_TEXT_GRANULARITY_PARAGRAPH:
            return com::sun::star::accessibility::AccessibleTextType::PARAGRAPH;
        default:
            assert(false && "Unhandled GtkAccessibleTextGranularity.");
            return GTK_ACCESSIBLE_TEXT_GRANULARITY_CHARACTER;
    }
}

css::uno::Reference<css::accessibility::XAccessibleText> getXText(GtkAccessibleText* pGtkText)
{
    LoAccessible* pAccessible = LO_ACCESSIBLE(pGtkText);
    if (!pAccessible->uno_accessible)
        return nullptr;

    css::uno::Reference<css::accessibility::XAccessibleContext> xContext(
        pAccessible->uno_accessible->getAccessibleContext());

    css::uno::Reference<css::accessibility::XAccessibleText> xText(xContext, css::uno::UNO_QUERY);
    return xText;
}
}

static GBytes* lo_accessible_text_get_contents(GtkAccessibleText* self, unsigned int start,
                                               unsigned int end)
{
    css::uno::Reference<css::accessibility::XAccessibleText> xText = getXText(self);
    if (!xText.is())
        return nullptr;

    // G_MAXUINT has special meaning: end of the text
    const sal_Int32 nEndIndex = (end == G_MAXUINT) ? xText->getCharacterCount() : end;

    const OString sText
        = rtl::OUStringToOString(xText->getTextRange(start, nEndIndex), RTL_TEXTENCODING_UTF8);
    return g_bytes_new(sText.getStr(), sText.getLength());
}

static GBytes* lo_accessible_text_get_contents_at(GtkAccessibleText* self, unsigned int offset,
                                                  GtkAccessibleTextGranularity eGranularity,
                                                  unsigned int* start, unsigned int* end)
{
    css::uno::Reference<css::accessibility::XAccessibleText> xText = getXText(self);
    if (!xText.is())
        return nullptr;

    if (offset > o3tl::make_unsigned(xText->getCharacterCount()))
    {
        SAL_WARN("vcl.gtk",
                 "lo_accessible_text_get_contents_at called with invalid offset: " << offset);
        return nullptr;
    }

    const sal_Int16 nUnoBoundaryType = lcl_GtkTextGranularityToUNOBoundaryType(eGranularity);
    const css::accessibility::TextSegment aSegment
        = xText->getTextAtIndex(offset, nUnoBoundaryType);
    *start = o3tl::make_unsigned(aSegment.SegmentStart);
    *end = o3tl::make_unsigned(aSegment.SegmentEnd);
    const OString sText = rtl::OUStringToOString(aSegment.SegmentText, RTL_TEXTENCODING_UTF8);
    return g_bytes_new(sText.getStr(), sText.getLength());
}

static unsigned int lo_accessible_text_get_caret_position(GtkAccessibleText* self)
{
    css::uno::Reference<css::accessibility::XAccessibleText> xText = getXText(self);
    if (!xText.is())
        return 0;

    return std::max(0, xText->getCaretPosition());
}

static gboolean lo_accessible_text_get_selection(GtkAccessibleText* self, gsize* n_ranges,
                                                 GtkAccessibleTextRange** ranges)
{
    css::uno::Reference<css::accessibility::XAccessibleText> xText = getXText(self);
    if (!xText.is())
        return 0;

    if (xText->getSelectedText().isEmpty())
        return false;

    const sal_Int32 nSelectionStart = xText->getSelectionStart();
    const sal_Int32 nSelectionEnd = xText->getSelectionEnd();

    *n_ranges = 1;
    *ranges = g_new(GtkAccessibleTextRange, 1);
    (*ranges)[0].start = std::min(nSelectionStart, nSelectionEnd);
    (*ranges)[0].length = std::abs(nSelectionEnd - nSelectionStart);
    return true;
}

static int
convertUnoTextAttributesToGtk(const css::uno::Sequence<css::beans::PropertyValue>& rAttribs,
                              char*** attribute_names, char*** attribute_values)
{
    std::vector<std::pair<OString, OUString>> aNameValuePairs;
    for (const css::beans::PropertyValue& rAttribute : rAttribs)
    {
        if (rAttribute.Name == "CharFontName")
        {
            const OUString sValue = *o3tl::doAccess<OUString>(rAttribute.Value);
            aNameValuePairs.emplace_back(GTK_ACCESSIBLE_ATTRIBUTE_FAMILY, sValue);
        }
    }

    if (aNameValuePairs.empty())
        return 0;

    const int nCount = aNameValuePairs.size();
    *attribute_names = g_new(char*, nCount + 1);
    *attribute_values = g_new(char*, nCount + 1);
    for (int i = 0; i < nCount; i++)
    {
        (*attribute_names)[i] = g_strdup(aNameValuePairs[i].first.getStr());
        (*attribute_values)[i] = g_strdup(
            OUStringToOString(aNameValuePairs[i].second, RTL_TEXTENCODING_UTF8).getStr());
    }
    (*attribute_names)[nCount] = nullptr;
    (*attribute_values)[nCount] = nullptr;

    return nCount;
}

static gboolean lo_accessible_text_get_attributes(GtkAccessibleText* self, unsigned int offset,
                                                  gsize* n_ranges, GtkAccessibleTextRange** ranges,
                                                  char*** attribute_names, char*** attribute_values)
{
    css::uno::Reference<css::accessibility::XAccessibleText> xText = getXText(self);
    if (!xText.is())
        return false;

    const unsigned int nTextLength = o3tl::make_unsigned(xText->getCharacterCount());
    if (offset == nTextLength)
        offset = nTextLength - 1;

    if (offset >= nTextLength)
    {
        SAL_WARN("vcl.gtk",
                 "lo_accessible_text_get_attributes called with invalid offset: " << offset);
        return false;
    }

    css::uno::Sequence<css::beans::PropertyValue> aAttribs;
    css::uno::Reference<css::accessibility::XAccessibleTextAttributes> xAttributes(
        xText, com::sun::star::uno::UNO_QUERY);
    if (xAttributes.is())
        aAttribs = xAttributes->getRunAttributes(offset, css::uno::Sequence<OUString>());
    else
        aAttribs = xText->getCharacterAttributes(offset, css::uno::Sequence<OUString>());

    const int nCount = convertUnoTextAttributesToGtk(aAttribs, attribute_names, attribute_values);
    if (nCount == 0)
        return false;

    *n_ranges = nCount;
    *ranges = g_new(GtkAccessibleTextRange, nCount);
    // just use start and end of attribute run for each single attribute
    const css::accessibility::TextSegment aAttributeRun
        = xText->getTextAtIndex(offset, css::accessibility::AccessibleTextType::ATTRIBUTE_RUN);
    for (int i = 0; i < nCount; i++)
    {
        ((*ranges)[i]).start = aAttributeRun.SegmentStart;
        ((*ranges)[i]).length = aAttributeRun.SegmentEnd - aAttributeRun.SegmentStart;
    }

    return true;
}

static void lo_accessible_text_get_default_attributes(GtkAccessibleText* self,
                                                      char*** attribute_names,
                                                      char*** attribute_values)
{
    css::uno::Reference<css::accessibility::XAccessibleText> xText = getXText(self);
    if (!xText.is())
        return;

    css::uno::Reference<css::accessibility::XAccessibleTextAttributes> xAttributes(
        xText, com::sun::star::uno::UNO_QUERY);
    if (!xAttributes.is())
        return;

    css::uno::Sequence<css::beans::PropertyValue> aAttribs
        = xAttributes->getDefaultAttributes(css::uno::Sequence<OUString>());

    convertUnoTextAttributesToGtk(aAttribs, attribute_names, attribute_values);
}

void lo_accessible_text_init(GtkAccessibleTextInterface* iface)
{
    iface->get_contents = lo_accessible_text_get_contents;
    iface->get_contents_at = lo_accessible_text_get_contents_at;
    iface->get_caret_position = lo_accessible_text_get_caret_position;
    iface->get_selection = lo_accessible_text_get_selection;
    iface->get_attributes = lo_accessible_text_get_attributes;
    iface->get_default_attributes = lo_accessible_text_get_default_attributes;
}

#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */