drawvg-reference.html 144 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Created by , GNU Texinfo 7.1.1 -->
  4. <head>
  5. <meta charset="utf-8">
  6. <title>
  7. drawvg - Language Reference
  8. </title>
  9. <meta name="viewport" content="width=device-width,initial-scale=1.0">
  10. <link rel="stylesheet" type="text/css" href="bootstrap.min.css">
  11. <link rel="stylesheet" type="text/css" href="style.min.css">
  12. </head>
  13. <body>
  14. <div class="container">
  15. <h1>
  16. drawvg - Language Reference
  17. </h1>
  18. <a name="SEC_Top"></a>
  19. <div class="element-contents" id="SEC_Contents">
  20. <h2 class="contents-heading">Table of Contents</h2>
  21. <div class="contents">
  22. <ul class="toc-numbered-mark">
  23. <li><a id="toc-Introduction" href="#Introduction">1 Introduction</a></li>
  24. <li><a id="toc-Syntax" href="#Syntax">2 Syntax</a>
  25. <ul class="toc-numbered-mark">
  26. <li><a id="toc-Structure" href="#Structure">2.1 Structure</a></li>
  27. <li><a id="toc-Comments" href="#Comments">2.2 Comments</a></li>
  28. <li><a id="toc-Commands" href="#Commands">2.3 Commands</a>
  29. <ul class="toc-numbered-mark">
  30. <li><a id="toc-Single_002dLetter-Aliases" href="#Single_002dLetter-Aliases">2.3.1 Single-Letter Aliases</a></li>
  31. <li><a id="toc-Implicit-Commands" href="#Implicit-Commands">2.3.2 Implicit Commands</a></li>
  32. </ul></li>
  33. <li><a id="toc-Arguments" href="#Arguments">2.4 Arguments</a>
  34. <ul class="toc-numbered-mark">
  35. <li><a id="toc-Number-Literals" href="#Number-Literals">2.4.1 Number Literals</a></li>
  36. <li><a id="toc-Expressions" href="#Expressions">2.4.2 Expressions</a></li>
  37. <li><a id="toc-Variable-Names" href="#Variable-Names">2.4.3 Variable Names</a></li>
  38. <li><a id="toc-Colors-1" href="#Colors-1">2.4.4 Colors</a></li>
  39. <li><a id="toc-Constants" href="#Constants">2.4.5 Constants</a></li>
  40. </ul></li>
  41. </ul></li>
  42. <li><a id="toc-Guide" href="#Guide">3 Guide</a>
  43. <ul class="toc-numbered-mark">
  44. <li><a id="toc-Paths" href="#Paths">3.1 Paths</a>
  45. <ul class="toc-numbered-mark">
  46. <li><a id="toc-Current-Point" href="#Current-Point">3.1.1 Current Point</a></li>
  47. <li><a id="toc-Defining-a-Shape" href="#Defining-a-Shape">3.1.2 Defining a Shape</a></li>
  48. <li><a id="toc-Fill" href="#Fill">3.1.3 Fill</a></li>
  49. <li><a id="toc-Stroke" href="#Stroke">3.1.4 Stroke</a></li>
  50. <li><a id="toc-Clip" href="#Clip">3.1.5 Clip</a></li>
  51. <li><a id="toc-Preserving-Paths" href="#Preserving-Paths">3.1.6 Preserving Paths</a></li>
  52. </ul></li>
  53. <li><a id="toc-Variables" href="#Variables">3.2 Variables</a>
  54. <ul class="toc-numbered-mark">
  55. <li><a id="toc-User-Variables-1" href="#User-Variables-1">3.2.1 User Variables</a></li>
  56. </ul></li>
  57. <li><a id="toc-Patterns" href="#Patterns">3.3 Patterns</a>
  58. <ul class="toc-numbered-mark">
  59. <li><a id="toc-Gradients" href="#Gradients">3.3.1 Gradients</a></li>
  60. <li><a id="toc-Variables-1" href="#Variables-1">3.3.2 Variables</a></li>
  61. </ul></li>
  62. <li><a id="toc-Transformations" href="#Transformations">3.4 Transformations</a></li>
  63. <li><a id="toc-State-Stack-1" href="#State-Stack-1">3.5 State Stack</a></li>
  64. <li><a id="toc-Frame-Metadata-1" href="#Frame-Metadata-1">3.6 Frame Metadata</a></li>
  65. <li><a id="toc-if-_002f-repeat-Statements" href="#if-_002f-repeat-Statements">3.7 <code class="code">if</code> / <code class="code">repeat</code> Statements</a>
  66. <ul class="toc-numbered-mark">
  67. <li><a id="toc-Comparison-and-Logical-Operators" href="#Comparison-and-Logical-Operators">3.7.1 Comparison and Logical Operators</a></li>
  68. <li><a id="toc-Early-Exit" href="#Early-Exit">3.7.2 Early Exit</a></li>
  69. </ul></li>
  70. <li><a id="toc-Procedures-1" href="#Procedures-1">3.8 Procedures</a></li>
  71. <li><a id="toc-Functions-in-Expressions" href="#Functions-in-Expressions">3.9 Functions in Expressions</a>
  72. <ul class="toc-numbered-mark">
  73. <li><a id="toc-Function-p" href="#Function-p">3.9.1 Function <code class="code">p</code></a></li>
  74. <li><a id="toc-Function-pathlen" href="#Function-pathlen">3.9.2 Function <code class="code">pathlen</code></a></li>
  75. <li><a id="toc-Function-randomg" href="#Function-randomg">3.9.3 Function <code class="code">randomg</code></a></li>
  76. </ul></li>
  77. <li><a id="toc-Tracing-with-print" href="#Tracing-with-print">3.10 Tracing with <code class="code">print</code></a>
  78. <ul class="toc-numbered-mark">
  79. <li><a id="toc-Function-print" href="#Function-print">3.10.1 Function print</a></li>
  80. <li><a id="toc-Command-print-1" href="#Command-print-1">3.10.2 Command <code class="code">print</code></a></li>
  81. </ul></li>
  82. </ul></li>
  83. <li><a id="toc-Commands-1" href="#Commands-1">4 Commands</a>
  84. <ul class="toc-numbered-mark">
  85. <li><a id="toc-arc" href="#arc">4.1 <code class="code">arc</code></a></li>
  86. <li><a id="toc-arcn" href="#arcn">4.2 <code class="code">arcn</code></a></li>
  87. <li><a id="toc-break" href="#break">4.3 <code class="code">break</code></a></li>
  88. <li><a id="toc-call" href="#call">4.4 <code class="code">call</code></a></li>
  89. <li><a id="toc-circle" href="#circle">4.5 <code class="code">circle</code></a></li>
  90. <li><a id="toc-clip_002c-eoclip" href="#clip_002c-eoclip">4.6 <code class="code">clip</code>, <code class="code">eoclip</code></a></li>
  91. <li><a id="toc-Z_002c-z_002c-closepath" href="#Z_002c-z_002c-closepath">4.7 <code class="code">Z</code>, <code class="code">z</code>, <code class="code">closepath</code></a></li>
  92. <li><a id="toc-colorstop" href="#colorstop">4.8 <code class="code">colorstop</code></a></li>
  93. <li><a id="toc-C_002c-curveto" href="#C_002c-curveto">4.9 <code class="code">C</code>, <code class="code">curveto</code></a></li>
  94. <li><a id="toc-c_002c-rcurveto" href="#c_002c-rcurveto">4.10 <code class="code">c</code>, <code class="code">rcurveto</code></a></li>
  95. <li><a id="toc-defhsla" href="#defhsla">4.11 <code class="code">defhsla</code></a></li>
  96. <li><a id="toc-defrgba" href="#defrgba">4.12 <code class="code">defrgba</code></a></li>
  97. <li><a id="toc-ellipse" href="#ellipse">4.13 <code class="code">ellipse</code></a></li>
  98. <li><a id="toc-fill_002c-eofill" href="#fill_002c-eofill">4.14 <code class="code">fill</code>, <code class="code">eofill</code></a></li>
  99. <li><a id="toc-getmetadata" href="#getmetadata">4.15 <code class="code">getmetadata</code></a></li>
  100. <li><a id="toc-H_002c-h" href="#H_002c-h">4.16 <code class="code">H</code>, <code class="code">h</code></a></li>
  101. <li><a id="toc-if" href="#if">4.17 <code class="code">if</code></a></li>
  102. <li><a id="toc-lineargrad" href="#lineargrad">4.18 <code class="code">lineargrad</code></a></li>
  103. <li><a id="toc-L_002c-lineto" href="#L_002c-lineto">4.19 <code class="code">L</code>, <code class="code">lineto</code></a></li>
  104. <li><a id="toc-l_002c-rlineto" href="#l_002c-rlineto">4.20 <code class="code">l</code>, <code class="code">rlineto</code></a></li>
  105. <li><a id="toc-M_002c-moveto" href="#M_002c-moveto">4.21 <code class="code">M</code>, <code class="code">moveto</code></a></li>
  106. <li><a id="toc-m_002c-rmoveto" href="#m_002c-rmoveto">4.22 <code class="code">m</code>, <code class="code">rmoveto</code></a></li>
  107. <li><a id="toc-newpath" href="#newpath">4.23 <code class="code">newpath</code></a></li>
  108. <li><a id="toc-preserve" href="#preserve">4.24 <code class="code">preserve</code></a></li>
  109. <li><a id="toc-print" href="#print">4.25 <code class="code">print</code></a></li>
  110. <li><a id="toc-proc" href="#proc">4.26 <code class="code">proc</code></a></li>
  111. <li><a id="toc-Q" href="#Q">4.27 <code class="code">Q</code></a></li>
  112. <li><a id="toc-q" href="#q">4.28 <code class="code">q</code></a></li>
  113. <li><a id="toc-radialgrad" href="#radialgrad">4.29 <code class="code">radialgrad</code></a></li>
  114. <li><a id="toc-rect" href="#rect">4.30 <code class="code">rect</code></a></li>
  115. <li><a id="toc-repeat" href="#repeat">4.31 <code class="code">repeat</code></a></li>
  116. <li><a id="toc-resetclip" href="#resetclip">4.32 <code class="code">resetclip</code></a></li>
  117. <li><a id="toc-resetdash" href="#resetdash">4.33 <code class="code">resetdash</code></a></li>
  118. <li><a id="toc-resetmatrix" href="#resetmatrix">4.34 <code class="code">resetmatrix</code></a></li>
  119. <li><a id="toc-restore" href="#restore">4.35 <code class="code">restore</code></a></li>
  120. <li><a id="toc-rotate" href="#rotate">4.36 <code class="code">rotate</code></a></li>
  121. <li><a id="toc-roundedrect" href="#roundedrect">4.37 <code class="code">roundedrect</code></a></li>
  122. <li><a id="toc-save" href="#save">4.38 <code class="code">save</code></a></li>
  123. <li><a id="toc-scale" href="#scale">4.39 <code class="code">scale</code></a></li>
  124. <li><a id="toc-scalexy" href="#scalexy">4.40 <code class="code">scalexy</code></a></li>
  125. <li><a id="toc-setcolor" href="#setcolor">4.41 <code class="code">setcolor</code></a></li>
  126. <li><a id="toc-setdash" href="#setdash">4.42 <code class="code">setdash</code></a></li>
  127. <li><a id="toc-setdashoffset" href="#setdashoffset">4.43 <code class="code">setdashoffset</code></a></li>
  128. <li><a id="toc-sethsla" href="#sethsla">4.44 <code class="code">sethsla</code></a></li>
  129. <li><a id="toc-setlinecap" href="#setlinecap">4.45 <code class="code">setlinecap</code></a></li>
  130. <li><a id="toc-setlinejoin" href="#setlinejoin">4.46 <code class="code">setlinejoin</code></a></li>
  131. <li><a id="toc-setlinewidth" href="#setlinewidth">4.47 <code class="code">setlinewidth</code></a></li>
  132. <li><a id="toc-setrgba" href="#setrgba">4.48 <code class="code">setrgba</code></a></li>
  133. <li><a id="toc-setvar" href="#setvar">4.49 <code class="code">setvar</code></a></li>
  134. <li><a id="toc-stroke" href="#stroke">4.50 <code class="code">stroke</code></a></li>
  135. <li><a id="toc-S_002c-s" href="#S_002c-s">4.51 <code class="code">S</code>, <code class="code">s</code></a></li>
  136. <li><a id="toc-translate" href="#translate">4.52 <code class="code">translate</code></a></li>
  137. <li><a id="toc-T_002c-t" href="#T_002c-t">4.53 <code class="code">T</code>, <code class="code">t</code></a></li>
  138. <li><a id="toc-V_002c-v" href="#V_002c-v">4.54 <code class="code">V</code>, <code class="code">v</code></a></li>
  139. </ul></li>
  140. </ul>
  141. </div>
  142. </div>
  143. <a name="Introduction"></a>
  144. <h2 class="chapter">1 Introduction<span class="pull-right"><a class="anchor hidden-xs" href="#Introduction" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Introduction" aria-hidden="true">TOC</a></span></h2>
  145. <p>drawvg (<em class="emph">draw vector graphics</em>) is a language to draw
  146. two-dimensional graphics on top of video frames. It is not intended to
  147. be used as a general-purpose language. Since its scope is limited, it
  148. prioritizes being concise and easy to use.
  149. </p>
  150. <p>For example, using the
  151. <a class="uref" href="https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API">Canvas
  152. API</a> we can render a triangle running this code in a Web browser:
  153. </p>
  154. <div class="example">
  155. <pre class="example-preformatted">const canvas = document.getElementById(&quot;canvas&quot;);
  156. const ctx = canvas.getContext(&quot;2d&quot;);
  157. ctx.beginPath();
  158. ctx.moveTo(125, 50);
  159. ctx.lineTo(100, 100);
  160. ctx.lineTo(150, 100);
  161. ctx.closePath();
  162. ctx.stroke();
  163. </pre></div>
  164. <p>The same triangle can be written with this drawvg script:
  165. </p>
  166. <div class="example">
  167. <pre class="example-preformatted">moveto 125 50
  168. lineto 100 100 150 100
  169. closepath
  170. stroke
  171. </pre></div>
  172. <p>It can be shortened using the aliases for <a class="ref" href="#cmd_005fmoveto"><code class="code">moveto</code></a>, <a class="ref" href="#cmd_005flineto"><code class="code">lineto</code></a>,
  173. and <a class="ref" href="#cmd_005fclosepath"><code class="code">closepath</code></a>:
  174. </p>
  175. <div class="example">
  176. <pre class="example-preformatted">M 125 50
  177. L 100 100 150 100
  178. Z
  179. stroke
  180. </pre></div>
  181. <p>Both newlines (<code class="code">U+000A</code>) and spaces (<code class="code">U+0020</code>) can be used
  182. interchangeably as delimiters, so multiple commands can appear on the
  183. same line:
  184. </p>
  185. <div class="example">
  186. <pre class="example-preformatted">M 125 50 L 100 100 150 100 Z
  187. stroke
  188. </pre></div>
  189. <p>Finally, drawvg can use <a data-manual="ffmpeg-utils" href="ffmpeg-utils.html#Expression-Evaluation">FFmpeg expressions</a> and frame metadata in command arguments. In
  190. this example, we are using the variables <var class="var">w</var> (frame width) and <var class="var">h</var>
  191. (frame height) to create a circle in the middle of the frame.
  192. </p>
  193. <div class="example">
  194. <pre class="example-preformatted">circle (w / 2) (h / 2) (w / 3)
  195. stroke
  196. </pre></div>
  197. <p>Many commands are a direct equivalent to a function in the
  198. <a class="uref" href="https://www.cairographics.org/">Cairo graphics library</a>. For such
  199. commands, the reference below provides a link to the related Cairo
  200. documentation.
  201. </p>
  202. <a name="Syntax"></a>
  203. <h2 class="chapter">2 Syntax<span class="pull-right"><a class="anchor hidden-xs" href="#Syntax" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Syntax" aria-hidden="true">TOC</a></span></h2>
  204. <p>The syntax is heavily inspired by languages like
  205. <a class="uref" href="https://imagemagick.org/script/magick-vector-graphics.php">Magick
  206. Vector Graphics</a>, or <a class="uref" href="https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Element/path">SVG&rsquo;s <code class="code">&lt;path&gt;</code></a>. Many command names are taken from
  207. <a class="uref" href="https://en.wikipedia.org/wiki/PostScript">PostScript</a>.
  208. </p>
  209. <a name="Structure"></a>
  210. <h3 class="section">2.1 Structure<span class="pull-right"><a class="anchor hidden-xs" href="#Structure" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Structure" aria-hidden="true">TOC</a></span></h3>
  211. <p>A drawvg script consists of a series of commands to describe 2D
  212. graphics.
  213. </p>
  214. <p>A command is an identifier (like <a class="ref" href="#cmd_005fsetcolor"><code class="code">setcolor</code></a> or <a class="ref" href="#cmd_005flineto"><code class="code">lineto</code></a>)
  215. followed by its arguments. Each item in the code (command name,
  216. arguments, etc.) is separated by any of the following characters:
  217. </p>
  218. <ul class="itemize mark-bullet">
  219. <li>Space (<code class="code">' '</code>)
  220. </li><li>Comma (<code class="code">','</code>)
  221. </li><li>Newline (<code class="code">'\n'</code>)
  222. </li><li>Tabs (<code class="code">'\t'</code>)
  223. </li><li>Return (<code class="code">'\r'</code>)
  224. </li></ul>
  225. <p>The beginning of the item indicates how it will be interpreted:
  226. </p>
  227. <dl class="table">
  228. <dt><span class="r"><code class="code">//</code></span></dt>
  229. <dd><p>Comment
  230. </p></dd>
  231. <dt><span class="r"><code class="code">0</code>, &hellip;, <code class="code">9</code>, <code class="code">+</code>, <code class="code">-</code></span></dt>
  232. <dd><p>Number literal
  233. </p></dd>
  234. <dt><span class="r"><code class="code">(</code></span></dt>
  235. <dd><p>Expression
  236. </p></dd>
  237. <dt><span class="r"><code class="code">{</code>, <code class="code">}</code></span></dt>
  238. <dd><p>Block delimiters
  239. </p></dd>
  240. <dt><span class="r">Anything else</span></dt>
  241. <dd><p>Name of a command, a color, etc.
  242. </p></dd>
  243. </dl>
  244. <a name="Comments"></a>
  245. <h3 class="section">2.2 Comments<span class="pull-right"><a class="anchor hidden-xs" href="#Comments" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Comments" aria-hidden="true">TOC</a></span></h3>
  246. <p>Comments start with two slashes (<code class="code">//</code>), and stop at the end of the
  247. line (either a <code class="code">\n</code>, or the end of the script).
  248. </p>
  249. <div class="example">
  250. <pre class="example-preformatted">circle 100 100 50 // this is ignored
  251. fill
  252. // this is also ignored
  253. </pre></div>
  254. <p><code class="code">//</code> must appear after a space, or at the beginning of the line. If
  255. <code class="code">//</code> is preceded by any non-blank character, the parser will
  256. consider <code class="code">//</code> as part of the previous item.
  257. </p>
  258. <p>For example, in this script:
  259. </p>
  260. <div class="example">
  261. <pre class="example-preformatted">circle 10 10 50// something
  262. </pre></div>
  263. <p>The parser throws an error because it tries to parse <code class="code">50//</code> as a
  264. number literal.
  265. </p>
  266. <a name="Commands"></a>
  267. <h3 class="section">2.3 Commands<span class="pull-right"><a class="anchor hidden-xs" href="#Commands" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Commands" aria-hidden="true">TOC</a></span></h3>
  268. <p>The way commands are parsed is inspired by <a class="uref" href="https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Element/path">SVG&rsquo;s <code class="code">&lt;path&gt;</code></a>:
  269. </p>
  270. <ul class="itemize mark-bullet">
  271. <li>Every command in the script starts with its name, and it is followed by
  272. zero or more arguments.
  273. </li><li>There are no explicit delimiters between commands or arguments.
  274. <p>Most programming languages expect characters like parenthesis, commas,
  275. or semicolons, to separate items. For example:
  276. </p>
  277. <div class="example">
  278. <pre class="example-preformatted">moveto(10, 10); lineto(20, 30);
  279. </pre></div>
  280. <p>The equivalent in drawvg is:
  281. </p>
  282. <div class="example">
  283. <pre class="example-preformatted">moveto 10 10 lineto 20 30
  284. </pre></div>
  285. </li><li>If the command has no arguments (like <a class="ref" href="#cmd_005fclosepath"><code class="code">closepath</code></a> or
  286. <a class="ref" href="#cmd_005fstroke"><code class="code">stroke</code></a>), the next command starts at the next item.
  287. </li></ul>
  288. <table class="cartouche" border="1"><tr><th>
  289. Example</th></tr><tr><td>
  290. <p>In the next script there are 4 different commands:
  291. </p>
  292. <div class="example">
  293. <pre class="example-preformatted">newpath rect 10 20 30 40 setcolor teal fill
  294. </pre></div>
  295. <ol class="enumerate">
  296. <li> <a class="ref" href="#cmd_005fnewpath"><code class="code">newpath</code></a> requires no arguments.
  297. </li><li> <a class="ref" href="#cmd_005frect"><code class="code">rect</code></a> requires 4 arguments, so it takes the next 4 numbers.
  298. </li><li> <a class="ref" href="#cmd_005fsetcolor"><code class="code">setcolor</code></a> requires 1 argument, so it takes the word <code class="code">teal</code>.
  299. </li><li> <a class="ref" href="#cmd_005ffill"><code class="code">fill</code></a> requires no arguments.
  300. </li></ol>
  301. </td></tr></table>
  302. <a name="Single_002dLetter-Aliases"></a>
  303. <h4 class="subsection">2.3.1 Single-Letter Aliases<span class="pull-right"><a class="anchor hidden-xs" href="#Single_002dLetter-Aliases" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Single_002dLetter-Aliases" aria-hidden="true">TOC</a></span></h4>
  304. <p>Most commands in <a class="uref" href="https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Element/path">SVG&rsquo;s <code class="code">&lt;path&gt;</code></a> are also present in drawvg. For some of them,
  305. there is an alias to a longer name:
  306. </p>
  307. <ul class="itemize mark-bullet">
  308. <li><a class="ref" href="#cmd_005fcurveto"><code class="code">curveto</code></a> for <a class="ref" href="#cmd_005fC"><code class="code">C</code></a>.
  309. </li><li><a class="ref" href="#cmd_005frcurveto"><code class="code">rcurveto</code></a> for <a class="ref" href="#cmd_005fc"><code class="code">c</code></a>.
  310. </li><li><a class="ref" href="#cmd_005flineto"><code class="code">lineto</code></a> for <a class="ref" href="#cmd_005fL"><code class="code">L</code></a>.
  311. </li><li><a class="ref" href="#cmd_005frlineto"><code class="code">rlineto</code></a> for <a class="ref" href="#cmd_005fl"><code class="code">l</code></a>.
  312. </li><li><a class="ref" href="#cmd_005fmoveto"><code class="code">moveto</code></a> for <a class="ref" href="#cmd_005fM"><code class="code">M</code></a>.
  313. </li><li><a class="ref" href="#cmd_005frmoveto"><code class="code">rmoveto</code></a> for <a class="ref" href="#cmd_005fm"><code class="code">m</code></a>.
  314. </li><li><a class="ref" href="#cmd_005fclosepath"><code class="code">closepath</code></a> for <a class="ref" href="#cmd_005fZ"><code class="code">Z</code></a>, <a class="ref" href="#cmd_005fz"><code class="code">z</code></a>.
  315. </li></ul>
  316. <p>Other commands only exist in a single-letter form:
  317. </p>
  318. <ul class="itemize mark-bullet">
  319. <li><a class="ref" href="#cmd_005fH"><code class="code">H</code></a>, <a class="ref" href="#cmd_005fh"><code class="code">h</code></a>
  320. </li><li><a class="ref" href="#cmd_005fQ"><code class="code">Q</code></a>, <a class="ref" href="#cmd_005fq"><code class="code">q</code></a>
  321. </li><li><a class="ref" href="#cmd_005fS"><code class="code">S</code></a>, <a class="ref" href="#cmd_005fs"><code class="code">s</code></a>
  322. </li><li><a class="ref" href="#cmd_005fV"><code class="code">V</code></a>, <a class="ref" href="#cmd_005fv"><code class="code">v</code></a>
  323. </li><li><a class="ref" href="#cmd_005fT"><code class="code">T</code></a>, <a class="ref" href="#cmd_005ft"><code class="code">t</code></a>
  324. </li></ul>
  325. <p>This makes it possible to use a path in SVG to create the same shape in
  326. a drawvg script.
  327. </p>
  328. <a class="anchor" id="implicit-commands"></a><a name="Implicit-Commands"></a>
  329. <h4 class="subsection">2.3.2 Implicit Commands<span class="pull-right"><a class="anchor hidden-xs" href="#Implicit-Commands" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Implicit-Commands" aria-hidden="true">TOC</a></span></h4>
  330. <p>For many commands, the name can be omitted when it is used multiple
  331. times in successive calls.
  332. </p>
  333. <p>In the reference below, these commands has a <em class="emph">Can be Implicit</em> note
  334. in their signature.
  335. </p>
  336. <table class="cartouche" border="1"><tr><th>
  337. Example</th></tr><tr><td>
  338. <p>For example, in this script:
  339. </p>
  340. <div class="example">
  341. <pre class="example-preformatted">M 50 50
  342. l 10 10
  343. l 10 -10
  344. l 10 10
  345. l 10 -10
  346. l 10 10
  347. stroke
  348. </pre></div>
  349. <p>After the first call to <a class="ref" href="#cmd_005fl"><code class="code">l</code></a> (alias to <a class="ref" href="#cmd_005frlineto"><code class="code">rlineto</code></a>), the command
  350. can be executed without the name, so it can be written as:
  351. </p>
  352. <div class="example">
  353. <pre class="example-preformatted">M 50 50
  354. l 10 10 10 -10 10 10 10 -10 10 10
  355. stroke
  356. </pre></div>
  357. </td></tr></table>
  358. <p>To reuse the same command (<a class="ref" href="#cmd_005fl"><code class="code">l</code></a>, in the previous example), the
  359. parser checks if the item after the last argument is a numeric value,
  360. like a number literal or a FFmpeg expression.
  361. </p>
  362. <table class="cartouche" border="1"><tr><th>
  363. Example</th></tr><tr><td>
  364. <p>In this example:
  365. </p>
  366. <div class="example">
  367. <pre class="example-preformatted">l 10 20 30 40 stroke
  368. </pre></div>
  369. <p><a class="ref" href="#cmd_005fl"><code class="code">l</code></a> requires 2 arguments, and can be implicit, so the parser
  370. performs this operation:
  371. </p>
  372. <ol class="enumerate">
  373. <li> Takes the two next items (<code class="code">10</code> and <code class="code">20</code>) and emits the first
  374. instruction.
  375. </li><li> Checks if the item after <code class="code">20</code> is a numeric value. Since it is
  376. <code class="code">30</code>, it takes <code class="code">30</code> and <code class="code">40</code> and emits the second
  377. instruction (<code class="code">l 30 40</code>).
  378. </li><li> Checks if the next item after <code class="code">40</code> is a numeric value, but it is a
  379. command (<a class="ref" href="#cmd_005fstroke"><code class="code">stroke</code></a>), so it stops reusing <a class="ref" href="#cmd_005fl"><code class="code">l</code></a>.
  380. </li></ol>
  381. </td></tr></table>
  382. <p>This is another feature taken from <a class="uref" href="https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Element/path">SVG&rsquo;s <code class="code">&lt;path&gt;</code></a>. An important difference with
  383. SVG is that the separator between items is always required. In SVG, it can be
  384. omitted in some cases. For example, the expression <code class="code">m1-2</code> is equivalent to
  385. <code class="code">m 1 -2</code> in SVG, but a syntax error in drawvg.
  386. </p>
  387. <a name="Arguments"></a>
  388. <h3 class="section">2.4 Arguments<span class="pull-right"><a class="anchor hidden-xs" href="#Arguments" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Arguments" aria-hidden="true">TOC</a></span></h3>
  389. <p>Most commands expect numeric arguments, like number literals, variable
  390. names, or expressions.
  391. </p>
  392. <p><a class="ref" href="#cmd_005fsetcolor"><code class="code">setcolor</code></a> and <a class="ref" href="#cmd_005fcolorstop"><code class="code">colorstop</code></a> expect a color.
  393. </p>
  394. <p><a class="ref" href="#cmd_005fsetlinecap"><code class="code">setlinecap</code></a> and <a class="ref" href="#cmd_005fsetlinejoin"><code class="code">setlinejoin</code></a> expect a constant value.
  395. </p>
  396. <a name="Number-Literals"></a>
  397. <h4 class="subsection">2.4.1 Number Literals<span class="pull-right"><a class="anchor hidden-xs" href="#Number-Literals" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Number-Literals" aria-hidden="true">TOC</a></span></h4>
  398. <p>A number literal is an item in the script that represents a constant
  399. value. Any item that starts with a decimal digit (between <code class="code">0</code> and
  400. <code class="code">9</code>), a <code class="code">-</code> or a <code class="code">+</code>, is interpreted as a number literal.
  401. </p>
  402. <p>The value is parsed with
  403. <a class="uref" href="https://ffmpeg.org/doxygen/trunk/eval_8c.html#a7d21905c92ee5af0bb529d2daf8cb7c3"><code class="code">av_strtod</code></a>.
  404. It supports the prefix <code class="code">0x</code> to write a value with hexadecimal
  405. digits, and
  406. <a class="uref" href="https://ffmpeg.org/ffmpeg-utils.html#:~:text=The%20evaluator%20also%20recognizes%20the%20International%20System%20unit%20prefixes">many
  407. units</a> (like <code class="code">K</code> or <code class="code">GiB</code>).
  408. </p>
  409. <p>In the next example, all literals represent the same value:
  410. </p>
  411. <div class="example">
  412. <pre class="example-preformatted">10000
  413. 1e4
  414. 10K
  415. 0x2710
  416. </pre></div>
  417. <a name="Expressions"></a>
  418. <h4 class="subsection">2.4.2 Expressions<span class="pull-right"><a class="anchor hidden-xs" href="#Expressions" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Expressions" aria-hidden="true">TOC</a></span></h4>
  419. <p><a data-manual="ffmpeg-utils" href="ffmpeg-utils.html#Expression-Evaluation">FFmpeg expressions</a> can be used as arguments for any command that expects a numeric
  420. argument. The expression must be enclosed in parenthesis.
  421. </p>
  422. <table class="cartouche" border="1"><tr><th>
  423. Example</th></tr><tr><td>
  424. <p>The variables <var class="var">w</var> and <var class="var">h</var> represent the width and height of the
  425. frame. We can compute the center of the frame by dividing them by <code class="code">2</code>:
  426. </p>
  427. <div class="example">
  428. <pre class="example-preformatted">M (w / 2) (h / 2)
  429. </pre></div>
  430. <p>They can also contain parenthesis (to group operations, to call functions,
  431. etc):
  432. </p>
  433. <div class="example">
  434. <pre class="example-preformatted">moveto
  435. ((w + 10) / 2) // x
  436. (h / (2 * cos(t))) // y
  437. </pre></div>
  438. </td></tr></table>
  439. <p>The variables <var class="var">n</var> and <var class="var">t</var> can be used to compute a value that changes
  440. over time.
  441. </p>
  442. <table class="cartouche" border="1"><tr><th>
  443. Example</th></tr><tr><td>
  444. <p>To draw a circle oscillating from left to right, we can use an
  445. expression based on <code class="code">sin(t)</code> for the <code class="code">x</code> coordinate:
  446. </p>
  447. <div class="example">
  448. <pre class="example-preformatted">circle
  449. (w / 2 + sin(2 * t) * w / 4) // x
  450. (h / 2) // y
  451. (w / 5) // radius
  452. stroke
  453. </pre></div>
  454. </td></tr></table>
  455. <p>Expressions can be split in multiple lines, but they can&rsquo;t contain
  456. comments within them.
  457. </p>
  458. <div class="example">
  459. <pre class="example-preformatted">moveto // This is a comment.
  460. (w // This is part of the expression, not a comment.
  461. + h)
  462. </pre></div>
  463. <a name="Variable-Names"></a>
  464. <h4 class="subsection">2.4.3 Variable Names<span class="pull-right"><a class="anchor hidden-xs" href="#Variable-Names" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Variable-Names" aria-hidden="true">TOC</a></span></h4>
  465. <p>When an expression is only a reference to a variable, the parenthesis
  466. can be omitted, and the item is just the variable name.
  467. </p>
  468. <table class="cartouche" border="1"><tr><th>
  469. Example</th></tr><tr><td>
  470. <p>The next 3 expressions are equivalent: in all cases, they create a
  471. rectangle covering the whole frame.
  472. </p>
  473. <div class="example">
  474. <pre class="example-preformatted">rect (0) (0) (w) (h)
  475. rect 0 0 w h
  476. rect (0) 0 (w) h
  477. </pre></div>
  478. </td></tr></table>
  479. <p>It is possible to create a variable with the same name of a command, and
  480. then use it as an argument. In the previous example, the item <var class="var">h</var> is a
  481. reference to a variable (frame height), but in other contexts it may be
  482. a command (<a class="ref" href="#cmd_005fh"><code class="code">h</code></a>).
  483. </p>
  484. <p>For <a class="ref" href="#implicit-commands">implicit commands</a>, the parser prioritizes
  485. commands over variable names when it has to determine if the command is
  486. reused.
  487. </p>
  488. <table class="cartouche" border="1"><tr><th>
  489. Example</th></tr><tr><td>
  490. <p>In this example, the variable <var class="var">c</var> is used as the first argument in two
  491. calls to <a class="ref" href="#cmd_005fl"><code class="code">l</code></a>. However, only the first one is valid, because in the
  492. second call the parser recognizes <a class="ref" href="#cmd_005fc"><code class="code">c</code></a> as a command.
  493. </p>
  494. <div class="example">
  495. <pre class="example-preformatted">setvar c 5
  496. l c 10 c 15
  497. </pre></div>
  498. <p>This issue can be fixed by surrounding the start of the second call with
  499. parenthesis:
  500. </p>
  501. <div class="example">
  502. <pre class="example-preformatted">setvar c 5
  503. l c 10 (c) 15
  504. </pre></div>
  505. </td></tr></table>
  506. <a class="anchor" id="Colors"></a><a name="Colors-1"></a>
  507. <h4 class="subsection">2.4.4 Colors<span class="pull-right"><a class="anchor hidden-xs" href="#Colors-1" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Colors-1" aria-hidden="true">TOC</a></span></h4>
  508. <p>The color to stroke and to fill paths can be set with <a class="ref" href="#cmd_005fsetcolor"><code class="code">setcolor</code></a>.
  509. Its argument has the same syntax for colors in FFmpeg:
  510. </p>
  511. <ul class="itemize mark-bullet">
  512. <li>A <a data-manual="ffmpeg-utils" href="ffmpeg-utils.html#Color">predefined color name</a>.
  513. </li><li>In <code class="code">#RRGGBB</code> format.
  514. </li><li>Optionally, an <code class="code">@a</code> suffix can be added to set the alpha value,
  515. where <code class="code">a</code> is a number between <code class="code">0</code> and <code class="code">1</code>.
  516. </li></ul>
  517. <div class="example">
  518. <pre class="example-preformatted">circle 70 70 60
  519. setcolor #FF0000
  520. fill
  521. circle 170 170 60
  522. setcolor blue@0.5
  523. fill
  524. </pre></div>
  525. <p>The color can be a variable name. In that case, it must be assigned with
  526. <a class="ref" href="#cmd_005fdefrgba"><code class="code">defrgba</code></a>, <a class="ref" href="#cmd_005fdefhsla"><code class="code">defhsla</code></a>, or <a class="ref" href="#cmd_005fsetvar"><code class="code">setvar</code></a> and a color.
  527. </p>
  528. <div class="example">
  529. <pre class="example-preformatted">circle 70 70 60
  530. setvar CustomGreen #22FF44
  531. setcolor CustomGreen
  532. fill
  533. circle 170 170 60
  534. defhsla CustomBlue 200 0.7 0.5 1
  535. setcolor CustomBlue
  536. fill
  537. </pre></div>
  538. <p>The commands <a class="ref" href="#cmd_005fsetrgba"><code class="code">setrgba</code></a> and <a class="ref" href="#cmd_005fsethsla"><code class="code">sethsla</code></a> allow setting colors using
  539. expressions. Similar to <a class="ref" href="#cmd_005fdefrgba"><code class="code">defrgba</code></a> and <a class="ref" href="#cmd_005fdefhsla"><code class="code">defhsla</code></a>, but with no
  540. intermediate variable.
  541. </p>
  542. <a name="Constants"></a>
  543. <h4 class="subsection">2.4.5 Constants<span class="pull-right"><a class="anchor hidden-xs" href="#Constants" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Constants" aria-hidden="true">TOC</a></span></h4>
  544. <p>The argument for <a class="ref" href="#cmd_005fsetlinecap"><code class="code">setlinecap</code></a> and <a class="ref" href="#cmd_005fsetlinejoin"><code class="code">setlinejoin</code></a> is an
  545. identifier referring to a constant value.
  546. </p>
  547. <div class="example">
  548. <pre class="example-preformatted">setlinecap round
  549. </pre></div>
  550. <a name="Guide"></a>
  551. <h2 class="chapter">3 Guide<span class="pull-right"><a class="anchor hidden-xs" href="#Guide" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Guide" aria-hidden="true">TOC</a></span></h2>
  552. <a name="Paths"></a>
  553. <h3 class="section">3.1 Paths<span class="pull-right"><a class="anchor hidden-xs" href="#Paths" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Paths" aria-hidden="true">TOC</a></span></h3>
  554. <p>A path is a complex shape, composed by lines and curves, that can be
  555. used to fill a region, to stroke an outline, or to establish a clip
  556. region.
  557. </p>
  558. <p>In order to draw anything on top of a video frame, first we have to
  559. define a path, and then use <a class="ref" href="#cmd_005fstroke"><code class="code">stroke</code></a> or <a class="ref" href="#cmd_005ffill"><code class="code">fill</code></a>.
  560. </p>
  561. <p>The
  562. <a class="uref" href="https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorials/SVG_from_scratch/Paths">tutorial
  563. on paths in MDN</a> is a good introduction to the topic. It is focused on
  564. <a class="uref" href="https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Element/path">SVG&rsquo;s <code class="code">&lt;path&gt;</code></a>, but the same concepts can be applied in drawvg.
  565. </p>
  566. <a class="anchor" id="current-point"></a><a name="Current-Point"></a>
  567. <h4 class="subsection">3.1.1 Current Point<span class="pull-right"><a class="anchor hidden-xs" href="#Current-Point" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Current-Point" aria-hidden="true">TOC</a></span></h4>
  568. <p>Some commands require a <em class="emph">current point</em>. Initially, the
  569. <em class="emph">current point</em> is set to
  570. <a class="uref" href="https://en.wikipedia.org/wiki/NaN"><code class="code">NaN</code></a>. It is initialized
  571. with <a class="ref" href="#cmd_005fM"><code class="code">M</code></a> or <a class="ref" href="#cmd_005fmoveto"><code class="code">moveto</code></a>. Other commands, like <a class="ref" href="#cmd_005flineto"><code class="code">lineto</code></a> or
  572. <a class="ref" href="#cmd_005fcurveto"><code class="code">curveto</code></a>, updates the <em class="emph">current point</em> to the new end of the
  573. shape.
  574. </p>
  575. <p>The <em class="emph">current point</em> can be cleared with <a class="ref" href="#cmd_005fnewpath"><code class="code">newpath</code></a>. Commands
  576. that clear the path, like <a class="ref" href="#cmd_005fstroke"><code class="code">stroke</code></a> or <a class="ref" href="#cmd_005ffill"><code class="code">fill</code></a>, also clear the
  577. <em class="emph">current point</em>.
  578. </p>
  579. <table class="cartouche" border="1"><tr><th>
  580. Example</th></tr><tr><td>
  581. <p><a class="ref" href="#cmd_005frlineto"><code class="code">rlineto</code></a> uses coordinates relative to the <em class="emph">current point</em>.
  582. </p>
  583. <p>Given this script:
  584. </p>
  585. <div class="example">
  586. <pre class="example-preformatted">moveto 20 100
  587. rlineto 150 -90
  588. rlineto -50 200
  589. closepath
  590. stroke
  591. </pre></div>
  592. <p>These are the coordinates of the <em class="emph">current point</em> after executing
  593. each command:
  594. </p>
  595. <table class="multitable">
  596. <thead><tr><th width="50%">Command</th><th width="50%">Current Point</th></tr></thead>
  597. <tbody><tr><td width="50%"><code class="code">moveto 20 100</code></td><td width="50%"><code class="code">20, 100</code></td></tr>
  598. <tr><td width="50%"><code class="code">rlineto 150 -90</code></td><td width="50%"><code class="code">170, 10</code></td></tr>
  599. <tr><td width="50%"><code class="code">rlineto -10 50</code></td><td width="50%"><code class="code">140, 210</code></td></tr>
  600. <tr><td width="50%"><code class="code">closepath</code></td><td width="50%"><code class="code">20, 100</code></td></tr>
  601. </tbody>
  602. </table>
  603. <p>The same script can be written with single-letter aliases:
  604. </p>
  605. <div class="example">
  606. <pre class="example-preformatted">M 20 100 l 150 -90 -50 200 z stroke
  607. </pre></div>
  608. </td></tr></table>
  609. <a name="Defining-a-Shape"></a>
  610. <h4 class="subsection">3.1.2 Defining a Shape<span class="pull-right"><a class="anchor hidden-xs" href="#Defining-a-Shape" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Defining-a-Shape" aria-hidden="true">TOC</a></span></h4>
  611. <p>A path is defined by adding lines, curves, or basic shapes.
  612. </p>
  613. <ul class="itemize mark-bullet">
  614. <li>Basic shapes
  615. <ul class="itemize mark-bullet">
  616. <li><a class="ref" href="#cmd_005fcircle"><code class="code">circle</code></a>
  617. </li><li><a class="ref" href="#cmd_005fellipse"><code class="code">ellipse</code></a>
  618. </li><li><a class="ref" href="#cmd_005frect"><code class="code">rect</code></a>
  619. </li><li><a class="ref" href="#cmd_005froundedrect"><code class="code">roundedrect</code></a>
  620. </li></ul>
  621. </li><li>Lines
  622. <ul class="itemize mark-bullet">
  623. <li><a class="ref" href="#cmd_005fM"><code class="code">M</code></a>, <a class="ref" href="#cmd_005fmoveto"><code class="code">moveto</code></a>
  624. </li><li><a class="ref" href="#cmd_005fm"><code class="code">m</code></a>, <a class="ref" href="#cmd_005frmoveto"><code class="code">rmoveto</code></a>
  625. </li><li><a class="ref" href="#cmd_005fH"><code class="code">H</code></a>, <a class="ref" href="#cmd_005fh"><code class="code">h</code></a>
  626. </li><li><a class="ref" href="#cmd_005fV"><code class="code">V</code></a>, <a class="ref" href="#cmd_005fv"><code class="code">v</code></a>
  627. </li><li><a class="ref" href="#cmd_005fL"><code class="code">L</code></a>, <a class="ref" href="#cmd_005flineto"><code class="code">lineto</code></a>
  628. </li><li><a class="ref" href="#cmd_005fl"><code class="code">l</code></a>, <a class="ref" href="#cmd_005frlineto"><code class="code">rlineto</code></a>
  629. </li><li><a class="ref" href="#cmd_005fZ"><code class="code">Z</code></a>, <a class="ref" href="#cmd_005fz"><code class="code">z</code></a>, <a class="ref" href="#cmd_005fclosepath"><code class="code">closepath</code></a>
  630. </li></ul>
  631. </li><li>Curves
  632. <ul class="itemize mark-bullet">
  633. <li><a class="ref" href="#cmd_005farc"><code class="code">arc</code></a>, <a class="ref" href="#cmd_005farcn"><code class="code">arcn</code></a>
  634. </li><li><a class="ref" href="#cmd_005fC"><code class="code">C</code></a>, <a class="ref" href="#cmd_005fcurveto"><code class="code">curveto</code></a>,
  635. </li><li><a class="ref" href="#cmd_005fc"><code class="code">c</code></a>, <a class="ref" href="#cmd_005frcurveto"><code class="code">rcurveto</code></a>
  636. </li><li><a class="ref" href="#cmd_005fQ"><code class="code">Q</code></a>, <a class="ref" href="#cmd_005fq"><code class="code">q</code></a>
  637. </li><li><a class="ref" href="#cmd_005fS"><code class="code">S</code></a>, <a class="ref" href="#cmd_005fs"><code class="code">s</code></a>
  638. </li><li><a class="ref" href="#cmd_005fT"><code class="code">T</code></a>, <a class="ref" href="#cmd_005ft"><code class="code">t</code></a>
  639. </li></ul>
  640. </li></ul>
  641. <p>Single-letter commands are taken from <a class="uref" href="https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Element/path">SVG&rsquo;s <code class="code">&lt;path&gt;</code></a>.
  642. </p>
  643. <a class="anchor" id="fill-rules"></a><a name="Fill"></a>
  644. <h4 class="subsection">3.1.3 Fill<span class="pull-right"><a class="anchor hidden-xs" href="#Fill" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Fill" aria-hidden="true">TOC</a></span></h4>
  645. <p>The region within the shape defined by a path can be filled with
  646. <a class="ref" href="#cmd_005ffill"><code class="code">fill</code></a> or <a class="ref" href="#cmd_005feofill"><code class="code">eofill</code></a>. Each command uses a different
  647. <a class="uref" href="https://www.cairographics.org/manual/cairo-cairo-t.html#cairo-fill-rule-t">fill
  648. rule</a>:
  649. </p>
  650. <ul class="itemize mark-bullet">
  651. <li><a class="ref" href="#cmd_005ffill"><code class="code">fill</code></a> uses the
  652. <a class="uref" href="https://www.cairographics.org/manual/cairo-cairo-t.html#CAIRO-FILL-RULE-WINDING:CAPS">winding
  653. rule</a>, also known as
  654. <a class="uref" href="https://en.wikipedia.org/wiki/Nonzero-rule">nonzero rule</a>.
  655. </li><li><a class="ref" href="#cmd_005feofill"><code class="code">eofill</code></a> uses the
  656. <a class="uref" href="https://www.cairographics.org/manual/cairo-cairo-t.html#CAIRO-FILL-RULE-EVEN-ODD:CAPS">even&ndash;odd
  657. rule</a>.
  658. </li></ul>
  659. <table class="cartouche" border="1"><tr><th>
  660. Example</th></tr><tr><td>
  661. <p>This script shows the difference between the
  662. <a class="uref" href="https://www.cairographics.org/manual/cairo-cairo-t.html#CAIRO-FILL-RULE-WINDING:CAPS">winding</a>
  663. and
  664. <a class="uref" href="https://www.cairographics.org/manual/cairo-cairo-t.html#CAIRO-FILL-RULE-EVEN-ODD:CAPS">even&ndash;odd</a>
  665. rules:
  666. </p>
  667. <div class="example">
  668. <pre class="example-preformatted">rect 50 10 100 60
  669. circle 150 70 40
  670. setcolor seagreen
  671. fill
  672. rect 50 130 100 60
  673. circle 150 190 40
  674. setcolor skyblue
  675. eofill
  676. </pre></div>
  677. </td></tr></table>
  678. <a name="Stroke"></a>
  679. <h4 class="subsection">3.1.4 Stroke<span class="pull-right"><a class="anchor hidden-xs" href="#Stroke" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Stroke" aria-hidden="true">TOC</a></span></h4>
  680. <p><a class="ref" href="#cmd_005fstroke"><code class="code">stroke</code></a> draws a line around the shape defined by the path. The
  681. stroke can be configured with different commands:
  682. </p>
  683. <ul class="itemize mark-bullet">
  684. <li><a class="ref" href="#cmd_005fsetdash"><code class="code">setdash</code></a>
  685. </li><li><a class="ref" href="#cmd_005fsetdashoffset"><code class="code">setdashoffset</code></a>
  686. </li><li><a class="ref" href="#cmd_005fsetlinecap"><code class="code">setlinecap</code></a>
  687. </li><li><a class="ref" href="#cmd_005fsetlinejoin"><code class="code">setlinejoin</code></a>
  688. </li><li><a class="ref" href="#cmd_005fsetlinewidth"><code class="code">setlinewidth</code></a>
  689. </li><li><a class="ref" href="#cmd_005fresetdash"><code class="code">resetdash</code></a>
  690. </li></ul>
  691. <table class="cartouche" border="1"><tr><th>
  692. Example</th></tr><tr><td>
  693. <p>This example use <a class="ref" href="#cmd_005fsetdashoffset"><code class="code">setdashoffset</code></a> to animate the stroke:
  694. </p>
  695. <div class="example">
  696. <pre class="example-preformatted">moveto 0 0
  697. lineto w h
  698. setlinecap round
  699. setdash 50 50
  700. setlinewidth 20
  701. setdashoffset (hypot(w, h) * t / -3)
  702. setcolor seagreen
  703. stroke
  704. </pre></div>
  705. </td></tr></table>
  706. <a name="Clip"></a>
  707. <h4 class="subsection">3.1.5 Clip<span class="pull-right"><a class="anchor hidden-xs" href="#Clip" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Clip" aria-hidden="true">TOC</a></span></h4>
  708. <p>A <a class="uref" href="https://en.wikipedia.org/wiki/Clipping_(computer_graphics)">clip
  709. region</a> can be established with <a class="ref" href="#cmd_005fclip"><code class="code">clip</code></a> and <a class="ref" href="#cmd_005feoclip"><code class="code">eoclip</code></a>.
  710. </p>
  711. <p>If there is an active clip region, the new clip region will be the
  712. intersection between the existing one and the path. <a class="ref" href="#cmd_005fresetclip"><code class="code">resetclip</code></a>
  713. reset the clip region to the whole frame.
  714. </p>
  715. <p><a class="ref" href="#cmd_005feoclip"><code class="code">eoclip</code></a> uses the
  716. <a class="uref" href="https://www.cairographics.org/manual/cairo-cairo-t.html#CAIRO-FILL-RULE-EVEN-ODD:CAPS">even&ndash;odd
  717. rule</a> to compute the clip region.
  718. </p>
  719. <table class="cartouche" border="1"><tr><th>
  720. Example</th></tr><tr><td>
  721. <div class="example">
  722. <pre class="example-preformatted">rect 50 50 100 200
  723. clip
  724. circle 30 30 150
  725. setcolor seagreen
  726. fill
  727. // Draw outside the clip region.
  728. resetclip
  729. circle 30 30 150
  730. setlinewidth 3
  731. setcolor skyblue
  732. stroke
  733. </pre></div>
  734. </td></tr></table>
  735. <a name="Preserving-Paths"></a>
  736. <h4 class="subsection">3.1.6 Preserving Paths<span class="pull-right"><a class="anchor hidden-xs" href="#Preserving-Paths" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Preserving-Paths" aria-hidden="true">TOC</a></span></h4>
  737. <p>The path is cleared after any operation on it, like <a class="ref" href="#cmd_005ffill"><code class="code">fill</code></a> or
  738. <a class="ref" href="#cmd_005fstroke"><code class="code">stroke</code></a>. To reuse the same path in multiple operations,
  739. <a class="ref" href="#cmd_005fpreserve"><code class="code">preserve</code></a> must be called before them.
  740. </p>
  741. <table class="cartouche" border="1"><tr><th>
  742. Example</th></tr><tr><td>
  743. <p>In this example, each path is used twice.
  744. </p>
  745. <div class="example">
  746. <pre class="example-preformatted">circle 120 120 50
  747. setcolor seagreen
  748. preserve stroke
  749. clip
  750. circle 100 100 50
  751. setcolor skyblue
  752. preserve fill
  753. setcolor tomato
  754. stroke
  755. </pre></div>
  756. </td></tr></table>
  757. <a name="Variables"></a>
  758. <h3 class="section">3.2 Variables<span class="pull-right"><a class="anchor hidden-xs" href="#Variables" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Variables" aria-hidden="true">TOC</a></span></h3>
  759. <p>A drawvg can use some variables, provided by the interpreter, to compute
  760. values in <a data-manual="ffmpeg-utils" href="ffmpeg-utils.html#Expression-Evaluation">FFmpeg expressions</a>:
  761. </p>
  762. <dl class="table">
  763. <dt><var class="var">cx</var></dt>
  764. <dd><p>X coordinate of the <a class="ref" href="#current-point">current point</a>.
  765. </p>
  766. </dd>
  767. <dt><var class="var">cy</var></dt>
  768. <dd><p>Y coordinate of the <a class="ref" href="#current-point">current point</a>.
  769. </p>
  770. </dd>
  771. <dt><var class="var">w</var></dt>
  772. <dd><p>Width, in pixels, of the frame.
  773. </p>
  774. </dd>
  775. <dt><var class="var">h</var></dt>
  776. <dd><p>Height, in pixels, of the frame.
  777. </p>
  778. </dd>
  779. <dt><var class="var">i</var></dt>
  780. <dd><p>The loop counter in repeat blocks.
  781. </p>
  782. </dd>
  783. <dt><var class="var">n</var></dt>
  784. <dd><p>Frame number.
  785. </p>
  786. </dd>
  787. <dt><var class="var">t</var></dt>
  788. <dd><p>Timestamp, in seconds.
  789. </p>
  790. </dd>
  791. <dt><var class="var">ts</var></dt>
  792. <dd><p>Timestamp, in seconds, of the first frame.
  793. </p>
  794. </dd>
  795. <dt><var class="var">duration</var></dt>
  796. <dd><p>Duration, in seconds, of the frame.
  797. </p></dd>
  798. </dl>
  799. <a class="anchor" id="User-Variables"></a><a name="User-Variables-1"></a>
  800. <h4 class="subsection">3.2.1 User Variables<span class="pull-right"><a class="anchor hidden-xs" href="#User-Variables-1" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-User-Variables-1" aria-hidden="true">TOC</a></span></h4>
  801. <p>New variables can be created with the <a class="ref" href="#cmd_005fsetvar"><code class="code">setvar</code></a> command. It
  802. associates a name with a numeric value.
  803. </p>
  804. <p>The name must follow these rules:
  805. </p>
  806. <ul class="itemize mark-bullet">
  807. <li>It must start with an ASCII letter or an underscore (<code class="code">_</code>).
  808. </li><li>It can contain only ASCII letters, underscores, and digits.
  809. </li><li>It must not match the name of a variable provided by the interpreter
  810. (like <var class="var">w</var> or <var class="var">t</var>).
  811. </li></ul>
  812. <p>The same variable can be assigned multiple times.
  813. </p>
  814. <table class="cartouche" border="1"><tr><th>
  815. Example</th></tr><tr><td>
  816. <p>In this example, the result of an expression is stored in a variable
  817. with the name <var class="var">progress</var>. Then, it is used for the <var class="var">x</var> and
  818. <var class="var">width</var> arguments of <a class="ref" href="#cmd_005frect"><code class="code">rect</code></a>.
  819. </p>
  820. <div class="example">
  821. <pre class="example-preformatted">setvar progress (w * (pow(mod(t / 2 + 0.5, 1), 2.5)))
  822. rect ((w - progress) / 2) 0 progress h
  823. setcolor darkblue
  824. fill
  825. </pre></div>
  826. </td></tr></table>
  827. <p>Currently, a script can contain only 20 different variable names, but
  828. this limit can be modified in the future.
  829. </p>
  830. <a class="anchor" id="current-pattern"></a><a name="Patterns"></a>
  831. <h3 class="section">3.3 Patterns<span class="pull-right"><a class="anchor hidden-xs" href="#Patterns" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Patterns" aria-hidden="true">TOC</a></span></h3>
  832. <p>The pattern for fill and stroke operations can be either a solid color,
  833. or a gradient.
  834. </p>
  835. <ul class="itemize mark-bullet">
  836. <li>Solid colors.
  837. <ul class="itemize mark-bullet">
  838. <li><a class="ref" href="#cmd_005fsetcolor"><code class="code">setcolor</code></a>
  839. </li><li><a class="ref" href="#cmd_005fsethsla"><code class="code">sethsla</code></a>
  840. </li><li><a class="ref" href="#cmd_005fsetrgba"><code class="code">setrgba</code></a>
  841. </li></ul>
  842. </li><li>Gradients.
  843. <ul class="itemize mark-bullet">
  844. <li><a class="ref" href="#cmd_005flineargrad"><code class="code">lineargrad</code></a>
  845. </li><li><a class="ref" href="#cmd_005fradialgrad"><code class="code">radialgrad</code></a>
  846. </li></ul>
  847. </li></ul>
  848. <p>The pattern is not cleared after being used in a fill or stroke
  849. operation, but it is replaced by any command that sets a new pattern.
  850. </p>
  851. <a name="Gradients"></a>
  852. <h4 class="subsection">3.3.1 Gradients<span class="pull-right"><a class="anchor hidden-xs" href="#Gradients" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Gradients" aria-hidden="true">TOC</a></span></h4>
  853. <p>To configure a gradient, first call to <a class="ref" href="#cmd_005flineargrad"><code class="code">lineargrad</code></a> or
  854. <a class="ref" href="#cmd_005fradialgrad"><code class="code">radialgrad</code></a>, and then add color stops by calling <a class="ref" href="#cmd_005fcolorstop"><code class="code">colorstop</code></a>
  855. for each stop.
  856. </p>
  857. <table class="cartouche" border="1"><tr><th>
  858. Example</th></tr><tr><td>
  859. <p>In this example, the whole frame is filled with a linear gradient:
  860. </p>
  861. <div class="example">
  862. <pre class="example-preformatted">lineargrad 0 0 w h
  863. colorstop 0 skyblue
  864. colorstop 1 darkblue
  865. rect 0 0 w h
  866. fill
  867. </pre></div>
  868. <p>In this example, a radial gradient is used to simulate a sphere:
  869. </p>
  870. <div class="example">
  871. <pre class="example-preformatted">radialgrad 90 90 5 120 120 100
  872. colorstop 0.0 #90DDFF
  873. colorstop 0.9 #000030
  874. colorstop 1.0 #000000
  875. rect 0 0 w h
  876. fill
  877. </pre></div>
  878. </td></tr></table>
  879. <a name="Variables-1"></a>
  880. <h4 class="subsection">3.3.2 Variables<span class="pull-right"><a class="anchor hidden-xs" href="#Variables-1" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Variables-1" aria-hidden="true">TOC</a></span></h4>
  881. <p><a class="ref" href="#cmd_005fsetcolor"><code class="code">setcolor</code></a> and <a class="ref" href="#cmd_005fcolorstop"><code class="code">colorstop</code></a> accept a variable name as the
  882. argument. The variable must be assigned with <a class="ref" href="#cmd_005fdefrgba"><code class="code">defrgba</code></a>,
  883. <a class="ref" href="#cmd_005fdefhsla"><code class="code">defhsla</code></a>, or <a class="ref" href="#cmd_005fsetvar"><code class="code">setvar</code></a> and a color.
  884. </p>
  885. <table class="cartouche" border="1"><tr><th>
  886. Example</th></tr><tr><td>
  887. <div class="example">
  888. <pre class="example-preformatted">// Use color #1020FF, alpha = 50%
  889. setvar someblue #1020FF@0.5
  890. setcolor someblue
  891. rect 30 30 120 120
  892. fill
  893. rect 90 90 120 120
  894. fill
  895. </pre></div>
  896. </td></tr></table>
  897. <p>If a variable has the same name of a <a data-manual="ffmpeg-utils" href="ffmpeg-utils.html#Color">known color</a>, the
  898. variable has preference, and will be used instead of the predefined color.
  899. </p>
  900. <table class="cartouche" border="1"><tr><th>
  901. Example</th></tr><tr><td>
  902. <div class="example">
  903. <pre class="example-preformatted">setcolor teal
  904. rect 30 30 120 120
  905. fill
  906. setvar teal #70AAAA
  907. setcolor teal // Use the new color for `teal`.
  908. rect 90 90 120 120
  909. fill
  910. </pre></div>
  911. </td></tr></table>
  912. <p><a class="ref" href="#cmd_005fdefrgba"><code class="code">defrgba</code></a> and <a class="ref" href="#cmd_005fdefhsla"><code class="code">defhsla</code></a> assign a color to a variable, by providing
  913. an expression for each color component:
  914. </p>
  915. <ul class="itemize mark-bullet">
  916. <li>For <a class="ref" href="#cmd_005fdefrgba"><code class="code">defrgba</code></a>: <em class="emph">red</em>, <em class="emph">green</em>, <em class="emph">blue</em>, and
  917. <em class="emph">alpha</em>.
  918. </li><li>For <a class="ref" href="#cmd_005fdefhsla"><code class="code">defhsla</code></a>: <em class="emph">hue</em>, <em class="emph">saturation</em>, <em class="emph">lightness</em>, and
  919. <em class="emph">alpha</em>.
  920. </li></ul>
  921. <p>Each color component must be in range <code class="code">0</code> to <code class="code">1</code>, except
  922. <em class="emph">hue</em>, which is <code class="code">0</code> to <code class="code">360</code>.
  923. </p>
  924. <table class="cartouche" border="1"><tr><th>
  925. Example</th></tr><tr><td>
  926. <div class="example">
  927. <pre class="example-preformatted">defrgba colorA 1 0.5 0.25 1 // colorA = RGB(255, 127, 63)
  928. defhsla colorB 200 0.75 0.25 1 // colorB = HSL(200, 75%, 25%)
  929. rect 0 0 (w / 2) h
  930. setcolor colorA
  931. fill
  932. rect (w / 2) 0 (w / 2) h
  933. setcolor colorB
  934. fill
  935. </pre></div>
  936. </td></tr></table>
  937. <a class="anchor" id="transformation-matrix"></a><a name="Transformations"></a>
  938. <h3 class="section">3.4 Transformations<span class="pull-right"><a class="anchor hidden-xs" href="#Transformations" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Transformations" aria-hidden="true">TOC</a></span></h3>
  939. <p>The coordinates for each command can be scaled, rotated, and translated,
  940. by using the following commands:
  941. </p>
  942. <ul class="itemize mark-bullet">
  943. <li><a class="ref" href="#cmd_005frotate"><code class="code">rotate</code></a>
  944. </li><li><a class="ref" href="#cmd_005fscale"><code class="code">scale</code></a>
  945. </li><li><a class="ref" href="#cmd_005fscalexy"><code class="code">scalexy</code></a>
  946. </li><li><a class="ref" href="#cmd_005ftranslate"><code class="code">translate</code></a>
  947. </li></ul>
  948. <p>The transformations are applied when the command is executed. They have
  949. no effect on the existing path, only on the new segments added to it.
  950. </p>
  951. <p>They are done by updating the
  952. <a class="uref" href="https://www.cairographics.org/manual/cairo-Transformations.html">current
  953. transformation matrix</a> in the Cairo context. To reset the matrix to its
  954. original state, before any transformation, use <a class="ref" href="#cmd_005fresetmatrix"><code class="code">resetmatrix</code></a>.
  955. </p>
  956. <p>The transform origin for scale and rotation is initially at <code class="code">0, 0</code>,
  957. but it can be adjusted with <a class="ref" href="#cmd_005ftranslate"><code class="code">translate</code></a>.
  958. </p>
  959. <table class="cartouche" border="1"><tr><th>
  960. Example</th></tr><tr><td>
  961. <div class="example">
  962. <pre class="example-preformatted">// Map (0, 0) as the center of the frame.
  963. translate (w / 2) (h / 2)
  964. // Scale the space as if the frame is 1x1 pixel.
  965. scalexy w h
  966. // Draw multiple lines with the same arguments,
  967. // but each one on a different rotation.
  968. repeat 10 {
  969. rotate (PI / 10)
  970. M -0.25 0
  971. H 0.25
  972. }
  973. // Reset transformations, so the scale does not
  974. // affect stroke.
  975. resetmatrix
  976. stroke
  977. </pre></div>
  978. </td></tr></table>
  979. <a class="anchor" id="State-Stack"></a><a name="State-Stack-1"></a>
  980. <h3 class="section">3.5 State Stack<span class="pull-right"><a class="anchor hidden-xs" href="#State-Stack-1" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-State-Stack-1" aria-hidden="true">TOC</a></span></h3>
  981. <p>The state of a drawvg script contains all parameters used for drawing
  982. operations, like the current color, the transformation matrix, the
  983. stroke configuration, etc.
  984. </p>
  985. <p>The <a class="ref" href="#cmd_005fsave"><code class="code">save</code></a> command pushes a snapshot of the state to an internal
  986. stack. Later, <a class="ref" href="#cmd_005frestore"><code class="code">restore</code></a> pops the latest snapshot from the stack,
  987. and uses it as the new state.
  988. </p>
  989. <p>The parameters that can be saved and restored are:
  990. </p>
  991. <ul class="itemize mark-bullet">
  992. <li>Pattern for stroke and fill operations.
  993. <ul class="itemize mark-bullet">
  994. <li><a class="ref" href="#cmd_005flineargrad"><code class="code">lineargrad</code></a>
  995. </li><li><a class="ref" href="#cmd_005fradialgrad"><code class="code">radialgrad</code></a>
  996. </li><li><a class="ref" href="#cmd_005fsetrgba"><code class="code">setrgba</code></a>
  997. </li><li><a class="ref" href="#cmd_005fsetcolor"><code class="code">setcolor</code></a>
  998. </li><li><a class="ref" href="#cmd_005fsethsla"><code class="code">sethsla</code></a>
  999. </li></ul>
  1000. </li><li>Transformation matrix.
  1001. <ul class="itemize mark-bullet">
  1002. <li><a class="ref" href="#cmd_005fresetmatrix"><code class="code">resetmatrix</code></a>
  1003. </li><li><a class="ref" href="#cmd_005frotate"><code class="code">rotate</code></a>
  1004. </li><li><a class="ref" href="#cmd_005fscale"><code class="code">scale</code></a>
  1005. </li><li><a class="ref" href="#cmd_005fscalexy"><code class="code">scalexy</code></a>
  1006. </li><li><a class="ref" href="#cmd_005ftranslate"><code class="code">translate</code></a>
  1007. </li></ul>
  1008. </li><li>Stroke configuration.
  1009. <ul class="itemize mark-bullet">
  1010. <li><a class="ref" href="#cmd_005fsetdash"><code class="code">setdash</code></a>
  1011. </li><li><a class="ref" href="#cmd_005fsetdashoffset"><code class="code">setdashoffset</code></a>
  1012. </li><li><a class="ref" href="#cmd_005fsetlinecap"><code class="code">setlinecap</code></a>
  1013. </li><li><a class="ref" href="#cmd_005fsetlinejoin"><code class="code">setlinejoin</code></a>
  1014. </li><li><a class="ref" href="#cmd_005fsetlinewidth"><code class="code">setlinewidth</code></a>
  1015. </li></ul>
  1016. </li><li>Clip region
  1017. <ul class="itemize mark-bullet">
  1018. <li><a class="ref" href="#cmd_005fclip"><code class="code">clip</code></a>
  1019. </li><li><a class="ref" href="#cmd_005fresetclip"><code class="code">resetclip</code></a>
  1020. </li></ul>
  1021. </li></ul>
  1022. <a class="anchor" id="Frame-Metadata"></a><a name="Frame-Metadata-1"></a>
  1023. <h3 class="section">3.6 Frame Metadata<span class="pull-right"><a class="anchor hidden-xs" href="#Frame-Metadata-1" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Frame-Metadata-1" aria-hidden="true">TOC</a></span></h3>
  1024. <p>Some FFmpeg filters add metadata to frames. The command
  1025. <a class="ref" href="#cmd_005fgetmetadata"><code class="code">getmetadata</code></a> can read metadata items containing a numeric value,
  1026. and store it in a variable that can be used for command arguments.
  1027. </p>
  1028. <table class="cartouche" border="1"><tr><th>
  1029. Example</th></tr><tr><td>
  1030. <p>The <code class="code">cropdetect</code> filter computes the parameters to remove empty
  1031. regions around the video. These parameters are accessible in the
  1032. <code class="code">lavfi.cropdetect</code> keys of the frame metadata.
  1033. </p>
  1034. <div class="example">
  1035. <pre class="example-preformatted">// Get metadata from cropdetect filter and store it
  1036. // in `cd*` variables.
  1037. getmetadata cdx lavfi.cropdetect.x
  1038. getmetadata cdy lavfi.cropdetect.y
  1039. getmetadata cdw lavfi.cropdetect.w
  1040. getmetadata cdh lavfi.cropdetect.h
  1041. rect cdx cdy cdw cdh
  1042. setcolor yellow@0.5
  1043. setlinewidth 10
  1044. stroke
  1045. </pre></div>
  1046. <p>To test the script, copy it to a <code class="code">drawcropdetect.vgs</code> file, and
  1047. then execute a command like this:
  1048. </p>
  1049. <div class="example">
  1050. <pre class="example-preformatted">ffplay -i example-video.webm -vf 'cropdetect, drawvg=file=drawcropdetect.vgs'
  1051. </pre></div>
  1052. </td></tr></table>
  1053. <a name="if-_002f-repeat-Statements"></a>
  1054. <h3 class="section">3.7 <code class="code">if</code> / <code class="code">repeat</code> Statements<span class="pull-right"><a class="anchor hidden-xs" href="#if-_002f-repeat-Statements" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-if-_002f-repeat-Statements" aria-hidden="true">TOC</a></span></h3>
  1055. <p>There is limited support for control flow statements: only <a class="ref" href="#cmd_005fif"><code class="code">if</code></a> and
  1056. <a class="ref" href="#cmd_005frepeat"><code class="code">repeat</code></a>.
  1057. </p>
  1058. <p>Both commands receive two arguments: an expression and a block.
  1059. </p>
  1060. <div class="example">
  1061. <pre class="example-preformatted">if (condition) {
  1062. // commands
  1063. }
  1064. repeat (count) {
  1065. // commands
  1066. }
  1067. </pre></div>
  1068. <p><a class="ref" href="#cmd_005fif"><code class="code">if</code></a> executes its block if the result of <code class="code">(condition)</code> is not
  1069. zero.
  1070. </p>
  1071. <p><a class="ref" href="#cmd_005frepeat"><code class="code">repeat</code></a> executes its block the number of times specified by
  1072. <code class="code">(count)</code>. In each iteration, the variable <var class="var">i</var> is used as a
  1073. <a class="uref" href="https://en.wikipedia.org/wiki/For_loop#Loop_counters">loop
  1074. counter</a>.
  1075. </p>
  1076. <p>If the result of the expression is not a finite number (like
  1077. <a class="uref" href="https://en.wikipedia.org/wiki/NaN"><code class="code">NaN</code></a>) the block is not
  1078. executed.
  1079. </p>
  1080. <a class="anchor" id="comp_002doperators"></a><a name="Comparison-and-Logical-Operators"></a>
  1081. <h4 class="subsection">3.7.1 Comparison and Logical Operators<span class="pull-right"><a class="anchor hidden-xs" href="#Comparison-and-Logical-Operators" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Comparison-and-Logical-Operators" aria-hidden="true">TOC</a></span></h4>
  1082. <p><a data-manual="ffmpeg-utils" href="ffmpeg-utils.html#Expression-Evaluation">FFmpeg expressions</a> only supports arithmetic operators (like <code class="code">+</code> for addition).
  1083. Comparison operators (like <code class="code">!=</code>) are supported via functions, while
  1084. logical operators (like <code class="code">&amp;&amp;</code> for <code class="code">AND</code>) can be emulated with
  1085. arithmetic operations.
  1086. </p>
  1087. <table class="multitable">
  1088. <thead><tr><th width="50%">Expression</th><th width="50%">FFmpeg Equivalent</th></tr></thead>
  1089. <tbody><tr><td width="50%"><code class="code">x = y</code></td><td width="50%"><code class="code">eq(x, y)</code></td></tr>
  1090. <tr><td width="50%"><code class="code">x &lt; y</code></td><td width="50%"><code class="code">lt(x, y)</code></td></tr>
  1091. <tr><td width="50%"><code class="code">x &gt; y</code></td><td width="50%"><code class="code">gt(x, y)</code></td></tr>
  1092. <tr><td width="50%"><code class="code">x ≤ y</code></td><td width="50%"><code class="code">lte(x, y)</code></td></tr>
  1093. <tr><td width="50%"><code class="code">x ≥ y</code></td><td width="50%"><code class="code">gte(x, y)</code></td></tr>
  1094. <tr><td width="50%"><code class="code">a ≤ x ≤ b</code></td><td width="50%"><code class="code">between(x, a, b)</code></td></tr>
  1095. </tbody>
  1096. </table>
  1097. <p>Logical operators can be emulated with multiplication (for <code class="code">AND</code>),
  1098. or addition (for <code class="code">OR</code>):
  1099. </p>
  1100. <table class="multitable">
  1101. <thead><tr><th width="50%">Expression</th><th width="50%">FFmpeg Equivalent</th></tr></thead>
  1102. <tbody><tr><td width="50%"><code class="code">x OR y</code></td><td width="50%"><code class="code">x + y</code></td></tr>
  1103. <tr><td width="50%"><code class="code">x AND y</code></td><td width="50%"><code class="code">x * y</code></td></tr>
  1104. </tbody>
  1105. </table>
  1106. <table class="cartouche" border="1"><tr><th>
  1107. Example</th></tr><tr><td>
  1108. <p>In other programming languages, a code like this:
  1109. </p>
  1110. <div class="example">
  1111. <pre class="example-preformatted">if (x &gt; y &amp;&amp; z != 1) {
  1112. // …
  1113. }
  1114. </pre></div>
  1115. <p>Can be written for drawvg like this:
  1116. </p>
  1117. <div class="example">
  1118. <pre class="example-preformatted">if (gt(x, y) * not(eq(z, 1))) {
  1119. // …
  1120. }
  1121. </pre></div>
  1122. </td></tr></table>
  1123. <a name="Early-Exit"></a>
  1124. <h4 class="subsection">3.7.2 Early Exit<span class="pull-right"><a class="anchor hidden-xs" href="#Early-Exit" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Early-Exit" aria-hidden="true">TOC</a></span></h4>
  1125. <p><a class="ref" href="#cmd_005fbreak"><code class="code">break</code></a> causes a <a class="ref" href="#cmd_005frepeat"><code class="code">repeat</code></a> loop to be terminated immediately.
  1126. </p>
  1127. <p>If it is executed outside a <a class="ref" href="#cmd_005frepeat"><code class="code">repeat</code></a> block, it terminates the whole
  1128. script, or the current procedure.
  1129. </p>
  1130. <table class="cartouche" border="1"><tr><th>
  1131. Example</th></tr><tr><td>
  1132. <p>In this example, we are using the <a class="ref" href="#func_002drandomg"><code class="code">randomg</code></a> function
  1133. to draw a line with random segments.
  1134. </p>
  1135. <p>The loop can be executed <code class="code">500</code> times, but it is interrupted if the X
  1136. coordinate of the <a class="ref" href="#current-point">current point</a> (<var class="var">cx</var>) exceeds the frame width
  1137. (<var class="var">w</var>). The <a class="ref" href="#current-point">current point</a> is updated after each call to
  1138. <a class="ref" href="#cmd_005frlineto"><code class="code">rlineto</code></a>.
  1139. </p>
  1140. <div class="example">
  1141. <pre class="example-preformatted">moveto 0 0
  1142. repeat 500 {
  1143. rlineto
  1144. (randomg(0) * 15)
  1145. (randomg(0) * 20)
  1146. if (gt(cx, w)) {
  1147. break
  1148. }
  1149. }
  1150. stroke
  1151. </pre></div>
  1152. </td></tr></table>
  1153. <a class="anchor" id="Procedures"></a><a name="Procedures-1"></a>
  1154. <h3 class="section">3.8 Procedures<span class="pull-right"><a class="anchor hidden-xs" href="#Procedures-1" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Procedures-1" aria-hidden="true">TOC</a></span></h3>
  1155. <p>A procedure is a name associated with a block that can be executed
  1156. multiple times. It can take between 0 and 6 parameters.
  1157. </p>
  1158. <p><a class="ref" href="#cmd_005fproc"><code class="code">proc</code></a> is used to set the parameter names and the block for a
  1159. procedure:
  1160. </p>
  1161. <div class="example">
  1162. <pre class="example-preformatted">proc p0 {
  1163. // …
  1164. }
  1165. proc p1 param1 param2 {
  1166. // …
  1167. }
  1168. </pre></div>
  1169. <p>Inside the block, the arguments can be accessed as regular variables:
  1170. </p>
  1171. <div class="example">
  1172. <pre class="example-preformatted">proc square center_x center_y side {
  1173. rect
  1174. (center_x - side / 2) (center_y - side / 2)
  1175. side side
  1176. }
  1177. </pre></div>
  1178. <p><a class="ref" href="#cmd_005fcall"><code class="code">call</code></a> executes the block assigned to the procedure name. It
  1179. requires the name of the procedure, and the value for each parameter
  1180. defined in the call to <a class="ref" href="#cmd_005fproc"><code class="code">proc</code></a>.
  1181. </p>
  1182. <div class="example">
  1183. <pre class="example-preformatted">call p0
  1184. call p1 1 2
  1185. call square (w / 2) (h / 2) (w / t)
  1186. </pre></div>
  1187. <table class="cartouche" border="1"><tr><th>
  1188. Example</th></tr><tr><td>
  1189. <p>In this example, the procedure <code class="code">zigzag</code> draws multiple lines from
  1190. the <a class="ref" href="#current-point">current point</a>.
  1191. </p>
  1192. <div class="example">
  1193. <pre class="example-preformatted">setvar len (w / 10)
  1194. setlinewidth 5
  1195. proc zigzag {
  1196. repeat 10 {
  1197. l len len len (-len)
  1198. }
  1199. stroke
  1200. }
  1201. setcolor #40C0FF
  1202. M 0 60
  1203. call zigzag
  1204. setcolor #00AABB
  1205. M 0 120
  1206. call zigzag
  1207. setcolor #20F0B7
  1208. M 0 180
  1209. call zigzag
  1210. </pre></div>
  1211. <p>The color and the Y coordinate of the starting point can be sent as
  1212. procedure arguments:
  1213. </p>
  1214. <div class="example">
  1215. <pre class="example-preformatted">setvar len (w / 10)
  1216. setlinewidth 5
  1217. proc zigzag color y {
  1218. setcolor color
  1219. M 0 y
  1220. repeat 10 {
  1221. l len len len (-len)
  1222. }
  1223. stroke
  1224. }
  1225. call zigzag #40C0FF 60
  1226. call zigzag #00AABB 120
  1227. call zigzag #20F0B7 180
  1228. </pre></div>
  1229. </td></tr></table>
  1230. <p>When the procedure returns, the value of the variable for each argument
  1231. is restored to the value it had before calling the procedure. Changes in
  1232. other variables (with <a class="ref" href="#cmd_005fsetvar"><code class="code">setvar</code></a>, <a class="ref" href="#cmd_005fgetmetadata"><code class="code">getmetadata</code></a>, <a class="ref" href="#cmd_005fdefhsla"><code class="code">defhsla</code></a>,
  1233. and <a class="ref" href="#cmd_005fdefrgba"><code class="code">defrgba</code></a>) are preserved.
  1234. </p>
  1235. <table class="cartouche" border="1"><tr><th>
  1236. Example</th></tr><tr><td>
  1237. <p>In the next example, the variable <var class="var">A</var> has the value <code class="code">0</code> before
  1238. calling the procedure <var class="var">P</var>. During the execution of <var class="var">P</var>,
  1239. <code class="code">A</code> is <code class="code">1</code>, but after it, <var class="var">A</var> is <code class="code">0</code> again.
  1240. </p>
  1241. <div class="example">
  1242. <pre class="example-preformatted">setvar A 0
  1243. proc P A {
  1244. print A
  1245. }
  1246. print A
  1247. call P 1
  1248. print A
  1249. </pre></div>
  1250. <p>It writes the following messages:
  1251. </p>
  1252. <pre class="verbatim">[7:7] A = 0.000
  1253. [4:8] A = 1.000
  1254. [9:7] A = 0.000
  1255. </pre>
  1256. </td></tr></table>
  1257. <p><a class="ref" href="#cmd_005fbreak"><code class="code">break</code></a> causes the script to leave the current procedure, similar
  1258. to the
  1259. <a class="uref" href="https://en.wikipedia.org/wiki/Return_statement"><code class="code">return</code>
  1260. statement</a> in other programming languages, unless it is called within a
  1261. <a class="ref" href="#cmd_005frepeat"><code class="code">repeat</code></a> loop.
  1262. </p>
  1263. <p>The body of the procedure must be defined with <a class="ref" href="#cmd_005fproc"><code class="code">proc</code></a> <em class="emph">before</em>
  1264. using <a class="ref" href="#cmd_005fcall"><code class="code">call</code></a>.
  1265. </p>
  1266. <table class="cartouche" border="1"><tr><th>
  1267. Example</th></tr><tr><td>
  1268. <p>In this example, when the procedure <code class="code">notyet</code> is called, its body
  1269. has not yet defined, so the execution fails with the error
  1270. <code class="code">Missing body for procedure 'notyet'</code>.
  1271. </p>
  1272. <div class="example">
  1273. <pre class="example-preformatted">call notyet
  1274. proc notyet {
  1275. // ...
  1276. }
  1277. </pre></div>
  1278. </td></tr></table>
  1279. <p>A procedure can be redefined by other calls to <a class="ref" href="#cmd_005fproc"><code class="code">proc</code></a> with the same
  1280. name. In such case, <a class="ref" href="#cmd_005fcall"><code class="code">call</code></a> invokes the last assigned block.
  1281. </p>
  1282. <table class="cartouche" border="1"><tr><th>
  1283. Example</th></tr><tr><td>
  1284. <p>In this example, the procedure <code class="code">example</code> has two different blocks.
  1285. </p>
  1286. <div class="example">
  1287. <pre class="example-preformatted">proc example {
  1288. // block1
  1289. }
  1290. call example // executes block1
  1291. proc example {
  1292. // block2
  1293. }
  1294. call example // executes block2
  1295. </pre></div>
  1296. </td></tr></table>
  1297. <a name="Functions-in-Expressions"></a>
  1298. <h3 class="section">3.9 Functions in Expressions<span class="pull-right"><a class="anchor hidden-xs" href="#Functions-in-Expressions" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Functions-in-Expressions" aria-hidden="true">TOC</a></span></h3>
  1299. <p>There are some functions specific to drawvg available in <a data-manual="ffmpeg-utils" href="ffmpeg-utils.html#Expression-Evaluation">FFmpeg expressions</a>.
  1300. </p>
  1301. <a name="Function-p"></a>
  1302. <h4 class="subsection">3.9.1 Function <code class="code">p</code><span class="pull-right"><a class="anchor hidden-xs" href="#Function-p" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Function-p" aria-hidden="true">TOC</a></span></h4>
  1303. <p><code class="code">p(x, y)</code> returns the color of the pixel at coordinates <code class="code">x, y</code>, as a
  1304. <code class="code">0xRRGGBBAA</code> value. It can be assigned to a variable, so the color can be
  1305. available for <a class="ref" href="#cmd_005fsetcolor"><code class="code">setcolor</code></a> and <a class="ref" href="#cmd_005fcolorstop"><code class="code">colorstop</code></a> commands.
  1306. </p>
  1307. <p>If a single expression contains multiple calls to the function, it must return
  1308. the value of the last call in order to use it as a color.
  1309. </p>
  1310. <table class="cartouche" border="1"><tr><th>
  1311. Example</th></tr><tr><td>
  1312. <p>In this example, the first call to <code class="code">p(0, 0)</code> is stored in the variable
  1313. <var class="var">0</var> of the expression. Then, the same expression makes a second call to
  1314. <code class="code">p(1, 1)</code>, and finally it returns the value in the variable <var class="var">0</var>.
  1315. </p>
  1316. <div class="example">
  1317. <pre class="example-preformatted">setvar pixel (st(0, p(0, 0)); p(1, 1); ld(0))
  1318. </pre></div>
  1319. <p>Since the result of the expression is not the last call to <code class="code">p</code>, the
  1320. variable <var class="var">pixel</var> can not be used as a color, but it still can be used as
  1321. a numeric <code class="code">0xRRGGBBAA</code> value.
  1322. </p>
  1323. </td></tr></table>
  1324. <p>If the coordinates are outside the frame, or any of the arguments is not
  1325. a finite number (like
  1326. <a class="uref" href="https://en.wikipedia.org/wiki/NaN"><code class="code">NaN</code></a>), the function
  1327. returns <code class="code">NaN</code>.
  1328. </p>
  1329. <p>The <a class="ref" href="#transformation-matrix">transformation matrix</a> is applied to the
  1330. arguments. To use the original frame coordinates, call
  1331. <a class="ref" href="#cmd_005fresetmatrix"><code class="code">resetmatrix</code></a> between <a class="ref" href="#cmd_005fsave"><code class="code">save</code></a> and <a class="ref" href="#cmd_005frestore"><code class="code">restore</code></a>:
  1332. </p>
  1333. <div class="example">
  1334. <pre class="example-preformatted">save
  1335. resetmatrix
  1336. setvar pixel (p(0, 0)) // top-left pixel of the frame.
  1337. restore
  1338. setcolor pixel
  1339. </pre></div>
  1340. <p>Bitwise operations can be used to extract individual color components:
  1341. </p>
  1342. <div class="example">
  1343. <pre class="example-preformatted">setvar pixel (p(x, y))
  1344. if (not(isnan(pixel))) {
  1345. setvar px_red (pixel / 0x1000000)
  1346. setvar px_green (bitand(pixel / 0x10000, 0xFF))
  1347. setvar px_blue (bitand(pixel / 0x100, 0xFF))
  1348. setvar px_alpha (bitand(pixel, 0xFF))
  1349. }
  1350. </pre></div>
  1351. <a name="Function-pathlen"></a>
  1352. <h4 class="subsection">3.9.2 Function <code class="code">pathlen</code><span class="pull-right"><a class="anchor hidden-xs" href="#Function-pathlen" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Function-pathlen" aria-hidden="true">TOC</a></span></h4>
  1353. <p><code class="code">pathlen(n)</code> computes the length of the current path, by adding the
  1354. length of each line segment returned by
  1355. <a class="uref" href="https://www.cairographics.org/manual/cairo-Paths.html#cairo-copy-path-flat"><code class="code">cairo_copy_path_flat</code></a>.
  1356. </p>
  1357. <p>The function expects an argument <var class="var">n</var>, as the maximum number of line
  1358. segments to add to the length, or <code class="code">0</code> to add all segments.
  1359. </p>
  1360. <table class="cartouche" border="1"><tr><th>
  1361. Example</th></tr><tr><td>
  1362. <p>In this example, <code class="code">pathlen</code> is used to animate the stroke of a
  1363. spiral, in a 5 seconds loop.
  1364. </p>
  1365. <div class="example">
  1366. <pre class="example-preformatted">M (w / 2) (h / 2)
  1367. setvar a -1
  1368. repeat 16 {
  1369. rcurveto
  1370. (a * 2 / 3) 0
  1371. (a * 2 / 3) (a)
  1372. 0 (a)
  1373. setvar a (-sgn(a) * (abs(a) + 10))
  1374. }
  1375. setlinewidth 3
  1376. setdash
  1377. (pathlen(0) * (1 - mod(t / 5, 1)))
  1378. 1e6
  1379. setcolor teal
  1380. stroke
  1381. </pre></div>
  1382. </td></tr></table>
  1383. <a class="anchor" id="func_002drandomg"></a><a name="Function-randomg"></a>
  1384. <h4 class="subsection">3.9.3 Function <code class="code">randomg</code><span class="pull-right"><a class="anchor hidden-xs" href="#Function-randomg" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Function-randomg" aria-hidden="true">TOC</a></span></h4>
  1385. <p><code class="code">randomg(idx)</code> is similar to the <code class="code">random(idx)</code> function,
  1386. available in <a data-manual="ffmpeg-utils" href="ffmpeg-utils.html#Expression-Evaluation">FFmpeg expressions</a>, but its state is global to the frame, instead
  1387. of specific to each expression.
  1388. </p>
  1389. <p>To understand the difference, we need to dive into how
  1390. <code class="code">random(idx)</code> works inside a drawvg script.
  1391. </p>
  1392. <p>First, each expression in FFmpeg has a set of 10 internal variables,
  1393. which can be written with <code class="code">st(idx, value)</code>, and can be read with
  1394. <code class="code">ld(idx)</code>. <var class="var">idx</var> is a value between <code class="code">0</code> and <code class="code">9</code>.
  1395. These variables are initialized to <code class="code">0</code>.
  1396. </p>
  1397. <p>When a drawvg script is parsed, each expression is compiled with
  1398. <a class="uref" href="https://ffmpeg.org/doxygen/8.0/eval_8h.html#ad3bf8f3330d1fd139de2ca156c313f34"><code class="code">av_expr_parse</code></a>,
  1399. from <a class="uref" href="https://ffmpeg.org/libavutil.html">libavutil</a>, and these
  1400. compiled expressions are reused for every frame. The changes in the
  1401. internal variables (with <code class="code">st(idx, value)</code>) are visible between
  1402. frames, but they are not shared between expressions.
  1403. </p>
  1404. <table class="cartouche" border="1"><tr><th>
  1405. Example</th></tr><tr><td>
  1406. <p>In this example, the expression for the X coordinate updates its
  1407. internal variable <code class="code">0</code> in every frame:
  1408. </p>
  1409. <div class="example">
  1410. <pre class="example-preformatted">circle
  1411. (st(0, mod(ld(0) + 15, w))) // X
  1412. 120 // Y
  1413. (ld(0) + 20) // radius
  1414. fill
  1415. </pre></div>
  1416. <p><code class="code">st(idx, value)</code> returns the updated value, so it can be used as
  1417. the result of the expression.
  1418. </p>
  1419. <p>The radius is not affected because its internal variable (from
  1420. <code class="code">ld(0)</code>) is not updated by the other expression.
  1421. </p>
  1422. <p>Also, note that this example is just to show how internal variables are
  1423. kept between frames. A better approach to create this animation is to
  1424. use the variables n or t:
  1425. </p>
  1426. <div class="example">
  1427. <pre class="example-preformatted">circle (mod(n * 15, w)) 120 20
  1428. fill
  1429. </pre></div>
  1430. </td></tr></table>
  1431. <p>The function <code class="code">random(idx)</code> returns a
  1432. <a class="uref" href="https://en.wikipedia.org/wiki/Pseudorandom_number_generator">pseudorandom</a>
  1433. value between <code class="code">0</code> and <code class="code">1</code>. <var class="var">idx</var> is the internal variable
  1434. that is used both as the seed and to keep the state of the number
  1435. generator.
  1436. </p>
  1437. <table class="cartouche" border="1"><tr><th>
  1438. Example</th></tr><tr><td>
  1439. <p>The next example uses <code class="code">random(0)</code> to generate a random value for
  1440. the center of a circle:
  1441. </p>
  1442. <div class="example">
  1443. <pre class="example-preformatted">circle
  1444. (random(0) * w)
  1445. (random(0) * h)
  1446. 10
  1447. fill
  1448. </pre></div>
  1449. <p>The circle in every frame is at a different position, but always on the
  1450. diagonal line of the frame. This happens because the values for the
  1451. coordinates X and Y are identical, since both number generators use the
  1452. same seed.
  1453. </p>
  1454. <p>To distribute the circles over the whole frame we need different seeds
  1455. for each expression. This can be achieved by writing a non-zero value
  1456. (like <code class="code">0xF0F0</code>) to the internal variable of one of expressions, but
  1457. only when its value is <code class="code">0</code>:
  1458. </p>
  1459. <div class="example">
  1460. <pre class="example-preformatted">circle
  1461. (random(0) * w)
  1462. (st(0, if(ld(0), ld(0), 0xF0F0)); random(0) * h)
  1463. 10
  1464. fill
  1465. </pre></div>
  1466. <p>This approach is only useful if we need completely different positions
  1467. in each frame. In the next example, random values are used to distribute
  1468. many circles over the frame, but the position is fixed. The only change
  1469. over time is the fill color:
  1470. </p>
  1471. <div class="example">
  1472. <pre class="example-preformatted">repeat 20 {
  1473. circle
  1474. (st(0, i + 1e5); random(0) * w)
  1475. (st(0, i + 1e10); random(0) * h)
  1476. 10
  1477. }
  1478. sethsla (t * 60) 0.5 0.5 1
  1479. preserve fill
  1480. setcolor black@0.5
  1481. setlinewidth 1
  1482. stroke
  1483. </pre></div>
  1484. <p>This is achieved by using a precomputed state before calling <code class="code">random(0)</code>.
  1485. The variable <var class="var">i</var>, updated by <a class="ref" href="#cmd_005frepeat"><code class="code">repeat</code></a>, is needed to compute
  1486. different states in each iteration.
  1487. </p>
  1488. </td></tr></table>
  1489. <p>The <code class="code">randomg(idx)</code> function, which is specific to drawvg scripts,
  1490. is similar to <code class="code">random(idx)</code>, but intended to solve the previous
  1491. problems:
  1492. </p>
  1493. <ul class="itemize mark-bullet">
  1494. <li>All frames have the same seed.
  1495. </li><li>The state is shared between expressions.
  1496. </li></ul>
  1497. <p>The parameter <var class="var">idx</var> has two uses:
  1498. </p>
  1499. <ul class="itemize mark-bullet">
  1500. <li>The last two bits are the index of an internal state, so it is possible
  1501. to have 4 different number generators.
  1502. </li><li>The first call to <code class="code">randomg</code> with a specific index will use the
  1503. argument as the seed for the number generator in that index.
  1504. </li></ul>
  1505. <p>In a script like this:
  1506. </p>
  1507. <div class="example">
  1508. <pre class="example-preformatted">M (randomg(0xFF1)) (randomg(0xFF0))
  1509. l (randomg(0xAA1)) (randomg(0xFF0))
  1510. </pre></div>
  1511. <p>There are 4 calls to <code class="code">randomg</code>:
  1512. </p>
  1513. <ol class="enumerate">
  1514. <li> The first call, with the argument <code class="code">0xFF1</code>, uses the internal state
  1515. at index <code class="code">1</code> (because <code class="code">0xFF1</code> modulo <code class="code">4</code> is <code class="code">1</code>).
  1516. <p>Since this is the first use of that index, the number generator is
  1517. initialized with the seed <code class="code">0xFF1</code>.
  1518. </p>
  1519. </li><li> The second call has the same behaviour: it initializes the state at
  1520. index <code class="code">0</code> with the value <code class="code">0xFF0</code>.
  1521. </li><li> The third call has the argument <code class="code">0xAA1</code>, and it uses index
  1522. <code class="code">1</code>. Since that state is already initialized (with the seed
  1523. <code class="code">0xFF1</code>), the value <code class="code">0xAA1</code> is ignored, and it returns the
  1524. next number.
  1525. </li></ol>
  1526. <table class="cartouche" border="1"><tr><th>
  1527. Example</th></tr><tr><td>
  1528. <p>This example renders a simple rain animation, moving lines from top to
  1529. bottom.
  1530. </p>
  1531. <p><code class="code">randomg</code> is used to distribute the lines over the frame, and to
  1532. apply different speeds to each one.
  1533. </p>
  1534. <div class="example">
  1535. <pre class="example-preformatted">rect 0 0 w h
  1536. setcolor midnightblue
  1537. fill
  1538. setcolor white
  1539. repeat 50 {
  1540. setvar offset (t * (randomg(0) + 1))
  1541. moveto
  1542. (mod(randomg(0) + offset / 6, 1) * w)
  1543. (mod(randomg(0) + offset, 1) * h)
  1544. rlineto 6 36
  1545. setlinewidth (randomg(1) / 2 + 0.2)
  1546. stroke
  1547. }
  1548. </pre></div>
  1549. </td></tr></table>
  1550. <a name="Tracing-with-print"></a>
  1551. <h3 class="section">3.10 Tracing with <code class="code">print</code><span class="pull-right"><a class="anchor hidden-xs" href="#Tracing-with-print" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Tracing-with-print" aria-hidden="true">TOC</a></span></h3>
  1552. <p>It is possible to trace the execution of a drawvg script by printing the
  1553. value of an expression, either with the <a class="ref" href="#cmd_005fprint"><code class="code">print</code></a> command, or with
  1554. the print function.
  1555. </p>
  1556. <p>In both cases, the values are written to the FFmpeg log.
  1557. </p>
  1558. <p>Printing expressions may have a noticeable impact on the performance, so
  1559. it is preferable to use it only when necessary.
  1560. </p>
  1561. <a name="Function-print"></a>
  1562. <h4 class="subsection">3.10.1 Function print<span class="pull-right"><a class="anchor hidden-xs" href="#Function-print" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Function-print" aria-hidden="true">TOC</a></span></h4>
  1563. <p>The function <code class="code">print(t)</code> writes the value of t, and returns its
  1564. argument.
  1565. </p>
  1566. <table class="cartouche" border="1"><tr><th>
  1567. Example</th></tr><tr><td>
  1568. <p>Given a line line this:
  1569. </p>
  1570. <div class="example">
  1571. <pre class="example-preformatted">M (sin(2 * PI * t) * w) 0
  1572. </pre></div>
  1573. <p>We can see the values of <code class="code">sin(2 * PI * t)</code> by surrounding it with a
  1574. call to <code class="code">print()</code>:
  1575. </p>
  1576. <div class="example">
  1577. <pre class="example-preformatted">M (print(sin(2 * PI * t)) * w) 0
  1578. </pre></div>
  1579. <p>Executing this script with a 1 second / 8 FPS video shows the expected
  1580. values for the sine function.
  1581. </p>
  1582. <pre class="verbatim">$ ffmpeg \
  1583. -f lavfi \
  1584. -i 'color=r=8:d=1, drawvg=M (print(sin(2 * PI * t)) * w) 0' \
  1585. -f null /dev/null \
  1586. |&amp; grep 'Eval @'
  1587. [Eval @ 0x7f500f502d20] 0.000000
  1588. [Eval @ 0x7f4ff784b420] 0.707107
  1589. [Eval @ 0x7f4ff784ba20] 1.000000
  1590. [Eval @ 0x7f4ff784c020] 0.707107
  1591. [Eval @ 0x7f4ff784c620] 0.000000
  1592. [Eval @ 0x7f4ff784cc20] -0.707107
  1593. [Eval @ 0x7f4ff784d220] -1.000000
  1594. [Eval @ 0x7f4ff784d820] -0.707107
  1595. </pre>
  1596. </td></tr></table>
  1597. <a class="anchor" id="Command-print"></a><a name="Command-print-1"></a>
  1598. <h4 class="subsection">3.10.2 Command <code class="code">print</code><span class="pull-right"><a class="anchor hidden-xs" href="#Command-print-1" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Command-print-1" aria-hidden="true">TOC</a></span></h4>
  1599. <p>The command <a class="ref" href="#cmd_005fprint"><code class="code">print</code></a> accepts an arbitrary number of arguments, and
  1600. for each one it writes:
  1601. </p>
  1602. <ul class="itemize mark-bullet">
  1603. <li>The source location (line and column).
  1604. </li><li>The source code of the expression.
  1605. </li><li>The result of evaluating the expression.
  1606. </li></ul>
  1607. <p>When there are multiple expressions, they are separated by the <code class="code">|</code>
  1608. character.
  1609. </p>
  1610. <table class="cartouche" border="1"><tr><th>
  1611. Example</th></tr><tr><td>
  1612. <p>The next script prints the position of the <a class="ref" href="#current-point">current point</a> after the
  1613. <a class="ref" href="#cmd_005fl"><code class="code">l</code></a> command:
  1614. </p>
  1615. <div class="example">
  1616. <pre class="example-preformatted">M 10 20
  1617. l 100 100
  1618. print cx cy
  1619. stroke
  1620. </pre></div>
  1621. <p>For each frame, it produces this output:
  1622. </p>
  1623. <pre class="verbatim">[3:7] cx = 110.000000 | [3:10] cy = 120.000000
  1624. </pre>
  1625. <p>The next example prints the values of <code class="code">random(0)</code>:
  1626. </p>
  1627. <pre class="verbatim">$ ffmpeg \
  1628. -f lavfi \
  1629. -i 'color=r=8:d=1, drawvg=print (random(0))' \
  1630. -f null /dev/null \
  1631. |&amp; grep 'drawvg @'
  1632. [drawvg @ 0x50a000000180] [1:7] (random(0)) = 0.229731
  1633. [drawvg @ 0x50a000000180] [1:7] (random(0)) = 0.959813
  1634. [drawvg @ 0x50a000000180] [1:7] (random(0)) = 0.071676
  1635. [drawvg @ 0x50a000000180] [1:7] (random(0)) = 0.044600
  1636. [drawvg @ 0x50a000000180] [1:7] (random(0)) = 0.134127
  1637. [drawvg @ 0x50a000000180] [1:7] (random(0)) = 0.320513
  1638. [drawvg @ 0x50a000000180] [1:7] (random(0)) = 0.857675
  1639. [drawvg @ 0x50a000000180] [1:7] (random(0)) = 0.562456
  1640. </pre>
  1641. </td></tr></table>
  1642. <a name="Commands-1"></a>
  1643. <h2 class="chapter">4 Commands<span class="pull-right"><a class="anchor hidden-xs" href="#Commands-1" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Commands-1" aria-hidden="true">TOC</a></span></h2>
  1644. <a class="anchor" id="cmd_005farc"></a><a name="arc"></a>
  1645. <h3 class="section">4.1 <code class="code">arc</code><span class="pull-right"><a class="anchor hidden-xs" href="#arc" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-arc" aria-hidden="true">TOC</a></span></h3>
  1646. <p><b class="b"><code class="code">arc <var class="var">xc</var> <var class="var">yc</var> <var class="var">radius</var> <var class="var">angle1</var> <var class="var">angle2</var></code></b> &nbsp;&nbsp;&nbsp;&mdash; <a class="ref" href="#implicit-commands"><i class="i">Can be implicit</i></a>
  1647. </p>
  1648. <p>Adds a circular arc of the given <var class="var">radius</var> to the current path. The
  1649. arc is centered at <var class="var">xc, yc</var>, begins at <var class="var">angle1</var> and proceeds
  1650. in the direction of increasing angles to end at <var class="var">angle2</var>.
  1651. </p>
  1652. <p>If there is a <a class="ref" href="#current-point">current point</a>, a line is added from it to the beginning of
  1653. the arc. If this is not desired, use <a class="ref" href="#cmd_005fnewpath"><code class="code">newpath</code></a> before <a class="ref" href="#cmd_005farc"><code class="code">arc</code></a> to clear
  1654. the <a class="ref" href="#current-point">current point</a>.
  1655. </p>
  1656. <p>See the documentation of the
  1657. <a class="uref" href="https://www.cairographics.org/manual/cairo-Paths.html#cairo-arc"><code class="code">cairo_arc</code></a>
  1658. function for more details.
  1659. </p>
  1660. <table class="cartouche" border="1"><tr><th>
  1661. Example</th></tr><tr><td>
  1662. <div class="example">
  1663. <pre class="example-preformatted">arc 120 120 60 0 (3 * PI / 2)
  1664. stroke
  1665. </pre></div>
  1666. </td></tr></table>
  1667. <a class="anchor" id="cmd_005farcn"></a><a name="arcn"></a>
  1668. <h3 class="section">4.2 <code class="code">arcn</code><span class="pull-right"><a class="anchor hidden-xs" href="#arcn" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-arcn" aria-hidden="true">TOC</a></span></h3>
  1669. <p><b class="b"><code class="code">arcn <var class="var">xc</var> <var class="var">yc</var> <var class="var">radius</var> <var class="var">angle1</var> <var class="var">angle2</var></code></b> &nbsp;&nbsp;&nbsp;&mdash; <a class="ref" href="#implicit-commands"><i class="i">Can be implicit</i></a>
  1670. </p>
  1671. <p>Similar to <a class="ref" href="#cmd_005farc"><code class="code">arc</code></a>, but it differs in the direction of the arc
  1672. between the two angles.
  1673. </p>
  1674. <p>See the documentation of the
  1675. <a class="uref" href="https://www.cairographics.org/manual/cairo-Paths.html#cairo-arc-negative"><code class="code">cairo_arc_negative</code></a>
  1676. function for more details.
  1677. </p>
  1678. <table class="cartouche" border="1"><tr><th>
  1679. Example</th></tr><tr><td>
  1680. <p>In this example, both <a class="ref" href="#cmd_005farc"><code class="code">arc</code></a> and <a class="ref" href="#cmd_005farcn"><code class="code">arcn</code></a> have the same angles,
  1681. but they render different arcs:
  1682. </p>
  1683. <div class="example">
  1684. <pre class="example-preformatted">arc 120 90 60 (PI / 2) 0
  1685. newpath
  1686. arcn 120 150 60 (PI / 2) 0
  1687. stroke
  1688. </pre></div>
  1689. </td></tr></table>
  1690. <p><a class="ref" href="#cmd_005fnewpath"><code class="code">newpath</code></a> is needed to prevent a line between the two arcs.
  1691. </p>
  1692. <a class="anchor" id="cmd_005fbreak"></a><a name="break"></a>
  1693. <h3 class="section">4.3 <code class="code">break</code><span class="pull-right"><a class="anchor hidden-xs" href="#break" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-break" aria-hidden="true">TOC</a></span></h3>
  1694. <p><b class="b"><code class="code">break</code></b>
  1695. </p>
  1696. <p><a class="ref" href="#cmd_005fbreak"><code class="code">break</code></a> terminates the execution of the innermost block, either a
  1697. <a class="ref" href="#cmd_005frepeat"><code class="code">repeat</code></a> loop or a procedure.
  1698. </p>
  1699. <p>If it is used outside of a <a class="ref" href="#cmd_005frepeat"><code class="code">repeat</code></a> / <a class="ref" href="#cmd_005fproc"><code class="code">proc</code></a> block, it
  1700. terminates the script for the current frame.
  1701. </p>
  1702. <a class="anchor" id="cmd_005fcall"></a><a name="call"></a>
  1703. <h3 class="section">4.4 <code class="code">call</code><span class="pull-right"><a class="anchor hidden-xs" href="#call" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-call" aria-hidden="true">TOC</a></span></h3>
  1704. <p><b class="b"><code class="code">call <var class="var">name</var> <var class="var">args</var>*</code></b>
  1705. </p>
  1706. <p>Invokes a procedure defined by <a class="ref" href="#cmd_005fproc"><code class="code">proc</code></a>.
  1707. </p>
  1708. <p>See the <a class="ref" href="#Procedures">Procedures</a> section above for more details.
  1709. </p>
  1710. <a class="anchor" id="cmd_005fcircle"></a><a name="circle"></a>
  1711. <h3 class="section">4.5 <code class="code">circle</code><span class="pull-right"><a class="anchor hidden-xs" href="#circle" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-circle" aria-hidden="true">TOC</a></span></h3>
  1712. <p><b class="b"><code class="code">circle <var class="var">xc</var> <var class="var">yc</var> <var class="var">radius</var></code></b> &nbsp;&nbsp;&nbsp;&mdash; <a class="ref" href="#implicit-commands"><i class="i">Can be implicit</i></a>
  1713. </p>
  1714. <p>Adds a circle of the given <var class="var">radius</var> to the current path. The circle
  1715. is centered at <var class="var">xc, yc</var>. The <a class="ref" href="#current-point">current point</a> is cleared before and
  1716. after adding the circle.
  1717. </p>
  1718. <p>This is a convenience wrapper for <a class="ref" href="#cmd_005farc"><code class="code">arc</code></a>. A call to <a class="ref" href="#cmd_005fcircle"><code class="code">circle</code></a> is
  1719. equivalent to:
  1720. </p>
  1721. <div class="example">
  1722. <pre class="example-preformatted">newpath
  1723. arc xc yc radius (0) (2 * PI)
  1724. newpath
  1725. </pre></div>
  1726. <a class="anchor" id="cmd_005fclip"></a><a class="anchor" id="cmd_005feoclip"></a><a name="clip_002c-eoclip"></a>
  1727. <h3 class="section">4.6 <code class="code">clip</code>, <code class="code">eoclip</code><span class="pull-right"><a class="anchor hidden-xs" href="#clip_002c-eoclip" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-clip_002c-eoclip" aria-hidden="true">TOC</a></span></h3>
  1728. <p><b class="b"><code class="code">clip, eoclip</code></b>
  1729. </p>
  1730. <p>Establishes a new clip region by intersecting the current clip region
  1731. with the current path as it would be filled by <a class="ref" href="#cmd_005ffill"><code class="code">fill</code></a> or
  1732. <a class="ref" href="#cmd_005feofill"><code class="code">eofill</code></a>.
  1733. </p>
  1734. <p><a class="ref" href="#cmd_005feoclip"><code class="code">eoclip</code></a> uses the
  1735. <a class="uref" href="https://www.cairographics.org/manual/cairo-cairo-t.html#CAIRO-FILL-RULE-EVEN-ODD:CAPS">even&ndash;odd
  1736. rule</a>. See <a class="ref" href="#fill-rules">fill rules</a> for more details.
  1737. </p>
  1738. <p>The path is cleared after updating the clip region, unless the
  1739. <a class="ref" href="#cmd_005fpreserve"><code class="code">preserve</code></a> command is used before <a class="ref" href="#cmd_005fclip"><code class="code">clip</code></a> or <a class="ref" href="#cmd_005feoclip"><code class="code">eoclip</code></a>.
  1740. </p>
  1741. <p>See the documentation of the
  1742. <a class="uref" href="https://www.cairographics.org/manual/cairo-cairo-t.html#cairo-clip"><code class="code">cairo_clip</code></a>
  1743. function for more details.
  1744. </p>
  1745. <a class="anchor" id="cmd_005fZ"></a><a class="anchor" id="cmd_005fz"></a><a class="anchor" id="cmd_005fclosepath"></a><a name="Z_002c-z_002c-closepath"></a>
  1746. <h3 class="section">4.7 <code class="code">Z</code>, <code class="code">z</code>, <code class="code">closepath</code><span class="pull-right"><a class="anchor hidden-xs" href="#Z_002c-z_002c-closepath" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Z_002c-z_002c-closepath" aria-hidden="true">TOC</a></span></h3>
  1747. <p><b class="b"><code class="code">Z, z, closepath</code></b>
  1748. </p>
  1749. <p>Adds a line segment to the path from the <a class="ref" href="#current-point">current point</a> to the beginning
  1750. of the current sub-path, and closes this sub-path. The beginning is set by any
  1751. of the <em class="emph">move</em> commands (<a class="ref" href="#cmd_005fM"><code class="code">M</code></a>, <a class="ref" href="#cmd_005fm"><code class="code">m</code></a>, <a class="ref" href="#cmd_005fmoveto"><code class="code">moveto</code></a>,
  1752. <a class="ref" href="#cmd_005frmoveto"><code class="code">rmoveto</code></a>).
  1753. </p>
  1754. <p>See the documentation of the
  1755. <a class="uref" href="https://www.cairographics.org/manual/cairo-Paths.html#cairo-close-path"><code class="code">cairo_close_path</code></a>
  1756. function for more details.
  1757. </p>
  1758. <a class="anchor" id="cmd_005fcolorstop"></a><a name="colorstop"></a>
  1759. <h3 class="section">4.8 <code class="code">colorstop</code><span class="pull-right"><a class="anchor hidden-xs" href="#colorstop" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-colorstop" aria-hidden="true">TOC</a></span></h3>
  1760. <p><b class="b"><code class="code">colorstop <var class="var">offset</var> <var class="var">color</var></code></b> &nbsp;&nbsp;&nbsp;&mdash; <a class="ref" href="#implicit-commands"><i class="i">Can be implicit</i></a>
  1761. </p>
  1762. <p>Adds a color stop to a gradient pattern.
  1763. </p>
  1764. <p><var class="var">offset</var> is a value between <code class="code">0</code> and <code class="code">1</code>, and it specifies
  1765. the location along the gradient&rsquo;s control vector.
  1766. </p>
  1767. <p>This command must be executed after <a class="ref" href="#cmd_005flineargrad"><code class="code">lineargrad</code></a> or
  1768. <a class="ref" href="#cmd_005fradialgrad"><code class="code">radialgrad</code></a>.
  1769. </p>
  1770. <p>Color stops can be added in any number of calls to <a class="ref" href="#cmd_005fcolorstop"><code class="code">colorstop</code></a>. In
  1771. the next example, the 3 blocks define the same gradient:
  1772. </p>
  1773. <div class="example">
  1774. <pre class="example-preformatted">// 1
  1775. colorstop 0.0 red
  1776. colorstop 0.5 green
  1777. colorstop 1.0 blue
  1778. // 2
  1779. colorstop 0 red 0.5 green
  1780. colorstop 1 blue
  1781. // 3
  1782. colorstop 0 red 0.5 green 1 blue
  1783. </pre></div>
  1784. <p>See the documentation of the
  1785. <a class="uref" href="https://www.cairographics.org/manual/cairo-cairo-pattern-t.html#cairo-pattern-add-color-stop-rgba"><code class="code">cairo_pattern_add_color_stop_rgba</code></a>
  1786. function for more details.
  1787. </p>
  1788. <table class="cartouche" border="1"><tr><th>
  1789. Example</th></tr><tr><td>
  1790. <p>In this example, color stops are added in a <a class="ref" href="#cmd_005frepeat"><code class="code">repeat</code></a> loop.
  1791. </p>
  1792. <div class="example">
  1793. <pre class="example-preformatted">lineargrad 0 0 w h
  1794. repeat 6 {
  1795. defhsla s (i * 60) 0.8 0.5 1
  1796. colorstop (i / 5) s
  1797. }
  1798. rect 0 0 w h
  1799. fill
  1800. </pre></div>
  1801. <p>It is possible to avoid transitions between color stops by repeating the
  1802. same color in two stops:
  1803. </p>
  1804. <div class="example">
  1805. <pre class="example-preformatted">lineargrad 0 0 w h
  1806. repeat 6 {
  1807. defhsla s (i * 60) 0.8 0.5 1
  1808. colorstop (i / 5) s
  1809. colorstop ((i + 1) / 5) s
  1810. }
  1811. rect 0 0 w h
  1812. fill
  1813. </pre></div>
  1814. </td></tr></table>
  1815. <a class="anchor" id="cmd_005fC"></a><a class="anchor" id="cmd_005fcurveto"></a><a name="C_002c-curveto"></a>
  1816. <h3 class="section">4.9 <code class="code">C</code>, <code class="code">curveto</code><span class="pull-right"><a class="anchor hidden-xs" href="#C_002c-curveto" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-C_002c-curveto" aria-hidden="true">TOC</a></span></h3>
  1817. <p><b class="b"><code class="code">C, curveto <var class="var">x1</var> <var class="var">y1</var> <var class="var">x2</var> <var class="var">y2</var> <var class="var">x</var> <var class="var">y</var></code></b> &nbsp;&nbsp;&nbsp;&mdash; <a class="ref" href="#implicit-commands"><i class="i">Can be implicit</i></a>
  1818. </p>
  1819. <p>Draw a cubic Bézier curve from the <a class="ref" href="#current-point">current point</a> to the <em class="emph">end point</em>
  1820. specified by <var class="var">x, y</var>. The <em class="emph">start control point</em> is specified by
  1821. <var class="var">x1, y1</var> and the <em class="emph">end control point</em> is specified by <var class="var">x2, y2</var>.
  1822. </p>
  1823. <p>The behaviour is identical to the <a class="ref" href="#cmd_005fC"><code class="code">C</code></a> command in <a class="uref" href="https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Element/path">SVG&rsquo;s <code class="code">&lt;path&gt;</code></a>. For more
  1824. details, see <a class="uref" href="https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Attribute/d#cubic_b%C3%A9zier_curve">Cubic Bézier Curve on MDN</a>, and the <a class="uref" href="https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorials/SVG_from_scratch/Paths#curve_commands">Curve Commands section of the Paths tutorial on MDN</a>.
  1825. </p>
  1826. <table class="cartouche" border="1"><tr><th>
  1827. Example</th></tr><tr><td>
  1828. <div class="example">
  1829. <pre class="example-preformatted">moveto 20 20
  1830. curveto
  1831. 0 (h / 2) // start control point
  1832. w (h / 2) // end control point
  1833. (w - 20) (h - 20) // end point
  1834. stroke
  1835. </pre></div>
  1836. </td></tr></table>
  1837. <a class="anchor" id="cmd_005fc"></a><a class="anchor" id="cmd_005frcurveto"></a><a name="c_002c-rcurveto"></a>
  1838. <h3 class="section">4.10 <code class="code">c</code>, <code class="code">rcurveto</code><span class="pull-right"><a class="anchor hidden-xs" href="#c_002c-rcurveto" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-c_002c-rcurveto" aria-hidden="true">TOC</a></span></h3>
  1839. <p><b class="b"><code class="code">c, rcurveto <var class="var">dx1</var> <var class="var">dy1</var> <var class="var">dx2</var> <var class="var">dy2</var> <var class="var">dx</var> <var class="var">dy</var></code></b> &nbsp;&nbsp;&nbsp;&mdash; <a class="ref" href="#implicit-commands"><i class="i">Can be implicit</i></a>
  1840. </p>
  1841. <p>Like <a class="ref" href="#cmd_005fcurveto"><code class="code">curveto</code></a>, but the coordinates are relative to the <a class="ref" href="#current-point">current point</a>.
  1842. </p>
  1843. <a class="anchor" id="cmd_005fdefhsla"></a><a name="defhsla"></a>
  1844. <h3 class="section">4.11 <code class="code">defhsla</code><span class="pull-right"><a class="anchor hidden-xs" href="#defhsla" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-defhsla" aria-hidden="true">TOC</a></span></h3>
  1845. <p><b class="b"><code class="code">defhsla varname <var class="var">h</var> <var class="var">s</var> <var class="var">l</var> <var class="var">a</var></code></b>
  1846. </p>
  1847. <p>Similar to <a class="ref" href="#cmd_005fsethsla"><code class="code">sethsla</code></a>, but instead of establishing the color for
  1848. stroke and fill operations, the computed color is assigned to the
  1849. variable <var class="var">varname</var>.
  1850. </p>
  1851. <p><var class="var">varname</var> can then be used as a color for <a class="ref" href="#cmd_005fsetcolor"><code class="code">setcolor</code></a> and
  1852. <a class="ref" href="#cmd_005fcolorstop"><code class="code">colorstop</code></a>.
  1853. </p>
  1854. <p>See <a class="ref" href="#cmd_005fsethsla"><code class="code">sethsla</code></a> for more details on how the color is computed.
  1855. </p>
  1856. <a class="anchor" id="cmd_005fdefrgba"></a><a name="defrgba"></a>
  1857. <h3 class="section">4.12 <code class="code">defrgba</code><span class="pull-right"><a class="anchor hidden-xs" href="#defrgba" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-defrgba" aria-hidden="true">TOC</a></span></h3>
  1858. <p><b class="b"><code class="code">defrgba varname <var class="var">r</var> <var class="var">g</var> <var class="var">b</var> <var class="var">a</var></code></b>
  1859. </p>
  1860. <p>Computes a color from the <em class="emph">red</em>, <em class="emph">green</em>, <em class="emph">blue</em>, and
  1861. <em class="emph">alpha</em> components, and assigns it to the variable <var class="var">varname</var>.
  1862. </p>
  1863. <p>All components are values between <code class="code">0</code> and <code class="code">1</code>. Values outside
  1864. that range are clamped to it.
  1865. </p>
  1866. <a class="anchor" id="cmd_005fellipse"></a><a name="ellipse"></a>
  1867. <h3 class="section">4.13 <code class="code">ellipse</code><span class="pull-right"><a class="anchor hidden-xs" href="#ellipse" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-ellipse" aria-hidden="true">TOC</a></span></h3>
  1868. <p><b class="b"><code class="code">ellipse <var class="var">cx</var> <var class="var">cy</var> <var class="var">rx</var> <var class="var">ry</var></code></b> &nbsp;&nbsp;&nbsp;&mdash; <a class="ref" href="#implicit-commands"><i class="i">Can be implicit</i></a>
  1869. </p>
  1870. <p>Adds an ellipse to the current path. Similar to <a class="ref" href="#cmd_005fcircle"><code class="code">circle</code></a>, but it is
  1871. possible to use different radius for both axes.
  1872. </p>
  1873. <table class="cartouche" border="1"><tr><th>
  1874. Example</th></tr><tr><td>
  1875. <pre class="verbatim">ellipse 120 120 75 50
  1876. stroke
  1877. </pre>
  1878. </td></tr></table>
  1879. <a class="anchor" id="cmd_005ffill"></a><a class="anchor" id="cmd_005feofill"></a><a name="fill_002c-eofill"></a>
  1880. <h3 class="section">4.14 <code class="code">fill</code>, <code class="code">eofill</code><span class="pull-right"><a class="anchor hidden-xs" href="#fill_002c-eofill" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-fill_002c-eofill" aria-hidden="true">TOC</a></span></h3>
  1881. <p><b class="b"><code class="code">fill, eofill</code></b>
  1882. </p>
  1883. <p>Fill the current path, using the <a class="ref" href="#current-pattern">current pattern</a> (either
  1884. a solid color or a gradient).
  1885. </p>
  1886. <p><a class="ref" href="#cmd_005feofill"><code class="code">eofill</code></a> uses the
  1887. <a class="uref" href="https://www.cairographics.org/manual/cairo-cairo-t.html#CAIRO-FILL-RULE-EVEN-ODD:CAPS">even&ndash;odd
  1888. rule</a>. See <a class="ref" href="#fill-rules">fill rules</a> for more details.
  1889. </p>
  1890. <p>The path is cleared after the operation, unless the <a class="ref" href="#cmd_005fpreserve"><code class="code">preserve</code></a>
  1891. command is used before <a class="ref" href="#cmd_005ffill"><code class="code">fill</code></a> or <a class="ref" href="#cmd_005feofill"><code class="code">eofill</code></a>.
  1892. </p>
  1893. <p>See the documentation of the
  1894. <a class="uref" href="https://www.cairographics.org/manual/cairo-cairo-t.html#cairo-fill"><code class="code">cairo_fill</code></a>
  1895. function for more details.
  1896. </p>
  1897. <a class="anchor" id="cmd_005fgetmetadata"></a><a name="getmetadata"></a>
  1898. <h3 class="section">4.15 <code class="code">getmetadata</code><span class="pull-right"><a class="anchor hidden-xs" href="#getmetadata" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-getmetadata" aria-hidden="true">TOC</a></span></h3>
  1899. <p><b class="b"><code class="code">getmetadata <var class="var">varname</var> <var class="var">key</var></code></b>
  1900. </p>
  1901. <p>Get the value of a metadata entry created by another filter, and assign
  1902. it to the variable <var class="var">varname</var>.
  1903. </p>
  1904. <p>If there is no metadata entry for <var class="var">key</var>, or its value is not a
  1905. number, <var class="var">varname</var> is set to
  1906. <a class="uref" href="https://en.wikipedia.org/wiki/NaN"><code class="code">NaN</code></a>.
  1907. </p>
  1908. <p>See the <a class="ref" href="#Frame-Metadata">Frame Metadata</a> section above for an
  1909. example.
  1910. </p>
  1911. <a class="anchor" id="cmd_005fH"></a><a class="anchor" id="cmd_005fh"></a><a name="H_002c-h"></a>
  1912. <h3 class="section">4.16 <code class="code">H</code>, <code class="code">h</code><span class="pull-right"><a class="anchor hidden-xs" href="#H_002c-h" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-H_002c-h" aria-hidden="true">TOC</a></span></h3>
  1913. <p><b class="b"><code class="code">H, h <var class="var">x</var></code></b> &nbsp;&nbsp;&nbsp;&mdash; <a class="ref" href="#implicit-commands"><i class="i">Can be implicit</i></a>
  1914. </p>
  1915. <p>Draw a horizontal line from the <a class="ref" href="#current-point">current point</a> to x.
  1916. </p>
  1917. <p>The coordinate for <a class="ref" href="#cmd_005fH"><code class="code">H</code></a> is absolute, and for <a class="ref" href="#cmd_005fh"><code class="code">h</code></a> it is relative
  1918. to the <a class="ref" href="#current-point">current point</a>.
  1919. </p>
  1920. <a class="anchor" id="cmd_005fif"></a><a name="if"></a>
  1921. <h3 class="section">4.17 <code class="code">if</code><span class="pull-right"><a class="anchor hidden-xs" href="#if" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-if" aria-hidden="true">TOC</a></span></h3>
  1922. <p><b class="b"><code class="code">if <var class="var">condition</var> { <var class="var">block</var> }</code></b>
  1923. </p>
  1924. <p>Executes a block if the value of <var class="var">condition</var> is not zero, and a
  1925. finite number (unlike
  1926. <a class="uref" href="https://en.wikipedia.org/wiki/NaN"><code class="code">NaN</code></a>).
  1927. </p>
  1928. <p>See the <a class="ref" href="#comp_002doperators">Comparison and Logical Operators</a> section
  1929. above for more details on how to write conditional expressions.
  1930. </p>
  1931. <a class="anchor" id="cmd_005flineargrad"></a><a name="lineargrad"></a>
  1932. <h3 class="section">4.18 <code class="code">lineargrad</code><span class="pull-right"><a class="anchor hidden-xs" href="#lineargrad" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-lineargrad" aria-hidden="true">TOC</a></span></h3>
  1933. <p><b class="b"><code class="code">lineargrad <var class="var">x0</var> <var class="var">y0</var> <var class="var">x1</var> <var class="var">y1</var></code></b>
  1934. </p>
  1935. <p>Set the <a class="ref" href="#current-pattern">current pattern</a> to a new linear gradient, along
  1936. the line from the coordinates <var class="var">x0, y0</var> to <var class="var">x1, y1</var>.
  1937. </p>
  1938. <p>This gradient can be used for stroke and fill operations.
  1939. </p>
  1940. <p>Use <a class="ref" href="#cmd_005fcolorstop"><code class="code">colorstop</code></a> to set the color for each position in the gradient.
  1941. </p>
  1942. <a class="anchor" id="cmd_005fL"></a><a class="anchor" id="cmd_005flineto"></a><a name="L_002c-lineto"></a>
  1943. <h3 class="section">4.19 <code class="code">L</code>, <code class="code">lineto</code><span class="pull-right"><a class="anchor hidden-xs" href="#L_002c-lineto" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-L_002c-lineto" aria-hidden="true">TOC</a></span></h3>
  1944. <p><b class="b"><code class="code">L, lineto <var class="var">x</var> <var class="var">y</var></code></b> &nbsp;&nbsp;&nbsp;&mdash; <a class="ref" href="#implicit-commands"><i class="i">Can be implicit</i></a>
  1945. </p>
  1946. <p>Draw a line from the <a class="ref" href="#current-point">current point</a> to the coordinates at <var class="var">x, y</var>.
  1947. </p>
  1948. <p>See the documentation of the
  1949. <a class="uref" href="https://www.cairographics.org/manual/cairo-Paths.html#cairo-line-to"><code class="code">cairo_line_to</code></a>
  1950. function for more details.
  1951. </p>
  1952. <a class="anchor" id="cmd_005fl"></a><a class="anchor" id="cmd_005frlineto"></a><a name="l_002c-rlineto"></a>
  1953. <h3 class="section">4.20 <code class="code">l</code>, <code class="code">rlineto</code><span class="pull-right"><a class="anchor hidden-xs" href="#l_002c-rlineto" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-l_002c-rlineto" aria-hidden="true">TOC</a></span></h3>
  1954. <p><b class="b"><code class="code">l, rlineto <var class="var">dx</var> <var class="var">dy</var></code></b> &nbsp;&nbsp;&nbsp;&mdash; <a class="ref" href="#implicit-commands"><i class="i">Can be implicit</i></a>
  1955. </p>
  1956. <p>Like <a class="ref" href="#cmd_005flineto"><code class="code">lineto</code></a>, but the coordinates are relative to the <a class="ref" href="#current-point">current point</a>.
  1957. </p>
  1958. <a class="anchor" id="cmd_005fM"></a><a class="anchor" id="cmd_005fmoveto"></a><a name="M_002c-moveto"></a>
  1959. <h3 class="section">4.21 <code class="code">M</code>, <code class="code">moveto</code><span class="pull-right"><a class="anchor hidden-xs" href="#M_002c-moveto" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-M_002c-moveto" aria-hidden="true">TOC</a></span></h3>
  1960. <p><b class="b"><code class="code">M, moveto <var class="var">x</var> <var class="var">y</var></code></b> &nbsp;&nbsp;&nbsp;&mdash; <a class="ref" href="#implicit-commands"><i class="i">Can be implicit</i></a>
  1961. </p>
  1962. <p>Begin a new sub-path, and set the <a class="ref" href="#current-point">current point</a> to <var class="var">x, y</var>.
  1963. </p>
  1964. <a class="anchor" id="cmd_005fm"></a><a class="anchor" id="cmd_005frmoveto"></a><a name="m_002c-rmoveto"></a>
  1965. <h3 class="section">4.22 <code class="code">m</code>, <code class="code">rmoveto</code><span class="pull-right"><a class="anchor hidden-xs" href="#m_002c-rmoveto" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-m_002c-rmoveto" aria-hidden="true">TOC</a></span></h3>
  1966. <p><b class="b"><code class="code">m, rmoveto <var class="var">dx</var> <var class="var">dy</var></code></b> &nbsp;&nbsp;&nbsp;&mdash; <a class="ref" href="#implicit-commands"><i class="i">Can be implicit</i></a>
  1967. </p>
  1968. <p>Like <a class="ref" href="#cmd_005fmoveto"><code class="code">moveto</code></a>, but the coordinates are relative to the <a class="ref" href="#current-point">current point</a>.
  1969. </p>
  1970. <a class="anchor" id="cmd_005fnewpath"></a><a name="newpath"></a>
  1971. <h3 class="section">4.23 <code class="code">newpath</code><span class="pull-right"><a class="anchor hidden-xs" href="#newpath" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-newpath" aria-hidden="true">TOC</a></span></h3>
  1972. <p><b class="b"><code class="code">newpath</code></b>
  1973. </p>
  1974. <p>Begin a new sub-path. Like <a class="ref" href="#cmd_005fmoveto"><code class="code">moveto</code></a>, but there is no
  1975. <a class="ref" href="#current-point">current point</a> after it.
  1976. </p>
  1977. <table class="cartouche" border="1"><tr><th>
  1978. Example</th></tr><tr><td>
  1979. <p>In the next example, <a class="ref" href="#cmd_005fnewpath"><code class="code">newpath</code></a> is used in the path on the right to
  1980. prevent the line connecting both arcs.
  1981. </p>
  1982. <pre class="verbatim">setlinewidth 3
  1983. setcolor skyblue
  1984. arcn 70 90 20 0 (PI)
  1985. arc 70 150 20 0 (PI)
  1986. stroke
  1987. setcolor seagreen
  1988. arcn 170 90 20 0 (PI)
  1989. newpath
  1990. arc 170 150 20 0 (PI)
  1991. stroke
  1992. </pre>
  1993. </td></tr></table>
  1994. <a class="anchor" id="cmd_005fpreserve"></a><a name="preserve"></a>
  1995. <h3 class="section">4.24 <code class="code">preserve</code><span class="pull-right"><a class="anchor hidden-xs" href="#preserve" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-preserve" aria-hidden="true">TOC</a></span></h3>
  1996. <p><b class="b"><code class="code">preserve</code></b>
  1997. </p>
  1998. <p>Indicates that the next operation to fill, stroke, or clip, must
  1999. preserve the path, so the same path can be used in multiple operations.
  2000. </p>
  2001. <p>It has effect on these commands:
  2002. </p>
  2003. <ul class="itemize mark-bullet">
  2004. <li><a class="ref" href="#cmd_005fclip"><code class="code">clip</code></a>
  2005. </li><li><a class="ref" href="#cmd_005feoclip"><code class="code">eoclip</code></a>
  2006. </li><li><a class="ref" href="#cmd_005feofill"><code class="code">eofill</code></a>
  2007. </li><li><a class="ref" href="#cmd_005ffill"><code class="code">fill</code></a>
  2008. </li><li><a class="ref" href="#cmd_005fstroke"><code class="code">stroke</code></a>
  2009. </li></ul>
  2010. <p>The script can contain any command between <a class="ref" href="#cmd_005fpreserve"><code class="code">preserve</code></a> and the
  2011. associated operation. This allows modifying other properties, like the
  2012. current color.
  2013. </p>
  2014. <table class="cartouche" border="1"><tr><th>
  2015. Example</th></tr><tr><td>
  2016. <p>In this example, the same path is used for both <a class="ref" href="#cmd_005ffill"><code class="code">fill</code></a> and
  2017. <a class="ref" href="#cmd_005fstroke"><code class="code">stroke</code></a>, but with different colors.
  2018. </p>
  2019. <pre class="verbatim">circle (w / 2) (h / 2) (w / 3)
  2020. setcolor skyblue
  2021. preserve fill
  2022. setlinewidth 10
  2023. setcolor seagreen
  2024. stroke
  2025. </pre>
  2026. </td></tr></table>
  2027. <p><a class="ref" href="#cmd_005fpreserve"><code class="code">preserve</code></a> can be called multiple times, if the same path has to be
  2028. used in 3 or more operations.
  2029. </p>
  2030. <table class="cartouche" border="1"><tr><th>
  2031. Example</th></tr><tr><td>
  2032. <p>In this example, the path created by <a class="ref" href="#cmd_005fcircle"><code class="code">circle</code></a> is used by
  2033. <a class="ref" href="#cmd_005ffill"><code class="code">fill</code></a>, <a class="ref" href="#cmd_005fstroke"><code class="code">stroke</code></a>, and <a class="ref" href="#cmd_005fclip"><code class="code">clip</code></a>. After <a class="ref" href="#cmd_005fclip"><code class="code">clip</code></a>, the
  2034. path is cleared.
  2035. </p>
  2036. <pre class="verbatim">circle 100 100 50
  2037. preserve fill
  2038. preserve stroke
  2039. clip
  2040. </pre>
  2041. </td></tr></table>
  2042. <a class="anchor" id="cmd_005fprint"></a><a name="print"></a>
  2043. <h3 class="section">4.25 <code class="code">print</code><span class="pull-right"><a class="anchor hidden-xs" href="#print" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-print" aria-hidden="true">TOC</a></span></h3>
  2044. <p><b class="b"><code class="code">print <var class="var">expr</var></code></b> &nbsp;&nbsp;&nbsp;&mdash; <a class="ref" href="#implicit-commands"><i class="i">Can be implicit</i></a>
  2045. </p>
  2046. <p>Print its arguments to the FFmpeg log.
  2047. </p>
  2048. <p>See the <a class="ref" href="#Command-print">Command print</a> section above for more details.
  2049. </p>
  2050. <a class="anchor" id="cmd_005fproc"></a><a name="proc"></a>
  2051. <h3 class="section">4.26 <code class="code">proc</code><span class="pull-right"><a class="anchor hidden-xs" href="#proc" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-proc" aria-hidden="true">TOC</a></span></h3>
  2052. <p><b class="b"><code class="code">proc <var class="var">name</var> <var class="var">params</var>* { <var class="var">block</var> }</code></b>
  2053. </p>
  2054. <p>Assign the block and the parameters for the procedure <var class="var">name</var>. The
  2055. procedure can be called multiple times with the <a class="ref" href="#cmd_005fcall"><code class="code">call</code></a> command.
  2056. </p>
  2057. <p>See the <a class="ref" href="#Procedures">Procedures</a> section above for more details.
  2058. </p>
  2059. <a class="anchor" id="cmd_005fQ"></a><a name="Q"></a>
  2060. <h3 class="section">4.27 <code class="code">Q</code><span class="pull-right"><a class="anchor hidden-xs" href="#Q" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-Q" aria-hidden="true">TOC</a></span></h3>
  2061. <p><b class="b"><code class="code">Q x1 y1 <var class="var">x</var> <var class="var">y</var></code></b>
  2062. </p>
  2063. <p>Draw a quadratic Bézier curve from the <a class="ref" href="#current-point">current point</a> to the <em class="emph">end
  2064. point</em> specified by <var class="var">x, y</var>. The <em class="emph">control point</em> is specified by
  2065. <var class="var">x1, y1</var>.
  2066. </p>
  2067. <p>The behaviour is identical to the <code class="code">Q</code> command in <a class="uref" href="https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Element/path">SVG&rsquo;s <code class="code">&lt;path&gt;</code></a>. For more
  2068. details, see <a class="uref" href="https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Attribute/d#quadratic_b%C3%A9zier_curve">Quadratic Bézier curve on MDN</a>, and the <a class="uref" href="https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorials/SVG_from_scratch/Paths#curve_commands">Curve Commands section of the Paths tutorial on MDN</a>.
  2069. </p>
  2070. <table class="cartouche" border="1"><tr><th>
  2071. Example</th></tr><tr><td>
  2072. <pre class="verbatim">moveto 20 20
  2073. Q
  2074. 0 h // control point
  2075. (w - 20) (h - 20) // end point
  2076. stroke
  2077. </pre>
  2078. </td></tr></table>
  2079. <a class="anchor" id="cmd_005fq"></a><a name="q"></a>
  2080. <h3 class="section">4.28 <code class="code">q</code><span class="pull-right"><a class="anchor hidden-xs" href="#q" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-q" aria-hidden="true">TOC</a></span></h3>
  2081. <p><b class="b"><code class="code">q <var class="var">dx1</var> <var class="var">dy1</var> <var class="var">dx</var> <var class="var">dy</var></code></b>
  2082. </p>
  2083. <p>Like <a class="ref" href="#cmd_005fQ"><code class="code">Q</code></a>, but the coordinates are relative to the <a class="ref" href="#current-point">current point</a>.
  2084. </p>
  2085. <a class="anchor" id="cmd_005fradialgrad"></a><a name="radialgrad"></a>
  2086. <h3 class="section">4.29 <code class="code">radialgrad</code><span class="pull-right"><a class="anchor hidden-xs" href="#radialgrad" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-radialgrad" aria-hidden="true">TOC</a></span></h3>
  2087. <p><b class="b"><code class="code">radialgrad <var class="var">cx0</var> <var class="var">cy0</var> <var class="var">radius0</var> <var class="var">cx1</var> <var class="var">cy1</var> <var class="var">radius1</var></code></b>
  2088. </p>
  2089. <p>Creates a new radial gradient between the two circles defined by
  2090. <var class="var">cx0 cy0 radius0</var> and <var class="var">cx1 cy1 radius1</var>. Each set of arguments
  2091. is the coordinates of the center and the radius.
  2092. </p>
  2093. <p>This gradient can be used for stroke and fill operations.
  2094. </p>
  2095. <p>Use <a class="ref" href="#cmd_005fcolorstop"><code class="code">colorstop</code></a> to set the color for each position in the gradient.
  2096. </p>
  2097. <table class="cartouche" border="1"><tr><th>
  2098. Example</th></tr><tr><td>
  2099. <p>The animation in the next example shows how the two circles defined in
  2100. the <a class="ref" href="#cmd_005fradialgrad"><code class="code">radialgrad</code></a> arguments interact with each other.
  2101. </p>
  2102. <p>The red circle represent the circle for the <var class="var">cx0 cy0 radius0</var>
  2103. arguments, and the yellow circle is the one for the
  2104. <var class="var">cx1 cy1 radius1</var> arguments.
  2105. </p>
  2106. <pre class="verbatim">setvar cx0 (mod(t * 30, w))
  2107. setvar cy0 120
  2108. setvar radius0 20
  2109. setvar cx1 120
  2110. setvar cy1 120
  2111. setvar radius1 70
  2112. radialgrad
  2113. cx0 cy0 radius0
  2114. cx1 cy1 radius1
  2115. colorstop
  2116. 0 lightblue
  2117. 1 darkblue
  2118. // Fill the frame with the gradient.
  2119. rect 0 0 w h
  2120. fill
  2121. // Draw inner circle.
  2122. circle cx0 cy0 radius0
  2123. setcolor red
  2124. stroke
  2125. // Draw outer circle.
  2126. circle cx1 cy1 radius1
  2127. setcolor yellow
  2128. stroke
  2129. </pre>
  2130. </td></tr></table>
  2131. <a class="anchor" id="cmd_005frect"></a><a name="rect"></a>
  2132. <h3 class="section">4.30 <code class="code">rect</code><span class="pull-right"><a class="anchor hidden-xs" href="#rect" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-rect" aria-hidden="true">TOC</a></span></h3>
  2133. <p><b class="b"><code class="code">rect <var class="var">x</var> <var class="var">y</var> <var class="var">width</var> <var class="var">height</var></code></b>
  2134. </p>
  2135. <p>Adds a rectangle of the given size (<var class="var">width</var> × <var class="var">height</var>), at
  2136. position <var class="var">x, y</var>, to the current path. The <a class="ref" href="#current-point">current point</a> is cleared
  2137. before and after adding the rectangle.
  2138. </p>
  2139. <p>See the documentation of the
  2140. <a class="uref" href="https://www.cairographics.org/manual/cairo-Paths.html#cairo-rectangle"><code class="code">cairo_rectangle</code></a>
  2141. function for more details.
  2142. </p>
  2143. <a class="anchor" id="cmd_005frepeat"></a><a name="repeat"></a>
  2144. <h3 class="section">4.31 <code class="code">repeat</code><span class="pull-right"><a class="anchor hidden-xs" href="#repeat" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-repeat" aria-hidden="true">TOC</a></span></h3>
  2145. <p><b class="b"><code class="code">repeat <var class="var">count</var> { <var class="var">block</var> }</code></b>
  2146. </p>
  2147. <p>Executes a block the number of times indicated by <var class="var">count</var>.
  2148. </p>
  2149. <p>In each iteration, the variable <var class="var">i</var> is used as a
  2150. <a class="uref" href="https://en.wikipedia.org/wiki/For_loop#Loop_counters">loop
  2151. counter</a>. It takes the values from <code class="code">0</code> to <code class="code">count - 1</code>. When
  2152. the loop is terminated, the variable is restored to the value before
  2153. starting the loop.
  2154. </p>
  2155. <p>If <var class="var">count</var> is less than <code class="code">1</code>, or it is not a finite number
  2156. (like <a class="uref" href="https://en.wikipedia.org/wiki/NaN"><code class="code">NaN</code></a>), the block is
  2157. not executed.
  2158. </p>
  2159. <a class="anchor" id="cmd_005fresetclip"></a><a name="resetclip"></a>
  2160. <h3 class="section">4.32 <code class="code">resetclip</code><span class="pull-right"><a class="anchor hidden-xs" href="#resetclip" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-resetclip" aria-hidden="true">TOC</a></span></h3>
  2161. <p><b class="b"><code class="code">resetclip</code></b>
  2162. </p>
  2163. <p>Reset the current clip region to its original state, covering the whole
  2164. frame.
  2165. </p>
  2166. <p>See the documentation of the
  2167. <a class="uref" href="https://www.cairographics.org/manual/cairo-cairo-t.html#cairo-reset-clip"><code class="code">cairo_reset_clip</code></a>
  2168. function for more details.
  2169. </p>
  2170. <a class="anchor" id="cmd_005fresetdash"></a><a name="resetdash"></a>
  2171. <h3 class="section">4.33 <code class="code">resetdash</code><span class="pull-right"><a class="anchor hidden-xs" href="#resetdash" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-resetdash" aria-hidden="true">TOC</a></span></h3>
  2172. <p><b class="b"><code class="code">resetdash</code></b>
  2173. </p>
  2174. <p>Disable the dash pattern to be used by <a class="ref" href="#cmd_005fstroke"><code class="code">stroke</code></a>. This reverts any
  2175. change made by <a class="ref" href="#cmd_005fsetdash"><code class="code">setdash</code></a> and <a class="ref" href="#cmd_005fsetdashoffset"><code class="code">setdashoffset</code></a>.
  2176. </p>
  2177. <p>It calls
  2178. <a class="uref" href="https://www.cairographics.org/manual/cairo-cairo-t.html#cairo-set-dash"><code class="code">cairo_set_dash</code></a>
  2179. with <code class="code">num_dashes</code> set to <code class="code">0</code>.
  2180. </p>
  2181. <a class="anchor" id="cmd_005fresetmatrix"></a><a name="resetmatrix"></a>
  2182. <h3 class="section">4.34 <code class="code">resetmatrix</code><span class="pull-right"><a class="anchor hidden-xs" href="#resetmatrix" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-resetmatrix" aria-hidden="true">TOC</a></span></h3>
  2183. <p><b class="b"><code class="code">resetmatrix</code></b>
  2184. </p>
  2185. <p>Resets the current <a class="ref" href="#transformation-matrix">transformation matrix</a>.
  2186. </p>
  2187. <a class="anchor" id="cmd_005frestore"></a><a name="restore"></a>
  2188. <h3 class="section">4.35 <code class="code">restore</code><span class="pull-right"><a class="anchor hidden-xs" href="#restore" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-restore" aria-hidden="true">TOC</a></span></h3>
  2189. <p><b class="b"><code class="code">restore</code></b>
  2190. </p>
  2191. <p>Restores the state saved by a preceding call to <a class="ref" href="#cmd_005fsave"><code class="code">save</code></a>.
  2192. </p>
  2193. <p>For more details, see the <a class="ref" href="#State-Stack">State Stack</a> section above, and the
  2194. <a class="uref" href="https://www.cairographics.org/manual/cairo-cairo-t.html#cairo-restore"><code class="code">cairo_restore</code></a>
  2195. function.
  2196. </p>
  2197. <a class="anchor" id="cmd_005frotate"></a><a name="rotate"></a>
  2198. <h3 class="section">4.36 <code class="code">rotate</code><span class="pull-right"><a class="anchor hidden-xs" href="#rotate" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-rotate" aria-hidden="true">TOC</a></span></h3>
  2199. <p><b class="b"><code class="code">rotate <var class="var">angle</var></code></b>
  2200. </p>
  2201. <p>Modifies the current <a class="ref" href="#transformation-matrix">transformation matrix</a> by rotating the user-space
  2202. axes by <var class="var">angle</var> radians.
  2203. </p>
  2204. <p>See the documentation of the
  2205. <a class="uref" href="https://www.cairographics.org/manual/cairo-Transformations.html#cairo-rotate"><code class="code">cairo_rotate</code></a>
  2206. function for more details.
  2207. </p>
  2208. <table class="cartouche" border="1"><tr><th>
  2209. Example</th></tr><tr><td>
  2210. <p>In this example:
  2211. </p>
  2212. <ul class="itemize mark-bullet">
  2213. <li><a class="ref" href="#cmd_005fscalexy"><code class="code">scalexy</code></a> maps the coordinates to a 1x1 frame.
  2214. </li><li><a class="ref" href="#cmd_005ftranslate"><code class="code">translate</code></a> put <code class="code">0, 0</code> at the center of the frame.
  2215. </li><li><a class="ref" href="#cmd_005frotate"><code class="code">rotate</code></a> rotates 45°.
  2216. </li><li><a class="ref" href="#cmd_005fresetmatrix"><code class="code">resetmatrix</code></a> reverts the transformations before <a class="ref" href="#cmd_005fstroke"><code class="code">stroke</code></a>, so the
  2217. line width is not affected by the scale.
  2218. </li></ul>
  2219. <pre class="verbatim">scalexy w h
  2220. translate 0.5 0.5
  2221. rotate (PI / 4)
  2222. rect -0.25 -0.25 0.5 0.5
  2223. resetmatrix
  2224. stroke
  2225. </pre>
  2226. </td></tr></table>
  2227. <a class="anchor" id="cmd_005froundedrect"></a><a name="roundedrect"></a>
  2228. <h3 class="section">4.37 <code class="code">roundedrect</code><span class="pull-right"><a class="anchor hidden-xs" href="#roundedrect" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-roundedrect" aria-hidden="true">TOC</a></span></h3>
  2229. <p><b class="b"><code class="code">roundedrect <var class="var">x</var> <var class="var">y</var> <var class="var">width</var> <var class="var">height</var> <var class="var">radius</var></code></b> &nbsp;&nbsp;&nbsp;&mdash; <a class="ref" href="#implicit-commands"><i class="i">Can be implicit</i></a>
  2230. </p>
  2231. <p>Like <a class="ref" href="#cmd_005frect"><code class="code">rect</code></a>, but a circular arc is used for the corners.
  2232. </p>
  2233. <table class="cartouche" border="1"><tr><th>
  2234. Example</th></tr><tr><td>
  2235. <p>The next example shows the same rectangle, with different values for the
  2236. corner radius.
  2237. </p>
  2238. <p>The radius is computed by multiplying <var class="var">i</var> (the
  2239. <a class="uref" href="https://en.wikipedia.org/wiki/For_loop#Loop_counters">loop counter</a>) by
  2240. <code class="code">4.5</code>. This number is chosen to make the last shape a perfect circle.
  2241. </p>
  2242. <div class="example">
  2243. <pre class="example-preformatted">repeat 9 {
  2244. roundedrect
  2245. (mod(i, 3) * 80 + 5) // x
  2246. (floor(i / 3) * 80 + 5) // y
  2247. 70 70 // size
  2248. (i * 4.5) // radius
  2249. }
  2250. stroke
  2251. </pre></div>
  2252. </td></tr></table>
  2253. <a class="anchor" id="cmd_005fsave"></a><a name="save"></a>
  2254. <h3 class="section">4.38 <code class="code">save</code><span class="pull-right"><a class="anchor hidden-xs" href="#save" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-save" aria-hidden="true">TOC</a></span></h3>
  2255. <p><b class="b"><code class="code">save</code></b>
  2256. </p>
  2257. <p>Saves a copy of the current state on an internal stack. This copy can be
  2258. restored later with <a class="ref" href="#cmd_005frestore"><code class="code">restore</code></a>.
  2259. </p>
  2260. <p>For more details, see the <a class="ref" href="#State-Stack">State Stack</a> section above, and the
  2261. <a class="uref" href="https://www.cairographics.org/manual/cairo-cairo-t.html#cairo-save"><code class="code">cairo_save</code></a>
  2262. function.
  2263. </p>
  2264. <a class="anchor" id="cmd_005fscale"></a><a name="scale"></a>
  2265. <h3 class="section">4.39 <code class="code">scale</code><span class="pull-right"><a class="anchor hidden-xs" href="#scale" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-scale" aria-hidden="true">TOC</a></span></h3>
  2266. <p><b class="b"><code class="code">scale <var class="var">sxy</var></code></b>
  2267. </p>
  2268. <p>Similar to <a class="ref" href="#cmd_005fscalexy"><code class="code">scalexy</code></a>, but the same value is used for both axes. It
  2269. is equivalent to:
  2270. </p>
  2271. <p><b class="b"><code class="code">scalexy <var class="var">sxy</var> <var class="var">sxy</var></code></b>
  2272. </p>
  2273. <a class="anchor" id="cmd_005fscalexy"></a><a name="scalexy"></a>
  2274. <h3 class="section">4.40 <code class="code">scalexy</code><span class="pull-right"><a class="anchor hidden-xs" href="#scalexy" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-scalexy" aria-hidden="true">TOC</a></span></h3>
  2275. <p><b class="b"><code class="code">scalexy <var class="var">sx</var> <var class="var">sy</var></code></b>
  2276. </p>
  2277. <p>Modifies the current <a class="ref" href="#transformation-matrix">transformation matrix</a> by scaling the X and Y
  2278. user-space axes by <var class="var">sx</var> and <var class="var">sy</var> respectively.
  2279. </p>
  2280. <p>See the documentation of the
  2281. <a class="uref" href="https://www.cairographics.org/manual/cairo-Transformations.html#cairo-scale"><code class="code">cairo_scale</code></a>
  2282. function for more details.
  2283. </p>
  2284. <p>See <a class="ref" href="#cmd_005frotate"><code class="code">rotate</code></a> for an example on combining multiple transformations.
  2285. </p>
  2286. <a class="anchor" id="cmd_005fsetcolor"></a><a name="setcolor"></a>
  2287. <h3 class="section">4.41 <code class="code">setcolor</code><span class="pull-right"><a class="anchor hidden-xs" href="#setcolor" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-setcolor" aria-hidden="true">TOC</a></span></h3>
  2288. <p><b class="b"><code class="code">setcolor <var class="var">color</var></code></b>
  2289. </p>
  2290. <p>Set a solid color as the <a class="ref" href="#current-pattern">current pattern</a> for stroke and fill operations
  2291. </p>
  2292. <p>See the <a class="ref" href="#Colors">Colors</a> section above for more details.
  2293. </p>
  2294. <a class="anchor" id="cmd_005fsetdash"></a><a name="setdash"></a>
  2295. <h3 class="section">4.42 <code class="code">setdash</code><span class="pull-right"><a class="anchor hidden-xs" href="#setdash" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-setdash" aria-hidden="true">TOC</a></span></h3>
  2296. <p><b class="b"><code class="code">setdash <var class="var">length</var></code></b> &nbsp;&nbsp;&nbsp;&mdash; <a class="ref" href="#implicit-commands"><i class="i">Can be implicit</i></a>
  2297. </p>
  2298. <p>Sets the dash pattern to be used by <a class="ref" href="#cmd_005fstroke"><code class="code">stroke</code></a>.
  2299. </p>
  2300. <p>Each call to <a class="ref" href="#cmd_005fsetdash"><code class="code">setdash</code></a> adds a length to the pattern, alternating
  2301. between <em class="emph">on</em> and <em class="emph">off</em> portions of the stroke.
  2302. </p>
  2303. <p>After a call to <a class="ref" href="#cmd_005fsetdash"><code class="code">setdash</code></a>, <a class="ref" href="#cmd_005fresetdash"><code class="code">resetdash</code></a> is needed either to
  2304. create a new pattern, or to discard the current one.
  2305. </p>
  2306. <p>See the documentation of the
  2307. <a class="uref" href="https://www.cairographics.org/manual/cairo-cairo-t.html#cairo-set-dash"><code class="code">cairo_set_dash</code></a>
  2308. function for more details.
  2309. </p>
  2310. <a class="anchor" id="cmd_005fsetdashoffset"></a><a name="setdashoffset"></a>
  2311. <h3 class="section">4.43 <code class="code">setdashoffset</code><span class="pull-right"><a class="anchor hidden-xs" href="#setdashoffset" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-setdashoffset" aria-hidden="true">TOC</a></span></h3>
  2312. <p><b class="b"><code class="code">setdashoffset <var class="var">offset</var></code></b>
  2313. </p>
  2314. <p>Set the offset into the dash pattern at which the stroke should start.
  2315. </p>
  2316. <p><a class="ref" href="#cmd_005fsetdash"><code class="code">setdash</code></a> must be called <em class="emph">before</em> <a class="ref" href="#cmd_005fsetdashoffset"><code class="code">setdashoffset</code></a>.
  2317. </p>
  2318. <p>See the documentation of the
  2319. <a class="uref" href="https://www.cairographics.org/manual/cairo-cairo-t.html#cairo-set-dash"><code class="code">cairo_set_dash</code></a>
  2320. function for more details.
  2321. </p>
  2322. <table class="cartouche" border="1"><tr><th>
  2323. Example</th></tr><tr><td>
  2324. <p>The next animation shows the effect of <a class="ref" href="#cmd_005fsetdashoffset"><code class="code">setdashoffset</code></a> when its
  2325. argument changes over time.
  2326. </p>
  2327. <pre class="verbatim">scalexy w h
  2328. M 0.5 1
  2329. curveto 0 0.5, 1 0.5, 0.5 0
  2330. resetmatrix
  2331. setdash 20 5 // 20 on, 5 off
  2332. setdashoffset (t * 100)
  2333. setlinewidth 20
  2334. stroke
  2335. </pre>
  2336. </td></tr></table>
  2337. <a class="anchor" id="cmd_005fsethsla"></a><a name="sethsla"></a>
  2338. <h3 class="section">4.44 <code class="code">sethsla</code><span class="pull-right"><a class="anchor hidden-xs" href="#sethsla" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-sethsla" aria-hidden="true">TOC</a></span></h3>
  2339. <p><b class="b"><code class="code">sethsla <var class="var">h</var> <var class="var">s</var> <var class="var">l</var> <var class="var">a</var></code></b>
  2340. </p>
  2341. <p>Set the <a class="ref" href="#current-pattern">current pattern</a> to a solid color, given the <em class="emph">hue</em>,
  2342. <em class="emph">saturation</em>, and <em class="emph">lightness</em>, and <em class="emph">alpha</em> components.
  2343. </p>
  2344. <p>h is the <em class="emph">hue</em>, a value between <code class="code">0</code> and <code class="code">359</code>. Negative
  2345. values are clamped to <code class="code">0</code>, and values greater than <code class="code">359</code> are
  2346. interpreted as modulo 360.
  2347. </p>
  2348. <p>s (<em class="emph">saturation</em>), l (<em class="emph">lightness</em>), and a (<em class="emph">alpha</em>), are
  2349. values between <code class="code">0</code> and <code class="code">1</code>.
  2350. </p>
  2351. <p>The conversion to RGB is implemented according to the
  2352. <a class="uref" href="https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB">formulae from
  2353. Wikipedia</a>.
  2354. </p>
  2355. <a class="anchor" id="cmd_005fsetlinecap"></a><a name="setlinecap"></a>
  2356. <h3 class="section">4.45 <code class="code">setlinecap</code><span class="pull-right"><a class="anchor hidden-xs" href="#setlinecap" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-setlinecap" aria-hidden="true">TOC</a></span></h3>
  2357. <p><b class="b"><code class="code">setlinecap <var class="var">cap</var></code></b>
  2358. </p>
  2359. <p>Set the current line cap style, which determines the shape used to draw
  2360. the end points of lines.
  2361. </p>
  2362. <p><var class="var">cap</var> must be one of the following names:
  2363. </p>
  2364. <ul class="itemize mark-bullet">
  2365. <li><code class="code">butt</code>
  2366. </li><li><code class="code">round</code>
  2367. </li><li><code class="code">square</code>
  2368. </li></ul>
  2369. <p>It calls to
  2370. <a class="uref" href="https://www.cairographics.org/manual/cairo-cairo-t.html#cairo-set-line-cap"><code class="code">cairo_set_line_cap</code></a>
  2371. to set the line cap style.
  2372. </p>
  2373. <table class="cartouche" border="1"><tr><th>
  2374. Example</th></tr><tr><td>
  2375. <p>This example draws 3 lines with the same length, each one with a
  2376. different line cap style:
  2377. </p>
  2378. <pre class="verbatim">setlinewidth 40
  2379. setlinecap butt
  2380. setcolor tomato
  2381. M 60 40 v 100 stroke
  2382. setlinecap round
  2383. setcolor seagreen
  2384. M 120 40 v 100 stroke
  2385. setlinecap square
  2386. setcolor skyblue
  2387. M 180 40 v 100 stroke
  2388. M 20 40 H 220 m 0 100 H 20
  2389. setcolor black@0.5
  2390. setlinewidth 2
  2391. stroke
  2392. </pre>
  2393. </td></tr></table>
  2394. <a class="anchor" id="cmd_005fsetlinejoin"></a><a name="setlinejoin"></a>
  2395. <h3 class="section">4.46 <code class="code">setlinejoin</code><span class="pull-right"><a class="anchor hidden-xs" href="#setlinejoin" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-setlinejoin" aria-hidden="true">TOC</a></span></h3>
  2396. <p><b class="b"><code class="code">setlinejoin <var class="var">join</var></code></b>
  2397. </p>
  2398. <p>Sets the current line join style, which determines the shape used to
  2399. join two line segments.
  2400. </p>
  2401. <p><var class="var">join</var> must be one of the following names:
  2402. </p>
  2403. <ul class="itemize mark-bullet">
  2404. <li><code class="code">bevel</code>
  2405. </li><li><code class="code">miter</code>
  2406. </li><li><code class="code">round</code>
  2407. </li></ul>
  2408. <p>It calls to
  2409. <a class="uref" href="https://www.cairographics.org/manual/cairo-cairo-t.html#cairo-set-line-join"><code class="code">cairo_set_line_join</code></a>
  2410. to set the line join style.
  2411. </p>
  2412. <table class="cartouche" border="1"><tr><th>
  2413. Example</th></tr><tr><td>
  2414. <p>This example draws 3 lines with the same length, each one with a
  2415. different line join style:
  2416. </p>
  2417. <pre class="verbatim">setlinewidth 30
  2418. setlinejoin bevel
  2419. setcolor tomato
  2420. M 70 20 l 50 50 50 -50 stroke
  2421. setlinejoin miter
  2422. setcolor seagreen
  2423. M 70 90 l 50 50 50 -50 stroke
  2424. setlinejoin round
  2425. setcolor skyblue
  2426. M 70 160 l 50 50 50 -50 stroke
  2427. </pre>
  2428. </td></tr></table>
  2429. <a class="anchor" id="cmd_005fsetlinewidth"></a><a name="setlinewidth"></a>
  2430. <h3 class="section">4.47 <code class="code">setlinewidth</code><span class="pull-right"><a class="anchor hidden-xs" href="#setlinewidth" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-setlinewidth" aria-hidden="true">TOC</a></span></h3>
  2431. <p><b class="b"><code class="code">setlinewidth <var class="var">width</var></code></b>
  2432. </p>
  2433. <p>Set the line width for <a class="ref" href="#cmd_005fstroke"><code class="code">stroke</code></a>.
  2434. </p>
  2435. <p><var class="var">width</var> is affected by the <a class="ref" href="#transformation-matrix">transformation matrix</a>.
  2436. </p>
  2437. <p>To specify a width that is not affected by other transformations,
  2438. <a class="ref" href="#cmd_005fresetmatrix"><code class="code">resetmatrix</code></a> can be used between <a class="ref" href="#cmd_005fsave"><code class="code">save</code></a> / <a class="ref" href="#cmd_005frestore"><code class="code">restore</code></a>:
  2439. </p>
  2440. <pre class="verbatim">save
  2441. resetmatrix
  2442. setlinewidth 1
  2443. stroke
  2444. // Restore matrix after stroke.
  2445. restore
  2446. </pre>
  2447. <p>See the documentation of the
  2448. <a class="uref" href="https://www.cairographics.org/manual/cairo-cairo-t.html#cairo-set-line-width"><code class="code">cairo_set_line_width</code></a>
  2449. function for more details.
  2450. </p>
  2451. <a class="anchor" id="cmd_005fsetrgba"></a><a name="setrgba"></a>
  2452. <h3 class="section">4.48 <code class="code">setrgba</code><span class="pull-right"><a class="anchor hidden-xs" href="#setrgba" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-setrgba" aria-hidden="true">TOC</a></span></h3>
  2453. <p><b class="b"><code class="code">setrgba <var class="var">r</var> <var class="var">g</var> <var class="var">b</var> <var class="var">a</var></code></b>
  2454. </p>
  2455. <p>Set the <a class="ref" href="#current-pattern">current pattern</a> to a solid color, given the
  2456. <em class="emph">red</em>, <em class="emph">green</em>, <em class="emph">blue</em>, and <em class="emph">alpha</em> components.
  2457. </p>
  2458. <p>All components are values between <code class="code">0</code> and <code class="code">1</code>. Values outside
  2459. that range are clamped to it.
  2460. </p>
  2461. <a class="anchor" id="cmd_005fsetvar"></a><a name="setvar"></a>
  2462. <h3 class="section">4.49 <code class="code">setvar</code><span class="pull-right"><a class="anchor hidden-xs" href="#setvar" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-setvar" aria-hidden="true">TOC</a></span></h3>
  2463. <p><b class="b"><code class="code">setvar <var class="var">varname</var> <var class="var">value</var></code></b>
  2464. </p>
  2465. <p>Set the variable <var class="var">varname</var> to <var class="var">value</var>.
  2466. </p>
  2467. <p>See the <a class="ref" href="#User-Variables">User Variables</a> section above for more details.
  2468. </p>
  2469. <a class="anchor" id="cmd_005fstroke"></a><a name="stroke"></a>
  2470. <h3 class="section">4.50 <code class="code">stroke</code><span class="pull-right"><a class="anchor hidden-xs" href="#stroke" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-stroke" aria-hidden="true">TOC</a></span></h3>
  2471. <p><b class="b"><code class="code">stroke</code></b>
  2472. </p>
  2473. <p>Strokes the current path according to the current line width, line join,
  2474. line cap, and dash settings.
  2475. </p>
  2476. <p>The path is cleared after the operation, unless the <a class="ref" href="#cmd_005fpreserve"><code class="code">preserve</code></a>
  2477. command is used before <a class="ref" href="#cmd_005fstroke"><code class="code">stroke</code></a>.
  2478. </p>
  2479. <p>See the documentation of the
  2480. <a class="uref" href="https://www.cairographics.org/manual/cairo-cairo-t.html#cairo-stroke"><code class="code">cairo_stroke</code></a>
  2481. function for more details.
  2482. </p>
  2483. <a class="anchor" id="cmd_005fS"></a><a class="anchor" id="cmd_005fs"></a><a name="S_002c-s"></a>
  2484. <h3 class="section">4.51 <code class="code">S</code>, <code class="code">s</code><span class="pull-right"><a class="anchor hidden-xs" href="#S_002c-s" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-S_002c-s" aria-hidden="true">TOC</a></span></h3>
  2485. <p><b class="b"><code class="code">S, s <var class="var">x2</var> <var class="var">y2</var> <var class="var">x</var> <var class="var">y</var></code></b> &nbsp;&nbsp;&nbsp;&mdash; <a class="ref" href="#implicit-commands"><i class="i">Can be implicit</i></a>
  2486. </p>
  2487. <p>Draw a smooth cubic Bézier curve from the <a class="ref" href="#current-point">current point</a> to the <em class="emph">end
  2488. point</em> specified by <var class="var">x, y</var>. The <em class="emph">end control point</em> is specified by
  2489. <var class="var">x2, y2</var>.
  2490. </p>
  2491. <p>The <em class="emph">start control point</em> is the reflection of the <em class="emph">end
  2492. control point</em> of the previous curve command about the <em class="emph">current
  2493. point</em>.
  2494. </p>
  2495. <p>The behaviour is identical to the <code class="code">S</code> command in <a class="uref" href="https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Element/path">SVG&rsquo;s <code class="code">&lt;path&gt;</code></a>. For more
  2496. details, see <a class="uref" href="https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Attribute/d#cubic_b%C3%A9zier_curve">Cubic Bézier Curve on MDN</a>, and the <a class="uref" href="https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorials/SVG_from_scratch/Paths#curve_commands">Curve Commands section of the Paths tutorial on MDN</a>.
  2497. </p>
  2498. <p><a class="ref" href="#cmd_005fs"><code class="code">s</code></a> is like <a class="ref" href="#cmd_005fS"><code class="code">S</code></a>, but the coordinates are relative to the <a class="ref" href="#current-point">current point</a>.
  2499. </p>
  2500. <table class="cartouche" border="1"><tr><th>
  2501. Example</th></tr><tr><td>
  2502. <div class="example">
  2503. <pre class="example-preformatted">M 20 120
  2504. c 25 -50, 25 50, 50 0
  2505. repeat 3 {
  2506. s 20 50, 50 0
  2507. }
  2508. stroke
  2509. </pre></div>
  2510. </td></tr></table>
  2511. <a class="anchor" id="cmd_005ftranslate"></a><a name="translate"></a>
  2512. <h3 class="section">4.52 <code class="code">translate</code><span class="pull-right"><a class="anchor hidden-xs" href="#translate" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-translate" aria-hidden="true">TOC</a></span></h3>
  2513. <p><b class="b"><code class="code">translate <var class="var">tx</var> <var class="var">ty</var></code></b>
  2514. </p>
  2515. <p>Modifies the current <a class="ref" href="#transformation-matrix">transformation matrix</a> by
  2516. translating the user-space origin by <var class="var">tx, ty</var>.
  2517. </p>
  2518. <p>See the documentation of the
  2519. <a class="uref" href="https://www.cairographics.org/manual/cairo-Transformations.html#cairo-translate"><code class="code">cairo_translate</code></a>
  2520. function for more details.
  2521. </p>
  2522. <a class="anchor" id="cmd_005fT"></a><a class="anchor" id="cmd_005ft"></a><a name="T_002c-t"></a>
  2523. <h3 class="section">4.53 <code class="code">T</code>, <code class="code">t</code><span class="pull-right"><a class="anchor hidden-xs" href="#T_002c-t" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-T_002c-t" aria-hidden="true">TOC</a></span></h3>
  2524. <p><b class="b"><code class="code">T, t <var class="var">x</var> <var class="var">y</var></code></b> &nbsp;&nbsp;&nbsp;&mdash; <a class="ref" href="#implicit-commands"><i class="i">Can be implicit</i></a>
  2525. </p>
  2526. <p>Draw a smooth quadratic Bézier curve from the <a class="ref" href="#current-point">current point</a> to the
  2527. <em class="emph">end point</em> specified by <var class="var">x, y</var>.
  2528. </p>
  2529. <p>The <em class="emph">control point</em> is the reflection of the <em class="emph">control point</em>
  2530. of the previous curve command about the <em class="emph">current point</em>.
  2531. </p>
  2532. <p>The behaviour is identical to the <code class="code">T</code> command in <a class="uref" href="https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Element/path">SVG&rsquo;s <code class="code">&lt;path&gt;</code></a>. For more
  2533. details, see <a class="uref" href="https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Attribute/d#quadratic_b%C3%A9zier_curve">Quadratic Bézier curve on MDN</a>, and the <a class="uref" href="https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorials/SVG_from_scratch/Paths#curve_commands">Curve Commands section of the Paths tutorial on MDN</a>.
  2534. </p>
  2535. <p><a class="ref" href="#cmd_005ft"><code class="code">t</code></a> is like <a class="ref" href="#cmd_005fT"><code class="code">T</code></a>, but the coordinates are relative to the
  2536. <a class="ref" href="#current-point">current point</a>.
  2537. </p>
  2538. <table class="cartouche" border="1"><tr><th>
  2539. Example</th></tr><tr><td>
  2540. <div class="example">
  2541. <pre class="example-preformatted">M 20 120
  2542. q 10 -20, 20 0
  2543. repeat 9 {
  2544. t 20 0
  2545. }
  2546. stroke
  2547. </pre></div>
  2548. </td></tr></table>
  2549. <a class="anchor" id="cmd_005fV"></a><a class="anchor" id="cmd_005fv"></a><a name="V_002c-v"></a>
  2550. <h3 class="section">4.54 <code class="code">V</code>, <code class="code">v</code><span class="pull-right"><a class="anchor hidden-xs" href="#V_002c-v" aria-hidden="true">#</a> <a class="anchor hidden-xs"href="#toc-V_002c-v" aria-hidden="true">TOC</a></span></h3>
  2551. <p><b class="b"><code class="code">V, v <var class="var">y</var></code></b> &nbsp;&nbsp;&nbsp;&mdash; <a class="ref" href="#implicit-commands"><i class="i">Can be implicit</i></a>
  2552. </p>
  2553. <p>Draw a vertical line from the <a class="ref" href="#current-point">current point</a> to y.
  2554. </p>
  2555. <p>The coordinate for <a class="ref" href="#cmd_005fV"><code class="code">V</code></a> is absolute, and for <a class="ref" href="#cmd_005fv"><code class="code">v</code></a> it is relative
  2556. to the <a class="ref" href="#current-point">current point</a>.
  2557. </p>
  2558. <p style="font-size: small;">
  2559. This document was generated using <a class="uref" href="https://www.gnu.org/software/texinfo/"><em class="emph">makeinfo</em></a>.
  2560. </p>
  2561. </div>
  2562. </body>
  2563. </html>