OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "Resources.h" | 8 #include "Resources.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkFixed.h" | 10 #include "SkFixed.h" |
(...skipping 528 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
539 | 539 |
540 // Deserialize picture | 540 // Deserialize picture |
541 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size); | 541 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size); |
542 SkAutoTUnref<SkPicture> readPict( | 542 SkAutoTUnref<SkPicture> readPict( |
543 SkPicture::CreateFromBuffer(reader)); | 543 SkPicture::CreateFromBuffer(reader)); |
544 REPORTER_ASSERT(reporter, readPict.get()); | 544 REPORTER_ASSERT(reporter, readPict.get()); |
545 } | 545 } |
546 | 546 |
547 TestPictureTypefaceSerialization(reporter); | 547 TestPictureTypefaceSerialization(reporter); |
548 } | 548 } |
| 549 |
| 550 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
| 551 #include "SkAnnotation.h" |
| 552 |
| 553 static SkPicture* copy_picture_via_serialization(SkPicture* src) { |
| 554 SkDynamicMemoryWStream wstream; |
| 555 src->serialize(&wstream); |
| 556 SkAutoTDelete<SkStreamAsset> rstream(wstream.detachAsStream()); |
| 557 return SkPicture::CreateFromStream(rstream); |
| 558 } |
| 559 |
| 560 struct AnnotationRec { |
| 561 const SkRect fRect; |
| 562 const char* fKey; |
| 563 SkData* fValue; |
| 564 }; |
| 565 |
| 566 class TestAnnotationCanvas : public SkCanvas { |
| 567 skiatest::Reporter* fReporter; |
| 568 const AnnotationRec* fRec; |
| 569 int fCount; |
| 570 int fCurrIndex; |
| 571 |
| 572 public: |
| 573 TestAnnotationCanvas(skiatest::Reporter* reporter, const AnnotationRec rec[]
, int count) |
| 574 : SkCanvas(100, 100) |
| 575 , fReporter(reporter) |
| 576 , fRec(rec) |
| 577 , fCount(count) |
| 578 , fCurrIndex(0) |
| 579 {} |
| 580 |
| 581 ~TestAnnotationCanvas() { |
| 582 REPORTER_ASSERT(fReporter, fCount == fCurrIndex); |
| 583 } |
| 584 |
| 585 protected: |
| 586 void onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) { |
| 587 REPORTER_ASSERT(fReporter, fCurrIndex < fCount); |
| 588 REPORTER_ASSERT(fReporter, rect == fRec[fCurrIndex].fRect); |
| 589 REPORTER_ASSERT(fReporter, !strcmp(key, fRec[fCurrIndex].fKey)); |
| 590 REPORTER_ASSERT(fReporter, value->equals(fRec[fCurrIndex].fValue)); |
| 591 fCurrIndex += 1; |
| 592 } |
| 593 }; |
| 594 |
| 595 /* |
| 596 * Test the 3 annotation types by recording them into a picture, serializing, a
nd then playing |
| 597 * them back into another canvas. |
| 598 */ |
| 599 DEF_TEST(Annotations, reporter) { |
| 600 SkPictureRecorder recorder; |
| 601 SkCanvas* recordingCanvas = recorder.beginRecording(SkRect::MakeWH(100, 100)
); |
| 602 |
| 603 const char* str0 = "rect-with-url"; |
| 604 const SkRect r0 = SkRect::MakeWH(10, 10); |
| 605 SkAutoTUnref<SkData> d0(SkData::NewWithCString(str0)); |
| 606 SkAnnotateRectWithURL(recordingCanvas, r0, d0); |
| 607 |
| 608 const char* str1 = "named-destination"; |
| 609 const SkRect r1 = SkRect::MakeXYWH(5, 5, 0, 0); // collapsed to a point |
| 610 SkAutoTUnref<SkData> d1(SkData::NewWithCString(str1)); |
| 611 SkAnnotateNamedDestination(recordingCanvas, {r1.x(), r1.y()}, d1); |
| 612 |
| 613 const char* str2 = "link-to-destination"; |
| 614 const SkRect r2 = SkRect::MakeXYWH(20, 20, 5, 6); |
| 615 SkAutoTUnref<SkData> d2(SkData::NewWithCString(str2)); |
| 616 SkAnnotateLinkToDestination(recordingCanvas, r2, d2); |
| 617 |
| 618 const AnnotationRec recs[] = { |
| 619 { r0, SkAnnotationKeys::URL_Key(), d0 }, |
| 620 { r1, SkAnnotationKeys::Define_Named_Dest_Key(), d1 }, |
| 621 { r2, SkAnnotationKeys::Link_Named_Dest_Key(), d2 }, |
| 622 }; |
| 623 |
| 624 SkAutoTUnref<SkPicture> pict0(recorder.endRecording()); |
| 625 SkAutoTUnref<SkPicture> pict1(copy_picture_via_serialization(pict0)); |
| 626 |
| 627 TestAnnotationCanvas canvas(reporter, recs, SK_ARRAY_COUNT(recs)); |
| 628 canvas.drawPicture(pict1); |
| 629 } |
| 630 |
OLD | NEW |