Home | History | Annotate | Download | only in c
      1 /*
      2  * Copyright 2015 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #include "SkPaint.h"
      9 
     10 #include "sk_paint.h"
     11 #include "sk_types_priv.h"
     12 
     13 #define MAKE_FROM_TO_NAME(FROM)     g_ ## FROM ## _map
     14 
     15 const struct {
     16     sk_stroke_cap_t fC;
     17     SkPaint::Cap    fSK;
     18 } MAKE_FROM_TO_NAME(sk_stroke_cap_t)[] = {
     19     { BUTT_SK_STROKE_CAP,   SkPaint::kButt_Cap   },
     20     { ROUND_SK_STROKE_CAP,  SkPaint::kRound_Cap  },
     21     { SQUARE_SK_STROKE_CAP, SkPaint::kSquare_Cap },
     22 };
     23 
     24 const struct {
     25     sk_stroke_join_t fC;
     26     SkPaint::Join    fSK;
     27 } MAKE_FROM_TO_NAME(sk_stroke_join_t)[] = {
     28     { MITER_SK_STROKE_JOIN, SkPaint::kMiter_Join },
     29     { ROUND_SK_STROKE_JOIN, SkPaint::kRound_Join },
     30     { BEVEL_SK_STROKE_JOIN, SkPaint::kBevel_Join },
     31 };
     32 
     33 #define CType           sk_stroke_cap_t
     34 #define SKType          SkPaint::Cap
     35 #define CTypeSkTypeMap  MAKE_FROM_TO_NAME(sk_stroke_cap_t)
     36 #include "sk_c_from_to.h"
     37 
     38 #define CType           sk_stroke_join_t
     39 #define SKType          SkPaint::Join
     40 #define CTypeSkTypeMap  MAKE_FROM_TO_NAME(sk_stroke_join_t)
     41 #include "sk_c_from_to.h"
     42 
     43 //////////////////////////////////////////////////////////////////////////////////////////////////
     44 
     45 sk_paint_t* sk_paint_new() { return (sk_paint_t*)new SkPaint; }
     46 
     47 void sk_paint_delete(sk_paint_t* cpaint) { delete AsPaint(cpaint); }
     48 
     49 bool sk_paint_is_antialias(const sk_paint_t* cpaint) {
     50     return AsPaint(*cpaint).isAntiAlias();
     51 }
     52 
     53 void sk_paint_set_antialias(sk_paint_t* cpaint, bool aa) {
     54     AsPaint(cpaint)->setAntiAlias(aa);
     55 }
     56 
     57 sk_color_t sk_paint_get_color(const sk_paint_t* cpaint) {
     58     return AsPaint(*cpaint).getColor();
     59 }
     60 
     61 void sk_paint_set_color(sk_paint_t* cpaint, sk_color_t c) {
     62     AsPaint(cpaint)->setColor(c);
     63 }
     64 
     65 void sk_paint_set_shader(sk_paint_t* cpaint, sk_shader_t* cshader) {
     66     AsPaint(cpaint)->setShader(AsShader(cshader));
     67 }
     68 
     69 void sk_paint_set_maskfilter(sk_paint_t* cpaint, sk_maskfilter_t* cfilter) {
     70     AsPaint(cpaint)->setMaskFilter(AsMaskFilter(cfilter));
     71 }
     72 
     73 bool sk_paint_is_stroke(const sk_paint_t* cpaint) {
     74     return AsPaint(*cpaint).getStyle() != SkPaint::kFill_Style;
     75 }
     76 
     77 void sk_paint_set_stroke(sk_paint_t* cpaint, bool doStroke) {
     78     AsPaint(cpaint)->setStyle(doStroke ? SkPaint::kStroke_Style : SkPaint::kFill_Style);
     79 }
     80 
     81 float sk_paint_get_stroke_width(const sk_paint_t* cpaint) {
     82     return AsPaint(*cpaint).getStrokeWidth();
     83 }
     84 
     85 void sk_paint_set_stroke_width(sk_paint_t* cpaint, float width) {
     86     AsPaint(cpaint)->setStrokeWidth(width);
     87 }
     88 
     89 float sk_paint_get_stroke_miter(const sk_paint_t* cpaint) {
     90     return AsPaint(*cpaint).getStrokeMiter();
     91 }
     92 
     93 void sk_paint_set_stroke_miter(sk_paint_t* cpaint, float miter) {
     94     AsPaint(cpaint)->setStrokeMiter(miter);
     95 }
     96 
     97 sk_stroke_cap_t sk_paint_get_stroke_cap(const sk_paint_t* cpaint) {
     98     sk_stroke_cap_t ccap;
     99     if (find_c(AsPaint(*cpaint).getStrokeCap(), &ccap)) {
    100         ccap = BUTT_SK_STROKE_CAP;
    101     }
    102     return ccap;
    103 }
    104 
    105 void sk_paint_set_stroke_cap(sk_paint_t* cpaint, sk_stroke_cap_t ccap) {
    106     SkPaint::Cap skcap;
    107     if (find_sk(ccap, &skcap)) {
    108         AsPaint(cpaint)->setStrokeCap(skcap);
    109     } else {
    110         // unknown ccap
    111     }
    112 }
    113 
    114 sk_stroke_join_t sk_paint_get_stroke_join(const sk_paint_t* cpaint) {
    115     sk_stroke_join_t cjoin;
    116     if (find_c(AsPaint(*cpaint).getStrokeJoin(), &cjoin)) {
    117         cjoin = MITER_SK_STROKE_JOIN;
    118     }
    119     return cjoin;
    120 }
    121 
    122 void sk_paint_set_stroke_join(sk_paint_t* cpaint, sk_stroke_join_t cjoin) {
    123     SkPaint::Join skjoin;
    124     if (find_sk(cjoin, &skjoin)) {
    125         AsPaint(cpaint)->setStrokeJoin(skjoin);
    126     } else {
    127         // unknown cjoin
    128     }
    129 }
    130 
    131 void sk_paint_set_xfermode_mode(sk_paint_t* paint, sk_xfermode_mode_t mode) {
    132     SkASSERT(paint);
    133     SkXfermode::Mode skmode;
    134     switch (mode) {
    135         #define MAP(X, Y) case (X): skmode = (Y); break
    136         MAP( CLEAR_SK_XFERMODE_MODE,      SkXfermode::kClear_Mode      );
    137         MAP( SRC_SK_XFERMODE_MODE,        SkXfermode::kSrc_Mode        );
    138         MAP( DST_SK_XFERMODE_MODE,        SkXfermode::kDst_Mode        );
    139         MAP( SRCOVER_SK_XFERMODE_MODE,    SkXfermode::kSrcOver_Mode    );
    140         MAP( DSTOVER_SK_XFERMODE_MODE,    SkXfermode::kDstOver_Mode    );
    141         MAP( SRCIN_SK_XFERMODE_MODE,      SkXfermode::kSrcIn_Mode      );
    142         MAP( DSTIN_SK_XFERMODE_MODE,      SkXfermode::kDstIn_Mode      );
    143         MAP( SRCOUT_SK_XFERMODE_MODE,     SkXfermode::kSrcOut_Mode     );
    144         MAP( DSTOUT_SK_XFERMODE_MODE,     SkXfermode::kDstOut_Mode     );
    145         MAP( SRCATOP_SK_XFERMODE_MODE,    SkXfermode::kSrcATop_Mode    );
    146         MAP( DSTATOP_SK_XFERMODE_MODE,    SkXfermode::kDstATop_Mode    );
    147         MAP( XOR_SK_XFERMODE_MODE,        SkXfermode::kXor_Mode        );
    148         MAP( PLUS_SK_XFERMODE_MODE,       SkXfermode::kPlus_Mode       );
    149         MAP( MODULATE_SK_XFERMODE_MODE,   SkXfermode::kModulate_Mode   );
    150         MAP( SCREEN_SK_XFERMODE_MODE,     SkXfermode::kScreen_Mode     );
    151         MAP( OVERLAY_SK_XFERMODE_MODE,    SkXfermode::kOverlay_Mode    );
    152         MAP( DARKEN_SK_XFERMODE_MODE,     SkXfermode::kDarken_Mode     );
    153         MAP( LIGHTEN_SK_XFERMODE_MODE,    SkXfermode::kLighten_Mode    );
    154         MAP( COLORDODGE_SK_XFERMODE_MODE, SkXfermode::kColorDodge_Mode );
    155         MAP( COLORBURN_SK_XFERMODE_MODE,  SkXfermode::kColorBurn_Mode  );
    156         MAP( HARDLIGHT_SK_XFERMODE_MODE,  SkXfermode::kHardLight_Mode  );
    157         MAP( SOFTLIGHT_SK_XFERMODE_MODE,  SkXfermode::kSoftLight_Mode  );
    158         MAP( DIFFERENCE_SK_XFERMODE_MODE, SkXfermode::kDifference_Mode );
    159         MAP( EXCLUSION_SK_XFERMODE_MODE,  SkXfermode::kExclusion_Mode  );
    160         MAP( MULTIPLY_SK_XFERMODE_MODE,   SkXfermode::kMultiply_Mode   );
    161         MAP( HUE_SK_XFERMODE_MODE,        SkXfermode::kHue_Mode        );
    162         MAP( SATURATION_SK_XFERMODE_MODE, SkXfermode::kSaturation_Mode );
    163         MAP( COLOR_SK_XFERMODE_MODE,      SkXfermode::kColor_Mode      );
    164         MAP( LUMINOSITY_SK_XFERMODE_MODE, SkXfermode::kLuminosity_Mode );
    165         #undef MAP
    166         default:
    167             return;
    168     }
    169     AsPaint(paint)->setXfermodeMode(skmode);
    170 }
    171