KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_svg_plotter.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <page_info.h>
24
25#include <wx/ffile.h>
26#include <wx/filename.h>
27
28#include <set>
29#include <string>
30#include <vector>
31
32namespace
33{
34
36wxString plotFilledShape( const COLOR4D& aColor )
37{
38 wxFileName tempFile( wxFileName::CreateTempFileName( wxT( "kicad_svg_plotter" ) ) );
39 tempFile.SetExt( wxT( "svg" ) );
40
41 SVG_PLOTTER plotter;
42 PAGE_INFO pageInfo;
43
44 pageInfo.SetWidthMils( 1000 );
45 pageInfo.SetHeightMils( 1000 );
46 plotter.SetPageSettings( pageInfo );
47 plotter.SetViewport( VECTOR2I( 0, 0 ), 1, 1.0, false );
48 plotter.SetColorMode( true );
49
50 BOOST_REQUIRE( plotter.OpenFile( tempFile.GetFullPath() ) );
51 BOOST_REQUIRE( plotter.StartPlot( wxT( "1" ) ) );
52
53 plotter.SetColor( aColor );
54 plotter.Rect( VECTOR2I( 100, 100 ), VECTOR2I( 500, 500 ), FILL_T::FILLED_SHAPE, 10 );
55
56 BOOST_REQUIRE( plotter.EndPlot() );
57
58 wxFFile file( tempFile.GetFullPath(), wxT( "r" ) );
59 BOOST_REQUIRE( file.IsOpened() );
60
61 wxString content;
62 file.ReadAll( &content );
63 file.Close();
64
65 wxRemoveFile( tempFile.GetFullPath() );
66
67 return content;
68}
69
70
72std::set<std::string> fillOpacities( const wxString& aSvg )
73{
74 const std::string doc = aSvg.ToStdString();
75 const std::string key = "fill-opacity:";
76 std::set<std::string> values;
77
78 for( size_t pos = doc.find( key ); pos != std::string::npos; pos = doc.find( key, pos + 1 ) )
79 {
80 size_t start = pos + key.size();
81 size_t end = doc.find_first_not_of( "0123456789.", start );
82
83 values.insert( doc.substr( start, end - start ) );
84 }
85
86 return values;
87}
88
89} // anonymous namespace
90
91
92BOOST_AUTO_TEST_SUITE( SvgPlotter )
93
94
95BOOST_AUTO_TEST_CASE( FillOpacityMatchesColorAlpha )
96{
97 std::set<std::string> values = fillOpacities( plotFilledShape( COLOR4D( 1.0, 0.0, 0.0, 0.5 ) ) );
98
99 // The plotter writes m_precision (4) digits, so a half-transparent brush is exactly 0.5000
100 BOOST_CHECK_EQUAL( values.count( "0.5000" ), 1 );
101 BOOST_CHECK_EQUAL( values.count( "0.0000" ), 0 );
102}
103
104
105BOOST_AUTO_TEST_CASE( FillOpacityFullyOpaque )
106{
107 std::set<std::string> values = fillOpacities( plotFilledShape( COLOR4D( 0.0, 0.0, 1.0, 1.0 ) ) );
108
109 BOOST_REQUIRE_EQUAL( values.count( "1.0000" ), 1 );
110
111 // Nothing may be drawn semi-transparently when the brush is opaque
112 for( const std::string& value : values )
113 BOOST_CHECK_EQUAL( value, "1.0000" );
114}
115
116
117BOOST_AUTO_TEST_CASE( FillOpacityPreservedThroughSetColor )
118{
119 const std::vector<std::pair<double, std::string>> cases = {
120 { 0.0, "0.0000" }, { 0.25, "0.2500" }, { 0.5, "0.5000" }, { 0.75, "0.7500" },
121 { 1.0, "1.0000" }
122 };
123
124 for( const auto& [alpha, expected] : cases )
125 {
126 BOOST_TEST_CONTEXT( "alpha " << alpha )
127 {
128 std::set<std::string> values =
129 fillOpacities( plotFilledShape( COLOR4D( 0.5, 0.5, 0.5, alpha ) ) );
130
131 BOOST_CHECK_EQUAL( values.count( expected ), 1 );
132
133 // No other requested alpha may appear, or SetColor is not reaching the output
134 for( const auto& [otherAlpha, other] : cases )
135 {
136 if( other != expected && other != "1.0000" )
137 BOOST_CHECK_EQUAL( values.count( other ), 0 );
138 }
139 }
140 }
141}
142
143
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition page_info.h:75
void SetHeightMils(double aHeightInMils)
void SetWidthMils(double aWidthInMils)
virtual bool OpenFile(const wxString &aFullFilename)
Open or create the plot file aFullFilename.
Definition plotter.cpp:73
virtual void SetPageSettings(const PAGE_INFO &aPageSettings)
Definition plotter.h:166
virtual void SetColorMode(bool aColorMode)
Plot in B/W or color.
Definition plotter.h:160
virtual void SetColor(const COLOR4D &color) override
The SetColor implementation is split with the subclasses: the PSLIKE computes the rgb values,...
virtual bool StartPlot(const wxString &aPageNumber) override
Create SVG file header.
virtual void SetViewport(const VECTOR2I &aOffset, double aIusPerDecimil, double aScale, bool aMirror) override
Set the plot offset and scaling for the current plot.
virtual void Rect(const VECTOR2I &p1, const VECTOR2I &p2, FILL_T fill, int width, int aCornerRadius=0) override
virtual bool EndPlot() override
@ FILLED_SHAPE
Fill with object color.
Definition eda_shape.h:61
Plotting engines similar to ps (PostScript, Gerber, svg)
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
VECTOR3I expected(15, 30, 45)
VECTOR2I end
BOOST_TEST_CONTEXT("Test Clearance")
BOOST_AUTO_TEST_CASE(FillOpacityMatchesColorAlpha)
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683