OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #include "SkPDFShader.h" | 10 #include "SkPDFShader.h" |
11 | 11 |
12 #include "SkData.h" | 12 #include "SkData.h" |
13 #include "SkOncePtr.h" | |
14 #include "SkPDFCanon.h" | 13 #include "SkPDFCanon.h" |
15 #include "SkPDFDevice.h" | 14 #include "SkPDFDevice.h" |
16 #include "SkPDFFormXObject.h" | 15 #include "SkPDFFormXObject.h" |
17 #include "SkPDFGraphicState.h" | 16 #include "SkPDFGraphicState.h" |
18 #include "SkPDFResourceDict.h" | 17 #include "SkPDFResourceDict.h" |
19 #include "SkPDFUtils.h" | 18 #include "SkPDFUtils.h" |
20 #include "SkScalar.h" | 19 #include "SkScalar.h" |
21 #include "SkStream.h" | 20 #include "SkStream.h" |
22 #include "SkTemplates.h" | 21 #include "SkTemplates.h" |
23 #include "SkTypes.h" | |
24 | 22 |
25 static bool inverse_transform_bbox(const SkMatrix& matrix, SkRect* bbox) { | 23 static bool inverse_transform_bbox(const SkMatrix& matrix, SkRect* bbox) { |
26 SkMatrix inverse; | 24 SkMatrix inverse; |
27 if (!matrix.invert(&inverse)) { | 25 if (!matrix.invert(&inverse)) { |
28 return false; | 26 return false; |
29 } | 27 } |
30 inverse.mapRect(bbox); | 28 inverse.mapRect(bbox); |
31 return true; | 29 return true; |
32 } | 30 } |
33 | 31 |
(...skipping 539 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
573 SkPDFUtils::PaintPath(SkPaint::kFill_Style, SkPath::kEvenOdd_FillType, | 571 SkPDFUtils::PaintPath(SkPaint::kFill_Style, SkPath::kEvenOdd_FillType, |
574 &content); | 572 &content); |
575 | 573 |
576 return content.detachAsStream(); | 574 return content.detachAsStream(); |
577 } | 575 } |
578 | 576 |
579 /** | 577 /** |
580 * Creates a ExtGState with the SMask set to the luminosityShader in | 578 * Creates a ExtGState with the SMask set to the luminosityShader in |
581 * luminosity mode. The shader pattern extends to the bbox. | 579 * luminosity mode. The shader pattern extends to the bbox. |
582 */ | 580 */ |
583 static SkPDFObject* create_smask_graphic_state( | 581 static sk_sp<SkPDFObject> create_smask_graphic_state( |
584 SkPDFCanon* canon, SkScalar dpi, const SkPDFShader::State& state) { | 582 SkPDFCanon* canon, SkScalar dpi, const SkPDFShader::State& state) { |
585 SkRect bbox; | 583 SkRect bbox; |
586 bbox.set(state.fBBox); | 584 bbox.set(state.fBBox); |
587 | 585 |
588 SkAutoTDelete<SkPDFShader::State> alphaToLuminosityState( | 586 SkAutoTDelete<SkPDFShader::State> alphaToLuminosityState( |
589 state.CreateAlphaToLuminosityState()); | 587 state.CreateAlphaToLuminosityState()); |
590 sk_sp<SkPDFObject> luminosityShader( | 588 sk_sp<SkPDFObject> luminosityShader( |
591 get_pdf_shader_by_state(canon, dpi, &alphaToLuminosityState)); | 589 get_pdf_shader_by_state(canon, dpi, &alphaToLuminosityState)); |
592 | 590 |
593 SkAutoTDelete<SkStream> alphaStream(create_pattern_fill_content(-1, bbox)); | 591 SkAutoTDelete<SkStream> alphaStream(create_pattern_fill_content(-1, bbox)); |
594 | 592 |
595 auto resources = | 593 auto resources = |
596 get_gradient_resource_dict(luminosityShader.get(), nullptr); | 594 get_gradient_resource_dict(luminosityShader.get(), nullptr); |
597 | 595 |
598 sk_sp<SkPDFFormXObject> alphaMask( | 596 sk_sp<SkPDFFormXObject> alphaMask( |
599 new SkPDFFormXObject(alphaStream.get(), bbox, resources.get())); | 597 new SkPDFFormXObject(alphaStream.get(), bbox, resources.get())); |
600 | 598 |
601 return SkPDFGraphicState::GetSMaskGraphicState( | 599 return SkPDFGraphicState::GetSMaskGraphicState( |
602 alphaMask.get(), false, | 600 alphaMask.get(), false, |
603 SkPDFGraphicState::kLuminosity_SMaskMode); | 601 SkPDFGraphicState::kLuminosity_SMaskMode, canon); |
604 } | 602 } |
605 | 603 |
606 SkPDFAlphaFunctionShader* SkPDFAlphaFunctionShader::Create( | 604 SkPDFAlphaFunctionShader* SkPDFAlphaFunctionShader::Create( |
607 SkPDFCanon* canon, | 605 SkPDFCanon* canon, |
608 SkScalar dpi, | 606 SkScalar dpi, |
609 SkAutoTDelete<SkPDFShader::State>* autoState) { | 607 SkAutoTDelete<SkPDFShader::State>* autoState) { |
610 const SkPDFShader::State& state = **autoState; | 608 const SkPDFShader::State& state = **autoState; |
611 SkRect bbox; | 609 SkRect bbox; |
612 bbox.set(state.fBBox); | 610 bbox.set(state.fBBox); |
613 | 611 |
614 SkAutoTDelete<SkPDFShader::State> opaqueState(state.CreateOpaqueState()); | 612 SkAutoTDelete<SkPDFShader::State> opaqueState(state.CreateOpaqueState()); |
615 | 613 |
616 sk_sp<SkPDFObject> colorShader( | 614 sk_sp<SkPDFObject> colorShader( |
617 get_pdf_shader_by_state(canon, dpi, &opaqueState)); | 615 get_pdf_shader_by_state(canon, dpi, &opaqueState)); |
618 if (!colorShader) { | 616 if (!colorShader) { |
619 return nullptr; | 617 return nullptr; |
620 } | 618 } |
621 | 619 |
622 // Create resource dict with alpha graphics state as G0 and | 620 // Create resource dict with alpha graphics state as G0 and |
623 // pattern shader as P0, then write content stream. | 621 // pattern shader as P0, then write content stream. |
624 sk_sp<SkPDFObject> alphaGs( | 622 auto alphaGs = create_smask_graphic_state(canon, dpi, state); |
625 create_smask_graphic_state(canon, dpi, state)); | |
626 | 623 |
627 SkPDFAlphaFunctionShader* alphaFunctionShader = | 624 SkPDFAlphaFunctionShader* alphaFunctionShader = |
628 new SkPDFAlphaFunctionShader(autoState->detach()); | 625 new SkPDFAlphaFunctionShader(autoState->detach()); |
629 | 626 |
630 auto resourceDict = | 627 auto resourceDict = |
631 get_gradient_resource_dict(colorShader.get(), alphaGs.get()); | 628 get_gradient_resource_dict(colorShader.get(), alphaGs.get()); |
632 | 629 |
633 SkAutoTDelete<SkStream> colorStream( | 630 SkAutoTDelete<SkStream> colorStream( |
634 create_pattern_fill_content(0, bbox)); | 631 create_pattern_fill_content(0, bbox)); |
635 alphaFunctionShader->setData(colorStream.get()); | 632 alphaFunctionShader->setData(colorStream.get()); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
671 zero, one, zero, | 668 zero, one, zero, |
672 -p0/p2, -p1/p2, 1/p2); | 669 -p0/p2, -p1/p2, 1/p2); |
673 | 670 |
674 affine->setAll(sx - p0 * tx / p2, kx - p1 * tx / p2, tx / p2, | 671 affine->setAll(sx - p0 * tx / p2, kx - p1 * tx / p2, tx / p2, |
675 ky - p0 * ty / p2, sy - p1 * ty / p2, ty / p2, | 672 ky - p0 * ty / p2, sy - p1 * ty / p2, ty / p2, |
676 zero, zero, one); | 673 zero, zero, one); |
677 | 674 |
678 return true; | 675 return true; |
679 } | 676 } |
680 | 677 |
681 static SkPDFObject* create_range_object() { | 678 sk_sp<SkPDFArray> SkPDFShader::MakeRangeObject() { |
682 SkPDFArray* range = new SkPDFArray; | 679 auto range = sk_make_sp<SkPDFArray>(); |
683 range->reserve(6); | 680 range->reserve(6); |
684 range->appendInt(0); | 681 range->appendInt(0); |
685 range->appendInt(1); | 682 range->appendInt(1); |
686 range->appendInt(0); | 683 range->appendInt(0); |
687 range->appendInt(1); | 684 range->appendInt(1); |
688 range->appendInt(0); | 685 range->appendInt(0); |
689 range->appendInt(1); | 686 range->appendInt(1); |
690 return range; | 687 return range; |
691 } | 688 } |
692 SK_DECLARE_STATIC_ONCE_PTR(SkPDFObject, rangeObject); | |
693 | 689 |
694 static SkPDFStream* make_ps_function(const SkString& psCode, | 690 static sk_sp<SkPDFStream> make_ps_function(const SkString& psCode, |
695 SkPDFArray* domain) { | 691 SkPDFArray* domain, |
| 692 sk_sp<SkPDFObject> range) { |
696 SkAutoDataUnref funcData( | 693 SkAutoDataUnref funcData( |
697 SkData::NewWithCopy(psCode.c_str(), psCode.size())); | 694 SkData::NewWithCopy(psCode.c_str(), psCode.size())); |
698 SkPDFStream* result = new SkPDFStream(funcData.get()); | 695 auto result = sk_make_sp<SkPDFStream>(funcData.get()); |
699 result->insertInt("FunctionType", 4); | 696 result->insertInt("FunctionType", 4); |
700 result->insertObject("Domain", sk_ref_sp(domain)); | 697 result->insertObject("Domain", sk_ref_sp(domain)); |
701 result->insertObject("Range", sk_ref_sp(rangeObject.get(create_range_object)
)); | 698 result->insertObject("Range", std::move(range)); |
702 return result; | 699 return result; |
703 } | 700 } |
704 | 701 |
705 SkPDFFunctionShader* SkPDFFunctionShader::Create( | 702 SkPDFFunctionShader* SkPDFFunctionShader::Create( |
706 SkPDFCanon* canon, SkAutoTDelete<SkPDFShader::State>* autoState) { | 703 SkPDFCanon* canon, SkAutoTDelete<SkPDFShader::State>* autoState) { |
707 const SkPDFShader::State& state = **autoState; | 704 const SkPDFShader::State& state = **autoState; |
708 | 705 |
709 SkString (*codeFunction)(const SkShader::GradientInfo& info, | 706 SkString (*codeFunction)(const SkShader::GradientInfo& info, |
710 const SkMatrix& perspectiveRemover) = nullptr; | 707 const SkMatrix& perspectiveRemover) = nullptr; |
711 SkPoint transformPoints[2]; | 708 SkPoint transformPoints[2]; |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
797 functionCode = codeFunction(twoPointRadialInfo, perspectiveInverseOnly); | 794 functionCode = codeFunction(twoPointRadialInfo, perspectiveInverseOnly); |
798 } else { | 795 } else { |
799 functionCode = codeFunction(*info, perspectiveInverseOnly); | 796 functionCode = codeFunction(*info, perspectiveInverseOnly); |
800 } | 797 } |
801 | 798 |
802 auto pdfShader = sk_make_sp<SkPDFDict>(); | 799 auto pdfShader = sk_make_sp<SkPDFDict>(); |
803 pdfShader->insertInt("ShadingType", 1); | 800 pdfShader->insertInt("ShadingType", 1); |
804 pdfShader->insertName("ColorSpace", "DeviceRGB"); | 801 pdfShader->insertName("ColorSpace", "DeviceRGB"); |
805 pdfShader->insertObject("Domain", sk_ref_sp(domain.get())); | 802 pdfShader->insertObject("Domain", sk_ref_sp(domain.get())); |
806 | 803 |
807 sk_sp<SkPDFStream> function( | 804 // Call canon->makeRangeObject() instead of |
808 make_ps_function(functionCode, domain.get())); | 805 // SkPDFShader::MakeRangeObject() so that the canon can |
| 806 // deduplicate. |
| 807 auto function = make_ps_function(functionCode, domain.get(), |
| 808 canon->makeRangeObject()); |
809 pdfShader->insertObjRef("Function", std::move(function)); | 809 pdfShader->insertObjRef("Function", std::move(function)); |
810 | 810 |
811 sk_sp<SkPDFFunctionShader> pdfFunctionShader( | 811 sk_sp<SkPDFFunctionShader> pdfFunctionShader( |
812 new SkPDFFunctionShader(autoState->detach())); | 812 new SkPDFFunctionShader(autoState->detach())); |
813 pdfFunctionShader->insertInt("PatternType", 2); | 813 pdfFunctionShader->insertInt("PatternType", 2); |
814 pdfFunctionShader->insertObject("Matrix", | 814 pdfFunctionShader->insertObject("Matrix", |
815 SkPDFUtils::MatrixToArray(finalMatrix)); | 815 SkPDFUtils::MatrixToArray(finalMatrix)); |
816 pdfFunctionShader->insertObject("Shading", std::move(pdfShader)); | 816 pdfFunctionShader->insertObject("Shading", std::move(pdfShader)); |
817 | 817 |
818 canon->addFunctionShader(pdfFunctionShader.get()); | 818 canon->addFunctionShader(pdfFunctionShader.get()); |
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1223 return false; | 1223 return false; |
1224 } | 1224 } |
1225 | 1225 |
1226 void SkPDFShader::State::AllocateGradientInfoStorage() { | 1226 void SkPDFShader::State::AllocateGradientInfoStorage() { |
1227 fColorData.set(sk_malloc_throw( | 1227 fColorData.set(sk_malloc_throw( |
1228 fInfo.fColorCount * (sizeof(SkColor) + sizeof(SkScalar)))); | 1228 fInfo.fColorCount * (sizeof(SkColor) + sizeof(SkScalar)))); |
1229 fInfo.fColors = reinterpret_cast<SkColor*>(fColorData.get()); | 1229 fInfo.fColors = reinterpret_cast<SkColor*>(fColorData.get()); |
1230 fInfo.fColorOffsets = | 1230 fInfo.fColorOffsets = |
1231 reinterpret_cast<SkScalar*>(fInfo.fColors + fInfo.fColorCount); | 1231 reinterpret_cast<SkScalar*>(fInfo.fColors + fInfo.fColorCount); |
1232 } | 1232 } |
OLD | NEW |