Skip to content

Commit

Permalink
Annotation Ink added.
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasMatthias committed Jun 5, 2021
1 parent 3cbef7f commit 987646c
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Following annotations are implemented so far:
- Line
- Polygon, PolyLine
- Stamp
- Ink
- FileAttachment

## Requirements
Expand Down
43 changes: 43 additions & 0 deletions flare-annot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,35 @@ function Page:getCoordinatesArray(annot, key)
end


--- Return an array of array of coordinates, eg. `InkList`.
-- @pdfe annot annotation dictionary
-- @string key key
-- @return Table
function Page:getCoordinatesArrayArray(annot, key)
if annot[key] then
local ctm = self:readFromCache('ctm')
ctm = ctm or self.IdentityCTM

local t = types.pdfarray:new()
for _, coords in ipairs(annot[key]) do
local newcoords = types.pdfarray:new()
local idx = 1
while idx <= #coords do
local x, y = coords[idx], coords[idx + 1]
local xn, yn = self:applyCTM(ctm, x, y)
newcoords[#newcoords + 1] = xn
newcoords[#newcoords + 1] = yn
idx = idx + 2
end
t[#t + 1] = newcoords
end
return t
else
return nil
end
end


--- Returns a `Text Markup` annotion dictionary.
-- @pdfe annot annotation dictionary.
-- @return Table
Expand Down Expand Up @@ -488,6 +517,20 @@ function Page:getAnnotStamp(annot)
end


--- Returns an `Ink` annotation dictionary.
-- @pdfe annot annotation dictionary
-- @return Table
function Page:getAnnotInk(annot)
local t = {
InkList = self:getCoordinatesArrayArray(annot, 'InkList'),
BS = self:getBorderStyle(annot, 'BS'),
}
self:appendTable(t, self:getAnnotCommonEntries(annot))
self:appendTable(t, self:getAnnotMarkupEntries(annot))
return t
end


--- Returns a `FileAttachment` annotation table.
-- @pdfe annot annotation dictionary
-- @return Table.
Expand Down
3 changes: 0 additions & 3 deletions flare.sty
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,20 @@

\AddToHook{begindocument}{
\directlua{
print()
doc = flare.Doc:new()
doc:loadCache()
}
}

\AddToHook{enddocument/afterlastpage}{
\directlua{
print()
doc:saveCache()
doc:warnIfCacheDirty()
}
}

\cs_new:Nn \FLR_CopyAnnotations: {
\directlua{
print()
page = flare.Page:new(doc)
page:processKeyvals("{\clist_use:Nnnn \l_FLR_kv_clist {,} {,} {,}}")
page:openFile()
Expand Down
Binary file added test/pdf/ink-01.pdf
Binary file not shown.
35 changes: 35 additions & 0 deletions test/test-flare-annot3.lua
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,41 @@ test('Page:getAnnotStamp()',
end)


test('Page:getAnnotInk() #ok',
function()
local pdf_fn = createTestFile(
'ink',
'\\includegraphics[scale=0.5]{pdf/ink-01.pdf}')

local d = Doc:new()
local p = Page:new(d)
local pagenum = 1
p:setGinKV('filename', pdf_fn)
p:setGinKV('page', pagenum)
p:openFile()
local pdf = pdfe.open(pdf_fn)
local annot = pdfe.getpage(pdf, 1).Annots[1]
local page_objnum = p:getPageObjNum(pagenum)

assert.same('Annot', annot.Type)
assert.same('Ink', annot.Subtype)
assert.nearly_same(
{{221.32, 637.97, 224.69, 643.27, 229.98, 651.47,
233.91, 656.76, 237.75, 660.86, 241.67, 663.89,
246.70, 666.28, 252.29, 667.43, 258.61, 667.36,
265.77, 666.07, 271.62, 664.29, 276.23, 662.18,
279.54, 659.77, 281.53, 657.08, 282.36, 653.89,
282.05, 650.36, 280.59, 646.59, 278.15, 643.03,
275.92, 640.23, 271.77, 635.53, 269.20, 633.83,
266.34, 632.91, 262.31, 632.96, 257.35, 634.23,
252.61, 636.39, 248.98, 639.07, 247.15, 641.38,
246.42, 643.72, 246.82, 646.00, 248.34, 648.20,
249.46, 649.21, 249.82, 649.30, 250.57, 649.20,
251.32, 649.09, 251.71, 649.21}},
p:getObj(annot, 'InkList'))
end)


test('Page:getAnnotFileAttachment()',
function()
--
Expand Down

0 comments on commit 987646c

Please sign in to comment.