OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkLua.h" | 8 #include "SkLua.h" |
9 | 9 |
10 #if SK_SUPPORT_GPU | 10 #if SK_SUPPORT_GPU |
11 #include "GrReducedClip.h" | 11 #include "GrReducedClip.h" |
12 #endif | 12 #endif |
13 | 13 |
14 #include "SkCanvas.h" | 14 #include "SkCanvas.h" |
15 #include "SkData.h" | 15 #include "SkData.h" |
16 #include "SkDocument.h" | 16 #include "SkDocument.h" |
17 #include "SkImage.h" | 17 #include "SkImage.h" |
18 #include "SkMatrix.h" | 18 #include "SkMatrix.h" |
19 #include "SkPaint.h" | 19 #include "SkPaint.h" |
20 #include "SkPath.h" | 20 #include "SkPath.h" |
| 21 #include "SkPathEffect.h" |
21 #include "SkPixelRef.h" | 22 #include "SkPixelRef.h" |
22 #include "SkRRect.h" | 23 #include "SkRRect.h" |
23 #include "SkString.h" | 24 #include "SkString.h" |
24 #include "SkTypeface.h" | 25 #include "SkTypeface.h" |
25 | 26 |
26 extern "C" { | 27 extern "C" { |
27 #include "lua.h" | 28 #include "lua.h" |
28 #include "lualib.h" | 29 #include "lualib.h" |
29 #include "lauxlib.h" | 30 #include "lauxlib.h" |
30 } | 31 } |
31 | 32 |
32 // return the metatable name for a given class | 33 // return the metatable name for a given class |
33 template <typename T> const char* get_mtname(); | 34 template <typename T> const char* get_mtname(); |
34 #define DEF_MTNAME(T) \ | 35 #define DEF_MTNAME(T) \ |
35 template <> const char* get_mtname<T>() { \ | 36 template <> const char* get_mtname<T>() { \ |
36 return #T "_LuaMetaTableName"; \ | 37 return #T "_LuaMetaTableName"; \ |
37 } | 38 } |
38 | 39 |
39 DEF_MTNAME(SkCanvas) | 40 DEF_MTNAME(SkCanvas) |
40 DEF_MTNAME(SkDocument) | 41 DEF_MTNAME(SkDocument) |
41 DEF_MTNAME(SkImage) | 42 DEF_MTNAME(SkImage) |
42 DEF_MTNAME(SkMatrix) | 43 DEF_MTNAME(SkMatrix) |
43 DEF_MTNAME(SkRRect) | 44 DEF_MTNAME(SkRRect) |
44 DEF_MTNAME(SkPath) | 45 DEF_MTNAME(SkPath) |
45 DEF_MTNAME(SkPaint) | 46 DEF_MTNAME(SkPaint) |
| 47 DEF_MTNAME(SkPathEffect) |
46 DEF_MTNAME(SkShader) | 48 DEF_MTNAME(SkShader) |
47 DEF_MTNAME(SkTypeface) | 49 DEF_MTNAME(SkTypeface) |
48 | 50 |
49 template <typename T> T* push_new(lua_State* L) { | 51 template <typename T> T* push_new(lua_State* L) { |
50 T* addr = (T*)lua_newuserdata(L, sizeof(T)); | 52 T* addr = (T*)lua_newuserdata(L, sizeof(T)); |
51 new (addr) T; | 53 new (addr) T; |
52 luaL_getmetatable(L, get_mtname<T>()); | 54 luaL_getmetatable(L, get_mtname<T>()); |
53 lua_setmetatable(L, -2); | 55 lua_setmetatable(L, -2); |
54 return addr; | 56 return addr; |
55 } | 57 } |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 | 200 |
199 void SkLua::pushArrayU16(const uint16_t array[], int count, const char key[]) { | 201 void SkLua::pushArrayU16(const uint16_t array[], int count, const char key[]) { |
200 lua_newtable(fL); | 202 lua_newtable(fL); |
201 for (int i = 0; i < count; ++i) { | 203 for (int i = 0; i < count; ++i) { |
202 // make it base-1 to match lua convention | 204 // make it base-1 to match lua convention |
203 setarray_number(fL, i + 1, (double)array[i]); | 205 setarray_number(fL, i + 1, (double)array[i]); |
204 } | 206 } |
205 CHECK_SETFIELD(key); | 207 CHECK_SETFIELD(key); |
206 } | 208 } |
207 | 209 |
| 210 void SkLua::pushArrayPoint(const SkPoint array[], int count, const char key[]) { |
| 211 lua_newtable(fL); |
| 212 for (int i = 0; i < count; ++i) { |
| 213 // make it base-1 to match lua convention |
| 214 lua_newtable(fL); |
| 215 this->pushScalar(array[i].fX, "x"); |
| 216 this->pushScalar(array[i].fY, "y"); |
| 217 lua_rawseti(fL, -2, i + 1); |
| 218 } |
| 219 CHECK_SETFIELD(key); |
| 220 } |
| 221 |
208 void SkLua::pushRect(const SkRect& r, const char key[]) { | 222 void SkLua::pushRect(const SkRect& r, const char key[]) { |
209 lua_newtable(fL); | 223 lua_newtable(fL); |
210 setfield_scalar(fL, "left", r.fLeft); | 224 setfield_scalar(fL, "left", r.fLeft); |
211 setfield_scalar(fL, "top", r.fTop); | 225 setfield_scalar(fL, "top", r.fTop); |
212 setfield_scalar(fL, "right", r.fRight); | 226 setfield_scalar(fL, "right", r.fRight); |
213 setfield_scalar(fL, "bottom", r.fBottom); | 227 setfield_scalar(fL, "bottom", r.fBottom); |
214 CHECK_SETFIELD(key); | 228 CHECK_SETFIELD(key); |
215 } | 229 } |
216 | 230 |
217 void SkLua::pushRRect(const SkRRect& rr, const char key[]) { | 231 void SkLua::pushRRect(const SkRRect& rr, const char key[]) { |
(...skipping 620 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
838 static int lpaint_getShader(lua_State* L) { | 852 static int lpaint_getShader(lua_State* L) { |
839 const SkPaint* paint = get_obj<SkPaint>(L, 1); | 853 const SkPaint* paint = get_obj<SkPaint>(L, 1); |
840 SkShader* shader = paint->getShader(); | 854 SkShader* shader = paint->getShader(); |
841 if (shader) { | 855 if (shader) { |
842 push_ref(L, shader); | 856 push_ref(L, shader); |
843 return 1; | 857 return 1; |
844 } | 858 } |
845 return 0; | 859 return 0; |
846 } | 860 } |
847 | 861 |
| 862 static int lpaint_getPathEffect(lua_State* L) { |
| 863 const SkPaint* paint = get_obj<SkPaint>(L, 1); |
| 864 SkPathEffect* pe = paint->getPathEffect(); |
| 865 if (pe) { |
| 866 push_ref(L, pe); |
| 867 return 1; |
| 868 } |
| 869 return 0; |
| 870 } |
| 871 |
848 static int lpaint_gc(lua_State* L) { | 872 static int lpaint_gc(lua_State* L) { |
849 get_obj<SkPaint>(L, 1)->~SkPaint(); | 873 get_obj<SkPaint>(L, 1)->~SkPaint(); |
850 return 0; | 874 return 0; |
851 } | 875 } |
852 | 876 |
853 static const struct luaL_Reg gSkPaint_Methods[] = { | 877 static const struct luaL_Reg gSkPaint_Methods[] = { |
854 { "isAntiAlias", lpaint_isAntiAlias }, | 878 { "isAntiAlias", lpaint_isAntiAlias }, |
855 { "setAntiAlias", lpaint_setAntiAlias }, | 879 { "setAntiAlias", lpaint_setAntiAlias }, |
856 { "isDither", lpaint_isDither }, | 880 { "isDither", lpaint_isDither }, |
857 { "isUnderlineText", lpaint_isUnderlineText }, | 881 { "isUnderlineText", lpaint_isUnderlineText }, |
(...skipping 23 matching lines...) Expand all Loading... |
881 { "getStrokeCap", lpaint_getStrokeCap }, | 905 { "getStrokeCap", lpaint_getStrokeCap }, |
882 { "getStrokeJoin", lpaint_getStrokeJoin }, | 906 { "getStrokeJoin", lpaint_getStrokeJoin }, |
883 { "getTextEncoding", lpaint_getTextEncoding }, | 907 { "getTextEncoding", lpaint_getTextEncoding }, |
884 { "getStrokeWidth", lpaint_getStrokeWidth }, | 908 { "getStrokeWidth", lpaint_getStrokeWidth }, |
885 { "setStrokeWidth", lpaint_setStrokeWidth }, | 909 { "setStrokeWidth", lpaint_setStrokeWidth }, |
886 { "getStrokeMiter", lpaint_getStrokeMiter }, | 910 { "getStrokeMiter", lpaint_getStrokeMiter }, |
887 { "measureText", lpaint_measureText }, | 911 { "measureText", lpaint_measureText }, |
888 { "getFontMetrics", lpaint_getFontMetrics }, | 912 { "getFontMetrics", lpaint_getFontMetrics }, |
889 { "getEffects", lpaint_getEffects }, | 913 { "getEffects", lpaint_getEffects }, |
890 { "getShader", lpaint_getShader }, | 914 { "getShader", lpaint_getShader }, |
| 915 { "getPathEffect", lpaint_getPathEffect }, |
891 { "__gc", lpaint_gc }, | 916 { "__gc", lpaint_gc }, |
892 { NULL, NULL } | 917 { NULL, NULL } |
893 }; | 918 }; |
894 | 919 |
895 /////////////////////////////////////////////////////////////////////////////// | 920 /////////////////////////////////////////////////////////////////////////////// |
896 | 921 |
897 static const char* mode2string(SkShader::TileMode mode) { | 922 static const char* mode2string(SkShader::TileMode mode) { |
898 static const char* gNames[] = { "clamp", "repeat", "mirror" }; | 923 static const char* gNames[] = { "clamp", "repeat", "mirror" }; |
899 SkASSERT((unsigned)mode < SK_ARRAY_COUNT(gNames)); | 924 SkASSERT((unsigned)mode < SK_ARRAY_COUNT(gNames)); |
900 return gNames[mode]; | 925 return gNames[mode]; |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
974 static const struct luaL_Reg gSkShader_Methods[] = { | 999 static const struct luaL_Reg gSkShader_Methods[] = { |
975 { "isOpaque", lshader_isOpaque }, | 1000 { "isOpaque", lshader_isOpaque }, |
976 { "asABitmap", lshader_asABitmap }, | 1001 { "asABitmap", lshader_asABitmap }, |
977 { "asAGradient", lshader_asAGradient }, | 1002 { "asAGradient", lshader_asAGradient }, |
978 { "__gc", lshader_gc }, | 1003 { "__gc", lshader_gc }, |
979 { NULL, NULL } | 1004 { NULL, NULL } |
980 }; | 1005 }; |
981 | 1006 |
982 /////////////////////////////////////////////////////////////////////////////// | 1007 /////////////////////////////////////////////////////////////////////////////// |
983 | 1008 |
| 1009 static int lpatheffect_gc(lua_State* L) { |
| 1010 get_ref<SkPathEffect>(L, 1)->unref(); |
| 1011 return 0; |
| 1012 } |
| 1013 |
| 1014 static const struct luaL_Reg gSkPathEffect_Methods[] = { |
| 1015 { "__gc", lpatheffect_gc }, |
| 1016 { NULL, NULL } |
| 1017 }; |
| 1018 |
| 1019 /////////////////////////////////////////////////////////////////////////////// |
| 1020 |
984 static int lmatrix_getType(lua_State* L) { | 1021 static int lmatrix_getType(lua_State* L) { |
985 SkMatrix::TypeMask mask = get_obj<SkMatrix>(L, 1)->getType(); | 1022 SkMatrix::TypeMask mask = get_obj<SkMatrix>(L, 1)->getType(); |
986 | 1023 |
987 lua_newtable(L); | 1024 lua_newtable(L); |
988 setfield_boolean(L, "translate", SkToBool(mask & SkMatrix::kTranslate_Mask
)); | 1025 setfield_boolean(L, "translate", SkToBool(mask & SkMatrix::kTranslate_Mask
)); |
989 setfield_boolean(L, "scale", SkToBool(mask & SkMatrix::kScale_Mask)); | 1026 setfield_boolean(L, "scale", SkToBool(mask & SkMatrix::kScale_Mask)); |
990 setfield_boolean(L, "affine", SkToBool(mask & SkMatrix::kAffine_Mask)); | 1027 setfield_boolean(L, "affine", SkToBool(mask & SkMatrix::kAffine_Mask)); |
991 setfield_boolean(L, "perspective", SkToBool(mask & SkMatrix::kPerspective_Ma
sk)); | 1028 setfield_boolean(L, "perspective", SkToBool(mask & SkMatrix::kPerspective_Ma
sk)); |
992 return 1; | 1029 return 1; |
993 } | 1030 } |
(...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1412 lua_setfield(L, -2, "__index"); \ | 1449 lua_setfield(L, -2, "__index"); \ |
1413 luaL_setfuncs(L, g##C##_Methods, 0); \ | 1450 luaL_setfuncs(L, g##C##_Methods, 0); \ |
1414 lua_pop(L, 1); /* pop off the meta-table */ \ | 1451 lua_pop(L, 1); /* pop off the meta-table */ \ |
1415 } while (0) | 1452 } while (0) |
1416 | 1453 |
1417 void SkLua::Load(lua_State* L) { | 1454 void SkLua::Load(lua_State* L) { |
1418 register_Sk(L); | 1455 register_Sk(L); |
1419 REG_CLASS(L, SkCanvas); | 1456 REG_CLASS(L, SkCanvas); |
1420 REG_CLASS(L, SkDocument); | 1457 REG_CLASS(L, SkDocument); |
1421 REG_CLASS(L, SkImage); | 1458 REG_CLASS(L, SkImage); |
| 1459 REG_CLASS(L, SkPaint); |
1422 REG_CLASS(L, SkPath); | 1460 REG_CLASS(L, SkPath); |
1423 REG_CLASS(L, SkPaint); | 1461 REG_CLASS(L, SkPathEffect); |
1424 REG_CLASS(L, SkRRect); | 1462 REG_CLASS(L, SkRRect); |
1425 REG_CLASS(L, SkShader); | 1463 REG_CLASS(L, SkShader); |
1426 REG_CLASS(L, SkTypeface); | 1464 REG_CLASS(L, SkTypeface); |
1427 REG_CLASS(L, SkMatrix); | 1465 REG_CLASS(L, SkMatrix); |
1428 } | 1466 } |
1429 | 1467 |
1430 extern "C" int luaopen_skia(lua_State* L); | 1468 extern "C" int luaopen_skia(lua_State* L); |
1431 extern "C" int luaopen_skia(lua_State* L) { | 1469 extern "C" int luaopen_skia(lua_State* L) { |
1432 SkLua::Load(L); | 1470 SkLua::Load(L); |
1433 return 0; | 1471 return 0; |
1434 } | 1472 } |
OLD | NEW |