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