Skip to content

Commit

Permalink
adding definitive stroke scaling test
Browse files Browse the repository at this point in the history
adding piano key graphic
  • Loading branch information
William Adams committed May 6, 2019
1 parent f75b377 commit 0397e57
Show file tree
Hide file tree
Showing 10 changed files with 455 additions and 103 deletions.
118 changes: 91 additions & 27 deletions blend2d/blend2d.lua
Expand Up @@ -9,34 +9,7 @@ BLContextCookie = ffi.typeof("struct BLContextCookie")
BLContextHints = ffi.typeof("struct BLContextHints")
BLContextState = ffi.typeof("struct BLContextState")

--[=[
BLContextCore_mt.__index = function(self, key)
print("BLContextCore_mt:__index: ", key)
-- first look in the implemenation for a field
local success, info = pcall(function() return self.impl[key] end)
if success then
return info
end
--[[
-- finally look for something in the virtual table
-- then look in the virtual table for a function
success, info = pcall(function() return self.impl.virt[key] end)
if success then
return info
end
--]]
-- look for a private implementation
local info rawget(BLContextCore_mt, key)
print("Info: ", key, info)
for k,v in pairs(BLContextCore_mt) do
print(k,v)
end

return info
end
--]=]
BLContext = ffi.typeof("struct BLContextCore")
ffi.metatype(BLContext, {
__gc = function(self)
Expand Down Expand Up @@ -1096,7 +1069,98 @@ BLArrayCore* __cdecl blImageCodecBuiltInCodecs(void) ;
}
ffi.metatype(BLImageCodec, BLImageCodec_mt)

--[[
--BLResult __cdecl blMatrix2DSetIdentity(BLMatrix2D* self) ;
--BLResult __cdecl blMatrix2DSetTranslation(BLMatrix2D* self, double x, double y) ;
--BLResult __cdecl blMatrix2DSetSkewing(BLMatrix2D* self, double x, double y) ;
--BLResult __cdecl blMatrix2DSetRotation(BLMatrix2D* self, double angle, double cx, double cy) ;
BLResult __cdecl blMatrix2DApplyOp(BLMatrix2D* self, uint32_t opType, const void* opData) ;
--BLResult __cdecl blMatrix2DInvert(BLMatrix2D* dst, const BLMatrix2D* src) ;
--uint32_t __cdecl blMatrix2DGetType(const BLMatrix2D* self) ;
--BLResult __cdecl blMatrix2DMapPointDArray(const BLMatrix2D* self, BLPoint* dst, const BLPoint* src, size_t count) ;
--]]
BLMatrix2D = ffi.typeof("struct BLMatrix2D")
local BLMatrix2D_mt = {
__tostring = function(self)
local tbl = {}
table.insert(tbl, string.format("%3.2f %3.2f", self.m00, self.m01))
table.insert(tbl, string.format("%3.2f %3.2f", self.m10, self.m11))
table.insert(tbl, string.format("%3.2f %3.2f", self.m20, self.m21))
return table.concat(tbl, "\n")
end;
__index = {
-- Constructors
createIdentity = function(self)
local m1 = BLMatrix2D();
blapi.blMatrix2DSetIdentity(m1)
return m1
end;
createScaling = function(self, sx, sy)
local m1 = BLMatrix2D()
blapi.blMatrix2DSetScaling(m1, sx, sy)
return m1
end;
createSkewing = function(self, skx, sky)
local m1 = BLMatrix2D()
blapi.blMatrix2DSetSkewing(m1, skx, sky) ;
return m1;
end;
createTranslation = function(self, x, y)
local m1 = BLMatrix2D();
blapi.blMatrix2DSetTranslation(m1, x, y)
return m1;
end;
createRotation = function(self, angle, cx, cy)
local m1 = BLMatrix2D()
blapi.blMatrix2DSetRotation(m1, angle, cx, cy) -- 45 degrees
return m1
end;
createInverse = function(self)
local dst = ffi.new("struct BLMatrix2D")
blapi.blMatrix2DInvert(dst, self) ;
return dst
end;
-- Matrix operations
applyOperation = function(self, opType, opData)
local bResult = blapi.blMatrix2DApplyOp(self, opType, opData) ;
end;
getType = function(self)
return blapi.blMatrix2DGetType(self) ;
end;
-- apply the current transformation to an array of points
-- should accept a lua table as a matter of convenience
transformPoints = function(self, dstPts, srcPts, count)
local bResult = blapi.blMatrix2DMapPointDArray(self, dstPts, srcPts, count) ;
if bResult ~= C.BL_SUCCESS then
return false, bResult;
end
return self;
end;
-- transform a single point
transformSinglePoint = function(self, srcPt)
local dstPoint = ffi.new("BLPoint")
local bResult = blapi.blMatrix2DMapPointDArray(self, dstPoint, srcPt, 1)
if bResult ~= C.BL_SUCCESS then
return false, bResult;
end
return dstPoint
end;
};
}
ffi.metatype("struct BLMatrix2D", BLMatrix2D_mt)
--[[
Expand Down
164 changes: 101 additions & 63 deletions testy/DrawingContext.lua
Expand Up @@ -362,6 +362,24 @@ function DrawingContext.rectMode(self, newMode)
end
-- Matrix operations
function DrawingContext.resetTransform(self)
local bResult = self:_applyMatrixOp(C.BL_MATRIX2D_OP_RESET, nil);
if bResult ~= C.BL_SUCCESS then
return false, bResult
end
return self
end
function DrawingContext.userToMeta(self)
local bResult = self.DC.impl.virt.userToMeta(self.DC.impl)
if bResult ~= C.BL_SUCCESS then
return false, bResult;
end
return self;
end
-- Applies a matrix operation to the current transformation matrix (internal).
function DrawingContext._applyMatrixOp (self, opType, opData)
Expand All @@ -375,6 +393,7 @@ end
function DrawingContext._applyMatrixOpV(self, opType, ...)
local opData = ffi.new("double[?]",select('#',...), {...});
--print("_applyMatrixOpV: ", opData[0], opData[1])
local bResult = self.DC.impl.virt.matrixOp(self.DC.impl, opType, opData);
if bResult == C.BL_SUCCESS then
return self;
Expand All @@ -398,27 +417,14 @@ function DrawingContext.translate (self, x, y)
return self:_applyMatrixOpV(C.BL_MATRIX2D_OP_TRANSLATE, x, y);
end
function DrawingContext.scale(self, ...)
local nargs = select('#',...)
--print("nargs: ", nargs)
local x, y = 1, 1;
if nargs == 1 then
if typeof(select(1,...) == "number") then
x = select(1,...)
y = x;
--print("blcontext.scale: ", x, y)
return self:_applyMatrixOpV(C.BL_MATRIX2D_OP_SCALE, x, y)
end
elseif nargs == 2 then
x = select(1,...)
y = select(2,...)
if x and y then
return self:_applyMatrixOpV(C.BL_MATRIX2D_OP_SCALE, x, y)
end
end
return false, "invalid arguments"
function DrawingContext.scale(self, x, y)
y = y or x
if not x and y then
return false, "at least one axis must be specified"
end
return self:_applyMatrixOpV(C.BL_MATRIX2D_OP_SCALE, x, y)
end
--[[
Expand Down Expand Up @@ -748,14 +754,25 @@ function DrawingContext.strokeRectI (self, rect)
return false, bResult
end

function DrawingContext.strokeRectD (self, rect)
function DrawingContext.strokeRectD (self, ...)
local nargs = select('#',...)
local rect = select(1,...)

if nargs == 4 then
rect = BLRect(...)
end

if not rect then
return false;
end

local bResult = self.DC.impl.virt.strokeRectD(self.DC.impl, rect);

if bResult == C.BL_SUCCESS then
return self;
if bResult ~= C.BL_SUCCESS then
return nil, bResult;
end
return false, bResult

return self;
end

function DrawingContext.strokeRect(self, x, y, w, h)
Expand Down Expand Up @@ -950,9 +967,56 @@ function DrawingContext.strokeWidth(self, weight)
self.DC:setStrokeWidth(weight);
end

--[[
]]
function DrawingContext.loadFont(self, faceFilename)
local fontDir = "c:\\windows\\fonts\\"
local fontfile = fontDir..faceFilename;

aFace, err = BLFontFace:createFromFile(fontfile)
if not aFace then
return false, err
end

self.FontFace = aFace;
-- select font of current font size

self:textSize(self.TextSize)
end

function DrawingContext.textSize(self, asize)
local afont, err = self.FontFace:createFont(asize)

if not afont then
return false, err;
end

self.Font = afont
self.TextSize = asize

return true;
end

function DrawingContext.textWidth(self, txt)
return self.Font:measureText(txt)
end

function DrawingContext.textAlign(self, halign, valign)
self.TextHAlignment = halign or DrawingContext.constants.LEFT
self.TextVAlignment = valign or DrawingContext.constants.BASELINE
end

function DrawingContext.textLeading(self, leading)
self.TextLeading = leading
end

function DrawingContext.textMode(self, mode)
self.TextMode = mode
end

function DrawingContext.calcTextPosition(self, txt, x, y)
local cx, cy = self:textWidth(txt)
local cx, cy = self.Font:measureText(txt)

if self.TextHAlignment == LEFT then
x = x
Expand All @@ -978,36 +1042,16 @@ function DrawingContext.text(self, txt, x, y)
self.DC:fillUtf8Text(BLPoint(x,y), self.Font, txt, #txt)
end

function DrawingContext.textAlign(self, halign, valign)
self.TextHAlignment = halign or DrawingContext.constants.LEFT
self.TextVAlignment = valign or DrawingContext.constants.BASELINE
end

function DrawingContext.textLeading(self, leading)
self.TextLeading = leading
end

function DrawingContext.textMode(self, mode)
self.TextMode = mode
end

function DrawingContext.textWidth(self, txt)
return self.Font:measureText(txt)
end

function DrawingContext.textSize(self, asize)
local afont, err = self.FontFace:createFont(asize)

if not afont then
return false, err;
end

self.Font = afont
self.TextSize = asize

return true;
end

--[[
Rectangles
]]
local function calcModeRect(mode, a,b,c,d)

local x1 = 0;
Expand Down Expand Up @@ -1043,22 +1087,16 @@ local function calcModeRect(mode, a,b,c,d)
return x1, y1, rwidth, rheight;
end

function DrawingContext.rect(self, ...)
local nargs = select('#',...)
if nargs < 4 then return false end
function DrawingContext.rect(self, a,b,c,d)

local x1, y1, rwidth, rheight = calcModeRect(self.RectMode, ...)
local x1, y1, rwidth, rheight = calcModeRect(self.RectMode, a,b,c,d)

if nargs == 4 then
if self.useFill then
self:fillRectD(x1, y1, rwidth, rheight)
end
if self.useFill then
self:fillRectD(x1, y1, rwidth, rheight)
end

if self.useStroke then
self:strokeRectD(BLRect(x1, y1, rwidth, rheight))
end
elseif nargs == 5 then
-- do rounded rect
if self.useStroke then
self:strokeRectD(BLRect(x1, y1, rwidth, rheight))
end

return true;
Expand Down
6 changes: 3 additions & 3 deletions testy/GCompKeyboard.lua
Expand Up @@ -26,7 +26,7 @@ function GKeyboard.new(self, obj)
obj.keysState = ffi.new("BYTE[256]")
obj.showKeyState = true;
obj.linear = Gradient.LinearGradient({
values = {0, 0, 0, self.unit};
values = {obj.unit/2, 0, obj.unit/2, obj.unit};
stops = {
{offset = 0, uint32 = 0xFFFFFFFF},
{offset = 1, uint32 = 0xFF1F7FFF}
Expand Down Expand Up @@ -72,8 +72,8 @@ function GKeyboard.draw(self, ctx)
local rrect = BLRoundRect(key.frame.x,key.frame.y,key.frame.width, key.frame.height, 3, 3)
local crect = insetRect(rrect,self.unit*0.30,self.unit*0.30)

ctx:fill(127)
--ctx:setFillStyle(self.linear)
--ctx:fill(127)
ctx:setFillStyle(self.linear)
ctx:fillRoundRect(rrect)
ctx:strokeRoundRect(rrect)

Expand Down

0 comments on commit 0397e57

Please sign in to comment.