summaryrefslogtreecommitdiffstats
path: root/WebKit/android/nav/SelectText.cpp
blob: e4713075f8fb58c8195e8e5eadf6694094e20ba0 (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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
 * Copyright 2008, The Android Open Source Project
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#define LOG_TAG "webcoreglue"

#include "CachedPrefix.h"
#include "CachedRoot.h"
#include "LayerAndroid.h"
#include "SelectText.h"
#include "SkBitmap.h"
#include "SkBounder.h"
#include "SkCanvas.h"
#include "SkMatrix.h"
#include "SkPicture.h"
#include "SkPixelXorXfermode.h"
#include "SkPoint.h"
#include "SkRect.h"
#include "SkRegion.h"
#include "SkUtils.h"

#ifdef DEBUG_NAV_UI
#include "CString.h"
#endif

namespace android {

class CommonCheck : public SkBounder {
public:
    CommonCheck() : mMatrix(NULL), mPaint(NULL) {}
    
    virtual void setUp(const SkPaint& paint, const SkMatrix& matrix, SkScalar y,
            const void* text) {
        mMatrix = &matrix;
        mPaint = &paint;
        mText = static_cast<const uint16_t*>(text);
        mY = y;
        mBase = mBottom = mTop = INT_MAX;
    }
    
    int base() {
        if (mBase == INT_MAX) {
            SkPoint result;
            mMatrix->mapXY(0, mY, &result);
            mBase = SkScalarFloor(result.fY);
        }
        return mBase;
    }
    
     int bottom() {
        if (mBottom == INT_MAX) {
            SkPoint result;
            SkPaint::FontMetrics metrics;
            mPaint->getFontMetrics(&metrics);
            mMatrix->mapXY(0, metrics.fDescent + mY, &result);
            mBottom = SkScalarCeil(result.fY);
        }
        return mBottom;
    }
    
    int top() {
        if (mTop == INT_MAX) {
            SkPoint result;
            SkPaint::FontMetrics metrics;
            mPaint->getFontMetrics(&metrics);
            mMatrix->mapXY(0, metrics.fAscent + mY, &result);
            mTop = SkScalarFloor(result.fY);
        }
        return mTop;
    }
    
protected:   
    const SkMatrix* mMatrix;
    const SkPaint* mPaint;
    const uint16_t* mText;
    SkScalar mY;
    int mBase;
    int mBottom;
    int mTop;
};

class FirstCheck : public CommonCheck {
public:
    FirstCheck(int x, int y) 
            : mDistance(INT_MAX), mFocusX(x), mFocusY(y) {
        mBestBounds.setEmpty(); 
    }

    const SkIRect& bestBounds() { 
        DBG_NAV_LOGD("mBestBounds:(%d, %d, %d, %d) mTop=%d mBottom=%d", 
            mBestBounds.fLeft, mBestBounds.fTop, mBestBounds.fRight, 
            mBestBounds.fBottom, mTop, mBottom);
        return mBestBounds; 
    }
    
    void offsetBounds(int dx, int dy) {
        mBestBounds.offset(dx, dy);
    }
    
    virtual bool onIRect(const SkIRect& rect) {
        int dx = ((rect.fLeft + rect.fRight) >> 1) - mFocusX;
        int dy = ((top() + bottom()) >> 1) - mFocusY;
        int distance = dx * dx + dy * dy;
#ifdef EXTRA_NOISY_LOGGING
        if (distance < 500 || abs(distance - mDistance) < 500)
            DBG_NAV_LOGD("distance=%d mDistance=%d", distance, mDistance);
#endif
        if (mDistance > distance) {
            mDistance = distance;
            mBestBounds.set(rect.fLeft, top(), rect.fRight, bottom());
#ifdef EXTRA_NOISY_LOGGING
            DBG_NAV_LOGD("mBestBounds={%d,%d,r=%d,b=%d}", 
                mBestBounds.fLeft, mBestBounds.fTop,
                mBestBounds.fRight, mBestBounds.fBottom);
#endif
        }
        return false;
    }
protected:
    SkIRect mBestBounds;
    int mDistance;
    int mFocusX;
    int mFocusY;
};

class MultilineBuilder : public CommonCheck {
public:
    MultilineBuilder(const SkIRect& start, const SkIRect& end, int dx, int dy,
            SkRegion* region)
            : mStart(start), mEnd(end), mSelectRegion(region), mCapture(false) {
        mLast.setEmpty();
        mLastBase = INT_MAX;
        mStart.offset(-dx, -dy);
        mEnd.offset(-dx, -dy);
    }

    virtual bool onIRect(const SkIRect& rect) {
        bool captureLast = false;
        if ((rect.fLeft == mStart.fLeft && rect.fRight == mStart.fRight &&
                top() == mStart.fTop && bottom() == mStart.fBottom) ||
                (rect.fLeft == mEnd.fLeft && rect.fRight == mEnd.fRight &&
                top() == mEnd.fTop && bottom() == mEnd.fBottom)) {
            captureLast = mCapture;
            mCapture ^= true;
        }
        if (mCapture || captureLast) {
            SkIRect full;
            full.set(rect.fLeft, top(), rect.fRight, bottom());
            if ((mLast.fTop < base() && mLast.fBottom >= base())
                    || (mLastBase <= full.fBottom && mLastBase > full.fTop)) {
                if (full.fLeft > mLast.fRight)
                    full.fLeft = mLast.fRight;
                else if (full.fRight < mLast.fLeft)
                    full.fRight = mLast.fLeft;
            }
            mSelectRegion->op(full, SkRegion::kUnion_Op);
            DBG_NAV_LOGD("MultilineBuilder full=(%d,%d,r=%d,b=%d)",
                full.fLeft, full.fTop, full.fRight, full.fBottom);
            mLast = full;
            mLastBase = base();
            if (mStart == mEnd)
                mCapture = false;
        }
        return false;
    }
protected:    
    SkIRect mStart;
    SkIRect mEnd;
    SkIRect mLast;
    int mLastBase;
    SkRegion* mSelectRegion;
    bool mCapture;
};

#define HYPHEN_MINUS 0x2D // ASCII hyphen
#define HYPHEN 0x2010 // unicode hyphen, first in range of dashes
#define HORZ_BAR 0x2015 // unicode horizontal bar, last in range of dashes

class TextExtractor : public CommonCheck {
public:
    TextExtractor(const SkRegion& region) : mSelectRegion(region),
        mSkipFirstSpace(true) { // don't start with a space
    }

    virtual void setUp(const SkPaint& paint, const SkMatrix& matrix, SkScalar y,
            const void* text) {
        INHERITED::setUp(paint, matrix, y, text);
        SkPaint charPaint = paint;
        charPaint.setTextEncoding(SkPaint::kUTF8_TextEncoding);
        mMinSpaceWidth = std::max(0, SkScalarToFixed(
            charPaint.measureText(" ", 1)) - SK_Fixed1);
    }

    virtual bool onIRectGlyph(const SkIRect& rect,
        const SkBounder::GlyphRec& rec)
    {
        SkIRect full;
        full.set(rect.fLeft, top(), rect.fRight, bottom());
        if (mSelectRegion.contains(full)) {
            if (!mSkipFirstSpace && (mLastUni < HYPHEN || mLastUni > HORZ_BAR)
                    && mLastUni != HYPHEN_MINUS
                    && (mLastGlyph.fLSB.fY != rec.fLSB.fY // new baseline
                    || mLastGlyph.fLSB.fX > rec.fLSB.fX // glyphs are LTR
                    || mLastGlyph.fRSB.fX + mMinSpaceWidth < rec.fLSB.fX)) {
                DBG_NAV_LOGD("TextExtractor append space"
                    " mLast=(%d,%d,r=%d,b=%d) mLastGlyph=((%g,%g),(%g,%g),%d)"
                    " full=(%d,%d,r=%d,b=%d) rec=((%g,%g),(%g,%g),%d)"
                    " mMinSpaceWidth=%g",
                    mLast.fLeft, mLast.fTop, mLast.fRight, mLast.fBottom,
                    SkFixedToScalar(mLastGlyph.fLSB.fX),
                    SkFixedToScalar(mLastGlyph.fLSB.fY),
                    SkFixedToScalar(mLastGlyph.fRSB.fX),
                    SkFixedToScalar(mLastGlyph.fRSB.fY), mLastGlyph.fGlyphID,
                    full.fLeft, full.fTop, full.fRight, full.fBottom,
                    SkFixedToScalar(rec.fLSB.fX),
                    SkFixedToScalar(rec.fLSB.fY),
                    SkFixedToScalar(rec.fRSB.fX),
                    SkFixedToScalar(rec.fRSB.fY), rec.fGlyphID,
                    SkFixedToScalar(mMinSpaceWidth));
                *mSelectText.append() = ' ';
            } else
                mSkipFirstSpace = false;
            DBG_NAV_LOGD("TextExtractor [%02x] append full=(%d,%d,r=%d,b=%d)",
                rec.fGlyphID, full.fLeft, full.fTop, full.fRight, full.fBottom);
            SkPaint utfPaint = *mPaint;
            utfPaint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
            utfPaint.glyphsToUnichars(&rec.fGlyphID, 1, &mLastUni);
            if (mLastUni) {
                uint16_t chars[2];
                size_t count = SkUTF16_FromUnichar(mLastUni, chars);
                *mSelectText.append() = chars[0];
                if (count == 2)
                    *mSelectText.append() = chars[1];
            }
            mLast = full;
            mLastGlyph = rec;
        } else {
            mSkipFirstSpace = true;
            DBG_NAV_LOGD("TextExtractor [%02x] skip full=(%d,%d,r=%d,b=%d)",
                rec.fGlyphID, full.fLeft, full.fTop, full.fRight, full.fBottom);
        }
        return false;
    }

    WebCore::String text() {
        return WebCore::String(mSelectText.begin(), mSelectText.count());
    }

protected:
    const SkRegion& mSelectRegion;
    SkTDArray<uint16_t> mSelectText;
    SkIRect mLast;
    SkBounder::GlyphRec mLastGlyph;
    SkUnichar mLastUni;
    SkFixed mMinSpaceWidth;
    bool mSkipFirstSpace;
private:
    typedef CommonCheck INHERITED;
};

class TextCanvas : public SkCanvas {
public:

    TextCanvas(CommonCheck* bounder, const SkPicture& picture, const SkIRect& area) 
            : mBounder(*bounder) {
        setBounder(bounder);
        SkBitmap bitmap;
        bitmap.setConfig(SkBitmap::kARGB_8888_Config, area.width(), 
            area.height());
        setBitmapDevice(bitmap);
        translate(SkIntToScalar(-area.fLeft), SkIntToScalar(-area.fTop));
    }

    virtual ~TextCanvas() {
        setBounder(NULL);
    }

    virtual void drawPaint(const SkPaint& paint) {
    }

    virtual void drawPoints(PointMode mode, size_t count, const SkPoint pts[],
                            const SkPaint& paint) {
    }

    virtual void drawRect(const SkRect& rect, const SkPaint& paint) {
    }

    virtual void drawPath(const SkPath& path, const SkPaint& paint) {
    }

    virtual void commonDrawBitmap(const SkBitmap& bitmap,
                              const SkMatrix& matrix, const SkPaint& paint) {
    }

    virtual void drawSprite(const SkBitmap& bitmap, int left, int top,
                            const SkPaint* paint = NULL) {
    }

    virtual void drawText(const void* text, size_t byteLength, SkScalar x, 
                          SkScalar y, const SkPaint& paint) {
        mBounder.setUp(paint, getTotalMatrix(), y, text);
        SkCanvas::drawText(text, byteLength, x, y, paint);
    }

    virtual void drawPosTextH(const void* text, size_t byteLength,
                              const SkScalar xpos[], SkScalar constY,
                              const SkPaint& paint) {
        mBounder.setUp(paint, getTotalMatrix(), constY, text);
        SkCanvas::drawPosTextH(text, byteLength, xpos, constY, paint);
    }

    virtual void drawVertices(VertexMode vmode, int vertexCount,
                              const SkPoint vertices[], const SkPoint texs[],
                              const SkColor colors[], SkXfermode* xmode,
                              const uint16_t indices[], int indexCount,
                              const SkPaint& paint) {
    }

    CommonCheck& mBounder;
};

void CopyPaste::buildSelection(const SkPicture& picture, const SkIRect& area,
        const SkIRect& selStart, const SkIRect& selEnd, SkRegion* region) {
    DBG_NAV_LOGD("area=(%d, %d, %d, %d) selStart=(%d, %d, %d, %d)"
        " selEnd=(%d, %d, %d, %d)", 
        area.fLeft, area.fTop, area.fRight, area.fBottom,
        selStart.fLeft, selStart.fTop, selStart.fRight, selStart.fBottom,
        selEnd.fLeft, selEnd.fTop, selEnd.fRight, selEnd.fBottom);
    MultilineBuilder builder(selStart, selEnd, area.fLeft, area.fTop, region);
    TextCanvas checker(&builder, picture, area);
    checker.drawPicture(const_cast<SkPicture&>(picture));
    region->translate(area.fLeft, area.fTop);
}

SkIRect CopyPaste::findClosest(const SkPicture& picture, const SkIRect& area,
        int x, int y) {
    FirstCheck _check(x - area.fLeft, y - area.fTop);
    DBG_NAV_LOGD("area=(%d, %d, %d, %d) x=%d y=%d", area.fLeft, area.fTop,
        area.fRight, area.fBottom, x, y);
    TextCanvas checker(&_check, picture, area);
    checker.drawPicture(const_cast<SkPicture&>(picture));
    _check.offsetBounds(area.fLeft, area.fTop);
    return _check.bestBounds();
}

WebCore::String CopyPaste::text(const SkPicture& picture, const SkIRect& area,
        const SkRegion& region) {
    SkRegion copy = region;
    copy.translate(-area.fLeft, -area.fTop);
    const SkIRect& bounds = copy.getBounds();
    DBG_NAV_LOGD("area=(%d, %d, %d, %d) region=(%d, %d, %d, %d)",
        area.fLeft, area.fTop, area.fRight, area.fBottom,
        bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom);
    TextExtractor extractor(copy);
    TextCanvas checker(&extractor, picture, area);
    checker.drawPicture(const_cast<SkPicture&>(picture));
    return extractor.text();
}

void SelectText::draw(SkCanvas* canvas, LayerAndroid* layer)
{
    if (layer->picture() != m_picture)
        return;
    if (m_drawRegion)
        drawSelectionRegion(canvas);
    if (m_drawPointer)
        drawSelectionPointer(canvas);
}

void SelectText::drawSelectionPointer(SkCanvas* canvas)
{
    SkPath path;
    if (m_extendSelection)
        getSelectionCaret(&path);
    else
        getSelectionArrow(&path);
    SkPaint paint;
    paint.setAntiAlias(true);
    paint.setStyle(SkPaint::kStroke_Style);
    paint.setColor(SK_ColorBLACK);
    SkPixelXorXfermode xorMode(SK_ColorWHITE);
    if (m_extendSelection)
        paint.setXfermode(&xorMode);
    else
        paint.setStrokeWidth(SK_Scalar1 * 2);
    int sc = canvas->save();
    canvas->scale(m_inverseScale, m_inverseScale);
    canvas->translate(SkIntToScalar(m_selectX), SkIntToScalar(m_selectY));
    canvas->drawPath(path, paint);
    if (!m_extendSelection) {
        paint.setStyle(SkPaint::kFill_Style);
        paint.setColor(SK_ColorWHITE);
        canvas->drawPath(path, paint);
    }
    canvas->restoreToCount(sc);
}

void SelectText::drawSelectionRegion(SkCanvas* canvas)
{
    m_selRegion.setEmpty();
    SkRect visBounds;
    if (!canvas->getClipBounds(&visBounds, SkCanvas::kAA_EdgeType))
        return;
    SkIRect ivisBounds;
    visBounds.round(&ivisBounds);
    CopyPaste::buildSelection(*m_picture, ivisBounds, m_selStart, m_selEnd,
        &m_selRegion);
    SkPath path;
    m_selRegion.getBoundaryPath(&path);
    SkPaint paint;
    paint.setAntiAlias(true);
    paint.setColor(SkColorSetARGB(0x40, 255, 51, 204));
    canvas->drawPath(path, paint);
}

const String SelectText::getSelection()
{
    String result = CopyPaste::text(*m_picture, m_visibleRect, m_selRegion);
    DBG_NAV_LOGD("text=%s", result.latin1().data()); // uses CString
    return result;
}

void SelectText::getSelectionArrow(SkPath* path)
{
    const int arrow[] = {
        0, 14, 3, 11, 5, 15, 9, 15, 7, 11, 11, 11
    };
    for (unsigned index = 0; index < sizeof(arrow)/sizeof(arrow[0]); index += 2)
        path->lineTo(SkIntToScalar(arrow[index]), SkIntToScalar(arrow[index + 1]));
    path->close();
}

void SelectText::getSelectionCaret(SkPath* path)
{
    SkScalar height = SkIntToScalar(m_selStart.fBottom - m_selStart.fTop);
    SkScalar dist = height / 4;
    path->moveTo(0, -height / 2);
    path->rLineTo(0, height);
    path->rLineTo(-dist, dist);
    path->rMoveTo(0, -SK_Scalar1/2);
    path->rLineTo(dist * 2, 0);
    path->rMoveTo(0, SK_Scalar1/2);
    path->rLineTo(-dist, -dist);
}

void SelectText::moveSelection(const SkPicture* picture, int x, int y,
    bool extendSelection)
{
    if (!extendSelection)
        m_picture = picture;
    m_selEnd = CopyPaste::findClosest(*picture, m_visibleRect, x, y);
    if (!extendSelection)
        m_selStart = m_selEnd;
    DBG_NAV_LOGD("x=%d y=%d extendSelection=%s m_selStart=(%d, %d, %d, %d)"
        " m_selEnd=(%d, %d, %d, %d)", x, y, extendSelection ? "true" : "false",
        m_selStart.fLeft, m_selStart.fTop, m_selStart.fRight, m_selStart.fBottom,
        m_selEnd.fLeft, m_selEnd.fTop, m_selEnd.fRight, m_selEnd.fBottom);
}

}