StyleAnimationValue.cpp 181 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /* Utilities for animation of computed style values */
  6. #include "mozilla/ArrayUtils.h"
  7. #include "mozilla/MathAlgorithms.h"
  8. #include "mozilla/RuleNodeCacheConditions.h"
  9. #include "mozilla/StyleAnimationValue.h"
  10. #include "mozilla/Tuple.h"
  11. #include "mozilla/UniquePtr.h"
  12. #include "nsStyleTransformMatrix.h"
  13. #include "nsAutoPtr.h"
  14. #include "nsCOMArray.h"
  15. #include "nsIStyleRule.h"
  16. #include "mozilla/css/StyleRule.h"
  17. #include "nsString.h"
  18. #include "nsStyleContext.h"
  19. #include "nsStyleSet.h"
  20. #include "nsComputedDOMStyle.h"
  21. #include "nsCSSParser.h"
  22. #include "nsCSSPseudoElements.h"
  23. #include "mozilla/css/Declaration.h"
  24. #include "mozilla/dom/Element.h"
  25. #include "mozilla/FloatingPoint.h"
  26. #include "mozilla/Likely.h"
  27. #include "mozilla/ServoBindings.h" // RawServoDeclarationBlock
  28. #include "gfxMatrix.h"
  29. #include "gfxQuaternion.h"
  30. #include "nsIDocument.h"
  31. #include "nsIFrame.h"
  32. #include "gfx2DGlue.h"
  33. using namespace mozilla;
  34. using namespace mozilla::css;
  35. using namespace mozilla::gfx;
  36. using nsStyleTransformMatrix::Decompose2DMatrix;
  37. using nsStyleTransformMatrix::Decompose3DMatrix;
  38. using nsStyleTransformMatrix::ShearType;
  39. // HELPER METHODS
  40. // --------------
  41. /*
  42. * Given two units, this method returns a common unit that they can both be
  43. * converted into, if possible. This is intended to facilitate
  44. * interpolation, distance-computation, and addition between "similar" units.
  45. *
  46. * The ordering of the arguments should not affect the output of this method.
  47. *
  48. * If there's no sensible common unit, this method returns eUnit_Null.
  49. *
  50. * @param aFirstUnit One unit to resolve.
  51. * @param aFirstUnit The other unit to resolve.
  52. * @return A "common" unit that both source units can be converted into, or
  53. * eUnit_Null if that's not possible.
  54. */
  55. static
  56. StyleAnimationValue::Unit
  57. GetCommonUnit(nsCSSPropertyID aProperty,
  58. StyleAnimationValue::Unit aFirstUnit,
  59. StyleAnimationValue::Unit aSecondUnit)
  60. {
  61. if (aFirstUnit != aSecondUnit) {
  62. bool numberAsPixel =
  63. nsCSSProps::PropHasFlags(aProperty, CSS_PROPERTY_NUMBERS_ARE_PIXELS);
  64. if (nsCSSProps::PropHasFlags(aProperty, CSS_PROPERTY_STORES_CALC) &&
  65. ((aFirstUnit == StyleAnimationValue::eUnit_Float && numberAsPixel) ||
  66. aFirstUnit == StyleAnimationValue::eUnit_Coord ||
  67. aFirstUnit == StyleAnimationValue::eUnit_Percent ||
  68. aFirstUnit == StyleAnimationValue::eUnit_Calc) &&
  69. ((aSecondUnit == StyleAnimationValue::eUnit_Float && numberAsPixel) ||
  70. aSecondUnit == StyleAnimationValue::eUnit_Coord ||
  71. aSecondUnit == StyleAnimationValue::eUnit_Percent ||
  72. aSecondUnit == StyleAnimationValue::eUnit_Calc)) {
  73. // We can use calc() as the common unit.
  74. return StyleAnimationValue::eUnit_Calc;
  75. }
  76. if ((aFirstUnit == StyleAnimationValue::eUnit_Color ||
  77. aFirstUnit == StyleAnimationValue::eUnit_CurrentColor ||
  78. aFirstUnit == StyleAnimationValue::eUnit_ComplexColor) &&
  79. (aSecondUnit == StyleAnimationValue::eUnit_Color ||
  80. aSecondUnit == StyleAnimationValue::eUnit_CurrentColor ||
  81. aSecondUnit == StyleAnimationValue::eUnit_ComplexColor)) {
  82. // We can use complex color as the common unit.
  83. return StyleAnimationValue::eUnit_ComplexColor;
  84. }
  85. return StyleAnimationValue::eUnit_Null;
  86. }
  87. return aFirstUnit;
  88. }
  89. static
  90. nsCSSUnit
  91. GetCommonUnit(nsCSSPropertyID aProperty,
  92. nsCSSUnit aFirstUnit,
  93. nsCSSUnit aSecondUnit)
  94. {
  95. if (aFirstUnit != aSecondUnit) {
  96. if (nsCSSProps::PropHasFlags(aProperty, CSS_PROPERTY_STORES_CALC) &&
  97. (aFirstUnit == eCSSUnit_Pixel ||
  98. aFirstUnit == eCSSUnit_Percent ||
  99. aFirstUnit == eCSSUnit_Calc) &&
  100. (aSecondUnit == eCSSUnit_Pixel ||
  101. aSecondUnit == eCSSUnit_Percent ||
  102. aSecondUnit == eCSSUnit_Calc)) {
  103. // We can use calc() as the common unit.
  104. return eCSSUnit_Calc;
  105. }
  106. return eCSSUnit_Null;
  107. }
  108. return aFirstUnit;
  109. }
  110. static nsCSSKeyword
  111. ToPrimitive(nsCSSKeyword aKeyword)
  112. {
  113. switch (aKeyword) {
  114. case eCSSKeyword_translatex:
  115. case eCSSKeyword_translatey:
  116. case eCSSKeyword_translatez:
  117. case eCSSKeyword_translate:
  118. return eCSSKeyword_translate3d;
  119. case eCSSKeyword_scalex:
  120. case eCSSKeyword_scaley:
  121. case eCSSKeyword_scalez:
  122. case eCSSKeyword_scale:
  123. return eCSSKeyword_scale3d;
  124. default:
  125. return aKeyword;
  126. }
  127. }
  128. static bool
  129. TransformFunctionsMatch(nsCSSKeyword func1, nsCSSKeyword func2)
  130. {
  131. return ToPrimitive(func1) == ToPrimitive(func2);
  132. }
  133. static already_AddRefed<nsCSSValue::Array>
  134. AppendFunction(nsCSSKeyword aTransformFunction)
  135. {
  136. uint32_t nargs;
  137. switch (aTransformFunction) {
  138. case eCSSKeyword_matrix3d:
  139. nargs = 16;
  140. break;
  141. case eCSSKeyword_matrix:
  142. nargs = 6;
  143. break;
  144. case eCSSKeyword_rotate3d:
  145. nargs = 4;
  146. break;
  147. case eCSSKeyword_interpolatematrix:
  148. case eCSSKeyword_translate3d:
  149. case eCSSKeyword_scale3d:
  150. nargs = 3;
  151. break;
  152. case eCSSKeyword_translate:
  153. case eCSSKeyword_skew:
  154. case eCSSKeyword_scale:
  155. nargs = 2;
  156. break;
  157. default:
  158. NS_ERROR("must be a transform function");
  159. MOZ_FALLTHROUGH;
  160. case eCSSKeyword_translatex:
  161. case eCSSKeyword_translatey:
  162. case eCSSKeyword_translatez:
  163. case eCSSKeyword_scalex:
  164. case eCSSKeyword_scaley:
  165. case eCSSKeyword_scalez:
  166. case eCSSKeyword_skewx:
  167. case eCSSKeyword_skewy:
  168. case eCSSKeyword_rotate:
  169. case eCSSKeyword_rotatex:
  170. case eCSSKeyword_rotatey:
  171. case eCSSKeyword_rotatez:
  172. case eCSSKeyword_perspective:
  173. nargs = 1;
  174. break;
  175. }
  176. RefPtr<nsCSSValue::Array> arr = nsCSSValue::Array::Create(nargs + 1);
  177. arr->Item(0).SetIntValue(aTransformFunction, eCSSUnit_Enumerated);
  178. return arr.forget();
  179. }
  180. static already_AddRefed<nsCSSValue::Array>
  181. ToPrimitive(nsCSSValue::Array* aArray)
  182. {
  183. nsCSSKeyword tfunc = nsStyleTransformMatrix::TransformFunctionOf(aArray);
  184. nsCSSKeyword primitive = ToPrimitive(tfunc);
  185. RefPtr<nsCSSValue::Array> arr = AppendFunction(primitive);
  186. // FIXME: This would produce fewer calc() expressions if the
  187. // zero were of compatible type (length vs. percent) when
  188. // needed.
  189. nsCSSValue zero(0.0f, eCSSUnit_Pixel);
  190. nsCSSValue one(1.0f, eCSSUnit_Number);
  191. switch(tfunc) {
  192. case eCSSKeyword_translate:
  193. {
  194. MOZ_ASSERT(aArray->Count() == 2 || aArray->Count() == 3,
  195. "unexpected count");
  196. arr->Item(1) = aArray->Item(1);
  197. arr->Item(2) = aArray->Count() == 3 ? aArray->Item(2) : zero;
  198. arr->Item(3) = zero;
  199. break;
  200. }
  201. case eCSSKeyword_translatex:
  202. {
  203. MOZ_ASSERT(aArray->Count() == 2, "unexpected count");
  204. arr->Item(1) = aArray->Item(1);
  205. arr->Item(2) = zero;
  206. arr->Item(3) = zero;
  207. break;
  208. }
  209. case eCSSKeyword_translatey:
  210. {
  211. MOZ_ASSERT(aArray->Count() == 2, "unexpected count");
  212. arr->Item(1) = zero;
  213. arr->Item(2) = aArray->Item(1);
  214. arr->Item(3) = zero;
  215. break;
  216. }
  217. case eCSSKeyword_translatez:
  218. {
  219. MOZ_ASSERT(aArray->Count() == 2, "unexpected count");
  220. arr->Item(1) = zero;
  221. arr->Item(2) = zero;
  222. arr->Item(3) = aArray->Item(1);
  223. break;
  224. }
  225. case eCSSKeyword_scale:
  226. {
  227. MOZ_ASSERT(aArray->Count() == 2 || aArray->Count() == 3,
  228. "unexpected count");
  229. arr->Item(1) = aArray->Item(1);
  230. arr->Item(2) = aArray->Count() == 3 ? aArray->Item(2) : aArray->Item(1);
  231. arr->Item(3) = one;
  232. break;
  233. }
  234. case eCSSKeyword_scalex:
  235. {
  236. MOZ_ASSERT(aArray->Count() == 2, "unexpected count");
  237. arr->Item(1) = aArray->Item(1);
  238. arr->Item(2) = one;
  239. arr->Item(3) = one;
  240. break;
  241. }
  242. case eCSSKeyword_scaley:
  243. {
  244. MOZ_ASSERT(aArray->Count() == 2, "unexpected count");
  245. arr->Item(1) = one;
  246. arr->Item(2) = aArray->Item(1);
  247. arr->Item(3) = one;
  248. break;
  249. }
  250. case eCSSKeyword_scalez:
  251. {
  252. MOZ_ASSERT(aArray->Count() == 2, "unexpected count");
  253. arr->Item(1) = one;
  254. arr->Item(2) = one;
  255. arr->Item(3) = aArray->Item(1);
  256. break;
  257. }
  258. default:
  259. arr = aArray;
  260. }
  261. return arr.forget();
  262. }
  263. static void
  264. AppendCSSShadowValue(const nsCSSShadowItem *aShadow,
  265. nsCSSValueList **&aResultTail)
  266. {
  267. MOZ_ASSERT(aShadow, "shadow expected");
  268. // X, Y, Radius, Spread, Color, Inset
  269. RefPtr<nsCSSValue::Array> arr = nsCSSValue::Array::Create(6);
  270. arr->Item(0).SetIntegerCoordValue(aShadow->mXOffset);
  271. arr->Item(1).SetIntegerCoordValue(aShadow->mYOffset);
  272. arr->Item(2).SetIntegerCoordValue(aShadow->mRadius);
  273. // NOTE: This code sometimes stores mSpread: 0 even when
  274. // the parser would be required to leave it null.
  275. arr->Item(3).SetIntegerCoordValue(aShadow->mSpread);
  276. if (aShadow->mHasColor) {
  277. arr->Item(4).SetColorValue(aShadow->mColor);
  278. }
  279. if (aShadow->mInset) {
  280. arr->Item(5).SetIntValue(uint8_t(StyleBoxShadowType::Inset),
  281. eCSSUnit_Enumerated);
  282. }
  283. nsCSSValueList *resultItem = new nsCSSValueList;
  284. resultItem->mValue.SetArrayValue(arr, eCSSUnit_Array);
  285. *aResultTail = resultItem;
  286. aResultTail = &resultItem->mNext;
  287. }
  288. // Like nsStyleCoord::CalcValue, but with length in float pixels instead
  289. // of nscoord.
  290. struct PixelCalcValue
  291. {
  292. float mLength, mPercent;
  293. bool mHasPercent;
  294. };
  295. // Requires a canonical calc() value that we generated.
  296. static PixelCalcValue
  297. ExtractCalcValueInternal(const nsCSSValue& aValue)
  298. {
  299. MOZ_ASSERT(aValue.GetUnit() == eCSSUnit_Calc, "unexpected unit");
  300. nsCSSValue::Array *arr = aValue.GetArrayValue();
  301. MOZ_ASSERT(arr->Count() == 1, "unexpected length");
  302. const nsCSSValue &topval = arr->Item(0);
  303. PixelCalcValue result;
  304. if (topval.GetUnit() == eCSSUnit_Pixel) {
  305. result.mLength = topval.GetFloatValue();
  306. result.mPercent = 0.0f;
  307. result.mHasPercent = false;
  308. } else {
  309. MOZ_ASSERT(topval.GetUnit() == eCSSUnit_Calc_Plus,
  310. "unexpected unit");
  311. nsCSSValue::Array *arr2 = topval.GetArrayValue();
  312. const nsCSSValue &len = arr2->Item(0);
  313. const nsCSSValue &pct = arr2->Item(1);
  314. MOZ_ASSERT(len.GetUnit() == eCSSUnit_Pixel, "unexpected unit");
  315. MOZ_ASSERT(pct.GetUnit() == eCSSUnit_Percent, "unexpected unit");
  316. result.mLength = len.GetFloatValue();
  317. result.mPercent = pct.GetPercentValue();
  318. result.mHasPercent = true;
  319. }
  320. return result;
  321. }
  322. // Requires a canonical calc() value that we generated.
  323. static PixelCalcValue
  324. ExtractCalcValue(const StyleAnimationValue& aValue)
  325. {
  326. PixelCalcValue result;
  327. if (aValue.GetUnit() == StyleAnimationValue::eUnit_Coord) {
  328. result.mLength =
  329. nsPresContext::AppUnitsToFloatCSSPixels(aValue.GetCoordValue());
  330. result.mPercent = 0.0f;
  331. result.mHasPercent = false;
  332. return result;
  333. }
  334. if (aValue.GetUnit() == StyleAnimationValue::eUnit_Percent) {
  335. result.mLength = 0.0f;
  336. result.mPercent = aValue.GetPercentValue();
  337. result.mHasPercent = true;
  338. return result;
  339. }
  340. if (aValue.GetUnit() == StyleAnimationValue::eUnit_Float) {
  341. result.mLength = aValue.GetFloatValue();
  342. result.mPercent = 0.0f;
  343. result.mHasPercent = false;
  344. return result;
  345. }
  346. MOZ_ASSERT(aValue.GetUnit() == StyleAnimationValue::eUnit_Calc,
  347. "unexpected unit");
  348. nsCSSValue *val = aValue.GetCSSValueValue();
  349. return ExtractCalcValueInternal(*val);
  350. }
  351. static PixelCalcValue
  352. ExtractCalcValue(const nsCSSValue& aValue)
  353. {
  354. PixelCalcValue result;
  355. if (aValue.GetUnit() == eCSSUnit_Pixel) {
  356. result.mLength = aValue.GetFloatValue();
  357. result.mPercent = 0.0f;
  358. result.mHasPercent = false;
  359. return result;
  360. }
  361. if (aValue.GetUnit() == eCSSUnit_Percent) {
  362. result.mLength = 0.0f;
  363. result.mPercent = aValue.GetPercentValue();
  364. result.mHasPercent = true;
  365. return result;
  366. }
  367. return ExtractCalcValueInternal(aValue);
  368. }
  369. static void
  370. CalcValueToCSSValue(const nsStyleCoord::CalcValue* aCalc, nsCSSValue& aValue)
  371. {
  372. aValue.SetCalcValue(aCalc);
  373. }
  374. static void
  375. CalcValueToCSSValue(const PixelCalcValue& aCalc, nsCSSValue& aValue)
  376. {
  377. RefPtr<nsCSSValue::Array> arr = nsCSSValue::Array::Create(1);
  378. if (!aCalc.mHasPercent) {
  379. arr->Item(0).SetFloatValue(aCalc.mLength, eCSSUnit_Pixel);
  380. } else {
  381. nsCSSValue::Array *arr2 = nsCSSValue::Array::Create(2);
  382. arr->Item(0).SetArrayValue(arr2, eCSSUnit_Calc_Plus);
  383. arr2->Item(0).SetFloatValue(aCalc.mLength, eCSSUnit_Pixel);
  384. arr2->Item(1).SetPercentValue(aCalc.mPercent);
  385. }
  386. aValue.SetArrayValue(arr, eCSSUnit_Calc);
  387. }
  388. double
  389. CalcPositionSquareDistance(const nsCSSValue& aPos1,
  390. const nsCSSValue& aPos2)
  391. {
  392. NS_ASSERTION(aPos1.GetUnit() == eCSSUnit_Array &&
  393. aPos2.GetUnit() == eCSSUnit_Array,
  394. "Expected two arrays");
  395. PixelCalcValue calcVal[4];
  396. nsCSSValue::Array* posArray = aPos1.GetArrayValue();
  397. MOZ_ASSERT(posArray->Count() == 4, "Invalid position value");
  398. NS_ASSERTION(posArray->Item(0).GetUnit() == eCSSUnit_Null &&
  399. posArray->Item(2).GetUnit() == eCSSUnit_Null,
  400. "Invalid list used");
  401. for (int i = 0; i < 2; ++i) {
  402. MOZ_ASSERT(posArray->Item(i*2+1).GetUnit() != eCSSUnit_Null,
  403. "Invalid position value");
  404. calcVal[i] = ExtractCalcValue(posArray->Item(i*2+1));
  405. }
  406. posArray = aPos2.GetArrayValue();
  407. MOZ_ASSERT(posArray->Count() == 4, "Invalid position value");
  408. NS_ASSERTION(posArray->Item(0).GetUnit() == eCSSUnit_Null &&
  409. posArray->Item(2).GetUnit() == eCSSUnit_Null,
  410. "Invalid list used");
  411. for (int i = 0; i < 2; ++i) {
  412. MOZ_ASSERT(posArray->Item(i*2+1).GetUnit() != eCSSUnit_Null,
  413. "Invalid position value");
  414. calcVal[i+2] = ExtractCalcValue(posArray->Item(i*2+1));
  415. }
  416. double squareDistance = 0.0;
  417. for (int i = 0; i < 2; ++i) {
  418. float difflen = calcVal[i+2].mLength - calcVal[i].mLength;
  419. float diffpct = calcVal[i+2].mPercent - calcVal[i].mPercent;
  420. squareDistance += difflen * difflen + diffpct * diffpct;
  421. }
  422. return squareDistance;
  423. }
  424. static PixelCalcValue
  425. CalcBackgroundCoord(const nsCSSValue& aCoord)
  426. {
  427. NS_ASSERTION(aCoord.GetUnit() == eCSSUnit_Array,
  428. "Expected array");
  429. nsCSSValue::Array* array = aCoord.GetArrayValue();
  430. MOZ_ASSERT(array->Count() == 2 &&
  431. array->Item(0).GetUnit() == eCSSUnit_Null &&
  432. array->Item(1).GetUnit() != eCSSUnit_Null,
  433. "Invalid position value");
  434. return ExtractCalcValue(array->Item(1));
  435. }
  436. double
  437. CalcPositionCoordSquareDistance(const nsCSSValue& aPos1,
  438. const nsCSSValue& aPos2)
  439. {
  440. PixelCalcValue calcVal1 = CalcBackgroundCoord(aPos1);
  441. PixelCalcValue calcVal2 = CalcBackgroundCoord(aPos2);
  442. float difflen = calcVal2.mLength - calcVal1.mLength;
  443. float diffpct = calcVal2.mPercent - calcVal1.mPercent;
  444. return difflen * difflen + diffpct * diffpct;
  445. }
  446. // Ensure that a float/double value isn't NaN by returning zero instead
  447. // (NaN doesn't have a sign) as a general restriction for floating point
  448. // values in RestrictValue.
  449. template<typename T>
  450. MOZ_ALWAYS_INLINE T
  451. EnsureNotNan(T aValue)
  452. {
  453. return aValue;
  454. }
  455. template<>
  456. MOZ_ALWAYS_INLINE float
  457. EnsureNotNan(float aValue)
  458. {
  459. // This would benefit from a MOZ_FLOAT_IS_NaN if we had one.
  460. return MOZ_LIKELY(!mozilla::IsNaN(aValue)) ? aValue : 0;
  461. }
  462. template<>
  463. MOZ_ALWAYS_INLINE double
  464. EnsureNotNan(double aValue)
  465. {
  466. return MOZ_LIKELY(!mozilla::IsNaN(aValue)) ? aValue : 0;
  467. }
  468. template <typename T>
  469. T
  470. RestrictValue(uint32_t aRestrictions, T aValue)
  471. {
  472. T result = EnsureNotNan(aValue);
  473. switch (aRestrictions) {
  474. case 0:
  475. break;
  476. case CSS_PROPERTY_VALUE_NONNEGATIVE:
  477. if (result < 0) {
  478. result = 0;
  479. }
  480. break;
  481. case CSS_PROPERTY_VALUE_AT_LEAST_ONE:
  482. if (result < 1) {
  483. result = 1;
  484. }
  485. break;
  486. default:
  487. MOZ_ASSERT(false, "bad value restriction");
  488. break;
  489. }
  490. return result;
  491. }
  492. template <typename T>
  493. T
  494. RestrictValue(nsCSSPropertyID aProperty, T aValue)
  495. {
  496. return RestrictValue(nsCSSProps::ValueRestrictions(aProperty), aValue);
  497. }
  498. static void
  499. AddCSSValueAngle(double aCoeff1, const nsCSSValue &aValue1,
  500. double aCoeff2, const nsCSSValue &aValue2,
  501. nsCSSValue &aResult)
  502. {
  503. if (aValue1.GetUnit() == aValue2.GetUnit()) {
  504. // To avoid floating point error, if the units match, maintain the unit.
  505. aResult.SetFloatValue(
  506. EnsureNotNan(aCoeff1 * aValue1.GetFloatValue() +
  507. aCoeff2 * aValue2.GetFloatValue()),
  508. aValue1.GetUnit());
  509. } else {
  510. aResult.SetFloatValue(
  511. EnsureNotNan(aCoeff1 * aValue1.GetAngleValueInRadians() +
  512. aCoeff2 * aValue2.GetAngleValueInRadians()),
  513. eCSSUnit_Radian);
  514. }
  515. }
  516. static inline void
  517. AddCSSValuePercent(double aCoeff1, const nsCSSValue &aValue1,
  518. double aCoeff2, const nsCSSValue &aValue2,
  519. nsCSSValue &aResult, uint32_t aValueRestrictions = 0)
  520. {
  521. MOZ_ASSERT(aValue1.GetUnit() == eCSSUnit_Percent, "unexpected unit");
  522. MOZ_ASSERT(aValue2.GetUnit() == eCSSUnit_Percent, "unexpected unit");
  523. aResult.SetPercentValue(RestrictValue(aValueRestrictions,
  524. aCoeff1 * aValue1.GetPercentValue() +
  525. aCoeff2 * aValue2.GetPercentValue()));
  526. }
  527. // Add two canonical-form calc values (eUnit_Calc) to make another
  528. // canonical-form calc value.
  529. static void
  530. AddCSSValueCanonicalCalc(double aCoeff1, const nsCSSValue &aValue1,
  531. double aCoeff2, const nsCSSValue &aValue2,
  532. nsCSSValue &aResult)
  533. {
  534. PixelCalcValue v1 = ExtractCalcValue(aValue1);
  535. PixelCalcValue v2 = ExtractCalcValue(aValue2);
  536. PixelCalcValue result;
  537. result.mLength = aCoeff1 * v1.mLength + aCoeff2 * v2.mLength;
  538. result.mPercent = aCoeff1 * v1.mPercent + aCoeff2 * v2.mPercent;
  539. result.mHasPercent = v1.mHasPercent || v2.mHasPercent;
  540. MOZ_ASSERT(result.mHasPercent || result.mPercent == 0.0f,
  541. "can't have a nonzero percentage part without having percentages");
  542. CalcValueToCSSValue(result, aResult);
  543. }
  544. static inline void
  545. AddCSSValuePixel(double aCoeff1, const nsCSSValue &aValue1,
  546. double aCoeff2, const nsCSSValue &aValue2,
  547. nsCSSValue &aResult, uint32_t aValueRestrictions = 0)
  548. {
  549. MOZ_ASSERT(aValue1.GetUnit() == eCSSUnit_Pixel, "unexpected unit");
  550. MOZ_ASSERT(aValue2.GetUnit() == eCSSUnit_Pixel, "unexpected unit");
  551. aResult.SetFloatValue(RestrictValue(aValueRestrictions,
  552. aCoeff1 * aValue1.GetFloatValue() +
  553. aCoeff2 * aValue2.GetFloatValue()),
  554. eCSSUnit_Pixel);
  555. }
  556. static bool
  557. AddCSSValuePixelPercentCalc(const uint32_t aValueRestrictions,
  558. const nsCSSUnit aCommonUnit,
  559. double aCoeff1, const nsCSSValue &aValue1,
  560. double aCoeff2, const nsCSSValue &aValue2,
  561. nsCSSValue &aResult)
  562. {
  563. switch (aCommonUnit) {
  564. case eCSSUnit_Pixel:
  565. AddCSSValuePixel(aCoeff1, aValue1,
  566. aCoeff2, aValue2,
  567. aResult, aValueRestrictions);
  568. break;
  569. case eCSSUnit_Percent:
  570. AddCSSValuePercent(aCoeff1, aValue1,
  571. aCoeff2, aValue2,
  572. aResult, aValueRestrictions);
  573. break;
  574. case eCSSUnit_Calc:
  575. AddCSSValueCanonicalCalc(aCoeff1, aValue1,
  576. aCoeff2, aValue2,
  577. aResult);
  578. break;
  579. default:
  580. return false;
  581. }
  582. return true;
  583. }
  584. static void
  585. AddTransformTranslate(double aCoeff1, const nsCSSValue &aValue1,
  586. double aCoeff2, const nsCSSValue &aValue2,
  587. nsCSSValue &aResult)
  588. {
  589. // Only three possible units: eCSSUnit_Pixel, eCSSUnit_Percent, or
  590. // eCSSUnit_Calc.
  591. MOZ_ASSERT(aValue1.GetUnit() == eCSSUnit_Percent ||
  592. aValue1.GetUnit() == eCSSUnit_Pixel ||
  593. aValue1.IsCalcUnit(),
  594. "unexpected unit");
  595. MOZ_ASSERT(aValue2.GetUnit() == eCSSUnit_Percent ||
  596. aValue2.GetUnit() == eCSSUnit_Pixel ||
  597. aValue2.IsCalcUnit(),
  598. "unexpected unit");
  599. AddCSSValuePixelPercentCalc(0,
  600. (aValue1.GetUnit() != aValue2.GetUnit() ||
  601. aValue1.IsCalcUnit())
  602. ? eCSSUnit_Calc
  603. : aValue1.GetUnit(),
  604. aCoeff1, aValue1,
  605. aCoeff2, aValue2,
  606. aResult);
  607. }
  608. // CLASS METHODS
  609. // -------------
  610. static RGBAColorData
  611. ExtractColor(const nsCSSValue& aValue)
  612. {
  613. MOZ_ASSERT(aValue.IsNumericColorUnit(), "The unit should be color");
  614. // PercentageRGBColor and PercentageRGBAColor component value might be
  615. // greater than 1.0 in case when the color value is accumulated, so we
  616. // can't use nsCSSValue::GetColorValue() here because that function
  617. // clamps its values.
  618. if (aValue.GetUnit() == eCSSUnit_PercentageRGBColor ||
  619. aValue.GetUnit() == eCSSUnit_PercentageRGBAColor) {
  620. nsCSSValueFloatColor* floatColor = aValue.GetFloatColorValue();
  621. return {
  622. floatColor->Comp1(),
  623. floatColor->Comp2(),
  624. floatColor->Comp3(),
  625. floatColor->Alpha()
  626. };
  627. }
  628. return RGBAColorData(aValue.GetColorValue());
  629. }
  630. static RGBAColorData
  631. ExtractColor(const StyleAnimationValue& aValue)
  632. {
  633. MOZ_ASSERT(aValue.GetUnit() == StyleAnimationValue::eUnit_Color);
  634. nsCSSValue* value = aValue.GetCSSValueValue();
  635. MOZ_ASSERT(value, "CSS value must be valid");
  636. return ExtractColor(*value);
  637. }
  638. static ComplexColorData
  639. ExtractComplexColor(const StyleAnimationValue& aValue)
  640. {
  641. switch (aValue.GetUnit()) {
  642. case StyleAnimationValue::eUnit_Color:
  643. return ComplexColorData(ExtractColor(aValue), 0.0f);
  644. case StyleAnimationValue::eUnit_CurrentColor:
  645. return ComplexColorData({0, 0, 0, 0}, 1.0f);
  646. case StyleAnimationValue::eUnit_ComplexColor:
  647. return ComplexColorData(aValue.GetComplexColorData());
  648. default:
  649. MOZ_ASSERT_UNREACHABLE("Unknown unit");
  650. return ComplexColorData({0, 0, 0, 0}, 0.0f);
  651. }
  652. }
  653. double
  654. StyleAnimationValue::ComputeColorDistance(const RGBAColorData& aStartColor,
  655. const RGBAColorData& aEndColor)
  656. {
  657. // http://www.w3.org/TR/smil-animation/#animateColorElement says
  658. // that we should use Euclidean RGB cube distance. However, we
  659. // have to extend that to RGBA. For now, we'll just use the
  660. // Euclidean distance in the (part of the) 4-cube of premultiplied
  661. // colors.
  662. double startA = aStartColor.mA;
  663. double startR = aStartColor.mR * startA;
  664. double startG = aStartColor.mG * startA;
  665. double startB = aStartColor.mB * startA;
  666. double endA = aEndColor.mA;
  667. double endR = aEndColor.mR * endA;
  668. double endG = aEndColor.mG * endA;
  669. double endB = aEndColor.mB * endA;
  670. double diffA = startA - endA;
  671. double diffR = startR - endR;
  672. double diffG = startG - endG;
  673. double diffB = startB - endB;
  674. return sqrt(diffA * diffA + diffR * diffR + diffG * diffG + diffB * diffB);
  675. }
  676. enum class Restrictions {
  677. Enable,
  678. Disable
  679. };
  680. static already_AddRefed<nsCSSValue::Array>
  681. AddShapeFunction(nsCSSPropertyID aProperty,
  682. double aCoeff1, const nsCSSValue::Array* aArray1,
  683. double aCoeff2, const nsCSSValue::Array* aArray2,
  684. Restrictions aRestriction = Restrictions::Enable);
  685. static double
  686. ComputeShapeDistance(nsCSSPropertyID aProperty,
  687. const nsCSSValue::Array* aArray1,
  688. const nsCSSValue::Array* aArray2)
  689. {
  690. // Use AddShapeFunction to get the difference between two shape functions.
  691. RefPtr<nsCSSValue::Array> diffShape =
  692. AddShapeFunction(aProperty, 1.0, aArray2, -1.0, aArray1,
  693. Restrictions::Disable);
  694. if (!diffShape) {
  695. return 0.0;
  696. }
  697. // A helper function to convert a calc() diff value into a double distance.
  698. auto pixelCalcDistance = [](const PixelCalcValue& aValue) {
  699. MOZ_ASSERT(aValue.mHasPercent || aValue.mPercent == 0.0f,
  700. "can't have a nonzero percentage part without having percentages");
  701. return aValue.mLength * aValue.mLength + aValue.mPercent * aValue.mPercent;
  702. };
  703. double squareDistance = 0.0;
  704. const nsCSSValue::Array* func = diffShape->Item(0).GetArrayValue();
  705. nsCSSKeyword shapeFuncName = func->Item(0).GetKeywordValue();
  706. switch (shapeFuncName) {
  707. case eCSSKeyword_ellipse:
  708. case eCSSKeyword_circle: {
  709. // Skip the first element which is the function keyword.
  710. // Also, skip the last element which is an array for <position>
  711. const size_t len = func->Count();
  712. for (size_t i = 1; i < len - 1; ++i) {
  713. squareDistance += pixelCalcDistance(ExtractCalcValue(func->Item(i)));
  714. }
  715. // Only iterate over elements 1 and 3. The <position> is 'uncomputed' to
  716. // only those elements. See also the comment in SetPositionValue.
  717. for (size_t i = 1; i < 4; i += 2) {
  718. const nsCSSValue& value = func->Item(len - 1).GetArrayValue()->Item(i);
  719. squareDistance += pixelCalcDistance(ExtractCalcValue(value));
  720. }
  721. break;
  722. }
  723. case eCSSKeyword_polygon: {
  724. // Don't care about the first element which is the function keyword, and
  725. // the second element which is the fill rule.
  726. const nsCSSValuePairList* list = func->Item(2).GetPairListValue();
  727. do {
  728. squareDistance += pixelCalcDistance(ExtractCalcValue(list->mXValue)) +
  729. pixelCalcDistance(ExtractCalcValue(list->mYValue));
  730. list = list->mNext;
  731. } while (list);
  732. break;
  733. }
  734. case eCSSKeyword_inset: {
  735. // Items 1-4 are respectively the top, right, bottom and left offsets
  736. // from the reference box.
  737. for (size_t i = 1; i <= 4; ++i) {
  738. const nsCSSValue& value = func->Item(i);
  739. squareDistance += pixelCalcDistance(ExtractCalcValue(value));
  740. }
  741. // Item 5 contains the radii of the rounded corners for the inset
  742. // rectangle.
  743. const nsCSSValue::Array* array = func->Item(5).GetArrayValue();
  744. const size_t len = array->Count();
  745. for (size_t i = 0; i < len; ++i) {
  746. const nsCSSValuePair& pair = array->Item(i).GetPairValue();
  747. squareDistance += pixelCalcDistance(ExtractCalcValue(pair.mXValue)) +
  748. pixelCalcDistance(ExtractCalcValue(pair.mYValue));
  749. }
  750. break;
  751. }
  752. default:
  753. MOZ_ASSERT_UNREACHABLE("Unknown shape type");
  754. }
  755. return sqrt(squareDistance);
  756. }
  757. static nsCSSValueList*
  758. AddTransformLists(double aCoeff1, const nsCSSValueList* aList1,
  759. double aCoeff2, const nsCSSValueList* aList2);
  760. static double
  761. ComputeTransform2DMatrixDistance(const Matrix& aMatrix1,
  762. const Matrix& aMatrix2)
  763. {
  764. Point3D scale1(1, 1, 1);
  765. Point3D translate1;
  766. gfxQuaternion rotate1;
  767. nsStyleTransformMatrix::ShearArray shear1{0.0f, 0.0f, 0.0f};
  768. Decompose2DMatrix(aMatrix1, scale1, shear1, rotate1, translate1);
  769. Point3D scale2(1, 1, 1);
  770. Point3D translate2;
  771. gfxQuaternion rotate2;
  772. nsStyleTransformMatrix::ShearArray shear2{0.0f, 0.0f, 0.0f};
  773. Decompose2DMatrix(aMatrix2, scale2, shear2, rotate2, translate2);
  774. // Note:
  775. // 1. Shear factor is the tangent value of shear angle, so we need to
  776. // call atan() to get the angle. For 2D transform, we only have XYSHEAR.
  777. // 2. The quaternion vector of the decomposed 2d matrix is got by
  778. // "gfxQuaternion(0, 0, sin(rotate/2), cos(rotate/2))"
  779. // ^^^^^^^^^^^^^ ^^^^^^^^^^^^^
  780. // z w
  781. // Therefore, we can get the rotate angle by 2 * atan2f(z, w).
  782. //
  783. // However, we can also get the rotate angle by the inner product of
  784. // two quaternion vectors, just as what we do for eCSSKeyword_rotate3d.
  785. // e.g.
  786. // rotate3d(0, 0, 1, 60deg) => rotate3d(0, 0, 1, 120deg);
  787. // quaternion 1: (0, 0, sin(30deg), cos(30deg)) = (0, 0, 1/2, sqrt(3)/2)
  788. // quaternion 2: (0, 0, sin(60deg), cos(60deg)) = (0, 0, sqrt(3)/2, 1/2)
  789. // inner product: sqrt(3)/4 + sqrt(3)/4 = sqrt(3)/2
  790. // Finally, the rotate angle: 2 * acos(sqrt(3)/2) = 60deg
  791. //
  792. // I think doing atan() may be faster than doing inner product together
  793. // with acos(), so let's adopt atan2f().
  794. const Point3D diffTranslate = translate2 - translate1;
  795. const Point3D diffScale = scale2 - scale1;
  796. const double diffShear = atan(shear2[ShearType::XYSHEAR]) -
  797. atan(shear1[ShearType::XYSHEAR]);
  798. const double diffRotate = 2.0 * (atan2f(rotate2.z, rotate2.w) -
  799. atan2f(rotate1.z, rotate1.w));
  800. // Returns the sum of squares because we will take a square root in
  801. // ComputeTransformListDistance.
  802. return diffTranslate.DotProduct(diffTranslate) +
  803. diffScale.DotProduct(diffScale) +
  804. diffRotate * diffRotate +
  805. diffShear * diffShear;
  806. }
  807. static double
  808. ComputeTransform3DMatrixDistance(const Matrix4x4& aMatrix1,
  809. const Matrix4x4& aMatrix2)
  810. {
  811. Point3D scale1(1, 1, 1);
  812. Point3D translate1;
  813. Point4D perspective1(0, 0, 0, 1);
  814. gfxQuaternion rotate1;
  815. nsStyleTransformMatrix::ShearArray shear1{0.0f, 0.0f, 0.0f};
  816. Decompose3DMatrix(aMatrix1, scale1, shear1, rotate1, translate1,
  817. perspective1);
  818. Point3D scale2(1, 1, 1);
  819. Point3D translate2;
  820. Point4D perspective2(0, 0, 0, 1);
  821. gfxQuaternion rotate2;
  822. nsStyleTransformMatrix::ShearArray shear2{0.0f, 0.0f, 0.0f};
  823. Decompose3DMatrix(aMatrix2, scale2, shear2, rotate2, translate2,
  824. perspective2);
  825. // Note:
  826. // 1. Shear factor is the tangent value of shear angle, so we need to
  827. // call atan() to get the angle.
  828. // 2. We use the same way to get the rotate angle of two quaternion vectors as
  829. // what we do for rotate3d.
  830. const Point3D diffTranslate = translate2 - translate1;
  831. const Point3D diffScale = scale2 - scale1;
  832. const Point3D diffShear(atan(shear2[ShearType::XYSHEAR]) -
  833. atan(shear1[ShearType::XYSHEAR]),
  834. atan(shear2[ShearType::XZSHEAR]) -
  835. atan(shear1[ShearType::XZSHEAR]),
  836. atan(shear2[ShearType::YZSHEAR]) -
  837. atan(shear1[ShearType::YZSHEAR]));
  838. const Point4D diffPerspective = perspective2 - perspective1;
  839. const double dot = clamped(rotate1.DotProduct(rotate2), -1.0, 1.0);
  840. const double diffRotate = 2.0 * acos(dot);
  841. // Returns the sum of squares because we will take a square root in
  842. // ComputeTransformListDistance.
  843. return diffTranslate.DotProduct(diffTranslate) +
  844. diffScale.DotProduct(diffScale) +
  845. diffPerspective.DotProduct(diffPerspective) +
  846. diffShear.DotProduct(diffShear) +
  847. diffRotate * diffRotate;
  848. }
  849. static double
  850. ComputeTransformDistance(nsCSSValue::Array* aArray1,
  851. nsCSSValue::Array* aArray2)
  852. {
  853. MOZ_ASSERT(aArray1, "aArray1 should be non-null.");
  854. MOZ_ASSERT(aArray2, "aArray2 should be non-null.");
  855. // Normalize translate and scale functions to equivalent "translate3d" and
  856. // "scale3d" functions.
  857. RefPtr<nsCSSValue::Array> a1 = ToPrimitive(aArray1),
  858. a2 = ToPrimitive(aArray2);
  859. nsCSSKeyword tfunc = nsStyleTransformMatrix::TransformFunctionOf(a1);
  860. MOZ_ASSERT(nsStyleTransformMatrix::TransformFunctionOf(a2) == tfunc);
  861. double distance = 0.0;
  862. switch (tfunc) {
  863. case eCSSKeyword_translate3d: {
  864. MOZ_ASSERT(a1->Count() == 4, "unexpected count");
  865. MOZ_ASSERT(a2->Count() == 4, "unexpected count");
  866. nsCSSValue x, y, z;
  867. AddTransformTranslate(1.0, a2->Item(1), -1.0, a1->Item(1), x);
  868. AddTransformTranslate(1.0, a2->Item(2), -1.0, a1->Item(2), y);
  869. AddTransformTranslate(1.0, a2->Item(3), -1.0, a1->Item(3), z);
  870. // Drop percent part because we only compute distance by computed values.
  871. double c1 = ExtractCalcValue(x).mLength;
  872. double c2 = ExtractCalcValue(y).mLength;
  873. double c3 = z.GetFloatValue();
  874. distance = c1 * c1 + c2 * c2 + c3 * c3;
  875. break;
  876. }
  877. case eCSSKeyword_scale3d: {
  878. MOZ_ASSERT(a1->Count() == 4, "unexpected count");
  879. MOZ_ASSERT(a2->Count() == 4, "unexpected count");
  880. auto ComputeScaleDiff = [](const nsCSSValue& aValue1,
  881. const nsCSSValue& aValue2) {
  882. float v1 = aValue1.GetFloatValue();
  883. float v2 = aValue2.GetFloatValue();
  884. return EnsureNotNan(v2 - v1);
  885. };
  886. double c1 = ComputeScaleDiff(a1->Item(1), a2->Item(1));
  887. double c2 = ComputeScaleDiff(a1->Item(2), a2->Item(2));
  888. double c3 = ComputeScaleDiff(a1->Item(3), a2->Item(3));
  889. distance = c1 * c1 + c2 * c2 + c3 * c3;
  890. break;
  891. }
  892. case eCSSKeyword_skew: {
  893. MOZ_ASSERT(a1->Count() == 2 || a1->Count() == 3, "unexpected count");
  894. MOZ_ASSERT(a2->Count() == 2 || a2->Count() == 3, "unexpected count");
  895. const nsCSSValue zero(0.0f, eCSSUnit_Radian);
  896. nsCSSValue x, y;
  897. AddCSSValueAngle(1.0, a2->Item(1), -1.0, a1->Item(1), x);
  898. AddCSSValueAngle(1.0, a2->Count() == 3 ? a2->Item(2) : zero,
  899. -1.0, a1->Count() == 3 ? a1->Item(2) : zero,
  900. y);
  901. distance = x.GetAngleValueInRadians() * x.GetAngleValueInRadians() +
  902. y.GetAngleValueInRadians() * y.GetAngleValueInRadians();
  903. break;
  904. }
  905. case eCSSKeyword_skewx:
  906. case eCSSKeyword_skewy:
  907. case eCSSKeyword_rotate:
  908. case eCSSKeyword_rotatex:
  909. case eCSSKeyword_rotatey:
  910. case eCSSKeyword_rotatez: {
  911. MOZ_ASSERT(a1->Count() == 2, "unexpected count");
  912. MOZ_ASSERT(a2->Count() == 2, "unexpected count");
  913. nsCSSValue angle;
  914. AddCSSValueAngle(1.0, a2->Item(1), -1.0, a1->Item(1), angle);
  915. distance = angle.GetAngleValueInRadians() *
  916. angle.GetAngleValueInRadians();
  917. break;
  918. }
  919. case eCSSKeyword_rotate3d: {
  920. MOZ_ASSERT(a1->Count() == 5, "unexpected count");
  921. MOZ_ASSERT(a2->Count() == 5, "unexpected count");
  922. Point3D vector1(a1->Item(1).GetFloatValue(),
  923. a1->Item(2).GetFloatValue(),
  924. a1->Item(3).GetFloatValue());
  925. vector1.Normalize();
  926. Point3D vector2(a2->Item(1).GetFloatValue(),
  927. a2->Item(2).GetFloatValue(),
  928. a2->Item(3).GetFloatValue());
  929. vector2.Normalize();
  930. if (vector1 == vector2) {
  931. // Handle rotate3d with matched (normalized) vectors.
  932. nsCSSValue angle;
  933. AddCSSValueAngle(1.0, a2->Item(4), -1.0, a1->Item(4), angle);
  934. distance = angle.GetAngleValueInRadians() *
  935. angle.GetAngleValueInRadians();
  936. } else {
  937. // Use quaternion vectors to get the angle difference. Both q1 and q2
  938. // are unit vectors, so we can get their angle difference by
  939. // cos(theta/2) = (q1 dot q2) / (|q1| * |q2|) = q1 dot q2.
  940. gfxQuaternion q1(vector1, a1->Item(4).GetAngleValueInRadians());
  941. gfxQuaternion q2(vector2, a2->Item(4).GetAngleValueInRadians());
  942. distance = 2.0 * acos(clamped(q1.DotProduct(q2), -1.0, 1.0));
  943. distance = distance * distance;
  944. }
  945. break;
  946. }
  947. case eCSSKeyword_perspective: {
  948. MOZ_ASSERT(a1->Count() == 2, "unexpected count");
  949. MOZ_ASSERT(a2->Count() == 2, "unexpected count");
  950. // We convert a perspective function into an equivalent matrix3d, and
  951. // then do matrix decomposition to get the distance.
  952. // Why don't we just subtract one perspective depth from the other?
  953. // I think it's better to follow the logic of our interpolation,
  954. // which does linear interpolation between two decomposed perspective
  955. // vectors.
  956. // e.g.
  957. // Do interpolation between perspective(100px) and perspective(1000px).
  958. // 1) Convert them into matrix3d, and then do matrix decomposition:
  959. // perspective vector 1: perspective(0, 0, -1/100, 1);
  960. // perspective vector 2: perspective(0, 0, -1/1000, 1);
  961. // 2) Do linear interpolation between these two vectors.
  962. // Therefore, we use the same rule to get the distance as what we do for
  963. // matrix3d.
  964. using nsStyleTransformMatrix::ApplyPerspectiveToMatrix;
  965. Matrix4x4 m1;
  966. ApplyPerspectiveToMatrix(m1, a1->Item(1).GetFloatValue());
  967. Matrix4x4 m2;
  968. ApplyPerspectiveToMatrix(m2, a2->Item(1).GetFloatValue());
  969. distance = ComputeTransform3DMatrixDistance(m1, m2);
  970. break;
  971. }
  972. case eCSSKeyword_matrix: {
  973. MOZ_ASSERT(a1->Count() == 7, "unexpected count");
  974. MOZ_ASSERT(a2->Count() == 7, "unexpected count");
  975. distance = ComputeTransform2DMatrixDistance(
  976. nsStyleTransformMatrix::CSSValueArrayTo2DMatrix(a1),
  977. nsStyleTransformMatrix::CSSValueArrayTo2DMatrix(a2));
  978. break;
  979. }
  980. case eCSSKeyword_matrix3d: {
  981. MOZ_ASSERT(a1->Count() == 17, "unexpected count");
  982. MOZ_ASSERT(a2->Count() == 17, "unexpected count");
  983. distance = ComputeTransform3DMatrixDistance(
  984. nsStyleTransformMatrix::CSSValueArrayTo3DMatrix(a1),
  985. nsStyleTransformMatrix::CSSValueArrayTo3DMatrix(a2));
  986. break;
  987. }
  988. case eCSSKeyword_interpolatematrix:
  989. default:
  990. MOZ_ASSERT_UNREACHABLE("Unsupported transform function");
  991. break;
  992. }
  993. return distance;
  994. }
  995. static double
  996. ComputeTransformListDistance(const nsCSSValueList* aList1,
  997. const nsCSSValueList* aList2)
  998. {
  999. MOZ_ASSERT(aList1, "aList1 should be non-null.");
  1000. MOZ_ASSERT(aList2, "aList2 should be non-null.");
  1001. double distance = 0.0;
  1002. do {
  1003. distance += ComputeTransformDistance(aList1->mValue.GetArrayValue(),
  1004. aList2->mValue.GetArrayValue());
  1005. aList1 = aList1->mNext;
  1006. aList2 = aList2->mNext;
  1007. MOZ_ASSERT(!aList1 == !aList2,
  1008. "aList1 and aList2 should have the same length.");
  1009. } while (aList1);
  1010. return sqrt(distance);
  1011. }
  1012. static double
  1013. ComputeMismatchedTransfromListDistance(const nsCSSValueList* aList1,
  1014. const nsCSSValueList* aList2,
  1015. nsStyleContext* aStyleContext)
  1016. {
  1017. // We need nsStyleContext and nsPresContext to compute calc() values while
  1018. // processing the translate part of transforms.
  1019. if (!aStyleContext) {
  1020. return 0.0;
  1021. }
  1022. RuleNodeCacheConditions dontCare;
  1023. bool dontCareBool;
  1024. nsStyleTransformMatrix::TransformReferenceBox emptyRefBox;
  1025. Matrix4x4 m1 = nsStyleTransformMatrix::ReadTransforms(
  1026. aList1,
  1027. aStyleContext,
  1028. aStyleContext->PresContext(),
  1029. dontCare,
  1030. emptyRefBox,
  1031. nsPresContext::AppUnitsPerCSSPixel(),
  1032. &dontCareBool);
  1033. Matrix4x4 m2 = nsStyleTransformMatrix::ReadTransforms(
  1034. aList2,
  1035. aStyleContext,
  1036. aStyleContext->PresContext(),
  1037. dontCare,
  1038. emptyRefBox,
  1039. nsPresContext::AppUnitsPerCSSPixel(),
  1040. &dontCareBool);
  1041. return sqrt(ComputeTransform3DMatrixDistance(m1, m2));
  1042. }
  1043. bool
  1044. StyleAnimationValue::ComputeDistance(nsCSSPropertyID aProperty,
  1045. const StyleAnimationValue& aStartValue,
  1046. const StyleAnimationValue& aEndValue,
  1047. nsStyleContext* aStyleContext,
  1048. double& aDistance)
  1049. {
  1050. Unit commonUnit =
  1051. GetCommonUnit(aProperty, aStartValue.GetUnit(), aEndValue.GetUnit());
  1052. switch (commonUnit) {
  1053. case eUnit_Null:
  1054. case eUnit_Auto:
  1055. case eUnit_None:
  1056. case eUnit_Normal:
  1057. case eUnit_UnparsedString:
  1058. case eUnit_URL:
  1059. case eUnit_DiscreteCSSValue:
  1060. return false;
  1061. case eUnit_Enumerated:
  1062. switch (aProperty) {
  1063. case eCSSProperty_font_stretch: {
  1064. // just like eUnit_Integer.
  1065. int32_t startInt = aStartValue.GetIntValue();
  1066. int32_t endInt = aEndValue.GetIntValue();
  1067. aDistance = Abs(endInt - startInt);
  1068. return true;
  1069. }
  1070. default:
  1071. return false;
  1072. }
  1073. case eUnit_Visibility: {
  1074. int32_t startEnum = aStartValue.GetIntValue();
  1075. int32_t endEnum = aEndValue.GetIntValue();
  1076. if (startEnum == endEnum) {
  1077. aDistance = 0;
  1078. return true;
  1079. }
  1080. if ((startEnum == NS_STYLE_VISIBILITY_VISIBLE) ==
  1081. (endEnum == NS_STYLE_VISIBILITY_VISIBLE)) {
  1082. return false;
  1083. }
  1084. aDistance = 1;
  1085. return true;
  1086. }
  1087. case eUnit_Integer: {
  1088. int32_t startInt = aStartValue.GetIntValue();
  1089. int32_t endInt = aEndValue.GetIntValue();
  1090. aDistance = Abs(double(endInt) - double(startInt));
  1091. return true;
  1092. }
  1093. case eUnit_Coord: {
  1094. nscoord startCoord = aStartValue.GetCoordValue();
  1095. nscoord endCoord = aEndValue.GetCoordValue();
  1096. aDistance = Abs(double(endCoord) - double(startCoord));
  1097. return true;
  1098. }
  1099. case eUnit_Percent: {
  1100. float startPct = aStartValue.GetPercentValue();
  1101. float endPct = aEndValue.GetPercentValue();
  1102. aDistance = Abs(double(endPct) - double(startPct));
  1103. return true;
  1104. }
  1105. case eUnit_Float: {
  1106. float startFloat = aStartValue.GetFloatValue();
  1107. float endFloat = aEndValue.GetFloatValue();
  1108. aDistance = Abs(double(endFloat) - double(startFloat));
  1109. return true;
  1110. }
  1111. case eUnit_Color: {
  1112. aDistance = ComputeColorDistance(ExtractColor(aStartValue),
  1113. ExtractColor(aEndValue));
  1114. return true;
  1115. }
  1116. case eUnit_CurrentColor: {
  1117. aDistance = 0;
  1118. return true;
  1119. }
  1120. case eUnit_ComplexColor: {
  1121. ComplexColorData color1 = ExtractComplexColor(aStartValue);
  1122. ComplexColorData color2 = ExtractComplexColor(aEndValue);
  1123. // Common case is interpolating between a color and a currentcolor
  1124. if (color1.IsNumericColor() && color2.IsCurrentColor()) {
  1125. double dist = ComputeColorDistance(color1.mColor, NS_RGBA(0, 0, 0, 0));
  1126. aDistance = sqrt(dist * dist + 1);
  1127. return true;
  1128. }
  1129. if (color1.IsCurrentColor() && color2.IsNumericColor()) {
  1130. double dist = ComputeColorDistance(NS_RGBA(0, 0, 0, 0), color2.mColor);
  1131. aDistance = sqrt(dist * dist + 1);
  1132. return true;
  1133. }
  1134. // If we ever reach here, we may want to use the code in
  1135. // bug 1299741 comment 79 to compute it.
  1136. MOZ_ASSERT_UNREACHABLE("We shouldn't get here as we only call "
  1137. "ComputeDistance on pre-interpolation values");
  1138. aDistance = 0.0;
  1139. return true;
  1140. }
  1141. case eUnit_Calc: {
  1142. PixelCalcValue v1 = ExtractCalcValue(aStartValue);
  1143. PixelCalcValue v2 = ExtractCalcValue(aEndValue);
  1144. float difflen = v2.mLength - v1.mLength;
  1145. float diffpct = v2.mPercent - v1.mPercent;
  1146. aDistance = sqrt(difflen * difflen + diffpct * diffpct);
  1147. return true;
  1148. }
  1149. case eUnit_ObjectPosition: {
  1150. const nsCSSValue* position1 = aStartValue.GetCSSValueValue();
  1151. const nsCSSValue* position2 = aEndValue.GetCSSValueValue();
  1152. double squareDistance =
  1153. CalcPositionSquareDistance(*position1,
  1154. *position2);
  1155. aDistance = sqrt(squareDistance);
  1156. return true;
  1157. }
  1158. case eUnit_CSSValuePair: {
  1159. const nsCSSValuePair *pair1 = aStartValue.GetCSSValuePairValue();
  1160. const nsCSSValuePair *pair2 = aEndValue.GetCSSValuePairValue();
  1161. nsCSSUnit unit[2];
  1162. unit[0] = GetCommonUnit(aProperty, pair1->mXValue.GetUnit(),
  1163. pair2->mXValue.GetUnit());
  1164. unit[1] = GetCommonUnit(aProperty, pair1->mYValue.GetUnit(),
  1165. pair2->mYValue.GetUnit());
  1166. if (unit[0] == eCSSUnit_Null || unit[1] == eCSSUnit_Null ||
  1167. unit[0] == eCSSUnit_URL || unit[0] == eCSSUnit_Enumerated) {
  1168. return false;
  1169. }
  1170. double squareDistance = 0.0;
  1171. static nsCSSValue nsCSSValuePair::* const pairValues[2] = {
  1172. &nsCSSValuePair::mXValue, &nsCSSValuePair::mYValue
  1173. };
  1174. for (uint32_t i = 0; i < 2; ++i) {
  1175. nsCSSValue nsCSSValuePair::*member = pairValues[i];
  1176. double diffsquared;
  1177. switch (unit[i]) {
  1178. case eCSSUnit_Pixel: {
  1179. float diff = (pair1->*member).GetFloatValue() -
  1180. (pair2->*member).GetFloatValue();
  1181. diffsquared = diff * diff;
  1182. break;
  1183. }
  1184. case eCSSUnit_Percent: {
  1185. float diff = (pair1->*member).GetPercentValue() -
  1186. (pair2->*member).GetPercentValue();
  1187. diffsquared = diff * diff;
  1188. break;
  1189. }
  1190. case eCSSUnit_Calc: {
  1191. PixelCalcValue v1 = ExtractCalcValue(pair1->*member);
  1192. PixelCalcValue v2 = ExtractCalcValue(pair2->*member);
  1193. float difflen = v2.mLength - v1.mLength;
  1194. float diffpct = v2.mPercent - v1.mPercent;
  1195. diffsquared = difflen * difflen + diffpct * diffpct;
  1196. break;
  1197. }
  1198. default:
  1199. MOZ_ASSERT(false, "unexpected unit");
  1200. return false;
  1201. }
  1202. squareDistance += diffsquared;
  1203. }
  1204. aDistance = sqrt(squareDistance);
  1205. return true;
  1206. }
  1207. case eUnit_CSSValueTriplet: {
  1208. const nsCSSValueTriplet *triplet1 = aStartValue.GetCSSValueTripletValue();
  1209. const nsCSSValueTriplet *triplet2 = aEndValue.GetCSSValueTripletValue();
  1210. nsCSSUnit unit[3];
  1211. unit[0] = GetCommonUnit(aProperty, triplet1->mXValue.GetUnit(),
  1212. triplet2->mXValue.GetUnit());
  1213. unit[1] = GetCommonUnit(aProperty, triplet1->mYValue.GetUnit(),
  1214. triplet2->mYValue.GetUnit());
  1215. unit[2] = GetCommonUnit(aProperty, triplet1->mZValue.GetUnit(),
  1216. triplet2->mZValue.GetUnit());
  1217. if (unit[0] == eCSSUnit_Null || unit[1] == eCSSUnit_Null ||
  1218. unit[2] == eCSSUnit_Null) {
  1219. return false;
  1220. }
  1221. double squareDistance = 0.0;
  1222. static nsCSSValue nsCSSValueTriplet::* const pairValues[3] = {
  1223. &nsCSSValueTriplet::mXValue, &nsCSSValueTriplet::mYValue, &nsCSSValueTriplet::mZValue
  1224. };
  1225. for (uint32_t i = 0; i < 3; ++i) {
  1226. nsCSSValue nsCSSValueTriplet::*member = pairValues[i];
  1227. double diffsquared;
  1228. switch (unit[i]) {
  1229. case eCSSUnit_Pixel: {
  1230. float diff = (triplet1->*member).GetFloatValue() -
  1231. (triplet2->*member).GetFloatValue();
  1232. diffsquared = diff * diff;
  1233. break;
  1234. }
  1235. case eCSSUnit_Percent: {
  1236. float diff = (triplet1->*member).GetPercentValue() -
  1237. (triplet2->*member).GetPercentValue();
  1238. diffsquared = diff * diff;
  1239. break;
  1240. }
  1241. case eCSSUnit_Calc: {
  1242. PixelCalcValue v1 = ExtractCalcValue(triplet1->*member);
  1243. PixelCalcValue v2 = ExtractCalcValue(triplet2->*member);
  1244. float difflen = v2.mLength - v1.mLength;
  1245. float diffpct = v2.mPercent - v1.mPercent;
  1246. diffsquared = difflen * difflen + diffpct * diffpct;
  1247. break;
  1248. }
  1249. case eCSSUnit_Null:
  1250. diffsquared = 0;
  1251. break;
  1252. default:
  1253. MOZ_ASSERT(false, "unexpected unit");
  1254. return false;
  1255. }
  1256. squareDistance += diffsquared;
  1257. }
  1258. aDistance = sqrt(squareDistance);
  1259. return true;
  1260. }
  1261. case eUnit_CSSRect: {
  1262. const nsCSSRect *rect1 = aStartValue.GetCSSRectValue();
  1263. const nsCSSRect *rect2 = aEndValue.GetCSSRectValue();
  1264. if (rect1->mTop.GetUnit() != rect2->mTop.GetUnit() ||
  1265. rect1->mRight.GetUnit() != rect2->mRight.GetUnit() ||
  1266. rect1->mBottom.GetUnit() != rect2->mBottom.GetUnit() ||
  1267. rect1->mLeft.GetUnit() != rect2->mLeft.GetUnit()) {
  1268. // At least until we have calc()
  1269. return false;
  1270. }
  1271. double squareDistance = 0.0;
  1272. for (uint32_t i = 0; i < ArrayLength(nsCSSRect::sides); ++i) {
  1273. nsCSSValue nsCSSRect::*member = nsCSSRect::sides[i];
  1274. MOZ_ASSERT((rect1->*member).GetUnit() == (rect2->*member).GetUnit(),
  1275. "should have returned above");
  1276. double diff;
  1277. switch ((rect1->*member).GetUnit()) {
  1278. case eCSSUnit_Pixel:
  1279. diff = (rect1->*member).GetFloatValue() -
  1280. (rect2->*member).GetFloatValue();
  1281. break;
  1282. case eCSSUnit_Auto:
  1283. diff = 0;
  1284. break;
  1285. default:
  1286. MOZ_ASSERT(false, "unexpected unit");
  1287. return false;
  1288. }
  1289. squareDistance += diff * diff;
  1290. }
  1291. aDistance = sqrt(squareDistance);
  1292. return true;
  1293. }
  1294. case eUnit_Dasharray: {
  1295. // NOTE: This produces results on substantially different scales
  1296. // for length values and percentage values, which might even be
  1297. // mixed in the same property value. This means the result isn't
  1298. // particularly useful for paced animation.
  1299. // Call AddWeighted to make us lists of the same length.
  1300. StyleAnimationValue normValue1, normValue2;
  1301. if (!AddWeighted(aProperty, 1.0, aStartValue, 0.0, aEndValue,
  1302. normValue1) ||
  1303. !AddWeighted(aProperty, 0.0, aStartValue, 1.0, aEndValue,
  1304. normValue2)) {
  1305. return false;
  1306. }
  1307. double squareDistance = 0.0;
  1308. const nsCSSValueList *list1 = normValue1.GetCSSValueListValue();
  1309. const nsCSSValueList *list2 = normValue2.GetCSSValueListValue();
  1310. MOZ_ASSERT(!list1 == !list2, "lists should be same length");
  1311. while (list1) {
  1312. const nsCSSValue &val1 = list1->mValue;
  1313. const nsCSSValue &val2 = list2->mValue;
  1314. MOZ_ASSERT(val1.GetUnit() == val2.GetUnit(),
  1315. "unit match should be assured by AddWeighted");
  1316. double diff;
  1317. switch (val1.GetUnit()) {
  1318. case eCSSUnit_Percent:
  1319. diff = val1.GetPercentValue() - val2.GetPercentValue();
  1320. break;
  1321. case eCSSUnit_Number:
  1322. diff = val1.GetFloatValue() - val2.GetFloatValue();
  1323. break;
  1324. default:
  1325. MOZ_ASSERT(false, "unexpected unit");
  1326. return false;
  1327. }
  1328. squareDistance += diff * diff;
  1329. list1 = list1->mNext;
  1330. list2 = list2->mNext;
  1331. MOZ_ASSERT(!list1 == !list2, "lists should be same length");
  1332. }
  1333. aDistance = sqrt(squareDistance);
  1334. return true;
  1335. }
  1336. case eUnit_Shadow: {
  1337. // Call AddWeighted to make us lists of the same length.
  1338. StyleAnimationValue normValue1, normValue2;
  1339. if (!AddWeighted(aProperty, 1.0, aStartValue, 0.0, aEndValue,
  1340. normValue1) ||
  1341. !AddWeighted(aProperty, 0.0, aStartValue, 1.0, aEndValue,
  1342. normValue2)) {
  1343. return false;
  1344. }
  1345. const nsCSSValueList *shadow1 = normValue1.GetCSSValueListValue();
  1346. const nsCSSValueList *shadow2 = normValue2.GetCSSValueListValue();
  1347. double squareDistance = 0.0;
  1348. MOZ_ASSERT(!shadow1 == !shadow2, "lists should be same length");
  1349. while (shadow1) {
  1350. nsCSSValue::Array *array1 = shadow1->mValue.GetArrayValue();
  1351. nsCSSValue::Array *array2 = shadow2->mValue.GetArrayValue();
  1352. for (size_t i = 0; i < 4; ++i) {
  1353. MOZ_ASSERT(array1->Item(i).GetUnit() == eCSSUnit_Pixel,
  1354. "unexpected unit");
  1355. MOZ_ASSERT(array2->Item(i).GetUnit() == eCSSUnit_Pixel,
  1356. "unexpected unit");
  1357. double diff = array1->Item(i).GetFloatValue() -
  1358. array2->Item(i).GetFloatValue();
  1359. squareDistance += diff * diff;
  1360. }
  1361. const nsCSSValue &color1 = array1->Item(4);
  1362. const nsCSSValue &color2 = array2->Item(4);
  1363. #ifdef DEBUG
  1364. {
  1365. const nsCSSValue &inset1 = array1->Item(5);
  1366. const nsCSSValue &inset2 = array2->Item(5);
  1367. // There are only two possible states of the inset value:
  1368. // (1) GetUnit() == eCSSUnit_Null
  1369. // (2) GetUnit() == eCSSUnit_Enumerated &&
  1370. // GetIntValue() == NS_STYLE_BOX_SHADOW_INSET
  1371. MOZ_ASSERT(((color1.IsNumericColorUnit() &&
  1372. color2.IsNumericColorUnit()) ||
  1373. (color1.GetUnit() == color2.GetUnit())) &&
  1374. inset1 == inset2,
  1375. "AddWeighted should have failed");
  1376. }
  1377. #endif
  1378. if (color1.GetUnit() != eCSSUnit_Null) {
  1379. double colorDistance = ComputeColorDistance(color1.GetColorValue(),
  1380. color2.GetColorValue());
  1381. squareDistance += colorDistance * colorDistance;
  1382. }
  1383. shadow1 = shadow1->mNext;
  1384. shadow2 = shadow2->mNext;
  1385. MOZ_ASSERT(!shadow1 == !shadow2, "lists should be same length");
  1386. }
  1387. aDistance = sqrt(squareDistance);
  1388. return true;
  1389. }
  1390. case eUnit_Shape:
  1391. aDistance = ComputeShapeDistance(aProperty,
  1392. aStartValue.GetCSSValueArrayValue(),
  1393. aEndValue.GetCSSValueArrayValue());
  1394. return true;
  1395. case eUnit_Filter:
  1396. // Bug 1286151: Support paced animations for filter function
  1397. // interpolation.
  1398. return false;
  1399. case eUnit_Transform: {
  1400. // FIXME: We don't have an official spec to define the distance of
  1401. // two transform lists, but paced spacing (defined in Web Animations API)
  1402. // needs this, so we implement this according to the concept of the
  1403. // interpolation of two transform lists.
  1404. // Issue: https://www.w3.org/TR/web-animations-1/#issue-789f9fd1
  1405. const nsCSSValueList* list1 =
  1406. aStartValue.GetCSSValueSharedListValue()->mHead;
  1407. const nsCSSValueList* list2 =
  1408. aEndValue.GetCSSValueSharedListValue()->mHead;
  1409. MOZ_ASSERT(list1);
  1410. MOZ_ASSERT(list2);
  1411. if (list1->mValue.GetUnit() == eCSSUnit_None &&
  1412. list2->mValue.GetUnit() == eCSSUnit_None) {
  1413. // Both none, nothing happens.
  1414. aDistance = 0.0;
  1415. } else if (list1->mValue.GetUnit() == eCSSUnit_None) {
  1416. nsAutoPtr<nsCSSValueList> none(AddTransformLists(0, list2, 0, list2));
  1417. aDistance = ComputeTransformListDistance(none, list2);
  1418. } else if (list2->mValue.GetUnit() == eCSSUnit_None) {
  1419. nsAutoPtr<nsCSSValueList> none(AddTransformLists(0, list1, 0, list1));
  1420. aDistance = ComputeTransformListDistance(list1, none);
  1421. } else {
  1422. const nsCSSValueList *item1 = list1, *item2 = list2;
  1423. do {
  1424. nsCSSKeyword func1 = nsStyleTransformMatrix::TransformFunctionOf(
  1425. item1->mValue.GetArrayValue());
  1426. nsCSSKeyword func2 = nsStyleTransformMatrix::TransformFunctionOf(
  1427. item2->mValue.GetArrayValue());
  1428. if (!TransformFunctionsMatch(func1, func2)) {
  1429. break;
  1430. }
  1431. item1 = item1->mNext;
  1432. item2 = item2->mNext;
  1433. } while (item1 && item2);
  1434. if (item1 || item2) {
  1435. // Either the transform function types don't match or
  1436. // the lengths don't match.
  1437. aDistance =
  1438. ComputeMismatchedTransfromListDistance(list1, list2, aStyleContext);
  1439. } else {
  1440. aDistance = ComputeTransformListDistance(list1, list2);
  1441. }
  1442. }
  1443. return true;
  1444. }
  1445. case eUnit_BackgroundPositionCoord: {
  1446. const nsCSSValueList *position1 = aStartValue.GetCSSValueListValue();
  1447. const nsCSSValueList *position2 = aEndValue.GetCSSValueListValue();
  1448. double squareDistance = 0.0;
  1449. MOZ_ASSERT(!position1 == !position2, "lists should be same length");
  1450. while (position1 && position2) {
  1451. squareDistance += CalcPositionCoordSquareDistance(position1->mValue,
  1452. position2->mValue);
  1453. position1 = position1->mNext;
  1454. position2 = position2->mNext;
  1455. }
  1456. // fail if lists differ in length.
  1457. if (position1 || position2) {
  1458. return false;
  1459. }
  1460. aDistance = sqrt(squareDistance);
  1461. return true;
  1462. }
  1463. case eUnit_CSSValuePairList: {
  1464. const nsCSSValuePairList *list1 = aStartValue.GetCSSValuePairListValue();
  1465. const nsCSSValuePairList *list2 = aEndValue.GetCSSValuePairListValue();
  1466. double squareDistance = 0.0;
  1467. do {
  1468. static nsCSSValue nsCSSValuePairList::* const pairListValues[] = {
  1469. &nsCSSValuePairList::mXValue,
  1470. &nsCSSValuePairList::mYValue,
  1471. };
  1472. for (uint32_t i = 0; i < ArrayLength(pairListValues); ++i) {
  1473. const nsCSSValue &v1 = list1->*(pairListValues[i]);
  1474. const nsCSSValue &v2 = list2->*(pairListValues[i]);
  1475. nsCSSUnit unit =
  1476. GetCommonUnit(aProperty, v1.GetUnit(), v2.GetUnit());
  1477. if (unit == eCSSUnit_Null) {
  1478. return false;
  1479. }
  1480. double diffsquared = 0.0;
  1481. switch (unit) {
  1482. case eCSSUnit_Pixel: {
  1483. float diff = v1.GetFloatValue() - v2.GetFloatValue();
  1484. diffsquared = diff * diff;
  1485. break;
  1486. }
  1487. case eCSSUnit_Percent: {
  1488. float diff = v1.GetPercentValue() - v2.GetPercentValue();
  1489. diffsquared = diff * diff;
  1490. break;
  1491. }
  1492. case eCSSUnit_Calc: {
  1493. PixelCalcValue val1 = ExtractCalcValue(v1);
  1494. PixelCalcValue val2 = ExtractCalcValue(v2);
  1495. float difflen = val2.mLength - val1.mLength;
  1496. float diffpct = val2.mPercent - val1.mPercent;
  1497. diffsquared = difflen * difflen + diffpct * diffpct;
  1498. break;
  1499. }
  1500. default:
  1501. if (v1 != v2) {
  1502. return false;
  1503. }
  1504. break;
  1505. }
  1506. squareDistance += diffsquared;
  1507. }
  1508. list1 = list1->mNext;
  1509. list2 = list2->mNext;
  1510. } while (list1 && list2);
  1511. if (list1 || list2) {
  1512. // We can't interpolate lists of different lengths.
  1513. return false;
  1514. }
  1515. aDistance = sqrt(squareDistance);
  1516. return true;
  1517. }
  1518. }
  1519. MOZ_ASSERT(false, "Can't compute distance using the given common unit");
  1520. return false;
  1521. }
  1522. static inline void
  1523. AddCSSValueNumber(double aCoeff1, const nsCSSValue &aValue1,
  1524. double aCoeff2, const nsCSSValue &aValue2,
  1525. nsCSSValue &aResult, uint32_t aValueRestrictions = 0)
  1526. {
  1527. MOZ_ASSERT(aValue1.GetUnit() == eCSSUnit_Number, "unexpected unit");
  1528. MOZ_ASSERT(aValue2.GetUnit() == eCSSUnit_Number, "unexpected unit");
  1529. aResult.SetFloatValue(RestrictValue(aValueRestrictions,
  1530. aCoeff1 * aValue1.GetFloatValue() +
  1531. aCoeff2 * aValue2.GetFloatValue()),
  1532. eCSSUnit_Number);
  1533. }
  1534. static inline float
  1535. GetNumberOrPercent(const nsCSSValue &aValue)
  1536. {
  1537. nsCSSUnit unit = aValue.GetUnit();
  1538. MOZ_ASSERT(unit == eCSSUnit_Number || unit == eCSSUnit_Percent,
  1539. "unexpected unit");
  1540. return (unit == eCSSUnit_Number) ?
  1541. aValue.GetFloatValue() : aValue.GetPercentValue();
  1542. }
  1543. static inline void
  1544. AddCSSValuePercentNumber(const uint32_t aValueRestrictions,
  1545. double aCoeff1, const nsCSSValue &aValue1,
  1546. double aCoeff2, const nsCSSValue &aValue2,
  1547. nsCSSValue &aResult, float aInitialVal)
  1548. {
  1549. float n1 = GetNumberOrPercent(aValue1);
  1550. float n2 = GetNumberOrPercent(aValue2);
  1551. // Rather than interpolating aValue1 and aValue2 directly, we
  1552. // interpolate their *distances from aInitialVal* (the initial value,
  1553. // which is either 1 or 0 for "filter" functions). This matters in
  1554. // cases where aInitialVal is nonzero and the coefficients don't add
  1555. // up to 1. For example, if initialVal is 1, aCoeff1 is 0.5, and
  1556. // aCoeff2 is 0, then we'll return the value halfway between 1 and
  1557. // aValue1, rather than the value halfway between 0 and aValue1.
  1558. // Note that we do something similar in AddTransformScale().
  1559. float result = (n1 - aInitialVal) * aCoeff1 + (n2 - aInitialVal) * aCoeff2;
  1560. aResult.SetFloatValue(RestrictValue(aValueRestrictions, result + aInitialVal),
  1561. eCSSUnit_Number);
  1562. }
  1563. enum class ColorAdditionType {
  1564. Clamped, // Clamp each color channel after adding.
  1565. Unclamped // Do not clamp color channels after adding.
  1566. };
  1567. // Unclamped AddWeightedColors.
  1568. static RGBAColorData
  1569. AddWeightedColors(double aCoeff1, const RGBAColorData& aValue1,
  1570. double aCoeff2, const RGBAColorData& aValue2)
  1571. {
  1572. float factor1 = aValue1.mA * aCoeff1;
  1573. float factor2 = aValue2.mA * aCoeff2;
  1574. float resultA = factor1 + factor2;
  1575. if (resultA <= 0.0) {
  1576. return {0, 0, 0, 0};
  1577. }
  1578. if (resultA > 1.0) {
  1579. resultA = 1.0;
  1580. }
  1581. float resultFactor = 1.0f / resultA;
  1582. return RGBAColorData(
  1583. (aValue1.mR * factor1 + aValue2.mR * factor2) * resultFactor,
  1584. (aValue1.mG * factor1 + aValue2.mG * factor2) * resultFactor,
  1585. (aValue1.mB * factor1 + aValue2.mB * factor2) * resultFactor,
  1586. resultA);
  1587. }
  1588. // Multiplies |aValue| color by |aDilutionRation|.
  1589. static nscolor
  1590. DiluteColor(const RGBAColorData& aValue, double aDilutionRatio)
  1591. {
  1592. MOZ_ASSERT(aDilutionRatio >= 0.0 && aDilutionRatio <= 1.0,
  1593. "Dilution ratio should be in [0, 1]");
  1594. float resultA = aValue.mA * aDilutionRatio;
  1595. return resultA <= 0.0 ? NS_RGBA(0, 0, 0, 0)
  1596. : aValue.WithAlpha(resultA).ToColor();
  1597. }
  1598. // Clamped AddWeightedColors.
  1599. static nscolor
  1600. AddWeightedColorsAndClamp(double aCoeff1, const RGBAColorData& aValue1,
  1601. double aCoeff2, const RGBAColorData& aValue2)
  1602. {
  1603. // We are using AddWeighted() with a zero aCoeff2 for colors to
  1604. // pretend AddWeighted() against transparent color, i.e. rgba(0, 0, 0, 0).
  1605. // But unpremultiplication in AddWeightedColors() does not work well
  1606. // for such cases, so we use another function named DiluteColor() which
  1607. // has a similar logic to AddWeightedColors().
  1608. return aCoeff2 == 0.0
  1609. ? DiluteColor(aValue1, aCoeff1)
  1610. : AddWeightedColors(aCoeff1, aValue1, aCoeff2, aValue2).ToColor();
  1611. }
  1612. void
  1613. AppendToCSSValueList(UniquePtr<nsCSSValueList>& aHead,
  1614. UniquePtr<nsCSSValueList>&& aValueToAppend,
  1615. nsCSSValueList** aTail)
  1616. {
  1617. MOZ_ASSERT(!aHead == !*aTail,
  1618. "Can't have head w/o tail, & vice versa");
  1619. if (!aHead) {
  1620. aHead = Move(aValueToAppend);
  1621. *aTail = aHead.get();
  1622. } else {
  1623. (*aTail) = (*aTail)->mNext = aValueToAppend.release();
  1624. }
  1625. }
  1626. static UniquePtr<nsCSSValueList>
  1627. AddWeightedShadowItems(double aCoeff1, const nsCSSValue &aValue1,
  1628. double aCoeff2, const nsCSSValue &aValue2,
  1629. ColorAdditionType aColorAdditionType)
  1630. {
  1631. // X, Y, Radius, Spread, Color, Inset
  1632. MOZ_ASSERT(aValue1.GetUnit() == eCSSUnit_Array,
  1633. "wrong unit");
  1634. MOZ_ASSERT(aValue2.GetUnit() == eCSSUnit_Array,
  1635. "wrong unit");
  1636. nsCSSValue::Array *array1 = aValue1.GetArrayValue();
  1637. nsCSSValue::Array *array2 = aValue2.GetArrayValue();
  1638. RefPtr<nsCSSValue::Array> resultArray = nsCSSValue::Array::Create(6);
  1639. for (size_t i = 0; i < 4; ++i) {
  1640. AddCSSValuePixel(aCoeff1, array1->Item(i), aCoeff2, array2->Item(i),
  1641. resultArray->Item(i),
  1642. // blur radius must be nonnegative
  1643. (i == 2) ? CSS_PROPERTY_VALUE_NONNEGATIVE : 0);
  1644. }
  1645. const nsCSSValue& colorValue1 = array1->Item(4);
  1646. const nsCSSValue& colorValue2 = array2->Item(4);
  1647. const nsCSSValue& inset1 = array1->Item(5);
  1648. const nsCSSValue& inset2 = array2->Item(5);
  1649. if ((colorValue1.GetUnit() != colorValue2.GetUnit() &&
  1650. (!colorValue1.IsNumericColorUnit() ||
  1651. !colorValue2.IsNumericColorUnit())) ||
  1652. inset1.GetUnit() != inset2.GetUnit()) {
  1653. // We don't know how to animate between color and no-color, or
  1654. // between inset and not-inset.
  1655. // NOTE: In case when both colors' units are eCSSUnit_Null, that means
  1656. // neither color value was specified, so we can interpolate.
  1657. return nullptr;
  1658. }
  1659. if (colorValue1.GetUnit() != eCSSUnit_Null) {
  1660. RGBAColorData color1 = ExtractColor(colorValue1);
  1661. RGBAColorData color2 = ExtractColor(colorValue2);
  1662. if (aColorAdditionType == ColorAdditionType::Clamped) {
  1663. resultArray->Item(4).SetColorValue(
  1664. AddWeightedColorsAndClamp(aCoeff1, color1, aCoeff2, color2));
  1665. } else {
  1666. resultArray->Item(4).SetRGBAColorValue(
  1667. AddWeightedColors(aCoeff1, color1, aCoeff2, color2));
  1668. }
  1669. }
  1670. MOZ_ASSERT(inset1 == inset2, "should match");
  1671. resultArray->Item(5) = inset1;
  1672. auto resultItem = MakeUnique<nsCSSValueList>();
  1673. resultItem->mValue.SetArrayValue(resultArray, eCSSUnit_Array);
  1674. return resultItem;
  1675. }
  1676. static void
  1677. AddTransformScale(double aCoeff1, const nsCSSValue &aValue1,
  1678. double aCoeff2, const nsCSSValue &aValue2,
  1679. nsCSSValue &aResult)
  1680. {
  1681. // Handle scale, and the two matrix components where identity is 1, by
  1682. // subtracting 1, multiplying by the coefficients, and then adding 1
  1683. // back. This gets the right AddWeighted behavior and gets us the
  1684. // interpolation-against-identity behavior for free.
  1685. MOZ_ASSERT(aValue1.GetUnit() == eCSSUnit_Number, "unexpected unit");
  1686. MOZ_ASSERT(aValue2.GetUnit() == eCSSUnit_Number, "unexpected unit");
  1687. float v1 = aValue1.GetFloatValue() - 1.0f,
  1688. v2 = aValue2.GetFloatValue() - 1.0f;
  1689. float result = v1 * aCoeff1 + v2 * aCoeff2;
  1690. aResult.SetFloatValue(EnsureNotNan(result + 1.0f), eCSSUnit_Number);
  1691. }
  1692. /* static */ already_AddRefed<nsCSSValue::Array>
  1693. StyleAnimationValue::AppendTransformFunction(nsCSSKeyword aTransformFunction,
  1694. nsCSSValueList**& aListTail)
  1695. {
  1696. RefPtr<nsCSSValue::Array> arr = AppendFunction(aTransformFunction);
  1697. nsCSSValueList *item = new nsCSSValueList;
  1698. item->mValue.SetArrayValue(arr, eCSSUnit_Function);
  1699. *aListTail = item;
  1700. aListTail = &item->mNext;
  1701. return arr.forget();
  1702. }
  1703. template<typename T>
  1704. T InterpolateNumerically(const T& aOne, const T& aTwo, double aCoeff)
  1705. {
  1706. return aOne + (aTwo - aOne) * aCoeff;
  1707. }
  1708. /* static */ Matrix4x4
  1709. StyleAnimationValue::InterpolateTransformMatrix(const Matrix4x4 &aMatrix1,
  1710. const Matrix4x4 &aMatrix2,
  1711. double aProgress)
  1712. {
  1713. // Decompose both matrices
  1714. // TODO: What do we do if one of these returns false (singular matrix)
  1715. Point3D scale1(1, 1, 1), translate1;
  1716. Point4D perspective1(0, 0, 0, 1);
  1717. gfxQuaternion rotate1;
  1718. nsStyleTransformMatrix::ShearArray shear1{0.0f, 0.0f, 0.0f};
  1719. Point3D scale2(1, 1, 1), translate2;
  1720. Point4D perspective2(0, 0, 0, 1);
  1721. gfxQuaternion rotate2;
  1722. nsStyleTransformMatrix::ShearArray shear2{0.0f, 0.0f, 0.0f};
  1723. Matrix matrix2d1, matrix2d2;
  1724. if (aMatrix1.Is2D(&matrix2d1) && aMatrix2.Is2D(&matrix2d2)) {
  1725. Decompose2DMatrix(matrix2d1, scale1, shear1, rotate1, translate1);
  1726. Decompose2DMatrix(matrix2d2, scale2, shear2, rotate2, translate2);
  1727. } else {
  1728. Decompose3DMatrix(aMatrix1, scale1, shear1,
  1729. rotate1, translate1, perspective1);
  1730. Decompose3DMatrix(aMatrix2, scale2, shear2,
  1731. rotate2, translate2, perspective2);
  1732. }
  1733. // Interpolate each of the pieces
  1734. Matrix4x4 result;
  1735. Point4D perspective =
  1736. InterpolateNumerically(perspective1, perspective2, aProgress);
  1737. result.SetTransposedVector(3, perspective);
  1738. Point3D translate =
  1739. InterpolateNumerically(translate1, translate2, aProgress);
  1740. result.PreTranslate(translate.x, translate.y, translate.z);
  1741. gfxQuaternion q3 = rotate1.Slerp(rotate2, aProgress);
  1742. Matrix4x4 rotate = q3.ToMatrix();
  1743. if (!rotate.IsIdentity()) {
  1744. result = rotate * result;
  1745. }
  1746. // TODO: Would it be better to interpolate these as angles?
  1747. // How do we convert back to angles?
  1748. float yzshear =
  1749. InterpolateNumerically(shear1[ShearType::YZSHEAR],
  1750. shear2[ShearType::YZSHEAR],
  1751. aProgress);
  1752. if (yzshear != 0.0) {
  1753. result.SkewYZ(yzshear);
  1754. }
  1755. float xzshear =
  1756. InterpolateNumerically(shear1[ShearType::XZSHEAR],
  1757. shear2[ShearType::XZSHEAR],
  1758. aProgress);
  1759. if (xzshear != 0.0) {
  1760. result.SkewXZ(xzshear);
  1761. }
  1762. float xyshear =
  1763. InterpolateNumerically(shear1[ShearType::XYSHEAR],
  1764. shear2[ShearType::XYSHEAR],
  1765. aProgress);
  1766. if (xyshear != 0.0) {
  1767. result.SkewXY(xyshear);
  1768. }
  1769. Point3D scale =
  1770. InterpolateNumerically(scale1, scale2, aProgress);
  1771. if (scale != Point3D(1.0, 1.0, 1.0)) {
  1772. result.PreScale(scale.x, scale.y, scale.z);
  1773. }
  1774. return result;
  1775. }
  1776. static nsCSSValueList*
  1777. AddDifferentTransformLists(double aCoeff1, const nsCSSValueList* aList1,
  1778. double aCoeff2, const nsCSSValueList* aList2)
  1779. {
  1780. nsAutoPtr<nsCSSValueList> result;
  1781. nsCSSValueList **resultTail = getter_Transfers(result);
  1782. RefPtr<nsCSSValue::Array> arr;
  1783. arr =
  1784. StyleAnimationValue::AppendTransformFunction(eCSSKeyword_interpolatematrix,
  1785. resultTail);
  1786. // FIXME: We should change the other transform code to also only
  1787. // take a single progress value, as having values that don't
  1788. // sum to 1 doesn't make sense for these.
  1789. if (aList1 == aList2) {
  1790. arr->Item(1).Reset();
  1791. } else {
  1792. aList1->CloneInto(arr->Item(1).SetListValue());
  1793. }
  1794. aList2->CloneInto(arr->Item(2).SetListValue());
  1795. arr->Item(3).SetPercentValue(aCoeff2);
  1796. return result.forget();
  1797. }
  1798. static UniquePtr<nsCSSValueList>
  1799. AddWeightedFilterFunctionImpl(double aCoeff1, const nsCSSValueList* aList1,
  1800. double aCoeff2, const nsCSSValueList* aList2,
  1801. ColorAdditionType aColorAdditionType)
  1802. {
  1803. // AddWeightedFilterFunction should be our only caller, and it should ensure
  1804. // that both args are non-null.
  1805. MOZ_ASSERT(aList1, "expected filter list");
  1806. MOZ_ASSERT(aList2, "expected filter list");
  1807. MOZ_ASSERT(aList1->mValue.GetUnit() == eCSSUnit_Function,
  1808. "expected function");
  1809. MOZ_ASSERT(aList2->mValue.GetUnit() == eCSSUnit_Function,
  1810. "expected function");
  1811. RefPtr<nsCSSValue::Array> a1 = aList1->mValue.GetArrayValue(),
  1812. a2 = aList2->mValue.GetArrayValue();
  1813. nsCSSKeyword filterFunction = a1->Item(0).GetKeywordValue();
  1814. if (filterFunction != a2->Item(0).GetKeywordValue()) {
  1815. return nullptr; // Can't add two filters of different types.
  1816. }
  1817. auto resultList = MakeUnique<nsCSSValueList>();
  1818. nsCSSValue::Array* result =
  1819. resultList->mValue.InitFunction(filterFunction, 1);
  1820. // "hue-rotate" is the only filter-function that accepts negative values, and
  1821. // we don't use this "restrictions" variable in its clause below.
  1822. const uint32_t restrictions = CSS_PROPERTY_VALUE_NONNEGATIVE;
  1823. const nsCSSValue& funcArg1 = a1->Item(1);
  1824. const nsCSSValue& funcArg2 = a2->Item(1);
  1825. nsCSSValue& resultArg = result->Item(1);
  1826. float initialVal = 1.0f;
  1827. switch (filterFunction) {
  1828. case eCSSKeyword_blur: {
  1829. nsCSSUnit unit;
  1830. if (funcArg1.GetUnit() == funcArg2.GetUnit()) {
  1831. unit = funcArg1.GetUnit();
  1832. } else {
  1833. // If units differ, we'll just combine them with calc().
  1834. unit = eCSSUnit_Calc;
  1835. }
  1836. if (!AddCSSValuePixelPercentCalc(restrictions,
  1837. unit,
  1838. aCoeff1, funcArg1,
  1839. aCoeff2, funcArg2,
  1840. resultArg)) {
  1841. return nullptr;
  1842. }
  1843. break;
  1844. }
  1845. case eCSSKeyword_grayscale:
  1846. case eCSSKeyword_invert:
  1847. case eCSSKeyword_sepia:
  1848. initialVal = 0.0f;
  1849. MOZ_FALLTHROUGH;
  1850. case eCSSKeyword_brightness:
  1851. case eCSSKeyword_contrast:
  1852. case eCSSKeyword_opacity:
  1853. case eCSSKeyword_saturate:
  1854. AddCSSValuePercentNumber(restrictions,
  1855. aCoeff1, funcArg1,
  1856. aCoeff2, funcArg2,
  1857. resultArg,
  1858. initialVal);
  1859. break;
  1860. case eCSSKeyword_hue_rotate:
  1861. AddCSSValueAngle(aCoeff1, funcArg1,
  1862. aCoeff2, funcArg2,
  1863. resultArg);
  1864. break;
  1865. case eCSSKeyword_drop_shadow: {
  1866. MOZ_ASSERT(!funcArg1.GetListValue()->mNext &&
  1867. !funcArg2.GetListValue()->mNext,
  1868. "drop-shadow filter func doesn't support lists");
  1869. UniquePtr<nsCSSValueList> shadowValue =
  1870. AddWeightedShadowItems(aCoeff1,
  1871. funcArg1.GetListValue()->mValue,
  1872. aCoeff2,
  1873. funcArg2.GetListValue()->mValue,
  1874. aColorAdditionType);
  1875. if (!shadowValue) {
  1876. return nullptr;
  1877. }
  1878. resultArg.AdoptListValue(Move(shadowValue));
  1879. break;
  1880. }
  1881. default:
  1882. MOZ_ASSERT(false, "unknown filter function");
  1883. return nullptr;
  1884. }
  1885. return resultList;
  1886. }
  1887. static UniquePtr<nsCSSValueList>
  1888. AddWeightedFilterFunction(double aCoeff1, const nsCSSValueList* aList1,
  1889. double aCoeff2, const nsCSSValueList* aList2,
  1890. ColorAdditionType aColorAdditionType)
  1891. {
  1892. MOZ_ASSERT(aList1 || aList2,
  1893. "one function list item must not be null");
  1894. // Note that one of our arguments could be null, indicating that
  1895. // it's the initial value. Rather than adding special null-handling
  1896. // logic, we just check for null values and replace them with
  1897. // 0 * the other value. That way, AddWeightedFilterFunctionImpl can assume
  1898. // its args are non-null.
  1899. if (!aList1) {
  1900. return AddWeightedFilterFunctionImpl(aCoeff2, aList2, 0, aList2,
  1901. aColorAdditionType);
  1902. }
  1903. if (!aList2) {
  1904. return AddWeightedFilterFunctionImpl(aCoeff1, aList1, 0, aList1,
  1905. aColorAdditionType);
  1906. }
  1907. return AddWeightedFilterFunctionImpl(aCoeff1, aList1, aCoeff2, aList2,
  1908. aColorAdditionType);
  1909. }
  1910. static inline uint32_t
  1911. ShapeArgumentCount(nsCSSKeyword aShapeFunction)
  1912. {
  1913. switch (aShapeFunction) {
  1914. case eCSSKeyword_circle:
  1915. return 2; // radius and center point
  1916. case eCSSKeyword_polygon:
  1917. return 2; // fill rule and a list of points
  1918. case eCSSKeyword_ellipse:
  1919. return 3; // two radii and center point
  1920. case eCSSKeyword_inset:
  1921. return 5; // four edge offsets and a list of corner radii
  1922. default:
  1923. MOZ_ASSERT_UNREACHABLE("Unknown shape type");
  1924. return 0;
  1925. }
  1926. }
  1927. static void
  1928. AddPositions(double aCoeff1, const nsCSSValue& aPos1,
  1929. double aCoeff2, const nsCSSValue& aPos2,
  1930. nsCSSValue& aResultPos)
  1931. {
  1932. MOZ_ASSERT(aPos1.GetUnit() == eCSSUnit_Array &&
  1933. aPos2.GetUnit() == eCSSUnit_Array,
  1934. "Args should be CSS <position>s, encoded as arrays");
  1935. const nsCSSValue::Array* posArray1 = aPos1.GetArrayValue();
  1936. const nsCSSValue::Array* posArray2 = aPos2.GetArrayValue();
  1937. MOZ_ASSERT(posArray1->Count() == 4 && posArray2->Count() == 4,
  1938. "CSSParserImpl::ParsePositionValue creates an array of length "
  1939. "4 - how did we get here?");
  1940. nsCSSValue::Array* resultPosArray = nsCSSValue::Array::Create(4);
  1941. aResultPos.SetArrayValue(resultPosArray, eCSSUnit_Array);
  1942. // Only iterate over elements 1 and 3. The <position> is 'uncomputed' to
  1943. // only those elements. See also the comment in SetPositionValue.
  1944. for (size_t i = 1; i < 4; i += 2) {
  1945. const nsCSSValue& v1 = posArray1->Item(i);
  1946. const nsCSSValue& v2 = posArray2->Item(i);
  1947. nsCSSValue& vr = resultPosArray->Item(i);
  1948. AddCSSValueCanonicalCalc(aCoeff1, v1,
  1949. aCoeff2, v2, vr);
  1950. }
  1951. }
  1952. static Maybe<nsCSSValuePair>
  1953. AddCSSValuePair(nsCSSPropertyID aProperty, uint32_t aRestrictions,
  1954. double aCoeff1, const nsCSSValuePair* aPair1,
  1955. double aCoeff2, const nsCSSValuePair* aPair2)
  1956. {
  1957. MOZ_ASSERT(aPair1, "expected pair");
  1958. MOZ_ASSERT(aPair2, "expected pair");
  1959. Maybe<nsCSSValuePair> result;
  1960. nsCSSUnit unit[2];
  1961. unit[0] = GetCommonUnit(aProperty, aPair1->mXValue.GetUnit(),
  1962. aPair2->mXValue.GetUnit());
  1963. unit[1] = GetCommonUnit(aProperty, aPair1->mYValue.GetUnit(),
  1964. aPair2->mYValue.GetUnit());
  1965. if (unit[0] == eCSSUnit_Null || unit[1] == eCSSUnit_Null ||
  1966. unit[0] == eCSSUnit_URL || unit[0] == eCSSUnit_Enumerated) {
  1967. return result; // Nothing() (returning |result| for RVO)
  1968. }
  1969. result.emplace();
  1970. static nsCSSValue nsCSSValuePair::* const pairValues[2] = {
  1971. &nsCSSValuePair::mXValue, &nsCSSValuePair::mYValue
  1972. };
  1973. for (uint32_t i = 0; i < 2; ++i) {
  1974. nsCSSValue nsCSSValuePair::*member = pairValues[i];
  1975. if (!AddCSSValuePixelPercentCalc(aRestrictions, unit[i],
  1976. aCoeff1, aPair1->*member,
  1977. aCoeff2, aPair2->*member,
  1978. result.ref().*member) ) {
  1979. MOZ_ASSERT(false, "unexpected unit");
  1980. result.reset();
  1981. return result; // Nothing() (returning |result| for RVO)
  1982. }
  1983. }
  1984. return result;
  1985. }
  1986. static UniquePtr<nsCSSValuePairList>
  1987. AddCSSValuePairList(nsCSSPropertyID aProperty,
  1988. double aCoeff1, const nsCSSValuePairList* aList1,
  1989. double aCoeff2, const nsCSSValuePairList* aList2)
  1990. {
  1991. MOZ_ASSERT(aList1, "Can't add a null list");
  1992. MOZ_ASSERT(aList2, "Can't add a null list");
  1993. auto result = MakeUnique<nsCSSValuePairList>();
  1994. nsCSSValuePairList* resultPtr = result.get();
  1995. do {
  1996. static nsCSSValue nsCSSValuePairList::* const pairListValues[] = {
  1997. &nsCSSValuePairList::mXValue,
  1998. &nsCSSValuePairList::mYValue,
  1999. };
  2000. uint32_t restrictions = nsCSSProps::ValueRestrictions(aProperty);
  2001. for (uint32_t i = 0; i < ArrayLength(pairListValues); ++i) {
  2002. const nsCSSValue& v1 = aList1->*(pairListValues[i]);
  2003. const nsCSSValue& v2 = aList2->*(pairListValues[i]);
  2004. nsCSSValue& vr = resultPtr->*(pairListValues[i]);
  2005. nsCSSUnit unit =
  2006. GetCommonUnit(aProperty, v1.GetUnit(), v2.GetUnit());
  2007. if (unit == eCSSUnit_Null) {
  2008. return nullptr;
  2009. }
  2010. if (!AddCSSValuePixelPercentCalc(restrictions, unit,
  2011. aCoeff1, v1,
  2012. aCoeff2, v2, vr)) {
  2013. if (v1 != v2) {
  2014. return nullptr;
  2015. }
  2016. vr = v1;
  2017. }
  2018. }
  2019. aList1 = aList1->mNext;
  2020. aList2 = aList2->mNext;
  2021. if (!aList1 || !aList2) {
  2022. break;
  2023. }
  2024. resultPtr->mNext = new nsCSSValuePairList;
  2025. resultPtr = resultPtr->mNext;
  2026. } while (aList1 && aList2);
  2027. if (aList1 || aList2) {
  2028. return nullptr; // We can't interpolate lists of different lengths
  2029. }
  2030. return result;
  2031. }
  2032. static already_AddRefed<nsCSSValue::Array>
  2033. AddShapeFunction(nsCSSPropertyID aProperty,
  2034. double aCoeff1, const nsCSSValue::Array* aArray1,
  2035. double aCoeff2, const nsCSSValue::Array* aArray2,
  2036. Restrictions aRestriction)
  2037. {
  2038. MOZ_ASSERT(aArray1 && aArray1->Count() == 2, "expected shape function");
  2039. MOZ_ASSERT(aArray2 && aArray2->Count() == 2, "expected shape function");
  2040. MOZ_ASSERT(aArray1->Item(0).GetUnit() == eCSSUnit_Function,
  2041. "expected function");
  2042. MOZ_ASSERT(aArray2->Item(0).GetUnit() == eCSSUnit_Function,
  2043. "expected function");
  2044. MOZ_ASSERT(aArray1->Item(1).GetUnit() == eCSSUnit_Enumerated,
  2045. "expected geometry-box");
  2046. MOZ_ASSERT(aArray2->Item(1).GetUnit() == eCSSUnit_Enumerated,
  2047. "expected geometry-box");
  2048. if (aArray1->Item(1).GetIntValue() != aArray2->Item(1).GetIntValue()) {
  2049. return nullptr; // Both shapes must use the same reference box.
  2050. }
  2051. const nsCSSValue::Array* func1 = aArray1->Item(0).GetArrayValue();
  2052. const nsCSSValue::Array* func2 = aArray2->Item(0).GetArrayValue();
  2053. nsCSSKeyword shapeFuncName = func1->Item(0).GetKeywordValue();
  2054. if (shapeFuncName != func2->Item(0).GetKeywordValue()) {
  2055. return nullptr; // Can't add two shapes of different types.
  2056. }
  2057. RefPtr<nsCSSValue::Array> result = nsCSSValue::Array::Create(2);
  2058. nsCSSValue::Array* resultFuncArgs =
  2059. result->Item(0).InitFunction(shapeFuncName,
  2060. ShapeArgumentCount(shapeFuncName));
  2061. switch (shapeFuncName) {
  2062. case eCSSKeyword_ellipse:
  2063. // Add ellipses' |ry| values (but fail if we encounter an enum):
  2064. if (!AddCSSValuePixelPercentCalc(aRestriction == Restrictions::Enable
  2065. ? CSS_PROPERTY_VALUE_NONNEGATIVE
  2066. : 0,
  2067. GetCommonUnit(aProperty,
  2068. func1->Item(2).GetUnit(),
  2069. func2->Item(2).GetUnit()),
  2070. aCoeff1, func1->Item(2),
  2071. aCoeff2, func2->Item(2),
  2072. resultFuncArgs->Item(2))) {
  2073. return nullptr;
  2074. }
  2075. MOZ_FALLTHROUGH; // to handle rx and center point
  2076. case eCSSKeyword_circle: {
  2077. // Add circles' |r| (or ellipses' |rx|) values:
  2078. if (!AddCSSValuePixelPercentCalc(aRestriction == Restrictions::Enable
  2079. ? CSS_PROPERTY_VALUE_NONNEGATIVE
  2080. : 0,
  2081. GetCommonUnit(aProperty,
  2082. func1->Item(1).GetUnit(),
  2083. func2->Item(1).GetUnit()),
  2084. aCoeff1, func1->Item(1),
  2085. aCoeff2, func2->Item(1),
  2086. resultFuncArgs->Item(1))) {
  2087. return nullptr;
  2088. }
  2089. // Add center points (defined as a <position>).
  2090. size_t posIndex = shapeFuncName == eCSSKeyword_circle ? 2 : 3;
  2091. AddPositions(aCoeff1, func1->Item(posIndex),
  2092. aCoeff2, func2->Item(posIndex),
  2093. resultFuncArgs->Item(posIndex));
  2094. break;
  2095. }
  2096. case eCSSKeyword_polygon: {
  2097. // Add polygons' corresponding points (if the fill rule matches):
  2098. int32_t fillRule = func1->Item(1).GetIntValue();
  2099. if (fillRule != func2->Item(1).GetIntValue()) {
  2100. return nullptr; // can't interpolate between different fill rules
  2101. }
  2102. resultFuncArgs->Item(1).SetIntValue(fillRule, eCSSUnit_Enumerated);
  2103. const nsCSSValuePairList* points1 = func1->Item(2).GetPairListValue();
  2104. const nsCSSValuePairList* points2 = func2->Item(2).GetPairListValue();
  2105. UniquePtr<nsCSSValuePairList> resultPoints =
  2106. AddCSSValuePairList(aProperty, aCoeff1, points1, aCoeff2, points2);
  2107. if (!resultPoints) {
  2108. return nullptr;
  2109. }
  2110. resultFuncArgs->Item(2).AdoptPairListValue(Move(resultPoints));
  2111. break;
  2112. }
  2113. case eCSSKeyword_inset: {
  2114. MOZ_ASSERT(func1->Count() == 6 && func2->Count() == 6,
  2115. "Update for CSSParserImpl::ParseInsetFunction changes");
  2116. // Items 1-4 are respectively the top, right, bottom and left offsets
  2117. // from the reference box.
  2118. for (size_t i = 1; i <= 4; ++i) {
  2119. if (!AddCSSValuePixelPercentCalc(aRestriction == Restrictions::Enable
  2120. ? CSS_PROPERTY_VALUE_NONNEGATIVE
  2121. : 0,
  2122. GetCommonUnit(aProperty,
  2123. func1->Item(i).GetUnit(),
  2124. func2->Item(i).GetUnit()),
  2125. aCoeff1, func1->Item(i),
  2126. aCoeff2, func2->Item(i),
  2127. resultFuncArgs->Item(i))) {
  2128. return nullptr;
  2129. }
  2130. }
  2131. // Item 5 contains the radii of the rounded corners for the inset
  2132. // rectangle.
  2133. MOZ_ASSERT(func1->Item(5).GetUnit() == eCSSUnit_Array &&
  2134. func2->Item(5).GetUnit() == eCSSUnit_Array,
  2135. "Expected two arrays");
  2136. const nsCSSValue::Array* radii1 = func1->Item(5).GetArrayValue();
  2137. const nsCSSValue::Array* radii2 = func2->Item(5).GetArrayValue();
  2138. MOZ_ASSERT(radii1->Count() == 4 && radii2->Count() == 4);
  2139. nsCSSValue::Array* resultRadii = nsCSSValue::Array::Create(4);
  2140. resultFuncArgs->Item(5).SetArrayValue(resultRadii, eCSSUnit_Array);
  2141. // We use an arbitrary border-radius property here to get the appropriate
  2142. // restrictions for radii since this is a <border-radius> value.
  2143. uint32_t restrictions =
  2144. aRestriction == Restrictions::Enable
  2145. ? nsCSSProps::ValueRestrictions(eCSSProperty_border_top_left_radius)
  2146. : 0;
  2147. for (size_t i = 0; i < 4; ++i) {
  2148. const nsCSSValuePair& pair1 = radii1->Item(i).GetPairValue();
  2149. const nsCSSValuePair& pair2 = radii2->Item(i).GetPairValue();
  2150. const Maybe<nsCSSValuePair> pairResult =
  2151. AddCSSValuePair(aProperty, restrictions,
  2152. aCoeff1, &pair1,
  2153. aCoeff2, &pair2);
  2154. if (!pairResult) {
  2155. return nullptr;
  2156. }
  2157. resultRadii->Item(i).SetPairValue(pairResult.ptr());
  2158. }
  2159. break;
  2160. }
  2161. default:
  2162. MOZ_ASSERT_UNREACHABLE("Unknown shape type");
  2163. return nullptr;
  2164. }
  2165. // set the geometry-box value
  2166. result->Item(1).SetIntValue(aArray1->Item(1).GetIntValue(),
  2167. eCSSUnit_Enumerated);
  2168. return result.forget();
  2169. }
  2170. static nsCSSValueList*
  2171. AddTransformLists(double aCoeff1, const nsCSSValueList* aList1,
  2172. double aCoeff2, const nsCSSValueList* aList2)
  2173. {
  2174. nsAutoPtr<nsCSSValueList> result;
  2175. nsCSSValueList **resultTail = getter_Transfers(result);
  2176. do {
  2177. RefPtr<nsCSSValue::Array> a1 = ToPrimitive(aList1->mValue.GetArrayValue()),
  2178. a2 = ToPrimitive(aList2->mValue.GetArrayValue());
  2179. MOZ_ASSERT(
  2180. TransformFunctionsMatch(nsStyleTransformMatrix::TransformFunctionOf(a1),
  2181. nsStyleTransformMatrix::TransformFunctionOf(a2)),
  2182. "transform function mismatch");
  2183. MOZ_ASSERT(!*resultTail,
  2184. "resultTail isn't pointing to the tail (may leak)");
  2185. nsCSSKeyword tfunc = nsStyleTransformMatrix::TransformFunctionOf(a1);
  2186. RefPtr<nsCSSValue::Array> arr;
  2187. if (tfunc != eCSSKeyword_matrix &&
  2188. tfunc != eCSSKeyword_matrix3d &&
  2189. tfunc != eCSSKeyword_interpolatematrix &&
  2190. tfunc != eCSSKeyword_rotate3d &&
  2191. tfunc != eCSSKeyword_perspective) {
  2192. arr = StyleAnimationValue::AppendTransformFunction(tfunc, resultTail);
  2193. }
  2194. switch (tfunc) {
  2195. case eCSSKeyword_translate3d: {
  2196. MOZ_ASSERT(a1->Count() == 4, "unexpected count");
  2197. MOZ_ASSERT(a2->Count() == 4, "unexpected count");
  2198. AddTransformTranslate(aCoeff1, a1->Item(1), aCoeff2, a2->Item(1),
  2199. arr->Item(1));
  2200. AddTransformTranslate(aCoeff1, a1->Item(2), aCoeff2, a2->Item(2),
  2201. arr->Item(2));
  2202. AddTransformTranslate(aCoeff1, a1->Item(3), aCoeff2, a2->Item(3),
  2203. arr->Item(3));
  2204. break;
  2205. }
  2206. case eCSSKeyword_scale3d: {
  2207. MOZ_ASSERT(a1->Count() == 4, "unexpected count");
  2208. MOZ_ASSERT(a2->Count() == 4, "unexpected count");
  2209. AddTransformScale(aCoeff1, a1->Item(1), aCoeff2, a2->Item(1),
  2210. arr->Item(1));
  2211. AddTransformScale(aCoeff1, a1->Item(2), aCoeff2, a2->Item(2),
  2212. arr->Item(2));
  2213. AddTransformScale(aCoeff1, a1->Item(3), aCoeff2, a2->Item(3),
  2214. arr->Item(3));
  2215. break;
  2216. }
  2217. // It would probably be nicer to animate skew in tangent space
  2218. // rather than angle space. However, it's easy to specify
  2219. // skews with infinite tangents, and behavior changes pretty
  2220. // drastically when crossing such skews (since the direction of
  2221. // animation flips), so interop is probably more important here.
  2222. case eCSSKeyword_skew: {
  2223. MOZ_ASSERT(a1->Count() == 2 || a1->Count() == 3,
  2224. "unexpected count");
  2225. MOZ_ASSERT(a2->Count() == 2 || a2->Count() == 3,
  2226. "unexpected count");
  2227. nsCSSValue zero(0.0f, eCSSUnit_Radian);
  2228. // Add Y component of skew.
  2229. AddCSSValueAngle(aCoeff1,
  2230. a1->Count() == 3 ? a1->Item(2) : zero,
  2231. aCoeff2,
  2232. a2->Count() == 3 ? a2->Item(2) : zero,
  2233. arr->Item(2));
  2234. // Add X component of skew (which can be merged with case below
  2235. // in non-DEBUG).
  2236. AddCSSValueAngle(aCoeff1, a1->Item(1), aCoeff2, a2->Item(1),
  2237. arr->Item(1));
  2238. break;
  2239. }
  2240. case eCSSKeyword_skewx:
  2241. case eCSSKeyword_skewy:
  2242. case eCSSKeyword_rotate:
  2243. case eCSSKeyword_rotatex:
  2244. case eCSSKeyword_rotatey:
  2245. case eCSSKeyword_rotatez: {
  2246. MOZ_ASSERT(a1->Count() == 2, "unexpected count");
  2247. MOZ_ASSERT(a2->Count() == 2, "unexpected count");
  2248. AddCSSValueAngle(aCoeff1, a1->Item(1), aCoeff2, a2->Item(1),
  2249. arr->Item(1));
  2250. break;
  2251. }
  2252. case eCSSKeyword_rotate3d: {
  2253. Point3D vector1(a1->Item(1).GetFloatValue(),
  2254. a1->Item(2).GetFloatValue(),
  2255. a1->Item(3).GetFloatValue());
  2256. vector1.Normalize();
  2257. Point3D vector2(a2->Item(1).GetFloatValue(),
  2258. a2->Item(2).GetFloatValue(),
  2259. a2->Item(3).GetFloatValue());
  2260. vector2.Normalize();
  2261. // Handle rotate3d with matched (normalized) vectors,
  2262. // otherwise fallthrough to the next switch statement
  2263. // and do matrix decomposition.
  2264. if (vector1 == vector2) {
  2265. // We skipped appending a transform function above for rotate3d,
  2266. // so do it now.
  2267. arr = StyleAnimationValue::AppendTransformFunction(tfunc, resultTail);
  2268. arr->Item(1).SetFloatValue(vector1.x, eCSSUnit_Number);
  2269. arr->Item(2).SetFloatValue(vector1.y, eCSSUnit_Number);
  2270. arr->Item(3).SetFloatValue(vector1.z, eCSSUnit_Number);
  2271. AddCSSValueAngle(aCoeff1, a1->Item(4), aCoeff2, a2->Item(4),
  2272. arr->Item(4));
  2273. break;
  2274. }
  2275. MOZ_FALLTHROUGH;
  2276. }
  2277. case eCSSKeyword_matrix:
  2278. case eCSSKeyword_matrix3d:
  2279. case eCSSKeyword_perspective:
  2280. if (aCoeff1 == 0.0 && aCoeff2 == 0.0) {
  2281. // Special case. If both coefficients are 0.0, we should apply an
  2282. // identity transform function.
  2283. arr = StyleAnimationValue::AppendTransformFunction(tfunc, resultTail);
  2284. if (tfunc == eCSSKeyword_rotate3d) {
  2285. arr->Item(1).SetFloatValue(0.0, eCSSUnit_Number);
  2286. arr->Item(2).SetFloatValue(0.0, eCSSUnit_Number);
  2287. arr->Item(3).SetFloatValue(1.0, eCSSUnit_Number);
  2288. arr->Item(4).SetFloatValue(0.0, eCSSUnit_Radian);
  2289. } else if (tfunc == eCSSKeyword_perspective) {
  2290. // The parameter of the identity perspective function is
  2291. // positive infinite.
  2292. arr->Item(1).SetFloatValue(std::numeric_limits<float>::infinity(),
  2293. eCSSUnit_Pixel);
  2294. } else {
  2295. nsStyleTransformMatrix::SetIdentityMatrix(arr);
  2296. }
  2297. break;
  2298. }
  2299. MOZ_FALLTHROUGH;
  2300. case eCSSKeyword_interpolatematrix: {
  2301. // FIXME: If the matrix contains only numbers then we could decompose
  2302. // here.
  2303. // Construct temporary lists with only this item in them.
  2304. nsCSSValueList tempList1, tempList2;
  2305. tempList1.mValue = aList1->mValue;
  2306. tempList2.mValue = aList2->mValue;
  2307. if (aList1 == aList2) {
  2308. *resultTail =
  2309. AddDifferentTransformLists(aCoeff1, &tempList1, aCoeff2, &tempList1);
  2310. } else {
  2311. *resultTail =
  2312. AddDifferentTransformLists(aCoeff1, &tempList1, aCoeff2, &tempList2);
  2313. }
  2314. // Now advance resultTail to point to the new tail slot.
  2315. while (*resultTail) {
  2316. resultTail = &(*resultTail)->mNext;
  2317. }
  2318. break;
  2319. }
  2320. default:
  2321. MOZ_ASSERT(false, "unknown transform function");
  2322. }
  2323. aList1 = aList1->mNext;
  2324. aList2 = aList2->mNext;
  2325. } while (aList1);
  2326. MOZ_ASSERT(!aList2, "list length mismatch");
  2327. MOZ_ASSERT(!*resultTail,
  2328. "resultTail isn't pointing to the tail");
  2329. return result.forget();
  2330. }
  2331. static void
  2332. AddPositionCoords(double aCoeff1, const nsCSSValue& aPos1,
  2333. double aCoeff2, const nsCSSValue& aPos2,
  2334. nsCSSValue& aResultPos)
  2335. {
  2336. const nsCSSValue::Array* posArray1 = aPos1.GetArrayValue();
  2337. const nsCSSValue::Array* posArray2 = aPos2.GetArrayValue();
  2338. nsCSSValue::Array* resultPosArray = nsCSSValue::Array::Create(2);
  2339. aResultPos.SetArrayValue(resultPosArray, eCSSUnit_Array);
  2340. /* Only compute element 1. The <position-coord> is
  2341. * 'uncomputed' to only that element.
  2342. */
  2343. const nsCSSValue& v1 = posArray1->Item(1);
  2344. const nsCSSValue& v2 = posArray2->Item(1);
  2345. nsCSSValue& vr = resultPosArray->Item(1);
  2346. AddCSSValueCanonicalCalc(aCoeff1, v1,
  2347. aCoeff2, v2, vr);
  2348. }
  2349. static UniquePtr<nsCSSValueList>
  2350. AddWeightedShadowList(double aCoeff1,
  2351. const nsCSSValueList* aShadow1,
  2352. double aCoeff2,
  2353. const nsCSSValueList* aShadow2,
  2354. ColorAdditionType aColorAdditionType)
  2355. {
  2356. // This is implemented according to:
  2357. // http://dev.w3.org/csswg/css3-transitions/#animation-of-property-types-
  2358. // and the third item in the summary of:
  2359. // http://lists.w3.org/Archives/Public/www-style/2009Jul/0050.html
  2360. UniquePtr<nsCSSValueList> result;
  2361. nsCSSValueList* tail = nullptr;
  2362. while (aShadow1 && aShadow2) {
  2363. UniquePtr<nsCSSValueList> shadowValue =
  2364. AddWeightedShadowItems(aCoeff1, aShadow1->mValue,
  2365. aCoeff2, aShadow2->mValue,
  2366. aColorAdditionType);
  2367. if (!shadowValue) {
  2368. return nullptr;
  2369. }
  2370. aShadow1 = aShadow1->mNext;
  2371. aShadow2 = aShadow2->mNext;
  2372. AppendToCSSValueList(result, Move(shadowValue), &tail);
  2373. }
  2374. if (aShadow1 || aShadow2) {
  2375. const nsCSSValueList *longShadow;
  2376. double longCoeff;
  2377. if (aShadow1) {
  2378. longShadow = aShadow1;
  2379. longCoeff = aCoeff1;
  2380. } else {
  2381. longShadow = aShadow2;
  2382. longCoeff = aCoeff2;
  2383. }
  2384. while (longShadow) {
  2385. // Passing coefficients that add to less than 1 produces the
  2386. // desired result of interpolating "0 0 0 transparent" with
  2387. // the current shadow.
  2388. UniquePtr<nsCSSValueList> shadowValue =
  2389. AddWeightedShadowItems(longCoeff, longShadow->mValue,
  2390. 0.0, longShadow->mValue,
  2391. aColorAdditionType);
  2392. if (!shadowValue) {
  2393. return nullptr;
  2394. }
  2395. longShadow = longShadow->mNext;
  2396. AppendToCSSValueList(result, Move(shadowValue), &tail);
  2397. }
  2398. }
  2399. return result;
  2400. }
  2401. static UniquePtr<nsCSSValueList>
  2402. AddWeightedFilterList(double aCoeff1, const nsCSSValueList* aList1,
  2403. double aCoeff2, const nsCSSValueList* aList2,
  2404. ColorAdditionType aColorAdditionType)
  2405. {
  2406. UniquePtr<nsCSSValueList> result;
  2407. nsCSSValueList* tail = nullptr;
  2408. while (aList1 || aList2) {
  2409. if ((aList1 && aList1->mValue.GetUnit() != eCSSUnit_Function) ||
  2410. (aList2 && aList2->mValue.GetUnit() != eCSSUnit_Function)) {
  2411. // If we don't have filter-functions, we must have filter-URLs, which
  2412. // we can't add or interpolate.
  2413. return nullptr;
  2414. }
  2415. UniquePtr<nsCSSValueList> resultFunction =
  2416. AddWeightedFilterFunction(aCoeff1, aList1, aCoeff2, aList2,
  2417. aColorAdditionType);
  2418. if (!resultFunction) {
  2419. // filter function mismatch
  2420. return nullptr;
  2421. }
  2422. AppendToCSSValueList(result, Move(resultFunction), &tail);
  2423. // move to next aList items
  2424. if (aList1) {
  2425. aList1 = aList1->mNext;
  2426. }
  2427. if (aList2) {
  2428. aList2 = aList2->mNext;
  2429. }
  2430. }
  2431. return result;
  2432. }
  2433. bool
  2434. StyleAnimationValue::AddWeighted(nsCSSPropertyID aProperty,
  2435. double aCoeff1,
  2436. const StyleAnimationValue& aValue1,
  2437. double aCoeff2,
  2438. const StyleAnimationValue& aValue2,
  2439. StyleAnimationValue& aResultValue)
  2440. {
  2441. Unit commonUnit =
  2442. GetCommonUnit(aProperty, aValue1.GetUnit(), aValue2.GetUnit());
  2443. // Maybe need a followup method to convert the inputs into the common
  2444. // unit-type, if they don't already match it. (Or would it make sense to do
  2445. // that in GetCommonUnit? in which case maybe ConvertToCommonUnit would be
  2446. // better.)
  2447. switch (commonUnit) {
  2448. case eUnit_Null:
  2449. case eUnit_Auto:
  2450. case eUnit_None:
  2451. case eUnit_Normal:
  2452. case eUnit_UnparsedString:
  2453. case eUnit_URL:
  2454. case eUnit_DiscreteCSSValue:
  2455. return false;
  2456. case eUnit_Enumerated:
  2457. switch (aProperty) {
  2458. case eCSSProperty_font_stretch: {
  2459. // Animate just like eUnit_Integer.
  2460. int32_t result = floor(aCoeff1 * double(aValue1.GetIntValue()) +
  2461. aCoeff2 * double(aValue2.GetIntValue()));
  2462. if (result < NS_STYLE_FONT_STRETCH_ULTRA_CONDENSED) {
  2463. result = NS_STYLE_FONT_STRETCH_ULTRA_CONDENSED;
  2464. } else if (result > NS_STYLE_FONT_STRETCH_ULTRA_EXPANDED) {
  2465. result = NS_STYLE_FONT_STRETCH_ULTRA_EXPANDED;
  2466. }
  2467. aResultValue.SetIntValue(result, eUnit_Enumerated);
  2468. return true;
  2469. }
  2470. default:
  2471. return false;
  2472. }
  2473. case eUnit_Visibility: {
  2474. int32_t enum1 = aValue1.GetIntValue();
  2475. int32_t enum2 = aValue2.GetIntValue();
  2476. if (enum1 == enum2) {
  2477. aResultValue.SetIntValue(enum1, eUnit_Visibility);
  2478. return true;
  2479. }
  2480. if ((enum1 == NS_STYLE_VISIBILITY_VISIBLE) ==
  2481. (enum2 == NS_STYLE_VISIBILITY_VISIBLE)) {
  2482. return false;
  2483. }
  2484. int32_t val1 = enum1 == NS_STYLE_VISIBILITY_VISIBLE;
  2485. int32_t val2 = enum2 == NS_STYLE_VISIBILITY_VISIBLE;
  2486. double interp = aCoeff1 * val1 + aCoeff2 * val2;
  2487. int32_t result = interp > 0.0 ? NS_STYLE_VISIBILITY_VISIBLE
  2488. : (val1 ? enum2 : enum1);
  2489. aResultValue.SetIntValue(result, eUnit_Visibility);
  2490. return true;
  2491. }
  2492. case eUnit_Integer: {
  2493. // http://dev.w3.org/csswg/css3-transitions/#animation-of-property-types-
  2494. // says we should use floor
  2495. int32_t result = floor(aCoeff1 * double(aValue1.GetIntValue()) +
  2496. aCoeff2 * double(aValue2.GetIntValue()));
  2497. if (aProperty == eCSSProperty_font_weight) {
  2498. if (result < 100) {
  2499. result = 100;
  2500. } else if (result > 900) {
  2501. result = 900;
  2502. }
  2503. result -= result % 100;
  2504. } else {
  2505. result = RestrictValue(aProperty, result);
  2506. }
  2507. aResultValue.SetIntValue(result, eUnit_Integer);
  2508. return true;
  2509. }
  2510. case eUnit_Coord: {
  2511. aResultValue.SetCoordValue(RestrictValue(aProperty, NSToCoordRound(
  2512. aCoeff1 * aValue1.GetCoordValue() +
  2513. aCoeff2 * aValue2.GetCoordValue())));
  2514. return true;
  2515. }
  2516. case eUnit_Percent: {
  2517. aResultValue.SetPercentValue(RestrictValue(aProperty,
  2518. aCoeff1 * aValue1.GetPercentValue() +
  2519. aCoeff2 * aValue2.GetPercentValue()));
  2520. return true;
  2521. }
  2522. case eUnit_Float: {
  2523. aResultValue.SetFloatValue(RestrictValue(aProperty,
  2524. aCoeff1 * aValue1.GetFloatValue() +
  2525. aCoeff2 * aValue2.GetFloatValue()));
  2526. return true;
  2527. }
  2528. case eUnit_Color: {
  2529. RGBAColorData color1 = ExtractColor(aValue1);
  2530. RGBAColorData color2 = ExtractColor(aValue2);
  2531. auto resultColor = MakeUnique<nsCSSValue>();
  2532. resultColor->SetColorValue(
  2533. AddWeightedColorsAndClamp(aCoeff1, color1, aCoeff2, color2));
  2534. aResultValue.SetAndAdoptCSSValueValue(resultColor.release(), eUnit_Color);
  2535. return true;
  2536. }
  2537. case eUnit_CurrentColor: {
  2538. aResultValue.SetCurrentColorValue();
  2539. return true;
  2540. }
  2541. case eUnit_ComplexColor: {
  2542. ComplexColorData color1 = ExtractComplexColor(aValue1);
  2543. ComplexColorData color2 = ExtractComplexColor(aValue2);
  2544. RefPtr<ComplexColorValue> result = new ComplexColorValue;
  2545. // Common case is interpolating between a color and a currentcolor.
  2546. if (color1.IsNumericColor() && color2.IsCurrentColor()) {
  2547. result->mColor = color1.mColor;
  2548. result->mForegroundRatio = aCoeff2;
  2549. } else if (color1.IsCurrentColor() && color2.IsNumericColor()) {
  2550. result->mColor = color2.mColor;
  2551. result->mForegroundRatio = aCoeff1;
  2552. } else {
  2553. float ratio1 = 1.0f - color1.mForegroundRatio;
  2554. float ratio2 = 1.0f - color2.mForegroundRatio;
  2555. float alpha1 = color1.mColor.mA * ratio1;
  2556. float alpha2 = color2.mColor.mA * ratio2;
  2557. RGBAColorData resultColor =
  2558. AddWeightedColors(aCoeff1, color1.mColor.WithAlpha(alpha1),
  2559. aCoeff2, color2.mColor.WithAlpha(alpha2));
  2560. float resultRatio = color1.mForegroundRatio * aCoeff1 +
  2561. color2.mForegroundRatio * aCoeff2;
  2562. float resultAlpha = resultColor.mA / (1.0f - resultRatio);
  2563. result->mColor = resultColor.WithAlpha(resultAlpha);
  2564. result->mForegroundRatio = resultRatio;
  2565. }
  2566. aResultValue.SetComplexColorValue(result.forget());
  2567. return true;
  2568. }
  2569. case eUnit_Calc: {
  2570. PixelCalcValue v1 = ExtractCalcValue(aValue1);
  2571. PixelCalcValue v2 = ExtractCalcValue(aValue2);
  2572. double len = aCoeff1 * v1.mLength + aCoeff2 * v2.mLength;
  2573. double pct = aCoeff1 * v1.mPercent + aCoeff2 * v2.mPercent;
  2574. bool hasPct = (aCoeff1 != 0.0 && v1.mHasPercent) ||
  2575. (aCoeff2 != 0.0 && v2.mHasPercent);
  2576. nsCSSValue *val = new nsCSSValue();
  2577. nsCSSValue::Array *arr = nsCSSValue::Array::Create(1);
  2578. val->SetArrayValue(arr, eCSSUnit_Calc);
  2579. if (hasPct) {
  2580. nsCSSValue::Array *arr2 = nsCSSValue::Array::Create(2);
  2581. arr2->Item(0).SetFloatValue(len, eCSSUnit_Pixel);
  2582. arr2->Item(1).SetPercentValue(pct);
  2583. arr->Item(0).SetArrayValue(arr2, eCSSUnit_Calc_Plus);
  2584. } else {
  2585. arr->Item(0).SetFloatValue(len, eCSSUnit_Pixel);
  2586. }
  2587. aResultValue.SetAndAdoptCSSValueValue(val, eUnit_Calc);
  2588. return true;
  2589. }
  2590. case eUnit_ObjectPosition: {
  2591. const nsCSSValue* position1 = aValue1.GetCSSValueValue();
  2592. const nsCSSValue* position2 = aValue2.GetCSSValueValue();
  2593. nsAutoPtr<nsCSSValue> result(new nsCSSValue);
  2594. AddPositions(aCoeff1, *position1,
  2595. aCoeff2, *position2, *result);
  2596. aResultValue.SetAndAdoptCSSValueValue(result.forget(),
  2597. eUnit_ObjectPosition);
  2598. return true;
  2599. }
  2600. case eUnit_CSSValuePair: {
  2601. uint32_t restrictions = nsCSSProps::ValueRestrictions(aProperty);
  2602. Maybe<nsCSSValuePair> result =
  2603. AddCSSValuePair(aProperty, restrictions,
  2604. aCoeff1, aValue1.GetCSSValuePairValue(),
  2605. aCoeff2, aValue2.GetCSSValuePairValue());
  2606. if (!result) {
  2607. return false;
  2608. }
  2609. // We need a heap allocated object to adopt here:
  2610. auto heapResult = MakeUnique<nsCSSValuePair>(*result);
  2611. aResultValue.SetAndAdoptCSSValuePairValue(heapResult.release(),
  2612. eUnit_CSSValuePair);
  2613. return true;
  2614. }
  2615. case eUnit_CSSValueTriplet: {
  2616. nsCSSValueTriplet triplet1(*aValue1.GetCSSValueTripletValue());
  2617. nsCSSValueTriplet triplet2(*aValue2.GetCSSValueTripletValue());
  2618. nsCSSUnit unit[3];
  2619. unit[0] = GetCommonUnit(aProperty, triplet1.mXValue.GetUnit(),
  2620. triplet2.mXValue.GetUnit());
  2621. unit[1] = GetCommonUnit(aProperty, triplet1.mYValue.GetUnit(),
  2622. triplet2.mYValue.GetUnit());
  2623. unit[2] = GetCommonUnit(aProperty, triplet1.mZValue.GetUnit(),
  2624. triplet2.mZValue.GetUnit());
  2625. if (unit[0] == eCSSUnit_Null || unit[1] == eCSSUnit_Null ||
  2626. unit[2] == eCSSUnit_Null) {
  2627. return false;
  2628. }
  2629. nsAutoPtr<nsCSSValueTriplet> result(new nsCSSValueTriplet);
  2630. static nsCSSValue nsCSSValueTriplet::* const tripletValues[3] = {
  2631. &nsCSSValueTriplet::mXValue, &nsCSSValueTriplet::mYValue, &nsCSSValueTriplet::mZValue
  2632. };
  2633. uint32_t restrictions = nsCSSProps::ValueRestrictions(aProperty);
  2634. for (uint32_t i = 0; i < 3; ++i) {
  2635. nsCSSValue nsCSSValueTriplet::*member = tripletValues[i];
  2636. if (!AddCSSValuePixelPercentCalc(restrictions, unit[i],
  2637. aCoeff1, &triplet1->*member,
  2638. aCoeff2, &triplet2->*member,
  2639. result->*member) ) {
  2640. MOZ_ASSERT(false, "unexpected unit");
  2641. return false;
  2642. }
  2643. }
  2644. aResultValue.SetAndAdoptCSSValueTripletValue(result.forget(),
  2645. eUnit_CSSValueTriplet);
  2646. return true;
  2647. }
  2648. case eUnit_CSSRect: {
  2649. MOZ_ASSERT(nsCSSProps::ValueRestrictions(aProperty) == 0,
  2650. "must add code for handling value restrictions");
  2651. const nsCSSRect *rect1 = aValue1.GetCSSRectValue();
  2652. const nsCSSRect *rect2 = aValue2.GetCSSRectValue();
  2653. if (rect1->mTop.GetUnit() != rect2->mTop.GetUnit() ||
  2654. rect1->mRight.GetUnit() != rect2->mRight.GetUnit() ||
  2655. rect1->mBottom.GetUnit() != rect2->mBottom.GetUnit() ||
  2656. rect1->mLeft.GetUnit() != rect2->mLeft.GetUnit()) {
  2657. // At least until we have calc()
  2658. return false;
  2659. }
  2660. nsAutoPtr<nsCSSRect> result(new nsCSSRect);
  2661. for (uint32_t i = 0; i < ArrayLength(nsCSSRect::sides); ++i) {
  2662. nsCSSValue nsCSSRect::*member = nsCSSRect::sides[i];
  2663. MOZ_ASSERT((rect1->*member).GetUnit() == (rect2->*member).GetUnit(),
  2664. "should have returned above");
  2665. switch ((rect1->*member).GetUnit()) {
  2666. case eCSSUnit_Pixel:
  2667. AddCSSValuePixel(aCoeff1, rect1->*member, aCoeff2, rect2->*member,
  2668. result->*member);
  2669. break;
  2670. case eCSSUnit_Auto:
  2671. if (float(aCoeff1 + aCoeff2) != 1.0f) {
  2672. // Interpolating between two auto values makes sense;
  2673. // adding in other ratios does not.
  2674. return false;
  2675. }
  2676. (result->*member).SetAutoValue();
  2677. break;
  2678. default:
  2679. MOZ_ASSERT(false, "unexpected unit");
  2680. return false;
  2681. }
  2682. }
  2683. aResultValue.SetAndAdoptCSSRectValue(result.forget(), eUnit_CSSRect);
  2684. return true;
  2685. }
  2686. case eUnit_Dasharray: {
  2687. const nsCSSValueList *list1 = aValue1.GetCSSValueListValue();
  2688. const nsCSSValueList *list2 = aValue2.GetCSSValueListValue();
  2689. uint32_t len1 = 0, len2 = 0;
  2690. for (const nsCSSValueList *v = list1; v; v = v->mNext) {
  2691. ++len1;
  2692. }
  2693. for (const nsCSSValueList *v = list2; v; v = v->mNext) {
  2694. ++len2;
  2695. }
  2696. MOZ_ASSERT(len1 > 0 && len2 > 0, "unexpected length");
  2697. nsAutoPtr<nsCSSValueList> result;
  2698. nsCSSValueList **resultTail = getter_Transfers(result);
  2699. for (uint32_t i = 0, i_end = EuclidLCM<uint32_t>(len1, len2); i != i_end; ++i) {
  2700. const nsCSSValue &v1 = list1->mValue;
  2701. const nsCSSValue &v2 = list2->mValue;
  2702. MOZ_ASSERT(v1.GetUnit() == eCSSUnit_Number ||
  2703. v1.GetUnit() == eCSSUnit_Percent, "unexpected");
  2704. MOZ_ASSERT(v2.GetUnit() == eCSSUnit_Number ||
  2705. v2.GetUnit() == eCSSUnit_Percent, "unexpected");
  2706. if (v1.GetUnit() != v2.GetUnit()) {
  2707. // Can't animate between lengths and percentages (until calc()).
  2708. return false;
  2709. }
  2710. nsCSSValueList *item = new nsCSSValueList;
  2711. *resultTail = item;
  2712. resultTail = &item->mNext;
  2713. if (v1.GetUnit() == eCSSUnit_Number) {
  2714. AddCSSValueNumber(aCoeff1, v1, aCoeff2, v2, item->mValue,
  2715. CSS_PROPERTY_VALUE_NONNEGATIVE);
  2716. } else {
  2717. AddCSSValuePercent(aCoeff1, v1, aCoeff2, v2, item->mValue,
  2718. CSS_PROPERTY_VALUE_NONNEGATIVE);
  2719. }
  2720. list1 = list1->mNext;
  2721. if (!list1) {
  2722. list1 = aValue1.GetCSSValueListValue();
  2723. }
  2724. list2 = list2->mNext;
  2725. if (!list2) {
  2726. list2 = aValue2.GetCSSValueListValue();
  2727. }
  2728. }
  2729. aResultValue.SetAndAdoptCSSValueListValue(result.forget(),
  2730. eUnit_Dasharray);
  2731. return true;
  2732. }
  2733. case eUnit_Shadow: {
  2734. UniquePtr<nsCSSValueList> result =
  2735. AddWeightedShadowList(aCoeff1,
  2736. aValue1.GetCSSValueListValue(),
  2737. aCoeff2,
  2738. aValue2.GetCSSValueListValue(),
  2739. ColorAdditionType::Clamped);
  2740. if (!result) {
  2741. return false;
  2742. }
  2743. aResultValue.SetAndAdoptCSSValueListValue(result.release(), eUnit_Shadow);
  2744. return true;
  2745. }
  2746. case eUnit_Shape: {
  2747. RefPtr<nsCSSValue::Array> result =
  2748. AddShapeFunction(aProperty,
  2749. aCoeff1, aValue1.GetCSSValueArrayValue(),
  2750. aCoeff2, aValue2.GetCSSValueArrayValue());
  2751. if (!result) {
  2752. return false;
  2753. }
  2754. aResultValue.SetCSSValueArrayValue(result, eUnit_Shape);
  2755. return true;
  2756. }
  2757. case eUnit_Filter: {
  2758. UniquePtr<nsCSSValueList> result =
  2759. AddWeightedFilterList(aCoeff1, aValue1.GetCSSValueListValue(),
  2760. aCoeff2, aValue2.GetCSSValueListValue(),
  2761. ColorAdditionType::Clamped);
  2762. if (!result) {
  2763. return false;
  2764. }
  2765. aResultValue.SetAndAdoptCSSValueListValue(result.release(),
  2766. eUnit_Filter);
  2767. return true;
  2768. }
  2769. case eUnit_Transform: {
  2770. const nsCSSValueList* list1 = aValue1.GetCSSValueSharedListValue()->mHead;
  2771. const nsCSSValueList* list2 = aValue2.GetCSSValueSharedListValue()->mHead;
  2772. MOZ_ASSERT(list1);
  2773. MOZ_ASSERT(list2);
  2774. // We want to avoid the matrix decomposition when we can, since
  2775. // avoiding it can produce better results both for compound
  2776. // transforms and for skew and skewY (see below). We can do this
  2777. // in two cases:
  2778. // (1) if one of the transforms is 'none'
  2779. // (2) if the lists have the same length and the transform
  2780. // functions match
  2781. nsAutoPtr<nsCSSValueList> result;
  2782. if (list1->mValue.GetUnit() == eCSSUnit_None) {
  2783. if (list2->mValue.GetUnit() == eCSSUnit_None) {
  2784. result = new nsCSSValueList;
  2785. if (result) {
  2786. result->mValue.SetNoneValue();
  2787. }
  2788. } else {
  2789. result = AddTransformLists(0, list2, aCoeff2, list2);
  2790. }
  2791. } else {
  2792. if (list2->mValue.GetUnit() == eCSSUnit_None) {
  2793. result = AddTransformLists(0, list1, aCoeff1, list1);
  2794. } else {
  2795. bool match = true;
  2796. {
  2797. const nsCSSValueList *item1 = list1, *item2 = list2;
  2798. do {
  2799. nsCSSKeyword func1 = nsStyleTransformMatrix::TransformFunctionOf(
  2800. item1->mValue.GetArrayValue());
  2801. nsCSSKeyword func2 = nsStyleTransformMatrix::TransformFunctionOf(
  2802. item2->mValue.GetArrayValue());
  2803. if (!TransformFunctionsMatch(func1, func2)) {
  2804. break;
  2805. }
  2806. item1 = item1->mNext;
  2807. item2 = item2->mNext;
  2808. } while (item1 && item2);
  2809. if (item1 || item2) {
  2810. // Either |break| above or length mismatch.
  2811. match = false;
  2812. }
  2813. }
  2814. if (match) {
  2815. result = AddTransformLists(aCoeff1, list1, aCoeff2, list2);
  2816. } else {
  2817. result = AddDifferentTransformLists(aCoeff1, list1, aCoeff2, list2);
  2818. }
  2819. }
  2820. }
  2821. aResultValue.SetTransformValue(new nsCSSValueSharedList(result.forget()));
  2822. return true;
  2823. }
  2824. case eUnit_BackgroundPositionCoord: {
  2825. const nsCSSValueList *position1 = aValue1.GetCSSValueListValue();
  2826. const nsCSSValueList *position2 = aValue2.GetCSSValueListValue();
  2827. nsAutoPtr<nsCSSValueList> result;
  2828. nsCSSValueList **resultTail = getter_Transfers(result);
  2829. while (position1 && position2) {
  2830. nsCSSValueList *item = new nsCSSValueList;
  2831. *resultTail = item;
  2832. resultTail = &item->mNext;
  2833. AddPositionCoords(aCoeff1, position1->mValue,
  2834. aCoeff2, position2->mValue, item->mValue);
  2835. position1 = position1->mNext;
  2836. position2 = position2->mNext;
  2837. }
  2838. // Check for different lengths
  2839. if (position1 || position2) {
  2840. return false;
  2841. }
  2842. aResultValue.SetAndAdoptCSSValueListValue(result.forget(),
  2843. eUnit_BackgroundPositionCoord);
  2844. return true;
  2845. }
  2846. case eUnit_CSSValuePairList: {
  2847. const nsCSSValuePairList *list1 = aValue1.GetCSSValuePairListValue();
  2848. const nsCSSValuePairList *list2 = aValue2.GetCSSValuePairListValue();
  2849. UniquePtr<nsCSSValuePairList> result =
  2850. AddCSSValuePairList(aProperty, aCoeff1, list1, aCoeff2, list2);
  2851. if (!result) {
  2852. return false;
  2853. }
  2854. aResultValue.SetAndAdoptCSSValuePairListValue(result.release());
  2855. return true;
  2856. }
  2857. }
  2858. MOZ_ASSERT(false, "Can't interpolate using the given common unit");
  2859. return false;
  2860. }
  2861. bool
  2862. StyleAnimationValue::Accumulate(nsCSSPropertyID aProperty,
  2863. StyleAnimationValue& aDest,
  2864. const StyleAnimationValue& aValueToAccumulate,
  2865. uint64_t aCount)
  2866. {
  2867. Unit commonUnit =
  2868. GetCommonUnit(aProperty, aDest.GetUnit(), aValueToAccumulate.GetUnit());
  2869. switch (commonUnit) {
  2870. case eUnit_Filter: {
  2871. UniquePtr<nsCSSValueList> result =
  2872. AddWeightedFilterList(1.0, aDest.GetCSSValueListValue(),
  2873. aCount, aValueToAccumulate.GetCSSValueListValue(),
  2874. ColorAdditionType::Unclamped);
  2875. if (!result) {
  2876. return false;
  2877. }
  2878. aDest.SetAndAdoptCSSValueListValue(result.release(), eUnit_Filter);
  2879. return true;
  2880. }
  2881. case eUnit_Shadow: {
  2882. UniquePtr<nsCSSValueList> result =
  2883. AddWeightedShadowList(1.0, aDest.GetCSSValueListValue(),
  2884. aCount, aValueToAccumulate.GetCSSValueListValue(),
  2885. ColorAdditionType::Unclamped);
  2886. if (!result) {
  2887. return false;
  2888. }
  2889. aDest.SetAndAdoptCSSValueListValue(result.release(), eUnit_Shadow);
  2890. return true;
  2891. }
  2892. case eUnit_Color: {
  2893. RGBAColorData color1 = ExtractColor(aDest);
  2894. RGBAColorData color2 = ExtractColor(aValueToAccumulate);
  2895. auto resultColor = MakeUnique<nsCSSValue>();
  2896. resultColor->SetRGBAColorValue(
  2897. AddWeightedColors(1.0, color1, aCount, color2));
  2898. aDest.SetAndAdoptCSSValueValue(resultColor.release(), eUnit_Color);
  2899. return true;
  2900. }
  2901. default:
  2902. return Add(aProperty, aDest, aValueToAccumulate, aCount);
  2903. }
  2904. MOZ_ASSERT_UNREACHABLE("Can't accumulate using the given common unit");
  2905. return false;
  2906. }
  2907. already_AddRefed<css::StyleRule>
  2908. BuildStyleRule(nsCSSPropertyID aProperty,
  2909. dom::Element* aTargetElement,
  2910. const nsAString& aSpecifiedValue,
  2911. bool aUseSVGMode)
  2912. {
  2913. // Set up an empty CSS Declaration
  2914. RefPtr<css::Declaration> declaration(new css::Declaration());
  2915. declaration->InitializeEmpty();
  2916. bool changed; // ignored, but needed as outparam for ParseProperty
  2917. nsIDocument* doc = aTargetElement->OwnerDoc();
  2918. nsCOMPtr<nsIURI> baseURI = aTargetElement->GetBaseURI();
  2919. nsCSSParser parser(doc->CSSLoader());
  2920. nsCSSPropertyID propertyToCheck = nsCSSProps::IsShorthand(aProperty) ?
  2921. nsCSSProps::SubpropertyEntryFor(aProperty)[0] : aProperty;
  2922. // Get a parser, parse the property, and check for CSS parsing errors.
  2923. // If this fails, we bail out and delete the declaration.
  2924. parser.ParseProperty(aProperty, aSpecifiedValue, doc->GetDocumentURI(),
  2925. baseURI, aTargetElement->NodePrincipal(), declaration,
  2926. &changed, false, aUseSVGMode);
  2927. // check whether property parsed without CSS parsing errors
  2928. if (!declaration->HasNonImportantValueFor(propertyToCheck)) {
  2929. return nullptr;
  2930. }
  2931. RefPtr<css::StyleRule> rule = new css::StyleRule(nullptr,
  2932. declaration,
  2933. 0, 0);
  2934. return rule.forget();
  2935. }
  2936. already_AddRefed<css::StyleRule>
  2937. BuildStyleRule(nsCSSPropertyID aProperty,
  2938. dom::Element* aTargetElement,
  2939. const nsCSSValue& aSpecifiedValue,
  2940. bool aUseSVGMode)
  2941. {
  2942. MOZ_ASSERT(!nsCSSProps::IsShorthand(aProperty),
  2943. "Should be a longhand property");
  2944. // Check if longhand failed to parse correctly.
  2945. if (aSpecifiedValue.GetUnit() == eCSSUnit_Null) {
  2946. return nullptr;
  2947. }
  2948. // Set up an empty CSS Declaration
  2949. RefPtr<css::Declaration> declaration(new css::Declaration());
  2950. declaration->InitializeEmpty();
  2951. // Add our longhand value
  2952. nsCSSExpandedDataBlock block;
  2953. declaration->ExpandTo(&block);
  2954. block.AddLonghandProperty(aProperty, aSpecifiedValue);
  2955. declaration->ValueAppended(aProperty);
  2956. declaration->CompressFrom(&block);
  2957. RefPtr<css::StyleRule> rule = new css::StyleRule(nullptr, declaration, 0, 0);
  2958. return rule.forget();
  2959. }
  2960. static bool
  2961. ComputeValuesFromStyleContext(
  2962. nsCSSPropertyID aProperty,
  2963. CSSEnabledState aEnabledState,
  2964. nsStyleContext* aStyleContext,
  2965. nsTArray<PropertyStyleAnimationValuePair>& aValues)
  2966. {
  2967. // Extract computed value of our property (or all longhand components, if
  2968. // aProperty is a shorthand) from the temporary style context
  2969. if (nsCSSProps::IsShorthand(aProperty)) {
  2970. CSSPROPS_FOR_SHORTHAND_SUBPROPERTIES(p, aProperty, aEnabledState) {
  2971. if (nsCSSProps::kAnimTypeTable[*p] == eStyleAnimType_None) {
  2972. // Skip non-animatable component longhands.
  2973. continue;
  2974. }
  2975. PropertyStyleAnimationValuePair* pair = aValues.AppendElement();
  2976. pair->mProperty = *p;
  2977. if (!StyleAnimationValue::ExtractComputedValue(*p, aStyleContext,
  2978. pair->mValue)) {
  2979. return false;
  2980. }
  2981. }
  2982. return true;
  2983. }
  2984. PropertyStyleAnimationValuePair* pair = aValues.AppendElement();
  2985. pair->mProperty = aProperty;
  2986. return StyleAnimationValue::ExtractComputedValue(aProperty, aStyleContext,
  2987. pair->mValue);
  2988. }
  2989. static bool
  2990. ComputeValuesFromStyleRule(nsCSSPropertyID aProperty,
  2991. CSSEnabledState aEnabledState,
  2992. nsStyleContext* aStyleContext,
  2993. css::StyleRule* aStyleRule,
  2994. nsTArray<PropertyStyleAnimationValuePair>& aValues,
  2995. bool* aIsContextSensitive)
  2996. {
  2997. MOZ_ASSERT(aStyleContext);
  2998. if (!nsCSSProps::IsEnabled(aProperty, aEnabledState)) {
  2999. return false;
  3000. }
  3001. MOZ_ASSERT(aStyleContext->PresContext()->StyleSet()->IsGecko(),
  3002. "ServoStyleSet should not use StyleAnimationValue for animations");
  3003. nsStyleSet* styleSet = aStyleContext->PresContext()->StyleSet()->AsGecko();
  3004. RefPtr<nsStyleContext> tmpStyleContext;
  3005. if (aIsContextSensitive) {
  3006. MOZ_ASSERT(!nsCSSProps::IsShorthand(aProperty),
  3007. "to correctly set aIsContextSensitive for shorthand properties, "
  3008. "this code must be adjusted");
  3009. nsCOMArray<nsIStyleRule> ruleArray;
  3010. ruleArray.AppendObject(styleSet->InitialStyleRule());
  3011. css::Declaration* declaration = aStyleRule->GetDeclaration();
  3012. ruleArray.AppendObject(declaration);
  3013. declaration->SetImmutable();
  3014. tmpStyleContext =
  3015. styleSet->ResolveStyleByAddingRules(aStyleContext, ruleArray);
  3016. if (!tmpStyleContext) {
  3017. return false;
  3018. }
  3019. // Force walk of rule tree
  3020. nsStyleStructID sid = nsCSSProps::kSIDTable[aProperty];
  3021. tmpStyleContext->StyleData(sid);
  3022. // The rule node will have unconditional cached style data if the value is
  3023. // not context-sensitive. So if there's nothing cached, it's not context
  3024. // sensitive.
  3025. *aIsContextSensitive =
  3026. !tmpStyleContext->RuleNode()->NodeHasCachedUnconditionalData(sid);
  3027. }
  3028. // If we're not concerned whether the property is context sensitive then just
  3029. // add the rule to a new temporary style context alongside the target
  3030. // element's style context.
  3031. // Also, if we previously discovered that this property IS context-sensitive
  3032. // then we need to throw the temporary style context out since the property's
  3033. // value may have been biased by the 'initial' values supplied.
  3034. if (!aIsContextSensitive || *aIsContextSensitive) {
  3035. nsCOMArray<nsIStyleRule> ruleArray;
  3036. css::Declaration* declaration = aStyleRule->GetDeclaration();
  3037. ruleArray.AppendObject(declaration);
  3038. declaration->SetImmutable();
  3039. tmpStyleContext =
  3040. styleSet->ResolveStyleByAddingRules(aStyleContext, ruleArray);
  3041. if (!tmpStyleContext) {
  3042. return false;
  3043. }
  3044. }
  3045. return ComputeValuesFromStyleContext(aProperty, aEnabledState,
  3046. tmpStyleContext, aValues);
  3047. }
  3048. /* static */ bool
  3049. StyleAnimationValue::ComputeValue(nsCSSPropertyID aProperty,
  3050. dom::Element* aTargetElement,
  3051. nsStyleContext* aStyleContext,
  3052. const nsAString& aSpecifiedValue,
  3053. bool aUseSVGMode,
  3054. StyleAnimationValue& aComputedValue,
  3055. bool* aIsContextSensitive)
  3056. {
  3057. MOZ_ASSERT(aTargetElement, "null target element");
  3058. // Parse specified value into a temporary css::StyleRule
  3059. // Note: BuildStyleRule needs an element's OwnerDoc, BaseURI, and Principal.
  3060. // If it is a pseudo element, use its parent element's OwnerDoc, BaseURI,
  3061. // and Principal.
  3062. RefPtr<css::StyleRule> styleRule =
  3063. BuildStyleRule(aProperty, aTargetElement, aSpecifiedValue, aUseSVGMode);
  3064. if (!styleRule) {
  3065. return false;
  3066. }
  3067. if (nsCSSProps::IsShorthand(aProperty) ||
  3068. nsCSSProps::kAnimTypeTable[aProperty] == eStyleAnimType_None) {
  3069. // Just capture the specified value
  3070. aComputedValue.SetUnparsedStringValue(nsString(aSpecifiedValue));
  3071. if (aIsContextSensitive) {
  3072. // Since we're just returning the string as-is, aComputedValue isn't going
  3073. // to change depending on the context
  3074. *aIsContextSensitive = false;
  3075. }
  3076. return true;
  3077. }
  3078. AutoTArray<PropertyStyleAnimationValuePair,1> values;
  3079. bool ok = ComputeValuesFromStyleRule(aProperty,
  3080. CSSEnabledState::eIgnoreEnabledState,
  3081. aStyleContext, styleRule,
  3082. values, aIsContextSensitive);
  3083. if (!ok) {
  3084. return false;
  3085. }
  3086. MOZ_ASSERT(values.Length() == 1);
  3087. MOZ_ASSERT(values[0].mProperty == aProperty);
  3088. aComputedValue = values[0].mValue;
  3089. return true;
  3090. }
  3091. template <class T>
  3092. bool
  3093. ComputeValuesFromSpecifiedValue(
  3094. nsCSSPropertyID aProperty,
  3095. CSSEnabledState aEnabledState,
  3096. dom::Element* aTargetElement,
  3097. nsStyleContext* aStyleContext,
  3098. T& aSpecifiedValue,
  3099. bool aUseSVGMode,
  3100. nsTArray<PropertyStyleAnimationValuePair>& aResult)
  3101. {
  3102. MOZ_ASSERT(aTargetElement, "null target element");
  3103. // Parse specified value into a temporary css::StyleRule
  3104. // Note: BuildStyleRule needs an element's OwnerDoc, BaseURI, and Principal.
  3105. // If it is a pseudo element, use its parent element's OwnerDoc, BaseURI,
  3106. // and Principal.
  3107. RefPtr<css::StyleRule> styleRule =
  3108. BuildStyleRule(aProperty, aTargetElement, aSpecifiedValue, aUseSVGMode);
  3109. if (!styleRule) {
  3110. return false;
  3111. }
  3112. aResult.Clear();
  3113. return ComputeValuesFromStyleRule(aProperty, aEnabledState,
  3114. aStyleContext, styleRule, aResult,
  3115. /* aIsContextSensitive */ nullptr);
  3116. }
  3117. /* static */ bool
  3118. StyleAnimationValue::ComputeValues(
  3119. nsCSSPropertyID aProperty,
  3120. CSSEnabledState aEnabledState,
  3121. dom::Element* aTargetElement,
  3122. nsStyleContext* aStyleContext,
  3123. const nsAString& aSpecifiedValue,
  3124. bool aUseSVGMode,
  3125. nsTArray<PropertyStyleAnimationValuePair>& aResult)
  3126. {
  3127. return ComputeValuesFromSpecifiedValue(aProperty, aEnabledState,
  3128. aTargetElement, aStyleContext,
  3129. aSpecifiedValue, aUseSVGMode,
  3130. aResult);
  3131. }
  3132. /* static */ bool
  3133. StyleAnimationValue::ComputeValues(
  3134. nsCSSPropertyID aProperty,
  3135. CSSEnabledState aEnabledState,
  3136. dom::Element* aTargetElement,
  3137. nsStyleContext* aStyleContext,
  3138. const nsCSSValue& aSpecifiedValue,
  3139. bool aUseSVGMode,
  3140. nsTArray<PropertyStyleAnimationValuePair>& aResult)
  3141. {
  3142. return ComputeValuesFromSpecifiedValue(aProperty, aEnabledState,
  3143. aTargetElement, aStyleContext,
  3144. aSpecifiedValue, aUseSVGMode,
  3145. aResult);
  3146. }
  3147. /* static */ bool
  3148. StyleAnimationValue::ComputeValues(
  3149. nsCSSPropertyID aProperty,
  3150. CSSEnabledState aEnabledState,
  3151. nsStyleContext* aStyleContext,
  3152. const RawServoDeclarationBlock& aDeclarations,
  3153. nsTArray<PropertyStyleAnimationValuePair>& aValues)
  3154. {
  3155. MOZ_ASSERT(aStyleContext->PresContext()->StyleSet()->IsServo(),
  3156. "Should be using ServoStyleSet if we have a"
  3157. " RawServoDeclarationBlock");
  3158. if (!nsCSSProps::IsEnabled(aProperty, aEnabledState)) {
  3159. return false;
  3160. }
  3161. const ServoComputedValues* previousStyle =
  3162. aStyleContext->StyleSource().AsServoComputedValues();
  3163. // FIXME: Servo bindings don't yet represent const-ness so we just
  3164. // cast it away for now.
  3165. auto declarations = const_cast<RawServoDeclarationBlock*>(&aDeclarations);
  3166. RefPtr<ServoComputedValues> computedValues =
  3167. Servo_RestyleWithAddedDeclaration(declarations, previousStyle).Consume();
  3168. if (!computedValues) {
  3169. return false;
  3170. }
  3171. RefPtr<nsStyleContext> tmpStyleContext =
  3172. NS_NewStyleContext(aStyleContext, aStyleContext->PresContext(),
  3173. aStyleContext->GetPseudo(),
  3174. aStyleContext->GetPseudoType(),
  3175. computedValues.forget(),
  3176. false /* skipFixup */);
  3177. return ComputeValuesFromStyleContext(aProperty, aEnabledState,
  3178. tmpStyleContext, aValues);
  3179. }
  3180. bool
  3181. StyleAnimationValue::UncomputeValue(nsCSSPropertyID aProperty,
  3182. const StyleAnimationValue& aComputedValue,
  3183. nsCSSValue& aSpecifiedValue)
  3184. {
  3185. Unit unit = aComputedValue.GetUnit();
  3186. switch (unit) {
  3187. case eUnit_Normal:
  3188. aSpecifiedValue.SetNormalValue();
  3189. break;
  3190. case eUnit_Auto:
  3191. aSpecifiedValue.SetAutoValue();
  3192. break;
  3193. case eUnit_None:
  3194. aSpecifiedValue.SetNoneValue();
  3195. break;
  3196. case eUnit_Enumerated:
  3197. case eUnit_Visibility:
  3198. aSpecifiedValue.
  3199. SetIntValue(aComputedValue.GetIntValue(), eCSSUnit_Enumerated);
  3200. break;
  3201. case eUnit_Integer:
  3202. aSpecifiedValue.
  3203. SetIntValue(aComputedValue.GetIntValue(), eCSSUnit_Integer);
  3204. break;
  3205. case eUnit_Coord:
  3206. aSpecifiedValue.SetIntegerCoordValue(aComputedValue.GetCoordValue());
  3207. break;
  3208. case eUnit_Percent:
  3209. aSpecifiedValue.SetPercentValue(aComputedValue.GetPercentValue());
  3210. break;
  3211. case eUnit_Float:
  3212. aSpecifiedValue.
  3213. SetFloatValue(aComputedValue.GetFloatValue(), eCSSUnit_Number);
  3214. break;
  3215. case eUnit_CurrentColor:
  3216. aSpecifiedValue.SetIntValue(NS_COLOR_CURRENTCOLOR, eCSSUnit_EnumColor);
  3217. break;
  3218. case eUnit_Calc:
  3219. case eUnit_Color:
  3220. case eUnit_ObjectPosition:
  3221. case eUnit_URL:
  3222. case eUnit_DiscreteCSSValue: {
  3223. nsCSSValue* val = aComputedValue.GetCSSValueValue();
  3224. // Sanity-check that the underlying unit in the nsCSSValue is what we
  3225. // expect for our StyleAnimationValue::Unit:
  3226. MOZ_ASSERT((unit == eUnit_Calc && val->GetUnit() == eCSSUnit_Calc) ||
  3227. (unit == eUnit_Color &&
  3228. nsCSSValue::IsNumericColorUnit(val->GetUnit())) ||
  3229. (unit == eUnit_ObjectPosition &&
  3230. val->GetUnit() == eCSSUnit_Array) ||
  3231. (unit == eUnit_URL && val->GetUnit() == eCSSUnit_URL) ||
  3232. unit == eUnit_DiscreteCSSValue,
  3233. "unexpected unit");
  3234. aSpecifiedValue = *val;
  3235. break;
  3236. }
  3237. case eUnit_ComplexColor: {
  3238. aSpecifiedValue.SetComplexColorValue(
  3239. do_AddRef(aComputedValue.mValue.mComplexColor));
  3240. break;
  3241. }
  3242. case eUnit_CSSValuePair: {
  3243. // Rule node processing expects pair values to be collapsed to a
  3244. // single value if both halves would be equal, for most but not
  3245. // all properties. At present, all animatable properties that
  3246. // use pairs do expect collapsing.
  3247. const nsCSSValuePair* pair = aComputedValue.GetCSSValuePairValue();
  3248. if (pair->mXValue == pair->mYValue) {
  3249. aSpecifiedValue = pair->mXValue;
  3250. } else {
  3251. aSpecifiedValue.SetPairValue(pair);
  3252. }
  3253. } break;
  3254. case eUnit_CSSValueTriplet: {
  3255. // Rule node processing expects triplet values to be collapsed to a
  3256. // single value if both halves would be equal, for most but not
  3257. // all properties. At present, all animatable properties that
  3258. // use pairs do expect collapsing.
  3259. const nsCSSValueTriplet* triplet = aComputedValue.GetCSSValueTripletValue();
  3260. if (triplet->mXValue == triplet->mYValue && triplet->mYValue == triplet->mZValue) {
  3261. aSpecifiedValue = triplet->mXValue;
  3262. } else {
  3263. aSpecifiedValue.SetTripletValue(triplet);
  3264. }
  3265. } break;
  3266. case eUnit_CSSRect: {
  3267. nsCSSRect& rect = aSpecifiedValue.SetRectValue();
  3268. rect = *aComputedValue.GetCSSRectValue();
  3269. } break;
  3270. case eUnit_Dasharray:
  3271. case eUnit_Shadow:
  3272. case eUnit_Filter:
  3273. case eUnit_BackgroundPositionCoord:
  3274. {
  3275. nsCSSValueList* computedList = aComputedValue.GetCSSValueListValue();
  3276. if (computedList) {
  3277. aSpecifiedValue.SetDependentListValue(computedList);
  3278. } else {
  3279. aSpecifiedValue.SetNoneValue();
  3280. }
  3281. }
  3282. break;
  3283. case eUnit_Shape: {
  3284. nsCSSValue::Array* computedArray = aComputedValue.GetCSSValueArrayValue();
  3285. aSpecifiedValue.SetArrayValue(computedArray, eCSSUnit_Array);
  3286. break;
  3287. }
  3288. case eUnit_Transform:
  3289. aSpecifiedValue.
  3290. SetSharedListValue(aComputedValue.GetCSSValueSharedListValue());
  3291. break;
  3292. case eUnit_CSSValuePairList:
  3293. aSpecifiedValue.
  3294. SetDependentPairListValue(aComputedValue.GetCSSValuePairListValue());
  3295. break;
  3296. default:
  3297. return false;
  3298. }
  3299. return true;
  3300. }
  3301. bool
  3302. StyleAnimationValue::UncomputeValue(nsCSSPropertyID aProperty,
  3303. StyleAnimationValue&& aComputedValue,
  3304. nsCSSValue& aSpecifiedValue)
  3305. {
  3306. Unit unit = aComputedValue.GetUnit();
  3307. switch (unit) {
  3308. case eUnit_Dasharray:
  3309. case eUnit_Shadow:
  3310. case eUnit_Filter:
  3311. case eUnit_BackgroundPositionCoord:
  3312. {
  3313. UniquePtr<nsCSSValueList> computedList =
  3314. aComputedValue.TakeCSSValueListValue();
  3315. if (computedList) {
  3316. aSpecifiedValue.AdoptListValue(Move(computedList));
  3317. } else {
  3318. aSpecifiedValue.SetNoneValue();
  3319. }
  3320. }
  3321. break;
  3322. case eUnit_CSSValuePairList:
  3323. {
  3324. UniquePtr<nsCSSValuePairList> computedList =
  3325. aComputedValue.TakeCSSValuePairListValue();
  3326. MOZ_ASSERT(computedList, "Pair list should never be null");
  3327. aSpecifiedValue.AdoptPairListValue(Move(computedList));
  3328. }
  3329. break;
  3330. default:
  3331. return UncomputeValue(aProperty, aComputedValue, aSpecifiedValue);
  3332. }
  3333. return true;
  3334. }
  3335. bool
  3336. StyleAnimationValue::UncomputeValue(nsCSSPropertyID aProperty,
  3337. const StyleAnimationValue& aComputedValue,
  3338. nsAString& aSpecifiedValue)
  3339. {
  3340. aSpecifiedValue.Truncate(); // Clear outparam, if it's not already empty
  3341. if (aComputedValue.GetUnit() == eUnit_UnparsedString) {
  3342. aComputedValue.GetStringValue(aSpecifiedValue);
  3343. return true;
  3344. }
  3345. nsCSSValue val;
  3346. if (!StyleAnimationValue::UncomputeValue(aProperty, aComputedValue, val)) {
  3347. return false;
  3348. }
  3349. val.AppendToString(aProperty, aSpecifiedValue, nsCSSValue::eNormalized);
  3350. return true;
  3351. }
  3352. template<typename T>
  3353. inline const T&
  3354. StyleDataAtOffset(const void* aStyleStruct, ptrdiff_t aOffset)
  3355. {
  3356. return *reinterpret_cast<const T*>(
  3357. reinterpret_cast<const uint8_t*>(aStyleStruct) + aOffset);
  3358. }
  3359. static bool
  3360. StyleCoordToValue(const nsStyleCoord& aCoord, StyleAnimationValue& aValue)
  3361. {
  3362. switch (aCoord.GetUnit()) {
  3363. case eStyleUnit_Normal:
  3364. aValue.SetNormalValue();
  3365. break;
  3366. case eStyleUnit_Auto:
  3367. aValue.SetAutoValue();
  3368. break;
  3369. case eStyleUnit_None:
  3370. aValue.SetNoneValue();
  3371. break;
  3372. case eStyleUnit_Percent:
  3373. aValue.SetPercentValue(aCoord.GetPercentValue());
  3374. break;
  3375. case eStyleUnit_Factor:
  3376. aValue.SetFloatValue(aCoord.GetFactorValue());
  3377. break;
  3378. case eStyleUnit_Coord:
  3379. aValue.SetCoordValue(aCoord.GetCoordValue());
  3380. break;
  3381. case eStyleUnit_Enumerated:
  3382. aValue.SetIntValue(aCoord.GetIntValue(),
  3383. StyleAnimationValue::eUnit_Enumerated);
  3384. break;
  3385. case eStyleUnit_Integer:
  3386. aValue.SetIntValue(aCoord.GetIntValue(),
  3387. StyleAnimationValue::eUnit_Integer);
  3388. break;
  3389. case eStyleUnit_Calc: {
  3390. nsAutoPtr<nsCSSValue> val(new nsCSSValue);
  3391. CalcValueToCSSValue(aCoord.GetCalcValue(), *val);
  3392. aValue.SetAndAdoptCSSValueValue(val.forget(),
  3393. StyleAnimationValue::eUnit_Calc);
  3394. break;
  3395. }
  3396. default:
  3397. return false;
  3398. }
  3399. return true;
  3400. }
  3401. static bool
  3402. StyleCoordToCSSValue(const nsStyleCoord& aCoord, nsCSSValue& aCSSValue)
  3403. {
  3404. switch (aCoord.GetUnit()) {
  3405. case eStyleUnit_Coord:
  3406. aCSSValue.SetIntegerCoordValue(aCoord.GetCoordValue());
  3407. break;
  3408. case eStyleUnit_Factor:
  3409. aCSSValue.SetFloatValue(aCoord.GetFactorValue(), eCSSUnit_Number);
  3410. break;
  3411. case eStyleUnit_Percent:
  3412. aCSSValue.SetPercentValue(aCoord.GetPercentValue());
  3413. break;
  3414. case eStyleUnit_Calc:
  3415. CalcValueToCSSValue(aCoord.GetCalcValue(), aCSSValue);
  3416. break;
  3417. case eStyleUnit_Degree:
  3418. aCSSValue.SetFloatValue(aCoord.GetAngleValue(), eCSSUnit_Degree);
  3419. break;
  3420. case eStyleUnit_Grad:
  3421. aCSSValue.SetFloatValue(aCoord.GetAngleValue(), eCSSUnit_Grad);
  3422. break;
  3423. case eStyleUnit_Radian:
  3424. aCSSValue.SetFloatValue(aCoord.GetAngleValue(), eCSSUnit_Radian);
  3425. break;
  3426. case eStyleUnit_Turn:
  3427. aCSSValue.SetFloatValue(aCoord.GetAngleValue(), eCSSUnit_Turn);
  3428. break;
  3429. default:
  3430. MOZ_ASSERT(false, "unexpected unit");
  3431. return false;
  3432. }
  3433. return true;
  3434. }
  3435. static void
  3436. SetPositionValue(const Position& aPos, nsCSSValue& aCSSValue)
  3437. {
  3438. RefPtr<nsCSSValue::Array> posArray = nsCSSValue::Array::Create(4);
  3439. aCSSValue.SetArrayValue(posArray.get(), eCSSUnit_Array);
  3440. // NOTE: Array entries #0 and #2 here are intentionally left untouched, with
  3441. // eCSSUnit_Null. The purpose of these entries in our specified-style
  3442. // <position> representation is to store edge names. But for values
  3443. // extracted from computed style (which is what we're dealing with here),
  3444. // we'll just have a normalized "x,y" position, with no edge names needed.
  3445. nsCSSValue& xValue = posArray->Item(1);
  3446. nsCSSValue& yValue = posArray->Item(3);
  3447. CalcValueToCSSValue(&aPos.mXPosition, xValue);
  3448. CalcValueToCSSValue(&aPos.mYPosition, yValue);
  3449. }
  3450. static void
  3451. SetPositionCoordValue(const Position::Coord& aPosCoord,
  3452. nsCSSValue& aCSSValue)
  3453. {
  3454. RefPtr<nsCSSValue::Array> posArray = nsCSSValue::Array::Create(2);
  3455. aCSSValue.SetArrayValue(posArray.get(), eCSSUnit_Array);
  3456. // NOTE: Array entry #0 here is intentionally left untouched, with
  3457. // eCSSUnit_Null. The purpose of this entry in our specified-style
  3458. // <position-coord> representation is to store edge names. But for values
  3459. // extracted from computed style (which is what we're dealing with here),
  3460. // we'll just have a normalized "x"/"y" position, with no edge names needed.
  3461. nsCSSValue& value = posArray->Item(1);
  3462. CalcValueToCSSValue(&aPosCoord, value);
  3463. }
  3464. /*
  3465. * Assign |aOutput = aInput|, except with any non-pixel lengths
  3466. * replaced with the equivalent in pixels, and any non-canonical calc()
  3467. * expressions replaced with canonical ones.
  3468. */
  3469. static void
  3470. SubstitutePixelValues(nsStyleContext* aStyleContext,
  3471. const nsCSSValue& aInput, nsCSSValue& aOutput)
  3472. {
  3473. if (aInput.IsCalcUnit()) {
  3474. RuleNodeCacheConditions conditions;
  3475. nsRuleNode::ComputedCalc c =
  3476. nsRuleNode::SpecifiedCalcToComputedCalc(aInput, aStyleContext,
  3477. aStyleContext->PresContext(),
  3478. conditions);
  3479. nsStyleCoord::CalcValue c2;
  3480. c2.mLength = c.mLength;
  3481. c2.mPercent = c.mPercent;
  3482. c2.mHasPercent = true; // doesn't matter for transform translate
  3483. CalcValueToCSSValue(&c2, aOutput);
  3484. } else if (aInput.UnitHasArrayValue()) {
  3485. const nsCSSValue::Array *inputArray = aInput.GetArrayValue();
  3486. RefPtr<nsCSSValue::Array> outputArray =
  3487. nsCSSValue::Array::Create(inputArray->Count());
  3488. for (size_t i = 0, i_end = inputArray->Count(); i < i_end; ++i) {
  3489. SubstitutePixelValues(aStyleContext,
  3490. inputArray->Item(i), outputArray->Item(i));
  3491. }
  3492. aOutput.SetArrayValue(outputArray, aInput.GetUnit());
  3493. } else if (aInput.IsLengthUnit() &&
  3494. aInput.GetUnit() != eCSSUnit_Pixel) {
  3495. RuleNodeCacheConditions conditions;
  3496. nscoord len = nsRuleNode::CalcLength(aInput, aStyleContext,
  3497. aStyleContext->PresContext(),
  3498. conditions);
  3499. aOutput.SetFloatValue(nsPresContext::AppUnitsToFloatCSSPixels(len),
  3500. eCSSUnit_Pixel);
  3501. } else {
  3502. aOutput = aInput;
  3503. }
  3504. }
  3505. static void
  3506. ExtractImageLayerPositionXList(const nsStyleImageLayers& aLayer,
  3507. StyleAnimationValue& aComputedValue)
  3508. {
  3509. MOZ_ASSERT(aLayer.mPositionXCount > 0, "unexpected count");
  3510. nsAutoPtr<nsCSSValueList> result;
  3511. nsCSSValueList **resultTail = getter_Transfers(result);
  3512. for (uint32_t i = 0, i_end = aLayer.mPositionXCount; i != i_end; ++i) {
  3513. nsCSSValueList *item = new nsCSSValueList;
  3514. *resultTail = item;
  3515. resultTail = &item->mNext;
  3516. SetPositionCoordValue(aLayer.mLayers[i].mPosition.mXPosition,
  3517. item->mValue);
  3518. }
  3519. aComputedValue.SetAndAdoptCSSValueListValue(result.forget(),
  3520. StyleAnimationValue::eUnit_BackgroundPositionCoord);
  3521. }
  3522. static void
  3523. ExtractImageLayerPositionYList(const nsStyleImageLayers& aLayer,
  3524. StyleAnimationValue& aComputedValue)
  3525. {
  3526. MOZ_ASSERT(aLayer.mPositionYCount > 0, "unexpected count");
  3527. nsAutoPtr<nsCSSValueList> result;
  3528. nsCSSValueList **resultTail = getter_Transfers(result);
  3529. for (uint32_t i = 0, i_end = aLayer.mPositionYCount; i != i_end; ++i) {
  3530. nsCSSValueList *item = new nsCSSValueList;
  3531. *resultTail = item;
  3532. resultTail = &item->mNext;
  3533. SetPositionCoordValue(aLayer.mLayers[i].mPosition.mYPosition,
  3534. item->mValue);
  3535. }
  3536. aComputedValue.SetAndAdoptCSSValueListValue(result.forget(),
  3537. StyleAnimationValue::eUnit_BackgroundPositionCoord);
  3538. }
  3539. static void
  3540. ExtractImageLayerSizePairList(const nsStyleImageLayers& aLayer,
  3541. StyleAnimationValue& aComputedValue)
  3542. {
  3543. MOZ_ASSERT(aLayer.mSizeCount > 0, "unexpected count");
  3544. nsAutoPtr<nsCSSValuePairList> result;
  3545. nsCSSValuePairList **resultTail = getter_Transfers(result);
  3546. for (uint32_t i = 0, i_end = aLayer.mSizeCount; i != i_end; ++i) {
  3547. nsCSSValuePairList *item = new nsCSSValuePairList;
  3548. *resultTail = item;
  3549. resultTail = &item->mNext;
  3550. const nsStyleImageLayers::Size &size = aLayer.mLayers[i].mSize;
  3551. switch (size.mWidthType) {
  3552. case nsStyleImageLayers::Size::eContain:
  3553. case nsStyleImageLayers::Size::eCover:
  3554. item->mXValue.SetIntValue(size.mWidthType,
  3555. eCSSUnit_Enumerated);
  3556. break;
  3557. case nsStyleImageLayers::Size::eAuto:
  3558. item->mXValue.SetAutoValue();
  3559. break;
  3560. case nsStyleImageLayers::Size::eLengthPercentage:
  3561. // XXXbz is there a good reason we can't just
  3562. // CalcValueToCSSValue(&size.mWidth, item->mXValue) here?
  3563. if (!size.mWidth.mHasPercent &&
  3564. // negative values must have come from calc()
  3565. size.mWidth.mLength >= 0) {
  3566. MOZ_ASSERT(size.mWidth.mPercent == 0.0f,
  3567. "Shouldn't have mPercent");
  3568. item->mXValue.SetIntegerCoordValue(size.mWidth.mLength);
  3569. } else if (size.mWidth.mLength == 0 &&
  3570. // negative values must have come from calc()
  3571. size.mWidth.mPercent >= 0.0f) {
  3572. item->mXValue.SetPercentValue(size.mWidth.mPercent);
  3573. } else {
  3574. CalcValueToCSSValue(&size.mWidth, item->mXValue);
  3575. }
  3576. break;
  3577. }
  3578. switch (size.mHeightType) {
  3579. case nsStyleImageLayers::Size::eContain:
  3580. case nsStyleImageLayers::Size::eCover:
  3581. // leave it null
  3582. break;
  3583. case nsStyleImageLayers::Size::eAuto:
  3584. item->mYValue.SetAutoValue();
  3585. break;
  3586. case nsStyleImageLayers::Size::eLengthPercentage:
  3587. // XXXbz is there a good reason we can't just
  3588. // CalcValueToCSSValue(&size.mHeight, item->mYValue) here?
  3589. if (!size.mHeight.mHasPercent &&
  3590. // negative values must have come from calc()
  3591. size.mHeight.mLength >= 0) {
  3592. MOZ_ASSERT(size.mHeight.mPercent == 0.0f,
  3593. "Shouldn't have mPercent");
  3594. item->mYValue.SetIntegerCoordValue(size.mHeight.mLength);
  3595. } else if (size.mHeight.mLength == 0 &&
  3596. // negative values must have come from calc()
  3597. size.mHeight.mPercent >= 0.0f) {
  3598. item->mYValue.SetPercentValue(size.mHeight.mPercent);
  3599. } else {
  3600. CalcValueToCSSValue(&size.mHeight, item->mYValue);
  3601. }
  3602. break;
  3603. }
  3604. }
  3605. aComputedValue.SetAndAdoptCSSValuePairListValue(result.forget());
  3606. }
  3607. static bool
  3608. StyleClipBasicShapeToCSSArray(const StyleClipPath& aClipPath,
  3609. nsCSSValue::Array* aResult)
  3610. {
  3611. MOZ_ASSERT(aResult->Count() == 2,
  3612. "Expected array to be presized for a function and the sizing-box");
  3613. const StyleBasicShape* shape = aClipPath.GetBasicShape();
  3614. nsCSSKeyword functionName = shape->GetShapeTypeName();
  3615. RefPtr<nsCSSValue::Array> functionArray;
  3616. switch (shape->GetShapeType()) {
  3617. case StyleBasicShapeType::Circle:
  3618. case StyleBasicShapeType::Ellipse: {
  3619. const nsTArray<nsStyleCoord>& coords = shape->Coordinates();
  3620. MOZ_ASSERT(coords.Length() == ShapeArgumentCount(functionName) - 1,
  3621. "Unexpected radii count");
  3622. // The "+1" is for the center point:
  3623. functionArray = aResult->Item(0).InitFunction(functionName,
  3624. coords.Length() + 1);
  3625. for (size_t i = 0; i < coords.Length(); ++i) {
  3626. if (coords[i].GetUnit() == eStyleUnit_Enumerated) {
  3627. functionArray->Item(i + 1).SetIntValue(coords[i].GetIntValue(),
  3628. eCSSUnit_Enumerated);
  3629. } else if (!StyleCoordToCSSValue(coords[i],
  3630. functionArray->Item(i + 1))) {
  3631. return false;
  3632. }
  3633. }
  3634. // Set functionArray's last item to the circle or ellipse's center point:
  3635. SetPositionValue(shape->GetPosition(),
  3636. functionArray->Item(functionArray->Count() - 1));
  3637. break;
  3638. }
  3639. case StyleBasicShapeType::Polygon: {
  3640. functionArray =
  3641. aResult->Item(0).InitFunction(functionName,
  3642. ShapeArgumentCount(functionName));
  3643. functionArray->Item(1).SetIntValue(shape->GetFillRule(),
  3644. eCSSUnit_Enumerated);
  3645. nsCSSValuePairList* list = functionArray->Item(2).SetPairListValue();
  3646. const nsTArray<nsStyleCoord>& coords = shape->Coordinates();
  3647. MOZ_ASSERT((coords.Length() % 2) == 0);
  3648. for (size_t i = 0; i < coords.Length(); i += 2) {
  3649. if (i > 0) {
  3650. list->mNext = new nsCSSValuePairList;
  3651. list = list->mNext;
  3652. }
  3653. if (!StyleCoordToCSSValue(coords[i], list->mXValue) ||
  3654. !StyleCoordToCSSValue(coords[i + 1], list->mYValue)) {
  3655. return false;
  3656. }
  3657. }
  3658. break;
  3659. }
  3660. case StyleBasicShapeType::Inset: {
  3661. const nsTArray<nsStyleCoord>& coords = shape->Coordinates();
  3662. MOZ_ASSERT(coords.Length() == ShapeArgumentCount(functionName) - 1,
  3663. "Unexpected offset count");
  3664. functionArray =
  3665. aResult->Item(0).InitFunction(functionName, coords.Length() + 1);
  3666. for (size_t i = 0; i < coords.Length(); ++i) {
  3667. if (!StyleCoordToCSSValue(coords[i], functionArray->Item(i + 1))) {
  3668. return false;
  3669. }
  3670. }
  3671. RefPtr<nsCSSValue::Array> radiusArray = nsCSSValue::Array::Create(4);
  3672. const nsStyleCorners& radii = shape->GetRadius();
  3673. NS_FOR_CSS_FULL_CORNERS(corner) {
  3674. auto pair = MakeUnique<nsCSSValuePair>();
  3675. if (!StyleCoordToCSSValue(radii.Get(NS_FULL_TO_HALF_CORNER(corner, false)),
  3676. pair->mXValue) ||
  3677. !StyleCoordToCSSValue(radii.Get(NS_FULL_TO_HALF_CORNER(corner, true)),
  3678. pair->mYValue)) {
  3679. return false;
  3680. }
  3681. radiusArray->Item(corner).SetPairValue(pair.get());
  3682. }
  3683. // Set the last item in functionArray to the radius array:
  3684. functionArray->Item(functionArray->Count() - 1).
  3685. SetArrayValue(radiusArray, eCSSUnit_Array);
  3686. break;
  3687. }
  3688. default:
  3689. MOZ_ASSERT_UNREACHABLE("Unknown shape type");
  3690. return false;
  3691. }
  3692. aResult->Item(1).SetIntValue(aClipPath.GetReferenceBox(),
  3693. eCSSUnit_Enumerated);
  3694. return true;
  3695. }
  3696. bool
  3697. StyleAnimationValue::ExtractComputedValue(nsCSSPropertyID aProperty,
  3698. nsStyleContext* aStyleContext,
  3699. StyleAnimationValue& aComputedValue)
  3700. {
  3701. MOZ_ASSERT(0 <= aProperty && aProperty < eCSSProperty_COUNT_no_shorthands,
  3702. "bad property");
  3703. const void* styleStruct =
  3704. aStyleContext->StyleData(nsCSSProps::kSIDTable[aProperty]);
  3705. ptrdiff_t ssOffset = nsCSSProps::kStyleStructOffsetTable[aProperty];
  3706. nsStyleAnimType animType = nsCSSProps::kAnimTypeTable[aProperty];
  3707. MOZ_ASSERT(0 <= ssOffset ||
  3708. animType == eStyleAnimType_Custom ||
  3709. animType == eStyleAnimType_Discrete,
  3710. "all animation types other than Custom and Discrete must " \
  3711. "specify a style struct offset to extract values from");
  3712. switch (animType) {
  3713. case eStyleAnimType_Custom:
  3714. switch (aProperty) {
  3715. // For border-width, ignore the border-image business (which
  3716. // only exists until we update our implementation to the current
  3717. // spec) and use GetComputedBorder
  3718. #define BORDER_WIDTH_CASE(prop_, side_) \
  3719. case prop_: \
  3720. aComputedValue.SetCoordValue( \
  3721. static_cast<const nsStyleBorder*>(styleStruct)-> \
  3722. GetComputedBorder().side_); \
  3723. break;
  3724. BORDER_WIDTH_CASE(eCSSProperty_border_bottom_width, bottom)
  3725. BORDER_WIDTH_CASE(eCSSProperty_border_left_width, left)
  3726. BORDER_WIDTH_CASE(eCSSProperty_border_right_width, right)
  3727. BORDER_WIDTH_CASE(eCSSProperty_border_top_width, top)
  3728. #undef BORDER_WIDTH_CASE
  3729. case eCSSProperty_column_rule_width:
  3730. aComputedValue.SetCoordValue(
  3731. static_cast<const nsStyleColumn*>(styleStruct)->
  3732. GetComputedColumnRuleWidth());
  3733. break;
  3734. case eCSSProperty_column_count: {
  3735. const nsStyleColumn *styleColumn =
  3736. static_cast<const nsStyleColumn*>(styleStruct);
  3737. if (styleColumn->mColumnCount == NS_STYLE_COLUMN_COUNT_AUTO) {
  3738. aComputedValue.SetAutoValue();
  3739. } else {
  3740. aComputedValue.SetIntValue(styleColumn->mColumnCount,
  3741. eUnit_Integer);
  3742. }
  3743. break;
  3744. }
  3745. case eCSSProperty_order: {
  3746. const nsStylePosition *stylePosition =
  3747. static_cast<const nsStylePosition*>(styleStruct);
  3748. aComputedValue.SetIntValue(stylePosition->mOrder,
  3749. eUnit_Integer);
  3750. break;
  3751. }
  3752. case eCSSProperty_border_spacing: {
  3753. const nsStyleTableBorder *styleTableBorder =
  3754. static_cast<const nsStyleTableBorder*>(styleStruct);
  3755. nsAutoPtr<nsCSSValuePair> pair(new nsCSSValuePair);
  3756. pair->mXValue.SetIntegerCoordValue(styleTableBorder->mBorderSpacingCol);
  3757. pair->mYValue.SetIntegerCoordValue(styleTableBorder->mBorderSpacingRow);
  3758. aComputedValue.SetAndAdoptCSSValuePairValue(pair.forget(),
  3759. eUnit_CSSValuePair);
  3760. break;
  3761. }
  3762. case eCSSProperty_transform_origin: {
  3763. const nsStyleDisplay *styleDisplay =
  3764. static_cast<const nsStyleDisplay*>(styleStruct);
  3765. nsAutoPtr<nsCSSValueTriplet> triplet(new nsCSSValueTriplet);
  3766. if (!StyleCoordToCSSValue(styleDisplay->mTransformOrigin[0],
  3767. triplet->mXValue) ||
  3768. !StyleCoordToCSSValue(styleDisplay->mTransformOrigin[1],
  3769. triplet->mYValue) ||
  3770. !StyleCoordToCSSValue(styleDisplay->mTransformOrigin[2],
  3771. triplet->mZValue)) {
  3772. return false;
  3773. }
  3774. aComputedValue.SetAndAdoptCSSValueTripletValue(triplet.forget(),
  3775. eUnit_CSSValueTriplet);
  3776. break;
  3777. }
  3778. case eCSSProperty_perspective_origin: {
  3779. const nsStyleDisplay *styleDisplay =
  3780. static_cast<const nsStyleDisplay*>(styleStruct);
  3781. nsAutoPtr<nsCSSValuePair> pair(new nsCSSValuePair);
  3782. if (!StyleCoordToCSSValue(styleDisplay->mPerspectiveOrigin[0],
  3783. pair->mXValue) ||
  3784. !StyleCoordToCSSValue(styleDisplay->mPerspectiveOrigin[1],
  3785. pair->mYValue)) {
  3786. return false;
  3787. }
  3788. aComputedValue.SetAndAdoptCSSValuePairValue(pair.forget(),
  3789. eUnit_CSSValuePair);
  3790. break;
  3791. }
  3792. case eCSSProperty_stroke_dasharray: {
  3793. const nsStyleSVG *svg = static_cast<const nsStyleSVG*>(styleStruct);
  3794. if (!svg->mStrokeDasharray.IsEmpty()) {
  3795. nsAutoPtr<nsCSSValueList> result;
  3796. nsCSSValueList **resultTail = getter_Transfers(result);
  3797. for (uint32_t i = 0, i_end = svg->mStrokeDasharray.Length();
  3798. i != i_end; ++i) {
  3799. nsCSSValueList *item = new nsCSSValueList;
  3800. *resultTail = item;
  3801. resultTail = &item->mNext;
  3802. const nsStyleCoord &coord = svg->mStrokeDasharray[i];
  3803. nsCSSValue &value = item->mValue;
  3804. switch (coord.GetUnit()) {
  3805. case eStyleUnit_Coord:
  3806. // Number means the same thing as length; we want to
  3807. // animate them the same way. Normalize both to number
  3808. // since it has more accuracy (float vs nscoord).
  3809. value.SetFloatValue(nsPresContext::
  3810. AppUnitsToFloatCSSPixels(coord.GetCoordValue()),
  3811. eCSSUnit_Number);
  3812. break;
  3813. case eStyleUnit_Factor:
  3814. value.SetFloatValue(coord.GetFactorValue(),
  3815. eCSSUnit_Number);
  3816. break;
  3817. case eStyleUnit_Percent:
  3818. value.SetPercentValue(coord.GetPercentValue());
  3819. break;
  3820. default:
  3821. MOZ_ASSERT(false, "unexpected unit");
  3822. return false;
  3823. }
  3824. }
  3825. aComputedValue.SetAndAdoptCSSValueListValue(result.forget(),
  3826. eUnit_Dasharray);
  3827. } else if (svg->StrokeDasharrayFromObject()) {
  3828. // An empty dasharray with StrokeDasharrayFromObject() == true
  3829. // corresponds to the "context-value" keyword.
  3830. aComputedValue.SetIntValue(NS_STYLE_STROKE_PROP_CONTEXT_VALUE,
  3831. eUnit_Enumerated);
  3832. } else {
  3833. // Otherwise, an empty dasharray corresponds to the "none" keyword.
  3834. aComputedValue.SetNoneValue();
  3835. }
  3836. break;
  3837. }
  3838. case eCSSProperty_font_stretch: {
  3839. int16_t stretch =
  3840. static_cast<const nsStyleFont*>(styleStruct)->mFont.stretch;
  3841. static_assert(NS_STYLE_FONT_STRETCH_ULTRA_CONDENSED == -4 &&
  3842. NS_STYLE_FONT_STRETCH_ULTRA_EXPANDED == 4,
  3843. "font stretch constants not as expected");
  3844. if (stretch < NS_STYLE_FONT_STRETCH_ULTRA_CONDENSED ||
  3845. stretch > NS_STYLE_FONT_STRETCH_ULTRA_EXPANDED) {
  3846. return false;
  3847. }
  3848. aComputedValue.SetIntValue(stretch, eUnit_Enumerated);
  3849. return true;
  3850. }
  3851. case eCSSProperty_font_weight: {
  3852. uint16_t weight =
  3853. static_cast<const nsStyleFont*>(styleStruct)->mFont.weight;
  3854. if (weight % 100 != 0) {
  3855. return false;
  3856. }
  3857. aComputedValue.SetIntValue(weight, eUnit_Integer);
  3858. return true;
  3859. }
  3860. case eCSSProperty_image_region: {
  3861. const nsStyleList *list =
  3862. static_cast<const nsStyleList*>(styleStruct);
  3863. const nsRect &srect = list->mImageRegion;
  3864. if (srect.IsEmpty()) {
  3865. aComputedValue.SetAutoValue();
  3866. break;
  3867. }
  3868. nsCSSRect *vrect = new nsCSSRect;
  3869. vrect->mLeft.SetIntegerCoordValue(srect.x);
  3870. vrect->mTop.SetIntegerCoordValue(srect.y);
  3871. vrect->mRight.SetIntegerCoordValue(srect.XMost());
  3872. vrect->mBottom.SetIntegerCoordValue(srect.YMost());
  3873. aComputedValue.SetAndAdoptCSSRectValue(vrect, eUnit_CSSRect);
  3874. break;
  3875. }
  3876. case eCSSProperty_clip: {
  3877. const nsStyleEffects* effects =
  3878. static_cast<const nsStyleEffects*>(styleStruct);
  3879. if (!(effects->mClipFlags & NS_STYLE_CLIP_RECT)) {
  3880. aComputedValue.SetAutoValue();
  3881. } else {
  3882. nsCSSRect *vrect = new nsCSSRect;
  3883. const nsRect &srect = effects->mClip;
  3884. if (effects->mClipFlags & NS_STYLE_CLIP_TOP_AUTO) {
  3885. vrect->mTop.SetAutoValue();
  3886. } else {
  3887. vrect->mTop.SetIntegerCoordValue(srect.y);
  3888. }
  3889. if (effects->mClipFlags & NS_STYLE_CLIP_RIGHT_AUTO) {
  3890. vrect->mRight.SetAutoValue();
  3891. } else {
  3892. vrect->mRight.SetIntegerCoordValue(srect.XMost());
  3893. }
  3894. if (effects->mClipFlags & NS_STYLE_CLIP_BOTTOM_AUTO) {
  3895. vrect->mBottom.SetAutoValue();
  3896. } else {
  3897. vrect->mBottom.SetIntegerCoordValue(srect.YMost());
  3898. }
  3899. if (effects->mClipFlags & NS_STYLE_CLIP_LEFT_AUTO) {
  3900. vrect->mLeft.SetAutoValue();
  3901. } else {
  3902. vrect->mLeft.SetIntegerCoordValue(srect.x);
  3903. }
  3904. aComputedValue.SetAndAdoptCSSRectValue(vrect, eUnit_CSSRect);
  3905. }
  3906. break;
  3907. }
  3908. case eCSSProperty_object_position: {
  3909. const nsStylePosition* stylePos =
  3910. static_cast<const nsStylePosition*>(styleStruct);
  3911. nsAutoPtr<nsCSSValue> val(new nsCSSValue);
  3912. SetPositionValue(stylePos->mObjectPosition, *val);
  3913. aComputedValue.SetAndAdoptCSSValueValue(val.forget(),
  3914. eUnit_ObjectPosition);
  3915. break;
  3916. }
  3917. case eCSSProperty_background_position_x: {
  3918. const nsStyleImageLayers& layers =
  3919. static_cast<const nsStyleBackground*>(styleStruct)->mImage;
  3920. ExtractImageLayerPositionXList(layers, aComputedValue);
  3921. break;
  3922. }
  3923. case eCSSProperty_background_position_y: {
  3924. const nsStyleImageLayers& layers =
  3925. static_cast<const nsStyleBackground*>(styleStruct)->mImage;
  3926. ExtractImageLayerPositionYList(layers, aComputedValue);
  3927. break;
  3928. }
  3929. case eCSSProperty_mask_position_x: {
  3930. const nsStyleImageLayers& layers =
  3931. static_cast<const nsStyleSVGReset*>(styleStruct)->mMask;
  3932. ExtractImageLayerPositionXList(layers, aComputedValue);
  3933. break;
  3934. }
  3935. case eCSSProperty_mask_position_y: {
  3936. const nsStyleImageLayers& layers =
  3937. static_cast<const nsStyleSVGReset*>(styleStruct)->mMask;
  3938. ExtractImageLayerPositionYList(layers, aComputedValue);
  3939. break;
  3940. }
  3941. case eCSSProperty_background_size: {
  3942. const nsStyleImageLayers& layers =
  3943. static_cast<const nsStyleBackground*>(styleStruct)->mImage;
  3944. ExtractImageLayerSizePairList(layers, aComputedValue);
  3945. break;
  3946. }
  3947. case eCSSProperty_mask_size: {
  3948. const nsStyleImageLayers& layers =
  3949. static_cast<const nsStyleSVGReset*>(styleStruct)->mMask;
  3950. ExtractImageLayerSizePairList(layers, aComputedValue);
  3951. break;
  3952. }
  3953. case eCSSProperty_clip_path: {
  3954. const nsStyleSVGReset* svgReset =
  3955. static_cast<const nsStyleSVGReset*>(styleStruct);
  3956. const StyleClipPath& clipPath = svgReset->mClipPath;
  3957. const StyleShapeSourceType type = clipPath.GetType();
  3958. if (type == StyleShapeSourceType::URL) {
  3959. auto result = MakeUnique<nsCSSValue>();
  3960. result->SetURLValue(clipPath.GetURL());
  3961. aComputedValue.SetAndAdoptCSSValueValue(result.release(), eUnit_URL);
  3962. } else if (type == StyleShapeSourceType::Box) {
  3963. aComputedValue.SetIntValue(clipPath.GetReferenceBox(),
  3964. eUnit_Enumerated);
  3965. } else if (type == StyleShapeSourceType::Shape) {
  3966. RefPtr<nsCSSValue::Array> result = nsCSSValue::Array::Create(2);
  3967. if (!StyleClipBasicShapeToCSSArray(clipPath, result)) {
  3968. return false;
  3969. }
  3970. aComputedValue.SetCSSValueArrayValue(result, eUnit_Shape);
  3971. } else {
  3972. MOZ_ASSERT(type == StyleShapeSourceType::None, "unknown type");
  3973. aComputedValue.SetNoneValue();
  3974. }
  3975. break;
  3976. }
  3977. case eCSSProperty_filter: {
  3978. const nsStyleEffects* effects =
  3979. static_cast<const nsStyleEffects*>(styleStruct);
  3980. const nsTArray<nsStyleFilter>& filters = effects->mFilters;
  3981. nsAutoPtr<nsCSSValueList> result;
  3982. nsCSSValueList **resultTail = getter_Transfers(result);
  3983. for (uint32_t i = 0; i < filters.Length(); ++i) {
  3984. nsCSSValueList *item = new nsCSSValueList;
  3985. *resultTail = item;
  3986. resultTail = &item->mNext;
  3987. const nsStyleFilter& filter = filters[i];
  3988. int32_t type = filter.GetType();
  3989. if (type == NS_STYLE_FILTER_URL) {
  3990. item->mValue.SetURLValue(filter.GetURL());
  3991. } else {
  3992. nsCSSKeyword functionName =
  3993. nsCSSProps::ValueToKeywordEnum(type,
  3994. nsCSSProps::kFilterFunctionKTable);
  3995. nsCSSValue::Array* filterArray =
  3996. item->mValue.InitFunction(functionName, 1);
  3997. if (type >= NS_STYLE_FILTER_BLUR && type <= NS_STYLE_FILTER_HUE_ROTATE) {
  3998. if (!StyleCoordToCSSValue(
  3999. filter.GetFilterParameter(),
  4000. filterArray->Item(1))) {
  4001. return false;
  4002. }
  4003. } else if (type == NS_STYLE_FILTER_DROP_SHADOW) {
  4004. nsCSSValueList* shadowResult = filterArray->Item(1).SetListValue();
  4005. nsAutoPtr<nsCSSValueList> tmpShadowValue;
  4006. nsCSSValueList **tmpShadowResultTail = getter_Transfers(tmpShadowValue);
  4007. nsCSSShadowArray* shadowArray = filter.GetDropShadow();
  4008. MOZ_ASSERT(shadowArray->Length() == 1,
  4009. "expected exactly one shadow");
  4010. AppendCSSShadowValue(shadowArray->ShadowAt(0), tmpShadowResultTail);
  4011. *shadowResult = *tmpShadowValue;
  4012. } else {
  4013. // We checked all possible nsStyleFilter types but
  4014. // NS_STYLE_FILTER_NULL before. We should never enter this path.
  4015. NS_NOTREACHED("no other filter functions defined");
  4016. return false;
  4017. }
  4018. }
  4019. }
  4020. aComputedValue.SetAndAdoptCSSValueListValue(result.forget(),
  4021. eUnit_Filter);
  4022. break;
  4023. }
  4024. case eCSSProperty_transform: {
  4025. const nsStyleDisplay *display =
  4026. static_cast<const nsStyleDisplay*>(styleStruct);
  4027. nsAutoPtr<nsCSSValueList> result;
  4028. if (display->mSpecifiedTransform) {
  4029. // Clone, and convert all lengths (not percents) to pixels.
  4030. nsCSSValueList **resultTail = getter_Transfers(result);
  4031. for (const nsCSSValueList *l = display->mSpecifiedTransform->mHead;
  4032. l; l = l->mNext) {
  4033. nsCSSValueList *clone = new nsCSSValueList;
  4034. *resultTail = clone;
  4035. resultTail = &clone->mNext;
  4036. SubstitutePixelValues(aStyleContext, l->mValue, clone->mValue);
  4037. }
  4038. } else {
  4039. result = new nsCSSValueList();
  4040. result->mValue.SetNoneValue();
  4041. }
  4042. aComputedValue.SetTransformValue(
  4043. new nsCSSValueSharedList(result.forget()));
  4044. break;
  4045. }
  4046. default:
  4047. MOZ_ASSERT(false, "missing property implementation");
  4048. return false;
  4049. };
  4050. return true;
  4051. case eStyleAnimType_Coord: {
  4052. const nsStyleCoord& coord =
  4053. StyleDataAtOffset<nsStyleCoord>(styleStruct, ssOffset);
  4054. if (nsCSSProps::PropHasFlags(aProperty, CSS_PROPERTY_NUMBERS_ARE_PIXELS) &&
  4055. coord.GetUnit() == eStyleUnit_Coord) {
  4056. // For SVG properties where number means the same thing as length,
  4057. // we want to animate them the same way. Normalize both to number
  4058. // since it has more accuracy (float vs nscoord).
  4059. aComputedValue.SetFloatValue(nsPresContext::
  4060. AppUnitsToFloatCSSPixels(coord.GetCoordValue()));
  4061. return true;
  4062. }
  4063. return StyleCoordToValue(coord, aComputedValue);
  4064. }
  4065. case eStyleAnimType_Sides_Top:
  4066. case eStyleAnimType_Sides_Right:
  4067. case eStyleAnimType_Sides_Bottom:
  4068. case eStyleAnimType_Sides_Left: {
  4069. static_assert(
  4070. NS_SIDE_TOP == eStyleAnimType_Sides_Top -eStyleAnimType_Sides_Top &&
  4071. NS_SIDE_RIGHT == eStyleAnimType_Sides_Right -eStyleAnimType_Sides_Top &&
  4072. NS_SIDE_BOTTOM == eStyleAnimType_Sides_Bottom-eStyleAnimType_Sides_Top &&
  4073. NS_SIDE_LEFT == eStyleAnimType_Sides_Left -eStyleAnimType_Sides_Top,
  4074. "box side constants out of sync with animation side constants");
  4075. const nsStyleCoord &coord =
  4076. StyleDataAtOffset<nsStyleSides>(styleStruct, ssOffset).
  4077. Get(mozilla::css::Side(animType - eStyleAnimType_Sides_Top));
  4078. return StyleCoordToValue(coord, aComputedValue);
  4079. }
  4080. case eStyleAnimType_Corner_TopLeft:
  4081. case eStyleAnimType_Corner_TopRight:
  4082. case eStyleAnimType_Corner_BottomRight:
  4083. case eStyleAnimType_Corner_BottomLeft: {
  4084. static_assert(
  4085. NS_CORNER_TOP_LEFT == eStyleAnimType_Corner_TopLeft -
  4086. eStyleAnimType_Corner_TopLeft &&
  4087. NS_CORNER_TOP_RIGHT == eStyleAnimType_Corner_TopRight -
  4088. eStyleAnimType_Corner_TopLeft &&
  4089. NS_CORNER_BOTTOM_RIGHT == eStyleAnimType_Corner_BottomRight -
  4090. eStyleAnimType_Corner_TopLeft &&
  4091. NS_CORNER_BOTTOM_LEFT == eStyleAnimType_Corner_BottomLeft -
  4092. eStyleAnimType_Corner_TopLeft,
  4093. "box corner constants out of sync with animation corner constants");
  4094. const nsStyleCorners& corners =
  4095. StyleDataAtOffset<nsStyleCorners>(styleStruct, ssOffset);
  4096. uint8_t fullCorner = animType - eStyleAnimType_Corner_TopLeft;
  4097. const nsStyleCoord &horiz =
  4098. corners.Get(NS_FULL_TO_HALF_CORNER(fullCorner, false));
  4099. const nsStyleCoord &vert =
  4100. corners.Get(NS_FULL_TO_HALF_CORNER(fullCorner, true));
  4101. nsAutoPtr<nsCSSValuePair> pair(new nsCSSValuePair);
  4102. if (!StyleCoordToCSSValue(horiz, pair->mXValue) ||
  4103. !StyleCoordToCSSValue(vert, pair->mYValue)) {
  4104. return false;
  4105. }
  4106. aComputedValue.SetAndAdoptCSSValuePairValue(pair.forget(),
  4107. eUnit_CSSValuePair);
  4108. return true;
  4109. }
  4110. case eStyleAnimType_nscoord:
  4111. aComputedValue.SetCoordValue(
  4112. StyleDataAtOffset<nscoord>(styleStruct, ssOffset));
  4113. return true;
  4114. case eStyleAnimType_float:
  4115. aComputedValue.SetFloatValue(
  4116. StyleDataAtOffset<float>(styleStruct, ssOffset));
  4117. if (aProperty == eCSSProperty_font_size_adjust &&
  4118. aComputedValue.GetFloatValue() == -1.0f) {
  4119. // In nsStyleFont, we set mFont.sizeAdjust to -1.0 to represent
  4120. // font-size-adjust: none. Here, we have to treat this as a keyword
  4121. // instead of a float value, to make sure we don't end up doing
  4122. // interpolation with it.
  4123. aComputedValue.SetNoneValue();
  4124. }
  4125. return true;
  4126. case eStyleAnimType_Color:
  4127. aComputedValue.SetColorValue(
  4128. StyleDataAtOffset<nscolor>(styleStruct, ssOffset));
  4129. return true;
  4130. case eStyleAnimType_ComplexColor: {
  4131. auto& color = StyleDataAtOffset<StyleComplexColor>(styleStruct, ssOffset);
  4132. if (color.mIsAuto) {
  4133. aComputedValue.SetAutoValue();
  4134. } else {
  4135. aComputedValue.SetComplexColorValue(color);
  4136. }
  4137. return true;
  4138. }
  4139. case eStyleAnimType_PaintServer: {
  4140. const nsStyleSVGPaint& paint =
  4141. StyleDataAtOffset<nsStyleSVGPaint>(styleStruct, ssOffset);
  4142. switch (paint.Type()) {
  4143. case eStyleSVGPaintType_Color:
  4144. aComputedValue.SetColorValue(paint.GetColor());
  4145. return true;
  4146. case eStyleSVGPaintType_Server: {
  4147. css::URLValue* url = paint.GetPaintServer();
  4148. if (!url) {
  4149. NS_WARNING("Null paint server");
  4150. return false;
  4151. }
  4152. nsAutoPtr<nsCSSValuePair> pair(new nsCSSValuePair);
  4153. pair->mXValue.SetURLValue(url);
  4154. pair->mYValue.SetColorValue(paint.GetFallbackColor());
  4155. aComputedValue.SetAndAdoptCSSValuePairValue(pair.forget(),
  4156. eUnit_CSSValuePair);
  4157. return true;
  4158. }
  4159. case eStyleSVGPaintType_ContextFill:
  4160. case eStyleSVGPaintType_ContextStroke: {
  4161. nsAutoPtr<nsCSSValuePair> pair(new nsCSSValuePair);
  4162. pair->mXValue.SetIntValue(paint.Type() == eStyleSVGPaintType_ContextFill ?
  4163. NS_COLOR_CONTEXT_FILL : NS_COLOR_CONTEXT_STROKE,
  4164. eCSSUnit_Enumerated);
  4165. pair->mYValue.SetColorValue(paint.GetFallbackColor());
  4166. aComputedValue.SetAndAdoptCSSValuePairValue(pair.forget(),
  4167. eUnit_CSSValuePair);
  4168. return true;
  4169. }
  4170. default:
  4171. MOZ_ASSERT(paint.Type() == eStyleSVGPaintType_None,
  4172. "Unexpected SVG paint type");
  4173. aComputedValue.SetNoneValue();
  4174. return true;
  4175. }
  4176. }
  4177. case eStyleAnimType_Shadow: {
  4178. const nsCSSShadowArray* shadowArray =
  4179. StyleDataAtOffset<RefPtr<nsCSSShadowArray>>(styleStruct, ssOffset);
  4180. if (!shadowArray) {
  4181. aComputedValue.SetAndAdoptCSSValueListValue(nullptr, eUnit_Shadow);
  4182. return true;
  4183. }
  4184. nsAutoPtr<nsCSSValueList> result;
  4185. nsCSSValueList **resultTail = getter_Transfers(result);
  4186. for (uint32_t i = 0, i_end = shadowArray->Length(); i < i_end; ++i) {
  4187. AppendCSSShadowValue(shadowArray->ShadowAt(i), resultTail);
  4188. }
  4189. aComputedValue.SetAndAdoptCSSValueListValue(result.forget(),
  4190. eUnit_Shadow);
  4191. return true;
  4192. }
  4193. case eStyleAnimType_Discrete: {
  4194. if (aProperty == eCSSProperty_visibility) {
  4195. aComputedValue.SetIntValue(
  4196. static_cast<const nsStyleVisibility*>(styleStruct)->mVisible,
  4197. eUnit_Visibility);
  4198. return true;
  4199. }
  4200. auto cssValue = MakeUnique<nsCSSValue>(eCSSUnit_Unset);
  4201. aStyleContext->RuleNode()->GetDiscretelyAnimatedCSSValue(aProperty,
  4202. cssValue.get());
  4203. aComputedValue.SetAndAdoptCSSValueValue(cssValue.release(),
  4204. eUnit_DiscreteCSSValue);
  4205. return true;
  4206. }
  4207. case eStyleAnimType_None:
  4208. NS_NOTREACHED("shouldn't use on non-animatable properties");
  4209. }
  4210. return false;
  4211. }
  4212. gfxSize
  4213. StyleAnimationValue::GetScaleValue(const nsIFrame* aForFrame) const
  4214. {
  4215. MOZ_ASSERT(aForFrame);
  4216. MOZ_ASSERT(GetUnit() == StyleAnimationValue::eUnit_Transform);
  4217. nsCSSValueSharedList* list = GetCSSValueSharedListValue();
  4218. MOZ_ASSERT(list->mHead);
  4219. RuleNodeCacheConditions dontCare;
  4220. bool dontCareBool;
  4221. nsStyleTransformMatrix::TransformReferenceBox refBox(aForFrame);
  4222. Matrix4x4 transform = nsStyleTransformMatrix::ReadTransforms(
  4223. list->mHead,
  4224. aForFrame->StyleContext(),
  4225. aForFrame->PresContext(), dontCare, refBox,
  4226. aForFrame->PresContext()->AppUnitsPerDevPixel(),
  4227. &dontCareBool);
  4228. Matrix transform2d;
  4229. bool canDraw2D = transform.CanDraw2D(&transform2d);
  4230. if (!canDraw2D) {
  4231. return gfxSize();
  4232. }
  4233. return ThebesMatrix(transform2d).ScaleFactors(true);
  4234. }
  4235. StyleAnimationValue::StyleAnimationValue(int32_t aInt, Unit aUnit,
  4236. IntegerConstructorType)
  4237. {
  4238. NS_ASSERTION(IsIntUnit(aUnit), "unit must be of integer type");
  4239. mUnit = aUnit;
  4240. mValue.mInt = aInt;
  4241. }
  4242. StyleAnimationValue::StyleAnimationValue(nscoord aLength, CoordConstructorType)
  4243. {
  4244. mUnit = eUnit_Coord;
  4245. mValue.mCoord = aLength;
  4246. }
  4247. StyleAnimationValue::StyleAnimationValue(float aPercent,
  4248. PercentConstructorType)
  4249. {
  4250. mUnit = eUnit_Percent;
  4251. mValue.mFloat = aPercent;
  4252. MOZ_ASSERT(!mozilla::IsNaN(mValue.mFloat));
  4253. }
  4254. StyleAnimationValue::StyleAnimationValue(float aFloat, FloatConstructorType)
  4255. {
  4256. mUnit = eUnit_Float;
  4257. mValue.mFloat = aFloat;
  4258. MOZ_ASSERT(!mozilla::IsNaN(mValue.mFloat));
  4259. }
  4260. StyleAnimationValue::StyleAnimationValue(nscolor aColor, ColorConstructorType)
  4261. {
  4262. mUnit = eUnit_Color;
  4263. mValue.mCSSValue = new nsCSSValue();
  4264. mValue.mCSSValue->SetColorValue(aColor);
  4265. }
  4266. StyleAnimationValue&
  4267. StyleAnimationValue::operator=(const StyleAnimationValue& aOther)
  4268. {
  4269. if (this == &aOther) {
  4270. return *this;
  4271. }
  4272. FreeValue();
  4273. mUnit = aOther.mUnit;
  4274. switch (mUnit) {
  4275. case eUnit_Null:
  4276. case eUnit_Normal:
  4277. case eUnit_Auto:
  4278. case eUnit_None:
  4279. case eUnit_CurrentColor:
  4280. break;
  4281. case eUnit_Enumerated:
  4282. case eUnit_Visibility:
  4283. case eUnit_Integer:
  4284. mValue.mInt = aOther.mValue.mInt;
  4285. break;
  4286. case eUnit_Coord:
  4287. mValue.mCoord = aOther.mValue.mCoord;
  4288. break;
  4289. case eUnit_Percent:
  4290. case eUnit_Float:
  4291. mValue.mFloat = aOther.mValue.mFloat;
  4292. MOZ_ASSERT(!mozilla::IsNaN(mValue.mFloat));
  4293. break;
  4294. case eUnit_Calc:
  4295. case eUnit_Color:
  4296. case eUnit_ObjectPosition:
  4297. case eUnit_URL:
  4298. case eUnit_DiscreteCSSValue:
  4299. MOZ_ASSERT(IsCSSValueUnit(mUnit),
  4300. "This clause is for handling nsCSSValue-backed units");
  4301. MOZ_ASSERT(aOther.mValue.mCSSValue, "values may not be null");
  4302. mValue.mCSSValue = new nsCSSValue(*aOther.mValue.mCSSValue);
  4303. break;
  4304. case eUnit_CSSValuePair:
  4305. MOZ_ASSERT(aOther.mValue.mCSSValuePair,
  4306. "value pairs may not be null");
  4307. mValue.mCSSValuePair = new nsCSSValuePair(*aOther.mValue.mCSSValuePair);
  4308. break;
  4309. case eUnit_CSSValueTriplet:
  4310. MOZ_ASSERT(aOther.mValue.mCSSValueTriplet,
  4311. "value triplets may not be null");
  4312. mValue.mCSSValueTriplet = new nsCSSValueTriplet(*aOther.mValue.mCSSValueTriplet);
  4313. break;
  4314. case eUnit_CSSRect:
  4315. MOZ_ASSERT(aOther.mValue.mCSSRect, "rects may not be null");
  4316. mValue.mCSSRect = new nsCSSRect(*aOther.mValue.mCSSRect);
  4317. break;
  4318. case eUnit_Dasharray:
  4319. case eUnit_Shadow:
  4320. case eUnit_Filter:
  4321. case eUnit_BackgroundPositionCoord:
  4322. MOZ_ASSERT(mUnit == eUnit_Shadow || mUnit == eUnit_Filter ||
  4323. aOther.mValue.mCSSValueList,
  4324. "value lists other than shadows and filters may not be null");
  4325. if (aOther.mValue.mCSSValueList) {
  4326. mValue.mCSSValueList = aOther.mValue.mCSSValueList->Clone();
  4327. } else {
  4328. mValue.mCSSValueList = nullptr;
  4329. }
  4330. break;
  4331. case eUnit_Shape:
  4332. MOZ_ASSERT(aOther.mValue.mCSSValueArray,
  4333. "value arrays may not be null");
  4334. mValue.mCSSValueArray = aOther.mValue.mCSSValueArray;
  4335. mValue.mCSSValueArray->AddRef();
  4336. break;
  4337. case eUnit_Transform:
  4338. mValue.mCSSValueSharedList = aOther.mValue.mCSSValueSharedList;
  4339. mValue.mCSSValueSharedList->AddRef();
  4340. break;
  4341. case eUnit_CSSValuePairList:
  4342. MOZ_ASSERT(aOther.mValue.mCSSValuePairList,
  4343. "value pair lists may not be null");
  4344. mValue.mCSSValuePairList = aOther.mValue.mCSSValuePairList->Clone();
  4345. break;
  4346. case eUnit_UnparsedString:
  4347. MOZ_ASSERT(aOther.mValue.mString, "expecting non-null string");
  4348. mValue.mString = aOther.mValue.mString;
  4349. mValue.mString->AddRef();
  4350. break;
  4351. case eUnit_ComplexColor:
  4352. MOZ_ASSERT(aOther.mValue.mComplexColor);
  4353. mValue.mComplexColor = aOther.mValue.mComplexColor;
  4354. mValue.mComplexColor->AddRef();
  4355. break;
  4356. }
  4357. return *this;
  4358. }
  4359. void
  4360. StyleAnimationValue::SetNormalValue()
  4361. {
  4362. FreeValue();
  4363. mUnit = eUnit_Normal;
  4364. }
  4365. void
  4366. StyleAnimationValue::SetAutoValue()
  4367. {
  4368. FreeValue();
  4369. mUnit = eUnit_Auto;
  4370. }
  4371. void
  4372. StyleAnimationValue::SetNoneValue()
  4373. {
  4374. FreeValue();
  4375. mUnit = eUnit_None;
  4376. }
  4377. void
  4378. StyleAnimationValue::SetIntValue(int32_t aInt, Unit aUnit)
  4379. {
  4380. NS_ASSERTION(IsIntUnit(aUnit), "unit must be of integer type");
  4381. FreeValue();
  4382. mUnit = aUnit;
  4383. mValue.mInt = aInt;
  4384. }
  4385. void
  4386. StyleAnimationValue::SetCoordValue(nscoord aLength)
  4387. {
  4388. FreeValue();
  4389. mUnit = eUnit_Coord;
  4390. mValue.mCoord = aLength;
  4391. }
  4392. void
  4393. StyleAnimationValue::SetPercentValue(float aPercent)
  4394. {
  4395. FreeValue();
  4396. mUnit = eUnit_Percent;
  4397. mValue.mFloat = aPercent;
  4398. MOZ_ASSERT(!mozilla::IsNaN(mValue.mFloat));
  4399. }
  4400. void
  4401. StyleAnimationValue::SetFloatValue(float aFloat)
  4402. {
  4403. FreeValue();
  4404. mUnit = eUnit_Float;
  4405. mValue.mFloat = aFloat;
  4406. MOZ_ASSERT(!mozilla::IsNaN(mValue.mFloat));
  4407. }
  4408. void
  4409. StyleAnimationValue::SetColorValue(nscolor aColor)
  4410. {
  4411. FreeValue();
  4412. mUnit = eUnit_Color;
  4413. mValue.mCSSValue = new nsCSSValue();
  4414. mValue.mCSSValue->SetColorValue(aColor);
  4415. }
  4416. void
  4417. StyleAnimationValue::SetCurrentColorValue()
  4418. {
  4419. FreeValue();
  4420. mUnit = eUnit_CurrentColor;
  4421. }
  4422. void
  4423. StyleAnimationValue::SetComplexColorValue(const StyleComplexColor& aColor)
  4424. {
  4425. if (aColor.mIsAuto) {
  4426. SetAutoValue();
  4427. } else if (aColor.IsCurrentColor()) {
  4428. SetCurrentColorValue();
  4429. } else if (aColor.IsNumericColor()) {
  4430. SetColorValue(aColor.mColor);
  4431. } else {
  4432. SetComplexColorValue(do_AddRef(new ComplexColorValue(aColor)));
  4433. }
  4434. }
  4435. void
  4436. StyleAnimationValue::SetComplexColorValue(
  4437. already_AddRefed<ComplexColorValue> aValue)
  4438. {
  4439. FreeValue();
  4440. mUnit = eUnit_ComplexColor;
  4441. mValue.mComplexColor = aValue.take();
  4442. }
  4443. void
  4444. StyleAnimationValue::SetUnparsedStringValue(const nsString& aString)
  4445. {
  4446. FreeValue();
  4447. mUnit = eUnit_UnparsedString;
  4448. mValue.mString = nsCSSValue::BufferFromString(aString).take();
  4449. }
  4450. void
  4451. StyleAnimationValue::SetAndAdoptCSSValueValue(nsCSSValue *aValue,
  4452. Unit aUnit)
  4453. {
  4454. FreeValue();
  4455. MOZ_ASSERT(IsCSSValueUnit(aUnit), "bad unit");
  4456. MOZ_ASSERT(aValue != nullptr, "values may not be null");
  4457. mUnit = aUnit;
  4458. mValue.mCSSValue = aValue; // take ownership
  4459. }
  4460. void
  4461. StyleAnimationValue::SetAndAdoptCSSValuePairValue(nsCSSValuePair *aValuePair,
  4462. Unit aUnit)
  4463. {
  4464. FreeValue();
  4465. MOZ_ASSERT(IsCSSValuePairUnit(aUnit), "bad unit");
  4466. MOZ_ASSERT(aValuePair != nullptr, "value pairs may not be null");
  4467. mUnit = aUnit;
  4468. mValue.mCSSValuePair = aValuePair; // take ownership
  4469. }
  4470. void
  4471. StyleAnimationValue::SetAndAdoptCSSValueTripletValue(
  4472. nsCSSValueTriplet *aValueTriplet, Unit aUnit)
  4473. {
  4474. FreeValue();
  4475. MOZ_ASSERT(IsCSSValueTripletUnit(aUnit), "bad unit");
  4476. MOZ_ASSERT(aValueTriplet != nullptr, "value pairs may not be null");
  4477. mUnit = aUnit;
  4478. mValue.mCSSValueTriplet = aValueTriplet; // take ownership
  4479. }
  4480. void
  4481. StyleAnimationValue::SetAndAdoptCSSRectValue(nsCSSRect *aRect, Unit aUnit)
  4482. {
  4483. FreeValue();
  4484. MOZ_ASSERT(IsCSSRectUnit(aUnit), "bad unit");
  4485. MOZ_ASSERT(aRect != nullptr, "value pairs may not be null");
  4486. mUnit = aUnit;
  4487. mValue.mCSSRect = aRect; // take ownership
  4488. }
  4489. void
  4490. StyleAnimationValue::SetCSSValueArrayValue(nsCSSValue::Array* aValue,
  4491. Unit aUnit)
  4492. {
  4493. FreeValue();
  4494. MOZ_ASSERT(IsCSSValueArrayUnit(aUnit), "bad unit");
  4495. MOZ_ASSERT(aValue != nullptr,
  4496. "not currently expecting any arrays to be null");
  4497. mUnit = aUnit;
  4498. mValue.mCSSValueArray = aValue;
  4499. mValue.mCSSValueArray->AddRef();
  4500. }
  4501. void
  4502. StyleAnimationValue::SetAndAdoptCSSValueListValue(nsCSSValueList *aValueList,
  4503. Unit aUnit)
  4504. {
  4505. FreeValue();
  4506. MOZ_ASSERT(IsCSSValueListUnit(aUnit), "bad unit");
  4507. MOZ_ASSERT(aUnit == eUnit_Shadow || aUnit == eUnit_Filter ||
  4508. aValueList != nullptr,
  4509. "value lists other than shadows and filters may not be null");
  4510. mUnit = aUnit;
  4511. mValue.mCSSValueList = aValueList; // take ownership
  4512. }
  4513. void
  4514. StyleAnimationValue::SetTransformValue(nsCSSValueSharedList* aList)
  4515. {
  4516. FreeValue();
  4517. mUnit = eUnit_Transform;
  4518. mValue.mCSSValueSharedList = aList;
  4519. mValue.mCSSValueSharedList->AddRef();
  4520. }
  4521. void
  4522. StyleAnimationValue::SetAndAdoptCSSValuePairListValue(
  4523. nsCSSValuePairList *aValuePairList)
  4524. {
  4525. FreeValue();
  4526. MOZ_ASSERT(aValuePairList, "may not be null");
  4527. mUnit = eUnit_CSSValuePairList;
  4528. mValue.mCSSValuePairList = aValuePairList; // take ownership
  4529. }
  4530. void
  4531. StyleAnimationValue::FreeValue()
  4532. {
  4533. if (IsCSSValueUnit(mUnit)) {
  4534. delete mValue.mCSSValue;
  4535. } else if (IsCSSValueListUnit(mUnit)) {
  4536. delete mValue.mCSSValueList;
  4537. } else if (IsCSSValueSharedListValue(mUnit)) {
  4538. mValue.mCSSValueSharedList->Release();
  4539. } else if (IsCSSValuePairUnit(mUnit)) {
  4540. delete mValue.mCSSValuePair;
  4541. } else if (IsCSSValueTripletUnit(mUnit)) {
  4542. delete mValue.mCSSValueTriplet;
  4543. } else if (IsCSSRectUnit(mUnit)) {
  4544. delete mValue.mCSSRect;
  4545. } else if (IsCSSValuePairListUnit(mUnit)) {
  4546. delete mValue.mCSSValuePairList;
  4547. } else if (IsCSSValueArrayUnit(mUnit)) {
  4548. mValue.mCSSValueArray->Release();
  4549. } else if (IsStringUnit(mUnit)) {
  4550. MOZ_ASSERT(mValue.mString, "expecting non-null string");
  4551. mValue.mString->Release();
  4552. } else if (mUnit == eUnit_ComplexColor) {
  4553. mValue.mComplexColor->Release();
  4554. }
  4555. }
  4556. bool
  4557. StyleAnimationValue::operator==(const StyleAnimationValue& aOther) const
  4558. {
  4559. if (mUnit != aOther.mUnit) {
  4560. return false;
  4561. }
  4562. switch (mUnit) {
  4563. case eUnit_Null:
  4564. case eUnit_Normal:
  4565. case eUnit_Auto:
  4566. case eUnit_None:
  4567. case eUnit_CurrentColor:
  4568. return true;
  4569. case eUnit_Enumerated:
  4570. case eUnit_Visibility:
  4571. case eUnit_Integer:
  4572. return mValue.mInt == aOther.mValue.mInt;
  4573. case eUnit_Coord:
  4574. return mValue.mCoord == aOther.mValue.mCoord;
  4575. case eUnit_Percent:
  4576. case eUnit_Float:
  4577. return mValue.mFloat == aOther.mValue.mFloat;
  4578. case eUnit_Calc:
  4579. case eUnit_Color:
  4580. case eUnit_ObjectPosition:
  4581. case eUnit_URL:
  4582. case eUnit_DiscreteCSSValue:
  4583. MOZ_ASSERT(IsCSSValueUnit(mUnit),
  4584. "This clause is for handling nsCSSValue-backed units");
  4585. return *mValue.mCSSValue == *aOther.mValue.mCSSValue;
  4586. case eUnit_CSSValuePair:
  4587. return *mValue.mCSSValuePair == *aOther.mValue.mCSSValuePair;
  4588. case eUnit_CSSValueTriplet:
  4589. return *mValue.mCSSValueTriplet == *aOther.mValue.mCSSValueTriplet;
  4590. case eUnit_CSSRect:
  4591. return *mValue.mCSSRect == *aOther.mValue.mCSSRect;
  4592. case eUnit_Dasharray:
  4593. case eUnit_Shadow:
  4594. case eUnit_Filter:
  4595. case eUnit_BackgroundPositionCoord:
  4596. return nsCSSValueList::Equal(mValue.mCSSValueList,
  4597. aOther.mValue.mCSSValueList);
  4598. case eUnit_Shape:
  4599. return *mValue.mCSSValueArray == *aOther.mValue.mCSSValueArray;
  4600. case eUnit_Transform:
  4601. return *mValue.mCSSValueSharedList == *aOther.mValue.mCSSValueSharedList;
  4602. case eUnit_CSSValuePairList:
  4603. return nsCSSValuePairList::Equal(mValue.mCSSValuePairList,
  4604. aOther.mValue.mCSSValuePairList);
  4605. case eUnit_UnparsedString:
  4606. return (NS_strcmp(GetStringBufferValue(),
  4607. aOther.GetStringBufferValue()) == 0);
  4608. case eUnit_ComplexColor:
  4609. return *mValue.mComplexColor == *aOther.mValue.mComplexColor;
  4610. }
  4611. NS_NOTREACHED("incomplete case");
  4612. return false;
  4613. }