summaryrefslogtreecommitdiffstats
path: root/accessible/windows/sdn/sdnTextAccessible.cpp
blob: 0acffa1661588cbdeeaa61384b7c2107d6eaae21 (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* 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 "sdnTextAccessible.h"

#include "ISimpleDOM.h"

#include "nsCoreUtils.h"
#include "DocAccessible.h"

#include "nsIFrame.h"
#include "nsFontMetrics.h"
#include "nsPresContext.h"
#include "nsLayoutUtils.h"
#include "nsRange.h"
#include "gfxTextRun.h"
#include "nsIAccessibleTypes.h"
#include "mozilla/gfx/2D.h"

using namespace mozilla::a11y;

////////////////////////////////////////////////////////////////////////////////
// sdnTextAccessible
////////////////////////////////////////////////////////////////////////////////

IMPL_IUNKNOWN_QUERY_HEAD(sdnTextAccessible)
IMPL_IUNKNOWN_QUERY_IFACE(ISimpleDOMText)
IMPL_IUNKNOWN_QUERY_TAIL_AGGREGATED(mAccessible)

STDMETHODIMP
sdnTextAccessible::get_domText(BSTR __RPC_FAR* aText) {
  if (!aText) return E_INVALIDARG;
  *aText = nullptr;

  if (mAccessible->IsDefunct()) return CO_E_OBJNOTCONNECTED;

  nsAutoString nodeValue;

  mAccessible->GetContent()->GetNodeValue(nodeValue);
  if (nodeValue.IsEmpty()) return S_FALSE;

  *aText = ::SysAllocStringLen(nodeValue.get(), nodeValue.Length());
  return *aText ? S_OK : E_OUTOFMEMORY;
}

STDMETHODIMP
sdnTextAccessible::get_clippedSubstringBounds(
    unsigned int aStartIndex, unsigned int aEndIndex, int __RPC_FAR* aX,
    int __RPC_FAR* aY, int __RPC_FAR* aWidth, int __RPC_FAR* aHeight) {
  nscoord x = 0, y = 0, width = 0, height = 0;
  HRESULT rv = get_unclippedSubstringBounds(aStartIndex, aEndIndex, &x, &y,
                                            &width, &height);
  if (FAILED(rv)) return rv;

  DocAccessible* document = mAccessible->Document();
  NS_ASSERTION(
      document,
      "There must always be a doc accessible, but there isn't. Crash!");

  nsIntRect docRect = document->Bounds();
  nsIntRect unclippedRect(x, y, width, height);

  nsIntRect clippedRect;
  clippedRect.IntersectRect(unclippedRect, docRect);

  *aX = clippedRect.X();
  *aY = clippedRect.Y();
  *aWidth = clippedRect.Width();
  *aHeight = clippedRect.Height();
  return S_OK;
}

STDMETHODIMP
sdnTextAccessible::get_unclippedSubstringBounds(
    unsigned int aStartIndex, unsigned int aEndIndex, int __RPC_FAR* aX,
    int __RPC_FAR* aY, int __RPC_FAR* aWidth, int __RPC_FAR* aHeight) {
  if (!aX || !aY || !aWidth || !aHeight) return E_INVALIDARG;
  *aX = *aY = *aWidth = *aHeight = 0;

  if (mAccessible->IsDefunct()) return CO_E_OBJNOTCONNECTED;

  nsIFrame* frame = mAccessible->GetFrame();
  NS_ENSURE_TRUE(frame, E_FAIL);

  nsPoint startPoint, endPoint;
  nsIFrame* startFrame =
      GetPointFromOffset(frame, aStartIndex, true, startPoint);
  nsIFrame* endFrame = GetPointFromOffset(frame, aEndIndex, false, endPoint);
  if (!startFrame || !endFrame) return E_FAIL;

  nsRect sum;
  nsIFrame* iter = startFrame;
  nsIFrame* stopLoopFrame = endFrame->GetNextContinuation();
  for (; iter != stopLoopFrame; iter = iter->GetNextContinuation()) {
    nsRect rect = iter->GetScreenRectInAppUnits();
    nscoord start = (iter == startFrame) ? startPoint.x : 0;
    nscoord end = (iter == endFrame) ? endPoint.x : rect.Width();
    rect.MoveByX(start);
    rect.SetWidth(end - start);
    sum.UnionRect(sum, rect);
  }

  nsPresContext* presContext = mAccessible->Document()->PresContext();
  *aX = presContext->AppUnitsToDevPixels(sum.X());
  *aY = presContext->AppUnitsToDevPixels(sum.Y());
  *aWidth = presContext->AppUnitsToDevPixels(sum.Width());
  *aHeight = presContext->AppUnitsToDevPixels(sum.Height());

  return S_OK;
}

STDMETHODIMP
sdnTextAccessible::scrollToSubstring(unsigned int aStartIndex,
                                     unsigned int aEndIndex) {
  if (mAccessible->IsDefunct()) return CO_E_OBJNOTCONNECTED;

  RefPtr<nsRange> range = nsRange::Create(mAccessible->GetContent());
  if (NS_FAILED(range->SetStart(mAccessible->GetContent(), aStartIndex)))
    return E_FAIL;

  if (NS_FAILED(range->SetEnd(mAccessible->GetContent(), aEndIndex)))
    return E_FAIL;

  nsresult rv = nsCoreUtils::ScrollSubstringTo(
      mAccessible->GetFrame(), range,
      nsIAccessibleScrollType::SCROLL_TYPE_ANYWHERE);
  return GetHRESULT(rv);
}

STDMETHODIMP
sdnTextAccessible::get_fontFamily(BSTR __RPC_FAR* aFontFamily) {
  if (!aFontFamily) return E_INVALIDARG;
  *aFontFamily = nullptr;

  if (mAccessible->IsDefunct()) return CO_E_OBJNOTCONNECTED;

  nsIFrame* frame = mAccessible->GetFrame();
  if (!frame) return E_FAIL;

  RefPtr<nsFontMetrics> fm = nsLayoutUtils::GetFontMetricsForFrame(frame, 1.0f);

  const nsCString& name =
      fm->GetThebesFontGroup()->GetFirstValidFont()->GetName();
  if (name.IsEmpty()) return S_FALSE;

  NS_ConvertUTF8toUTF16 str(name);
  *aFontFamily = ::SysAllocStringLen(str.get(), str.Length());
  return *aFontFamily ? S_OK : E_OUTOFMEMORY;
}

nsIFrame* sdnTextAccessible::GetPointFromOffset(nsIFrame* aContainingFrame,
                                                int32_t aOffset,
                                                bool aPreferNext,
                                                nsPoint& aOutPoint) {
  nsIFrame* textFrame = nullptr;
  int32_t outOffset;
  aContainingFrame->GetChildFrameContainingOffset(aOffset, aPreferNext,
                                                  &outOffset, &textFrame);
  if (textFrame) textFrame->GetPointFromOffset(aOffset, &aOutPoint);

  return textFrame;
}