webengine/osswebengine/WebCore/platform/graphics/svg/cg/CgSupport.cpp
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2  * Copyright (C) 2005 Apple Computer, Inc.  All rights reserved.
       
     3  *               2006 Rob Buis <buis@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 
       
    28 #include "config.h"
       
    29 #if ENABLE(SVG)
       
    30 #include <ApplicationServices/ApplicationServices.h>
       
    31 #include "CgSupport.h"
       
    32 
       
    33 #include "FloatConversion.h"
       
    34 #include "GraphicsContext.h"
       
    35 #include "KCanvasRenderingStyle.h"
       
    36 #include "RenderStyle.h"
       
    37 #include "SVGRenderStyle.h"
       
    38 #include <wtf/Assertions.h>
       
    39 
       
    40 namespace WebCore {
       
    41 
       
    42 CFStringRef CFStringFromCGAffineTransform(CGAffineTransform t)
       
    43 {
       
    44     return CFStringCreateWithFormat(0, 0, CFSTR("a: %f b: %f c: %f d: %f tx: %f ty: %f"), t.a, t.b, t.c, t.d, t.tx, t.ty);
       
    45 }
       
    46 
       
    47 CGAffineTransform CGAffineTransformMakeMapBetweenRects(CGRect source, CGRect dest)
       
    48 {
       
    49     CGAffineTransform transform = CGAffineTransformMakeTranslation(dest.origin.x - source.origin.x, dest.origin.y - source.origin.y);
       
    50     transform = CGAffineTransformScale(transform, dest.size.width/source.size.width, dest.size.height/source.size.height);
       
    51     return transform;
       
    52 }
       
    53 
       
    54 void applyStrokeStyleToContext(CGContextRef context, RenderStyle* style, const RenderObject* object)
       
    55 {
       
    56     /* Shouldn't all these be in the stroke painter? */
       
    57     CGContextSetLineWidth(context, narrowPrecisionToCGFloat(KSVGPainterFactory::cssPrimitiveToLength(object, style->svgStyle()->strokeWidth(), 1.0)));
       
    58 
       
    59     CGContextSetLineCap(context, CGLineCapFromKC(style->svgStyle()->capStyle()));
       
    60     CGContextSetLineJoin(context, CGLineJoinFromKC(style->svgStyle()->joinStyle()));
       
    61 
       
    62     CGContextSetMiterLimit(context, style->svgStyle()->strokeMiterLimit());
       
    63 
       
    64     const KCDashArray& dashes = KSVGPainterFactory::dashArrayFromRenderingStyle(style);
       
    65     double dashOffset = KSVGPainterFactory::cssPrimitiveToLength(object, style->svgStyle()->strokeDashOffset(), 0.0);
       
    66 
       
    67     CGContextSetLineDash(context, narrowPrecisionToCGFloat(dashOffset), dashes.data(), dashes.size());
       
    68 }
       
    69 
       
    70 CGContextRef scratchContext()
       
    71 {
       
    72     static CGContextRef scratch = 0;
       
    73     if (!scratch) {
       
    74         CFMutableDataRef empty = CFDataCreateMutable(NULL, 0);
       
    75         CGDataConsumerRef consumer = CGDataConsumerCreateWithCFData(empty);
       
    76         scratch = CGPDFContextCreate(consumer, NULL, NULL);
       
    77         CGDataConsumerRelease(consumer);
       
    78         CFRelease(empty);
       
    79 
       
    80         CGFloat black[4] = {0, 0, 0, 1};
       
    81         CGContextSetFillColor(scratch, black);
       
    82         CGContextSetStrokeColor(scratch, black);
       
    83      }
       
    84     return scratch;
       
    85 }
       
    86 
       
    87 FloatRect strokeBoundingBox(const Path& path, RenderStyle* style, const RenderObject* object)
       
    88  {
       
    89     // the bbox might grow if the path is stroked.
       
    90     // and CGPathGetBoundingBox doesn't support that, so we'll have
       
    91     // to make an alternative call...
       
    92  
       
    93     // FIXME: since this is mainly used to decide what to repaint,
       
    94     // perhaps it would be sufficien to just outset the fill bbox by
       
    95     // the stroke width - that should be way cheaper and simpler than
       
    96     // what we do here.
       
    97  
       
    98     CGPathRef cgPath = path.platformPath();
       
    99 
       
   100     CGContextRef context = scratchContext();
       
   101     CGContextSaveGState(context);
       
   102 
       
   103     CGContextBeginPath(context);
       
   104     CGContextAddPath(context, cgPath);
       
   105     applyStrokeStyleToContext(context, style, object);
       
   106     CGContextReplacePathWithStrokedPath(context);
       
   107     CGRect box = CGContextGetPathBoundingBox(context);
       
   108         
       
   109     CGContextRestoreGState(context);
       
   110 
       
   111     return FloatRect(box);
       
   112 }
       
   113 
       
   114 }
       
   115 
       
   116 #endif // ENABLE(SVG)
       
   117