[JW Lua] Metrics update problem

Jan Angermüller jan at angermueller.com
Fri Jun 27 21:29:59 CEST 2014


An HTML attachment was scrubbed...
URL: <http://jwmusic.nu/pipermail/jwlua_jwmusic.nu/attachments/20140627/02d301b9/attachment.htm>
-------------- next part --------------
function plugindef()

   -- This function and the 'finaleplugin' namespace

   -- are both reserved for the plug-in definition.

   finaleplugin.RequireSelection = true

   finaleplugin.MinJWLuaVersion = "0.07"

   finaleplugin.Author = "Jari Williamsson"

   finaleplugin.Version = "0.01"

   finaleplugin.CategoryTags = "Expression, Layout, Region"

   return "Dynamic Pos (w. grace note adjust)", "Dynamic Pos", "Centers dynamics horizontally to the notehead and moves to first grace note."

end



function AdjustmentForGraceNote(entry)

    local previous = entry:Previous()

    if not previous then return 0 end           

    if not previous.GraceNote then return 0 end

    

    local gracenotewidth = previous:CalcWidestNoteheadWidth()

    while previous:Previous() and previous:Previous().GraceNote do

        -- Compensate for multiple grace notes. This seems to work ok...

        gracenotewidth = gracenotewidth - previous:GetManualPosition()

        previous = previous:Previous()

        gracenotewidth = gracenotewidth + (previous:CalcWidestNoteheadWidth() * 2 / 3)

    end



    return -previous:GetManualPosition() + gracenotewidth

end



function RemoveArticulationCollision(entry,expr)

    -- move dynamics down when there is a collision with an articulation

    if entry.ArticulationFlag then

       local articulations= entry:CreateArticulations()

       local point = finale.FCPoint(1,1)

       local point2 =finale.FCPoint(1,1)      

       expr:CalcMetricPos(point2)  

       for a in each(articulations) do

          a:CalcMetricPos(point)

print(point.X,point.Y)

          local mindiff=60 -- minimum difference, below this value there will probably be a collision

          local vertdiffabs= math.abs(point2.Y)-math.abs(point.Y)

          local vertdiff =point2.Y-point.Y

          if    ((point2.Y<point.Y) and (vertdiffabs<mindiff)) then

            if vertdiffabs~=mindiff then

               expr.VerticalPos= expr.VerticalPos+(vertdiffabs-mindiff)

               point2.Y=point2.Y+(vertdiffabs-mindiff)

            end

         elseif ((point2.Y>point.Y) and (vertdiffabs>-90)) then

               expr.VerticalPos= expr.VerticalPos+(vertdiffabs-mindiff)

               point2.Y=point2.Y+(vertdiffabs-mindiff)

         end

       end

    end

    return expr

end



function RepositionOneExpression(expr, str, fontinfo)

    -- pos_table syntax: [character] = {downstem-pos, upstem-pos, wholenote-pos}

    local pos_table = {

        ["Maestro"] =

        {

            [236] = {-4, 5, 6}, -- fff

            [196] = {-2, 5, 10}, -- ff

            [102] = {-2, 3, 8}, -- f

            [70] = {-5, 3, 6}, -- mf            

            [80] = {2, 8, 12}, -- mp

            [112] = {9, 12, 15}, -- p

            [185] = {8, 12, 16},  -- pp

            [184] = {10, 15, 17}, -- ppp            

            [234] = {8, 14, 14},  -- fp

            [167] = {2, 12, 14} -- sfz

        },

        ["Maestro Wide"] =

        {

            [236] = {0, 3, 8}, -- fff

            [196] = {2, 10, 10}, -- ff

            [102] = {0, 6, 12}, -- f

            [70] = {-3, 3, 9}, -- mf            

            [80] = {4, 13, 15}, -- mp

            [112] = {5, 10, 18}, -- p

            [185] = {6, 14, 18},  -- pp

            [184] = {6, 17, 19}, -- ppp            

            [234] = {8, 14, 18},  -- fp

            [167] = {4, 12, 16} -- sfz

        }

        }





    local char = str:GetItemAt(0)   

    local postableref = pos_table[fontinfo.Name]

    if postableref == nil then

        print ("Unknown font at measure", expr.Measure, ":", fontinfo.Name )

        return

    end

    postableref = postableref[char]

    if postableref == nil then

        print("Unreferenced font character at measure", expr.Measure, ":", char)

        return

    end

    if (postableref == nil) then 

        print ("Character", char, "not found in table in measure", expr.Measure )

        return 

    end

    -- Check if it's upstem or downstem

    local notecell = finale.FCNoteEntryCell(expr.Measure, expr.Staff)

    notecell:Load()



    local entry = notecell:FindEntryStartPosition(expr.MeasurePos, -1)



    -- load metrics to refresh

    local cellmetrics = finale.FCCellMetrics()

    cellmetrics:LoadAtEntry(entry)   

    local entrymetrics = finale.FCEntryMetrics()

    entrymetrics:Load(entry)   



    if not entry then return end

    if entry:IsRest() then return end    



    local graceadjust = AdjustmentForGraceNote(entry)

    

    local refvalue = nil

    if entry.Duration == finale.WHOLE_NOTE then

        refvalue = postableref[3]

    elseif entry:CalcStemUp() then

        refvalue = postableref[2]

    else

        refvalue = postableref[1]

    end

    if refvalue == nil then

        print ("Unknown reference value for character", char, ", Measure:", expr.Measure, ", Stem up:", entry:CalcStemUp(), ", Duration:", entry.Duration)

        return

    end



    expr.HorizontalPos = refvalue - graceadjust

    expr=RemoveArticulationCollision(entry,expr)

    expr:Save()

end



function RepositionDynamics(m, region)

    local expressions = finale.FCExpressions()

    expressions:LoadAllForItem(m)

    for e in each(expressions) do

        if region:IsStaffIncluded(e.Staff) and e.StaffGroupID == 0 then

            local textexp = e:CreateTextExpressionDef()

            if textexp then

                local fontinfo = finale.FCFontInfo()

                local str = textexp:CreateTextString()

                fontinfo = str:CreateLastFontInfo()

                str:TrimEnigmaTags()                    

                if str:GetLength() == 1 then                        

                    RepositionOneExpression(e, str, fontinfo)

                end

            end

        end

    end

end







local region = finenv.Region()

for m = region.StartMeasure, region.EndMeasure do

      RepositionDynamics(m, region)  

end



-------------- next part --------------
A non-text attachment was scrubbed...
Name: before.jpg
Type: image/jpeg
Size: 65767 bytes
Desc: not available
URL: <http://jwmusic.nu/pipermail/jwlua_jwmusic.nu/attachments/20140627/02d301b9/attachment.jpg>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: after.jpg
Type: image/jpeg
Size: 68146 bytes
Desc: not available
URL: <http://jwmusic.nu/pipermail/jwlua_jwmusic.nu/attachments/20140627/02d301b9/attachment-0001.jpg>


More information about the JWLua mailing list