DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (31ec81b5d7bb)

VCS Links

Line Code
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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * 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 "gfxDWriteTextAnalysis.h"

TextAnalysis::TextAnalysis(const wchar_t* text,
                           UINT32 textLength,
                           const wchar_t* localeName,
                           DWRITE_READING_DIRECTION readingDirection)
  : mText(text)
  , mTextLength(textLength)
  , mLocaleName(localeName)
  , mReadingDirection(readingDirection)
  , mCurrentRun(nullptr)
{
}

TextAnalysis::~TextAnalysis()
{
    // delete runs, except mRunHead which is part of the TextAnalysis object
    for (Run *run = mRunHead.nextRun; run;) {
        Run *origRun = run;
        run = run->nextRun;
        delete origRun;
    }
}

STDMETHODIMP 
TextAnalysis::GenerateResults(IDWriteTextAnalyzer* textAnalyzer,
                              OUT Run **runHead)
{
    // Analyzes the text using the script analyzer and returns
    // the result as a series of runs.

    HRESULT hr = S_OK;

    // Initially start out with one result that covers the entire range.
    // This result will be subdivided by the analysis processes.
    mRunHead.mTextStart = 0;
    mRunHead.mTextLength = mTextLength;
    mRunHead.mBidiLevel = 
        (mReadingDirection == DWRITE_READING_DIRECTION_RIGHT_TO_LEFT);
    mRunHead.nextRun = nullptr;
    mCurrentRun = &mRunHead;

    // Call each of the analyzers in sequence, recording their results.
    if (SUCCEEDED(hr = textAnalyzer->AnalyzeScript(this,
                                                   0,
                                                   mTextLength,
                                                   this))) {
        *runHead = &mRunHead;
    }

    return hr;
}


////////////////////////////////////////////////////////////////////////////////
// IDWriteTextAnalysisSource source implementation

IFACEMETHODIMP 
TextAnalysis::GetTextAtPosition(UINT32 textPosition,
                                OUT WCHAR const** textString,
                                OUT UINT32* textLength)
{
    if (textPosition >= mTextLength) {
        // No text at this position, valid query though.
        *textString = nullptr;
        *textLength = 0;
    } else {
        *textString = mText + textPosition;
        *textLength = mTextLength - textPosition;
    }
    return S_OK;
}


IFACEMETHODIMP 
TextAnalysis::GetTextBeforePosition(UINT32 textPosition,
                                    OUT WCHAR const** textString,
                                    OUT UINT32* textLength)
{
    if (textPosition == 0 || textPosition > mTextLength) {
        // Either there is no text before here (== 0), or this
        // is an invalid position. The query is considered valid thouh.
        *textString = nullptr;
        *textLength = 0;
    } else {
        *textString = mText;
        *textLength = textPosition;
    }
    return S_OK;
}


DWRITE_READING_DIRECTION STDMETHODCALLTYPE 
TextAnalysis::GetParagraphReadingDirection()
{
    // We support only a single reading direction.
    return mReadingDirection;
}


IFACEMETHODIMP 
TextAnalysis::GetLocaleName(UINT32 textPosition,
                            OUT UINT32* textLength,
                            OUT WCHAR const** localeName)
{
    // Single locale name is used, valid until the end of the string.
    *localeName = mLocaleName;
    *textLength = mTextLength - textPosition;

    return S_OK;
}


IFACEMETHODIMP 
TextAnalysis::GetNumberSubstitution(UINT32 textPosition,
                                    OUT UINT32* textLength,
                                    OUT IDWriteNumberSubstitution** numberSubstitution)
{
    // We do not support number substitution.
    *numberSubstitution = nullptr;
    *textLength = mTextLength - textPosition;

    return S_OK;
}


////////////////////////////////////////////////////////////////////////////////
// IDWriteTextAnalysisSink implementation

IFACEMETHODIMP 
TextAnalysis::SetLineBreakpoints(UINT32 textPosition,
                                 UINT32 textLength,
                                 DWRITE_LINE_BREAKPOINT const* lineBreakpoints)
{
    // We don't use this for now.
    return S_OK;
}


IFACEMETHODIMP 
TextAnalysis::SetScriptAnalysis(UINT32 textPosition,
                                UINT32 textLength,
                                DWRITE_SCRIPT_ANALYSIS const* scriptAnalysis)
{
    SetCurrentRun(textPosition);
    SplitCurrentRun(textPosition);
    while (textLength > 0) {
        Run *run = FetchNextRun(&textLength);
        run->mScript = *scriptAnalysis;
    }

    return S_OK;
}


IFACEMETHODIMP 
TextAnalysis::SetBidiLevel(UINT32 textPosition,
                           UINT32 textLength,
                           UINT8 explicitLevel,
                           UINT8 resolvedLevel)
{
    // We don't use this for now.
    return S_OK;
}


IFACEMETHODIMP 
TextAnalysis::SetNumberSubstitution(UINT32 textPosition,
                                    UINT32 textLength,
                                    IDWriteNumberSubstitution* numberSubstitution)
{
    // We don't use this for now.
    return S_OK;
}


////////////////////////////////////////////////////////////////////////////////
// Run modification.

TextAnalysis::Run *
TextAnalysis::FetchNextRun(IN OUT UINT32* textLength)
{
    // Used by the sink setters, this returns a reference to the next run.
    // Position and length are adjusted to now point after the current run
    // being returned.

    Run *origRun = mCurrentRun;
    // Split the tail if needed (the length remaining is less than the
    // current run's size).
    if (*textLength < mCurrentRun->mTextLength) {
        SplitCurrentRun(mCurrentRun->mTextStart + *textLength);
    } else {
        // Just advance the current run.
        mCurrentRun = mCurrentRun->nextRun;
    }
    *textLength -= origRun->mTextLength;

    // Return a reference to the run that was just current.
    return origRun;
}


void TextAnalysis::SetCurrentRun(UINT32 textPosition)
{
    // Move the current run to the given position.
    // Since the analyzers generally return results in a forward manner,
    // this will usually just return early. If not, find the
    // corresponding run for the text position.

    if (mCurrentRun && mCurrentRun->ContainsTextPosition(textPosition)) {
        return;
    }

    for (Run *run = &mRunHead; run; run = run->nextRun) {
        if (run->ContainsTextPosition(textPosition)) {
            mCurrentRun = run;
            return;
        }
    }
    NS_NOTREACHED("We should always be able to find the text position in one \
        of our runs");
}


void TextAnalysis::SplitCurrentRun(UINT32 splitPosition)
{
    if (!mCurrentRun) {
        NS_ASSERTION(false, "SplitCurrentRun called without current run.");
        // Shouldn't be calling this when no current run is set!
        return;
    }
    // Split the current run.
    if (splitPosition <= mCurrentRun->mTextStart) {
        // No need to split, already the start of a run
        // or before it. Usually the first.
        return;
    }
    Run *newRun = new Run;

    *newRun = *mCurrentRun;

    // Insert the new run in our linked list.
    newRun->nextRun = mCurrentRun->nextRun;
    mCurrentRun->nextRun = newRun;

    // Adjust runs' text positions and lengths.
    UINT32 splitPoint = splitPosition - mCurrentRun->mTextStart;
    newRun->mTextStart += splitPoint;
    newRun->mTextLength -= splitPoint;
    mCurrentRun->mTextLength = splitPoint;
    mCurrentRun = newRun;
}