add gradients and drawPaint to lua
BUG=skia: TBR= Review URL: https://codereview.chromium.org/667803004
This commit is contained in:
parent
1f66e45391
commit
9fbc3f31c0
@ -96,16 +96,25 @@ function drawSlide(canvas, slide, template)
|
||||
local x = template.margin_x
|
||||
local y = template.margin_y
|
||||
|
||||
local scale = 1.15
|
||||
local scale = 1.25
|
||||
for i = 1, #slide do
|
||||
local node = slide[i]
|
||||
local paint = template[node.indent + 1]
|
||||
local paint = template[node.indent + 1].paint
|
||||
local extra_dy = template[node.indent + 1].extra_dy
|
||||
local fm = paint:getFontMetrics()
|
||||
local x_offset = -fm.ascent * node.indent
|
||||
|
||||
y = y - fm.ascent * scale
|
||||
canvas:drawText(node.text, x + x_offset, y, paint)
|
||||
y = y + fm.descent * scale
|
||||
y = y + fm.descent * scale + extra_dy
|
||||
end
|
||||
end
|
||||
|
||||
function scale_text_delta(template, delta)
|
||||
template = template.slide
|
||||
for i = 1, #template do
|
||||
local paint = template[i].paint
|
||||
paint:setTextSize(paint:getTextSize() + delta)
|
||||
end
|
||||
end
|
||||
|
||||
@ -140,10 +149,9 @@ end
|
||||
|
||||
function sqr(value) return value * value end
|
||||
|
||||
function ease(value)
|
||||
function set_blur(paint, alpha)
|
||||
local sigma = sqr(1 - alpha) * 20
|
||||
-- paint:setImageFilter(Sk.newBlurImageFilter(sigma, sigma))
|
||||
paint:setImageFilter(Sk.newBlurImageFilter(sigma, sigma))
|
||||
paint:setAlpha(alpha)
|
||||
end
|
||||
|
||||
@ -194,24 +202,27 @@ function fade_slide_transition(prev, next, is_forward)
|
||||
end
|
||||
|
||||
--------------------------------------------------------------------------------------
|
||||
function make_tmpl(paint, extra_dy)
|
||||
return { paint = paint, extra_dy = extra_dy }
|
||||
end
|
||||
|
||||
function SkiaPoint_make_template()
|
||||
local title = {
|
||||
margin_x = 30,
|
||||
margin_y = 100,
|
||||
}
|
||||
title[1] = make_paint("Arial", 1, 50, { a=1, r=0, g=0, b=0 })
|
||||
title[1] = make_paint("Arial", 1, 50, { a=1, r=1, g=1, b=1 })
|
||||
title[1]:setTextAlign("center")
|
||||
title[2] = make_paint("Arial", 1, 25, { a=1, r=.3, g=.3, b=.3 })
|
||||
title[2] = make_paint("Arial", 1, 25, { a=1, r=.75, g=.75, b=.75 })
|
||||
title[2]:setTextAlign("center")
|
||||
|
||||
local slide = {
|
||||
margin_x = 20,
|
||||
margin_y = 30,
|
||||
margin_y = 25,
|
||||
}
|
||||
slide[1] = make_paint("Arial", 1, 36, { a=1, r=0, g=0, b=0 })
|
||||
slide[2] = make_paint("Arial", 0, 30, { a=1, r=0, g=0, b=0 })
|
||||
slide[3] = make_paint("Arial", 0, 24, { a=1, r=.2, g=.2, b=.2 })
|
||||
slide[1] = make_tmpl(make_paint("Arial", 1, 36, { a=1, r=1, g=1, b=1 }), 18)
|
||||
slide[2] = make_tmpl(make_paint("Arial", 0, 30, { a=1, r=1, g=1, b=1 }), 0)
|
||||
slide[3] = make_tmpl(make_paint("Arial", 0, 24, { a=1, r=.8, g=.8, b=.8 }), 0)
|
||||
|
||||
return {
|
||||
title = title,
|
||||
@ -359,11 +370,26 @@ function spawn_scale_animation()
|
||||
}
|
||||
end
|
||||
|
||||
local bgPaint = nil
|
||||
|
||||
function draw_bg(canvas)
|
||||
if not bgPaint then
|
||||
bgPaint = Sk.newPaint()
|
||||
local grad = Sk.newLinearGradient( 0, 0, { a=1, r=0, g=0, b=.3 },
|
||||
640, 480, { a=1, r=0, g=0, b=.8 })
|
||||
bgPaint:setShader(grad)
|
||||
end
|
||||
|
||||
canvas:drawPaint(bgPaint)
|
||||
end
|
||||
|
||||
function onDrawContent(canvas, width, height)
|
||||
local matrix = Sk.newMatrix()
|
||||
matrix:setRectToRect(make_rect(0, 0, 640, 480), make_rect(0, 0, width, height), "center")
|
||||
canvas:concat(matrix)
|
||||
|
||||
draw_bg(canvas)
|
||||
|
||||
local drawSlideProc = function(canvas)
|
||||
drawSlide(canvas, gSlides[gSlideIndex], gTemplate)
|
||||
end
|
||||
@ -386,6 +412,8 @@ local keyProcs = {
|
||||
p = prev_slide,
|
||||
r = spawn_rotate_animation,
|
||||
s = spawn_scale_animation,
|
||||
u = function () scale_text_delta(gTemplate, 1) end,
|
||||
d = function () scale_text_delta(gTemplate, -1) end,
|
||||
}
|
||||
|
||||
function onCharHandler(uni)
|
||||
|
@ -18,8 +18,8 @@ extern "C" {
|
||||
#include "lauxlib.h"
|
||||
}
|
||||
|
||||
#define LUA_FILENAME "test.lua"
|
||||
//#define LUA_FILENAME "slides.lua"
|
||||
//#define LUA_FILENAME "test.lua"
|
||||
#define LUA_FILENAME "slides.lua"
|
||||
|
||||
static const char gDrawName[] = "onDrawContent";
|
||||
static const char gClickName[] = "onClickHandler";
|
||||
|
@ -16,6 +16,7 @@
|
||||
#include "SkData.h"
|
||||
#include "SkDecodingImageGenerator.h"
|
||||
#include "SkDocument.h"
|
||||
#include "SkGradientShader.h"
|
||||
#include "SkImage.h"
|
||||
#include "SkMatrix.h"
|
||||
#include "SkPaint.h"
|
||||
@ -71,10 +72,11 @@ template <typename T> void push_obj(lua_State* L, const T& obj) {
|
||||
lua_setmetatable(L, -2);
|
||||
}
|
||||
|
||||
template <typename T> void push_ref(lua_State* L, T* ref) {
|
||||
template <typename T> T* push_ref(lua_State* L, T* ref) {
|
||||
*(T**)lua_newuserdata(L, sizeof(T*)) = SkSafeRef(ref);
|
||||
luaL_getmetatable(L, get_mtname<T>());
|
||||
lua_setmetatable(L, -2);
|
||||
return ref;
|
||||
}
|
||||
|
||||
template <typename T> T* get_ref(lua_State* L, int index) {
|
||||
@ -443,6 +445,11 @@ static int lcanvas_drawColor(lua_State* L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lcanvas_drawPaint(lua_State* L) {
|
||||
get_ref<SkCanvas>(L, 1)->drawPaint(*get_obj<SkPaint>(L, 2));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lcanvas_drawRect(lua_State* L) {
|
||||
SkRect rect;
|
||||
get_ref<SkCanvas>(L, 1)->drawRect(*lua2rect(L, 2, &rect),
|
||||
@ -646,8 +653,7 @@ static int lcanvas_newSurface(lua_State* L) {
|
||||
if (NULL == surface) {
|
||||
lua_pushnil(L);
|
||||
} else {
|
||||
push_ref(L, surface);
|
||||
surface->unref();
|
||||
push_ref(L, surface)->unref();
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
@ -660,6 +666,7 @@ static int lcanvas_gc(lua_State* L) {
|
||||
const struct luaL_Reg gSkCanvas_Methods[] = {
|
||||
{ "clear", lcanvas_clear },
|
||||
{ "drawColor", lcanvas_drawColor },
|
||||
{ "drawPaint", lcanvas_drawPaint },
|
||||
{ "drawRect", lcanvas_drawRect },
|
||||
{ "drawOval", lcanvas_drawOval },
|
||||
{ "drawCircle", lcanvas_drawCircle },
|
||||
@ -1012,6 +1019,12 @@ static int lpaint_getShader(lua_State* L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lpaint_setShader(lua_State* L) {
|
||||
SkPaint* paint = get_obj<SkPaint>(L, 1);
|
||||
paint->setShader(get_ref<SkShader>(L, 2));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lpaint_getPathEffect(lua_State* L) {
|
||||
const SkPaint* paint = get_obj<SkPaint>(L, 1);
|
||||
SkPathEffect* pe = paint->getPathEffect();
|
||||
@ -1070,6 +1083,7 @@ static const struct luaL_Reg gSkPaint_Methods[] = {
|
||||
{ "getImageFilter", lpaint_getImageFilter },
|
||||
{ "setImageFilter", lpaint_setImageFilter },
|
||||
{ "getShader", lpaint_getShader },
|
||||
{ "setShader", lpaint_setShader },
|
||||
{ "getPathEffect", lpaint_getPathEffect },
|
||||
{ "__gc", lpaint_gc },
|
||||
{ NULL, NULL }
|
||||
@ -1564,8 +1578,7 @@ static int lsurface_newImageSnapshot(lua_State* L) {
|
||||
if (NULL == image) {
|
||||
lua_pushnil(L);
|
||||
} else {
|
||||
push_ref(L, image);
|
||||
image->unref();
|
||||
push_ref(L, image)->unref();
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
@ -1578,8 +1591,7 @@ static int lsurface_newSurface(lua_State* L) {
|
||||
if (NULL == surface) {
|
||||
lua_pushnil(L);
|
||||
} else {
|
||||
push_ref(L, surface);
|
||||
surface->unref();
|
||||
push_ref(L, surface)->unref();
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
@ -1635,8 +1647,7 @@ static int lpicturerecorder_endRecording(lua_State* L) {
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
push_ref(L, pic);
|
||||
pic->unref(); // lua is the only owner, so we unref ours
|
||||
push_ref(L, pic)->unref();
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -1730,8 +1741,7 @@ static int lsk_newDocumentPDF(lua_State* L) {
|
||||
// do I need to push a nil on the stack and return 1?
|
||||
return 0;
|
||||
} else {
|
||||
push_ref(L, doc);
|
||||
doc->unref();
|
||||
push_ref(L, doc)->unref();
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@ -1743,8 +1753,26 @@ static int lsk_newBlurImageFilter(lua_State* L) {
|
||||
if (NULL == imf) {
|
||||
lua_pushnil(L);
|
||||
} else {
|
||||
push_ref(L, imf);
|
||||
imf->unref();
|
||||
push_ref(L, imf)->unref();
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int lsk_newLinearGradient(lua_State* L) {
|
||||
SkScalar x0 = lua2scalar_def(L, 1, 0);
|
||||
SkScalar y0 = lua2scalar_def(L, 2, 0);
|
||||
SkColor c0 = lua2color(L, 3);
|
||||
SkScalar x1 = lua2scalar_def(L, 4, 0);
|
||||
SkScalar y1 = lua2scalar_def(L, 5, 0);
|
||||
SkColor c1 = lua2color(L, 6);
|
||||
|
||||
SkPoint pts[] = { { x0, y0 }, { x1, y1 } };
|
||||
SkColor colors[] = { c0, c1 };
|
||||
SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode);
|
||||
if (NULL == s) {
|
||||
lua_pushnil(L);
|
||||
} else {
|
||||
push_ref(L, s)->unref();
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
@ -1792,8 +1820,7 @@ static int lsk_newTypeface(lua_State* L) {
|
||||
if (NULL == face) {
|
||||
face = SkTypeface::RefDefault();
|
||||
}
|
||||
push_ref(L, face);
|
||||
face->unref();
|
||||
push_ref(L, face)->unref();
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -1805,8 +1832,7 @@ static int lsk_newRasterSurface(lua_State* L) {
|
||||
if (NULL == surface) {
|
||||
lua_pushnil(L);
|
||||
} else {
|
||||
push_ref(L, surface);
|
||||
surface->unref();
|
||||
push_ref(L, surface)->unref();
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
@ -1820,8 +1846,7 @@ static int lsk_loadImage(lua_State* L) {
|
||||
SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options()));
|
||||
|
||||
if (image) {
|
||||
push_ref(L, image);
|
||||
image->unref();
|
||||
push_ref(L, image)->unref();
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@ -1838,6 +1863,7 @@ static void register_Sk(lua_State* L) {
|
||||
setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF);
|
||||
setfield_function(L, "loadImage", lsk_loadImage);
|
||||
setfield_function(L, "newBlurImageFilter", lsk_newBlurImageFilter);
|
||||
setfield_function(L, "newLinearGradient", lsk_newLinearGradient);
|
||||
setfield_function(L, "newMatrix", lsk_newMatrix);
|
||||
setfield_function(L, "newPaint", lsk_newPaint);
|
||||
setfield_function(L, "newPath", lsk_newPath);
|
||||
|
Loading…
Reference in New Issue
Block a user