Home | History | Annotate | Download | only in cairo
      1 /*
      2  * Copyright (C) 2010 Igalia S.L.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #include "config.h"
     27 #include "CairoUtilities.h"
     28 
     29 #include "AffineTransform.h"
     30 #include "Color.h"
     31 #include "FloatPoint.h"
     32 #include "FloatRect.h"
     33 #include "IntRect.h"
     34 #include "OwnPtrCairo.h"
     35 #include "Path.h"
     36 #include "PlatformPathCairo.h"
     37 #include "RefPtrCairo.h"
     38 #include <wtf/Vector.h>
     39 
     40 namespace WebCore {
     41 
     42 void copyContextProperties(cairo_t* srcCr, cairo_t* dstCr)
     43 {
     44     cairo_set_antialias(dstCr, cairo_get_antialias(srcCr));
     45 
     46     size_t dashCount = cairo_get_dash_count(srcCr);
     47     Vector<double> dashes(dashCount);
     48 
     49     double offset;
     50     cairo_get_dash(srcCr, dashes.data(), &offset);
     51     cairo_set_dash(dstCr, dashes.data(), dashCount, offset);
     52     cairo_set_line_cap(dstCr, cairo_get_line_cap(srcCr));
     53     cairo_set_line_join(dstCr, cairo_get_line_join(srcCr));
     54     cairo_set_line_width(dstCr, cairo_get_line_width(srcCr));
     55     cairo_set_miter_limit(dstCr, cairo_get_miter_limit(srcCr));
     56     cairo_set_fill_rule(dstCr, cairo_get_fill_rule(srcCr));
     57 }
     58 
     59 void setSourceRGBAFromColor(cairo_t* context, const Color& color)
     60 {
     61     float red, green, blue, alpha;
     62     color.getRGBA(red, green, blue, alpha);
     63     cairo_set_source_rgba(context, red, green, blue, alpha);
     64 }
     65 
     66 void appendPathToCairoContext(cairo_t* to, cairo_t* from)
     67 {
     68     OwnPtr<cairo_path_t> cairoPath(cairo_copy_path(from));
     69     cairo_append_path(to, cairoPath.get());
     70 }
     71 
     72 void setPathOnCairoContext(cairo_t* to, cairo_t* from)
     73 {
     74     cairo_new_path(to);
     75     appendPathToCairoContext(to, from);
     76 }
     77 
     78 void appendWebCorePathToCairoContext(cairo_t* context, const Path& path)
     79 {
     80     appendPathToCairoContext(context, path.platformPath()->context());
     81 }
     82 
     83 cairo_operator_t toCairoOperator(CompositeOperator op)
     84 {
     85     switch (op) {
     86     case CompositeClear:
     87         return CAIRO_OPERATOR_CLEAR;
     88     case CompositeCopy:
     89         return CAIRO_OPERATOR_SOURCE;
     90     case CompositeSourceOver:
     91         return CAIRO_OPERATOR_OVER;
     92     case CompositeSourceIn:
     93         return CAIRO_OPERATOR_IN;
     94     case CompositeSourceOut:
     95         return CAIRO_OPERATOR_OUT;
     96     case CompositeSourceAtop:
     97         return CAIRO_OPERATOR_ATOP;
     98     case CompositeDestinationOver:
     99         return CAIRO_OPERATOR_DEST_OVER;
    100     case CompositeDestinationIn:
    101         return CAIRO_OPERATOR_DEST_IN;
    102     case CompositeDestinationOut:
    103         return CAIRO_OPERATOR_DEST_OUT;
    104     case CompositeDestinationAtop:
    105         return CAIRO_OPERATOR_DEST_ATOP;
    106     case CompositeXOR:
    107         return CAIRO_OPERATOR_XOR;
    108     case CompositePlusDarker:
    109 #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 10, 0)
    110         return CAIRO_OPERATOR_DARKEN;
    111 #else
    112         return CAIRO_OPERATOR_SATURATE;
    113 #endif
    114     case CompositeHighlight:
    115         // There is no Cairo equivalent for CompositeHighlight.
    116         return CAIRO_OPERATOR_OVER;
    117     case CompositePlusLighter:
    118         return CAIRO_OPERATOR_ADD;
    119     default:
    120         return CAIRO_OPERATOR_SOURCE;
    121     }
    122 }
    123 
    124 void drawPatternToCairoContext(cairo_t* cr, cairo_surface_t* image, const IntSize& imageSize, const FloatRect& tileRect,
    125                                const AffineTransform& patternTransform, const FloatPoint& phase, cairo_operator_t op, const FloatRect& destRect)
    126 {
    127     // Avoid NaN
    128     if (!isfinite(phase.x()) || !isfinite(phase.y()))
    129        return;
    130 
    131     cairo_save(cr);
    132 
    133     RefPtr<cairo_surface_t> clippedImageSurface = 0;
    134     if (tileRect.size() != imageSize) {
    135         IntRect imageRect = enclosingIntRect(tileRect);
    136         clippedImageSurface = adoptRef(cairo_image_surface_create(CAIRO_FORMAT_ARGB32, imageRect.width(), imageRect.height()));
    137         RefPtr<cairo_t> clippedImageContext = adoptRef(cairo_create(clippedImageSurface.get()));
    138         cairo_set_source_surface(clippedImageContext.get(), image, -tileRect.x(), -tileRect.y());
    139         cairo_paint(clippedImageContext.get());
    140         image = clippedImageSurface.get();
    141     }
    142 
    143     cairo_pattern_t* pattern = cairo_pattern_create_for_surface(image);
    144     cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
    145 
    146     cairo_matrix_t patternMatrix = cairo_matrix_t(patternTransform);
    147     cairo_matrix_t phaseMatrix = {1, 0, 0, 1, phase.x() + tileRect.x() * patternTransform.a(), phase.y() + tileRect.y() * patternTransform.d()};
    148     cairo_matrix_t combined;
    149     cairo_matrix_multiply(&combined, &patternMatrix, &phaseMatrix);
    150     cairo_matrix_invert(&combined);
    151     cairo_pattern_set_matrix(pattern, &combined);
    152 
    153     cairo_set_operator(cr, op);
    154     cairo_set_source(cr, pattern);
    155     cairo_pattern_destroy(pattern);
    156     cairo_rectangle(cr, destRect.x(), destRect.y(), destRect.width(), destRect.height());
    157     cairo_fill(cr);
    158 
    159     cairo_restore(cr);
    160 }
    161 
    162 } // namespace WebCore
    163