style.cls.php 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429
  1. <?php
  2. /**
  3. * @package dompdf
  4. * @link http://dompdf.github.com/
  5. * @author Benj Carson <benjcarson@digitaljunkies.ca>
  6. * @author Helmut Tischer <htischer@weihenstephan.org>
  7. * @author Fabien Ménager <fabien.menager@gmail.com>
  8. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  9. */
  10. /**
  11. * Represents CSS properties.
  12. *
  13. * The Style class is responsible for handling and storing CSS properties.
  14. * It includes methods to resolve colors and lengths, as well as getters &
  15. * setters for many CSS properites.
  16. *
  17. * Actual CSS parsing is performed in the {@link Stylesheet} class.
  18. *
  19. * @package dompdf
  20. */
  21. class Style {
  22. const CSS_IDENTIFIER = "-?[_a-zA-Z]+[_a-zA-Z0-9-]*";
  23. const CSS_INTEGER = "-?\d+";
  24. /**
  25. * Default font size, in points.
  26. *
  27. * @var float
  28. */
  29. static $default_font_size = 12;
  30. /**
  31. * Default line height, as a fraction of the font size.
  32. *
  33. * @var float
  34. */
  35. static $default_line_height = 1.2;
  36. /**
  37. * Default "absolute" font sizes relative to the default font-size
  38. * http://www.w3.org/TR/css3-fonts/#font-size-the-font-size-property
  39. * @var array<float>
  40. */
  41. static $font_size_keywords = array(
  42. "xx-small" => 0.6, // 3/5
  43. "x-small" => 0.75, // 3/4
  44. "small" => 0.889, // 8/9
  45. "medium" => 1, // 1
  46. "large" => 1.2, // 6/5
  47. "x-large" => 1.5, // 3/2
  48. "xx-large" => 2.0, // 2/1
  49. );
  50. /**
  51. * List of all inline types. Should really be a constant.
  52. *
  53. * @var array
  54. */
  55. static $INLINE_TYPES = array("inline");
  56. /**
  57. * List of all block types. Should really be a constant.
  58. *
  59. * @var array
  60. */
  61. static $BLOCK_TYPES = array("block", "inline-block", "table-cell", "list-item");
  62. /**
  63. * List of all positionned types. Should really be a constant.
  64. *
  65. * @var array
  66. */
  67. static $POSITIONNED_TYPES = array("relative", "absolute", "fixed");
  68. /**
  69. * List of all table types. Should really be a constant.
  70. *
  71. * @var array;
  72. */
  73. static $TABLE_TYPES = array("table", "inline-table");
  74. /**
  75. * List of valid border styles. Should also really be a constant.
  76. *
  77. * @var array
  78. */
  79. static $BORDER_STYLES = array("none", "hidden", "dotted", "dashed", "solid",
  80. "double", "groove", "ridge", "inset", "outset");
  81. /**
  82. * Default style values.
  83. *
  84. * @link http://www.w3.org/TR/CSS21/propidx.html
  85. *
  86. * @var array
  87. */
  88. static protected $_defaults = null;
  89. /**
  90. * List of inherited properties
  91. *
  92. * @link http://www.w3.org/TR/CSS21/propidx.html
  93. *
  94. * @var array
  95. */
  96. static protected $_inherited = null;
  97. /**
  98. * Caches method_exists result
  99. *
  100. * @var array<bool>
  101. */
  102. static protected $_methods_cache = array();
  103. /**
  104. * The stylesheet this style belongs to
  105. *
  106. * @see Stylesheet
  107. * @var Stylesheet
  108. */
  109. protected $_stylesheet; // stylesheet this style is attached to
  110. /**
  111. * Main array of all CSS properties & values
  112. *
  113. * @var array
  114. */
  115. protected $_props;
  116. /* var instead of protected would allow access outside of class */
  117. protected $_important_props;
  118. /**
  119. * Cached property values
  120. *
  121. * @var array
  122. */
  123. protected $_prop_cache;
  124. /**
  125. * Font size of parent element in document tree. Used for relative font
  126. * size resolution.
  127. *
  128. * @var float
  129. */
  130. protected $_parent_font_size; // Font size of parent element
  131. protected $_font_family;
  132. /**
  133. * @var Frame
  134. */
  135. protected $_frame;
  136. /**
  137. * The origin of the style
  138. *
  139. * @var int
  140. */
  141. protected $_origin = Stylesheet::ORIG_AUTHOR;
  142. // private members
  143. /**
  144. * True once the font size is resolved absolutely
  145. *
  146. * @var bool
  147. */
  148. private $__font_size_calculated; // Cache flag
  149. /**
  150. * The computed border radius
  151. */
  152. private $_computed_border_radius = null;
  153. /**
  154. * @var bool
  155. */
  156. public $_has_border_radius = false;
  157. /**
  158. * Class constructor
  159. *
  160. * @param Stylesheet $stylesheet the stylesheet this Style is associated with.
  161. * @param int $origin
  162. */
  163. function __construct(Stylesheet $stylesheet, $origin = Stylesheet::ORIG_AUTHOR) {
  164. $this->_props = array();
  165. $this->_important_props = array();
  166. $this->_stylesheet = $stylesheet;
  167. $this->_origin = $origin;
  168. $this->_parent_font_size = null;
  169. $this->__font_size_calculated = false;
  170. if ( !isset(self::$_defaults) ) {
  171. // Shorthand
  172. $d =& self::$_defaults;
  173. // All CSS 2.1 properties, and their default values
  174. $d["azimuth"] = "center";
  175. $d["background_attachment"] = "scroll";
  176. $d["background_color"] = "transparent";
  177. $d["background_image"] = "none";
  178. $d["background_image_resolution"] = "normal";
  179. $d["_dompdf_background_image_resolution"] = $d["background_image_resolution"];
  180. $d["background_position"] = "0% 0%";
  181. $d["background_repeat"] = "repeat";
  182. $d["background"] = "";
  183. $d["border_collapse"] = "separate";
  184. $d["border_color"] = "";
  185. $d["border_spacing"] = "0";
  186. $d["border_style"] = "";
  187. $d["border_top"] = "";
  188. $d["border_right"] = "";
  189. $d["border_bottom"] = "";
  190. $d["border_left"] = "";
  191. $d["border_top_color"] = "";
  192. $d["border_right_color"] = "";
  193. $d["border_bottom_color"] = "";
  194. $d["border_left_color"] = "";
  195. $d["border_top_style"] = "none";
  196. $d["border_right_style"] = "none";
  197. $d["border_bottom_style"] = "none";
  198. $d["border_left_style"] = "none";
  199. $d["border_top_width"] = "medium";
  200. $d["border_right_width"] = "medium";
  201. $d["border_bottom_width"] = "medium";
  202. $d["border_left_width"] = "medium";
  203. $d["border_width"] = "medium";
  204. $d["border_bottom_left_radius"] = "";
  205. $d["border_bottom_right_radius"] = "";
  206. $d["border_top_left_radius"] = "";
  207. $d["border_top_right_radius"] = "";
  208. $d["border_radius"] = "";
  209. $d["border"] = "";
  210. $d["bottom"] = "auto";
  211. $d["caption_side"] = "top";
  212. $d["clear"] = "none";
  213. $d["clip"] = "auto";
  214. $d["color"] = "#000000";
  215. $d["content"] = "normal";
  216. $d["counter_increment"] = "none";
  217. $d["counter_reset"] = "none";
  218. $d["cue_after"] = "none";
  219. $d["cue_before"] = "none";
  220. $d["cue"] = "";
  221. $d["cursor"] = "auto";
  222. $d["direction"] = "ltr";
  223. $d["display"] = "inline";
  224. $d["elevation"] = "level";
  225. $d["empty_cells"] = "show";
  226. $d["float"] = "none";
  227. $d["font_family"] = $stylesheet->get_dompdf()->get_option("default_font");
  228. $d["font_size"] = "medium";
  229. $d["font_style"] = "normal";
  230. $d["font_variant"] = "normal";
  231. $d["font_weight"] = "normal";
  232. $d["font"] = "";
  233. $d["height"] = "auto";
  234. $d["image_resolution"] = "normal";
  235. $d["_dompdf_image_resolution"] = $d["image_resolution"];
  236. $d["left"] = "auto";
  237. $d["letter_spacing"] = "normal";
  238. $d["line_height"] = "normal";
  239. $d["list_style_image"] = "none";
  240. $d["list_style_position"] = "outside";
  241. $d["list_style_type"] = "disc";
  242. $d["list_style"] = "";
  243. $d["margin_right"] = "0";
  244. $d["margin_left"] = "0";
  245. $d["margin_top"] = "0";
  246. $d["margin_bottom"] = "0";
  247. $d["margin"] = "";
  248. $d["max_height"] = "none";
  249. $d["max_width"] = "none";
  250. $d["min_height"] = "0";
  251. $d["min_width"] = "0";
  252. $d["opacity"] = "1.0"; // CSS3
  253. $d["orphans"] = "2";
  254. $d["outline_color"] = ""; // "invert" special color is not supported
  255. $d["outline_style"] = "none";
  256. $d["outline_width"] = "medium";
  257. $d["outline"] = "";
  258. $d["overflow"] = "visible";
  259. $d["padding_top"] = "0";
  260. $d["padding_right"] = "0";
  261. $d["padding_bottom"] = "0";
  262. $d["padding_left"] = "0";
  263. $d["padding"] = "";
  264. $d["page_break_after"] = "auto";
  265. $d["page_break_before"] = "auto";
  266. $d["page_break_inside"] = "auto";
  267. $d["pause_after"] = "0";
  268. $d["pause_before"] = "0";
  269. $d["pause"] = "";
  270. $d["pitch_range"] = "50";
  271. $d["pitch"] = "medium";
  272. $d["play_during"] = "auto";
  273. $d["position"] = "static";
  274. $d["quotes"] = "";
  275. $d["richness"] = "50";
  276. $d["right"] = "auto";
  277. $d["size"] = "auto"; // @page
  278. $d["speak_header"] = "once";
  279. $d["speak_numeral"] = "continuous";
  280. $d["speak_punctuation"] = "none";
  281. $d["speak"] = "normal";
  282. $d["speech_rate"] = "medium";
  283. $d["stress"] = "50";
  284. $d["table_layout"] = "auto";
  285. $d["text_align"] = "left";
  286. $d["text_decoration"] = "none";
  287. $d["text_indent"] = "0";
  288. $d["text_transform"] = "none";
  289. $d["top"] = "auto";
  290. $d["transform"] = "none"; // CSS3
  291. $d["transform_origin"] = "50% 50%"; // CSS3
  292. $d["_webkit_transform"] = $d["transform"]; // CSS3
  293. $d["_webkit_transform_origin"] = $d["transform_origin"]; // CSS3
  294. $d["unicode_bidi"] = "normal";
  295. $d["vertical_align"] = "baseline";
  296. $d["visibility"] = "visible";
  297. $d["voice_family"] = "";
  298. $d["volume"] = "medium";
  299. $d["white_space"] = "normal";
  300. $d["word_wrap"] = "normal";
  301. $d["widows"] = "2";
  302. $d["width"] = "auto";
  303. $d["word_spacing"] = "normal";
  304. $d["z_index"] = "auto";
  305. // for @font-face
  306. $d["src"] = "";
  307. $d["unicode_range"] = "";
  308. // Properties that inherit by default
  309. self::$_inherited = array("azimuth",
  310. "background_image_resolution",
  311. "border_collapse",
  312. "border_spacing",
  313. "caption_side",
  314. "color",
  315. "cursor",
  316. "direction",
  317. "elevation",
  318. "empty_cells",
  319. "font_family",
  320. "font_size",
  321. "font_style",
  322. "font_variant",
  323. "font_weight",
  324. "font",
  325. "image_resolution",
  326. "letter_spacing",
  327. "line_height",
  328. "list_style_image",
  329. "list_style_position",
  330. "list_style_type",
  331. "list_style",
  332. "orphans",
  333. "page_break_inside",
  334. "pitch_range",
  335. "pitch",
  336. "quotes",
  337. "richness",
  338. "speak_header",
  339. "speak_numeral",
  340. "speak_punctuation",
  341. "speak",
  342. "speech_rate",
  343. "stress",
  344. "text_align",
  345. "text_indent",
  346. "text_transform",
  347. "visibility",
  348. "voice_family",
  349. "volume",
  350. "white_space",
  351. "word_wrap",
  352. "widows",
  353. "word_spacing");
  354. }
  355. }
  356. /**
  357. * "Destructor": forcibly free all references held by this object
  358. */
  359. function dispose() {
  360. clear_object($this);
  361. }
  362. function set_frame(Frame $frame) {
  363. $this->_frame = $frame;
  364. }
  365. function get_frame() {
  366. return $this->_frame;
  367. }
  368. function set_origin($origin) {
  369. $this->_origin = $origin;
  370. }
  371. function get_origin() {
  372. return $this->_origin;
  373. }
  374. /**
  375. * returns the {@link Stylesheet} this Style is associated with.
  376. *
  377. * @return Stylesheet
  378. */
  379. function get_stylesheet() { return $this->_stylesheet; }
  380. /**
  381. * Converts any CSS length value into an absolute length in points.
  382. *
  383. * length_in_pt() takes a single length (e.g. '1em') or an array of
  384. * lengths and returns an absolute length. If an array is passed, then
  385. * the return value is the sum of all elements.
  386. *
  387. * If a reference size is not provided, the default font size is used
  388. * ({@link Style::$default_font_size}).
  389. *
  390. * @param float|array $length the length or array of lengths to resolve
  391. * @param float $ref_size an absolute reference size to resolve percentage lengths
  392. * @return float
  393. */
  394. function length_in_pt($length, $ref_size = null) {
  395. static $cache = array();
  396. if ( !is_array($length) ) {
  397. $length = array($length);
  398. }
  399. if ( !isset($ref_size) ) {
  400. $ref_size = self::$default_font_size;
  401. }
  402. $key = implode("@", $length)."/$ref_size";
  403. if ( isset($cache[$key]) ) {
  404. return $cache[$key];
  405. }
  406. $ret = 0;
  407. foreach ($length as $l) {
  408. if ( $l === "auto" ) {
  409. return "auto";
  410. }
  411. if ( $l === "none" ) {
  412. return "none";
  413. }
  414. // Assume numeric values are already in points
  415. if ( is_numeric($l) ) {
  416. $ret += $l;
  417. continue;
  418. }
  419. if ( $l === "normal" ) {
  420. $ret += $ref_size;
  421. continue;
  422. }
  423. // Border lengths
  424. if ( $l === "thin" ) {
  425. $ret += 0.5;
  426. continue;
  427. }
  428. if ( $l === "medium" ) {
  429. $ret += 1.5;
  430. continue;
  431. }
  432. if ( $l === "thick" ) {
  433. $ret += 2.5;
  434. continue;
  435. }
  436. if ( ($i = mb_strpos($l, "px")) !== false ) {
  437. $dpi = $this->_stylesheet->get_dompdf()->get_option("dpi");
  438. $ret += ( mb_substr($l, 0, $i) * 72 ) / $dpi;
  439. continue;
  440. }
  441. if ( ($i = mb_strpos($l, "pt")) !== false ) {
  442. $ret += (float)mb_substr($l, 0, $i);
  443. continue;
  444. }
  445. if ( ($i = mb_strpos($l, "%")) !== false ) {
  446. $ret += (float)mb_substr($l, 0, $i)/100 * $ref_size;
  447. continue;
  448. }
  449. if ( ($i = mb_strpos($l, "rem")) !== false ) {
  450. $ret += (float)mb_substr($l, 0, $i) * $this->_stylesheet->get_dompdf()->get_tree()->get_root()->get_style()->font_size;
  451. continue;
  452. }
  453. if ( ($i = mb_strpos($l, "em")) !== false ) {
  454. $ret += (float)mb_substr($l, 0, $i) * $this->__get("font_size");
  455. continue;
  456. }
  457. if ( ($i = mb_strpos($l, "cm")) !== false ) {
  458. $ret += mb_substr($l, 0, $i) * 72 / 2.54;
  459. continue;
  460. }
  461. if ( ($i = mb_strpos($l, "mm")) !== false ) {
  462. $ret += mb_substr($l, 0, $i) * 72 / 25.4;
  463. continue;
  464. }
  465. // FIXME: em:ex ratio?
  466. if ( ($i = mb_strpos($l, "ex")) !== false ) {
  467. $ret += mb_substr($l, 0, $i) * $this->__get("font_size") / 2;
  468. continue;
  469. }
  470. if ( ($i = mb_strpos($l, "in")) !== false ) {
  471. $ret += (float)mb_substr($l, 0, $i) * 72;
  472. continue;
  473. }
  474. if ( ($i = mb_strpos($l, "pc")) !== false ) {
  475. $ret += (float)mb_substr($l, 0, $i) * 12;
  476. continue;
  477. }
  478. // Bogus value
  479. $ret += $ref_size;
  480. }
  481. return $cache[$key] = $ret;
  482. }
  483. /**
  484. * Set inherited properties in this style using values in $parent
  485. *
  486. * @param Style $parent
  487. *
  488. * @return Style
  489. */
  490. function inherit(Style $parent) {
  491. // Set parent font size
  492. $this->_parent_font_size = $parent->get_font_size();
  493. foreach (self::$_inherited as $prop) {
  494. //inherit the !important property also.
  495. //if local property is also !important, don't inherit.
  496. if ( isset($parent->_props[$prop]) &&
  497. ( !isset($this->_props[$prop]) ||
  498. ( isset($parent->_important_props[$prop]) && !isset($this->_important_props[$prop]) )
  499. )
  500. ) {
  501. if ( isset($parent->_important_props[$prop]) ) {
  502. $this->_important_props[$prop] = true;
  503. }
  504. //see __set and __get, on all assignments clear cache!
  505. $this->_prop_cache[$prop] = null;
  506. $this->_props[$prop] = $parent->_props[$prop];
  507. }
  508. }
  509. foreach ($this->_props as $prop => $value) {
  510. if ( $value === "inherit" ) {
  511. if ( isset($parent->_important_props[$prop]) ) {
  512. $this->_important_props[$prop] = true;
  513. }
  514. //do not assign direct, but
  515. //implicite assignment through __set, redirect to specialized, get value with __get
  516. //This is for computing defaults if the parent setting is also missing.
  517. //Therefore do not directly assign the value without __set
  518. //set _important_props before that to be able to propagate.
  519. //see __set and __get, on all assignments clear cache!
  520. //$this->_prop_cache[$prop] = null;
  521. //$this->_props[$prop] = $parent->_props[$prop];
  522. //props_set for more obvious explicite assignment not implemented, because
  523. //too many implicite uses.
  524. // $this->props_set($prop, $parent->$prop);
  525. $this->__set($prop, $parent->__get($prop));
  526. }
  527. }
  528. return $this;
  529. }
  530. /**
  531. * Override properties in this style with those in $style
  532. *
  533. * @param Style $style
  534. */
  535. function merge(Style $style) {
  536. //treat the !important attribute
  537. //if old rule has !important attribute, override with new rule only if
  538. //the new rule is also !important
  539. foreach($style->_props as $prop => $val ) {
  540. if (isset($style->_important_props[$prop])) {
  541. $this->_important_props[$prop] = true;
  542. //see __set and __get, on all assignments clear cache!
  543. $this->_prop_cache[$prop] = null;
  544. $this->_props[$prop] = $val;
  545. }
  546. else if ( !isset($this->_important_props[$prop]) ) {
  547. //see __set and __get, on all assignments clear cache!
  548. $this->_prop_cache[$prop] = null;
  549. $this->_props[$prop] = $val;
  550. }
  551. }
  552. if ( isset($style->_props["font_size"]) ) {
  553. $this->__font_size_calculated = false;
  554. }
  555. }
  556. /**
  557. * Returns an array(r, g, b, "r"=> r, "g"=>g, "b"=>b, "hex"=>"#rrggbb")
  558. * based on the provided CSS color value.
  559. *
  560. * @param string $color
  561. * @return array
  562. */
  563. function munge_color($color) {
  564. return CSS_Color::parse($color);
  565. }
  566. /* direct access to _important_props array from outside would work only when declared as
  567. * 'var $_important_props;' instead of 'protected $_important_props;'
  568. * Don't call _set/__get on missing attribute. Therefore need a special access.
  569. * Assume that __set will be also called when this is called, so do not check validity again.
  570. * Only created, if !important exists -> always set true.
  571. */
  572. function important_set($prop) {
  573. $prop = str_replace("-", "_", $prop);
  574. $this->_important_props[$prop] = true;
  575. }
  576. function important_get($prop) {
  577. return isset($this->_important_props[$prop]);
  578. }
  579. /**
  580. * PHP5 overloaded setter
  581. *
  582. * This function along with {@link Style::__get()} permit a user of the
  583. * Style class to access any (CSS) property using the following syntax:
  584. * <code>
  585. * Style->margin_top = "1em";
  586. * echo (Style->margin_top);
  587. * </code>
  588. *
  589. * __set() automatically calls the provided set function, if one exists,
  590. * otherwise it sets the property directly. Typically, __set() is not
  591. * called directly from outside of this class.
  592. *
  593. * On each modification clear cache to return accurate setting.
  594. * Also affects direct settings not using __set
  595. * For easier finding all assignments, attempted to allowing only explicite assignment:
  596. * Very many uses, e.g. frame_reflower.cls.php -> for now leave as it is
  597. * function __set($prop, $val) {
  598. * throw new DOMPDF_Exception("Implicite replacement of assignment by __set. Not good.");
  599. * }
  600. * function props_set($prop, $val) { ... }
  601. *
  602. * @param string $prop the property to set
  603. * @param mixed $val the value of the property
  604. *
  605. */
  606. function __set($prop, $val) {
  607. $prop = str_replace("-", "_", $prop);
  608. $this->_prop_cache[$prop] = null;
  609. if ( !isset(self::$_defaults[$prop]) ) {
  610. global $_dompdf_warnings;
  611. $_dompdf_warnings[] = "'$prop' is not a valid CSS2 property.";
  612. return;
  613. }
  614. if ( $prop !== "content" && is_string($val) && strlen($val) > 5 && mb_strpos($val, "url") === false ) {
  615. $val = mb_strtolower(trim(str_replace(array("\n", "\t"), array(" "), $val)));
  616. $val = preg_replace("/([0-9]+) (pt|px|pc|em|ex|in|cm|mm|%)/S", "\\1\\2", $val);
  617. }
  618. $method = "set_$prop";
  619. if ( !isset(self::$_methods_cache[$method]) ) {
  620. self::$_methods_cache[$method] = method_exists($this, $method);
  621. }
  622. if ( self::$_methods_cache[$method] ) {
  623. $this->$method($val);
  624. }
  625. else {
  626. $this->_props[$prop] = $val;
  627. }
  628. }
  629. /**
  630. * PHP5 overloaded getter
  631. * Along with {@link Style::__set()} __get() provides access to all CSS
  632. * properties directly. Typically __get() is not called directly outside
  633. * of this class.
  634. * On each modification clear cache to return accurate setting.
  635. * Also affects direct settings not using __set
  636. *
  637. * @param string $prop
  638. *
  639. * @throws DOMPDF_Exception
  640. * @return mixed
  641. */
  642. function __get($prop) {
  643. if ( !isset(self::$_defaults[$prop]) ) {
  644. throw new DOMPDF_Exception("'$prop' is not a valid CSS2 property.");
  645. }
  646. if ( isset($this->_prop_cache[$prop]) && $this->_prop_cache[$prop] != null ) {
  647. return $this->_prop_cache[$prop];
  648. }
  649. $method = "get_$prop";
  650. // Fall back on defaults if property is not set
  651. if ( !isset($this->_props[$prop]) ) {
  652. $this->_props[$prop] = self::$_defaults[$prop];
  653. }
  654. if ( !isset(self::$_methods_cache[$method]) ) {
  655. self::$_methods_cache[$method] = method_exists($this, $method);
  656. }
  657. if ( self::$_methods_cache[$method] ) {
  658. return $this->_prop_cache[$prop] = $this->$method();
  659. }
  660. return $this->_prop_cache[$prop] = $this->_props[$prop];
  661. }
  662. function get_font_family_raw(){
  663. return trim($this->_props["font_family"], " \t\n\r\x0B\"'");
  664. }
  665. /**
  666. * Getter for the 'font-family' CSS property.
  667. * Uses the {@link Font_Metrics} class to resolve the font family into an
  668. * actual font file.
  669. *
  670. * @link http://www.w3.org/TR/CSS21/fonts.html#propdef-font-family
  671. * @throws DOMPDF_Exception
  672. *
  673. * @return string
  674. */
  675. function get_font_family() {
  676. if ( isset($this->_font_family) ) {
  677. return $this->_font_family;
  678. }
  679. $DEBUGCSS=DEBUGCSS; //=DEBUGCSS; Allow override of global setting for ad hoc debug
  680. // Select the appropriate font. First determine the subtype, then check
  681. // the specified font-families for a candidate.
  682. // Resolve font-weight
  683. $weight = $this->__get("font_weight");
  684. if ( is_numeric($weight) ) {
  685. if ( $weight < 600 ) {
  686. $weight = "normal";
  687. }
  688. else {
  689. $weight = "bold";
  690. }
  691. }
  692. else if ( $weight === "bold" || $weight === "bolder" ) {
  693. $weight = "bold";
  694. }
  695. else {
  696. $weight = "normal";
  697. }
  698. // Resolve font-style
  699. $font_style = $this->__get("font_style");
  700. if ( $weight === "bold" && ($font_style === "italic" || $font_style === "oblique") ) {
  701. $subtype = "bold_italic";
  702. }
  703. else if ( $weight === "bold" && $font_style !== "italic" && $font_style !== "oblique" ) {
  704. $subtype = "bold";
  705. }
  706. else if ( $weight !== "bold" && ($font_style === "italic" || $font_style === "oblique") ) {
  707. $subtype = "italic";
  708. }
  709. else {
  710. $subtype = "normal";
  711. }
  712. // Resolve the font family
  713. if ( $DEBUGCSS ) {
  714. print "<pre>[get_font_family:";
  715. print '('.$this->_props["font_family"].'.'.$font_style.'.'.$this->__get("font_weight").'.'.$weight.'.'.$subtype.')';
  716. }
  717. $families = preg_split("/\s*,\s*/", $this->_props["font_family"]);
  718. $font = null;
  719. foreach($families as $family) {
  720. //remove leading and trailing string delimiters, e.g. on font names with spaces;
  721. //remove leading and trailing whitespace
  722. $family = trim($family, " \t\n\r\x0B\"'");
  723. if ( $DEBUGCSS ) {
  724. print '('.$family.')';
  725. }
  726. $font = Font_Metrics::get_font($family, $subtype);
  727. if ( $font ) {
  728. if ($DEBUGCSS) print '('.$font.")get_font_family]\n</pre>";
  729. return $this->_font_family = $font;
  730. }
  731. }
  732. $family = null;
  733. if ( $DEBUGCSS ) {
  734. print '(default)';
  735. }
  736. $font = Font_Metrics::get_font($family, $subtype);
  737. if ( $font ) {
  738. if ( $DEBUGCSS ) print '('.$font.")get_font_family]\n</pre>";
  739. return$this->_font_family = $font;
  740. }
  741. throw new DOMPDF_Exception("Unable to find a suitable font replacement for: '" . $this->_props["font_family"] ."'");
  742. }
  743. /**
  744. * Returns the resolved font size, in points
  745. *
  746. * @link http://www.w3.org/TR/CSS21/fonts.html#propdef-font-size
  747. * @return float
  748. */
  749. function get_font_size() {
  750. if ( $this->__font_size_calculated ) {
  751. return $this->_props["font_size"];
  752. }
  753. if ( !isset($this->_props["font_size"]) ) {
  754. $fs = self::$_defaults["font_size"];
  755. }
  756. else {
  757. $fs = $this->_props["font_size"];
  758. }
  759. if ( !isset($this->_parent_font_size) ) {
  760. $this->_parent_font_size = self::$default_font_size;
  761. }
  762. switch ($fs) {
  763. case "xx-small":
  764. case "x-small":
  765. case "small":
  766. case "medium":
  767. case "large":
  768. case "x-large":
  769. case "xx-large":
  770. $fs = self::$default_font_size * self::$font_size_keywords[$fs];
  771. break;
  772. case "smaller":
  773. $fs = 8/9 * $this->_parent_font_size;
  774. break;
  775. case "larger":
  776. $fs = 6/5 * $this->_parent_font_size;
  777. break;
  778. default:
  779. break;
  780. }
  781. // Ensure relative sizes resolve to something
  782. if ( ($i = mb_strpos($fs, "em")) !== false ) {
  783. $fs = mb_substr($fs, 0, $i) * $this->_parent_font_size;
  784. }
  785. else if ( ($i = mb_strpos($fs, "ex")) !== false ) {
  786. $fs = mb_substr($fs, 0, $i) * $this->_parent_font_size;
  787. }
  788. else {
  789. $fs = $this->length_in_pt($fs);
  790. }
  791. //see __set and __get, on all assignments clear cache!
  792. $this->_prop_cache["font_size"] = null;
  793. $this->_props["font_size"] = $fs;
  794. $this->__font_size_calculated = true;
  795. return $this->_props["font_size"];
  796. }
  797. /**
  798. * @link http://www.w3.org/TR/CSS21/text.html#propdef-word-spacing
  799. * @return float
  800. */
  801. function get_word_spacing() {
  802. if ( $this->_props["word_spacing"] === "normal" ) {
  803. return 0;
  804. }
  805. return $this->_props["word_spacing"];
  806. }
  807. /**
  808. * @link http://www.w3.org/TR/CSS21/text.html#propdef-letter-spacing
  809. * @return float
  810. */
  811. function get_letter_spacing() {
  812. if ( $this->_props["letter_spacing"] === "normal" ) {
  813. return 0;
  814. }
  815. return $this->_props["letter_spacing"];
  816. }
  817. /**
  818. * @link http://www.w3.org/TR/CSS21/visudet.html#propdef-line-height
  819. * @return float
  820. */
  821. function get_line_height() {
  822. $line_height = $this->_props["line_height"];
  823. if ( $line_height === "normal" ) {
  824. return self::$default_line_height * $this->get_font_size();
  825. }
  826. if ( is_numeric($line_height) ) {
  827. return $this->length_in_pt( $line_height . "em", $this->get_font_size());
  828. }
  829. return $this->length_in_pt( $line_height, $this->_parent_font_size );
  830. }
  831. /**
  832. * Returns the color as an array
  833. *
  834. * The array has the following format:
  835. * <code>array(r,g,b, "r" => r, "g" => g, "b" => b, "hex" => "#rrggbb")</code>
  836. *
  837. * @link http://www.w3.org/TR/CSS21/colors.html#propdef-color
  838. * @return array
  839. */
  840. function get_color() {
  841. return $this->munge_color( $this->_props["color"] );
  842. }
  843. /**
  844. * Returns the background color as an array
  845. *
  846. * The returned array has the same format as {@link Style::get_color()}
  847. *
  848. * @link http://www.w3.org/TR/CSS21/colors.html#propdef-background-color
  849. * @return array
  850. */
  851. function get_background_color() {
  852. return $this->munge_color( $this->_props["background_color"] );
  853. }
  854. /**
  855. * Returns the background position as an array
  856. *
  857. * The returned array has the following format:
  858. * <code>array(x,y, "x" => x, "y" => y)</code>
  859. *
  860. * @link http://www.w3.org/TR/CSS21/colors.html#propdef-background-position
  861. * @return array
  862. */
  863. function get_background_position() {
  864. $tmp = explode(" ", $this->_props["background_position"]);
  865. switch ($tmp[0]) {
  866. case "left":
  867. $x = "0%";
  868. break;
  869. case "right":
  870. $x = "100%";
  871. break;
  872. case "top":
  873. $y = "0%";
  874. break;
  875. case "bottom":
  876. $y = "100%";
  877. break;
  878. case "center":
  879. $x = "50%";
  880. $y = "50%";
  881. break;
  882. default:
  883. $x = $tmp[0];
  884. break;
  885. }
  886. if ( isset($tmp[1]) ) {
  887. switch ($tmp[1]) {
  888. case "left":
  889. $x = "0%";
  890. break;
  891. case "right":
  892. $x = "100%";
  893. break;
  894. case "top":
  895. $y = "0%";
  896. break;
  897. case "bottom":
  898. $y = "100%";
  899. break;
  900. case "center":
  901. if ( $tmp[0] === "left" || $tmp[0] === "right" || $tmp[0] === "center" ) {
  902. $y = "50%";
  903. }
  904. else {
  905. $x = "50%";
  906. }
  907. break;
  908. default:
  909. $y = $tmp[1];
  910. break;
  911. }
  912. }
  913. else {
  914. $y = "50%";
  915. }
  916. if ( !isset($x) ) {
  917. $x = "0%";
  918. }
  919. if ( !isset($y) ) {
  920. $y = "0%";
  921. }
  922. return array(
  923. 0 => $x, "x" => $x,
  924. 1 => $y, "y" => $y,
  925. );
  926. }
  927. /**
  928. * Returns the background as it is currently stored
  929. *
  930. * (currently anyway only for completeness.
  931. * not used for further processing)
  932. *
  933. * @link http://www.w3.org/TR/CSS21/colors.html#propdef-background-attachment
  934. * @return string
  935. */
  936. function get_background_attachment() {
  937. return $this->_props["background_attachment"];
  938. }
  939. /**
  940. * Returns the background_repeat as it is currently stored
  941. *
  942. * (currently anyway only for completeness.
  943. * not used for further processing)
  944. *
  945. * @link http://www.w3.org/TR/CSS21/colors.html#propdef-background-repeat
  946. * @return string
  947. */
  948. function get_background_repeat() {
  949. return $this->_props["background_repeat"];
  950. }
  951. /**
  952. * Returns the background as it is currently stored
  953. *
  954. * (currently anyway only for completeness.
  955. * not used for further processing, but the individual get_background_xxx)
  956. *
  957. * @link http://www.w3.org/TR/CSS21/colors.html#propdef-background
  958. * @return string
  959. */
  960. function get_background() {
  961. return $this->_props["background"];
  962. }
  963. /**#@+
  964. * Returns the border color as an array
  965. *
  966. * See {@link Style::get_color()}
  967. *
  968. * @link http://www.w3.org/TR/CSS21/box.html#border-color-properties
  969. * @return array
  970. */
  971. function get_border_top_color() {
  972. if ( $this->_props["border_top_color"] === "" ) {
  973. //see __set and __get, on all assignments clear cache!
  974. $this->_prop_cache["border_top_color"] = null;
  975. $this->_props["border_top_color"] = $this->__get("color");
  976. }
  977. return $this->munge_color($this->_props["border_top_color"]);
  978. }
  979. function get_border_right_color() {
  980. if ( $this->_props["border_right_color"] === "" ) {
  981. //see __set and __get, on all assignments clear cache!
  982. $this->_prop_cache["border_right_color"] = null;
  983. $this->_props["border_right_color"] = $this->__get("color");
  984. }
  985. return $this->munge_color($this->_props["border_right_color"]);
  986. }
  987. function get_border_bottom_color() {
  988. if ( $this->_props["border_bottom_color"] === "" ) {
  989. //see __set and __get, on all assignments clear cache!
  990. $this->_prop_cache["border_bottom_color"] = null;
  991. $this->_props["border_bottom_color"] = $this->__get("color");
  992. }
  993. return $this->munge_color($this->_props["border_bottom_color"]);
  994. }
  995. function get_border_left_color() {
  996. if ( $this->_props["border_left_color"] === "" ) {
  997. //see __set and __get, on all assignments clear cache!
  998. $this->_prop_cache["border_left_color"] = null;
  999. $this->_props["border_left_color"] = $this->__get("color");
  1000. }
  1001. return $this->munge_color($this->_props["border_left_color"]);
  1002. }
  1003. /**#@-*/
  1004. /**#@+
  1005. * Returns the border width, as it is currently stored
  1006. *
  1007. * @link http://www.w3.org/TR/CSS21/box.html#border-width-properties
  1008. * @return float|string
  1009. */
  1010. function get_border_top_width() {
  1011. $style = $this->__get("border_top_style");
  1012. return $style !== "none" && $style !== "hidden" ? $this->length_in_pt($this->_props["border_top_width"]) : 0;
  1013. }
  1014. function get_border_right_width() {
  1015. $style = $this->__get("border_right_style");
  1016. return $style !== "none" && $style !== "hidden" ? $this->length_in_pt($this->_props["border_right_width"]) : 0;
  1017. }
  1018. function get_border_bottom_width() {
  1019. $style = $this->__get("border_bottom_style");
  1020. return $style !== "none" && $style !== "hidden" ? $this->length_in_pt($this->_props["border_bottom_width"]) : 0;
  1021. }
  1022. function get_border_left_width() {
  1023. $style = $this->__get("border_left_style");
  1024. return $style !== "none" && $style !== "hidden" ? $this->length_in_pt($this->_props["border_left_width"]) : 0;
  1025. }
  1026. /**#@-*/
  1027. /**
  1028. * Return an array of all border properties.
  1029. *
  1030. * The returned array has the following structure:
  1031. * <code>
  1032. * array("top" => array("width" => [border-width],
  1033. * "style" => [border-style],
  1034. * "color" => [border-color (array)]),
  1035. * "bottom" ... )
  1036. * </code>
  1037. *
  1038. * @return array
  1039. */
  1040. function get_border_properties() {
  1041. return array(
  1042. "top" => array(
  1043. "width" => $this->__get("border_top_width"),
  1044. "style" => $this->__get("border_top_style"),
  1045. "color" => $this->__get("border_top_color"),
  1046. ),
  1047. "bottom" => array(
  1048. "width" => $this->__get("border_bottom_width"),
  1049. "style" => $this->__get("border_bottom_style"),
  1050. "color" => $this->__get("border_bottom_color"),
  1051. ),
  1052. "right" => array(
  1053. "width" => $this->__get("border_right_width"),
  1054. "style" => $this->__get("border_right_style"),
  1055. "color" => $this->__get("border_right_color"),
  1056. ),
  1057. "left" => array(
  1058. "width" => $this->__get("border_left_width"),
  1059. "style" => $this->__get("border_left_style"),
  1060. "color" => $this->__get("border_left_color"),
  1061. ),
  1062. );
  1063. }
  1064. /**
  1065. * Return a single border property
  1066. *
  1067. * @param string $side
  1068. *
  1069. * @return mixed
  1070. */
  1071. protected function _get_border($side) {
  1072. $color = $this->__get("border_" . $side . "_color");
  1073. return $this->__get("border_" . $side . "_width") . " " .
  1074. $this->__get("border_" . $side . "_style") . " " . $color["hex"];
  1075. }
  1076. /**#@+
  1077. * Return full border properties as a string
  1078. *
  1079. * Border properties are returned just as specified in CSS:
  1080. * <pre>[width] [style] [color]</pre>
  1081. * e.g. "1px solid blue"
  1082. *
  1083. * @link http://www.w3.org/TR/CSS21/box.html#border-shorthand-properties
  1084. * @return string
  1085. */
  1086. function get_border_top() {
  1087. return $this->_get_border("top");
  1088. }
  1089. function get_border_right() {
  1090. return $this->_get_border("right");
  1091. }
  1092. function get_border_bottom() {
  1093. return $this->_get_border("bottom");
  1094. }
  1095. function get_border_left() {
  1096. return $this->_get_border("left");
  1097. }
  1098. /**#@-*/
  1099. function get_computed_border_radius($w, $h) {
  1100. if ( !empty($this->_computed_border_radius) ) {
  1101. return $this->_computed_border_radius;
  1102. }
  1103. $rTL = $this->__get("border_top_left_radius");
  1104. $rTR = $this->__get("border_top_right_radius");
  1105. $rBL = $this->__get("border_bottom_left_radius");
  1106. $rBR = $this->__get("border_bottom_right_radius");
  1107. if ( $rTL + $rTR + $rBL + $rBR == 0 ) {
  1108. return $this->_computed_border_radius = array(
  1109. 0, 0, 0, 0,
  1110. "top-left" => 0,
  1111. "top-right" => 0,
  1112. "bottom-right" => 0,
  1113. "bottom-left" => 0,
  1114. );
  1115. }
  1116. $t = $this->__get("border_top_width");
  1117. $r = $this->__get("border_right_width");
  1118. $b = $this->__get("border_bottom_width");
  1119. $l = $this->__get("border_left_width");
  1120. $rTL = min($rTL, $h - $rBL - $t/2 - $b/2, $w - $rTR - $l/2 - $r/2);
  1121. $rTR = min($rTR, $h - $rBR - $t/2 - $b/2, $w - $rTL - $l/2 - $r/2);
  1122. $rBL = min($rBL, $h - $rTL - $t/2 - $b/2, $w - $rBR - $l/2 - $r/2);
  1123. $rBR = min($rBR, $h - $rTR - $t/2 - $b/2, $w - $rBL - $l/2 - $r/2);
  1124. return $this->_computed_border_radius = array(
  1125. $rTL, $rTR, $rBR, $rBL,
  1126. "top-left" => $rTL,
  1127. "top-right" => $rTR,
  1128. "bottom-right" => $rBR,
  1129. "bottom-left" => $rBL,
  1130. );
  1131. }
  1132. /**#@-*/
  1133. /**
  1134. * Returns the outline color as an array
  1135. *
  1136. * See {@link Style::get_color()}
  1137. *
  1138. * @link http://www.w3.org/TR/CSS21/box.html#border-color-properties
  1139. * @return array
  1140. */
  1141. function get_outline_color() {
  1142. if ( $this->_props["outline_color"] === "" ) {
  1143. //see __set and __get, on all assignments clear cache!
  1144. $this->_prop_cache["outline_color"] = null;
  1145. $this->_props["outline_color"] = $this->__get("color");
  1146. }
  1147. return $this->munge_color($this->_props["outline_color"]);
  1148. }
  1149. /**#@+
  1150. * Returns the outline width, as it is currently stored
  1151. * @return float|string
  1152. */
  1153. function get_outline_width() {
  1154. $style = $this->__get("outline_style");
  1155. return $style !== "none" && $style !== "hidden" ? $this->length_in_pt($this->_props["outline_width"]) : 0;
  1156. }
  1157. /**#@+
  1158. * Return full outline properties as a string
  1159. *
  1160. * Outline properties are returned just as specified in CSS:
  1161. * <pre>[width] [style] [color]</pre>
  1162. * e.g. "1px solid blue"
  1163. *
  1164. * @link http://www.w3.org/TR/CSS21/box.html#border-shorthand-properties
  1165. * @return string
  1166. */
  1167. function get_outline() {
  1168. $color = $this->__get("outline_color");
  1169. return
  1170. $this->__get("outline_width") . " " .
  1171. $this->__get("outline_style") . " " .
  1172. $color["hex"];
  1173. }
  1174. /**#@-*/
  1175. /**
  1176. * Returns border spacing as an array
  1177. *
  1178. * The array has the format (h_space,v_space)
  1179. *
  1180. * @link http://www.w3.org/TR/CSS21/tables.html#propdef-border-spacing
  1181. * @return array
  1182. */
  1183. function get_border_spacing() {
  1184. return explode(" ", $this->_props["border_spacing"]);
  1185. }
  1186. /*==============================*/
  1187. /*
  1188. !important attribute
  1189. For basic functionality of the !important attribute with overloading
  1190. of several styles of an element, changes in inherit(), merge() and _parse_properties()
  1191. are sufficient [helpers var $_important_props, __construct(), important_set(), important_get()]
  1192. Only for combined attributes extra treatment needed. See below.
  1193. div { border: 1px red; }
  1194. div { border: solid; } // Not combined! Only one occurence of same style per context
  1195. //
  1196. div { border: 1px red; }
  1197. div a { border: solid; } // Adding to border style ok by inheritance
  1198. //
  1199. div { border-style: solid; } // Adding to border style ok because of different styles
  1200. div { border: 1px red; }
  1201. //
  1202. div { border-style: solid; !important} // border: overrides, even though not !important
  1203. div { border: 1px dashed red; }
  1204. //
  1205. div { border: 1px red; !important }
  1206. div a { border-style: solid; } // Need to override because not set
  1207. Special treatment:
  1208. At individual property like border-top-width need to check whether overriding value is also !important.
  1209. Also store the !important condition for later overrides.
  1210. Since not known who is initiating the override, need to get passed !important as parameter.
  1211. !important Paramter taken as in the original style in the css file.
  1212. When property border !important given, do not mark subsets like border_style as important. Only
  1213. individual properties.
  1214. Note:
  1215. Setting individual property directly from css with e.g. set_border_top_style() is not needed, because
  1216. missing set funcions handled by a generic handler __set(), including the !important.
  1217. Setting individual property of as sub-property is handled below.
  1218. Implementation see at _set_style_side_type()
  1219. Callers _set_style_sides_type(), _set_style_type, _set_style_type_important()
  1220. Related functionality for background, padding, margin, font, list_style
  1221. */
  1222. /* Generalized set function for individual attribute of combined style.
  1223. * With check for !important
  1224. * Applicable for background, border, padding, margin, font, list_style
  1225. * Note: $type has a leading underscore (or is empty), the others not.
  1226. */
  1227. protected function _set_style_side_type($style, $side, $type, $val, $important) {
  1228. $prop = $style.'_'.$side.$type;
  1229. if ( !isset($this->_important_props[$prop]) || $important) {
  1230. //see __set and __get, on all assignments clear cache!
  1231. $this->_prop_cache[$prop] = null;
  1232. if ( $important ) {
  1233. $this->_important_props[$prop] = true;
  1234. }
  1235. $this->_props[$prop] = $val;
  1236. }
  1237. }
  1238. protected function _set_style_sides_type($style,$top,$right,$bottom,$left,$type,$important) {
  1239. $this->_set_style_side_type($style,'top',$type,$top,$important);
  1240. $this->_set_style_side_type($style,'right',$type,$right,$important);
  1241. $this->_set_style_side_type($style,'bottom',$type,$bottom,$important);
  1242. $this->_set_style_side_type($style,'left',$type,$left,$important);
  1243. }
  1244. protected function _set_style_type($style,$type,$val,$important) {
  1245. $val = preg_replace("/\s*\,\s*/", ",", $val); // when rgb() has spaces
  1246. $arr = explode(" ", $val);
  1247. switch (count($arr)) {
  1248. case 1: $this->_set_style_sides_type($style,$arr[0],$arr[0],$arr[0],$arr[0],$type,$important); break;
  1249. case 2: $this->_set_style_sides_type($style,$arr[0],$arr[1],$arr[0],$arr[1],$type,$important); break;
  1250. case 3: $this->_set_style_sides_type($style,$arr[0],$arr[1],$arr[2],$arr[1],$type,$important); break;
  1251. case 4: $this->_set_style_sides_type($style,$arr[0],$arr[1],$arr[2],$arr[3],$type,$important); break;
  1252. }
  1253. //see __set and __get, on all assignments clear cache!
  1254. $this->_prop_cache[$style.$type] = null;
  1255. $this->_props[$style.$type] = $val;
  1256. }
  1257. protected function _set_style_type_important($style,$type,$val) {
  1258. $this->_set_style_type($style,$type,$val,isset($this->_important_props[$style.$type]));
  1259. }
  1260. /* Anyway only called if _important matches and is assigned
  1261. * E.g. _set_style_side_type($style,$side,'',str_replace("none", "0px", $val),isset($this->_important_props[$style.'_'.$side]));
  1262. */
  1263. protected function _set_style_side_width_important($style,$side,$val) {
  1264. //see __set and __get, on all assignments clear cache!
  1265. $this->_prop_cache[$style.'_'.$side] = null;
  1266. $this->_props[$style.'_'.$side] = str_replace("none", "0px", $val);
  1267. }
  1268. protected function _set_style($style,$val,$important) {
  1269. if ( !isset($this->_important_props[$style]) || $important) {
  1270. if ( $important ) {
  1271. $this->_important_props[$style] = true;
  1272. }
  1273. //see __set and __get, on all assignments clear cache!
  1274. $this->_prop_cache[$style] = null;
  1275. $this->_props[$style] = $val;
  1276. }
  1277. }
  1278. protected function _image($val) {
  1279. $DEBUGCSS=DEBUGCSS;
  1280. $parsed_url = "none";
  1281. if ( mb_strpos($val, "url") === false ) {
  1282. $path = "none"; //Don't resolve no image -> otherwise would prefix path and no longer recognize as none
  1283. }
  1284. else {
  1285. $val = preg_replace("/url\(['\"]?([^'\")]+)['\"]?\)/","\\1", trim($val));
  1286. // Resolve the url now in the context of the current stylesheet
  1287. $parsed_url = explode_url($val);
  1288. if ( $parsed_url["protocol"] == "" && $this->_stylesheet->get_protocol() == "" ) {
  1289. if ($parsed_url["path"][0] === '/' || $parsed_url["path"][0] === '\\' ) {
  1290. $path = $_SERVER["DOCUMENT_ROOT"].'/';
  1291. }
  1292. else {
  1293. $path = $this->_stylesheet->get_base_path();
  1294. }
  1295. $path .= $parsed_url["path"] . $parsed_url["file"];
  1296. $path = realpath($path);
  1297. // If realpath returns FALSE then specifically state that there is no background image
  1298. if ( !$path ) {
  1299. $path = 'none';
  1300. }
  1301. }
  1302. else {
  1303. $path = build_url($this->_stylesheet->get_protocol(),
  1304. $this->_stylesheet->get_host(),
  1305. $this->_stylesheet->get_base_path(),
  1306. $val);
  1307. }
  1308. }
  1309. if ($DEBUGCSS) {
  1310. print "<pre>[_image\n";
  1311. print_r($parsed_url);
  1312. print $this->_stylesheet->get_protocol()."\n".$this->_stylesheet->get_base_path()."\n".$path."\n";
  1313. print "_image]</pre>";;
  1314. }
  1315. return $path;
  1316. }
  1317. /*======================*/
  1318. /**
  1319. * Sets color
  1320. *
  1321. * The color parameter can be any valid CSS color value
  1322. *
  1323. * @link http://www.w3.org/TR/CSS21/colors.html#propdef-color
  1324. * @param string $color
  1325. */
  1326. function set_color($color) {
  1327. $col = $this->munge_color($color);
  1328. if ( is_null($col) || !isset($col["hex"]) ) {
  1329. $color = "inherit";
  1330. }
  1331. else {
  1332. $color = $col["hex"];
  1333. }
  1334. //see __set and __get, on all assignments clear cache, not needed on direct set through __set
  1335. $this->_prop_cache["color"] = null;
  1336. $this->_props["color"] = $color;
  1337. }
  1338. /**
  1339. * Sets the background color
  1340. *
  1341. * @link http://www.w3.org/TR/CSS21/colors.html#propdef-background-color
  1342. * @param string $color
  1343. */
  1344. function set_background_color($color) {
  1345. $col = $this->munge_color($color);
  1346. if ( is_null($col) ) {
  1347. return;
  1348. //$col = self::$_defaults["background_color"];
  1349. }
  1350. //see __set and __get, on all assignments clear cache, not needed on direct set through __set
  1351. $this->_prop_cache["background_color"] = null;
  1352. $this->_props["background_color"] = is_array($col) ? $col["hex"] : $col;
  1353. }
  1354. /**
  1355. * Set the background image url
  1356. * @link http://www.w3.org/TR/CSS21/colors.html#background-properties
  1357. *
  1358. * @param string $val
  1359. */
  1360. function set_background_image($val) {
  1361. //see __set and __get, on all assignments clear cache, not needed on direct set through __set
  1362. $this->_prop_cache["background_image"] = null;
  1363. $this->_props["background_image"] = $this->_image($val);
  1364. }
  1365. /**
  1366. * Sets the background repeat
  1367. *
  1368. * @link http://www.w3.org/TR/CSS21/colors.html#propdef-background-repeat
  1369. * @param string $val
  1370. */
  1371. function set_background_repeat($val) {
  1372. if ( is_null($val) ) {
  1373. $val = self::$_defaults["background_repeat"];
  1374. }
  1375. //see __set and __get, on all assignments clear cache, not needed on direct set through __set
  1376. $this->_prop_cache["background_repeat"] = null;
  1377. $this->_props["background_repeat"] = $val;
  1378. }
  1379. /**
  1380. * Sets the background attachment
  1381. *
  1382. * @link http://www.w3.org/TR/CSS21/colors.html#propdef-background-attachment
  1383. * @param string $val
  1384. */
  1385. function set_background_attachment($val) {
  1386. if ( is_null($val) ) {
  1387. $val = self::$_defaults["background_attachment"];
  1388. }
  1389. //see __set and __get, on all assignments clear cache, not needed on direct set through __set
  1390. $this->_prop_cache["background_attachment"] = null;
  1391. $this->_props["background_attachment"] = $val;
  1392. }
  1393. /**
  1394. * Sets the background position
  1395. *
  1396. * @link http://www.w3.org/TR/CSS21/colors.html#propdef-background-position
  1397. * @param string $val
  1398. */
  1399. function set_background_position($val) {
  1400. if ( is_null($val) ) {
  1401. $val = self::$_defaults["background_position"];
  1402. }
  1403. //see __set and __get, on all assignments clear cache, not needed on direct set through __set
  1404. $this->_prop_cache["background_position"] = null;
  1405. $this->_props["background_position"] = $val;
  1406. }
  1407. /**
  1408. * Sets the background - combined options
  1409. *
  1410. * @link http://www.w3.org/TR/CSS21/colors.html#propdef-background
  1411. * @param string $val
  1412. */
  1413. function set_background($val) {
  1414. $val = trim($val);
  1415. $important = isset($this->_important_props["background"]);
  1416. if ( $val === "none" ) {
  1417. $this->_set_style("background_image", "none", $important);
  1418. $this->_set_style("background_color", "transparent", $important);
  1419. }
  1420. else {
  1421. $pos = array();
  1422. $tmp = preg_replace("/\s*\,\s*/", ",", $val); // when rgb() has spaces
  1423. $tmp = preg_split("/\s+/", $tmp);
  1424. foreach($tmp as $attr) {
  1425. if ( mb_substr($attr, 0, 3) === "url" || $attr === "none" ) {
  1426. $this->_set_style("background_image", $this->_image($attr), $important);
  1427. }
  1428. elseif ( $attr === "fixed" || $attr === "scroll" ) {
  1429. $this->_set_style("background_attachment", $attr, $important);
  1430. }
  1431. elseif ( $attr === "repeat" || $attr === "repeat-x" || $attr === "repeat-y" || $attr === "no-repeat" ) {
  1432. $this->_set_style("background_repeat", $attr, $important);
  1433. }
  1434. elseif ( ($col = $this->munge_color($attr)) != null ) {
  1435. $this->_set_style("background_color", is_array($col) ? $col["hex"] : $col, $important);
  1436. }
  1437. else {
  1438. $pos[] = $attr;
  1439. }
  1440. }
  1441. if (count($pos)) {
  1442. $this->_set_style("background_position", implode(" ", $pos), $important);
  1443. }
  1444. }
  1445. //see __set and __get, on all assignments clear cache, not needed on direct set through __set
  1446. $this->_prop_cache["background"] = null;
  1447. $this->_props["background"] = $val;
  1448. }
  1449. /**
  1450. * Sets the font size
  1451. *
  1452. * $size can be any acceptable CSS size
  1453. *
  1454. * @link http://www.w3.org/TR/CSS21/fonts.html#propdef-font-size
  1455. * @param string|float $size
  1456. */
  1457. function set_font_size($size) {
  1458. $this->__font_size_calculated = false;
  1459. //see __set and __get, on all assignments clear cache, not needed on direct set through __set
  1460. $this->_prop_cache["font_size"] = null;
  1461. $this->_props["font_size"] = $size;
  1462. }
  1463. /**
  1464. * Sets the font style
  1465. *
  1466. * combined attributes
  1467. * set individual attributes also, respecting !important mark
  1468. * exactly this order, separate by space. Multiple fonts separated by comma:
  1469. * font-style, font-variant, font-weight, font-size, line-height, font-family
  1470. *
  1471. * Other than with border and list, existing partial attributes should
  1472. * reset when starting here, even when not mentioned.
  1473. * If individual attribute is !important and explicite or implicite replacement is not,
  1474. * keep individual attribute
  1475. *
  1476. * require whitespace as delimiters for single value attributes
  1477. * On delimiter "/" treat first as font height, second as line height
  1478. * treat all remaining at the end of line as font
  1479. * font-style, font-variant, font-weight, font-size, line-height, font-family
  1480. *
  1481. * missing font-size and font-family might be not allowed, but accept it here and
  1482. * use default (medium size, enpty font name)
  1483. *
  1484. * @link http://www.w3.org/TR/CSS21/generate.html#propdef-list-style
  1485. * @param $val
  1486. */
  1487. function set_font($val) {
  1488. $this->__font_size_calculated = false;
  1489. //see __set and __get, on all assignments clear cache, not needed on direct set through __set
  1490. $this->_prop_cache["font"] = null;
  1491. $this->_props["font"] = $val;
  1492. $important = isset($this->_important_props["font"]);
  1493. if ( preg_match("/^(italic|oblique|normal)\s*(.*)$/i",$val,$match) ) {
  1494. $this->_set_style("font_style", $match[1], $important);
  1495. $val = $match[2];
  1496. }
  1497. else {
  1498. $this->_set_style("font_style", self::$_defaults["font_style"], $important);
  1499. }
  1500. if ( preg_match("/^(small-caps|normal)\s*(.*)$/i",$val,$match) ) {
  1501. $this->_set_style("font_variant", $match[1], $important);
  1502. $val = $match[2];
  1503. }
  1504. else {
  1505. $this->_set_style("font_variant", self::$_defaults["font_variant"], $important);
  1506. }
  1507. //matching numeric value followed by unit -> this is indeed a subsequent font size. Skip!
  1508. if ( preg_match("/^(bold|bolder|lighter|100|200|300|400|500|600|700|800|900|normal)\s*(.*)$/i", $val, $match) &&
  1509. !preg_match("/^(?:pt|px|pc|em|ex|in|cm|mm|%)/",$match[2])
  1510. ) {
  1511. $this->_set_style("font_weight", $match[1], $important);
  1512. $val = $match[2];
  1513. }
  1514. else {
  1515. $this->_set_style("font_weight", self::$_defaults["font_weight"], $important);
  1516. }
  1517. if ( preg_match("/^(xx-small|x-small|small|medium|large|x-large|xx-large|smaller|larger|\d+\s*(?:pt|px|pc|em|ex|in|cm|mm|%))\s*(.*)$/i",$val,$match) ) {
  1518. $this->_set_style("font_size", $match[1], $important);
  1519. $val = $match[2];
  1520. if ( preg_match("/^\/\s*(\d+\s*(?:pt|px|pc|em|ex|in|cm|mm|%))\s*(.*)$/i", $val, $match ) ) {
  1521. $this->_set_style("line_height", $match[1], $important);
  1522. $val = $match[2];
  1523. }
  1524. else {
  1525. $this->_set_style("line_height", self::$_defaults["line_height"], $important);
  1526. }
  1527. }
  1528. else {
  1529. $this->_set_style("font_size", self::$_defaults["font_size"], $important);
  1530. $this->_set_style("line_height", self::$_defaults["line_height"], $important);
  1531. }
  1532. if( strlen($val) != 0 ) {
  1533. $this->_set_style("font_family", $val, $important);
  1534. }
  1535. else {
  1536. $this->_set_style("font_family", self::$_defaults["font_family"], $important);
  1537. }
  1538. }
  1539. /**#@+
  1540. * Sets page break properties
  1541. *
  1542. * @link http://www.w3.org/TR/CSS21/page.html#page-breaks
  1543. * @param string $break
  1544. */
  1545. function set_page_break_before($break) {
  1546. if ( $break === "left" || $break === "right" ) {
  1547. $break = "always";
  1548. }
  1549. //see __set and __get, on all assignments clear cache, not needed on direct set through __set
  1550. $this->_prop_cache["page_break_before"] = null;
  1551. $this->_props["page_break_before"] = $break;
  1552. }
  1553. function set_page_break_after($break) {
  1554. if ( $break === "left" || $break === "right" ) {
  1555. $break = "always";
  1556. }
  1557. //see __set and __get, on all assignments clear cache, not needed on direct set through __set
  1558. $this->_prop_cache["page_break_after"] = null;
  1559. $this->_props["page_break_after"] = $break;
  1560. }
  1561. /**#@-*/
  1562. //........................................................................
  1563. /**#@+
  1564. * Sets the margin size
  1565. *
  1566. * @link http://www.w3.org/TR/CSS21/box.html#margin-properties
  1567. * @param $val
  1568. */
  1569. function set_margin_top($val) {
  1570. $this->_set_style_side_width_important('margin','top',$val);
  1571. }
  1572. function set_margin_right($val) {
  1573. $this->_set_style_side_width_important('margin','right',$val);
  1574. }
  1575. function set_margin_bottom($val) {
  1576. $this->_set_style_side_width_important('margin','bottom',$val);
  1577. }
  1578. function set_margin_left($val) {
  1579. $this->_set_style_side_width_important('margin','left',$val);
  1580. }
  1581. function set_margin($val) {
  1582. $val = str_replace("none", "0px", $val);
  1583. $this->_set_style_type_important('margin','',$val);
  1584. }
  1585. /**#@-*/
  1586. /**#@+
  1587. * Sets the padding size
  1588. *
  1589. * @link http://www.w3.org/TR/CSS21/box.html#padding-properties
  1590. * @param $val
  1591. */
  1592. function set_padding_top($val) {
  1593. $this->_set_style_side_width_important('padding','top',$val);
  1594. }
  1595. function set_padding_right($val) {
  1596. $this->_set_style_side_width_important('padding','right',$val);
  1597. }
  1598. function set_padding_bottom($val) {
  1599. $this->_set_style_side_width_important('padding','bottom',$val);
  1600. }
  1601. function set_padding_left($val) {
  1602. $this->_set_style_side_width_important('padding','left',$val);
  1603. }
  1604. function set_padding($val) {
  1605. $val = str_replace("none", "0px", $val);
  1606. $this->_set_style_type_important('padding','',$val);
  1607. }
  1608. /**#@-*/
  1609. /**
  1610. * Sets a single border
  1611. *
  1612. * @param string $side
  1613. * @param string $border_spec ([width] [style] [color])
  1614. * @param boolean $important
  1615. */
  1616. protected function _set_border($side, $border_spec, $important) {
  1617. $border_spec = preg_replace("/\s*\,\s*/", ",", $border_spec);
  1618. //$border_spec = str_replace(",", " ", $border_spec); // Why did we have this ?? rbg(10, 102, 10) > rgb(10 102 10)
  1619. $arr = explode(" ", $border_spec);
  1620. // FIXME: handle partial values
  1621. //For consistency of individal and combined properties, and with ie8 and firefox3
  1622. //reset all attributes, even if only partially given
  1623. $this->_set_style_side_type('border',$side,'_style',self::$_defaults['border_'.$side.'_style'],$important);
  1624. $this->_set_style_side_type('border',$side,'_width',self::$_defaults['border_'.$side.'_width'],$important);
  1625. $this->_set_style_side_type('border',$side,'_color',self::$_defaults['border_'.$side.'_color'],$important);
  1626. foreach ($arr as $value) {
  1627. $value = trim($value);
  1628. if ( in_array($value, self::$BORDER_STYLES) ) {
  1629. $this->_set_style_side_type('border',$side,'_style',$value,$important);
  1630. }
  1631. else if ( preg_match("/[.0-9]+(?:px|pt|pc|em|ex|%|in|mm|cm)|(?:thin|medium|thick)/", $value ) ) {
  1632. $this->_set_style_side_type('border',$side,'_width',$value,$important);
  1633. }
  1634. else {
  1635. // must be color
  1636. $this->_set_style_side_type('border',$side,'_color',$value,$important);
  1637. }
  1638. }
  1639. //see __set and __get, on all assignments clear cache!
  1640. $this->_prop_cache['border_'.$side] = null;
  1641. $this->_props['border_'.$side] = $border_spec;
  1642. }
  1643. /**
  1644. * Sets the border styles
  1645. *
  1646. * @link http://www.w3.org/TR/CSS21/box.html#border-properties
  1647. * @param string $val
  1648. */
  1649. function set_border_top($val) {
  1650. $this->_set_border("top", $val, isset($this->_important_props['border_top']));
  1651. }
  1652. function set_border_right($val) {
  1653. $this->_set_border("right", $val, isset($this->_important_props['border_right']));
  1654. }
  1655. function set_border_bottom($val) {
  1656. $this->_set_border("bottom", $val, isset($this->_important_props['border_bottom']));
  1657. }
  1658. function set_border_left($val) {
  1659. $this->_set_border("left", $val, isset($this->_important_props['border_left']));
  1660. }
  1661. function set_border($val) {
  1662. $important = isset($this->_important_props["border"]);
  1663. $this->_set_border("top", $val, $important);
  1664. $this->_set_border("right", $val, $important);
  1665. $this->_set_border("bottom", $val, $important);
  1666. $this->_set_border("left", $val, $important);
  1667. //see __set and __get, on all assignments clear cache, not needed on direct set through __set
  1668. $this->_prop_cache["border"] = null;
  1669. $this->_props["border"] = $val;
  1670. }
  1671. function set_border_width($val) {
  1672. $this->_set_style_type_important('border','_width',$val);
  1673. }
  1674. function set_border_color($val) {
  1675. $this->_set_style_type_important('border','_color',$val);
  1676. }
  1677. function set_border_style($val) {
  1678. $this->_set_style_type_important('border','_style',$val);
  1679. }
  1680. /**
  1681. * Sets the border radius size
  1682. *
  1683. * http://www.w3.org/TR/css3-background/#corners
  1684. */
  1685. function set_border_top_left_radius($val) {
  1686. $this->_set_border_radius_corner($val, "top_left");
  1687. }
  1688. function set_border_top_right_radius($val) {
  1689. $this->_set_border_radius_corner($val, "top_right");
  1690. }
  1691. function set_border_bottom_left_radius($val) {
  1692. $this->_set_border_radius_corner($val, "bottom_left");
  1693. }
  1694. function set_border_bottom_right_radius($val) {
  1695. $this->_set_border_radius_corner($val, "bottom_right");
  1696. }
  1697. function set_border_radius($val) {
  1698. $val = preg_replace("/\s*\,\s*/", ",", $val); // when border-radius has spaces
  1699. $arr = explode(" ", $val);
  1700. switch (count($arr)) {
  1701. case 1: $this->_set_border_radii($arr[0],$arr[0],$arr[0],$arr[0]); break;
  1702. case 2: $this->_set_border_radii($arr[0],$arr[1],$arr[0],$arr[1]); break;
  1703. case 3: $this->_set_border_radii($arr[0],$arr[1],$arr[2],$arr[1]); break;
  1704. case 4: $this->_set_border_radii($arr[0],$arr[1],$arr[2],$arr[3]); break;
  1705. }
  1706. }
  1707. protected function _set_border_radii($val1, $val2, $val3, $val4) {
  1708. $this->_set_border_radius_corner($val1, "top_left");
  1709. $this->_set_border_radius_corner($val2, "top_right");
  1710. $this->_set_border_radius_corner($val3, "bottom_right");
  1711. $this->_set_border_radius_corner($val4, "bottom_left");
  1712. }
  1713. protected function _set_border_radius_corner($val, $corner) {
  1714. $this->_has_border_radius = true;
  1715. //see __set and __get, on all assignments clear cache!
  1716. $this->_prop_cache["border_" . $corner . "_radius"] = null;
  1717. $this->_props["border_" . $corner . "_radius"] = $this->length_in_pt($val);
  1718. }
  1719. /**
  1720. * Sets the outline styles
  1721. *
  1722. * @link http://www.w3.org/TR/CSS21/ui.html#dynamic-outlines
  1723. * @param string $val
  1724. */
  1725. function set_outline($val) {
  1726. $important = isset($this->_important_props["outline"]);
  1727. $props = array(
  1728. "outline_style",
  1729. "outline_width",
  1730. "outline_color",
  1731. );
  1732. foreach($props as $prop) {
  1733. $_val = self::$_defaults[$prop];
  1734. if ( !isset($this->_important_props[$prop]) || $important) {
  1735. //see __set and __get, on all assignments clear cache!
  1736. $this->_prop_cache[$prop] = null;
  1737. if ( $important ) {
  1738. $this->_important_props[$prop] = true;
  1739. }
  1740. $this->_props[$prop] = $_val;
  1741. }
  1742. }
  1743. $val = preg_replace("/\s*\,\s*/", ",", $val); // when rgb() has spaces
  1744. $arr = explode(" ", $val);
  1745. foreach ($arr as $value) {
  1746. $value = trim($value);
  1747. if ( in_array($value, self::$BORDER_STYLES) ) {
  1748. $this->set_outline_style($value);
  1749. }
  1750. else if ( preg_match("/[.0-9]+(?:px|pt|pc|em|ex|%|in|mm|cm)|(?:thin|medium|thick)/", $value ) ) {
  1751. $this->set_outline_width($value);
  1752. }
  1753. else {
  1754. // must be color
  1755. $this->set_outline_color($value);
  1756. }
  1757. }
  1758. //see __set and __get, on all assignments clear cache, not needed on direct set through __set
  1759. $this->_prop_cache["outline"] = null;
  1760. $this->_props["outline"] = $val;
  1761. }
  1762. function set_outline_width($val) {
  1763. $this->_set_style_type_important('outline','_width',$val);
  1764. }
  1765. function set_outline_color($val) {
  1766. $this->_set_style_type_important('outline','_color',$val);
  1767. }
  1768. function set_outline_style($val) {
  1769. $this->_set_style_type_important('outline','_style',$val);
  1770. }
  1771. /**
  1772. * Sets the border spacing
  1773. *
  1774. * @link http://www.w3.org/TR/CSS21/box.html#border-properties
  1775. * @param float $val
  1776. */
  1777. function set_border_spacing($val) {
  1778. $arr = explode(" ", $val);
  1779. if ( count($arr) == 1 ) {
  1780. $arr[1] = $arr[0];
  1781. }
  1782. //see __set and __get, on all assignments clear cache, not needed on direct set through __set
  1783. $this->_prop_cache["border_spacing"] = null;
  1784. $this->_props["border_spacing"] = "$arr[0] $arr[1]";
  1785. }
  1786. /**
  1787. * Sets the list style image
  1788. *
  1789. * @link http://www.w3.org/TR/CSS21/generate.html#propdef-list-style-image
  1790. * @param $val
  1791. */
  1792. function set_list_style_image($val) {
  1793. //see __set and __get, on all assignments clear cache, not needed on direct set through __set
  1794. $this->_prop_cache["list_style_image"] = null;
  1795. $this->_props["list_style_image"] = $this->_image($val);
  1796. }
  1797. /**
  1798. * Sets the list style
  1799. *
  1800. * @link http://www.w3.org/TR/CSS21/generate.html#propdef-list-style
  1801. * @param $val
  1802. */
  1803. function set_list_style($val) {
  1804. $important = isset($this->_important_props["list_style"]);
  1805. $arr = explode(" ", str_replace(",", " ", $val));
  1806. static $types = array(
  1807. "disc", "circle", "square",
  1808. "decimal-leading-zero", "decimal", "1",
  1809. "lower-roman", "upper-roman", "a", "A",
  1810. "lower-greek",
  1811. "lower-latin", "upper-latin",
  1812. "lower-alpha", "upper-alpha",
  1813. "armenian", "georgian", "hebrew",
  1814. "cjk-ideographic", "hiragana", "katakana",
  1815. "hiragana-iroha", "katakana-iroha", "none"
  1816. );
  1817. static $positions = array("inside", "outside");
  1818. foreach ($arr as $value) {
  1819. /* http://www.w3.org/TR/CSS21/generate.html#list-style
  1820. * A value of 'none' for the 'list-style' property sets both 'list-style-type' and 'list-style-image' to 'none'
  1821. */
  1822. if ( $value === "none" ) {
  1823. $this->_set_style("list_style_type", $value, $important);
  1824. $this->_set_style("list_style_image", $value, $important);
  1825. continue;
  1826. }
  1827. //On setting or merging or inheriting list_style_image as well as list_style_type,
  1828. //and url exists, then url has precedence, otherwise fall back to list_style_type
  1829. //Firefox is wrong here (list_style_image gets overwritten on explicite list_style_type)
  1830. //Internet Explorer 7/8 and dompdf is right.
  1831. if ( mb_substr($value, 0, 3) === "url" ) {
  1832. $this->_set_style("list_style_image", $this->_image($value), $important);
  1833. continue;
  1834. }
  1835. if ( in_array($value, $types) ) {
  1836. $this->_set_style("list_style_type", $value, $important);
  1837. }
  1838. else if ( in_array($value, $positions) ) {
  1839. $this->_set_style("list_style_position", $value, $important);
  1840. }
  1841. }
  1842. //see __set and __get, on all assignments clear cache, not needed on direct set through __set
  1843. $this->_prop_cache["list_style"] = null;
  1844. $this->_props["list_style"] = $val;
  1845. }
  1846. function set_size($val) {
  1847. $length_re = "/(\d+\s*(?:pt|px|pc|em|ex|in|cm|mm|%))/";
  1848. $val = mb_strtolower($val);
  1849. if ( $val === "auto" ) {
  1850. return;
  1851. }
  1852. $parts = preg_split("/\s+/", $val);
  1853. $computed = array();
  1854. if ( preg_match($length_re, $parts[0]) ) {
  1855. $computed[] = $this->length_in_pt($parts[0]);
  1856. if ( isset($parts[1]) && preg_match($length_re, $parts[1]) ) {
  1857. $computed[] = $this->length_in_pt($parts[1]);
  1858. }
  1859. else {
  1860. $computed[] = $computed[0];
  1861. }
  1862. }
  1863. elseif ( isset(CPDF_Adapter::$PAPER_SIZES[$parts[0]]) ) {
  1864. $computed = array_slice(CPDF_Adapter::$PAPER_SIZES[$parts[0]], 2, 2);
  1865. if ( isset($parts[1]) && $parts[1] === "landscape" ) {
  1866. $computed = array_reverse($computed);
  1867. }
  1868. }
  1869. else {
  1870. return;
  1871. }
  1872. $this->_props["size"] = $computed;
  1873. }
  1874. /**
  1875. * Sets the CSS3 transform property
  1876. *
  1877. * @link http://www.w3.org/TR/css3-2d-transforms/#transform-property
  1878. * @param string $val
  1879. */
  1880. function set_transform($val) {
  1881. $number = "\s*([^,\s]+)\s*";
  1882. $tr_value = "\s*([^,\s]+)\s*";
  1883. $angle = "\s*([^,\s]+(?:deg|rad)?)\s*";
  1884. if ( !preg_match_all("/[a-z]+\([^\)]+\)/i", $val, $parts, PREG_SET_ORDER) ) {
  1885. return;
  1886. }
  1887. $functions = array(
  1888. //"matrix" => "\($number,$number,$number,$number,$number,$number\)",
  1889. "translate" => "\($tr_value(?:,$tr_value)?\)",
  1890. "translateX" => "\($tr_value\)",
  1891. "translateY" => "\($tr_value\)",
  1892. "scale" => "\($number(?:,$number)?\)",
  1893. "scaleX" => "\($number\)",
  1894. "scaleY" => "\($number\)",
  1895. "rotate" => "\($angle\)",
  1896. "skew" => "\($angle(?:,$angle)?\)",
  1897. "skewX" => "\($angle\)",
  1898. "skewY" => "\($angle\)",
  1899. );
  1900. $transforms = array();
  1901. foreach($parts as $part) {
  1902. $t = $part[0];
  1903. foreach($functions as $name => $pattern) {
  1904. if ( preg_match("/$name\s*$pattern/i", $t, $matches) ) {
  1905. $values = array_slice($matches, 1);
  1906. switch($name) {
  1907. // <angle> units
  1908. case "rotate":
  1909. case "skew":
  1910. case "skewX":
  1911. case "skewY":
  1912. foreach($values as $i => $value) {
  1913. if ( strpos($value, "rad") ) {
  1914. $values[$i] = rad2deg(floatval($value));
  1915. }
  1916. else {
  1917. $values[$i] = floatval($value);
  1918. }
  1919. }
  1920. switch($name) {
  1921. case "skew":
  1922. if ( !isset($values[1]) ) {
  1923. $values[1] = 0;
  1924. }
  1925. break;
  1926. case "skewX":
  1927. $name = "skew";
  1928. $values = array($values[0], 0);
  1929. break;
  1930. case "skewY":
  1931. $name = "skew";
  1932. $values = array(0, $values[0]);
  1933. break;
  1934. }
  1935. break;
  1936. // <translation-value> units
  1937. case "translate":
  1938. $values[0] = $this->length_in_pt($values[0], $this->width);
  1939. if ( isset($values[1]) ) {
  1940. $values[1] = $this->length_in_pt($values[1], $this->height);
  1941. }
  1942. else {
  1943. $values[1] = 0;
  1944. }
  1945. break;
  1946. case "translateX":
  1947. $name = "translate";
  1948. $values = array($this->length_in_pt($values[0], $this->width), 0);
  1949. break;
  1950. case "translateY":
  1951. $name = "translate";
  1952. $values = array(0, $this->length_in_pt($values[0], $this->height));
  1953. break;
  1954. // <number> units
  1955. case "scale":
  1956. if ( !isset($values[1]) ) {
  1957. $values[1] = $values[0];
  1958. }
  1959. break;
  1960. case "scaleX":
  1961. $name = "scale";
  1962. $values = array($values[0], 1.0);
  1963. break;
  1964. case "scaleY":
  1965. $name = "scale";
  1966. $values = array(1.0, $values[0]);
  1967. break;
  1968. }
  1969. $transforms[] = array(
  1970. $name,
  1971. $values,
  1972. );
  1973. }
  1974. }
  1975. }
  1976. //see __set and __get, on all assignments clear cache, not needed on direct set through __set
  1977. $this->_prop_cache["transform"] = null;
  1978. $this->_props["transform"] = $transforms;
  1979. }
  1980. function set__webkit_transform($val) {
  1981. $this->set_transform($val);
  1982. }
  1983. function set__webkit_transform_origin($val) {
  1984. $this->set_transform_origin($val);
  1985. }
  1986. /**
  1987. * Sets the CSS3 transform-origin property
  1988. *
  1989. * @link http://www.w3.org/TR/css3-2d-transforms/#transform-origin
  1990. * @param string $val
  1991. */
  1992. function set_transform_origin($val) {
  1993. $values = preg_split("/\s+/", $val);
  1994. if ( count($values) === 0) {
  1995. return;
  1996. }
  1997. foreach($values as &$value) {
  1998. if ( in_array($value, array("top", "left")) ) {
  1999. $value = 0;
  2000. }
  2001. if ( in_array($value, array("bottom", "right")) ) {
  2002. $value = "100%";
  2003. }
  2004. }
  2005. if ( !isset($values[1]) ) {
  2006. $values[1] = $values[0];
  2007. }
  2008. //see __set and __get, on all assignments clear cache, not needed on direct set through __set
  2009. $this->_prop_cache["transform_origin"] = null;
  2010. $this->_props["transform_origin"] = $values;
  2011. }
  2012. protected function parse_image_resolution($val) {
  2013. // If exif data could be get:
  2014. // $re = '/^\s*(\d+|normal|auto)(?:\s*,\s*(\d+|normal))?\s*$/';
  2015. $re = '/^\s*(\d+|normal|auto)\s*$/';
  2016. if ( !preg_match($re, $val, $matches) ) {
  2017. return null;
  2018. }
  2019. return $matches[1];
  2020. }
  2021. // auto | normal | dpi
  2022. function set_background_image_resolution($val) {
  2023. $parsed = $this->parse_image_resolution($val);
  2024. $this->_prop_cache["background_image_resolution"] = null;
  2025. $this->_props["background_image_resolution"] = $parsed;
  2026. }
  2027. // auto | normal | dpi
  2028. function set_image_resolution($val) {
  2029. $parsed = $this->parse_image_resolution($val);
  2030. $this->_prop_cache["image_resolution"] = null;
  2031. $this->_props["image_resolution"] = $parsed;
  2032. }
  2033. function set__dompdf_background_image_resolution($val) {
  2034. $this->set_background_image_resolution($val);
  2035. }
  2036. function set__dompdf_image_resolution($val) {
  2037. $this->set_image_resolution($val);
  2038. }
  2039. function set_z_index($val) {
  2040. if ( round($val) != $val && $val !== "auto" ) {
  2041. return;
  2042. }
  2043. $this->_prop_cache["z_index"] = null;
  2044. $this->_props["z_index"] = $val;
  2045. }
  2046. function set_counter_increment($val) {
  2047. $val = trim($val);
  2048. $value = null;
  2049. if ( in_array($val, array("none", "inherit")) ) {
  2050. $value = $val;
  2051. }
  2052. else {
  2053. if ( preg_match_all("/(".self::CSS_IDENTIFIER.")(?:\s+(".self::CSS_INTEGER."))?/", $val, $matches, PREG_SET_ORDER) ){
  2054. $value = array();
  2055. foreach($matches as $match) {
  2056. $value[$match[1]] = isset($match[2]) ? $match[2] : 1;
  2057. }
  2058. }
  2059. }
  2060. $this->_prop_cache["counter_increment"] = null;
  2061. $this->_props["counter_increment"] = $value;
  2062. }
  2063. /**
  2064. * Generate a string representation of the Style
  2065. *
  2066. * This dumps the entire property array into a string via print_r. Useful
  2067. * for debugging.
  2068. *
  2069. * @return string
  2070. */
  2071. /*DEBUGCSS print: see below additional debugging util*/
  2072. function __toString() {
  2073. return print_r(array_merge(array("parent_font_size" => $this->_parent_font_size),
  2074. $this->_props), true);
  2075. }
  2076. /*DEBUGCSS*/ function debug_print() {
  2077. /*DEBUGCSS*/ print "parent_font_size:".$this->_parent_font_size . ";\n";
  2078. /*DEBUGCSS*/ foreach($this->_props as $prop => $val ) {
  2079. /*DEBUGCSS*/ print $prop.':'.$val;
  2080. /*DEBUGCSS*/ if (isset($this->_important_props[$prop])) {
  2081. /*DEBUGCSS*/ print '!important';
  2082. /*DEBUGCSS*/ }
  2083. /*DEBUGCSS*/ print ";\n";
  2084. /*DEBUGCSS*/ }
  2085. /*DEBUGCSS*/ }
  2086. }