cairo-script-operators.c 156 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793
  1. /*
  2. * Copyright © 2008 Chris Wilson <chris@chris-wilson.co.uk>
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it either under the terms of the GNU Lesser General Public
  6. * License version 2.1 as published by the Free Software Foundation
  7. * (the "LGPL") or, at your option, under the terms of the Mozilla
  8. * Public License Version 1.1 (the "MPL"). If you do not alter this
  9. * notice, a recipient may use your version of this file under either
  10. * the MPL or the LGPL.
  11. *
  12. * You should have received a copy of the LGPL along with this library
  13. * in the file COPYING-LGPL-2.1; if not, write to the Free Software
  14. * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
  15. * You should have received a copy of the MPL along with this library
  16. * in the file COPYING-MPL-1.1
  17. *
  18. * The contents of this file are subject to the Mozilla Public License
  19. * Version 1.1 (the "License"); you may not use this file except in
  20. * compliance with the License. You may obtain a copy of the License at
  21. * http://www.mozilla.org/MPL/
  22. *
  23. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
  24. * OF ANY KIND, either express or implied. See the LGPL or the MPL for
  25. * the specific language governing rights and limitations.
  26. *
  27. * The Original Code is the cairo graphics library.
  28. *
  29. * The Initial Developer of the Original Code is Chris Wilson.
  30. *
  31. * Contributor(s):
  32. * Chris Wilson <chris@chris-wilson.co.uk>
  33. */
  34. #include "config.h"
  35. /* TODO real path type */
  36. #include "cairo-script-private.h"
  37. #if CAIRO_HAS_SCRIPT_SURFACE
  38. #include "cairo-script.h"
  39. #endif
  40. #include <stdio.h> /* snprintf */
  41. #include <stdlib.h> /* mkstemp */
  42. #include <string.h>
  43. #ifdef _MSC_VER
  44. #define _USE_MATH_DEFINES /* for M_LN2, M_PI and M_SQRT2 on win32 */
  45. #define snprintf _snprintf
  46. #endif
  47. #include <math.h>
  48. #include <limits.h> /* INT_MAX */
  49. #include <assert.h>
  50. #if HAVE_ZLIB
  51. #include <zlib.h>
  52. #endif
  53. #if HAVE_LZO
  54. #include <lzo/lzo2a.h>
  55. #endif
  56. #ifdef HAVE_MMAP
  57. # ifdef HAVE_UNISTD_H
  58. # include <sys/mman.h>
  59. # include <unistd.h>
  60. # else
  61. # undef HAVE_MMAP
  62. # endif
  63. #endif
  64. typedef struct _csi_proxy {
  65. csi_t *ctx;
  66. void *ptr;
  67. csi_dictionary_t *dictionary;
  68. csi_destroy_func_t destroy_func;
  69. void *destroy_data;
  70. } csi_proxy_t;
  71. typedef struct _csi_blob {
  72. csi_list_t list;
  73. unsigned long hash;
  74. uint8_t *bytes;
  75. unsigned int len;
  76. } csi_blob_t;
  77. static const cairo_user_data_key_t _csi_proxy_key;
  78. static const cairo_user_data_key_t _csi_blob_key;
  79. enum mime_type {
  80. MIME_TYPE_NONE = 0,
  81. MIME_TYPE_PNG
  82. };
  83. #define check(CNT) do {\
  84. if (_csi_unlikely (! _csi_check_ostack (ctx, (CNT)))) \
  85. return _csi_error (CSI_STATUS_INVALID_SCRIPT); \
  86. } while (0)
  87. #define pop(CNT) _csi_pop_ostack (ctx, (CNT))
  88. #define push(OBJ) _csi_push_ostack (ctx, (OBJ))
  89. static csi_proxy_t *
  90. _csi_proxy_create (csi_t *ctx,
  91. void *ptr,
  92. csi_dictionary_t *dictionary,
  93. csi_destroy_func_t destroy_func,
  94. void *destroy_data)
  95. {
  96. csi_proxy_t *proxy;
  97. proxy = _csi_slab_alloc (ctx, sizeof (csi_proxy_t));
  98. if (proxy == NULL)
  99. return NULL;
  100. proxy->ctx = cairo_script_interpreter_reference (ctx);
  101. proxy->ptr = ptr;
  102. proxy->destroy_func = destroy_func;
  103. proxy->destroy_data = destroy_data;
  104. proxy->dictionary = dictionary;
  105. if (dictionary != NULL)
  106. dictionary->base.ref++;
  107. return proxy;
  108. }
  109. static void
  110. _csi_proxy_destroy (void *closure)
  111. {
  112. csi_proxy_t *proxy = closure;
  113. csi_t *ctx = proxy->ctx;
  114. /* XXX this doesn't work because user_data_destroy is called too late.
  115. * Considering another hook into the (cairo internal) object system.
  116. */
  117. if (proxy->destroy_func != NULL)
  118. proxy->destroy_func (proxy->destroy_data, proxy->ptr);
  119. if (proxy->dictionary != NULL && --proxy->dictionary->base.ref == 0)
  120. csi_dictionary_free (ctx, proxy->dictionary);
  121. _csi_slab_free (ctx, proxy, sizeof (csi_proxy_t));
  122. cairo_script_interpreter_destroy (ctx);
  123. }
  124. static void
  125. _csi_blob_hash (csi_blob_t *blob, const uint32_t *data, int len)
  126. {
  127. unsigned long hash = blob->hash;
  128. /* very simple! */
  129. while (len--) {
  130. unsigned long c = *data++;
  131. hash *= 33;
  132. hash ^= c;
  133. }
  134. blob->hash = hash;
  135. }
  136. static csi_boolean_t
  137. _csi_blob_equal (const csi_list_t *link, void *data)
  138. {
  139. csi_blob_t *A, *B;
  140. A = csi_container_of (link, csi_blob_t, list);
  141. B = data;
  142. if (A->len != B->len)
  143. return FALSE;
  144. if (A->hash != B->hash)
  145. return FALSE;
  146. return memcmp (A->bytes, B->bytes, A->len) == 0;
  147. }
  148. static void
  149. _csi_blob_init (csi_blob_t *blob, uint8_t *bytes, int len)
  150. {
  151. blob->hash = 5381;
  152. blob->len = len;
  153. blob->bytes = bytes;
  154. }
  155. static csi_list_t *
  156. _csi_list_unlink (csi_list_t *head, csi_list_t *link)
  157. {
  158. if (link->next != NULL)
  159. link->next->prev = link->prev;
  160. if (link->prev != NULL)
  161. link->prev->next = link->next;
  162. else
  163. head = link->next;
  164. return head;
  165. }
  166. static csi_list_t *
  167. _csi_list_prepend (csi_list_t *head, csi_list_t *link)
  168. {
  169. if (head != NULL)
  170. head->prev = link;
  171. link->next = head;
  172. link->prev = NULL;
  173. return link;
  174. }
  175. static csi_list_t *
  176. _csi_list_find (csi_list_t *head,
  177. csi_boolean_t (*predicate) (const csi_list_t *link, void *data),
  178. void *data)
  179. {
  180. while (head != NULL) {
  181. if (predicate (head, data))
  182. return head;
  183. head = head->next;
  184. }
  185. return NULL;
  186. }
  187. static csi_status_t
  188. _csi_ostack_get_boolean (csi_t *ctx, unsigned int i, csi_boolean_t *out)
  189. {
  190. csi_object_t *obj;
  191. int type;
  192. obj = _csi_peek_ostack (ctx, i);
  193. type = csi_object_get_type (obj);
  194. switch (type) {
  195. case CSI_OBJECT_TYPE_BOOLEAN:
  196. *out = obj->datum.boolean;
  197. break;
  198. case CSI_OBJECT_TYPE_INTEGER:
  199. *out = !! obj->datum.integer;
  200. break;
  201. case CSI_OBJECT_TYPE_REAL:
  202. *out = obj->datum.real != 0.;
  203. break;
  204. default:
  205. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  206. }
  207. return CSI_STATUS_SUCCESS;
  208. }
  209. static csi_status_t
  210. _csi_ostack_get_integer (csi_t *ctx, unsigned int i, csi_integer_t *out)
  211. {
  212. csi_object_t *obj;
  213. int type;
  214. obj = _csi_peek_ostack (ctx, i);
  215. type = csi_object_get_type (obj);
  216. switch (type) {
  217. case CSI_OBJECT_TYPE_BOOLEAN:
  218. *out = obj->datum.boolean;
  219. break;
  220. case CSI_OBJECT_TYPE_INTEGER:
  221. *out = obj->datum.integer;
  222. break;
  223. case CSI_OBJECT_TYPE_REAL:
  224. *out = obj->datum.real;
  225. break;
  226. default:
  227. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  228. }
  229. return CSI_STATUS_SUCCESS;
  230. }
  231. static csi_status_t
  232. _csi_ostack_get_number (csi_t *ctx, unsigned int i, double *out)
  233. {
  234. csi_object_t *obj;
  235. int type;
  236. obj = _csi_peek_ostack (ctx, i);
  237. type = csi_object_get_type (obj);
  238. switch (type) {
  239. case CSI_OBJECT_TYPE_BOOLEAN:
  240. *out = obj->datum.boolean;
  241. break;
  242. case CSI_OBJECT_TYPE_INTEGER:
  243. *out = obj->datum.integer;
  244. break;
  245. case CSI_OBJECT_TYPE_REAL:
  246. *out = obj->datum.real;
  247. break;
  248. default:
  249. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  250. }
  251. return CSI_STATUS_SUCCESS;
  252. }
  253. static double
  254. _csi_object_as_real (csi_object_t *obj)
  255. {
  256. int type;
  257. type = csi_object_get_type (obj);
  258. switch (type) {
  259. case CSI_OBJECT_TYPE_BOOLEAN:
  260. return obj->datum.boolean;
  261. case CSI_OBJECT_TYPE_INTEGER:
  262. return obj->datum.integer;
  263. case CSI_OBJECT_TYPE_REAL:
  264. return obj->datum.real;
  265. default:
  266. return 0;
  267. }
  268. }
  269. static csi_status_t
  270. _csi_ostack_get_name (csi_t *ctx, unsigned int i, csi_name_t *out)
  271. {
  272. csi_object_t *obj;
  273. obj = _csi_peek_ostack (ctx, i);
  274. if (_csi_unlikely (csi_object_get_type (obj) != CSI_OBJECT_TYPE_NAME))
  275. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  276. *out = obj->datum.name;
  277. return CSI_STATUS_SUCCESS;
  278. }
  279. static csi_status_t
  280. _csi_ostack_get_context (csi_t *ctx, unsigned int i, cairo_t **out)
  281. {
  282. csi_object_t *obj;
  283. obj = _csi_peek_ostack (ctx, i);
  284. if (_csi_unlikely (csi_object_get_type (obj) != CSI_OBJECT_TYPE_CONTEXT))
  285. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  286. *out = obj->datum.cr;
  287. return CSI_STATUS_SUCCESS;
  288. }
  289. static csi_status_t
  290. _csi_ostack_get_font_face (csi_t *ctx, unsigned int i, cairo_font_face_t **out)
  291. {
  292. csi_object_t *obj;
  293. obj = _csi_peek_ostack (ctx, i);
  294. if (_csi_unlikely (csi_object_get_type (obj) != CSI_OBJECT_TYPE_FONT))
  295. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  296. *out = obj->datum.font_face;
  297. return CSI_STATUS_SUCCESS;
  298. }
  299. static csi_status_t
  300. _csi_ostack_get_pattern (csi_t *ctx, unsigned int i, cairo_pattern_t **out)
  301. {
  302. csi_object_t *obj;
  303. obj = _csi_peek_ostack (ctx, i);
  304. if (_csi_unlikely (csi_object_get_type (obj) != CSI_OBJECT_TYPE_PATTERN))
  305. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  306. *out = obj->datum.pattern;
  307. return CSI_STATUS_SUCCESS;
  308. }
  309. static csi_status_t
  310. _csi_ostack_get_scaled_font (csi_t *ctx, unsigned int i,
  311. cairo_scaled_font_t **out)
  312. {
  313. csi_object_t *obj;
  314. obj = _csi_peek_ostack (ctx, i);
  315. if (_csi_unlikely
  316. (csi_object_get_type (obj) != CSI_OBJECT_TYPE_SCALED_FONT))
  317. {
  318. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  319. }
  320. *out = obj->datum.scaled_font;
  321. return CSI_STATUS_SUCCESS;
  322. }
  323. static csi_status_t
  324. _csi_ostack_get_surface (csi_t *ctx, unsigned int i, cairo_surface_t **out)
  325. {
  326. csi_object_t *obj;
  327. int type;
  328. obj = _csi_peek_ostack (ctx, i);
  329. type = csi_object_get_type (obj);
  330. switch (type) {
  331. case CSI_OBJECT_TYPE_CONTEXT:
  332. *out = cairo_get_target (obj->datum.cr);
  333. break;
  334. case CSI_OBJECT_TYPE_SURFACE:
  335. *out = obj->datum.surface;
  336. break;
  337. default:
  338. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  339. }
  340. return CSI_STATUS_SUCCESS;
  341. }
  342. static csi_status_t
  343. _csi_ostack_get_array (csi_t *ctx, unsigned int i, csi_array_t **out)
  344. {
  345. csi_object_t *obj;
  346. obj = _csi_peek_ostack (ctx, i);
  347. if (_csi_unlikely (csi_object_get_type (obj) != CSI_OBJECT_TYPE_ARRAY))
  348. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  349. *out = obj->datum.array;
  350. return CSI_STATUS_SUCCESS;
  351. }
  352. static csi_status_t
  353. _csi_ostack_get_procedure (csi_t *ctx, unsigned int i, csi_array_t **out)
  354. {
  355. csi_object_t *obj;
  356. obj = _csi_peek_ostack (ctx, i);
  357. if (_csi_unlikely (! csi_object_is_procedure (obj)))
  358. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  359. *out = obj->datum.array;
  360. return CSI_STATUS_SUCCESS;
  361. }
  362. static csi_status_t
  363. _csi_ostack_get_dictionary (csi_t *ctx, unsigned int i, csi_dictionary_t **out)
  364. {
  365. csi_object_t *obj;
  366. obj = _csi_peek_ostack (ctx, i);
  367. if (_csi_unlikely
  368. (csi_object_get_type (obj) != CSI_OBJECT_TYPE_DICTIONARY))
  369. {
  370. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  371. }
  372. *out = obj->datum.dictionary;
  373. return CSI_STATUS_SUCCESS;
  374. }
  375. static csi_status_t
  376. _csi_ostack_get_matrix (csi_t *ctx, unsigned int i, cairo_matrix_t *out)
  377. {
  378. csi_object_t *obj;
  379. int type;
  380. obj = _csi_peek_ostack (ctx, i);
  381. type = csi_object_get_type (obj);
  382. switch (type) {
  383. case CSI_OBJECT_TYPE_MATRIX:
  384. *out = obj->datum.matrix->matrix;
  385. return CSI_STATUS_SUCCESS;
  386. case CSI_OBJECT_TYPE_ARRAY:
  387. if (obj->datum.array->stack.len == 6) {
  388. cairo_matrix_init (out,
  389. csi_number_get_value (&obj->datum.array->stack.objects[0]),
  390. csi_number_get_value (&obj->datum.array->stack.objects[1]),
  391. csi_number_get_value (&obj->datum.array->stack.objects[2]),
  392. csi_number_get_value (&obj->datum.array->stack.objects[3]),
  393. csi_number_get_value (&obj->datum.array->stack.objects[4]),
  394. csi_number_get_value (&obj->datum.array->stack.objects[5]));
  395. return CSI_STATUS_SUCCESS;
  396. }
  397. default:
  398. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  399. }
  400. }
  401. static csi_status_t
  402. _csi_dictionary_get_integer (csi_t *ctx,
  403. csi_dictionary_t *dict,
  404. const char *name,
  405. csi_boolean_t optional,
  406. long *value)
  407. {
  408. csi_status_t status;
  409. csi_object_t key, obj;
  410. int type;
  411. status = csi_name_new_static (ctx, &key, name);
  412. if (_csi_unlikely (status))
  413. return status;
  414. if (optional && ! csi_dictionary_has (dict, key.datum.name))
  415. return CSI_STATUS_SUCCESS;
  416. status = csi_dictionary_get (ctx, dict, key.datum.name, &obj);
  417. if (_csi_unlikely (status))
  418. return status;
  419. type = csi_object_get_type (&obj);
  420. switch (type) {
  421. case CSI_OBJECT_TYPE_BOOLEAN:
  422. *value = obj.datum.boolean;
  423. break;
  424. case CSI_OBJECT_TYPE_INTEGER:
  425. *value = obj.datum.integer;
  426. break;
  427. case CSI_OBJECT_TYPE_REAL:
  428. *value = obj.datum.real;
  429. break;
  430. default:
  431. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  432. }
  433. return CSI_STATUS_SUCCESS;
  434. }
  435. static csi_status_t
  436. _csi_dictionary_get_number (csi_t *ctx,
  437. csi_dictionary_t *dict,
  438. const char *name,
  439. csi_boolean_t optional,
  440. double *value)
  441. {
  442. csi_status_t status;
  443. csi_object_t key, obj;
  444. status = csi_name_new_static (ctx, &key, name);
  445. if (_csi_unlikely (status))
  446. return status;
  447. if (optional && ! csi_dictionary_has (dict, key.datum.name))
  448. return CSI_STATUS_SUCCESS;
  449. status = csi_dictionary_get (ctx, dict, key.datum.name, &obj);
  450. if (_csi_unlikely (status))
  451. return status;
  452. *value = csi_number_get_value (&obj);
  453. return CSI_STATUS_SUCCESS;
  454. }
  455. static csi_status_t
  456. _csi_ostack_get_string (csi_t *ctx, unsigned int i, csi_string_t **out)
  457. {
  458. csi_object_t *obj;
  459. obj = _csi_peek_ostack (ctx, i);
  460. if (_csi_unlikely (csi_object_get_type (obj) != CSI_OBJECT_TYPE_STRING))
  461. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  462. *out = obj->datum.string;
  463. return CSI_STATUS_SUCCESS;
  464. }
  465. static csi_status_t
  466. _csi_ostack_get_string_constant (csi_t *ctx, unsigned int i, const char **out)
  467. {
  468. csi_object_t *obj;
  469. int type;
  470. obj = _csi_peek_ostack (ctx, i);
  471. type = csi_object_get_type (obj);
  472. switch (type) {
  473. case CSI_OBJECT_TYPE_NAME:
  474. *out = (const char *) obj->datum.name;
  475. break;
  476. case CSI_OBJECT_TYPE_STRING:
  477. *out = obj->datum.string->string;
  478. break;
  479. default:
  480. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  481. }
  482. return CSI_STATUS_SUCCESS;
  483. }
  484. static csi_status_t
  485. _do_cairo_op (csi_t *ctx, void (*op) (cairo_t *))
  486. {
  487. cairo_t *cr;
  488. csi_status_t status;
  489. check (1);
  490. status = _csi_ostack_get_context (ctx, 0, &cr);
  491. if (_csi_unlikely (status))
  492. return status;
  493. op (cr);
  494. return CSI_STATUS_SUCCESS;
  495. }
  496. static csi_status_t
  497. end_dict_construction (csi_t *ctx)
  498. {
  499. csi_object_t obj;
  500. csi_dictionary_t *dict;
  501. csi_status_t status;
  502. status = csi_dictionary_new (ctx, &obj);
  503. if (_csi_unlikely (status))
  504. return status;
  505. dict = obj.datum.dictionary;
  506. do {
  507. csi_object_t *name, *value;
  508. check (1);
  509. value = _csi_peek_ostack (ctx, 0);
  510. if (csi_object_get_type (value) == CSI_OBJECT_TYPE_MARK) {
  511. pop (1);
  512. break;
  513. }
  514. check (2);
  515. name = _csi_peek_ostack (ctx, 1);
  516. if (_csi_unlikely
  517. (csi_object_get_type (name) != CSI_OBJECT_TYPE_NAME))
  518. {
  519. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  520. }
  521. status = csi_dictionary_put (ctx, dict, name->datum.name, value);
  522. if (_csi_unlikely (status))
  523. return status;
  524. pop (2);
  525. } while (TRUE);
  526. return push (&obj);
  527. }
  528. static csi_status_t
  529. end_array_construction (csi_t *ctx)
  530. {
  531. csi_object_t obj;
  532. csi_status_t status;
  533. int len = 0;
  534. do {
  535. check (len + 1);
  536. if (csi_object_get_type (_csi_peek_ostack (ctx, len)) ==
  537. CSI_OBJECT_TYPE_MARK)
  538. {
  539. break;
  540. }
  541. len++;
  542. } while (TRUE);
  543. status = csi_array_new (ctx, len, &obj);
  544. if (_csi_unlikely (status))
  545. return status;
  546. if (len != 0) {
  547. csi_array_t *array;
  548. array = obj.datum.array;
  549. memcpy (array->stack.objects,
  550. _csi_peek_ostack (ctx, len - 1),
  551. sizeof (csi_object_t) * len);
  552. array->stack.len = len;
  553. }
  554. ctx->ostack.len -= len + 1;
  555. return push (&obj);
  556. }
  557. static csi_status_t
  558. _alpha (csi_t *ctx)
  559. {
  560. csi_object_t obj;
  561. csi_status_t status;
  562. double a;
  563. check (1);
  564. status = _csi_ostack_get_number (ctx, 0, &a);
  565. if (_csi_unlikely (status))
  566. return status;
  567. pop (1);
  568. obj.type = CSI_OBJECT_TYPE_PATTERN;
  569. obj.datum.pattern = cairo_pattern_create_rgba (0, 0, 0, a);
  570. return push (&obj);
  571. }
  572. static csi_status_t
  573. _add (csi_t *ctx)
  574. {
  575. csi_object_t *A;
  576. csi_object_t *B;
  577. csi_object_type_t type_a, type_b;
  578. check (2);
  579. B = _csi_peek_ostack (ctx, 0);
  580. A = _csi_peek_ostack (ctx, 1);
  581. type_a = csi_object_get_type (A);
  582. if (_csi_unlikely (! (type_a == CSI_OBJECT_TYPE_INTEGER ||
  583. type_a == CSI_OBJECT_TYPE_REAL)))
  584. {
  585. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  586. }
  587. type_b = csi_object_get_type (B);
  588. if (_csi_unlikely (! (type_b == CSI_OBJECT_TYPE_INTEGER ||
  589. type_b == CSI_OBJECT_TYPE_REAL)))
  590. {
  591. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  592. }
  593. pop (2);
  594. if (type_a == CSI_OBJECT_TYPE_REAL &&
  595. type_b == CSI_OBJECT_TYPE_REAL)
  596. {
  597. return _csi_push_ostack_real (ctx, A->datum.real + B->datum.real);
  598. }
  599. else if (type_a == CSI_OBJECT_TYPE_INTEGER &&
  600. type_b == CSI_OBJECT_TYPE_INTEGER)
  601. {
  602. return _csi_push_ostack_integer (ctx,
  603. A->datum.integer + B->datum.integer);
  604. }
  605. else
  606. {
  607. double v;
  608. if (type_a == CSI_OBJECT_TYPE_REAL)
  609. v = A->datum.real;
  610. else
  611. v = A->datum.integer;
  612. if (type_b == CSI_OBJECT_TYPE_REAL)
  613. v += B->datum.real;
  614. else
  615. v += B->datum.integer;
  616. return _csi_push_ostack_real (ctx, v);
  617. }
  618. }
  619. static csi_status_t
  620. _add_color_stop (csi_t *ctx)
  621. {
  622. csi_status_t status;
  623. double offset, r, g, b, a;
  624. cairo_pattern_t *pattern = NULL; /* silence the compiler */
  625. check (6);
  626. status = _csi_ostack_get_number (ctx, 0, &a);
  627. if (_csi_unlikely (status))
  628. return status;
  629. status = _csi_ostack_get_number (ctx, 1, &b);
  630. if (_csi_unlikely (status))
  631. return status;
  632. status = _csi_ostack_get_number (ctx, 2, &g);
  633. if (_csi_unlikely (status))
  634. return status;
  635. status = _csi_ostack_get_number (ctx, 3, &r);
  636. if (_csi_unlikely (status))
  637. return status;
  638. status = _csi_ostack_get_number (ctx, 4, &offset);
  639. if (_csi_unlikely (status))
  640. return status;
  641. status = _csi_ostack_get_pattern (ctx, 5, &pattern);
  642. if (_csi_unlikely (status))
  643. return status;
  644. cairo_pattern_add_color_stop_rgba (pattern, offset, r, g, b, a);
  645. pop (5);
  646. return CSI_STATUS_SUCCESS;
  647. }
  648. static csi_status_t
  649. _and (csi_t *ctx)
  650. {
  651. csi_object_t *a, *b;
  652. int type;
  653. check (2);
  654. a = _csi_peek_ostack (ctx, 0);
  655. b = _csi_peek_ostack (ctx, 1);
  656. if (_csi_unlikely (csi_object_get_type (a) != csi_object_get_type (b)))
  657. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  658. pop (2);
  659. type = csi_object_get_type (a);
  660. switch (type) {
  661. case CSI_OBJECT_TYPE_INTEGER:
  662. return _csi_push_ostack_integer (ctx,
  663. a->datum.integer & b->datum.integer);
  664. case CSI_OBJECT_TYPE_BOOLEAN:
  665. return _csi_push_ostack_boolean (ctx,
  666. a->datum.boolean & b->datum.boolean);
  667. default:
  668. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  669. }
  670. }
  671. static csi_status_t
  672. _arc (csi_t *ctx)
  673. {
  674. csi_status_t status;
  675. double x, y, r;
  676. double theta1, theta2;
  677. cairo_t *cr;
  678. check (6);
  679. status = _csi_ostack_get_number (ctx, 0, &theta2);
  680. if (_csi_unlikely (status))
  681. return status;
  682. status = _csi_ostack_get_number (ctx, 1, &theta1);
  683. if (_csi_unlikely (status))
  684. return status;
  685. status = _csi_ostack_get_number (ctx, 2, &r);
  686. if (_csi_unlikely (status))
  687. return status;
  688. status = _csi_ostack_get_number (ctx, 3, &y);
  689. if (_csi_unlikely (status))
  690. return status;
  691. status = _csi_ostack_get_number (ctx, 4, &x);
  692. if (_csi_unlikely (status))
  693. return status;
  694. status = _csi_ostack_get_context (ctx, 5, &cr);
  695. if (_csi_unlikely (status))
  696. return status;
  697. /* XXX handle path object */
  698. cairo_arc (cr, x, y, r, theta1, theta2);
  699. pop (5);
  700. return CSI_STATUS_SUCCESS;
  701. }
  702. static csi_status_t
  703. _arc_negative (csi_t *ctx)
  704. {
  705. csi_status_t status;
  706. double x, y, r;
  707. double theta1, theta2;
  708. cairo_t *cr;
  709. check (6);
  710. status = _csi_ostack_get_number (ctx, 0, &theta2);
  711. if (_csi_unlikely (status))
  712. return status;
  713. status = _csi_ostack_get_number (ctx, 1, &theta1);
  714. if (_csi_unlikely (status))
  715. return status;
  716. status = _csi_ostack_get_number (ctx, 2, &r);
  717. if (_csi_unlikely (status))
  718. return status;
  719. status = _csi_ostack_get_number (ctx, 3, &y);
  720. if (_csi_unlikely (status))
  721. return status;
  722. status = _csi_ostack_get_number (ctx, 4, &x);
  723. if (_csi_unlikely (status))
  724. return status;
  725. status = _csi_ostack_get_context (ctx, 5, &cr);
  726. if (_csi_unlikely (status))
  727. return status;
  728. /* XXX handle path object */
  729. cairo_arc_negative (cr, x, y, r, theta1, theta2);
  730. pop (5);
  731. return CSI_STATUS_SUCCESS;
  732. }
  733. static csi_status_t
  734. _array (csi_t *ctx)
  735. {
  736. csi_object_t obj;
  737. csi_status_t status;
  738. status = csi_array_new (ctx, 0, &obj);
  739. if (_csi_unlikely (status))
  740. return status;
  741. return push (&obj);
  742. }
  743. static csi_status_t
  744. _bind_substitute (csi_t *ctx, csi_array_t *array)
  745. {
  746. csi_status_t status;
  747. csi_integer_t i, n;
  748. csi_dictionary_t *dict;
  749. /* perform operator substitution on the executable array (procedure) */
  750. dict = ctx->dstack.objects[0].datum.dictionary;
  751. n = array->stack.len;
  752. for (i = 0; i < n; i++) {
  753. csi_object_t *obj = &array->stack.objects[i];
  754. if (obj->type == (CSI_OBJECT_TYPE_NAME | CSI_OBJECT_ATTR_EXECUTABLE)) {
  755. csi_dictionary_entry_t *entry;
  756. entry = _csi_hash_table_lookup (&dict->hash_table,
  757. (csi_hash_entry_t *)
  758. &obj->datum.name);
  759. if (entry != NULL)
  760. *obj = entry->value;
  761. } else if (csi_object_is_procedure (obj)) {
  762. status = _bind_substitute (ctx, obj->datum.array);
  763. if (_csi_unlikely (status))
  764. return status;
  765. }
  766. }
  767. return CSI_STATUS_SUCCESS;
  768. }
  769. static csi_status_t
  770. _idiom_substitute (csi_t *ctx, csi_array_t *array)
  771. {
  772. #if 0
  773. csi_status_t status;
  774. csi_integer_t i, j;
  775. /* XXX substring search, build array once then search for
  776. * longest matching idiom, repeat. */
  777. /* scan the top-most array for sequences we can pre-compile */
  778. /* now recurse for subroutines */
  779. j = array->stack.len;
  780. for (i = 0; i < j; i++) {
  781. csi_object_t *obj = &array->stack.objects[i];
  782. if (csi_object_is_procedure (obj)) {
  783. status = _idiom_substitute (ctx, obj->datum.array);
  784. if (_csi_unlikely (_cairo_is_error (status))
  785. return status;
  786. }
  787. }
  788. #endif
  789. return CSI_STATUS_SUCCESS;
  790. }
  791. static csi_status_t
  792. _bind (csi_t *ctx)
  793. {
  794. csi_array_t *array;
  795. csi_status_t status;
  796. check (1);
  797. status = _csi_ostack_get_procedure (ctx, 0, &array);
  798. if (_csi_unlikely (status))
  799. return status;
  800. status = _bind_substitute (ctx, array);
  801. if (_csi_unlikely (status))
  802. return status;
  803. status = _idiom_substitute (ctx, array);
  804. if (_csi_unlikely (status))
  805. return status;
  806. return CSI_STATUS_SUCCESS;
  807. }
  808. static csi_status_t
  809. _bitshift (csi_t *ctx)
  810. {
  811. long v, shift;
  812. csi_status_t status;
  813. check (2);
  814. status = _csi_ostack_get_integer (ctx, 0, &shift);
  815. if (_csi_unlikely (status))
  816. return status;
  817. status = _csi_ostack_get_integer (ctx, 1, &v);
  818. if (_csi_unlikely (status))
  819. return status;
  820. if (shift < 0) {
  821. shift = -shift;
  822. v >>= shift;
  823. } else
  824. v <<= shift;
  825. pop (1);
  826. _csi_peek_ostack (ctx, 0)->datum.integer = v;
  827. return CSI_STATUS_SUCCESS;
  828. }
  829. static csi_status_t
  830. _clip (csi_t *ctx)
  831. {
  832. return _do_cairo_op (ctx, cairo_clip);
  833. }
  834. static csi_status_t
  835. _clip_preserve (csi_t *ctx)
  836. {
  837. return _do_cairo_op (ctx, cairo_clip_preserve);
  838. }
  839. static csi_status_t
  840. _close_path (csi_t *ctx)
  841. {
  842. return _do_cairo_op (ctx, cairo_close_path);
  843. }
  844. static csi_status_t
  845. _context (csi_t *ctx)
  846. {
  847. csi_object_t obj;
  848. csi_status_t status;
  849. cairo_surface_t *surface;
  850. cairo_t *cr;
  851. csi_context_create_func_t hook;
  852. csi_proxy_t *proxy;
  853. check (1);
  854. status = _csi_ostack_get_surface (ctx, 0, &surface);
  855. if (_csi_unlikely (status))
  856. return status;
  857. hook = ctx->hooks.context_create;
  858. if (hook != NULL)
  859. cr = hook (ctx->hooks.closure, surface);
  860. else
  861. cr = cairo_create (surface);
  862. proxy = _csi_proxy_create (ctx, cr, NULL,
  863. ctx->hooks.context_destroy,
  864. ctx->hooks.closure);
  865. if (_csi_unlikely (proxy == NULL)) {
  866. cairo_destroy (cr);
  867. return _csi_error (CSI_STATUS_NO_MEMORY);
  868. }
  869. status = cairo_set_user_data (cr, &_csi_proxy_key,
  870. proxy, _csi_proxy_destroy);
  871. if (_csi_unlikely (status)) {
  872. _csi_proxy_destroy (proxy);
  873. cairo_destroy (cr);
  874. return status;
  875. }
  876. pop (1);
  877. obj.type = CSI_OBJECT_TYPE_CONTEXT;
  878. obj.datum.cr = cr;
  879. return push (&obj);
  880. }
  881. static csi_status_t
  882. _copy (csi_t *ctx)
  883. {
  884. csi_object_t *obj;
  885. int type;
  886. check (1);
  887. obj = csi_object_reference (_csi_peek_ostack (ctx, 0));
  888. pop (1);
  889. type = csi_object_get_type (obj);
  890. switch (type) {
  891. /*XXX array, string, dictionary, etc */
  892. case CSI_OBJECT_TYPE_INTEGER:
  893. {
  894. long i, n;
  895. n = obj->datum.integer;
  896. if (_csi_unlikely (n < 0))
  897. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  898. check (n);
  899. for (i = n; i--; ) {
  900. csi_status_t status;
  901. status = _csi_push_ostack_copy (ctx,
  902. _csi_peek_ostack (ctx, n-1));
  903. if (_csi_unlikely (status))
  904. return status;
  905. }
  906. break;
  907. }
  908. default:
  909. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  910. }
  911. return CSI_STATUS_SUCCESS;
  912. }
  913. static csi_status_t
  914. _copy_page (csi_t *ctx)
  915. {
  916. csi_object_t *obj;
  917. int type;
  918. check (1);
  919. obj = _csi_peek_ostack (ctx, 0);
  920. type = csi_object_get_type (obj);
  921. switch (type) {
  922. case CSI_OBJECT_TYPE_CONTEXT:
  923. cairo_copy_page (obj->datum.cr);
  924. if (ctx->hooks.copy_page != NULL)
  925. ctx->hooks.copy_page (ctx->hooks.closure, obj->datum.cr);
  926. break;
  927. case CSI_OBJECT_TYPE_SURFACE:
  928. cairo_surface_copy_page (obj->datum.surface);
  929. /* XXX hook? */
  930. break;
  931. default:
  932. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  933. }
  934. return CSI_STATUS_SUCCESS;
  935. }
  936. static csi_status_t
  937. _curve_to (csi_t *ctx)
  938. {
  939. csi_status_t status;
  940. csi_object_t *obj;
  941. int type;
  942. double x1, y1;
  943. double x2, y2;
  944. double x3, y3;
  945. check (7);
  946. status = _csi_ostack_get_number (ctx, 0, &y3);
  947. if (_csi_unlikely (status))
  948. return status;
  949. status = _csi_ostack_get_number (ctx, 1, &x3);
  950. if (_csi_unlikely (status))
  951. return status;
  952. status = _csi_ostack_get_number (ctx, 2, &y2);
  953. if (_csi_unlikely (status))
  954. return status;
  955. status = _csi_ostack_get_number (ctx, 3, &x2);
  956. if (_csi_unlikely (status))
  957. return status;
  958. status = _csi_ostack_get_number (ctx, 4, &y1);
  959. if (_csi_unlikely (status))
  960. return status;
  961. status = _csi_ostack_get_number (ctx, 5, &x1);
  962. if (_csi_unlikely (status))
  963. return status;
  964. obj = _csi_peek_ostack (ctx, 6);
  965. type = csi_object_get_type (obj);
  966. switch (type) {
  967. case CSI_OBJECT_TYPE_CONTEXT:
  968. cairo_curve_to (obj->datum.cr, x1, y1, x2, y2, x3, y3);
  969. break;
  970. case CSI_OBJECT_TYPE_PATTERN:
  971. cairo_mesh_pattern_curve_to (obj->datum.pattern,
  972. x1, y1, x2, y2, x3, y3);
  973. break;
  974. /* XXX handle path object */
  975. }
  976. pop (6);
  977. return CSI_STATUS_SUCCESS;
  978. }
  979. static csi_status_t
  980. _cvi (csi_t *ctx)
  981. {
  982. csi_object_t *val, obj;
  983. int type;
  984. check (1);
  985. val = _csi_peek_ostack (ctx, 0);
  986. type = csi_object_get_type (val);
  987. switch (type) {
  988. case CSI_OBJECT_TYPE_INTEGER:
  989. return CSI_STATUS_SUCCESS;
  990. case CSI_OBJECT_TYPE_REAL:
  991. pop (1);
  992. return _csi_push_ostack_integer (ctx, val->datum.real);
  993. case CSI_OBJECT_TYPE_STRING:
  994. if (! _csi_parse_number (&obj,
  995. val->datum.string->string,
  996. val->datum.string->len))
  997. {
  998. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  999. }
  1000. pop (1);
  1001. if (csi_object_get_type (&obj) == CSI_OBJECT_TYPE_INTEGER)
  1002. return push (&obj);
  1003. else
  1004. return _csi_push_ostack_integer (ctx, obj.datum.real);
  1005. default:
  1006. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  1007. }
  1008. }
  1009. static csi_status_t
  1010. _cvr (csi_t *ctx)
  1011. {
  1012. csi_object_t *val, obj;
  1013. int type;
  1014. check (1);
  1015. val = _csi_peek_ostack (ctx, 0);
  1016. type = csi_object_get_type (val);
  1017. switch (type) {
  1018. case CSI_OBJECT_TYPE_REAL:
  1019. return CSI_STATUS_SUCCESS;
  1020. case CSI_OBJECT_TYPE_INTEGER:
  1021. pop (1);
  1022. return _csi_push_ostack_real (ctx, val->datum.integer);
  1023. case CSI_OBJECT_TYPE_STRING:
  1024. if (! _csi_parse_number (&obj,
  1025. val->datum.string->string,
  1026. val->datum.string->len))
  1027. {
  1028. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  1029. }
  1030. pop (1);
  1031. if (csi_object_get_type (&obj) == CSI_OBJECT_TYPE_REAL)
  1032. return push (&obj);
  1033. else
  1034. return _csi_push_ostack_real (ctx, obj.datum.integer);
  1035. default:
  1036. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  1037. }
  1038. }
  1039. static csi_status_t
  1040. _def (csi_t *ctx)
  1041. {
  1042. csi_name_t name = 0; /* silence the compiler */
  1043. csi_status_t status;
  1044. check (2);
  1045. status = _csi_ostack_get_name (ctx, 1, &name);
  1046. if (_csi_unlikely (status))
  1047. return status;
  1048. status = _csi_name_define (ctx, name, _csi_peek_ostack (ctx, 0));
  1049. if (_csi_unlikely (status))
  1050. return status;
  1051. pop (2);
  1052. return CSI_STATUS_SUCCESS;
  1053. }
  1054. static csi_status_t
  1055. _dict (csi_t *ctx)
  1056. {
  1057. csi_object_t obj;
  1058. csi_status_t status;
  1059. status = csi_dictionary_new (ctx, &obj);
  1060. if (_csi_unlikely (status))
  1061. return status;
  1062. return push (&obj);
  1063. }
  1064. static csi_status_t
  1065. _div (csi_t *ctx)
  1066. {
  1067. csi_object_t *A;
  1068. csi_object_t *B;
  1069. csi_object_type_t type_a, type_b;
  1070. check (2);
  1071. B = _csi_peek_ostack (ctx, 0);
  1072. A = _csi_peek_ostack (ctx, 1);
  1073. type_a = csi_object_get_type (A);
  1074. if (_csi_unlikely (! (type_a == CSI_OBJECT_TYPE_INTEGER ||
  1075. type_a == CSI_OBJECT_TYPE_REAL)))
  1076. {
  1077. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  1078. }
  1079. type_b = csi_object_get_type (B);
  1080. if (_csi_unlikely (! (type_b == CSI_OBJECT_TYPE_INTEGER ||
  1081. type_b == CSI_OBJECT_TYPE_REAL)))
  1082. {
  1083. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  1084. }
  1085. pop (2);
  1086. if (type_a == CSI_OBJECT_TYPE_REAL &&
  1087. type_b == CSI_OBJECT_TYPE_REAL)
  1088. {
  1089. return _csi_push_ostack_real (ctx, A->datum.real / B->datum.real);
  1090. }
  1091. else if (type_a == CSI_OBJECT_TYPE_INTEGER &&
  1092. type_b == CSI_OBJECT_TYPE_INTEGER)
  1093. {
  1094. return _csi_push_ostack_integer (ctx,
  1095. A->datum.integer / B->datum.integer);
  1096. }
  1097. else
  1098. {
  1099. double v;
  1100. if (type_a == CSI_OBJECT_TYPE_REAL)
  1101. v = A->datum.real;
  1102. else
  1103. v = A->datum.integer;
  1104. if (type_b == CSI_OBJECT_TYPE_REAL)
  1105. v /= B->datum.real;
  1106. else
  1107. v /= B->datum.integer;
  1108. return _csi_push_ostack_real (ctx, v);
  1109. }
  1110. }
  1111. static csi_status_t
  1112. _duplicate (csi_t *ctx)
  1113. {
  1114. check (1);
  1115. return _csi_push_ostack_copy (ctx, _csi_peek_ostack (ctx, 0));
  1116. }
  1117. static csi_status_t
  1118. _eq (csi_t *ctx)
  1119. {
  1120. csi_object_t *a, *b;
  1121. csi_boolean_t v;
  1122. check (2);
  1123. b = _csi_peek_ostack (ctx, 0);
  1124. a = _csi_peek_ostack (ctx, 1);
  1125. v = csi_object_eq (a, b);
  1126. pop (2);
  1127. return _csi_push_ostack_boolean (ctx, v);
  1128. }
  1129. static csi_status_t
  1130. _exch (csi_t *ctx)
  1131. {
  1132. return _csi_stack_exch (&ctx->ostack);
  1133. }
  1134. static csi_status_t
  1135. _false (csi_t *ctx)
  1136. {
  1137. return _csi_push_ostack_boolean (ctx, FALSE);
  1138. }
  1139. static csi_status_t
  1140. _fill (csi_t *ctx)
  1141. {
  1142. return _do_cairo_op (ctx, cairo_fill);
  1143. }
  1144. static csi_status_t
  1145. _fill_preserve (csi_t *ctx)
  1146. {
  1147. return _do_cairo_op (ctx, cairo_fill_preserve);
  1148. }
  1149. static csi_status_t
  1150. _filter (csi_t *ctx)
  1151. {
  1152. csi_object_t *src;
  1153. csi_dictionary_t *dict = NULL;
  1154. csi_status_t status;
  1155. const char *name = NULL; /* silence the compiler */
  1156. const struct filters {
  1157. const char *name;
  1158. csi_status_t (*constructor) (csi_t *t,
  1159. csi_object_t *,
  1160. csi_dictionary_t *,
  1161. csi_object_t *);
  1162. } filters[] = {
  1163. { "ascii85", csi_file_new_ascii85_decode },
  1164. #if HAVE_ZLIB
  1165. { "deflate", csi_file_new_deflate_decode },
  1166. #endif
  1167. #if 0
  1168. { "lzw", csi_file_new_lzw_decode },
  1169. #endif
  1170. { NULL, NULL }
  1171. }, *filter;
  1172. int cnt;
  1173. check (2);
  1174. status = _csi_ostack_get_string_constant (ctx, 0, &name);
  1175. if (_csi_unlikely (status))
  1176. return status;
  1177. src = _csi_peek_ostack (ctx, 1);
  1178. cnt = 2;
  1179. if (csi_object_get_type (src) == CSI_OBJECT_TYPE_DICTIONARY) {
  1180. dict = src->datum.dictionary;
  1181. check (3);
  1182. src = _csi_peek_ostack (ctx, 2);
  1183. cnt = 3;
  1184. }
  1185. for (filter = filters; filter->name != NULL; filter++) {
  1186. if (strcmp (name, filter->name) == 0) {
  1187. csi_object_t file;
  1188. status = filter->constructor (ctx, &file, dict, src);
  1189. if (_csi_unlikely (status))
  1190. return status;
  1191. pop (cnt);
  1192. return push (&file);
  1193. }
  1194. }
  1195. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  1196. }
  1197. static cairo_status_t
  1198. _type3_init (cairo_scaled_font_t *scaled_font,
  1199. cairo_t *cr,
  1200. cairo_font_extents_t *metrics)
  1201. {
  1202. cairo_font_face_t *face;
  1203. csi_proxy_t *proxy;
  1204. csi_t *ctx;
  1205. csi_dictionary_t *font;
  1206. csi_object_t key;
  1207. csi_object_t obj;
  1208. csi_array_t *array;
  1209. csi_status_t status;
  1210. face = cairo_scaled_font_get_font_face (scaled_font);
  1211. proxy = cairo_font_face_get_user_data (face, &_csi_proxy_key);
  1212. if (_csi_unlikely (proxy == NULL))
  1213. return CAIRO_STATUS_NO_MEMORY;
  1214. ctx = proxy->ctx;
  1215. font = proxy->dictionary;
  1216. status = csi_name_new_static (ctx, &key, "metrics");
  1217. if (_csi_unlikely (status))
  1218. return CAIRO_STATUS_NO_MEMORY;
  1219. if (! csi_dictionary_has (font, key.datum.name))
  1220. return CAIRO_STATUS_SUCCESS;
  1221. status = csi_dictionary_get (ctx, font, key.datum.name, &obj);
  1222. if (_csi_unlikely (status))
  1223. return status;
  1224. if (csi_object_get_type (&obj) != CSI_OBJECT_TYPE_ARRAY)
  1225. return CAIRO_STATUS_USER_FONT_ERROR;
  1226. array = obj.datum.array;
  1227. if (array->stack.len != 5)
  1228. return CAIRO_STATUS_USER_FONT_ERROR;
  1229. metrics->ascent = csi_number_get_value (&array->stack.objects[0]);
  1230. metrics->descent = csi_number_get_value (&array->stack.objects[1]);
  1231. metrics->height = csi_number_get_value (&array->stack.objects[2]);
  1232. metrics->max_x_advance = csi_number_get_value (&array->stack.objects[3]);
  1233. metrics->max_y_advance = csi_number_get_value (&array->stack.objects[4]);
  1234. return CAIRO_STATUS_SUCCESS;
  1235. }
  1236. static cairo_status_t
  1237. _type3_lookup (cairo_scaled_font_t *scaled_font,
  1238. unsigned long unicode,
  1239. unsigned long *glyph)
  1240. {
  1241. cairo_font_face_t *face;
  1242. csi_proxy_t *proxy;
  1243. csi_t *ctx;
  1244. csi_dictionary_t *font;
  1245. csi_object_t obj, key;
  1246. csi_array_t *array;
  1247. char buf[12];
  1248. csi_integer_t i;
  1249. cairo_status_t status;
  1250. face = cairo_scaled_font_get_font_face (scaled_font);
  1251. proxy = cairo_font_face_get_user_data (face, &_csi_proxy_key);
  1252. if (_csi_unlikely (proxy == NULL))
  1253. return CAIRO_STATUS_USER_FONT_ERROR;
  1254. ctx = proxy->ctx;
  1255. font = proxy->dictionary;
  1256. status = csi_name_new_static (ctx, &key, "encoding");
  1257. if (_csi_unlikely (status))
  1258. return CAIRO_STATUS_USER_FONT_ERROR;
  1259. if (! csi_dictionary_has (font, key.datum.name)) {
  1260. *glyph = unicode;
  1261. return CAIRO_STATUS_SUCCESS;
  1262. }
  1263. status = csi_dictionary_get (ctx, font, key.datum.name, &obj);
  1264. if (_csi_unlikely (status))
  1265. return CAIRO_STATUS_USER_FONT_ERROR;
  1266. if (_csi_unlikely (csi_object_get_type (&obj) != CSI_OBJECT_TYPE_ARRAY))
  1267. return CAIRO_STATUS_USER_FONT_ERROR;
  1268. snprintf (buf, sizeof (buf), "uni%04lu", unicode);
  1269. array = obj.datum.array;
  1270. for (i = 0; i < array->stack.len; i++) {
  1271. csi_object_t *name;
  1272. name = &array->stack.objects[i];
  1273. if (csi_object_get_type (name) != CSI_OBJECT_TYPE_NAME)
  1274. continue;
  1275. if (strcmp ((char *) name->datum.name, buf) == 0) {
  1276. *glyph = i;
  1277. return CAIRO_STATUS_SUCCESS;
  1278. }
  1279. }
  1280. return CAIRO_STATUS_USER_FONT_ERROR;
  1281. }
  1282. static cairo_status_t
  1283. _type3_render (cairo_scaled_font_t *scaled_font,
  1284. unsigned long glyph_index,
  1285. cairo_t *cr,
  1286. cairo_text_extents_t *metrics)
  1287. {
  1288. cairo_font_face_t *face;
  1289. csi_proxy_t *proxy;
  1290. csi_t *ctx;
  1291. csi_dictionary_t *font;
  1292. csi_array_t *glyphs;
  1293. csi_object_t *glyph;
  1294. csi_object_t key;
  1295. csi_object_t obj;
  1296. csi_object_t render;
  1297. csi_status_t status;
  1298. face = cairo_scaled_font_get_font_face (scaled_font);
  1299. proxy = cairo_font_face_get_user_data (face, &_csi_proxy_key);
  1300. if (_csi_unlikely (proxy == NULL))
  1301. return CAIRO_STATUS_USER_FONT_ERROR;
  1302. ctx = proxy->ctx;
  1303. font = proxy->dictionary;
  1304. status = csi_name_new_static (ctx, &key, "glyphs");
  1305. if (_csi_unlikely (status))
  1306. return CAIRO_STATUS_USER_FONT_ERROR;
  1307. status = csi_dictionary_get (ctx, font, key.datum.name, &obj);
  1308. if (_csi_unlikely (status))
  1309. return CAIRO_STATUS_USER_FONT_ERROR;
  1310. if (_csi_unlikely (csi_object_get_type (&obj) != CSI_OBJECT_TYPE_ARRAY))
  1311. return CAIRO_STATUS_USER_FONT_ERROR;
  1312. glyphs = obj.datum.array;
  1313. glyph = &glyphs->stack.objects[glyph_index];
  1314. if (csi_object_get_type (glyph) == CSI_OBJECT_TYPE_NULL)
  1315. return CAIRO_STATUS_SUCCESS; /* .notdef */
  1316. if (_csi_unlikely (csi_object_get_type (glyph) != CSI_OBJECT_TYPE_DICTIONARY))
  1317. return CAIRO_STATUS_USER_FONT_ERROR;
  1318. status = csi_name_new_static (ctx, &key, "metrics");
  1319. if (_csi_unlikely (status))
  1320. return CAIRO_STATUS_USER_FONT_ERROR;
  1321. font = glyph->datum.dictionary;
  1322. if (csi_dictionary_has (font, key.datum.name)) {
  1323. csi_array_t *array;
  1324. status = csi_dictionary_get (ctx, font, key.datum.name, &obj);
  1325. if (_csi_unlikely (status))
  1326. return CAIRO_STATUS_USER_FONT_ERROR;
  1327. if (_csi_unlikely (csi_object_get_type (&obj) !=
  1328. CSI_OBJECT_TYPE_ARRAY))
  1329. return CAIRO_STATUS_USER_FONT_ERROR;
  1330. array = obj.datum.array;
  1331. if (_csi_unlikely (array->stack.len != 6))
  1332. return CAIRO_STATUS_USER_FONT_ERROR;
  1333. metrics->x_bearing = csi_number_get_value (&array->stack.objects[0]);
  1334. metrics->y_bearing = csi_number_get_value (&array->stack.objects[1]);
  1335. metrics->width = csi_number_get_value (&array->stack.objects[2]);
  1336. metrics->height = csi_number_get_value (&array->stack.objects[3]);
  1337. metrics->x_advance = csi_number_get_value (&array->stack.objects[4]);
  1338. metrics->y_advance = csi_number_get_value (&array->stack.objects[5]);
  1339. }
  1340. status = csi_name_new_static (ctx, &key, "render");
  1341. if (_csi_unlikely (status))
  1342. return CAIRO_STATUS_USER_FONT_ERROR;
  1343. status = csi_dictionary_get (ctx, font, key.datum.name, &render);
  1344. if (_csi_unlikely (status))
  1345. return CAIRO_STATUS_USER_FONT_ERROR;
  1346. if (_csi_unlikely (! csi_object_is_procedure (&render)))
  1347. return CAIRO_STATUS_USER_FONT_ERROR;
  1348. obj.type = CSI_OBJECT_TYPE_CONTEXT;
  1349. obj.datum.cr = cairo_reference (cr);
  1350. status = push (&obj);
  1351. if (_csi_unlikely (status)) {
  1352. cairo_destroy (cr);
  1353. return CAIRO_STATUS_USER_FONT_ERROR;
  1354. }
  1355. status = csi_object_execute (ctx, &render);
  1356. pop (1);
  1357. return status ? CAIRO_STATUS_USER_FONT_ERROR : CAIRO_STATUS_SUCCESS;
  1358. }
  1359. static csi_status_t
  1360. _font_type3 (csi_t *ctx,
  1361. csi_dictionary_t *font,
  1362. cairo_font_face_t **font_face_out)
  1363. {
  1364. cairo_font_face_t *font_face;
  1365. font_face = cairo_user_font_face_create ();
  1366. cairo_user_font_face_set_init_func (font_face, _type3_init);
  1367. cairo_user_font_face_set_unicode_to_glyph_func (font_face, _type3_lookup);
  1368. cairo_user_font_face_set_render_glyph_func (font_face, _type3_render);
  1369. *font_face_out = font_face;
  1370. return CSI_STATUS_SUCCESS;
  1371. }
  1372. #if CAIRO_HAS_FT_FONT
  1373. #include <cairo-ft.h>
  1374. #include <ft2build.h>
  1375. #include FT_FREETYPE_H
  1376. static FT_Library _ft_lib;
  1377. struct _ft_face_data {
  1378. csi_t *ctx;
  1379. csi_blob_t blob;
  1380. FT_Face face;
  1381. csi_string_t *source;
  1382. void *bytes;
  1383. cairo_font_face_t *font_face;
  1384. };
  1385. static void
  1386. _ft_done_face (void *closure)
  1387. {
  1388. struct _ft_face_data *data = closure;
  1389. csi_t *ctx;
  1390. ctx = data->ctx;
  1391. if (data->face != NULL)
  1392. FT_Done_Face (data->face);
  1393. ctx->_faces = _csi_list_unlink (ctx->_faces, &data->blob.list);
  1394. if (data->source != NULL) {
  1395. if (--data->source->base.ref == 0)
  1396. csi_string_free (ctx, data->source);
  1397. } else {
  1398. #ifdef HAVE_MMAP
  1399. munmap (data->blob.bytes, data->blob.len);
  1400. #endif
  1401. }
  1402. if (data->bytes != NULL)
  1403. _csi_free (ctx, data->bytes);
  1404. _csi_slab_free (ctx, data, sizeof (*data));
  1405. cairo_script_interpreter_destroy (ctx);
  1406. }
  1407. struct mmap_vec {
  1408. const uint8_t *bytes;
  1409. size_t num_bytes;
  1410. };
  1411. #ifdef HAVE_MMAP
  1412. /* manual form of swapping for swapless systems like tiny */
  1413. static void *
  1414. _mmap_bytes (const struct mmap_vec *vec, int count)
  1415. {
  1416. char template[] = "/tmp/csi-font.XXXXXX";
  1417. void *ptr;
  1418. int fd;
  1419. int num_bytes;
  1420. fd = mkstemp (template);
  1421. if (fd == -1)
  1422. return MAP_FAILED;
  1423. unlink (template);
  1424. num_bytes = 0;
  1425. while (count--) {
  1426. const uint8_t *bytes = vec->bytes;
  1427. size_t len = vec->num_bytes;
  1428. while (len) {
  1429. int ret = write (fd, bytes, len);
  1430. if (ret < 0) {
  1431. close (fd);
  1432. return MAP_FAILED;
  1433. }
  1434. len -= ret;
  1435. bytes += ret;
  1436. }
  1437. num_bytes += vec->num_bytes;
  1438. vec++;
  1439. }
  1440. ptr = mmap (NULL, num_bytes, PROT_READ, MAP_SHARED, fd, 0);
  1441. close (fd);
  1442. return ptr;
  1443. }
  1444. #endif
  1445. static void *
  1446. inflate_string (csi_t *ctx, csi_string_t *src)
  1447. {
  1448. uLongf len;
  1449. uint8_t *bytes;
  1450. len = src->deflate;
  1451. bytes = _csi_alloc (ctx, len + 1);
  1452. if (bytes == NULL)
  1453. return NULL;
  1454. switch (src->method) {
  1455. default:
  1456. case NONE:
  1457. free (bytes);
  1458. return NULL;
  1459. case ZLIB:
  1460. #if HAVE_ZLIB
  1461. if (uncompress ((Bytef *) bytes, &len,
  1462. (Bytef *) src->string, src->len) != Z_OK)
  1463. #endif
  1464. {
  1465. _csi_free (ctx, bytes);
  1466. return NULL;
  1467. }
  1468. break;
  1469. case LZO:
  1470. #if HAVE_LZO
  1471. if (lzo2a_decompress ((Bytef *) src->string, src->len,
  1472. (Bytef *) bytes, &len,
  1473. NULL))
  1474. #endif
  1475. {
  1476. _csi_free (ctx, bytes);
  1477. return NULL;
  1478. }
  1479. break;
  1480. }
  1481. bytes[len] = '\0';
  1482. return bytes;
  1483. }
  1484. static csi_status_t
  1485. _ft_create_for_source (csi_t *ctx,
  1486. csi_string_t *source,
  1487. int index, int load_flags,
  1488. cairo_font_face_t **font_face_out)
  1489. {
  1490. csi_blob_t tmpl;
  1491. struct _ft_face_data *data;
  1492. csi_list_t *link;
  1493. FT_Error err;
  1494. cairo_font_face_t *font_face;
  1495. csi_status_t status;
  1496. struct mmap_vec vec[2];
  1497. int vec_count;
  1498. void *bytes;
  1499. int len;
  1500. /* check for an existing FT_Face (kept alive by the font cache) */
  1501. /* XXX index/flags */
  1502. _csi_blob_init (&tmpl, (uint8_t *) source->string, source->len);
  1503. _csi_blob_hash (&tmpl, (uint32_t *) source->string, source->len / sizeof (uint32_t));
  1504. link = _csi_list_find (ctx->_faces, _csi_blob_equal, &tmpl);
  1505. if (link) {
  1506. if (--source->base.ref == 0)
  1507. csi_string_free (ctx, source);
  1508. data = csi_container_of (link, struct _ft_face_data, blob.list);
  1509. *font_face_out = cairo_font_face_reference (data->font_face);
  1510. return CSI_STATUS_SUCCESS;
  1511. }
  1512. /* no existing font_face, create new FT_Face */
  1513. if (_ft_lib == NULL) {
  1514. err = FT_Init_FreeType (&_ft_lib);
  1515. if (_csi_unlikely (err != FT_Err_Ok))
  1516. return _csi_error (CSI_STATUS_NO_MEMORY);
  1517. }
  1518. data = _csi_slab_alloc (ctx, sizeof (*data));
  1519. data->bytes = NULL;
  1520. data->source = source;
  1521. vec[0].bytes = tmpl.bytes;
  1522. vec[0].num_bytes = tmpl.len;
  1523. if (source->deflate) {
  1524. len = source->deflate;
  1525. bytes = inflate_string (ctx, source);
  1526. if (_csi_unlikely (bytes == NULL))
  1527. return _csi_error (CSI_STATUS_NO_MEMORY);
  1528. vec[1].bytes = bytes;
  1529. vec[1].num_bytes = len;
  1530. data->bytes = bytes;
  1531. vec_count = 2;
  1532. } else {
  1533. bytes = tmpl.bytes;
  1534. len = tmpl.len;
  1535. vec_count = 1;
  1536. }
  1537. data->face = NULL;
  1538. ctx->_faces = _csi_list_prepend (ctx->_faces, &data->blob.list);
  1539. data->ctx = cairo_script_interpreter_reference (ctx);
  1540. data->blob.hash = tmpl.hash;
  1541. data->blob.len = tmpl.len;
  1542. #ifdef HAVE_MMAP
  1543. data->blob.bytes = _mmap_bytes (vec, vec_count);
  1544. if (data->blob.bytes != MAP_FAILED) {
  1545. if (--source->base.ref == 0)
  1546. csi_string_free (ctx, source);
  1547. if (source->deflate) {
  1548. _csi_free (ctx, bytes);
  1549. bytes = data->blob.bytes + vec[0].num_bytes;
  1550. } else
  1551. bytes = data->blob.bytes;
  1552. data->source = NULL;
  1553. data->bytes = NULL;
  1554. } else {
  1555. data->blob.bytes = tmpl.bytes;
  1556. }
  1557. #else
  1558. data->blob.bytes = tmpl.bytes;
  1559. #endif
  1560. err = FT_New_Memory_Face (_ft_lib,
  1561. bytes, len,
  1562. index,
  1563. &data->face);
  1564. if (_csi_unlikely (err != FT_Err_Ok)) {
  1565. _ft_done_face (data);
  1566. if (err == FT_Err_Out_Of_Memory)
  1567. return _csi_error (CSI_STATUS_NO_MEMORY);
  1568. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  1569. }
  1570. font_face = cairo_ft_font_face_create_for_ft_face (data->face, load_flags);
  1571. status = cairo_font_face_set_user_data (font_face,
  1572. &_csi_blob_key,
  1573. data, _ft_done_face);
  1574. if (_csi_unlikely (status)) {
  1575. _ft_done_face (data);
  1576. cairo_font_face_destroy (font_face);
  1577. return status;
  1578. }
  1579. data->font_face = font_face;
  1580. *font_face_out = font_face;
  1581. return CSI_STATUS_SUCCESS;
  1582. }
  1583. static csi_status_t
  1584. _ft_create_for_pattern (csi_t *ctx,
  1585. csi_string_t *string,
  1586. cairo_font_face_t **font_face_out)
  1587. {
  1588. #if CAIRO_HAS_FC_FONT
  1589. csi_blob_t tmpl;
  1590. struct _ft_face_data *data;
  1591. csi_list_t *link;
  1592. cairo_font_face_t *font_face;
  1593. FcPattern *pattern, *resolved;
  1594. csi_status_t status;
  1595. struct mmap_vec vec;
  1596. void *bytes;
  1597. _csi_blob_init (&tmpl, (uint8_t *) string->string, string->len);
  1598. _csi_blob_hash (&tmpl, (uint32_t *) string->string, string->len / sizeof (uint32_t));
  1599. link = _csi_list_find (ctx->_faces, _csi_blob_equal, &tmpl);
  1600. if (link) {
  1601. if (--string->base.ref == 0)
  1602. csi_string_free (ctx, string);
  1603. data = csi_container_of (link, struct _ft_face_data, blob.list);
  1604. *font_face_out = cairo_font_face_reference (data->font_face);
  1605. return CSI_STATUS_SUCCESS;
  1606. }
  1607. if (string->deflate) {
  1608. bytes = inflate_string (ctx, string);
  1609. if (_csi_unlikely (bytes == NULL))
  1610. return _csi_error (CSI_STATUS_NO_MEMORY);
  1611. } else {
  1612. bytes = tmpl.bytes;
  1613. }
  1614. pattern = FcNameParse (bytes);
  1615. if (!pattern)
  1616. {
  1617. /* Fontconfig's representation of charset changed mid 2014;
  1618. * We used to record charset before that. Remove everything
  1619. * after charset if that's present, and try again. */
  1620. char *s = strstr ((char *) bytes, ":charset=");
  1621. if (s)
  1622. {
  1623. *s = '\0';
  1624. pattern = FcNameParse (bytes);
  1625. }
  1626. }
  1627. if (bytes != tmpl.bytes)
  1628. _csi_free (ctx, bytes);
  1629. retry:
  1630. resolved = pattern;
  1631. if (cairo_version () < CAIRO_VERSION_ENCODE (1, 9, 0)) {
  1632. /* prior to 1.9, you needed to pass a resolved pattern */
  1633. resolved = FcFontMatch (NULL, pattern, NULL);
  1634. if (_csi_unlikely (resolved == NULL)) {
  1635. FcPatternDestroy (pattern);
  1636. return _csi_error (CSI_STATUS_NO_MEMORY);
  1637. }
  1638. }
  1639. font_face = cairo_ft_font_face_create_for_pattern (resolved);
  1640. if (resolved != pattern)
  1641. FcPatternDestroy (resolved);
  1642. if (cairo_font_face_status (font_face)) {
  1643. char *filename = NULL;
  1644. /* Try a manual fallback process by eliminating specific requests */
  1645. if (FcPatternGetString (pattern,
  1646. FC_FILE, 0,
  1647. (FcChar8 **) &filename) == FcResultMatch) {
  1648. FcPatternDel (pattern, FC_FILE);
  1649. goto retry;
  1650. }
  1651. }
  1652. FcPatternDestroy (pattern);
  1653. data = _csi_slab_alloc (ctx, sizeof (*data));
  1654. ctx->_faces = _csi_list_prepend (ctx->_faces, &data->blob.list);
  1655. data->ctx = cairo_script_interpreter_reference (ctx);
  1656. data->blob.hash = tmpl.hash;
  1657. data->blob.len = tmpl.len;
  1658. data->bytes = NULL;
  1659. data->face = NULL;
  1660. #ifdef HAVE_MMAP
  1661. vec.bytes = tmpl.bytes;
  1662. vec.num_bytes = tmpl.len;
  1663. data->blob.bytes = _mmap_bytes (&vec, 1);
  1664. if (data->blob.bytes != MAP_FAILED) {
  1665. data->source = NULL;
  1666. if (--string->base.ref == 0)
  1667. csi_string_free (ctx, string);
  1668. } else {
  1669. data->blob.bytes = tmpl.bytes;
  1670. data->source = string;
  1671. }
  1672. #else
  1673. data->blob.bytes = tmpl.bytes;
  1674. data->source = string;
  1675. #endif
  1676. status = cairo_font_face_set_user_data (font_face,
  1677. &_csi_blob_key,
  1678. data, _ft_done_face);
  1679. if (_csi_unlikely (status)) {
  1680. _ft_done_face (data);
  1681. cairo_font_face_destroy (font_face);
  1682. return status;
  1683. }
  1684. data->font_face = font_face;
  1685. *font_face_out = font_face;
  1686. return CSI_STATUS_SUCCESS;
  1687. #else
  1688. if (--string->base.ref == 0)
  1689. csi_string_free (ctx, string);
  1690. return CSI_INT_STATUS_UNSUPPORTED;
  1691. #endif
  1692. }
  1693. static csi_status_t
  1694. _ft_type42_create (csi_t *ctx,
  1695. csi_dictionary_t *font,
  1696. cairo_font_face_t **font_face_out)
  1697. {
  1698. csi_object_t key;
  1699. csi_status_t status;
  1700. /* two basic sub-types, either an FcPattern or embedded font */
  1701. status = csi_name_new_static (ctx, &key, "pattern");
  1702. if (_csi_unlikely (status))
  1703. return status;
  1704. if (csi_dictionary_has (font, key.datum.name)) {
  1705. csi_object_t obj;
  1706. int type;
  1707. status = csi_dictionary_get (ctx, font, key.datum.name, &obj);
  1708. if (_csi_unlikely (status))
  1709. return status;
  1710. type = csi_object_get_type (&obj);
  1711. switch (type) {
  1712. case CSI_OBJECT_TYPE_FILE:
  1713. status = _csi_file_as_string (ctx, obj.datum.file, &obj);
  1714. if (_csi_unlikely (status))
  1715. return status;
  1716. break;
  1717. case CSI_OBJECT_TYPE_STRING:
  1718. obj.datum.object->ref++;
  1719. break;
  1720. default:
  1721. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  1722. }
  1723. return _ft_create_for_pattern (ctx,
  1724. obj.datum.string,
  1725. font_face_out);
  1726. }
  1727. status = csi_name_new_static (ctx, &key, "source");
  1728. if (_csi_unlikely (status))
  1729. return status;
  1730. if (csi_dictionary_has (font, key.datum.name)) {
  1731. csi_object_t obj;
  1732. long index, flags;
  1733. int type;
  1734. index = 0;
  1735. status = _csi_dictionary_get_integer (ctx, font, "index", TRUE, &index);
  1736. if (_csi_unlikely (status))
  1737. return status;
  1738. flags = 0;
  1739. status = _csi_dictionary_get_integer (ctx, font, "flags", TRUE, &flags);
  1740. if (_csi_unlikely (status))
  1741. return status;
  1742. status = csi_name_new_static (ctx, &key, "source");
  1743. if (_csi_unlikely (status))
  1744. return status;
  1745. status = csi_dictionary_get (ctx, font, key.datum.name, &obj);
  1746. if (_csi_unlikely (status))
  1747. return status;
  1748. type = csi_object_get_type (&obj);
  1749. switch (type) {
  1750. case CSI_OBJECT_TYPE_FILE:
  1751. status = _csi_file_as_string (ctx, obj.datum.file, &obj);
  1752. if (_csi_unlikely (status))
  1753. return status;
  1754. break;
  1755. case CSI_OBJECT_TYPE_STRING:
  1756. obj.datum.object->ref++;
  1757. break;
  1758. default:
  1759. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  1760. }
  1761. return _ft_create_for_source (ctx, obj.datum.string,
  1762. index, flags,
  1763. font_face_out);
  1764. }
  1765. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  1766. }
  1767. #else
  1768. #define _ft_type42_create(ctx, font, face_out) CSI_INT_STATUS_UNSUPPORTED
  1769. #endif
  1770. static char *
  1771. _fc_strcpy (csi_t *ctx, const char *str)
  1772. {
  1773. char *ret;
  1774. int len;
  1775. ret = strchr (str, ':');
  1776. if (ret != NULL)
  1777. len = ret - str;
  1778. else
  1779. len = strlen (str);
  1780. ret = _csi_alloc (ctx, len+1);
  1781. if (_csi_unlikely (ret == NULL))
  1782. return NULL;
  1783. memcpy (ret, str, len);
  1784. ret[len] = '\0';
  1785. return ret;
  1786. }
  1787. static cairo_font_face_t *
  1788. _select_font (const char *name)
  1789. {
  1790. cairo_surface_t *surface;
  1791. cairo_font_face_t *face;
  1792. cairo_t *cr;
  1793. /* create a dummy context to choose a font */
  1794. surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 0, 0);
  1795. cr = cairo_create (surface);
  1796. cairo_surface_destroy (surface);
  1797. cairo_select_font_face (cr, name,
  1798. CAIRO_FONT_SLANT_NORMAL,
  1799. CAIRO_FONT_WEIGHT_NORMAL);
  1800. face = cairo_font_face_reference (cairo_get_font_face (cr));
  1801. cairo_destroy (cr);
  1802. return face;
  1803. }
  1804. static csi_status_t
  1805. _ft_fallback_create_for_pattern (csi_t *ctx,
  1806. csi_string_t *string,
  1807. cairo_font_face_t **font_face_out)
  1808. {
  1809. char *str, *name;
  1810. str = string->string;
  1811. #if 0
  1812. name = strstr (str, "fullname=");
  1813. if (name != NULL)
  1814. str = name + 9;
  1815. #endif
  1816. name = _fc_strcpy (ctx, str);
  1817. if (_csi_unlikely (name == NULL))
  1818. return _csi_error (CSI_STATUS_NO_MEMORY);
  1819. *font_face_out = _select_font (name);
  1820. _csi_free (ctx, name);
  1821. return CSI_STATUS_SUCCESS;
  1822. }
  1823. static csi_status_t
  1824. _ft_type42_fallback_create (csi_t *ctx,
  1825. csi_dictionary_t *font,
  1826. cairo_font_face_t **font_face_out)
  1827. {
  1828. csi_object_t key;
  1829. csi_status_t status;
  1830. /* attempt to select a similar font */
  1831. /* two basic sub-types, either an FcPattern or embedded font */
  1832. status = csi_name_new_static (ctx, &key, "pattern");
  1833. if (_csi_unlikely (status))
  1834. return status;
  1835. if (csi_dictionary_has (font, key.datum.name)) {
  1836. csi_object_t obj;
  1837. int type;
  1838. status = csi_dictionary_get (ctx, font, key.datum.name, &obj);
  1839. if (_csi_unlikely (status))
  1840. return status;
  1841. type = csi_object_get_type (&obj);
  1842. switch (type) {
  1843. case CSI_OBJECT_TYPE_FILE:
  1844. status = _csi_file_as_string (ctx, obj.datum.file, &obj);
  1845. if (_csi_unlikely (status))
  1846. return status;
  1847. break;
  1848. case CSI_OBJECT_TYPE_STRING:
  1849. obj.datum.object->ref++;
  1850. break;
  1851. default:
  1852. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  1853. }
  1854. return _ft_fallback_create_for_pattern (ctx,
  1855. obj.datum.string,
  1856. font_face_out);
  1857. }
  1858. /* XXX: enable the trace to run */
  1859. *font_face_out = _select_font ("Sans");
  1860. return CSI_STATUS_SUCCESS;
  1861. }
  1862. static csi_status_t
  1863. _font_type42 (csi_t *ctx, csi_dictionary_t *font, cairo_font_face_t **font_face)
  1864. {
  1865. csi_status_t status;
  1866. status = _ft_type42_create (ctx, font, font_face);
  1867. if (_csi_likely (status != CSI_INT_STATUS_UNSUPPORTED))
  1868. return status;
  1869. return _ft_type42_fallback_create (ctx, font, font_face);
  1870. }
  1871. static csi_status_t
  1872. _font (csi_t *ctx)
  1873. {
  1874. csi_dictionary_t *font;
  1875. csi_status_t status;
  1876. cairo_font_face_t *font_face = NULL; /* silence the compiler */
  1877. csi_proxy_t *proxy;
  1878. csi_object_t obj;
  1879. long type;
  1880. check (1);
  1881. status = _csi_ostack_get_dictionary (ctx, 0, &font);
  1882. if (_csi_unlikely (status))
  1883. return status;
  1884. status = _csi_dictionary_get_integer (ctx, font, "type", FALSE, &type);
  1885. if (_csi_unlikely (status))
  1886. return status;
  1887. switch (type) {
  1888. case 3:
  1889. status = _font_type3 (ctx, font, &font_face);
  1890. break;
  1891. case 42:
  1892. status = _font_type42 (ctx, font, &font_face);
  1893. break;
  1894. default:
  1895. status = _csi_error (CSI_STATUS_INVALID_SCRIPT);
  1896. break;
  1897. }
  1898. if (_csi_unlikely (status))
  1899. return status;
  1900. /* transfer ownership of dictionary to cairo_font_face_t */
  1901. proxy = _csi_proxy_create (ctx, font_face, font, NULL, NULL);
  1902. if (_csi_unlikely (proxy == NULL)) {
  1903. cairo_font_face_destroy (font_face);
  1904. return _csi_error (CSI_STATUS_NO_MEMORY);
  1905. }
  1906. status = cairo_font_face_set_user_data (font_face,
  1907. &_csi_proxy_key,
  1908. proxy, _csi_proxy_destroy);
  1909. if (_csi_unlikely (status)) {
  1910. _csi_proxy_destroy (proxy);
  1911. cairo_font_face_destroy (font_face);
  1912. return status;
  1913. }
  1914. obj.type = CSI_OBJECT_TYPE_FONT;
  1915. obj.datum.font_face = font_face;
  1916. pop (1);
  1917. status = push (&obj);
  1918. if (_csi_unlikely (status)) {
  1919. cairo_font_face_destroy (font_face);
  1920. return status;
  1921. }
  1922. return CSI_STATUS_SUCCESS;
  1923. }
  1924. static csi_status_t
  1925. _for (csi_t *ctx)
  1926. {
  1927. csi_array_t *proc;
  1928. csi_status_t status;
  1929. long i, inc, limit;
  1930. check (4);
  1931. status = _csi_ostack_get_procedure (ctx, 0, &proc);
  1932. if (_csi_unlikely (status))
  1933. return status;
  1934. status = _csi_ostack_get_integer (ctx, 1, &limit);
  1935. if (_csi_unlikely (status))
  1936. return status;
  1937. status = _csi_ostack_get_integer (ctx, 2, &inc);
  1938. if (_csi_unlikely (status))
  1939. return status;
  1940. status = _csi_ostack_get_integer (ctx, 3, &i);
  1941. if (_csi_unlikely (status))
  1942. return status;
  1943. proc->base.ref++;
  1944. pop (4);
  1945. for (; i <= limit; i += inc) {
  1946. status = _csi_push_ostack_integer (ctx, i);
  1947. if (_csi_unlikely (status))
  1948. break;
  1949. status = _csi_array_execute (ctx, proc);
  1950. if (_csi_unlikely (status))
  1951. break;
  1952. }
  1953. if (--proc->base.ref == 0)
  1954. csi_array_free (ctx, proc);
  1955. return status;
  1956. }
  1957. static csi_status_t
  1958. _ge (csi_t *ctx)
  1959. {
  1960. csi_status_t status;
  1961. csi_object_t *a, *b;
  1962. int cmp;
  1963. check (2);
  1964. b = _csi_peek_ostack (ctx, 0);
  1965. a = _csi_peek_ostack (ctx, 1);
  1966. status = csi_object_compare (a, b, &cmp);
  1967. if (_csi_unlikely (status))
  1968. return status;
  1969. pop (2);
  1970. return _csi_push_ostack_boolean (ctx, cmp >= 0);
  1971. }
  1972. static csi_status_t
  1973. _proxy_get (csi_proxy_t *proxy,
  1974. csi_name_t key)
  1975. {
  1976. csi_object_t obj;
  1977. csi_status_t status;
  1978. if (_csi_unlikely (proxy == NULL || proxy->dictionary == NULL))
  1979. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  1980. status = csi_dictionary_get (proxy->ctx, proxy->dictionary, key, &obj);
  1981. if (_csi_unlikely (status))
  1982. return status;
  1983. return _csi_push_ostack_copy (proxy->ctx, &obj);
  1984. }
  1985. static csi_status_t
  1986. _context_get (csi_t *ctx,
  1987. cairo_t *cr,
  1988. csi_name_t key)
  1989. {
  1990. csi_status_t status;
  1991. csi_object_t obj;
  1992. if (strcmp ((char *) key, "current-point") == 0) {
  1993. double x, y;
  1994. cairo_get_current_point (cr, &x, &y);
  1995. status = _csi_push_ostack_real (ctx, x);
  1996. if (_csi_unlikely (status))
  1997. return status;
  1998. status = _csi_push_ostack_real (ctx, y);
  1999. if (_csi_unlikely (status))
  2000. return status;
  2001. return CSI_STATUS_SUCCESS;
  2002. } else if (strcmp ((char *) key, "source") == 0) {
  2003. obj.type = CSI_OBJECT_TYPE_PATTERN;
  2004. obj.datum.pattern = cairo_pattern_reference (cairo_get_source (cr));
  2005. } else if (strcmp ((char *) key, "target") == 0) {
  2006. obj.type = CSI_OBJECT_TYPE_SURFACE;
  2007. obj.datum.surface = cairo_surface_reference (cairo_get_target (cr));
  2008. } else if (strcmp ((char *) key, "group-target") == 0) {
  2009. obj.type = CSI_OBJECT_TYPE_SURFACE;
  2010. obj.datum.surface = cairo_surface_reference (cairo_get_group_target (cr));
  2011. } else if (strcmp ((char *) key, "scaled-font") == 0) {
  2012. obj.type = CSI_OBJECT_TYPE_SCALED_FONT;
  2013. obj.datum.scaled_font = cairo_scaled_font_reference (cairo_get_scaled_font (cr));
  2014. } else if (strcmp ((char *) key, "font-face") == 0) {
  2015. obj.type = CSI_OBJECT_TYPE_FONT;
  2016. obj.datum.font_face = cairo_font_face_reference (cairo_get_font_face (cr));
  2017. } else
  2018. return _proxy_get (cairo_get_user_data (cr, &_csi_proxy_key), key);
  2019. return push (&obj);
  2020. }
  2021. static csi_status_t
  2022. _font_get (csi_t *ctx,
  2023. cairo_font_face_t *font_face,
  2024. csi_name_t key)
  2025. {
  2026. return _proxy_get (cairo_font_face_get_user_data (font_face,
  2027. &_csi_proxy_key),
  2028. key);
  2029. }
  2030. static csi_status_t
  2031. _pattern_get (csi_t *ctx,
  2032. cairo_pattern_t *pattern,
  2033. csi_name_t key)
  2034. {
  2035. csi_status_t status;
  2036. if (strcmp ((char *) key, "type") == 0)
  2037. return _csi_push_ostack_integer (ctx, cairo_pattern_get_type (pattern));
  2038. if (strcmp ((char *) key, "filter") == 0)
  2039. return _csi_push_ostack_integer (ctx, cairo_pattern_get_filter (pattern));
  2040. if (strcmp ((char *) key, "extend") == 0)
  2041. return _csi_push_ostack_integer (ctx, cairo_pattern_get_extend (pattern));
  2042. if (strcmp ((char *) key, "matrix") == 0) {
  2043. csi_object_t obj;
  2044. cairo_matrix_t m;
  2045. cairo_pattern_get_matrix (pattern, &m);
  2046. status = csi_matrix_new_from_matrix (ctx, &obj, &m);
  2047. if (_csi_unlikely (status))
  2048. return status;
  2049. return push (&obj);
  2050. }
  2051. return _proxy_get (cairo_pattern_get_user_data (pattern, &_csi_proxy_key),
  2052. key);
  2053. }
  2054. static csi_status_t
  2055. _scaled_font_get (csi_t *ctx,
  2056. cairo_scaled_font_t *font,
  2057. csi_name_t key)
  2058. {
  2059. return _proxy_get (cairo_scaled_font_get_user_data (font, &_csi_proxy_key),
  2060. key);
  2061. }
  2062. static csi_status_t
  2063. _surface_get (csi_t *ctx,
  2064. cairo_surface_t *surface,
  2065. csi_name_t key)
  2066. {
  2067. if (strcmp ((char *) key, "type") == 0) {
  2068. return _csi_push_ostack_integer (ctx, cairo_surface_get_type (surface));
  2069. }
  2070. if (strcmp ((char *) key, "content") == 0) {
  2071. return _csi_push_ostack_integer (ctx,
  2072. cairo_surface_get_content (surface));
  2073. }
  2074. return _proxy_get (cairo_surface_get_user_data (surface, &_csi_proxy_key),
  2075. key);
  2076. }
  2077. static csi_status_t
  2078. _get (csi_t *ctx)
  2079. {
  2080. csi_object_t *key, *src, obj;
  2081. csi_status_t status;
  2082. int type;
  2083. check (2);
  2084. key = _csi_peek_ostack (ctx, 0);
  2085. src = _csi_peek_ostack (ctx, 1);
  2086. pop (1);
  2087. type = csi_object_get_type (src);
  2088. switch (type) {
  2089. case CSI_OBJECT_TYPE_DICTIONARY:
  2090. if (_csi_unlikely (csi_object_get_type (key) != CSI_OBJECT_TYPE_NAME))
  2091. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  2092. status = csi_dictionary_get (ctx,
  2093. src->datum.dictionary,
  2094. key->datum.name,
  2095. &obj);
  2096. break;
  2097. case CSI_OBJECT_TYPE_ARRAY:
  2098. if (_csi_unlikely (csi_object_get_type (key) != CSI_OBJECT_TYPE_INTEGER))
  2099. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  2100. status = csi_array_get (ctx,
  2101. src->datum.array,
  2102. key->datum.integer,
  2103. &obj);
  2104. break;
  2105. #if 0
  2106. case CSI_OBJECT_TYPE_STRING:
  2107. status = csi_string_get (src, key, &obj);
  2108. break;
  2109. #endif
  2110. case CSI_OBJECT_TYPE_CONTEXT:
  2111. if (_csi_unlikely (csi_object_get_type (key) != CSI_OBJECT_TYPE_NAME))
  2112. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  2113. return _context_get (ctx, src->datum.cr, key->datum.name);
  2114. case CSI_OBJECT_TYPE_FONT:
  2115. if (_csi_unlikely (csi_object_get_type (key) != CSI_OBJECT_TYPE_NAME))
  2116. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  2117. return _font_get (ctx, src->datum.font_face, key->datum.name);
  2118. case CSI_OBJECT_TYPE_PATTERN:
  2119. if (_csi_unlikely (csi_object_get_type (key) != CSI_OBJECT_TYPE_NAME))
  2120. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  2121. return _pattern_get (ctx, src->datum.pattern, key->datum.name);
  2122. case CSI_OBJECT_TYPE_SCALED_FONT:
  2123. if (_csi_unlikely (csi_object_get_type (key) != CSI_OBJECT_TYPE_NAME))
  2124. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  2125. return _scaled_font_get (ctx, src->datum.scaled_font, key->datum.name);
  2126. case CSI_OBJECT_TYPE_SURFACE:
  2127. if (_csi_unlikely (csi_object_get_type (key) != CSI_OBJECT_TYPE_NAME))
  2128. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  2129. return _surface_get (ctx, src->datum.surface, key->datum.name);
  2130. default:
  2131. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  2132. }
  2133. if (_csi_unlikely (status))
  2134. return status;
  2135. return _csi_push_ostack_copy (ctx, &obj);
  2136. }
  2137. struct glyph_advance_cache {
  2138. csi_t *ctx;
  2139. double glyph_advance[256][2];
  2140. unsigned long have_glyph_advance[256];
  2141. };
  2142. static void
  2143. glyph_advance_cache_destroy (void *closure)
  2144. {
  2145. struct glyph_advance_cache *cache = closure;
  2146. _csi_free (cache->ctx, cache);
  2147. }
  2148. static int
  2149. _glyph_string (csi_t *ctx,
  2150. csi_array_t *array,
  2151. cairo_scaled_font_t *scaled_font,
  2152. cairo_glyph_t *glyphs)
  2153. {
  2154. struct glyph_advance_cache uncached;
  2155. struct glyph_advance_cache *cache;
  2156. csi_integer_t nglyphs, i, j;
  2157. double x, y, dx;
  2158. cairo_status_t status;
  2159. if (cairo_scaled_font_status (scaled_font))
  2160. return 0;
  2161. cache = cairo_scaled_font_get_user_data (scaled_font,
  2162. (cairo_user_data_key_t *) ctx);
  2163. if (cache == NULL) {
  2164. cache = _csi_alloc (ctx, sizeof (*cache));
  2165. if (_csi_likely (cache != NULL)) {
  2166. cache->ctx = ctx;
  2167. memset (cache->have_glyph_advance, 0xff,
  2168. sizeof (cache->have_glyph_advance));
  2169. status = cairo_scaled_font_set_user_data (scaled_font,
  2170. (cairo_user_data_key_t *) ctx,
  2171. cache,
  2172. glyph_advance_cache_destroy);
  2173. if (_csi_unlikely (status)) {
  2174. _csi_free (ctx, cache);
  2175. cache = NULL;
  2176. }
  2177. }
  2178. }
  2179. if (_csi_unlikely (cache == NULL)) {
  2180. cache = &uncached;
  2181. cache->ctx = ctx;
  2182. memset (cache->have_glyph_advance, 0xff,
  2183. sizeof (cache->have_glyph_advance));
  2184. }
  2185. nglyphs = 0;
  2186. x = y = 0;
  2187. for (i = 0; i < array->stack.len; i++) {
  2188. const csi_object_t *obj = &array->stack.objects[i];
  2189. int type = csi_object_get_type (obj);
  2190. switch (type) {
  2191. case CSI_OBJECT_TYPE_ARRAY: {
  2192. const csi_array_t *glyph_array = obj->datum.array;
  2193. for (j = 0; j < glyph_array->stack.len; j++) {
  2194. unsigned long g;
  2195. int gi;
  2196. obj = &glyph_array->stack.objects[j];
  2197. if (csi_object_get_type (obj) != CSI_OBJECT_TYPE_INTEGER)
  2198. break;
  2199. g = obj->datum.integer;
  2200. glyphs[nglyphs].index = g;
  2201. glyphs[nglyphs].x = x;
  2202. glyphs[nglyphs].y = y;
  2203. gi = g % ARRAY_LENGTH (cache->have_glyph_advance);
  2204. if (cache->have_glyph_advance[gi] != g) {
  2205. cairo_text_extents_t extents;
  2206. cairo_scaled_font_glyph_extents (scaled_font,
  2207. &glyphs[nglyphs], 1,
  2208. &extents);
  2209. cache->glyph_advance[gi][0] = extents.x_advance;
  2210. cache->glyph_advance[gi][1] = extents.y_advance;
  2211. cache->have_glyph_advance[gi] = g;
  2212. }
  2213. x += cache->glyph_advance[gi][0];
  2214. y += cache->glyph_advance[gi][1];
  2215. nglyphs++;
  2216. }
  2217. break;
  2218. }
  2219. case CSI_OBJECT_TYPE_STRING: {
  2220. const csi_string_t *glyph_string = obj->datum.string;
  2221. for (j = 0; j < glyph_string->len; j++) {
  2222. uint8_t g;
  2223. g = glyph_string->string[j];
  2224. glyphs[nglyphs].index = g;
  2225. glyphs[nglyphs].x = x;
  2226. glyphs[nglyphs].y = y;
  2227. if (cache->have_glyph_advance[g] != g) {
  2228. cairo_text_extents_t extents;
  2229. cairo_scaled_font_glyph_extents (scaled_font,
  2230. &glyphs[nglyphs], 1,
  2231. &extents);
  2232. cache->glyph_advance[g][0] = extents.x_advance;
  2233. cache->glyph_advance[g][1] = extents.y_advance;
  2234. cache->have_glyph_advance[g] = g;
  2235. }
  2236. x += cache->glyph_advance[g][0];
  2237. y += cache->glyph_advance[g][1];
  2238. nglyphs++;
  2239. }
  2240. break;
  2241. }
  2242. case CSI_OBJECT_TYPE_INTEGER:
  2243. case CSI_OBJECT_TYPE_REAL: /* dx or x*/
  2244. dx = csi_number_get_value (obj);
  2245. if (i+1 == array->stack.len)
  2246. break;
  2247. type = csi_object_get_type (&array->stack.objects[i+1]);
  2248. switch (type) {
  2249. case CSI_OBJECT_TYPE_INTEGER:
  2250. case CSI_OBJECT_TYPE_REAL: /* y */
  2251. y = csi_number_get_value (&array->stack.objects[i+1]);
  2252. x = dx;
  2253. i++;
  2254. break;
  2255. default:
  2256. x += dx;
  2257. }
  2258. }
  2259. }
  2260. return nglyphs;
  2261. }
  2262. static csi_status_t
  2263. _glyph_path (csi_t *ctx)
  2264. {
  2265. csi_array_t *array;
  2266. csi_status_t status;
  2267. cairo_t *cr;
  2268. cairo_glyph_t stack_glyphs[256], *glyphs;
  2269. csi_integer_t nglyphs, i;
  2270. check (2);
  2271. status = _csi_ostack_get_array (ctx, 0, &array);
  2272. if (_csi_unlikely (status))
  2273. return status;
  2274. status = _csi_ostack_get_context (ctx, 1, &cr);
  2275. if (_csi_unlikely (status))
  2276. return status;
  2277. /* count glyphs */
  2278. nglyphs = 0;
  2279. for (i = 0; i < array->stack.len; i++) {
  2280. csi_object_t *obj = &array->stack.objects[i];
  2281. int type = csi_object_get_type (obj);
  2282. switch (type) {
  2283. case CSI_OBJECT_TYPE_ARRAY:
  2284. nglyphs += obj->datum.array->stack.len;
  2285. break;
  2286. case CSI_OBJECT_TYPE_STRING:
  2287. nglyphs += obj->datum.string->len;
  2288. break;
  2289. }
  2290. }
  2291. if (nglyphs == 0) {
  2292. pop (1);
  2293. return CSI_STATUS_SUCCESS;
  2294. }
  2295. if (nglyphs > ARRAY_LENGTH (stack_glyphs)) {
  2296. if (_csi_unlikely ((unsigned) nglyphs >= INT_MAX / sizeof (cairo_glyph_t)))
  2297. return _csi_error (CSI_STATUS_NO_MEMORY);
  2298. glyphs = _csi_alloc (ctx, sizeof (cairo_glyph_t) * nglyphs);
  2299. if (_csi_unlikely (glyphs == NULL))
  2300. return _csi_error (CSI_STATUS_NO_MEMORY);
  2301. } else
  2302. glyphs = stack_glyphs;
  2303. nglyphs = _glyph_string (ctx, array, cairo_get_scaled_font (cr), glyphs);
  2304. cairo_glyph_path (cr, glyphs, nglyphs);
  2305. if (glyphs != stack_glyphs)
  2306. _csi_free (ctx, glyphs);
  2307. pop (1);
  2308. return CSI_STATUS_SUCCESS;
  2309. }
  2310. static csi_status_t
  2311. _gray (csi_t *ctx)
  2312. {
  2313. csi_object_t obj;
  2314. csi_status_t status;
  2315. double g;
  2316. check (1);
  2317. status = _csi_ostack_get_number (ctx, 0, &g);
  2318. if (_csi_unlikely (status))
  2319. return status;
  2320. pop (1);
  2321. obj.type = CSI_OBJECT_TYPE_PATTERN;
  2322. obj.datum.pattern = cairo_pattern_create_rgba (g, g, g, 1);
  2323. return push (&obj);
  2324. }
  2325. static csi_status_t
  2326. _gt (csi_t *ctx)
  2327. {
  2328. csi_status_t status;
  2329. csi_object_t *a, *b;
  2330. int cmp;
  2331. check (2);
  2332. b = _csi_peek_ostack (ctx, 0);
  2333. a = _csi_peek_ostack (ctx, 1);
  2334. status = csi_object_compare (a, b, &cmp);
  2335. if (_csi_unlikely (status))
  2336. return status;
  2337. pop (2);
  2338. return _csi_push_ostack_boolean (ctx, cmp > 0);
  2339. }
  2340. static csi_status_t
  2341. _identity (csi_t *ctx)
  2342. {
  2343. csi_object_t obj;
  2344. csi_status_t status;
  2345. status = csi_matrix_new (ctx, &obj);
  2346. if (_csi_unlikely (status))
  2347. return status;
  2348. return push (&obj);
  2349. }
  2350. static csi_status_t
  2351. _if (csi_t *ctx)
  2352. {
  2353. csi_array_t *proc;
  2354. csi_boolean_t predicate = FALSE; /* silence the compiler */
  2355. csi_status_t status;
  2356. check (2);
  2357. status = _csi_ostack_get_procedure (ctx, 0, &proc);
  2358. if (_csi_unlikely (status))
  2359. return status;
  2360. status = _csi_ostack_get_boolean (ctx, 1, &predicate);
  2361. if (_csi_unlikely (status))
  2362. return status;
  2363. proc->base.ref++;
  2364. pop (2);
  2365. if (predicate)
  2366. status = _csi_array_execute (ctx, proc);
  2367. if (--proc->base.ref == 0)
  2368. csi_array_free (ctx, proc);
  2369. return status;
  2370. }
  2371. static csi_status_t
  2372. _ifelse (csi_t *ctx)
  2373. {
  2374. csi_array_t *true_proc, *false_proc;
  2375. csi_boolean_t predicate = FALSE; /* silence the compiler */
  2376. csi_status_t status;
  2377. check (3);
  2378. status = _csi_ostack_get_procedure (ctx, 0, &false_proc);
  2379. if (_csi_unlikely (status))
  2380. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  2381. status = _csi_ostack_get_procedure (ctx, 1, &true_proc);
  2382. if (_csi_unlikely (status))
  2383. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  2384. status = _csi_ostack_get_boolean (ctx, 2, &predicate);
  2385. if (_csi_unlikely (status))
  2386. return status;
  2387. true_proc->base.ref++;
  2388. false_proc->base.ref++;
  2389. pop (3);
  2390. if (predicate)
  2391. status = _csi_array_execute (ctx, true_proc);
  2392. else
  2393. status = _csi_array_execute (ctx, false_proc);
  2394. if (--true_proc->base.ref == 0)
  2395. csi_array_free (ctx, true_proc);
  2396. if (--false_proc->base.ref == 0)
  2397. csi_array_free (ctx, false_proc);
  2398. return status;
  2399. }
  2400. static csi_status_t
  2401. _image_read_raw (csi_t *ctx,
  2402. csi_object_t *src,
  2403. cairo_format_t format,
  2404. int width, int height,
  2405. cairo_surface_t **image_out)
  2406. {
  2407. cairo_surface_t *image;
  2408. uint8_t *bp, *data;
  2409. int rem, len, ret, x, rowlen, instride, stride;
  2410. cairo_status_t status;
  2411. if (width == 0 || height == 0) {
  2412. *image_out = cairo_image_surface_create (format, 0, 0);
  2413. return CSI_STATUS_SUCCESS;
  2414. }
  2415. if (ctx->hooks.create_source_image != NULL) {
  2416. image = ctx->hooks.create_source_image (ctx->hooks.closure,
  2417. format, width, height,
  2418. 0);
  2419. stride = cairo_image_surface_get_stride (image);
  2420. data = cairo_image_surface_get_data (image);
  2421. } else {
  2422. stride = cairo_format_stride_for_width (format, width);
  2423. data = malloc (stride * height);
  2424. if (data == NULL)
  2425. return CAIRO_STATUS_NO_MEMORY;
  2426. image = cairo_image_surface_create_for_data (data, format,
  2427. width, height, stride);
  2428. status = cairo_surface_set_user_data (image,
  2429. (const cairo_user_data_key_t *) image,
  2430. data, free);
  2431. if (status) {
  2432. cairo_surface_destroy (image);
  2433. free (image);
  2434. return status;
  2435. }
  2436. }
  2437. switch (format) {
  2438. case CAIRO_FORMAT_A1:
  2439. instride = rowlen = (width+7)/8;
  2440. break;
  2441. case CAIRO_FORMAT_A8:
  2442. instride = rowlen = width;
  2443. break;
  2444. case CAIRO_FORMAT_RGB16_565:
  2445. instride = rowlen = 2 * width;
  2446. break;
  2447. case CAIRO_FORMAT_RGB24:
  2448. rowlen = 3 * width;
  2449. instride = 4 *width;
  2450. break;
  2451. default:
  2452. case CAIRO_FORMAT_RGB30:
  2453. case CAIRO_FORMAT_INVALID:
  2454. case CAIRO_FORMAT_ARGB32:
  2455. instride = rowlen = 4 * width;
  2456. break;
  2457. }
  2458. len = rowlen * height;
  2459. if (rowlen == instride &&
  2460. src->type == CSI_OBJECT_TYPE_STRING &&
  2461. len == src->datum.string->deflate)
  2462. {
  2463. csi_string_t *s = src->datum.string;
  2464. unsigned long out = s->deflate;
  2465. switch (s->method) {
  2466. default:
  2467. case NONE:
  2468. err_decompress:
  2469. cairo_surface_destroy (image);
  2470. return _csi_error (CSI_STATUS_READ_ERROR);
  2471. case ZLIB:
  2472. #if HAVE_ZLIB
  2473. if (uncompress ((Bytef *) data, &out,
  2474. (Bytef *) s->string, s->len) != Z_OK)
  2475. #endif
  2476. goto err_decompress;
  2477. break;
  2478. case LZO:
  2479. #if HAVE_LZO
  2480. if (lzo2a_decompress ((Bytef *) s->string, s->len,
  2481. (Bytef *) data, &out,
  2482. NULL))
  2483. #endif
  2484. goto err_decompress;
  2485. break;
  2486. }
  2487. }
  2488. else
  2489. {
  2490. csi_object_t file;
  2491. status = csi_object_as_file (ctx, src, &file);
  2492. if (_csi_unlikely (status)) {
  2493. cairo_surface_destroy (image);
  2494. return status;
  2495. }
  2496. bp = data;
  2497. rem = len;
  2498. while (rem) {
  2499. ret = csi_file_read (file.datum.file, bp, rem);
  2500. if (_csi_unlikely (ret == 0)) {
  2501. cairo_surface_destroy (image);
  2502. return _csi_error (CSI_STATUS_READ_ERROR);
  2503. }
  2504. rem -= ret;
  2505. bp += ret;
  2506. }
  2507. if (len != height * stride) {
  2508. while (--height) {
  2509. uint8_t *row = data + height * stride;
  2510. /* XXX pixel conversion */
  2511. switch (format) {
  2512. case CAIRO_FORMAT_A1:
  2513. for (x = rowlen; x--; ) {
  2514. uint8_t byte = *--bp;
  2515. row[x] = CSI_BITSWAP8_IF_LITTLE_ENDIAN (byte);
  2516. }
  2517. break;
  2518. case CAIRO_FORMAT_A8:
  2519. for (x = width; x--; )
  2520. row[x] = *--bp;
  2521. break;
  2522. case CAIRO_FORMAT_RGB16_565:
  2523. for (x = width; x--; ) {
  2524. #ifdef WORDS_BIGENDIAN
  2525. row[2*x + 1] = *--bp;
  2526. row[2*x + 0] = *--bp;
  2527. #else
  2528. row[2*x + 0] = *--bp;
  2529. row[2*x + 1] = *--bp;
  2530. #endif
  2531. }
  2532. break;
  2533. case CAIRO_FORMAT_RGB24:
  2534. for (x = width; x--; ) {
  2535. #ifdef WORDS_BIGENDIAN
  2536. row[4*x + 3] = *--bp;
  2537. row[4*x + 2] = *--bp;
  2538. row[4*x + 1] = *--bp;
  2539. row[4*x + 0] = 0xff;
  2540. #else
  2541. row[4*x + 0] = *--bp;
  2542. row[4*x + 1] = *--bp;
  2543. row[4*x + 2] = *--bp;
  2544. row[4*x + 3] = 0xff;
  2545. #endif
  2546. }
  2547. break;
  2548. case CAIRO_FORMAT_RGB30:
  2549. case CAIRO_FORMAT_INVALID:
  2550. case CAIRO_FORMAT_ARGB32:
  2551. /* stride == width */
  2552. break;
  2553. }
  2554. memset (row + instride, 0, stride - instride);
  2555. }
  2556. /* need to treat last row carefully */
  2557. switch (format) {
  2558. case CAIRO_FORMAT_A1:
  2559. for (x = rowlen; x--; ) {
  2560. uint8_t byte = *--bp;
  2561. data[x] = CSI_BITSWAP8_IF_LITTLE_ENDIAN (byte);
  2562. }
  2563. break;
  2564. case CAIRO_FORMAT_A8:
  2565. for (x = width; x--; )
  2566. data[x] = *--bp;
  2567. break;
  2568. case CAIRO_FORMAT_RGB16_565:
  2569. for (x = width; x--; ) {
  2570. #ifdef WORDS_BIGENDIAN
  2571. data[2*x + 1] = *--bp;
  2572. data[2*x + 0] = *--bp;
  2573. #else
  2574. data[2*x + 0] = *--bp;
  2575. data[2*x + 1] = *--bp;
  2576. #endif
  2577. }
  2578. break;
  2579. case CAIRO_FORMAT_RGB24:
  2580. for (x = width; --x>1; ) {
  2581. #ifdef WORDS_BIGENDIAN
  2582. data[4*x + 3] = *--bp;
  2583. data[4*x + 2] = *--bp;
  2584. data[4*x + 1] = *--bp;
  2585. data[4*x + 0] = 0xff;
  2586. #else
  2587. data[4*x + 0] = *--bp;
  2588. data[4*x + 1] = *--bp;
  2589. data[4*x + 2] = *--bp;
  2590. data[4*x + 3] = 0xff;
  2591. #endif
  2592. }
  2593. if (width > 1) {
  2594. uint8_t rgb[2][3];
  2595. /* shuffle the last couple of overlapping pixels */
  2596. rgb[1][0] = data[5];
  2597. rgb[1][1] = data[4];
  2598. rgb[1][2] = data[3];
  2599. rgb[0][0] = data[2];
  2600. rgb[0][1] = data[1];
  2601. rgb[0][2] = data[0];
  2602. #ifdef WORDS_BIGENDIAN
  2603. data[4] = 0xff;
  2604. data[5] = rgb[1][2];
  2605. data[6] = rgb[1][1];
  2606. data[7] = rgb[1][0];
  2607. data[0] = 0xff;
  2608. data[1] = rgb[0][2];
  2609. data[2] = rgb[0][1];
  2610. data[3] = rgb[0][0];
  2611. #else
  2612. data[7] = 0xff;
  2613. data[6] = rgb[1][2];
  2614. data[5] = rgb[1][1];
  2615. data[4] = rgb[1][0];
  2616. data[3] = 0xff;
  2617. data[2] = rgb[0][2];
  2618. data[1] = rgb[0][1];
  2619. data[0] = rgb[0][0];
  2620. #endif
  2621. } else {
  2622. #ifdef WORDS_BIGENDIAN
  2623. data[0] = 0xff;
  2624. data[1] = data[0];
  2625. data[2] = data[1];
  2626. data[3] = data[2];
  2627. #else
  2628. data[3] = data[0];
  2629. data[0] = data[2];
  2630. data[2] = data[3];
  2631. data[3] = 0xff;
  2632. #endif
  2633. }
  2634. break;
  2635. case CAIRO_FORMAT_RGB30:
  2636. case CAIRO_FORMAT_INVALID:
  2637. case CAIRO_FORMAT_ARGB32:
  2638. /* stride == width */
  2639. break;
  2640. }
  2641. memset (data + instride, 0, stride - instride);
  2642. } else {
  2643. #ifndef WORDS_BIGENDIAN
  2644. switch (format) {
  2645. case CAIRO_FORMAT_A1:
  2646. for (x = 0; x < len; x++) {
  2647. uint8_t byte = data[x];
  2648. data[x] = CSI_BITSWAP8_IF_LITTLE_ENDIAN (byte);
  2649. }
  2650. break;
  2651. case CAIRO_FORMAT_RGB16_565:
  2652. {
  2653. uint32_t *rgba = (uint32_t *) data;
  2654. for (x = len/2; x--; rgba++) {
  2655. *rgba = bswap_16 (*rgba);
  2656. }
  2657. }
  2658. break;
  2659. case CAIRO_FORMAT_ARGB32:
  2660. {
  2661. uint32_t *rgba = (uint32_t *) data;
  2662. for (x = len/4; x--; rgba++) {
  2663. *rgba = bswap_32 (*rgba);
  2664. }
  2665. }
  2666. break;
  2667. case CAIRO_FORMAT_A8:
  2668. break;
  2669. case CAIRO_FORMAT_RGB30:
  2670. case CAIRO_FORMAT_RGB24:
  2671. case CAIRO_FORMAT_INVALID:
  2672. default:
  2673. break;
  2674. }
  2675. #endif
  2676. }
  2677. csi_object_free (ctx, &file);
  2678. }
  2679. cairo_surface_mark_dirty (image);
  2680. *image_out = image;
  2681. return CSI_STATUS_SUCCESS;
  2682. }
  2683. static cairo_status_t
  2684. png_read_func (void *closure, uint8_t *data, unsigned int len)
  2685. {
  2686. int ret;
  2687. ret = csi_file_read (closure, data, len);
  2688. if ((unsigned int) ret != len)
  2689. return CAIRO_STATUS_READ_ERROR;
  2690. return CAIRO_STATUS_SUCCESS;
  2691. }
  2692. static csi_status_t
  2693. _image_read_png (csi_file_t *src, cairo_surface_t **out)
  2694. {
  2695. #if CAIRO_HAS_PNG_FUNCTIONS
  2696. *out = cairo_image_surface_create_from_png_stream (png_read_func, src);
  2697. return cairo_surface_status (*out);
  2698. #else
  2699. return CAIRO_STATUS_READ_ERROR;
  2700. #endif
  2701. }
  2702. struct _image_tag {
  2703. csi_t *ctx;
  2704. csi_blob_t blob;
  2705. cairo_surface_t *surface;
  2706. };
  2707. static void
  2708. _image_tag_done (void *closure)
  2709. {
  2710. struct _image_tag *tag = closure;
  2711. csi_t *ctx = tag->ctx;
  2712. ctx->_images = _csi_list_unlink (ctx->_images, &tag->blob.list);
  2713. _csi_slab_free (ctx, tag, sizeof (*tag));
  2714. cairo_script_interpreter_destroy (ctx);
  2715. }
  2716. static void
  2717. _image_hash (csi_blob_t *blob,
  2718. cairo_surface_t *surface)
  2719. {
  2720. uint32_t value;
  2721. value = cairo_image_surface_get_width (surface);
  2722. _csi_blob_hash (blob, &value, 1);
  2723. value = cairo_image_surface_get_height (surface);
  2724. _csi_blob_hash (blob, &value, 1);
  2725. value = cairo_image_surface_get_format (surface);
  2726. _csi_blob_hash (blob, &value, 1);
  2727. }
  2728. static cairo_surface_t *
  2729. _image_cached (csi_t *ctx, cairo_surface_t *surface)
  2730. {
  2731. csi_blob_t tmpl;
  2732. csi_list_t *link;
  2733. uint8_t *data;
  2734. int stride, height;
  2735. struct _image_tag *tag;
  2736. /* check for an existing image */
  2737. data = cairo_image_surface_get_data (surface);
  2738. stride = cairo_image_surface_get_stride (surface);
  2739. height = cairo_image_surface_get_height (surface);
  2740. _csi_blob_init (&tmpl, data, stride * height);
  2741. _image_hash (&tmpl, surface);
  2742. link = _csi_list_find (ctx->_images, _csi_blob_equal, &tmpl);
  2743. if (link) {
  2744. cairo_surface_destroy (surface);
  2745. tag = csi_container_of (link, struct _image_tag, blob.list);
  2746. return cairo_surface_reference (tag->surface);
  2747. }
  2748. /* none found, insert a tag for this one */
  2749. tag = _csi_slab_alloc (ctx, sizeof (struct _image_tag));
  2750. if (tag == NULL)
  2751. return surface;
  2752. ctx->_images = _csi_list_prepend (ctx->_images, &tag->blob.list);
  2753. tag->ctx = cairo_script_interpreter_reference (ctx);
  2754. tag->blob.hash = tmpl.hash;
  2755. tag->blob.bytes = tmpl.bytes;
  2756. tag->blob.len = tmpl.len;
  2757. tag->surface = surface;
  2758. if (cairo_surface_set_user_data (surface, &_csi_blob_key,
  2759. tag, _image_tag_done))
  2760. {
  2761. _image_tag_done (tag);
  2762. }
  2763. return surface;
  2764. }
  2765. static csi_status_t
  2766. _image_load_from_dictionary (csi_t *ctx,
  2767. csi_dictionary_t *dict,
  2768. cairo_surface_t **image_out)
  2769. {
  2770. csi_object_t obj, key;
  2771. long width;
  2772. long height;
  2773. long format;
  2774. cairo_surface_t *image = NULL; /* silence the compiler */
  2775. csi_status_t status;
  2776. /* check for "status? */
  2777. status = _csi_dictionary_get_integer (ctx, dict, "width", FALSE, &width);
  2778. if (_csi_unlikely (status))
  2779. return status;
  2780. status = _csi_dictionary_get_integer (ctx, dict, "height", FALSE, &height);
  2781. if (_csi_unlikely (status))
  2782. return status;
  2783. format = CAIRO_FORMAT_ARGB32;
  2784. status = _csi_dictionary_get_integer (ctx, dict, "format", TRUE, &format);
  2785. if (_csi_unlikely (status))
  2786. return status;
  2787. status = csi_name_new_static (ctx, &key, "source");
  2788. if (_csi_unlikely (status))
  2789. return status;
  2790. if (csi_dictionary_has (dict, key.datum.name)) {
  2791. enum mime_type mime_type;
  2792. csi_object_t file;
  2793. status = csi_dictionary_get (ctx, dict, key.datum.name, &obj);
  2794. if (_csi_unlikely (status))
  2795. return status;
  2796. status = csi_name_new_static (ctx, &key, "mime-type");
  2797. if (_csi_unlikely (status))
  2798. return status;
  2799. mime_type = MIME_TYPE_NONE;
  2800. if (csi_dictionary_has (dict, key.datum.name)) {
  2801. csi_object_t type_obj;
  2802. const char *type_str;
  2803. int type;
  2804. status = csi_dictionary_get (ctx, dict, key.datum.name, &type_obj);
  2805. if (_csi_unlikely (status))
  2806. return status;
  2807. type = csi_object_get_type (&type_obj);
  2808. switch (type) {
  2809. case CSI_OBJECT_TYPE_STRING:
  2810. type_str = type_obj.datum.string->string;
  2811. break;
  2812. case CSI_OBJECT_TYPE_NAME:
  2813. type_str = (char *) type_obj.datum.name;
  2814. break;
  2815. default:
  2816. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  2817. }
  2818. if (strcmp (type_str, CAIRO_MIME_TYPE_PNG) == 0)
  2819. mime_type = MIME_TYPE_PNG;
  2820. }
  2821. /* XXX hook for general mime-type decoder */
  2822. switch (mime_type) {
  2823. case MIME_TYPE_NONE:
  2824. status = _image_read_raw (ctx, &obj, format, width, height, &image);
  2825. break;
  2826. case MIME_TYPE_PNG:
  2827. status = csi_object_as_file (ctx, &obj, &file);
  2828. if (_csi_unlikely (status))
  2829. return status;
  2830. status = _image_read_png (file.datum.file, &image);
  2831. csi_object_free (ctx, &file);
  2832. break;
  2833. }
  2834. if (_csi_unlikely (status))
  2835. return status;
  2836. image = _image_cached (ctx, image);
  2837. } else
  2838. image = cairo_image_surface_create (format, width, height);
  2839. *image_out = image;
  2840. return CSI_STATUS_SUCCESS;
  2841. }
  2842. static csi_status_t
  2843. _image (csi_t *ctx)
  2844. {
  2845. csi_dictionary_t *dict;
  2846. cairo_surface_t *image;
  2847. csi_status_t status;
  2848. csi_object_t obj;
  2849. check (1);
  2850. status = _csi_ostack_get_dictionary (ctx, 0, &dict);
  2851. if (_csi_unlikely (status))
  2852. return status;
  2853. status = _image_load_from_dictionary (ctx, dict, &image);
  2854. if (_csi_unlikely (status))
  2855. return status;
  2856. pop (1);
  2857. obj.type = CSI_OBJECT_TYPE_SURFACE;
  2858. obj.datum.surface = image;
  2859. return push (&obj);
  2860. }
  2861. static csi_status_t
  2862. _index (csi_t *ctx)
  2863. {
  2864. csi_status_t status;
  2865. long n;
  2866. check (1);
  2867. status = _csi_ostack_get_integer (ctx, 0, &n);
  2868. if (_csi_unlikely (status))
  2869. return status;
  2870. pop (1);
  2871. check (n);
  2872. return _csi_push_ostack_copy (ctx, _csi_peek_ostack (ctx, n));
  2873. }
  2874. static csi_status_t
  2875. _integer (csi_t *ctx)
  2876. {
  2877. csi_object_t *obj;
  2878. int type;
  2879. check (1);
  2880. obj = _csi_peek_ostack (ctx, 0);
  2881. type = csi_object_get_type (obj);
  2882. switch (type) {
  2883. case CSI_OBJECT_TYPE_INTEGER:
  2884. break;
  2885. case CSI_OBJECT_TYPE_REAL:
  2886. obj->datum.integer = obj->datum.real;
  2887. break;
  2888. default:
  2889. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  2890. }
  2891. obj->type = CSI_OBJECT_TYPE_INTEGER;
  2892. return CSI_STATUS_SUCCESS;
  2893. }
  2894. static csi_status_t
  2895. _invert (csi_t *ctx)
  2896. {
  2897. csi_object_t obj;
  2898. csi_status_t status;
  2899. cairo_matrix_t m;
  2900. check (1);
  2901. status = _csi_ostack_get_matrix (ctx, 0, &m);
  2902. if (_csi_unlikely (status))
  2903. return status;
  2904. cairo_matrix_invert (&m);
  2905. status = csi_matrix_new_from_matrix (ctx, &obj, &m);
  2906. if (_csi_unlikely (status))
  2907. return status;
  2908. pop (1);
  2909. return push (&obj);
  2910. }
  2911. static csi_status_t
  2912. _le (csi_t *ctx)
  2913. {
  2914. csi_status_t status;
  2915. csi_object_t *a, *b;
  2916. int cmp;
  2917. check (2);
  2918. b = _csi_peek_ostack (ctx, 0);
  2919. a = _csi_peek_ostack (ctx, 1);
  2920. status = csi_object_compare (a, b, &cmp);
  2921. if (_csi_unlikely (status))
  2922. return status;
  2923. pop (2);
  2924. return _csi_push_ostack_boolean (ctx, cmp <= 0);
  2925. }
  2926. static csi_status_t
  2927. _linear (csi_t *ctx)
  2928. {
  2929. csi_object_t obj;
  2930. csi_status_t status;
  2931. double x1, y1, x2, y2;
  2932. check (4);
  2933. status = _csi_ostack_get_number (ctx, 0, &y2);
  2934. if (_csi_unlikely (status))
  2935. return status;
  2936. status = _csi_ostack_get_number (ctx, 1, &x2);
  2937. if (_csi_unlikely (status))
  2938. return status;
  2939. status = _csi_ostack_get_number (ctx, 2, &y1);
  2940. if (_csi_unlikely (status))
  2941. return status;
  2942. status = _csi_ostack_get_number (ctx, 3, &x1);
  2943. if (_csi_unlikely (status))
  2944. return status;
  2945. pop (4);
  2946. obj.type = CSI_OBJECT_TYPE_PATTERN;
  2947. obj.datum.pattern = cairo_pattern_create_linear (x1, y1, x2, y2);
  2948. return push (&obj);
  2949. }
  2950. static csi_status_t
  2951. _line_to (csi_t *ctx)
  2952. {
  2953. csi_status_t status;
  2954. csi_object_t *obj;
  2955. int type;
  2956. double x, y;
  2957. check (3);
  2958. status = _csi_ostack_get_number (ctx, 0, &y);
  2959. if (_csi_unlikely (status))
  2960. return status;
  2961. status = _csi_ostack_get_number (ctx, 1, &x);
  2962. if (_csi_unlikely (status))
  2963. return status;
  2964. /* XXX path object */
  2965. obj = _csi_peek_ostack (ctx, 2);
  2966. type = csi_object_get_type (obj);
  2967. switch (type) {
  2968. case CSI_OBJECT_TYPE_CONTEXT:
  2969. cairo_line_to (obj->datum.cr, x, y);
  2970. break;
  2971. case CSI_OBJECT_TYPE_PATTERN:
  2972. cairo_mesh_pattern_line_to (obj->datum.pattern, x, y);
  2973. break;
  2974. }
  2975. pop (2);
  2976. return CSI_STATUS_SUCCESS;
  2977. }
  2978. static csi_status_t
  2979. _lt (csi_t *ctx)
  2980. {
  2981. csi_status_t status;
  2982. csi_object_t *a, *b;
  2983. int cmp;
  2984. check (2);
  2985. b = _csi_peek_ostack (ctx, 0);
  2986. a = _csi_peek_ostack (ctx, 1);
  2987. status = csi_object_compare (a, b, &cmp);
  2988. if (_csi_unlikely (status))
  2989. return status;
  2990. pop (2);
  2991. return _csi_push_ostack_boolean (ctx, cmp < 0);
  2992. }
  2993. static csi_status_t
  2994. _mark (csi_t *ctx)
  2995. {
  2996. return _csi_push_ostack_mark (ctx);
  2997. }
  2998. static csi_status_t
  2999. _ne (csi_t *ctx)
  3000. {
  3001. csi_object_t *a, *b;
  3002. csi_boolean_t v;
  3003. check (2);
  3004. b = _csi_peek_ostack (ctx, 0);
  3005. a = _csi_peek_ostack (ctx, 1);
  3006. v = ! csi_object_eq (a, b);
  3007. pop (2);
  3008. return _csi_push_ostack_boolean (ctx, v);
  3009. }
  3010. static csi_status_t
  3011. _neg (csi_t *ctx)
  3012. {
  3013. csi_object_t *obj;
  3014. int type;
  3015. check (1);
  3016. obj = _csi_peek_ostack (ctx, 0);
  3017. type = csi_object_get_type (obj);
  3018. switch (type) {
  3019. case CSI_OBJECT_TYPE_INTEGER:
  3020. obj->datum.integer = -obj->datum.integer;
  3021. break;
  3022. case CSI_OBJECT_TYPE_REAL:
  3023. obj->datum.real = -obj->datum.real;
  3024. break;
  3025. default:
  3026. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  3027. }
  3028. return CSI_STATUS_SUCCESS;
  3029. }
  3030. static csi_status_t
  3031. _not (csi_t *ctx)
  3032. {
  3033. csi_object_t *obj;
  3034. int type;
  3035. check (1);
  3036. obj = _csi_peek_ostack (ctx, 0);
  3037. type = csi_object_get_type (obj);
  3038. switch (type) {
  3039. case CSI_OBJECT_TYPE_BOOLEAN:
  3040. obj->datum.boolean = ! obj->datum.boolean;
  3041. break;
  3042. case CSI_OBJECT_TYPE_INTEGER:
  3043. obj->type = CSI_OBJECT_TYPE_BOOLEAN;
  3044. obj->datum.boolean = ! obj->datum.integer;
  3045. break;
  3046. case CSI_OBJECT_TYPE_REAL:
  3047. obj->type = CSI_OBJECT_TYPE_BOOLEAN;
  3048. obj->datum.boolean = obj->datum.real == 0.0;
  3049. break;
  3050. default:
  3051. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  3052. }
  3053. return CSI_STATUS_SUCCESS;
  3054. }
  3055. static csi_status_t
  3056. _new_path (csi_t *ctx)
  3057. {
  3058. /* XXX handle path object */
  3059. return _do_cairo_op (ctx, cairo_new_path);
  3060. }
  3061. static csi_status_t
  3062. _new_sub_path (csi_t *ctx)
  3063. {
  3064. /* XXX handle path object */
  3065. return _do_cairo_op (ctx, cairo_new_sub_path);
  3066. }
  3067. static csi_status_t
  3068. _null (csi_t *ctx)
  3069. {
  3070. return _csi_push_ostack_null (ctx);
  3071. }
  3072. static csi_status_t
  3073. _mask (csi_t *ctx)
  3074. {
  3075. cairo_t *cr;
  3076. cairo_pattern_t *pattern = NULL; /* silence the compiler */
  3077. csi_status_t status;
  3078. check (2);
  3079. status = _csi_ostack_get_pattern (ctx, 0, &pattern);
  3080. if (_csi_unlikely (status))
  3081. return status;
  3082. status = _csi_ostack_get_context (ctx, 1, &cr);
  3083. if (_csi_unlikely (status))
  3084. return status;
  3085. cairo_mask (cr, pattern);
  3086. pop (1);
  3087. return CSI_STATUS_SUCCESS;
  3088. }
  3089. static csi_status_t
  3090. _matrix (csi_t *ctx)
  3091. {
  3092. csi_object_t *obj, matrix;
  3093. double v[6];
  3094. csi_status_t status;
  3095. int n;
  3096. check (1);
  3097. obj = _csi_peek_ostack (ctx, 0);
  3098. if (csi_object_is_number (obj)) {
  3099. check (6);
  3100. for (n = 6; n--; ) {
  3101. status = _csi_ostack_get_number (ctx, 5-n, &v[n]);
  3102. if (_csi_unlikely (status))
  3103. return status;
  3104. }
  3105. status = csi_matrix_new_from_values (ctx, &matrix, v);
  3106. if (_csi_unlikely (status))
  3107. return status;
  3108. pop (6);
  3109. } else {
  3110. csi_array_t *array;
  3111. status = _csi_ostack_get_array (ctx, 0, &array);
  3112. if (_csi_unlikely (status))
  3113. return status;
  3114. status = csi_matrix_new_from_array (ctx, &matrix, array);
  3115. if (_csi_unlikely (status))
  3116. return status;
  3117. pop (1);
  3118. }
  3119. return push (&matrix);
  3120. }
  3121. static csi_status_t
  3122. _map_to_image (csi_t *ctx)
  3123. {
  3124. csi_object_t obj;
  3125. csi_array_t *array;
  3126. csi_status_t status;
  3127. cairo_rectangle_int_t extents, *r;
  3128. cairo_surface_t *surface;
  3129. check (2);
  3130. status = _csi_ostack_get_array (ctx, 0, &array);
  3131. if (_csi_unlikely (status))
  3132. return status;
  3133. status = _csi_ostack_get_surface (ctx, 1, &surface);
  3134. if (_csi_unlikely (status))
  3135. return status;
  3136. switch (array->stack.len) {
  3137. case 0:
  3138. r = NULL;
  3139. break;
  3140. case 4:
  3141. extents.x = floor (_csi_object_as_real (&array->stack.objects[0]));
  3142. extents.y = floor (_csi_object_as_real (&array->stack.objects[1]));
  3143. extents.width = ceil (_csi_object_as_real (&array->stack.objects[2]));
  3144. extents.height = ceil (_csi_object_as_real (&array->stack.objects[3]));
  3145. r = &extents;
  3146. break;
  3147. default:
  3148. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  3149. }
  3150. obj.type = CSI_OBJECT_TYPE_SURFACE;
  3151. obj.datum.surface = cairo_surface_reference (cairo_surface_map_to_image (surface, r));
  3152. pop (1);
  3153. return push (&obj);
  3154. }
  3155. static csi_status_t
  3156. _unmap_image (csi_t *ctx)
  3157. {
  3158. cairo_surface_t *surface, *image;
  3159. csi_status_t status;
  3160. check (2);
  3161. status = _csi_ostack_get_surface (ctx, 0, &image);
  3162. if (_csi_unlikely (status))
  3163. return status;
  3164. status = _csi_ostack_get_surface (ctx, 1, &surface);
  3165. if (_csi_unlikely (status))
  3166. return status;
  3167. cairo_surface_unmap_image (surface, image);
  3168. pop (1);
  3169. return CSI_STATUS_SUCCESS;
  3170. }
  3171. static csi_status_t
  3172. _mesh (csi_t *ctx)
  3173. {
  3174. csi_object_t obj;
  3175. obj.type = CSI_OBJECT_TYPE_PATTERN;
  3176. obj.datum.pattern = cairo_pattern_create_mesh ();
  3177. return push (&obj);
  3178. }
  3179. static csi_status_t
  3180. _mesh_begin_patch (csi_t *ctx)
  3181. {
  3182. csi_status_t status;
  3183. cairo_pattern_t *pattern = NULL; /* silence the compiler */
  3184. check (1);
  3185. status = _csi_ostack_get_pattern (ctx, 0, &pattern);
  3186. if (_csi_unlikely (status))
  3187. return status;
  3188. cairo_mesh_pattern_begin_patch (pattern);
  3189. return CSI_STATUS_SUCCESS;
  3190. }
  3191. static csi_status_t
  3192. _mesh_end_patch (csi_t *ctx)
  3193. {
  3194. csi_status_t status;
  3195. cairo_pattern_t *pattern = NULL; /* silence the compiler */
  3196. check (1);
  3197. status = _csi_ostack_get_pattern (ctx, 0, &pattern);
  3198. if (_csi_unlikely (status))
  3199. return status;
  3200. cairo_mesh_pattern_end_patch (pattern);
  3201. return CSI_STATUS_SUCCESS;
  3202. }
  3203. static csi_status_t
  3204. _mesh_set_control_point (csi_t *ctx)
  3205. {
  3206. csi_status_t status;
  3207. double x, y;
  3208. csi_integer_t point;
  3209. cairo_pattern_t *pattern = NULL; /* silence the compiler */
  3210. check (4);
  3211. status = _csi_ostack_get_number (ctx, 0, &y);
  3212. if (_csi_unlikely (status))
  3213. return status;
  3214. status = _csi_ostack_get_number (ctx, 1, &x);
  3215. if (_csi_unlikely (status))
  3216. return status;
  3217. status = _csi_ostack_get_integer (ctx, 2, &point);
  3218. if (_csi_unlikely (status))
  3219. return status;
  3220. status = _csi_ostack_get_pattern (ctx, 3, &pattern);
  3221. if (_csi_unlikely (status))
  3222. return status;
  3223. cairo_mesh_pattern_set_control_point (pattern, point, x, y);
  3224. pop (3);
  3225. return CSI_STATUS_SUCCESS;
  3226. }
  3227. static csi_status_t
  3228. _mesh_set_corner_color (csi_t *ctx)
  3229. {
  3230. csi_status_t status;
  3231. double r, g, b, a;
  3232. csi_integer_t corner;
  3233. cairo_pattern_t *pattern = NULL; /* silence the compiler */
  3234. check (6);
  3235. status = _csi_ostack_get_number (ctx, 0, &a);
  3236. if (_csi_unlikely (status))
  3237. return status;
  3238. status = _csi_ostack_get_number (ctx, 1, &b);
  3239. if (_csi_unlikely (status))
  3240. return status;
  3241. status = _csi_ostack_get_number (ctx, 2, &g);
  3242. if (_csi_unlikely (status))
  3243. return status;
  3244. status = _csi_ostack_get_number (ctx, 3, &r);
  3245. if (_csi_unlikely (status))
  3246. return status;
  3247. status = _csi_ostack_get_integer (ctx, 4, &corner);
  3248. if (_csi_unlikely (status))
  3249. return status;
  3250. status = _csi_ostack_get_pattern (ctx, 5, &pattern);
  3251. if (_csi_unlikely (status))
  3252. return status;
  3253. cairo_mesh_pattern_set_corner_color_rgba (pattern, corner, r, g, b, a);
  3254. pop (5);
  3255. return CSI_STATUS_SUCCESS;
  3256. }
  3257. static csi_status_t
  3258. _mod (csi_t *ctx)
  3259. {
  3260. csi_integer_t x, y;
  3261. csi_status_t status;
  3262. check (2);
  3263. status = _csi_ostack_get_integer (ctx, 0, &y);
  3264. if (_csi_unlikely (status))
  3265. return status;
  3266. status = _csi_ostack_get_integer (ctx, 1, &x);
  3267. if (_csi_unlikely (status))
  3268. return status;
  3269. pop (2);
  3270. return _csi_push_ostack_integer (ctx, x % y);
  3271. }
  3272. static csi_status_t
  3273. _move_to (csi_t *ctx)
  3274. {
  3275. csi_status_t status;
  3276. csi_object_t *obj;
  3277. int type;
  3278. double x, y;
  3279. check (3);
  3280. status = _csi_ostack_get_number (ctx, 0, &y);
  3281. if (_csi_unlikely (status))
  3282. return status;
  3283. status = _csi_ostack_get_number (ctx, 1, &x);
  3284. if (_csi_unlikely (status))
  3285. return status;
  3286. obj = _csi_peek_ostack (ctx, 2);
  3287. type = csi_object_get_type (obj);
  3288. switch (type) {
  3289. case CSI_OBJECT_TYPE_CONTEXT:
  3290. cairo_move_to (obj->datum.cr, x, y);
  3291. break;
  3292. case CSI_OBJECT_TYPE_PATTERN:
  3293. cairo_mesh_pattern_move_to (obj->datum.pattern, x, y);
  3294. break;
  3295. /* XXX path object */
  3296. }
  3297. pop (2);
  3298. return CSI_STATUS_SUCCESS;
  3299. }
  3300. static csi_status_t
  3301. _mul (csi_t *ctx)
  3302. {
  3303. csi_object_t *A;
  3304. csi_object_t *B;
  3305. csi_object_type_t type_a, type_b;
  3306. check (2);
  3307. B = _csi_peek_ostack (ctx, 0);
  3308. A = _csi_peek_ostack (ctx, 1);
  3309. type_a = csi_object_get_type (A);
  3310. if (_csi_unlikely (! (type_a == CSI_OBJECT_TYPE_INTEGER ||
  3311. type_a == CSI_OBJECT_TYPE_REAL)))
  3312. {
  3313. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  3314. }
  3315. type_b = csi_object_get_type (B);
  3316. if (_csi_unlikely (! (type_b == CSI_OBJECT_TYPE_INTEGER ||
  3317. type_b == CSI_OBJECT_TYPE_REAL)))
  3318. {
  3319. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  3320. }
  3321. pop (2);
  3322. if (type_a == CSI_OBJECT_TYPE_REAL &&
  3323. type_b == CSI_OBJECT_TYPE_REAL)
  3324. {
  3325. return _csi_push_ostack_real (ctx, A->datum.real * B->datum.real);
  3326. }
  3327. else if (type_a == CSI_OBJECT_TYPE_INTEGER &&
  3328. type_b == CSI_OBJECT_TYPE_INTEGER)
  3329. {
  3330. return _csi_push_ostack_integer (ctx,
  3331. A->datum.integer * B->datum.integer);
  3332. }
  3333. else
  3334. {
  3335. double v;
  3336. if (type_a == CSI_OBJECT_TYPE_REAL)
  3337. v = A->datum.real;
  3338. else
  3339. v = A->datum.integer;
  3340. if (type_b == CSI_OBJECT_TYPE_REAL)
  3341. v *= B->datum.real;
  3342. else
  3343. v *= B->datum.integer;
  3344. return _csi_push_ostack_real (ctx, v);
  3345. }
  3346. }
  3347. static csi_status_t
  3348. _or (csi_t *ctx)
  3349. {
  3350. csi_object_t *a, *b;
  3351. int type;
  3352. check (2);
  3353. a = _csi_peek_ostack (ctx, 0);
  3354. b = _csi_peek_ostack (ctx, 1);
  3355. if (_csi_unlikely (csi_object_get_type (a) != csi_object_get_type (b)))
  3356. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  3357. pop (2);
  3358. type = csi_object_get_type (a);
  3359. switch (type) {
  3360. case CSI_OBJECT_TYPE_INTEGER:
  3361. return _csi_push_ostack_integer (ctx,
  3362. a->datum.integer | b->datum.integer);
  3363. case CSI_OBJECT_TYPE_BOOLEAN:
  3364. return _csi_push_ostack_boolean (ctx,
  3365. a->datum.boolean | b->datum.boolean);
  3366. default:
  3367. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  3368. }
  3369. }
  3370. static csi_status_t
  3371. _paint (csi_t *ctx)
  3372. {
  3373. return _do_cairo_op (ctx, cairo_paint);
  3374. }
  3375. static csi_status_t
  3376. _paint_with_alpha (csi_t *ctx)
  3377. {
  3378. cairo_t *cr;
  3379. csi_status_t status;
  3380. double alpha;
  3381. check (2);
  3382. status = _csi_ostack_get_number (ctx, 0, &alpha);
  3383. if (_csi_unlikely (status))
  3384. return status;
  3385. status = _csi_ostack_get_context (ctx, 1, &cr);
  3386. if (_csi_unlikely (status))
  3387. return status;
  3388. cairo_paint_with_alpha (cr, alpha);
  3389. pop (1);
  3390. return CSI_STATUS_SUCCESS;
  3391. }
  3392. static csi_status_t
  3393. _pattern (csi_t *ctx)
  3394. {
  3395. csi_object_t obj;
  3396. csi_status_t status;
  3397. cairo_surface_t *surface;
  3398. check (1);
  3399. status = _csi_ostack_get_surface (ctx, 0, &surface);
  3400. if (_csi_unlikely (status))
  3401. return status;
  3402. obj.type = CSI_OBJECT_TYPE_PATTERN;
  3403. obj.datum.pattern = cairo_pattern_create_for_surface (surface);
  3404. pop (1);
  3405. return push (&obj);
  3406. }
  3407. static csi_status_t
  3408. _pop (csi_t *ctx)
  3409. {
  3410. check (1);
  3411. pop (1);
  3412. return CSI_STATUS_SUCCESS;
  3413. }
  3414. static csi_status_t
  3415. _pop_group (csi_t *ctx)
  3416. {
  3417. csi_object_t obj;
  3418. csi_status_t status;
  3419. cairo_t *cr;
  3420. check (1);
  3421. status = _csi_ostack_get_context (ctx, 0, &cr);
  3422. if (_csi_unlikely (status))
  3423. return status;
  3424. obj.type = CSI_OBJECT_TYPE_PATTERN;
  3425. obj.datum.pattern = cairo_pop_group (cr);
  3426. return push (&obj);
  3427. }
  3428. static csi_status_t
  3429. _push_group (csi_t *ctx)
  3430. {
  3431. csi_status_t status;
  3432. cairo_t *cr;
  3433. long content;
  3434. check (2);
  3435. status = _csi_ostack_get_integer (ctx, 0, &content);
  3436. if (_csi_unlikely (status))
  3437. return status;
  3438. status = _csi_ostack_get_context (ctx, 1, &cr);
  3439. if (_csi_unlikely (status))
  3440. return status;
  3441. cairo_push_group_with_content (cr, content);
  3442. pop (1);
  3443. return CSI_STATUS_SUCCESS;
  3444. }
  3445. static csi_status_t
  3446. _radial (csi_t *ctx)
  3447. {
  3448. csi_object_t obj;
  3449. csi_status_t status;
  3450. double x1, y1, r1, x2, y2, r2;
  3451. check (6);
  3452. status = _csi_ostack_get_number (ctx, 0, &r2);
  3453. if (_csi_unlikely (status))
  3454. return status;
  3455. status = _csi_ostack_get_number (ctx, 1, &y2);
  3456. if (_csi_unlikely (status))
  3457. return status;
  3458. status = _csi_ostack_get_number (ctx, 2, &x2);
  3459. if (_csi_unlikely (status))
  3460. return status;
  3461. status = _csi_ostack_get_number (ctx, 3, &r1);
  3462. if (_csi_unlikely (status))
  3463. return status;
  3464. status = _csi_ostack_get_number (ctx, 4, &y1);
  3465. if (_csi_unlikely (status))
  3466. return status;
  3467. status = _csi_ostack_get_number (ctx, 5, &x1);
  3468. if (_csi_unlikely (status))
  3469. return status;
  3470. obj.type = CSI_OBJECT_TYPE_PATTERN;
  3471. obj.datum.pattern = cairo_pattern_create_radial (x1, y1, r1, x2, y2, r2);
  3472. pop (6);
  3473. return push (&obj);
  3474. }
  3475. static csi_status_t
  3476. _rectangle (csi_t *ctx)
  3477. {
  3478. csi_status_t status;
  3479. double x, y;
  3480. double w, h;
  3481. cairo_t *cr;
  3482. check (5);
  3483. status = _csi_ostack_get_number (ctx, 0, &h);
  3484. if (_csi_unlikely (status))
  3485. return status;
  3486. status = _csi_ostack_get_number (ctx, 1, &w);
  3487. if (_csi_unlikely (status))
  3488. return status;
  3489. status = _csi_ostack_get_number (ctx, 2, &y);
  3490. if (_csi_unlikely (status))
  3491. return status;
  3492. status = _csi_ostack_get_number (ctx, 3, &x);
  3493. if (_csi_unlikely (status))
  3494. return status;
  3495. status = _csi_ostack_get_context (ctx, 4, &cr);
  3496. if (_csi_unlikely (status))
  3497. return status;
  3498. /* XXX path object */
  3499. cairo_rectangle (cr, x, y, w, h);
  3500. pop(4);
  3501. return CSI_STATUS_SUCCESS;
  3502. }
  3503. static csi_status_t
  3504. _rel_curve_to (csi_t *ctx)
  3505. {
  3506. csi_status_t status;
  3507. double x1, y1;
  3508. double x2, y2;
  3509. double x3, y3;
  3510. cairo_t *cr;
  3511. check (7);
  3512. status = _csi_ostack_get_number (ctx, 0, &y3);
  3513. if (_csi_unlikely (status))
  3514. return status;
  3515. status = _csi_ostack_get_number (ctx, 1, &x3);
  3516. if (_csi_unlikely (status))
  3517. return status;
  3518. status = _csi_ostack_get_number (ctx, 2, &y2);
  3519. if (_csi_unlikely (status))
  3520. return status;
  3521. status = _csi_ostack_get_number (ctx, 3, &x2);
  3522. if (_csi_unlikely (status))
  3523. return status;
  3524. status = _csi_ostack_get_number (ctx, 4, &y1);
  3525. if (_csi_unlikely (status))
  3526. return status;
  3527. status = _csi_ostack_get_number (ctx, 5, &x1);
  3528. if (_csi_unlikely (status))
  3529. return status;
  3530. status = _csi_ostack_get_context (ctx, 6, &cr);
  3531. if (_csi_unlikely (status))
  3532. return status;
  3533. /* XXX path object */
  3534. cairo_rel_curve_to (cr, x1, y1, x2, y2, x3, y3);
  3535. pop (6);
  3536. return CSI_STATUS_SUCCESS;
  3537. }
  3538. static csi_status_t
  3539. _rel_line_to (csi_t *ctx)
  3540. {
  3541. csi_status_t status;
  3542. double x, y;
  3543. cairo_t *cr;
  3544. check (3);
  3545. status = _csi_ostack_get_number (ctx, 0, &y);
  3546. if (_csi_unlikely (status))
  3547. return status;
  3548. status = _csi_ostack_get_number (ctx, 1, &x);
  3549. if (_csi_unlikely (status))
  3550. return status;
  3551. status = _csi_ostack_get_context (ctx, 2, &cr);
  3552. if (_csi_unlikely (status))
  3553. return status;
  3554. /* XXX path object */
  3555. cairo_rel_line_to (cr, x, y);
  3556. pop (2);
  3557. return CSI_STATUS_SUCCESS;
  3558. }
  3559. static csi_status_t
  3560. _rel_move_to (csi_t *ctx)
  3561. {
  3562. csi_status_t status;
  3563. double x, y;
  3564. cairo_t *cr;
  3565. check (3);
  3566. status = _csi_ostack_get_number (ctx, 0, &y);
  3567. if (_csi_unlikely (status))
  3568. return status;
  3569. status = _csi_ostack_get_number (ctx, 1, &x);
  3570. if (_csi_unlikely (status))
  3571. return status;
  3572. status = _csi_ostack_get_context (ctx, 2, &cr);
  3573. if (_csi_unlikely (status))
  3574. return status;
  3575. /* XXX path object */
  3576. cairo_rel_move_to (cr, x, y);
  3577. pop (2);
  3578. return CSI_STATUS_SUCCESS;
  3579. }
  3580. static csi_status_t
  3581. _repeat (csi_t *ctx)
  3582. {
  3583. csi_array_t *proc;
  3584. csi_integer_t count;
  3585. csi_status_t status;
  3586. check (2);
  3587. status = _csi_ostack_get_procedure (ctx, 0, &proc);
  3588. if (_csi_unlikely (status))
  3589. return status;
  3590. status = _csi_ostack_get_integer (ctx, 1, &count);
  3591. if (_csi_unlikely (status))
  3592. return status;
  3593. if (_csi_unlikely (count < 0))
  3594. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  3595. proc->base.ref++;
  3596. pop (2);
  3597. while (count--) {
  3598. status = _csi_array_execute (ctx, proc);
  3599. if (_csi_unlikely (status))
  3600. break;
  3601. }
  3602. if (--proc->base.ref == 0)
  3603. csi_array_free (ctx, proc);
  3604. return status;
  3605. }
  3606. static csi_status_t
  3607. _reset_clip (csi_t *ctx)
  3608. {
  3609. return _do_cairo_op (ctx, cairo_reset_clip);
  3610. }
  3611. static csi_status_t
  3612. _restore (csi_t *ctx)
  3613. {
  3614. return _do_cairo_op (ctx, cairo_restore);
  3615. }
  3616. static csi_status_t
  3617. _rgb (csi_t *ctx)
  3618. {
  3619. csi_object_t obj;
  3620. csi_status_t status;
  3621. double r,g,b;
  3622. check (3);
  3623. status = _csi_ostack_get_number (ctx, 0, &b);
  3624. if (_csi_unlikely (status))
  3625. return status;
  3626. status = _csi_ostack_get_number (ctx, 1, &g);
  3627. if (_csi_unlikely (status))
  3628. return status;
  3629. status = _csi_ostack_get_number (ctx, 2, &r);
  3630. if (_csi_unlikely (status))
  3631. return status;
  3632. obj.type = CSI_OBJECT_TYPE_PATTERN;
  3633. obj.datum.pattern = cairo_pattern_create_rgb (r, g, b);
  3634. pop (3);
  3635. return push (&obj);
  3636. }
  3637. static csi_status_t
  3638. _rgba (csi_t *ctx)
  3639. {
  3640. csi_object_t obj;
  3641. csi_status_t status;
  3642. double r,g,b,a;
  3643. check (4);
  3644. status = _csi_ostack_get_number (ctx, 0, &a);
  3645. if (_csi_unlikely (status))
  3646. return status;
  3647. status = _csi_ostack_get_number (ctx, 1, &b);
  3648. if (_csi_unlikely (status))
  3649. return status;
  3650. status = _csi_ostack_get_number (ctx, 2, &g);
  3651. if (_csi_unlikely (status))
  3652. return status;
  3653. status = _csi_ostack_get_number (ctx, 3, &r);
  3654. if (_csi_unlikely (status))
  3655. return status;
  3656. obj.type = CSI_OBJECT_TYPE_PATTERN;
  3657. obj.datum.pattern = cairo_pattern_create_rgba (r, g, b, a);
  3658. pop (4);
  3659. return push (&obj);
  3660. }
  3661. static csi_status_t
  3662. _roll (csi_t *ctx)
  3663. {
  3664. csi_status_t status;
  3665. long j, n;
  3666. check (2);
  3667. status = _csi_ostack_get_integer (ctx, 0, &j);
  3668. if (_csi_unlikely (status))
  3669. return status;
  3670. status = _csi_ostack_get_integer (ctx, 1, &n);
  3671. if (_csi_unlikely (status))
  3672. return status;
  3673. pop (2);
  3674. check (n);
  3675. return _csi_stack_roll (ctx, &ctx->ostack, j, n);
  3676. }
  3677. static csi_status_t
  3678. _rotate (csi_t *ctx)
  3679. {
  3680. csi_object_t *obj;
  3681. csi_status_t status;
  3682. double theta;
  3683. int type;
  3684. check (2);
  3685. status = _csi_ostack_get_number (ctx, 0, &theta);
  3686. if (_csi_unlikely (status))
  3687. return status;
  3688. obj = _csi_peek_ostack (ctx, 1);
  3689. type = csi_object_get_type (obj);
  3690. switch (type) {
  3691. case CSI_OBJECT_TYPE_CONTEXT:
  3692. cairo_rotate (obj->datum.cr, theta);
  3693. break;
  3694. case CSI_OBJECT_TYPE_PATTERN:
  3695. {
  3696. cairo_matrix_t ctm;
  3697. cairo_pattern_get_matrix (obj->datum.pattern, &ctm);
  3698. cairo_matrix_rotate (&ctm, theta);
  3699. cairo_pattern_set_matrix (obj->datum.pattern, &ctm);
  3700. }
  3701. break;
  3702. case CSI_OBJECT_TYPE_MATRIX:
  3703. cairo_matrix_rotate (&obj->datum.matrix->matrix, theta);
  3704. break;
  3705. default:
  3706. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  3707. }
  3708. pop (1);
  3709. return CSI_STATUS_SUCCESS;
  3710. }
  3711. static csi_status_t
  3712. _save (csi_t *ctx)
  3713. {
  3714. return _do_cairo_op (ctx, cairo_save);
  3715. }
  3716. static csi_status_t
  3717. _scale (csi_t *ctx)
  3718. {
  3719. csi_object_t *obj;
  3720. csi_status_t status;
  3721. double x, y;
  3722. int type;
  3723. check (3);
  3724. status = _csi_ostack_get_number (ctx, 0, &y);
  3725. if (_csi_unlikely (status))
  3726. return status;
  3727. status = _csi_ostack_get_number (ctx, 1, &x);
  3728. if (_csi_unlikely (status))
  3729. return status;
  3730. obj = _csi_peek_ostack (ctx, 2);
  3731. type = csi_object_get_type (obj);
  3732. switch (type) {
  3733. case CSI_OBJECT_TYPE_CONTEXT:
  3734. cairo_scale (obj->datum.cr, x, y);
  3735. break;
  3736. case CSI_OBJECT_TYPE_PATTERN:
  3737. {
  3738. cairo_matrix_t ctm;
  3739. cairo_pattern_get_matrix (obj->datum.pattern, &ctm);
  3740. cairo_matrix_scale (&ctm, x, y);
  3741. cairo_pattern_set_matrix (obj->datum.pattern, &ctm);
  3742. }
  3743. break;
  3744. case CSI_OBJECT_TYPE_MATRIX:
  3745. cairo_matrix_scale (&obj->datum.matrix->matrix, x, y);
  3746. break;
  3747. default:
  3748. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  3749. }
  3750. pop (2);
  3751. return CSI_STATUS_SUCCESS;
  3752. }
  3753. static csi_status_t
  3754. _font_options_load_from_dictionary (csi_t *ctx,
  3755. csi_dictionary_t *dict,
  3756. cairo_font_options_t *options)
  3757. {
  3758. const struct {
  3759. const char *key;
  3760. void (*setter) (cairo_font_options_t *, int val);
  3761. } properties[] = {
  3762. { "antialias",
  3763. (void (*)(cairo_font_options_t *, int val))
  3764. cairo_font_options_set_antialias },
  3765. { "subpixel-order",
  3766. (void (*)(cairo_font_options_t *, int val))
  3767. cairo_font_options_set_subpixel_order },
  3768. { "hint-style",
  3769. (void (*)(cairo_font_options_t *, int val))
  3770. cairo_font_options_set_hint_style },
  3771. { "hint-metrics",
  3772. (void (*)(cairo_font_options_t *, int val))
  3773. cairo_font_options_set_hint_metrics },
  3774. { NULL, NULL },
  3775. }, *prop = properties;
  3776. while (prop->key != NULL) {
  3777. csi_object_t key, value;
  3778. csi_status_t status;
  3779. status = csi_name_new_static (ctx, &key, prop->key);
  3780. if (_csi_unlikely (status))
  3781. return status;
  3782. if (csi_dictionary_has (dict, key.datum.name)) {
  3783. status = csi_dictionary_get (ctx, dict, key.datum.name, &value);
  3784. if (_csi_unlikely (status))
  3785. return status;
  3786. if (_csi_unlikely (csi_object_get_type (&value) !=
  3787. CSI_OBJECT_TYPE_INTEGER))
  3788. {
  3789. csi_object_free (ctx, &value);
  3790. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  3791. }
  3792. prop->setter (options, value.datum.integer);
  3793. }
  3794. prop++;
  3795. }
  3796. return CSI_STATUS_SUCCESS;
  3797. }
  3798. static csi_status_t
  3799. _scaled_font (csi_t *ctx)
  3800. {
  3801. csi_object_t obj;
  3802. csi_dictionary_t *dict;
  3803. cairo_font_face_t *font_face = NULL; /* silence the compiler */
  3804. cairo_matrix_t font_matrix, ctm;
  3805. cairo_font_options_t *options;
  3806. csi_status_t status;
  3807. check (4);
  3808. status = _csi_ostack_get_dictionary (ctx, 0, &dict);
  3809. if (_csi_unlikely (status))
  3810. return status;
  3811. options = cairo_font_options_create ();
  3812. status = _font_options_load_from_dictionary (ctx, dict, options);
  3813. if (_csi_unlikely (status)) {
  3814. cairo_font_options_destroy (options);
  3815. return status;
  3816. }
  3817. status = _csi_ostack_get_matrix (ctx, 1, &ctm);
  3818. if (_csi_unlikely (status)) {
  3819. cairo_font_options_destroy (options);
  3820. return status;
  3821. }
  3822. status = _csi_ostack_get_matrix (ctx, 2, &font_matrix);
  3823. if (_csi_unlikely (status)) {
  3824. cairo_font_options_destroy (options);
  3825. return status;
  3826. }
  3827. status = _csi_ostack_get_font_face (ctx, 3, &font_face);
  3828. if (_csi_unlikely (status)) {
  3829. cairo_font_options_destroy (options);
  3830. return status;
  3831. }
  3832. obj.type = CSI_OBJECT_TYPE_SCALED_FONT;
  3833. obj.datum.scaled_font = cairo_scaled_font_create (font_face,
  3834. &font_matrix,
  3835. &ctm,
  3836. options);
  3837. cairo_font_options_destroy (options);
  3838. pop (4);
  3839. return push (&obj);
  3840. }
  3841. static csi_status_t
  3842. _select_font_face (csi_t *ctx)
  3843. {
  3844. cairo_t *cr;
  3845. long weight;
  3846. long slant;
  3847. csi_string_t *family;
  3848. csi_status_t status;
  3849. check (4);
  3850. status = _csi_ostack_get_integer (ctx, 0, &weight);
  3851. if (_csi_unlikely (status))
  3852. return status;
  3853. status = _csi_ostack_get_integer (ctx, 1, &slant);
  3854. if (_csi_unlikely (status))
  3855. return status;
  3856. status = _csi_ostack_get_string (ctx, 2, &family);
  3857. if (_csi_unlikely (status))
  3858. return status;
  3859. status = _csi_ostack_get_context (ctx, 3, &cr);
  3860. if (_csi_unlikely (status))
  3861. return status;
  3862. cairo_select_font_face (cr, family->string, slant, weight);
  3863. pop (3);
  3864. return CSI_STATUS_SUCCESS;
  3865. }
  3866. static csi_status_t
  3867. _context_set (csi_t *ctx,
  3868. cairo_t *cr,
  3869. csi_name_t key,
  3870. csi_object_t *obj)
  3871. {
  3872. if (strcmp ((char *) key, "source") == 0) {
  3873. if (_csi_unlikely (csi_object_get_type (obj) !=
  3874. CSI_OBJECT_TYPE_PATTERN))
  3875. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  3876. cairo_set_source (cr, obj->datum.pattern);
  3877. return CSI_STATUS_SUCCESS;
  3878. }
  3879. if (strcmp ((char *) key, "scaled-font") == 0) {
  3880. if (_csi_unlikely (csi_object_get_type (obj) !=
  3881. CSI_OBJECT_TYPE_SCALED_FONT))
  3882. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  3883. cairo_set_scaled_font (cr, obj->datum.scaled_font);
  3884. return CSI_STATUS_SUCCESS;
  3885. }
  3886. if (strcmp ((char *) key, "font-face") == 0) {
  3887. if (_csi_unlikely (csi_object_get_type (obj) !=
  3888. CSI_OBJECT_TYPE_FONT))
  3889. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  3890. cairo_set_font_face (cr, obj->datum.font_face);
  3891. return CSI_STATUS_SUCCESS;
  3892. }
  3893. /* return _proxy_set()? */
  3894. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  3895. }
  3896. static csi_status_t
  3897. _set (csi_t *ctx)
  3898. {
  3899. csi_object_t *key, *value, *dst;
  3900. csi_status_t status;
  3901. int type;
  3902. check (3);
  3903. value = _csi_peek_ostack (ctx, 0);
  3904. key = _csi_peek_ostack (ctx, 1);
  3905. dst = _csi_peek_ostack (ctx, 2);
  3906. type = csi_object_get_type (dst);
  3907. switch (type) {
  3908. case CSI_OBJECT_TYPE_DICTIONARY:
  3909. if (_csi_unlikely (csi_object_get_type (key) !=
  3910. CSI_OBJECT_TYPE_NAME))
  3911. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  3912. status = csi_dictionary_put (ctx,
  3913. dst->datum.dictionary,
  3914. key->datum.name,
  3915. value);
  3916. break;
  3917. case CSI_OBJECT_TYPE_ARRAY:
  3918. if (_csi_unlikely (csi_object_get_type (key) !=
  3919. CSI_OBJECT_TYPE_INTEGER))
  3920. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  3921. status = csi_array_put (ctx,
  3922. dst->datum.array,
  3923. key->datum.integer,
  3924. value);
  3925. break;
  3926. case CSI_OBJECT_TYPE_CONTEXT:
  3927. if (_csi_unlikely (csi_object_get_type (key) !=
  3928. CSI_OBJECT_TYPE_NAME))
  3929. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  3930. status = _context_set (ctx,
  3931. dst->datum.cr,
  3932. key->datum.name,
  3933. value);
  3934. break;
  3935. case CSI_OBJECT_TYPE_STRING:
  3936. #if 0
  3937. status = csi_string_put (dst, key, value);
  3938. break;
  3939. #endif
  3940. default:
  3941. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  3942. }
  3943. pop (2);
  3944. return status;
  3945. }
  3946. static csi_status_t
  3947. _set_antialias (csi_t *ctx)
  3948. {
  3949. csi_status_t status;
  3950. cairo_t *cr;
  3951. long antialias;
  3952. check (2);
  3953. status = _csi_ostack_get_integer (ctx, 0, &antialias);
  3954. if (_csi_unlikely (status))
  3955. return status;
  3956. status = _csi_ostack_get_context (ctx, 1, &cr);
  3957. if (_csi_unlikely (status))
  3958. return status;
  3959. cairo_set_antialias (cr, antialias);
  3960. pop (1);
  3961. return CSI_STATUS_SUCCESS;
  3962. }
  3963. static csi_status_t
  3964. _set_dash (csi_t *ctx)
  3965. {
  3966. csi_array_t *array;
  3967. csi_status_t status;
  3968. cairo_t *cr;
  3969. double offset;
  3970. check (3);
  3971. status = _csi_ostack_get_number (ctx, 0, &offset);
  3972. if (_csi_unlikely (status))
  3973. return status;
  3974. status = _csi_ostack_get_array (ctx, 1, &array);
  3975. if (_csi_unlikely (status))
  3976. return status;
  3977. status = _csi_ostack_get_context (ctx, 2, &cr);
  3978. if (_csi_unlikely (status))
  3979. return status;
  3980. if (array->stack.len == 0) {
  3981. cairo_set_dash (cr, NULL, 0., 0.);
  3982. } else {
  3983. double stack_dashes[8];
  3984. double *dashes;
  3985. csi_integer_t n;
  3986. if (_csi_likely (array->stack.len < ARRAY_LENGTH (stack_dashes))) {
  3987. dashes = stack_dashes;
  3988. } else {
  3989. if (_csi_unlikely ((unsigned) array->stack.len >= INT_MAX / sizeof (double)))
  3990. return _csi_error (CSI_STATUS_NO_MEMORY);
  3991. dashes = _csi_alloc (ctx, sizeof (double) * array->stack.len);
  3992. if (_csi_unlikely (dashes == NULL))
  3993. return _csi_error (CSI_STATUS_NO_MEMORY);
  3994. }
  3995. for (n = 0; n < array->stack.len; n++) {
  3996. if (_csi_unlikely (! csi_object_is_number
  3997. (&array->stack.objects[n])))
  3998. {
  3999. if (dashes != stack_dashes)
  4000. _csi_free (ctx, dashes);
  4001. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  4002. }
  4003. dashes[n] = csi_number_get_value (&array->stack.objects[n]);
  4004. }
  4005. cairo_set_dash (cr, dashes, n, offset);
  4006. if (dashes != stack_dashes)
  4007. _csi_free (ctx, dashes);
  4008. }
  4009. pop (2);
  4010. return CSI_STATUS_SUCCESS;
  4011. }
  4012. static csi_status_t
  4013. _set_device_offset (csi_t *ctx)
  4014. {
  4015. csi_status_t status;
  4016. cairo_surface_t *surface;
  4017. double x, y;
  4018. check (3);
  4019. status = _csi_ostack_get_number (ctx, 0, &y);
  4020. if (_csi_unlikely (status))
  4021. return status;
  4022. status = _csi_ostack_get_number (ctx, 1, &x);
  4023. if (_csi_unlikely (status))
  4024. return status;
  4025. status = _csi_ostack_get_surface (ctx, 2, &surface);
  4026. if (_csi_unlikely (status))
  4027. return status;
  4028. cairo_surface_set_device_offset (surface, x, y);
  4029. pop (2);
  4030. return CSI_STATUS_SUCCESS;
  4031. }
  4032. static csi_status_t
  4033. _set_device_scale (csi_t *ctx)
  4034. {
  4035. csi_status_t status;
  4036. cairo_surface_t *surface;
  4037. double x, y;
  4038. check (3);
  4039. status = _csi_ostack_get_number (ctx, 0, &y);
  4040. if (_csi_unlikely (status))
  4041. return status;
  4042. status = _csi_ostack_get_number (ctx, 1, &x);
  4043. if (_csi_unlikely (status))
  4044. return status;
  4045. status = _csi_ostack_get_surface (ctx, 2, &surface);
  4046. if (_csi_unlikely (status))
  4047. return status;
  4048. cairo_surface_set_device_scale (surface, x, y);
  4049. pop (2);
  4050. return CSI_STATUS_SUCCESS;
  4051. }
  4052. static csi_status_t
  4053. _set_extend (csi_t *ctx)
  4054. {
  4055. csi_status_t status;
  4056. csi_object_t *obj;
  4057. long extend;
  4058. int type;
  4059. check (2);
  4060. status = _csi_ostack_get_integer (ctx, 0, &extend);
  4061. if (_csi_unlikely (status))
  4062. return status;
  4063. obj = _csi_peek_ostack (ctx, 1);
  4064. type = csi_object_get_type (obj);
  4065. switch (type) {
  4066. case CSI_OBJECT_TYPE_CONTEXT:
  4067. cairo_pattern_set_extend (cairo_get_source (obj->datum.cr),
  4068. extend);
  4069. break;
  4070. case CSI_OBJECT_TYPE_PATTERN:
  4071. cairo_pattern_set_extend (obj->datum.pattern, extend);
  4072. break;
  4073. default:
  4074. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  4075. }
  4076. pop (1);
  4077. return CSI_STATUS_SUCCESS;
  4078. }
  4079. static csi_status_t
  4080. _set_fallback_resolution (csi_t *ctx)
  4081. {
  4082. csi_status_t status;
  4083. cairo_surface_t *surface;
  4084. double dpi_x, dpi_y;
  4085. check (3);
  4086. status = _csi_ostack_get_number (ctx, 0, &dpi_y);
  4087. if (_csi_unlikely (status))
  4088. return status;
  4089. status = _csi_ostack_get_number (ctx, 1, &dpi_x);
  4090. if (_csi_unlikely (status))
  4091. return status;
  4092. status = _csi_ostack_get_surface (ctx, 2, &surface);
  4093. if (_csi_unlikely (status))
  4094. return status;
  4095. cairo_surface_set_fallback_resolution (surface, dpi_x, dpi_y);
  4096. pop (2);
  4097. return CSI_STATUS_SUCCESS;
  4098. }
  4099. static csi_status_t
  4100. _set_fill_rule (csi_t *ctx)
  4101. {
  4102. csi_status_t status;
  4103. cairo_t *cr;
  4104. long fill_rule;
  4105. check (2);
  4106. status = _csi_ostack_get_integer (ctx, 0, &fill_rule);
  4107. if (_csi_unlikely (status))
  4108. return status;
  4109. status = _csi_ostack_get_context (ctx, 1, &cr);
  4110. if (_csi_unlikely (status))
  4111. return status;
  4112. cairo_set_fill_rule (cr, fill_rule);
  4113. pop (1);
  4114. return CSI_STATUS_SUCCESS;
  4115. }
  4116. static csi_status_t
  4117. _set_filter (csi_t *ctx)
  4118. {
  4119. csi_status_t status;
  4120. csi_object_t *obj;
  4121. long filter;
  4122. int type;
  4123. check (2);
  4124. status = _csi_ostack_get_integer (ctx, 0, &filter);
  4125. if (_csi_unlikely (status))
  4126. return status;
  4127. obj = _csi_peek_ostack (ctx, 1);
  4128. type = csi_object_get_type (obj);
  4129. switch (type) {
  4130. case CSI_OBJECT_TYPE_CONTEXT:
  4131. cairo_pattern_set_filter (cairo_get_source (obj->datum.cr),
  4132. filter);
  4133. break;
  4134. case CSI_OBJECT_TYPE_PATTERN:
  4135. cairo_pattern_set_filter (obj->datum.pattern, filter);
  4136. break;
  4137. default:
  4138. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  4139. }
  4140. pop (1);
  4141. return CSI_STATUS_SUCCESS;
  4142. }
  4143. static csi_status_t
  4144. _set_font_face (csi_t *ctx)
  4145. {
  4146. cairo_t *cr;
  4147. cairo_font_face_t *font = NULL; /* silence the compiler */
  4148. csi_status_t status;
  4149. check (2);
  4150. status = _csi_ostack_get_font_face (ctx, 0, &font);
  4151. if (_csi_unlikely (status))
  4152. return status;
  4153. status = _csi_ostack_get_context (ctx, 1, &cr);
  4154. if (_csi_unlikely (status))
  4155. return status;
  4156. cairo_set_font_face (cr, font);
  4157. pop (1);
  4158. return CSI_STATUS_SUCCESS;
  4159. }
  4160. static csi_status_t
  4161. _set_font_options (csi_t *ctx)
  4162. {
  4163. csi_status_t status;
  4164. cairo_t *cr;
  4165. csi_dictionary_t *dict;
  4166. cairo_font_options_t *options;
  4167. check (2);
  4168. status = _csi_ostack_get_dictionary (ctx, 0, &dict);
  4169. if (_csi_unlikely (status))
  4170. return status;
  4171. status = _csi_ostack_get_context (ctx, 1, &cr);
  4172. if (_csi_unlikely (status))
  4173. return status;
  4174. options = cairo_font_options_create ();
  4175. status = _font_options_load_from_dictionary (ctx, dict, options);
  4176. if (_csi_unlikely (status))
  4177. return status;
  4178. cairo_set_font_options (cr, options);
  4179. cairo_font_options_destroy (options);
  4180. pop (1);
  4181. return CSI_STATUS_SUCCESS;
  4182. }
  4183. static csi_status_t
  4184. _set_font_matrix (csi_t *ctx)
  4185. {
  4186. csi_status_t status;
  4187. cairo_t *cr;
  4188. cairo_matrix_t m;
  4189. check (2);
  4190. status = _csi_ostack_get_matrix (ctx, 0, &m);
  4191. if (_csi_unlikely (status))
  4192. return status;
  4193. status = _csi_ostack_get_context (ctx, 1, &cr);
  4194. if (_csi_unlikely (status))
  4195. return status;
  4196. cairo_set_font_matrix (cr, &m);
  4197. pop(1);
  4198. return CSI_STATUS_SUCCESS;
  4199. }
  4200. static csi_status_t
  4201. _set_font_size (csi_t *ctx)
  4202. {
  4203. csi_status_t status;
  4204. cairo_t *cr;
  4205. double size;
  4206. check (2);
  4207. status = _csi_ostack_get_number (ctx, 0, &size);
  4208. if (_csi_unlikely (status))
  4209. return status;
  4210. status = _csi_ostack_get_context (ctx, 1, &cr);
  4211. if (_csi_unlikely (status))
  4212. return status;
  4213. cairo_set_font_size (cr, size);
  4214. pop (1);
  4215. return CSI_STATUS_SUCCESS;
  4216. }
  4217. static csi_status_t
  4218. _set_line_cap (csi_t *ctx)
  4219. {
  4220. csi_status_t status;
  4221. cairo_t *cr;
  4222. long line_cap;
  4223. check (2);
  4224. status = _csi_ostack_get_integer (ctx, 0, &line_cap);
  4225. if (_csi_unlikely (status))
  4226. return status;
  4227. status = _csi_ostack_get_context (ctx, 1, &cr);
  4228. if (_csi_unlikely (status))
  4229. return status;
  4230. cairo_set_line_cap (cr, line_cap);
  4231. pop (1);
  4232. return CSI_STATUS_SUCCESS;
  4233. }
  4234. static csi_status_t
  4235. _set_line_join (csi_t *ctx)
  4236. {
  4237. csi_status_t status;
  4238. cairo_t *cr;
  4239. long line_join;
  4240. status = _csi_ostack_get_integer (ctx, 0, &line_join);
  4241. if (_csi_unlikely (status))
  4242. return status;
  4243. status = _csi_ostack_get_context (ctx, 1, &cr);
  4244. if (_csi_unlikely (status))
  4245. return status;
  4246. cairo_set_line_join (cr, line_join);
  4247. pop (1);
  4248. return CSI_STATUS_SUCCESS;
  4249. }
  4250. static csi_status_t
  4251. _set_line_width (csi_t *ctx)
  4252. {
  4253. csi_status_t status;
  4254. cairo_t *cr;
  4255. double line_width;
  4256. check (2);
  4257. status = _csi_ostack_get_number (ctx, 0, &line_width);
  4258. if (_csi_unlikely (status))
  4259. return status;
  4260. status = _csi_ostack_get_context (ctx, 1, &cr);
  4261. if (_csi_unlikely (status))
  4262. return status;
  4263. cairo_set_line_width (cr, line_width);
  4264. pop (1);
  4265. return CSI_STATUS_SUCCESS;
  4266. }
  4267. static csi_status_t
  4268. _set_matrix (csi_t *ctx)
  4269. {
  4270. csi_object_t *obj;
  4271. csi_status_t status;
  4272. cairo_matrix_t m;
  4273. int type;
  4274. check (2);
  4275. status = _csi_ostack_get_matrix (ctx, 0, &m);
  4276. if (_csi_unlikely (status))
  4277. return status;
  4278. obj = _csi_peek_ostack (ctx, 1);
  4279. type = csi_object_get_type (obj);
  4280. switch (type) {
  4281. case CSI_OBJECT_TYPE_CONTEXT:
  4282. cairo_set_matrix (obj->datum.cr, &m);
  4283. break;
  4284. case CSI_OBJECT_TYPE_PATTERN:
  4285. cairo_pattern_set_matrix (obj->datum.pattern, &m);
  4286. break;
  4287. case CSI_OBJECT_TYPE_MATRIX:
  4288. obj->datum.matrix->matrix = m;
  4289. break;
  4290. default:
  4291. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  4292. }
  4293. pop (1);
  4294. return CSI_STATUS_SUCCESS;
  4295. }
  4296. struct _mime_tag {
  4297. csi_t *ctx;
  4298. csi_string_t *source;
  4299. };
  4300. static void
  4301. _mime_tag_destroy (void *closure)
  4302. {
  4303. struct _mime_tag *tag = closure;
  4304. if (--tag->source->base.ref)
  4305. csi_string_free (tag->ctx, tag->source);
  4306. _csi_slab_free (tag->ctx, tag, sizeof (struct _mime_tag));
  4307. }
  4308. static csi_status_t
  4309. _set_mime_data (csi_t *ctx)
  4310. {
  4311. csi_status_t status;
  4312. csi_object_t *obj;
  4313. const char *mime = NULL; /* silence the compiler */
  4314. csi_object_t source;
  4315. cairo_surface_t *surface;
  4316. struct _mime_tag *tag;
  4317. int type;
  4318. check (3);
  4319. obj = _csi_peek_ostack (ctx, 0);
  4320. type = csi_object_get_type (obj);
  4321. switch (type) {
  4322. case CSI_OBJECT_TYPE_FILE:
  4323. status = _csi_file_as_string (ctx, obj->datum.file, &source);
  4324. if (_csi_unlikely (status))
  4325. return status;
  4326. break;
  4327. case CSI_OBJECT_TYPE_STRING:
  4328. source = *csi_object_reference (obj);
  4329. break;
  4330. default:
  4331. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  4332. }
  4333. status = _csi_ostack_get_string_constant (ctx, 1, &mime);
  4334. if (_csi_unlikely (status))
  4335. return status;
  4336. status = _csi_ostack_get_surface (ctx, 2, &surface);
  4337. if (_csi_unlikely (status))
  4338. return status;
  4339. /* XXX free source */
  4340. tag = _csi_slab_alloc (ctx, sizeof (struct _mime_tag));
  4341. if (_csi_unlikely (tag == NULL))
  4342. return _csi_error (CSI_STATUS_NO_MEMORY);
  4343. tag->ctx = cairo_script_interpreter_reference (ctx);
  4344. tag->source = source.datum.string;
  4345. tag->source->base.ref++;
  4346. status = cairo_surface_set_mime_data (surface,
  4347. mime,
  4348. (uint8_t *)
  4349. source.datum.string->string,
  4350. source.datum.string->len,
  4351. _mime_tag_destroy, tag);
  4352. if (_csi_unlikely (status)) {
  4353. _mime_tag_destroy (tag);
  4354. return status;
  4355. }
  4356. pop (2);
  4357. return CSI_STATUS_SUCCESS;
  4358. }
  4359. static csi_status_t
  4360. _set_miter_limit (csi_t *ctx)
  4361. {
  4362. csi_status_t status;
  4363. cairo_t *cr;
  4364. double miter_limit;
  4365. check (2);
  4366. status = _csi_ostack_get_number (ctx, 0, &miter_limit);
  4367. if (_csi_unlikely (status))
  4368. return status;
  4369. status = _csi_ostack_get_context (ctx, 1, &cr);
  4370. if (_csi_unlikely (status))
  4371. return status;
  4372. cairo_set_miter_limit (cr, miter_limit);
  4373. pop (1);
  4374. return CSI_STATUS_SUCCESS;
  4375. }
  4376. static csi_status_t
  4377. _set_operator (csi_t *ctx)
  4378. {
  4379. cairo_t *cr;
  4380. long val;
  4381. csi_status_t status;
  4382. check (2);
  4383. status = _csi_ostack_get_integer (ctx, 0, &val);
  4384. if (_csi_unlikely (status))
  4385. return status;
  4386. status = _csi_ostack_get_context (ctx, 1, &cr);
  4387. if (_csi_unlikely (status))
  4388. return status;
  4389. cairo_set_operator (cr, val);
  4390. pop (1);
  4391. return CSI_STATUS_SUCCESS;
  4392. }
  4393. static csi_status_t
  4394. _set_scaled_font (csi_t *ctx)
  4395. {
  4396. cairo_t *cr;
  4397. cairo_scaled_font_t *font = NULL; /* silence the compiler */
  4398. csi_status_t status;
  4399. check (2);
  4400. status = _csi_ostack_get_scaled_font (ctx, 0, &font);
  4401. if (_csi_unlikely (status))
  4402. return status;
  4403. status = _csi_ostack_get_context (ctx, 1, &cr);
  4404. if (_csi_unlikely (status))
  4405. return status;
  4406. cairo_set_scaled_font (cr, font);
  4407. pop (1);
  4408. return CSI_STATUS_SUCCESS;
  4409. }
  4410. static csi_status_t
  4411. _set_source (csi_t *ctx)
  4412. {
  4413. cairo_t *cr;
  4414. cairo_pattern_t *pattern = NULL; /* silence the compiler */
  4415. csi_status_t status;
  4416. check (2);
  4417. status = _csi_ostack_get_pattern (ctx, 0, &pattern);
  4418. if (_csi_unlikely (status))
  4419. return status;
  4420. status = _csi_ostack_get_context (ctx, 1, &cr);
  4421. if (_csi_unlikely (status))
  4422. return status;
  4423. cairo_set_source (cr, pattern);
  4424. pop (1);
  4425. return CSI_STATUS_SUCCESS;
  4426. }
  4427. static csi_boolean_t
  4428. _matching_images (cairo_surface_t *a, cairo_surface_t *b)
  4429. {
  4430. cairo_format_t format_a, format_b;
  4431. if (cairo_surface_get_type (a) != CAIRO_SURFACE_TYPE_IMAGE)
  4432. return FALSE;
  4433. if (cairo_surface_get_type (b) != CAIRO_SURFACE_TYPE_IMAGE)
  4434. return FALSE;
  4435. if (cairo_image_surface_get_height (a) != cairo_image_surface_get_height (b))
  4436. return FALSE;
  4437. if (cairo_image_surface_get_width (a) != cairo_image_surface_get_width (b))
  4438. return FALSE;
  4439. format_a = cairo_image_surface_get_format (a);
  4440. if (format_a == CAIRO_FORMAT_RGB24)
  4441. format_a = CAIRO_FORMAT_ARGB32;
  4442. format_b = cairo_image_surface_get_format (b);
  4443. if (format_b == CAIRO_FORMAT_RGB24)
  4444. format_b = CAIRO_FORMAT_ARGB32;
  4445. if (format_a != format_b)
  4446. return FALSE;
  4447. return TRUE;
  4448. }
  4449. static csi_status_t
  4450. _set_source_image (csi_t *ctx)
  4451. {
  4452. csi_status_t status;
  4453. cairo_surface_t *surface;
  4454. cairo_surface_t *source;
  4455. check (2);
  4456. status = _csi_ostack_get_surface (ctx, 0, &source);
  4457. if (_csi_unlikely (status))
  4458. return status;
  4459. status = _csi_ostack_get_surface (ctx, 1, &surface);
  4460. if (_csi_unlikely (status))
  4461. return status;
  4462. /* Catch the most frequent use of simply uploading pixel data,
  4463. * principally to remove the pixman ops from the profiles.
  4464. */
  4465. if (_csi_likely (_matching_images (surface, source))) {
  4466. if (cairo_surface_get_reference_count (surface) == 1 &&
  4467. cairo_surface_get_reference_count (source) == 1)
  4468. {
  4469. _csi_peek_ostack (ctx, 0)->datum.surface = surface;
  4470. _csi_peek_ostack (ctx, 1)->datum.surface = source;
  4471. }
  4472. else
  4473. {
  4474. cairo_surface_flush (surface);
  4475. memcpy (cairo_image_surface_get_data (surface),
  4476. cairo_image_surface_get_data (source),
  4477. cairo_image_surface_get_height (source) * cairo_image_surface_get_stride (source));
  4478. cairo_surface_mark_dirty (surface);
  4479. }
  4480. } else {
  4481. cairo_t *cr;
  4482. cr = cairo_create (surface);
  4483. cairo_set_source_surface (cr, source, 0, 0);
  4484. cairo_paint (cr);
  4485. cairo_destroy (cr);
  4486. }
  4487. pop (1);
  4488. return CSI_STATUS_SUCCESS;
  4489. }
  4490. static csi_status_t
  4491. _set_source_rgb (csi_t *ctx)
  4492. {
  4493. csi_status_t status;
  4494. double r,g,b;
  4495. cairo_t *cr;
  4496. check (4);
  4497. status = _csi_ostack_get_number (ctx, 0, &b);
  4498. if (_csi_unlikely (status))
  4499. return status;
  4500. status = _csi_ostack_get_number (ctx, 1, &g);
  4501. if (_csi_unlikely (status))
  4502. return status;
  4503. status = _csi_ostack_get_number (ctx, 2, &r);
  4504. if (_csi_unlikely (status))
  4505. return status;
  4506. status = _csi_ostack_get_context (ctx, 3, &cr);
  4507. if (_csi_unlikely (status))
  4508. return status;
  4509. cairo_set_source_rgb (cr, r, g, b);
  4510. pop (3);
  4511. return CSI_STATUS_SUCCESS;
  4512. }
  4513. static csi_status_t
  4514. _set_source_rgba (csi_t *ctx)
  4515. {
  4516. csi_status_t status;
  4517. double r,g,b,a;
  4518. cairo_t *cr;
  4519. check (5);
  4520. status = _csi_ostack_get_number (ctx, 0, &a);
  4521. if (_csi_unlikely (status))
  4522. return status;
  4523. status = _csi_ostack_get_number (ctx, 1, &b);
  4524. if (_csi_unlikely (status))
  4525. return status;
  4526. status = _csi_ostack_get_number (ctx, 2, &g);
  4527. if (_csi_unlikely (status))
  4528. return status;
  4529. status = _csi_ostack_get_number (ctx, 3, &r);
  4530. if (_csi_unlikely (status))
  4531. return status;
  4532. status = _csi_ostack_get_context (ctx, 4, &cr);
  4533. if (_csi_unlikely (status))
  4534. return status;
  4535. cairo_set_source_rgba (cr, r, g, b, a);
  4536. pop (4);
  4537. return CSI_STATUS_SUCCESS;
  4538. }
  4539. static csi_status_t
  4540. _set_tolerance (csi_t *ctx)
  4541. {
  4542. csi_status_t status;
  4543. cairo_t *cr;
  4544. double tolerance;
  4545. check (2);
  4546. status = _csi_ostack_get_number (ctx, 0, &tolerance);
  4547. if (_csi_unlikely (status))
  4548. return status;
  4549. status = _csi_ostack_get_context (ctx, 1, &cr);
  4550. if (_csi_unlikely (status))
  4551. return status;
  4552. cairo_set_tolerance (cr, tolerance);
  4553. pop (1);
  4554. return CSI_STATUS_SUCCESS;
  4555. }
  4556. static csi_status_t
  4557. _transform (csi_t *ctx)
  4558. {
  4559. csi_object_t *obj;
  4560. csi_status_t status;
  4561. cairo_matrix_t m;
  4562. int type;
  4563. check (2);
  4564. status = _csi_ostack_get_matrix (ctx, 0, &m);
  4565. if (_csi_unlikely (status))
  4566. return status;
  4567. obj = _csi_peek_ostack (ctx, 1);
  4568. type = csi_object_get_type (obj);
  4569. switch (type) {
  4570. case CSI_OBJECT_TYPE_CONTEXT:
  4571. cairo_transform (obj->datum.cr, &m);
  4572. break;
  4573. case CSI_OBJECT_TYPE_PATTERN:
  4574. {
  4575. cairo_matrix_t ctm;
  4576. cairo_pattern_get_matrix (obj->datum.pattern, &ctm);
  4577. cairo_matrix_multiply (&ctm, &m, &ctm);
  4578. cairo_pattern_set_matrix (obj->datum.pattern, &ctm);
  4579. }
  4580. break;
  4581. case CSI_OBJECT_TYPE_MATRIX:
  4582. cairo_matrix_multiply (&obj->datum.matrix->matrix,
  4583. &m,
  4584. &obj->datum.matrix->matrix);
  4585. break;
  4586. default:
  4587. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  4588. }
  4589. pop (1);
  4590. return CSI_STATUS_SUCCESS;
  4591. }
  4592. static csi_status_t
  4593. _translate (csi_t *ctx)
  4594. {
  4595. csi_object_t *obj;
  4596. csi_status_t status;
  4597. double x, y;
  4598. int type;
  4599. check (3);
  4600. status = _csi_ostack_get_number (ctx, 0, &y);
  4601. if (_csi_unlikely (status))
  4602. return status;
  4603. status = _csi_ostack_get_number (ctx, 1, &x);
  4604. if (_csi_unlikely (status))
  4605. return status;
  4606. obj = _csi_peek_ostack (ctx, 2);
  4607. type = csi_object_get_type (obj);
  4608. switch (type) {
  4609. case CSI_OBJECT_TYPE_CONTEXT:
  4610. cairo_translate (obj->datum.cr, x, y);
  4611. break;
  4612. case CSI_OBJECT_TYPE_PATTERN:
  4613. {
  4614. cairo_matrix_t ctm;
  4615. cairo_pattern_get_matrix (obj->datum.pattern, &ctm);
  4616. cairo_matrix_translate (&ctm, x, y);
  4617. cairo_pattern_set_matrix (obj->datum.pattern, &ctm);
  4618. }
  4619. break;
  4620. case CSI_OBJECT_TYPE_MATRIX:
  4621. cairo_matrix_translate (&obj->datum.matrix->matrix, x, y);
  4622. break;
  4623. default:
  4624. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  4625. }
  4626. pop (2);
  4627. return CSI_STATUS_SUCCESS;
  4628. }
  4629. static csi_status_t
  4630. _true (csi_t *ctx)
  4631. {
  4632. return _csi_push_ostack_boolean (ctx, TRUE);
  4633. }
  4634. static csi_status_t
  4635. _show_page (csi_t *ctx)
  4636. {
  4637. csi_object_t *obj;
  4638. int type;
  4639. check (1);
  4640. obj = _csi_peek_ostack (ctx, 0);
  4641. type = csi_object_get_type (obj);
  4642. switch (type) {
  4643. case CSI_OBJECT_TYPE_CONTEXT:
  4644. cairo_show_page (obj->datum.cr);
  4645. if (ctx->hooks.copy_page != NULL)
  4646. ctx->hooks.copy_page (ctx->hooks.closure, obj->datum.cr);
  4647. break;
  4648. case CSI_OBJECT_TYPE_SURFACE:
  4649. cairo_surface_show_page (obj->datum.surface);
  4650. /* XXX hook? */
  4651. break;
  4652. default:
  4653. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  4654. }
  4655. return CSI_STATUS_SUCCESS;
  4656. }
  4657. static csi_status_t
  4658. _similar (csi_t *ctx)
  4659. {
  4660. csi_object_t obj;
  4661. long content;
  4662. double width, height;
  4663. cairo_surface_t *other;
  4664. csi_status_t status;
  4665. check (4);
  4666. status = _csi_ostack_get_integer (ctx, 0, &content);
  4667. if (_csi_unlikely (status))
  4668. return status;
  4669. status = _csi_ostack_get_number (ctx, 1, &height);
  4670. if (_csi_unlikely (status))
  4671. return status;
  4672. status = _csi_ostack_get_number (ctx, 2, &width);
  4673. if (_csi_unlikely (status))
  4674. return status;
  4675. status = _csi_ostack_get_surface (ctx, 3, &other);
  4676. if (_csi_unlikely (status))
  4677. return status;
  4678. /* silently fix-up a common bug when writing CS */
  4679. if ((content & CAIRO_CONTENT_COLOR_ALPHA) == 0) {
  4680. if (_csi_unlikely (content & ~CAIRO_CONTENT_COLOR_ALPHA))
  4681. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  4682. switch ((int) content) {
  4683. default:
  4684. case CAIRO_FORMAT_ARGB32:
  4685. content = CAIRO_CONTENT_COLOR_ALPHA;
  4686. break;
  4687. case CAIRO_FORMAT_RGB16_565:
  4688. case CAIRO_FORMAT_RGB24:
  4689. content = CAIRO_CONTENT_COLOR;
  4690. break;
  4691. case CAIRO_FORMAT_A8:
  4692. case CAIRO_FORMAT_A1:
  4693. content = CAIRO_CONTENT_ALPHA;
  4694. break;
  4695. }
  4696. }
  4697. obj.type = CSI_OBJECT_TYPE_SURFACE;
  4698. obj.datum.surface = cairo_surface_create_similar (other,
  4699. content, width, height);
  4700. pop (4);
  4701. return push (&obj);
  4702. }
  4703. static csi_status_t
  4704. _similar_image (csi_t *ctx)
  4705. {
  4706. csi_object_t obj;
  4707. long format;
  4708. double width, height;
  4709. cairo_surface_t *other;
  4710. csi_status_t status;
  4711. check (4);
  4712. status = _csi_ostack_get_number (ctx, 0, &height);
  4713. if (_csi_unlikely (status))
  4714. return status;
  4715. status = _csi_ostack_get_number (ctx, 1, &width);
  4716. if (_csi_unlikely (status))
  4717. return status;
  4718. status = _csi_ostack_get_integer (ctx, 2, &format);
  4719. if (_csi_unlikely (status))
  4720. return status;
  4721. status = _csi_ostack_get_surface (ctx, 3, &other);
  4722. if (_csi_unlikely (status))
  4723. return status;
  4724. obj.type = CSI_OBJECT_TYPE_SURFACE;
  4725. obj.datum.surface = cairo_surface_create_similar_image (other,
  4726. format,
  4727. width, height);
  4728. pop (4);
  4729. return push (&obj);
  4730. }
  4731. static csi_status_t
  4732. _subsurface (csi_t *ctx)
  4733. {
  4734. csi_object_t obj;
  4735. double x, y, width, height;
  4736. cairo_surface_t *target;
  4737. csi_status_t status;
  4738. check (5);
  4739. status = _csi_ostack_get_number (ctx, 0, &height);
  4740. if (_csi_unlikely (status))
  4741. return status;
  4742. status = _csi_ostack_get_number (ctx, 1, &width);
  4743. if (_csi_unlikely (status))
  4744. return status;
  4745. status = _csi_ostack_get_number (ctx, 2, &y);
  4746. if (_csi_unlikely (status))
  4747. return status;
  4748. status = _csi_ostack_get_number (ctx, 3, &x);
  4749. if (_csi_unlikely (status))
  4750. return status;
  4751. status = _csi_ostack_get_surface (ctx, 4, &target);
  4752. if (_csi_unlikely (status))
  4753. return status;
  4754. obj.type = CSI_OBJECT_TYPE_SURFACE;
  4755. obj.datum.surface = cairo_surface_create_for_rectangle (target, x, y, width, height);
  4756. pop (5);
  4757. return push (&obj);
  4758. }
  4759. static csi_status_t
  4760. _show_text (csi_t *ctx)
  4761. {
  4762. csi_status_t status;
  4763. csi_string_t *text;
  4764. cairo_t *cr;
  4765. check (2);
  4766. status = _csi_ostack_get_string (ctx, 0, &text);
  4767. if (_csi_unlikely (status))
  4768. return status;
  4769. status = _csi_ostack_get_context (ctx, 1, &cr);
  4770. if (_csi_unlikely (status))
  4771. return status;
  4772. cairo_show_text (cr, text->string);
  4773. pop (1);
  4774. return CSI_STATUS_SUCCESS;
  4775. }
  4776. static csi_status_t
  4777. _show_glyphs (csi_t *ctx)
  4778. {
  4779. csi_array_t *array;
  4780. csi_status_t status;
  4781. cairo_t *cr;
  4782. cairo_glyph_t stack_glyphs[256], *glyphs;
  4783. csi_integer_t nglyphs, i;
  4784. check (2);
  4785. status = _csi_ostack_get_array (ctx, 0, &array);
  4786. if (_csi_unlikely (status))
  4787. return status;
  4788. status = _csi_ostack_get_context (ctx, 1, &cr);
  4789. if (_csi_unlikely (status))
  4790. return status;
  4791. /* count glyphs */
  4792. nglyphs = 0;
  4793. for (i = 0; i < array->stack.len; i++) {
  4794. csi_object_t *obj = &array->stack.objects[i];
  4795. int type = csi_object_get_type (obj);
  4796. switch (type) {
  4797. case CSI_OBJECT_TYPE_ARRAY:
  4798. nglyphs += obj->datum.array->stack.len;
  4799. break;
  4800. case CSI_OBJECT_TYPE_STRING:
  4801. nglyphs += obj->datum.string->len;
  4802. break;
  4803. }
  4804. }
  4805. if (nglyphs == 0) {
  4806. pop (1);
  4807. return CSI_STATUS_SUCCESS;
  4808. }
  4809. if (nglyphs > ARRAY_LENGTH (stack_glyphs)) {
  4810. if (_csi_unlikely ((unsigned) nglyphs >= INT_MAX / sizeof (cairo_glyph_t)))
  4811. return _csi_error (CSI_STATUS_NO_MEMORY);
  4812. glyphs = _csi_alloc (ctx, sizeof (cairo_glyph_t) * nglyphs);
  4813. if (_csi_unlikely (glyphs == NULL))
  4814. return _csi_error (CSI_STATUS_NO_MEMORY);
  4815. } else
  4816. glyphs = stack_glyphs;
  4817. nglyphs = _glyph_string (ctx, array, cairo_get_scaled_font (cr), glyphs);
  4818. cairo_show_glyphs (cr, glyphs, nglyphs);
  4819. if (glyphs != stack_glyphs)
  4820. _csi_free (ctx, glyphs);
  4821. pop (1);
  4822. return CSI_STATUS_SUCCESS;
  4823. }
  4824. static csi_status_t
  4825. _show_text_glyphs (csi_t *ctx)
  4826. {
  4827. csi_object_t *obj;
  4828. csi_array_t *array;
  4829. csi_string_t *string;
  4830. csi_string_t *utf8_string;
  4831. csi_status_t status;
  4832. cairo_t *cr;
  4833. cairo_text_cluster_t stack_clusters[256], *clusters;
  4834. cairo_glyph_t stack_glyphs[256], *glyphs;
  4835. csi_integer_t nglyphs, nclusters, i;
  4836. long direction;
  4837. int type;
  4838. check (5);
  4839. status = _csi_ostack_get_integer (ctx, 0, &direction);
  4840. if (_csi_unlikely (status))
  4841. return status;
  4842. obj = _csi_peek_ostack (ctx, 1);
  4843. type = csi_object_get_type (obj);
  4844. switch (type) {
  4845. case CSI_OBJECT_TYPE_ARRAY:
  4846. array = obj->datum.array;
  4847. nclusters = array->stack.len / 2;
  4848. if (nclusters > ARRAY_LENGTH (stack_clusters)) {
  4849. if (_csi_unlikely ((unsigned) nclusters >= INT_MAX / sizeof (cairo_text_cluster_t)))
  4850. return _csi_error (CSI_STATUS_NO_MEMORY);
  4851. clusters = _csi_alloc (ctx, sizeof (cairo_text_cluster_t) * nclusters);
  4852. if (_csi_unlikely (clusters == NULL))
  4853. return _csi_error (CSI_STATUS_NO_MEMORY);
  4854. } else
  4855. clusters = stack_clusters;
  4856. for (i = 0; i < nclusters; i++) {
  4857. clusters[i].num_bytes = csi_number_get_value (&array->stack.objects[2*i+0]);
  4858. clusters[i].num_glyphs = csi_number_get_value (&array->stack.objects[2*i+1]);
  4859. }
  4860. break;
  4861. case CSI_OBJECT_TYPE_STRING:
  4862. string = obj->datum.string;
  4863. nclusters = string->len / 2;
  4864. if (nclusters > ARRAY_LENGTH (stack_clusters)) {
  4865. if (_csi_unlikely ((unsigned) nclusters >= INT_MAX / sizeof (cairo_text_cluster_t)))
  4866. return _csi_error (CSI_STATUS_NO_MEMORY);
  4867. clusters = _csi_alloc (ctx, sizeof (cairo_text_cluster_t) * nclusters);
  4868. if (_csi_unlikely (clusters == NULL))
  4869. return _csi_error (CSI_STATUS_NO_MEMORY);
  4870. } else
  4871. clusters = stack_clusters;
  4872. for (i = 0; i < nclusters; i++) {
  4873. clusters[i].num_bytes = string->string[2*i+0];
  4874. clusters[i].num_glyphs = string->string[2*i+1];
  4875. }
  4876. break;
  4877. default:
  4878. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  4879. }
  4880. status = _csi_ostack_get_array (ctx, 2, &array);
  4881. if (_csi_unlikely (status))
  4882. return status;
  4883. status = _csi_ostack_get_string (ctx, 3, &utf8_string);
  4884. if (_csi_unlikely (status))
  4885. return status;
  4886. status = _csi_ostack_get_context (ctx, 4, &cr);
  4887. if (_csi_unlikely (status))
  4888. return status;
  4889. /* count glyphs */
  4890. nglyphs = 0;
  4891. for (i = 0; i < array->stack.len; i++) {
  4892. obj = &array->stack.objects[i];
  4893. type = csi_object_get_type (obj);
  4894. switch (type) {
  4895. case CSI_OBJECT_TYPE_ARRAY:
  4896. nglyphs += obj->datum.array->stack.len;
  4897. break;
  4898. case CSI_OBJECT_TYPE_STRING:
  4899. nglyphs += obj->datum.string->len;
  4900. break;
  4901. }
  4902. }
  4903. if (nglyphs == 0) {
  4904. pop (4);
  4905. return CSI_STATUS_SUCCESS;
  4906. }
  4907. if (nglyphs > ARRAY_LENGTH (stack_glyphs)) {
  4908. if (_csi_unlikely ((unsigned) nglyphs >= INT_MAX / sizeof (cairo_glyph_t)))
  4909. return _csi_error (CSI_STATUS_NO_MEMORY);
  4910. glyphs = _csi_alloc (ctx, sizeof (cairo_glyph_t) * nglyphs);
  4911. if (_csi_unlikely (glyphs == NULL))
  4912. return _csi_error (CSI_STATUS_NO_MEMORY);
  4913. } else
  4914. glyphs = stack_glyphs;
  4915. nglyphs = _glyph_string (ctx, array, cairo_get_scaled_font (cr), glyphs);
  4916. cairo_show_text_glyphs (cr,
  4917. utf8_string->string, utf8_string->len,
  4918. glyphs, nglyphs,
  4919. clusters, nclusters,
  4920. direction);
  4921. if (clusters != stack_clusters)
  4922. _csi_free (ctx, clusters);
  4923. if (glyphs != stack_glyphs)
  4924. _csi_free (ctx, glyphs);
  4925. pop (4);
  4926. return CSI_STATUS_SUCCESS;
  4927. }
  4928. static csi_status_t
  4929. _stroke (csi_t *ctx)
  4930. {
  4931. return _do_cairo_op (ctx, cairo_stroke);
  4932. }
  4933. static csi_status_t
  4934. _stroke_preserve (csi_t *ctx)
  4935. {
  4936. return _do_cairo_op (ctx, cairo_stroke_preserve);
  4937. }
  4938. static csi_status_t
  4939. _sub (csi_t *ctx)
  4940. {
  4941. csi_object_t *A;
  4942. csi_object_t *B;
  4943. csi_object_type_t type_a, type_b;
  4944. check (2);
  4945. B = _csi_peek_ostack (ctx, 0);
  4946. A = _csi_peek_ostack (ctx, 1);
  4947. type_a = csi_object_get_type (A);
  4948. if (_csi_unlikely (! (type_a == CSI_OBJECT_TYPE_INTEGER ||
  4949. type_a == CSI_OBJECT_TYPE_REAL)))
  4950. {
  4951. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  4952. }
  4953. type_b = csi_object_get_type (B);
  4954. if (_csi_unlikely (! (type_b == CSI_OBJECT_TYPE_INTEGER ||
  4955. type_b == CSI_OBJECT_TYPE_REAL)))
  4956. {
  4957. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  4958. }
  4959. pop (2);
  4960. if (type_a == CSI_OBJECT_TYPE_REAL &&
  4961. type_b == CSI_OBJECT_TYPE_REAL)
  4962. {
  4963. return _csi_push_ostack_real (ctx, A->datum.real - B->datum.real);
  4964. }
  4965. else if (type_a == CSI_OBJECT_TYPE_INTEGER &&
  4966. type_b == CSI_OBJECT_TYPE_INTEGER)
  4967. {
  4968. return _csi_push_ostack_integer (ctx,
  4969. A->datum.integer - B->datum.integer);
  4970. }
  4971. else
  4972. {
  4973. double v;
  4974. if (type_a == CSI_OBJECT_TYPE_REAL)
  4975. v = A->datum.real;
  4976. else
  4977. v = A->datum.integer;
  4978. if (type_b == CSI_OBJECT_TYPE_REAL)
  4979. v -= B->datum.real;
  4980. else
  4981. v -= B->datum.integer;
  4982. return _csi_push_ostack_real (ctx, v);
  4983. }
  4984. }
  4985. static csi_status_t
  4986. _surface (csi_t *ctx)
  4987. {
  4988. csi_object_t obj;
  4989. csi_dictionary_t *dict;
  4990. csi_proxy_t *proxy;
  4991. csi_object_t key;
  4992. double width, height;
  4993. csi_surface_create_func_t hook;
  4994. long content;
  4995. cairo_surface_t *surface;
  4996. long uid;
  4997. csi_status_t status;
  4998. check (1);
  4999. status = _csi_ostack_get_dictionary (ctx, 0, &dict);
  5000. if (_csi_unlikely (status))
  5001. return status;
  5002. status = _csi_dictionary_get_number (ctx, dict, "width", FALSE, &width);
  5003. if (_csi_unlikely (status))
  5004. return status;
  5005. status = _csi_dictionary_get_number (ctx, dict, "height", FALSE, &height);
  5006. if (_csi_unlikely (status))
  5007. return status;
  5008. content = CAIRO_CONTENT_COLOR_ALPHA;
  5009. status = _csi_dictionary_get_integer (ctx, dict, "content", TRUE, &content);
  5010. if (_csi_unlikely (status))
  5011. return status;
  5012. uid = 0;
  5013. status = _csi_dictionary_get_integer (ctx, dict, "uid", TRUE, &uid);
  5014. if (_csi_unlikely (status))
  5015. return status;
  5016. if (uid == 0) {
  5017. status = _csi_dictionary_get_integer (ctx, dict, "drawable", TRUE, &uid);
  5018. if (_csi_unlikely (status))
  5019. return status;
  5020. }
  5021. hook = ctx->hooks.surface_create;
  5022. assert (hook != NULL);
  5023. surface = hook (ctx->hooks.closure, content, width, height, uid);
  5024. if (_csi_unlikely (surface == NULL)) {
  5025. return _csi_error (CSI_STATUS_NULL_POINTER);
  5026. }
  5027. proxy = _csi_proxy_create (ctx, surface, dict,
  5028. ctx->hooks.surface_destroy,
  5029. ctx->hooks.closure);
  5030. if (_csi_unlikely (proxy == NULL)) {
  5031. cairo_surface_destroy (surface);
  5032. return _csi_error (CSI_STATUS_NO_MEMORY);
  5033. }
  5034. status = cairo_surface_set_user_data (surface,
  5035. &_csi_proxy_key,
  5036. proxy, _csi_proxy_destroy);
  5037. if (_csi_unlikely (status)) {
  5038. _csi_proxy_destroy (proxy);
  5039. cairo_surface_destroy (surface);
  5040. return status;
  5041. }
  5042. status = csi_name_new_static (ctx, &key, "fallback-resolution");
  5043. if (_csi_unlikely (status)) {
  5044. cairo_surface_destroy (surface);
  5045. return status;
  5046. }
  5047. if (csi_dictionary_has (dict, key.datum.name)) {
  5048. status = csi_dictionary_get (ctx, dict, key.datum.name, &obj);
  5049. if (_csi_unlikely (status)) {
  5050. cairo_surface_destroy (surface);
  5051. return status;
  5052. }
  5053. if (csi_object_get_type (&obj) == CSI_OBJECT_TYPE_ARRAY) {
  5054. csi_array_t *array = obj.datum.array;
  5055. if (array->stack.len == 2) {
  5056. cairo_surface_set_fallback_resolution (surface,
  5057. csi_number_get_value
  5058. (&array->stack.objects[0]),
  5059. csi_number_get_value
  5060. (&array->stack.objects[1]));
  5061. }
  5062. }
  5063. }
  5064. /* initialise surface to source */
  5065. status = csi_name_new_static (ctx, &key, "source");
  5066. if (_csi_unlikely (status)) {
  5067. cairo_surface_destroy (surface);
  5068. return status;
  5069. }
  5070. if (csi_dictionary_has (dict, key.datum.name)) {
  5071. cairo_surface_t *image;
  5072. cairo_t *cr;
  5073. status = _image_load_from_dictionary (ctx, dict, &image);
  5074. if (_csi_unlikely (status)) {
  5075. cairo_surface_destroy (surface);
  5076. return status;
  5077. }
  5078. cr = cairo_create (surface);
  5079. cairo_set_source_surface (cr, image, 0, 0);
  5080. cairo_surface_destroy (image);
  5081. cairo_paint (cr);
  5082. status = cairo_status (cr);
  5083. cairo_destroy (cr);
  5084. if (_csi_unlikely (status))
  5085. return status;
  5086. }
  5087. status = csi_name_new_static (ctx, &key, "device-offset");
  5088. if (_csi_unlikely (status)) {
  5089. cairo_surface_destroy (surface);
  5090. return status;
  5091. }
  5092. if (csi_dictionary_has (dict, key.datum.name)) {
  5093. status = csi_dictionary_get (ctx, dict, key.datum.name, &obj);
  5094. if (_csi_unlikely (status))
  5095. return status;
  5096. if (csi_object_get_type (&obj) == CSI_OBJECT_TYPE_ARRAY) {
  5097. csi_array_t *array = obj.datum.array;
  5098. if (array->stack.len == 2) {
  5099. cairo_surface_set_device_offset (surface,
  5100. csi_number_get_value
  5101. (&array->stack.objects[0]),
  5102. csi_number_get_value
  5103. (&array->stack.objects[1]));
  5104. }
  5105. }
  5106. }
  5107. status = csi_name_new_static (ctx, &key, "device-scale");
  5108. if (_csi_unlikely (status)) {
  5109. cairo_surface_destroy (surface);
  5110. return status;
  5111. }
  5112. if (csi_dictionary_has (dict, key.datum.name)) {
  5113. status = csi_dictionary_get (ctx, dict, key.datum.name, &obj);
  5114. if (_csi_unlikely (status))
  5115. return status;
  5116. if (csi_object_get_type (&obj) == CSI_OBJECT_TYPE_ARRAY) {
  5117. csi_array_t *array = obj.datum.array;
  5118. if (array->stack.len == 2) {
  5119. cairo_surface_set_device_scale (surface,
  5120. csi_number_get_value
  5121. (&array->stack.objects[0]),
  5122. csi_number_get_value
  5123. (&array->stack.objects[1]));
  5124. }
  5125. }
  5126. }
  5127. obj.type = CSI_OBJECT_TYPE_SURFACE;
  5128. obj.datum.surface = surface;
  5129. pop (1);
  5130. return push (&obj);
  5131. }
  5132. static csi_status_t
  5133. _record (csi_t *ctx)
  5134. {
  5135. csi_object_t obj;
  5136. long content;
  5137. csi_array_t *array;
  5138. csi_status_t status;
  5139. cairo_rectangle_t extents;
  5140. cairo_rectangle_t *r;
  5141. check (2);
  5142. status = _csi_ostack_get_array (ctx, 0, &array);
  5143. if (_csi_unlikely (status))
  5144. return status;
  5145. status = _csi_ostack_get_integer (ctx, 1, &content);
  5146. if (_csi_unlikely (status))
  5147. return status;
  5148. switch (array->stack.len) {
  5149. case 0:
  5150. r = NULL;
  5151. break;
  5152. case 2:
  5153. extents.x = extents.y = 0;
  5154. extents.width = _csi_object_as_real (&array->stack.objects[0]);
  5155. extents.height = _csi_object_as_real (&array->stack.objects[1]);
  5156. r = &extents;
  5157. break;
  5158. case 4:
  5159. extents.x = _csi_object_as_real (&array->stack.objects[0]);
  5160. extents.y = _csi_object_as_real (&array->stack.objects[1]);
  5161. extents.width = _csi_object_as_real (&array->stack.objects[2]);
  5162. extents.height = _csi_object_as_real (&array->stack.objects[3]);
  5163. r = &extents;
  5164. break;
  5165. default:
  5166. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  5167. }
  5168. obj.type = CSI_OBJECT_TYPE_SURFACE;
  5169. obj.datum.surface = cairo_recording_surface_create (content, r);
  5170. pop (2);
  5171. return push (&obj);
  5172. }
  5173. static csi_status_t
  5174. _text_path (csi_t *ctx)
  5175. {
  5176. csi_status_t status;
  5177. csi_string_t *text;
  5178. cairo_t *cr;
  5179. check (2);
  5180. status = _csi_ostack_get_string (ctx, 0, &text);
  5181. if (_csi_unlikely (status))
  5182. return status;
  5183. status = _csi_ostack_get_context (ctx, 1, &cr);
  5184. if (_csi_unlikely (status))
  5185. return status;
  5186. cairo_text_path (cr, text->string);
  5187. pop (1);
  5188. return CSI_STATUS_SUCCESS;
  5189. }
  5190. static csi_status_t
  5191. _undef (csi_t *ctx)
  5192. {
  5193. csi_name_t name = 0; /* silence the compiler */
  5194. csi_status_t status;
  5195. check (1);
  5196. status = _csi_ostack_get_name (ctx, 0, &name);
  5197. if (_csi_unlikely (status))
  5198. return status;
  5199. status = _csi_name_undefine (ctx, name);
  5200. if (_csi_unlikely (status))
  5201. return status;
  5202. pop (1);
  5203. return CSI_STATUS_SUCCESS;
  5204. }
  5205. static csi_status_t
  5206. _unset (csi_t *ctx)
  5207. {
  5208. csi_object_t *dst;
  5209. csi_name_t name = 0; /* silence the compiler */
  5210. csi_status_t status;
  5211. int type;
  5212. check (2);
  5213. status = _csi_ostack_get_name (ctx, 0, &name);
  5214. if (_csi_unlikely (status))
  5215. return status;
  5216. dst = _csi_peek_ostack (ctx, 1);
  5217. type = csi_object_get_type (dst);
  5218. switch (type) {
  5219. case CSI_OBJECT_TYPE_DICTIONARY:
  5220. csi_dictionary_remove (ctx, dst->datum.dictionary, name);
  5221. break;
  5222. case CSI_OBJECT_TYPE_STRING:
  5223. case CSI_OBJECT_TYPE_ARRAY:
  5224. default:
  5225. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  5226. }
  5227. pop (1);
  5228. return CSI_STATUS_SUCCESS;
  5229. }
  5230. static csi_status_t
  5231. _write_to_png (csi_t *ctx)
  5232. {
  5233. csi_status_t status;
  5234. csi_string_t *filename;
  5235. cairo_surface_t *surface;
  5236. check (2);
  5237. status = _csi_ostack_get_string (ctx, 0, &filename);
  5238. if (_csi_unlikely (status))
  5239. return status;
  5240. status = _csi_ostack_get_surface (ctx, 1, &surface);
  5241. if (_csi_unlikely (status))
  5242. return status;
  5243. #if CAIRO_HAS_PNG_FUNCTIONS
  5244. status = cairo_surface_write_to_png (surface, filename->string);
  5245. if (_csi_unlikely (status))
  5246. return status;
  5247. #else
  5248. return CAIRO_STATUS_WRITE_ERROR;
  5249. #endif
  5250. pop (1);
  5251. return CSI_STATUS_SUCCESS;
  5252. }
  5253. static csi_status_t
  5254. _write_to_script (csi_t *ctx)
  5255. {
  5256. csi_status_t status;
  5257. csi_string_t *filename;
  5258. cairo_surface_t *record;
  5259. check (2);
  5260. status = _csi_ostack_get_string (ctx, 0, &filename);
  5261. if (_csi_unlikely (status))
  5262. return status;
  5263. status = _csi_ostack_get_surface (ctx, 1, &record);
  5264. if (_csi_unlikely (status))
  5265. return status;
  5266. if (cairo_surface_get_type (record) != CAIRO_SURFACE_TYPE_RECORDING)
  5267. return CAIRO_STATUS_SURFACE_TYPE_MISMATCH;
  5268. #if CAIRO_HAS_SCRIPT_SURFACE
  5269. {
  5270. cairo_device_t *script;
  5271. script = cairo_script_create (filename->string);
  5272. status = cairo_script_from_recording_surface (script, record);
  5273. cairo_device_destroy (script);
  5274. if (_csi_unlikely (status))
  5275. return status;
  5276. }
  5277. #else
  5278. return CAIRO_STATUS_WRITE_ERROR;
  5279. #endif
  5280. pop (1);
  5281. return CSI_STATUS_SUCCESS;
  5282. }
  5283. static csi_status_t
  5284. _xor (csi_t *ctx)
  5285. {
  5286. csi_object_t *a, *b;
  5287. int type;
  5288. check (2);
  5289. a = _csi_peek_ostack (ctx, 0);
  5290. b = _csi_peek_ostack (ctx, 1);
  5291. if (_csi_unlikely (csi_object_get_type (a) != csi_object_get_type (b)))
  5292. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  5293. pop (2);
  5294. type = csi_object_get_type (a);
  5295. switch (type) {
  5296. case CSI_OBJECT_TYPE_INTEGER:
  5297. return _csi_push_ostack_integer (ctx,
  5298. a->datum.integer ^ b->datum.integer);
  5299. case CSI_OBJECT_TYPE_BOOLEAN:
  5300. return _csi_push_ostack_boolean (ctx,
  5301. a->datum.boolean ^ b->datum.boolean);
  5302. default:
  5303. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  5304. }
  5305. }
  5306. static csi_status_t
  5307. _debug_print (csi_t *ctx)
  5308. {
  5309. csi_object_t *obj;
  5310. check (1);
  5311. obj = _csi_peek_ostack (ctx, 0);
  5312. switch (csi_object_get_type (obj)) {
  5313. case CSI_OBJECT_TYPE_NULL:
  5314. fprintf (stderr, "NULL\n");
  5315. break;
  5316. /* atomics */
  5317. case CSI_OBJECT_TYPE_BOOLEAN:
  5318. fprintf (stderr, "boolean: %s\n",
  5319. obj->datum.boolean ? "true" : "false");
  5320. break;
  5321. case CSI_OBJECT_TYPE_INTEGER:
  5322. fprintf (stderr, "integer: %ld\n", obj->datum.integer);
  5323. break;
  5324. case CSI_OBJECT_TYPE_MARK:
  5325. fprintf (stderr, "mark\n");
  5326. break;
  5327. case CSI_OBJECT_TYPE_NAME:
  5328. fprintf (stderr, "name: %s\n", (char *) obj->datum.name);
  5329. break;
  5330. case CSI_OBJECT_TYPE_OPERATOR:
  5331. fprintf (stderr, "operator: %p\n", obj->datum.ptr);
  5332. break;
  5333. case CSI_OBJECT_TYPE_REAL:
  5334. fprintf (stderr, "real: %g\n", obj->datum.real);
  5335. break;
  5336. /* compound */
  5337. case CSI_OBJECT_TYPE_ARRAY:
  5338. fprintf (stderr, "array\n");
  5339. break;
  5340. case CSI_OBJECT_TYPE_DICTIONARY:
  5341. fprintf (stderr, "dictionary\n");
  5342. break;
  5343. case CSI_OBJECT_TYPE_FILE:
  5344. fprintf (stderr, "file\n");
  5345. break;
  5346. case CSI_OBJECT_TYPE_MATRIX:
  5347. fprintf (stderr, "matrix: [%g %g %g %g %g %g]\n",
  5348. obj->datum.matrix->matrix.xx,
  5349. obj->datum.matrix->matrix.yx,
  5350. obj->datum.matrix->matrix.xy,
  5351. obj->datum.matrix->matrix.yy,
  5352. obj->datum.matrix->matrix.x0,
  5353. obj->datum.matrix->matrix.y0);
  5354. break;
  5355. case CSI_OBJECT_TYPE_STRING:
  5356. fprintf (stderr, "string: %s\n", obj->datum.string->string);
  5357. break;
  5358. /* cairo */
  5359. case CSI_OBJECT_TYPE_CONTEXT:
  5360. fprintf (stderr, "context\n");
  5361. break;
  5362. case CSI_OBJECT_TYPE_FONT:
  5363. fprintf (stderr, "font\n");
  5364. break;
  5365. case CSI_OBJECT_TYPE_PATTERN:
  5366. fprintf (stderr, "pattern\n");
  5367. break;
  5368. case CSI_OBJECT_TYPE_SCALED_FONT:
  5369. fprintf (stderr, "scaled-font\n");
  5370. break;
  5371. case CSI_OBJECT_TYPE_SURFACE:
  5372. fprintf (stderr, "surface\n");
  5373. break;
  5374. }
  5375. pop (1);
  5376. return CSI_STATUS_SUCCESS;
  5377. }
  5378. static const csi_operator_def_t
  5379. _defs[] = {
  5380. { "<<", _mark },
  5381. { ">>", end_dict_construction },
  5382. { "[", _mark },
  5383. { "]", end_array_construction },
  5384. { "a", _alpha },
  5385. { "abs", NULL },
  5386. { "add", _add },
  5387. { "add-color-stop", _add_color_stop },
  5388. { "and", _and },
  5389. { "arc", _arc },
  5390. { "arc-negative", _arc_negative },
  5391. { "arc-", _arc_negative },
  5392. { "arc-to", NULL },
  5393. { "array", _array },
  5394. { "astore", NULL },
  5395. { "atan", NULL },
  5396. { "bind", _bind },
  5397. { "bitshift", _bitshift },
  5398. { "c", _curve_to },
  5399. { "C", _rel_curve_to },
  5400. { "ceiling", NULL },
  5401. { "clear", NULL },
  5402. { "clear-to-mark", NULL },
  5403. { "clip", _clip },
  5404. { "clip-extents", NULL },
  5405. { "clip-preserve", _clip_preserve },
  5406. { "clip+", _clip_preserve },
  5407. { "close-path", _close_path },
  5408. { "context", _context },
  5409. { "copy", _copy },
  5410. { "copy-page", _copy_page },
  5411. { "cos", NULL },
  5412. { "count", NULL },
  5413. { "count-to-mark", NULL },
  5414. { "curve-to", _curve_to },
  5415. { "cvi", _cvi },
  5416. { "cvr", _cvr },
  5417. { "def", _def },
  5418. { "device-to-user", NULL },
  5419. { "device-to-user-distance", NULL },
  5420. { "dict", _dict },
  5421. { "div", _div },
  5422. { "dup", _duplicate },
  5423. { "eq", _eq },
  5424. { "exch", _exch },
  5425. { "exec", NULL },
  5426. { "exp", NULL },
  5427. { "false", _false },
  5428. { "fill", _fill },
  5429. { "fill-extents", NULL },
  5430. { "fill-preserve", _fill_preserve },
  5431. { "fill+", _fill_preserve },
  5432. { "filter", _filter },
  5433. { "floor", NULL },
  5434. { "font", _font },
  5435. { "for", _for },
  5436. { "forall", NULL },
  5437. { "g", _gray },
  5438. { "ge", _ge },
  5439. { "get", _get },
  5440. { "glyph-path", _glyph_path },
  5441. { "gt", _gt },
  5442. { "h", _close_path },
  5443. { "identity", _identity },
  5444. { "if", _if },
  5445. { "ifelse", _ifelse },
  5446. { "image", _image },
  5447. { "index", _index },
  5448. { "integer", _integer },
  5449. { "invert", _invert },
  5450. { "in-stroke", NULL },
  5451. { "in-fill", NULL },
  5452. { "known", NULL },
  5453. { "l", _line_to },
  5454. { "L", _rel_line_to },
  5455. { "languagelevel", NULL },
  5456. { "le", _le },
  5457. { "length", NULL },
  5458. { "linear", _linear },
  5459. { "line-to", _line_to },
  5460. { "ln", NULL },
  5461. { "load", NULL },
  5462. { "log", NULL },
  5463. { "loop", NULL },
  5464. { "lt", _lt },
  5465. { "m", _move_to },
  5466. { "M", _rel_move_to },
  5467. { "map-to-image", _map_to_image },
  5468. { "mark", _mark },
  5469. { "mask", _mask },
  5470. { "matrix", _matrix },
  5471. { "mesh", _mesh },
  5472. { "begin-patch", _mesh_begin_patch },
  5473. { "end-patch", _mesh_end_patch },
  5474. { "set-control-point", _mesh_set_control_point },
  5475. { "set-corner-color", _mesh_set_corner_color },
  5476. { "mod", _mod },
  5477. { "move-to", _move_to },
  5478. { "mul", _mul },
  5479. { "multiply", NULL },
  5480. { "n", _new_path },
  5481. { "N", _new_sub_path },
  5482. { "ne", _ne },
  5483. { "neg", _neg },
  5484. { "new-path", _new_path },
  5485. { "new-sub-path", _new_sub_path },
  5486. { "not", _not },
  5487. { "null", _null },
  5488. { "or", _or },
  5489. { "paint", _paint },
  5490. { "paint-with-alpha", _paint_with_alpha },
  5491. { "pattern", _pattern },
  5492. { "pop", _pop },
  5493. { "pop-group", _pop_group },
  5494. { "push-group", _push_group },
  5495. { "radial", _radial },
  5496. { "rand", NULL },
  5497. { "record", _record },
  5498. { "rectangle", _rectangle },
  5499. { "repeat", _repeat },
  5500. { "restore", _restore },
  5501. { "rel-curve-to", _rel_curve_to },
  5502. { "rel-line-to", _rel_line_to },
  5503. { "rel-move-to", _rel_move_to },
  5504. { "reset-clip", _reset_clip },
  5505. { "rgb", _rgb },
  5506. { "rgba", _rgba },
  5507. { "roll", _roll },
  5508. { "rotate", _rotate },
  5509. { "round", NULL },
  5510. { "run", NULL },
  5511. { "save", _save },
  5512. { "scale", _scale },
  5513. { "scaled-font", _scaled_font },
  5514. { "select-font-face", _select_font_face },
  5515. { "set", _set },
  5516. { "set-antialias", _set_antialias },
  5517. { "set-dash", _set_dash },
  5518. { "set-device-offset", _set_device_offset },
  5519. { "set-device-scale", _set_device_scale },
  5520. { "set-extend", _set_extend },
  5521. { "set-fallback-resolution", _set_fallback_resolution },
  5522. { "set-fill-rule", _set_fill_rule },
  5523. { "set-filter", _set_filter },
  5524. { "set-font-face", _set_font_face },
  5525. { "set-font-options", _set_font_options },
  5526. { "set-font-matrix", _set_font_matrix },
  5527. { "set-font-size", _set_font_size },
  5528. { "set-line-cap", _set_line_cap },
  5529. { "set-line-join", _set_line_join },
  5530. { "set-line-width", _set_line_width },
  5531. { "set-matrix", _set_matrix },
  5532. { "set-miter-limit", _set_miter_limit },
  5533. { "set-mime-data", _set_mime_data },
  5534. { "set-operator", _set_operator },
  5535. { "set-scaled-font", _set_scaled_font },
  5536. { "set-source", _set_source },
  5537. { "set-source-image", _set_source_image },
  5538. { "set-source-rgb", _set_source_rgb },
  5539. { "set-source-rgba", _set_source_rgba },
  5540. { "set-tolerance", _set_tolerance },
  5541. { "show-glyphs", _show_glyphs },
  5542. { "show-text", _show_text },
  5543. { "show-text-glyphs", _show_text_glyphs },
  5544. { "show-page", _show_page },
  5545. { "similar", _similar },
  5546. { "similar-image", _similar_image },
  5547. { "sin", NULL },
  5548. { "sqrt", NULL },
  5549. { "sub", _sub },
  5550. { "subsurface", _subsurface },
  5551. { "surface", _surface },
  5552. { "string", NULL },
  5553. { "stroke", _stroke },
  5554. { "stroke-extents", NULL },
  5555. { "stroke-preserve", _stroke_preserve },
  5556. { "stroke+", _stroke_preserve },
  5557. { "text-path", _text_path },
  5558. { "transform", _transform },
  5559. { "transform-distance", NULL },
  5560. { "transform-point", NULL },
  5561. { "translate", _translate },
  5562. { "true", _true },
  5563. { "type", NULL },
  5564. { "undef", _undef },
  5565. { "unmap-image", _unmap_image },
  5566. { "unset", _unset },
  5567. { "user-to-device", NULL },
  5568. { "user-to-device-distance", NULL },
  5569. { "where", NULL },
  5570. { "write-to-png", _write_to_png },
  5571. { "write-to-script", _write_to_script },
  5572. { "xor", _xor },
  5573. { "=", _debug_print },
  5574. { NULL, NULL },
  5575. };
  5576. const csi_operator_def_t *
  5577. _csi_operators (void)
  5578. {
  5579. return _defs;
  5580. }
  5581. static const csi_integer_constant_def_t
  5582. _integer_constants[] = {
  5583. { "CLEAR", CAIRO_OPERATOR_CLEAR },
  5584. { "SOURCE", CAIRO_OPERATOR_SOURCE },
  5585. { "OVER", CAIRO_OPERATOR_OVER },
  5586. { "IN", CAIRO_OPERATOR_IN },
  5587. { "OUT", CAIRO_OPERATOR_OUT },
  5588. { "ATOP", CAIRO_OPERATOR_ATOP },
  5589. { "DEST", CAIRO_OPERATOR_DEST },
  5590. { "DEST_OVER", CAIRO_OPERATOR_DEST_OVER },
  5591. { "DEST_IN", CAIRO_OPERATOR_DEST_IN },
  5592. { "DEST_OUT", CAIRO_OPERATOR_DEST_OUT },
  5593. { "DEST_ATOP", CAIRO_OPERATOR_DEST_ATOP },
  5594. { "XOR", CAIRO_OPERATOR_XOR },
  5595. { "ADD", CAIRO_OPERATOR_ADD },
  5596. { "SATURATE", CAIRO_OPERATOR_SATURATE },
  5597. { "MULTIPLY", CAIRO_OPERATOR_MULTIPLY },
  5598. { "SCREEN", CAIRO_OPERATOR_SCREEN },
  5599. { "OVERLAY", CAIRO_OPERATOR_OVERLAY },
  5600. { "DARKEN", CAIRO_OPERATOR_DARKEN },
  5601. { "LIGHTEN", CAIRO_OPERATOR_LIGHTEN },
  5602. { "DODGE", CAIRO_OPERATOR_COLOR_DODGE },
  5603. { "BURN", CAIRO_OPERATOR_COLOR_BURN },
  5604. { "HARD_LIGHT", CAIRO_OPERATOR_HARD_LIGHT },
  5605. { "SOFT_LIGHT", CAIRO_OPERATOR_SOFT_LIGHT },
  5606. { "DIFFERENCE", CAIRO_OPERATOR_DIFFERENCE },
  5607. { "EXCLUSION", CAIRO_OPERATOR_EXCLUSION },
  5608. { "HSL_HUE", CAIRO_OPERATOR_HSL_HUE },
  5609. { "HSL_SATURATION", CAIRO_OPERATOR_HSL_SATURATION },
  5610. { "HSL_COLOR", CAIRO_OPERATOR_HSL_COLOR },
  5611. { "HSL_LUMINOSITY", CAIRO_OPERATOR_HSL_LUMINOSITY },
  5612. { "WINDING", CAIRO_FILL_RULE_WINDING },
  5613. { "EVEN_ODD", CAIRO_FILL_RULE_EVEN_ODD },
  5614. { "ANTIALIAS_DEFAULT", CAIRO_ANTIALIAS_DEFAULT },
  5615. { "ANTIALIAS_NONE", CAIRO_ANTIALIAS_NONE },
  5616. { "ANTIALIAS_GRAY", CAIRO_ANTIALIAS_GRAY },
  5617. { "ANTIALIAS_SUBPIXEL", CAIRO_ANTIALIAS_SUBPIXEL },
  5618. { "ANTIALIAS_FAST", CAIRO_ANTIALIAS_FAST },
  5619. { "ANTIALIAS_GOOD", CAIRO_ANTIALIAS_GOOD },
  5620. { "ANTIALIAS_BEST", CAIRO_ANTIALIAS_BEST },
  5621. { "LINE_CAP_BUTT", CAIRO_LINE_CAP_BUTT },
  5622. { "LINE_CAP_ROUND", CAIRO_LINE_CAP_ROUND },
  5623. { "LINE_CAP_SQUARE", CAIRO_LINE_CAP_SQUARE },
  5624. { "LINE_JOIN_MITER", CAIRO_LINE_JOIN_MITER },
  5625. { "LINE_JOIN_ROUND", CAIRO_LINE_JOIN_ROUND },
  5626. { "LINE_JOIN_BEVEL", CAIRO_LINE_JOIN_BEVEL },
  5627. { "EXTEND_NONE", CAIRO_EXTEND_NONE },
  5628. { "EXTEND_REPEAT", CAIRO_EXTEND_REPEAT },
  5629. { "EXTEND_REFLECT", CAIRO_EXTEND_REFLECT },
  5630. { "EXTEND_PAD", CAIRO_EXTEND_PAD },
  5631. { "FILTER_FAST", CAIRO_FILTER_FAST },
  5632. { "FILTER_GOOD", CAIRO_FILTER_GOOD },
  5633. { "FILTER_BEST", CAIRO_FILTER_BEST },
  5634. { "FILTER_BILINEAR", CAIRO_FILTER_BILINEAR },
  5635. { "FILTER_NEAREST", CAIRO_FILTER_NEAREST },
  5636. { "FILTER_GAUSSIAN", CAIRO_FILTER_GAUSSIAN },
  5637. { "SLANT_NORMAL", CAIRO_FONT_SLANT_NORMAL },
  5638. { "SLANT_ITALIC", CAIRO_FONT_SLANT_ITALIC },
  5639. { "SLANT_OBLIQUE", CAIRO_FONT_SLANT_OBLIQUE },
  5640. { "WEIGHT_NORMAL", CAIRO_FONT_WEIGHT_NORMAL },
  5641. { "WEIGHT_BOLD", CAIRO_FONT_WEIGHT_BOLD },
  5642. { "SUBPIXEL_ORDER_DEFAULT", CAIRO_SUBPIXEL_ORDER_DEFAULT },
  5643. { "SUBPIXEL_ORDER_RGB", CAIRO_SUBPIXEL_ORDER_RGB },
  5644. { "SUBPIXEL_ORDER_BGR", CAIRO_SUBPIXEL_ORDER_BGR },
  5645. { "SUBPIXEL_ORDER_VRGB", CAIRO_SUBPIXEL_ORDER_VRGB },
  5646. { "SUBPIXEL_ORDER_VBGR", CAIRO_SUBPIXEL_ORDER_VBGR },
  5647. { "HINT_STYLE_DEFAULT", CAIRO_HINT_STYLE_DEFAULT },
  5648. { "HINT_STYLE_NONE", CAIRO_HINT_STYLE_NONE },
  5649. { "HINT_STYLE_SLIGHT", CAIRO_HINT_STYLE_SLIGHT },
  5650. { "HINT_STYLE_MEDIUM", CAIRO_HINT_STYLE_MEDIUM },
  5651. { "HINT_STYLE_FULL", CAIRO_HINT_STYLE_FULL },
  5652. { "HINT_METRICS_DEFAULT", CAIRO_HINT_METRICS_DEFAULT },
  5653. { "HINT_METRICS_OFF", CAIRO_HINT_METRICS_OFF },
  5654. { "HINT_METRICS_ON", CAIRO_HINT_METRICS_ON },
  5655. { "FORWARD", 0 },
  5656. { "BACKWARD", 1 },
  5657. { "COLOR", CAIRO_CONTENT_COLOR },
  5658. { "ALPHA", CAIRO_CONTENT_ALPHA },
  5659. { "COLOR_ALPHA", CAIRO_CONTENT_COLOR_ALPHA },
  5660. { "A1", CAIRO_FORMAT_A1 },
  5661. { "A8", CAIRO_FORMAT_A8 },
  5662. { "RGB16_565", CAIRO_FORMAT_RGB16_565 },
  5663. { "RGB24", CAIRO_FORMAT_RGB24 },
  5664. { "ARGB32", CAIRO_FORMAT_ARGB32 },
  5665. { "INVALID", CAIRO_FORMAT_INVALID },
  5666. { NULL, 0 }
  5667. };
  5668. const csi_integer_constant_def_t *
  5669. _csi_integer_constants (void)
  5670. {
  5671. return _integer_constants;
  5672. }
  5673. static const csi_real_constant_def_t
  5674. _real_constants[] = {
  5675. { "math.pi", M_PI },
  5676. { "math.2pi", 2 * M_PI },
  5677. { "math.sqrt2", M_SQRT2 },
  5678. { "math.ln2", M_LN2 },
  5679. { NULL, 0 }
  5680. };
  5681. const csi_real_constant_def_t *
  5682. _csi_real_constants (void)
  5683. {
  5684. return _real_constants;
  5685. }