Reviewed by Hyatt.
[WebKit-https.git] / WebCore / kcanvas / device / quartz / KCanvasItemQuartz.mm
1 /*
2  * Copyright (C) 2005 Apple Computer, Inc.  All rights reserved.
3  *           (C) 2006 Alexander Kellett <lypanov@kde.org>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
25  */
26
27 #include "config.h"
28 #if SVG_SUPPORT
29 #import "KCanvasItemQuartz.h"
30
31 #import <kxmlcore/Assertions.h>
32
33 #import "kcanvas/RenderPath.h"
34 #import "KCanvasRenderingStyle.h"
35 #import "KRenderingFillPainter.h"
36 #import "KRenderingStrokePainter.h"
37 #import "KCanvasMatrix.h"
38
39 #import "KCanvasPathQuartz.h"
40 #import "KRenderingDeviceQuartz.h"
41 #import "KCanvasFilterQuartz.h"
42 #import "KCanvasResourcesQuartz.h"
43 #import "KCanvasMaskerQuartz.h"
44 #import "QuartzSupport.h"
45
46 #import "SVGRenderStyle.h"
47 #import "SVGStyledElement.h"
48 #import "KCanvasRenderingStyle.h"
49
50
51 namespace WebCore {
52
53 KCanvasItemQuartz::KCanvasItemQuartz(RenderStyle *style, SVGStyledElement *node) : RenderPath(style, node)
54 {
55 }
56
57 typedef enum {
58     Start,
59     Mid,
60     End
61 } MarkerType;
62
63 struct MarkerData {
64     CGPoint origin;
65     double strokeWidth;
66     CGPoint inslopePoints[2];
67     CGPoint outslopePoints[2];
68     MarkerType type;
69     KCanvasMarker *marker;
70 };
71
72 struct DrawMarkersData {
73     DrawMarkersData(GraphicsContext*, KCanvasMarker* startMarker, KCanvasMarker* midMarker, double strokeWidth);
74     GraphicsContext* context;
75     int elementIndex;
76     MarkerData previousMarkerData;
77     KCanvasMarker* midMarker;
78 };
79
80 DrawMarkersData::DrawMarkersData(GraphicsContext* c, KCanvasMarker *start, KCanvasMarker *mid, double strokeWidth)
81     : context(c)
82 {
83     elementIndex = 0;
84     midMarker = mid;
85     
86     previousMarkerData.origin = CGPointZero;
87     previousMarkerData.strokeWidth = strokeWidth;
88     previousMarkerData.marker = start;
89     previousMarkerData.type = Start;
90 }
91
92 static void drawMarkerWithData(GraphicsContext* context, MarkerData &data)
93 {
94     if (!data.marker)
95         return;
96     
97     CGPoint inslopeChange = CGPointSubtractPoints(data.inslopePoints[1], data.inslopePoints[0]);
98     CGPoint outslopeChange = CGPointSubtractPoints(data.outslopePoints[1], data.outslopePoints[0]);
99     
100     static const double deg2rad = M_PI/180.0;
101     double inslope = atan2(inslopeChange.y, inslopeChange.x) / deg2rad;
102     double outslope = atan2(outslopeChange.y, outslopeChange.x) / deg2rad;
103     
104     double angle;
105     if (data.type == Start)
106         angle = outslope;
107     else if (data.type == Mid)
108         angle = (inslope + outslope) / 2;
109     else // (data.type == End)
110         angle = inslope;
111     
112     data.marker->draw(context, FloatRect(), data.origin.x, data.origin.y, data.strokeWidth, angle);
113 }
114
115 static inline void updateMarkerDataForElement(MarkerData &previousMarkerData, const CGPathElement *element)
116 {
117     CGPoint *points = element->points;
118     
119     switch (element->type) {
120     case kCGPathElementAddQuadCurveToPoint:
121         // TODO
122         previousMarkerData.origin = points[1];
123         break;
124     case kCGPathElementAddCurveToPoint:
125         previousMarkerData.inslopePoints[0] = points[1];
126         previousMarkerData.inslopePoints[1] = points[2];
127         previousMarkerData.origin = points[2];
128         break;
129     default:
130         previousMarkerData.inslopePoints[0] = previousMarkerData.origin;
131         previousMarkerData.inslopePoints[1] = points[0];
132         previousMarkerData.origin = points[0];
133         break;
134     }
135 }
136
137 static void drawStartAndMidMarkers(void *info, const CGPathElement *element)
138 {
139     DrawMarkersData &data = *(DrawMarkersData *)info;
140
141     int elementIndex = data.elementIndex;
142     MarkerData &previousMarkerData = data.previousMarkerData;
143
144     CGPoint *points = element->points;
145
146     // First update the outslope for the previous element
147     previousMarkerData.outslopePoints[0] = previousMarkerData.origin;
148     previousMarkerData.outslopePoints[1] = points[0];
149
150     // Draw the marker for the previous element
151     if (elementIndex != 0)
152         drawMarkerWithData(data.context, previousMarkerData);
153
154     // Update our marker data for this element
155     updateMarkerDataForElement(previousMarkerData, element);
156
157     if (elementIndex == 1) {
158         // After drawing the start marker, switch to drawing mid markers
159         previousMarkerData.marker = data.midMarker;
160         previousMarkerData.type = Mid;
161     }
162
163     data.elementIndex++;
164 }
165
166 void KCanvasItemQuartz::drawMarkersIfNeeded(GraphicsContext* context, const FloatRect& rect, const KCanvasPath *path) const
167 {
168     Document *doc = document();
169     const SVGRenderStyle *svgStyle = style()->svgStyle();
170
171     KCanvasMarker *startMarker = getMarkerById(doc, svgStyle->startMarker().mid(1));
172     KCanvasMarker *midMarker = getMarkerById(doc, svgStyle->midMarker().mid(1));
173     KCanvasMarker *endMarker = getMarkerById(doc, svgStyle->endMarker().mid(1));
174     
175     if (!startMarker && !midMarker && !endMarker)
176         return;
177
178     double strokeWidth = KSVGPainterFactory::cssPrimitiveToLength(this, style()->svgStyle()->strokeWidth(), 1.0);
179
180     DrawMarkersData data(context, startMarker, midMarker, strokeWidth);
181
182     CGPathRef cgPath = static_cast<const KCanvasPathQuartz*>(path)->cgPath();
183     CGPathApply(cgPath, &data, drawStartAndMidMarkers);
184
185     data.previousMarkerData.marker = endMarker;
186     data.previousMarkerData.type = End;
187     drawMarkerWithData(context, data.previousMarkerData);
188 }
189
190 }
191
192 #endif // SVG_SUPPORT