css-apply.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835
  1. // Copyright (C) 2004-2024 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #include "mupdf/fitz.h"
  23. #include "html-imp.h"
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <assert.h>
  28. static const char *border_width_kw[] = {
  29. "medium",
  30. "thick",
  31. "thin",
  32. };
  33. static const char *border_style_kw[] = {
  34. "dashed",
  35. "dotted",
  36. "double",
  37. "groove",
  38. "hidden",
  39. "inset",
  40. "none",
  41. "outset",
  42. "ridge",
  43. "solid",
  44. };
  45. static const char *color_kw[] = {
  46. "aqua",
  47. "black",
  48. "blue",
  49. "fuchsia",
  50. "gray",
  51. "green",
  52. "lime",
  53. "maroon",
  54. "navy",
  55. "olive",
  56. "orange",
  57. "purple",
  58. "red",
  59. "silver",
  60. "teal",
  61. "transparent",
  62. "white",
  63. "yellow",
  64. };
  65. static const char *list_style_type_kw[] = {
  66. "armenian",
  67. "circle",
  68. "decimal",
  69. "decimal-leading-zero",
  70. "disc",
  71. "georgian",
  72. "lower-alpha",
  73. "lower-greek",
  74. "lower-latin",
  75. "lower-roman",
  76. "none",
  77. "square",
  78. "upper-alpha",
  79. "upper-greek",
  80. "upper-latin",
  81. "upper-roman",
  82. };
  83. static const char *list_style_position_kw[] = {
  84. "inside",
  85. "outside",
  86. };
  87. static const char *font_style_kw[] = {
  88. "italic",
  89. "oblique",
  90. };
  91. static const char *font_variant_kw[] = {
  92. "small-caps",
  93. };
  94. static const char *font_weight_kw[] = {
  95. "bold",
  96. "bolder",
  97. "lighter",
  98. };
  99. static const char *font_size_kw[] = {
  100. "large",
  101. "larger",
  102. "medium",
  103. "small",
  104. "smaller",
  105. "x-large",
  106. "x-small",
  107. "xx-large",
  108. "xx-small",
  109. };
  110. /* Properties to ignore when scanning through font-family. We set font-family
  111. * to the full font shorthand value list because Adobe generates DS strings
  112. * where the font-family comes before the font-size (and not at the end as it's
  113. * supposed to). This lets us scan the font shorthand list without trying to
  114. * look up fonts named "bold", etc.
  115. */
  116. static const char *font_family_ignore[] = {
  117. ",",
  118. "/",
  119. "bold",
  120. "bolder",
  121. "italic",
  122. "large",
  123. "larger",
  124. "lighter",
  125. "medium",
  126. "oblique",
  127. "small",
  128. "small-caps",
  129. "smaller",
  130. "x-large",
  131. "x-small",
  132. "xx-large",
  133. "xx-small",
  134. };
  135. static int
  136. keyword_in_list(const char *name, const char **list, int n)
  137. {
  138. int l = 0;
  139. int r = n - 1;
  140. while (l <= r)
  141. {
  142. int m = (l + r) >> 1;
  143. int c = strcmp(name, list[m]);
  144. if (c < 0)
  145. r = m - 1;
  146. else if (c > 0)
  147. l = m + 1;
  148. else
  149. return 1;
  150. }
  151. return 0;
  152. }
  153. static int
  154. is_bold_from_font_weight(const char *weight)
  155. {
  156. return !strcmp(weight, "bold") || !strcmp(weight, "bolder") || atoi(weight) > 400;
  157. }
  158. static int
  159. is_italic_from_font_style(const char *style)
  160. {
  161. return !strcmp(style, "italic") || !strcmp(style, "oblique");
  162. }
  163. /*
  164. * Compute specificity
  165. */
  166. static int
  167. count_condition_ids(fz_css_condition *cond)
  168. {
  169. int n = 0;
  170. while (cond)
  171. {
  172. if (cond->type == '#')
  173. n ++;
  174. cond = cond->next;
  175. }
  176. return n;
  177. }
  178. static int
  179. count_selector_ids(fz_css_selector *sel)
  180. {
  181. int n = count_condition_ids(sel->cond);
  182. if (sel->left && sel->right)
  183. {
  184. n += count_selector_ids(sel->left);
  185. n += count_selector_ids(sel->right);
  186. }
  187. return n;
  188. }
  189. static int
  190. count_condition_atts(fz_css_condition *cond)
  191. {
  192. int n = 0;
  193. while (cond)
  194. {
  195. if (cond->type != '#' && cond->type != ':')
  196. n ++;
  197. cond = cond->next;
  198. }
  199. return n;
  200. }
  201. static int
  202. count_selector_atts(fz_css_selector *sel)
  203. {
  204. int n = count_condition_atts(sel->cond);
  205. if (sel->left && sel->right)
  206. {
  207. n += count_selector_atts(sel->left);
  208. n += count_selector_atts(sel->right);
  209. }
  210. return n;
  211. }
  212. static int
  213. count_condition_names(fz_css_condition *cond)
  214. {
  215. int n = 0;
  216. while (cond)
  217. {
  218. if (cond->type == ':')
  219. n ++;
  220. cond = cond->next;
  221. }
  222. return n;
  223. }
  224. static int
  225. count_selector_names(fz_css_selector *sel)
  226. {
  227. int n = count_condition_names(sel->cond);
  228. if (sel->left && sel->right)
  229. {
  230. n += count_selector_names(sel->left);
  231. n += count_selector_names(sel->right);
  232. }
  233. else if (sel->name)
  234. {
  235. n ++;
  236. }
  237. return n;
  238. }
  239. #define INLINE_SPECIFICITY 10000
  240. static int
  241. selector_specificity(fz_css_selector *sel, int important)
  242. {
  243. int b = count_selector_ids(sel);
  244. int c = count_selector_atts(sel);
  245. int d = count_selector_names(sel);
  246. return important * 1000 + b * 100 + c * 10 + d;
  247. }
  248. /*
  249. * Selector matching
  250. */
  251. static int
  252. match_att_exists_condition(fz_xml *node, const char *key)
  253. {
  254. const char *s = fz_xml_att(node, key);
  255. return s != NULL;
  256. }
  257. static int
  258. match_att_is_condition(fz_xml *node, const char *key, const char *val)
  259. {
  260. const char *att = fz_xml_att(node, key);
  261. return att && !strcmp(val, att);
  262. }
  263. static int
  264. match_att_has_condition(fz_xml *node, const char *att, const char *needle)
  265. {
  266. const char *haystack = fz_xml_att(node, att);
  267. const char *ss;
  268. size_t n;
  269. if (haystack) {
  270. ss = strstr(haystack, needle);
  271. if (ss)
  272. {
  273. n = strlen(needle);
  274. /* Look for exact matches or matching words. */
  275. if ((ss[n] == ' ' || ss[n] == 0) && (ss == haystack || ss[-1] == ' '))
  276. return 1;
  277. }
  278. }
  279. return 0;
  280. }
  281. static int
  282. match_condition(fz_css_condition *cond, fz_xml *node)
  283. {
  284. if (!cond)
  285. return 1;
  286. switch (cond->type) {
  287. default: return 0;
  288. case ':': return 0; /* don't support pseudo-classes */
  289. case '#': if (!match_att_is_condition(node, "id", cond->val)) return 0; break;
  290. case '.': if (!match_att_has_condition(node, "class", cond->val)) return 0; break;
  291. case '[': if (!match_att_exists_condition(node, cond->key)) return 0; break;
  292. case '=': if (!match_att_is_condition(node, cond->key, cond->val)) return 0; break;
  293. case '~': if (!match_att_has_condition(node, cond->key, cond->val)) return 0; break;
  294. case '|': if (!match_att_is_condition(node, cond->key, cond->val)) return 0; break;
  295. }
  296. return match_condition(cond->next, node);
  297. }
  298. static int
  299. match_selector(fz_css_selector *sel, fz_xml *node)
  300. {
  301. if (!node)
  302. return 0;
  303. if (sel->combine)
  304. {
  305. /* descendant */
  306. if (sel->combine == ' ')
  307. {
  308. fz_xml *parent = fz_xml_up(node);
  309. if (!parent || !match_selector(sel->right, node))
  310. return 0;
  311. while (parent)
  312. {
  313. if (match_selector(sel->left, parent))
  314. return 1;
  315. parent = fz_xml_up(parent);
  316. }
  317. return 0;
  318. }
  319. /* child */
  320. if (sel->combine == '>')
  321. {
  322. fz_xml *parent = fz_xml_up(node);
  323. if (!parent)
  324. return 0;
  325. if (!match_selector(sel->left, parent))
  326. return 0;
  327. if (!match_selector(sel->right, node))
  328. return 0;
  329. }
  330. /* adjacent */
  331. if (sel->combine == '+')
  332. {
  333. fz_xml *prev = fz_xml_prev(node);
  334. while (prev && !fz_xml_tag(prev))
  335. prev = fz_xml_prev(prev);
  336. if (!prev)
  337. return 0;
  338. if (!fz_xml_tag(prev))
  339. return 0;
  340. if (!match_selector(sel->left, prev))
  341. return 0;
  342. if (!match_selector(sel->right, node))
  343. return 0;
  344. }
  345. }
  346. if (sel->name)
  347. {
  348. if (!fz_xml_is_tag(node, sel->name))
  349. return 0;
  350. }
  351. if (sel->cond)
  352. {
  353. if (!match_condition(sel->cond, node))
  354. return 0;
  355. }
  356. return 1;
  357. }
  358. /*
  359. * Annotating nodes with properties and expanding shorthand forms.
  360. */
  361. static int
  362. count_values(fz_css_value *value)
  363. {
  364. int n = 0;
  365. while (value)
  366. {
  367. n++;
  368. value = value->next;
  369. }
  370. return n;
  371. }
  372. static void add_property(fz_css_match *match, int name, fz_css_value *value, int spec);
  373. static void
  374. add_shorthand_trbl(fz_css_match *match, fz_css_value *value, int spec,
  375. int name_t, int name_r, int name_b, int name_l)
  376. {
  377. int n = count_values(value);
  378. if (n == 1)
  379. {
  380. add_property(match, name_t, value, spec);
  381. add_property(match, name_r, value, spec);
  382. add_property(match, name_b, value, spec);
  383. add_property(match, name_l, value, spec);
  384. }
  385. if (n == 2)
  386. {
  387. fz_css_value *a = value;
  388. fz_css_value *b = value->next;
  389. add_property(match, name_t, a, spec);
  390. add_property(match, name_r, b, spec);
  391. add_property(match, name_b, a, spec);
  392. add_property(match, name_l, b, spec);
  393. }
  394. if (n == 3)
  395. {
  396. fz_css_value *a = value;
  397. fz_css_value *b = value->next;
  398. fz_css_value *c = value->next->next;
  399. add_property(match, name_t, a, spec);
  400. add_property(match, name_r, b, spec);
  401. add_property(match, name_b, c, spec);
  402. add_property(match, name_l, b, spec);
  403. }
  404. if (n == 4)
  405. {
  406. fz_css_value *a = value;
  407. fz_css_value *b = value->next;
  408. fz_css_value *c = value->next->next;
  409. fz_css_value *d = value->next->next->next;
  410. add_property(match, name_t, a, spec);
  411. add_property(match, name_r, b, spec);
  412. add_property(match, name_b, c, spec);
  413. add_property(match, name_l, d, spec);
  414. }
  415. }
  416. static void
  417. add_shorthand_margin(fz_css_match *match, fz_css_value *value, int spec)
  418. {
  419. add_shorthand_trbl(match, value, spec,
  420. PRO_MARGIN_TOP,
  421. PRO_MARGIN_RIGHT,
  422. PRO_MARGIN_BOTTOM,
  423. PRO_MARGIN_LEFT);
  424. }
  425. static void
  426. add_shorthand_padding(fz_css_match *match, fz_css_value *value, int spec)
  427. {
  428. add_shorthand_trbl(match, value, spec,
  429. PRO_PADDING_TOP,
  430. PRO_PADDING_RIGHT,
  431. PRO_PADDING_BOTTOM,
  432. PRO_PADDING_LEFT);
  433. }
  434. static void
  435. add_shorthand_border_width(fz_css_match *match, fz_css_value *value, int spec)
  436. {
  437. add_shorthand_trbl(match, value, spec,
  438. PRO_BORDER_TOP_WIDTH,
  439. PRO_BORDER_RIGHT_WIDTH,
  440. PRO_BORDER_BOTTOM_WIDTH,
  441. PRO_BORDER_LEFT_WIDTH);
  442. }
  443. static void
  444. add_shorthand_border_color(fz_css_match *match, fz_css_value *value, int spec)
  445. {
  446. add_shorthand_trbl(match, value, spec,
  447. PRO_BORDER_TOP_COLOR,
  448. PRO_BORDER_RIGHT_COLOR,
  449. PRO_BORDER_BOTTOM_COLOR,
  450. PRO_BORDER_LEFT_COLOR);
  451. }
  452. static void
  453. add_shorthand_border_style(fz_css_match *match, fz_css_value *value, int spec)
  454. {
  455. add_shorthand_trbl(match, value, spec,
  456. PRO_BORDER_TOP_STYLE,
  457. PRO_BORDER_RIGHT_STYLE,
  458. PRO_BORDER_BOTTOM_STYLE,
  459. PRO_BORDER_LEFT_STYLE);
  460. }
  461. static void
  462. add_shorthand_border(fz_css_match *match, fz_css_value *value, int spec, int T, int R, int B, int L)
  463. {
  464. while (value)
  465. {
  466. if (value->type == CSS_HASH)
  467. {
  468. if (T) add_property(match, PRO_BORDER_TOP_COLOR, value, spec);
  469. if (R) add_property(match, PRO_BORDER_RIGHT_COLOR, value, spec);
  470. if (B) add_property(match, PRO_BORDER_BOTTOM_COLOR, value, spec);
  471. if (L) add_property(match, PRO_BORDER_LEFT_COLOR, value, spec);
  472. }
  473. else if (value->type == CSS_KEYWORD)
  474. {
  475. if (keyword_in_list(value->data, border_width_kw, nelem(border_width_kw)))
  476. {
  477. if (T) add_property(match, PRO_BORDER_TOP_WIDTH, value, spec);
  478. if (R) add_property(match, PRO_BORDER_RIGHT_WIDTH, value, spec);
  479. if (B) add_property(match, PRO_BORDER_BOTTOM_WIDTH, value, spec);
  480. if (L) add_property(match, PRO_BORDER_LEFT_WIDTH, value, spec);
  481. }
  482. else if (keyword_in_list(value->data, border_style_kw, nelem(border_style_kw)))
  483. {
  484. if (T) add_property(match, PRO_BORDER_TOP_STYLE, value, spec);
  485. if (R) add_property(match, PRO_BORDER_RIGHT_STYLE, value, spec);
  486. if (B) add_property(match, PRO_BORDER_BOTTOM_STYLE, value, spec);
  487. if (L) add_property(match, PRO_BORDER_LEFT_STYLE, value, spec);
  488. }
  489. else if (keyword_in_list(value->data, color_kw, nelem(color_kw)))
  490. {
  491. if (T) add_property(match, PRO_BORDER_TOP_COLOR, value, spec);
  492. if (R) add_property(match, PRO_BORDER_RIGHT_COLOR, value, spec);
  493. if (B) add_property(match, PRO_BORDER_BOTTOM_COLOR, value, spec);
  494. if (L) add_property(match, PRO_BORDER_LEFT_COLOR, value, spec);
  495. }
  496. }
  497. else
  498. {
  499. if (T) add_property(match, PRO_BORDER_TOP_WIDTH, value, spec);
  500. if (R) add_property(match, PRO_BORDER_RIGHT_WIDTH, value, spec);
  501. if (B) add_property(match, PRO_BORDER_BOTTOM_WIDTH, value, spec);
  502. if (L) add_property(match, PRO_BORDER_LEFT_WIDTH, value, spec);
  503. }
  504. value = value->next;
  505. }
  506. }
  507. static void
  508. add_shorthand_list_style(fz_css_match *match, fz_css_value *value, int spec)
  509. {
  510. while (value)
  511. {
  512. if (value->type == CSS_KEYWORD)
  513. {
  514. if (keyword_in_list(value->data, list_style_type_kw, nelem(list_style_type_kw)))
  515. {
  516. add_property(match, PRO_LIST_STYLE_TYPE, value, spec);
  517. }
  518. else if (keyword_in_list(value->data, list_style_position_kw, nelem(list_style_position_kw)))
  519. {
  520. add_property(match, PRO_LIST_STYLE_POSITION, value, spec);
  521. }
  522. }
  523. value = value->next;
  524. }
  525. }
  526. static fz_css_value static_value_normal = { CSS_KEYWORD, "normal", NULL, NULL };
  527. static fz_css_value *
  528. add_shorthand_font_size(fz_css_match *match, fz_css_value *value, int spec)
  529. {
  530. /* font-size */
  531. add_property(match, PRO_FONT_SIZE, value, spec);
  532. /* / line-height */
  533. if (value->next && value->next->next && !strcmp(value->next->data, "/"))
  534. {
  535. value = value->next->next;
  536. add_property(match, PRO_LINE_HEIGHT, value, spec);
  537. }
  538. return value;
  539. }
  540. static void
  541. add_shorthand_font(fz_css_match *match, fz_css_value *value, int spec)
  542. {
  543. fz_css_value *font_style = NULL;
  544. fz_css_value *font_variant = NULL;
  545. fz_css_value *font_weight = NULL;
  546. /* add the start as font-family for most robust scanning of matching font names */
  547. add_property(match, PRO_FONT_FAMILY, value, spec);
  548. /* then look for known style/variant/weight keywords and font-size/line-height */
  549. for (; value; value = value->next)
  550. {
  551. /* style/variant/weight/size */
  552. if (value->type == CSS_KEYWORD)
  553. {
  554. if (keyword_in_list(value->data, font_style_kw, nelem(font_style_kw)))
  555. font_style = value;
  556. else if (keyword_in_list(value->data, font_variant_kw, nelem(font_variant_kw)))
  557. font_variant = value;
  558. else if (keyword_in_list(value->data, font_weight_kw, nelem(font_weight_kw)))
  559. font_weight = value;
  560. else if (keyword_in_list(value->data, font_size_kw, nelem(font_size_kw)))
  561. value = add_shorthand_font_size(match, value, spec);
  562. }
  563. else if (value->type == CSS_NUMBER)
  564. font_weight = value;
  565. else if (value->type == CSS_LENGTH || value->type == CSS_PERCENT)
  566. value = add_shorthand_font_size(match, value, spec);
  567. }
  568. /* set all properties to their initial values if not specified! */
  569. if (font_style)
  570. add_property(match, PRO_FONT_STYLE, font_style, spec);
  571. else
  572. add_property(match, PRO_FONT_STYLE, &static_value_normal, spec);
  573. if (font_variant)
  574. add_property(match, PRO_FONT_VARIANT, font_variant, spec);
  575. else
  576. add_property(match, PRO_FONT_VARIANT, &static_value_normal, spec);
  577. if (font_weight)
  578. add_property(match, PRO_FONT_WEIGHT, font_weight, spec);
  579. else
  580. add_property(match, PRO_FONT_WEIGHT, &static_value_normal, spec);
  581. }
  582. static void
  583. add_property(fz_css_match *match, int name, fz_css_value *value, int spec)
  584. {
  585. /* shorthand expansions: */
  586. switch (name)
  587. {
  588. case PRO_MARGIN:
  589. add_shorthand_margin(match, value, spec);
  590. return;
  591. case PRO_PADDING:
  592. add_shorthand_padding(match, value, spec);
  593. return;
  594. case PRO_BORDER_WIDTH:
  595. add_shorthand_border_width(match, value, spec);
  596. return;
  597. case PRO_BORDER_COLOR:
  598. add_shorthand_border_color(match, value, spec);
  599. return;
  600. case PRO_BORDER_STYLE:
  601. add_shorthand_border_style(match, value, spec);
  602. return;
  603. case PRO_BORDER:
  604. add_shorthand_border(match, value, spec, 1, 1, 1, 1);
  605. return;
  606. case PRO_BORDER_TOP:
  607. add_shorthand_border(match, value, spec, 1, 0, 0, 0);
  608. return;
  609. case PRO_BORDER_RIGHT:
  610. add_shorthand_border(match, value, spec, 0, 1, 0, 0);
  611. return;
  612. case PRO_BORDER_BOTTOM:
  613. add_shorthand_border(match, value, spec, 0, 0, 1, 0);
  614. return;
  615. case PRO_BORDER_LEFT:
  616. add_shorthand_border(match, value, spec, 0, 0, 0, 1);
  617. return;
  618. case PRO_LIST_STYLE:
  619. add_shorthand_list_style(match, value, spec);
  620. return;
  621. case PRO_FONT:
  622. add_shorthand_font(match, value, spec);
  623. return;
  624. /* TODO: background */
  625. }
  626. if (name < NUM_PROPERTIES && match->spec[name] <= spec)
  627. {
  628. match->value[name] = value;
  629. match->spec[name] = spec;
  630. }
  631. }
  632. void
  633. fz_match_css(fz_context *ctx, fz_css_match *match, fz_css_match *up, fz_css *css, fz_xml *node)
  634. {
  635. fz_css_rule *rule;
  636. fz_css_selector *sel;
  637. fz_css_property *prop;
  638. const char *s;
  639. int i;
  640. match->up = up;
  641. for (i = 0; i < NUM_PROPERTIES; ++i)
  642. {
  643. match->spec[i] = -1;
  644. match->value[i] = NULL;
  645. }
  646. for (rule = css->rule; rule; rule = rule->next)
  647. {
  648. sel = rule->selector;
  649. while (sel)
  650. {
  651. if (match_selector(sel, node))
  652. {
  653. for (prop = rule->declaration; prop; prop = prop->next)
  654. add_property(match, prop->name, prop->value, selector_specificity(sel, prop->important));
  655. break;
  656. }
  657. sel = sel->next;
  658. }
  659. }
  660. if (fz_use_document_css(ctx))
  661. {
  662. s = fz_xml_att(node, "style");
  663. if (s)
  664. {
  665. fz_try(ctx)
  666. {
  667. prop = fz_parse_css_properties(ctx, css->pool, s);
  668. while (prop)
  669. {
  670. add_property(match, prop->name, prop->value, INLINE_SPECIFICITY);
  671. prop = prop->next;
  672. }
  673. /* We can "leak" the property here, since it is freed along with the pool allocator. */
  674. }
  675. fz_catch(ctx)
  676. {
  677. fz_rethrow_if(ctx, FZ_ERROR_SYSTEM);
  678. fz_report_error(ctx);
  679. fz_warn(ctx, "ignoring style attribute");
  680. }
  681. }
  682. }
  683. }
  684. void
  685. fz_match_css_at_page(fz_context *ctx, fz_css_match *match, fz_css *css)
  686. {
  687. fz_css_rule *rule;
  688. fz_css_selector *sel;
  689. fz_css_property *prop;
  690. int i;
  691. match->up = NULL;
  692. for (i = 0; i < NUM_PROPERTIES; ++i)
  693. {
  694. match->spec[i] = -1;
  695. match->value[i] = NULL;
  696. }
  697. for (rule = css->rule; rule; rule = rule->next)
  698. {
  699. sel = rule->selector;
  700. while (sel)
  701. {
  702. if (sel->name && !strcmp(sel->name, "@page"))
  703. {
  704. for (prop = rule->declaration; prop; prop = prop->next)
  705. add_property(match, prop->name, prop->value, selector_specificity(sel, prop->important));
  706. break;
  707. }
  708. sel = sel->next;
  709. }
  710. }
  711. }
  712. void
  713. fz_add_css_font_face(fz_context *ctx, fz_html_font_set *set, fz_archive *zip, const char *base_uri, fz_css_property *declaration)
  714. {
  715. fz_html_font_face *custom;
  716. fz_css_property *prop;
  717. fz_font *font = NULL;
  718. fz_buffer *buf = NULL;
  719. fz_stream *stm = NULL;
  720. int is_bold, is_italic, is_small_caps;
  721. char path[2048];
  722. const char *family = "serif";
  723. const char *weight = "normal";
  724. const char *style = "normal";
  725. const char *variant = "normal";
  726. const char *src = NULL;
  727. for (prop = declaration; prop; prop = prop->next)
  728. {
  729. if (prop->name == PRO_FONT_FAMILY) family = prop->value->data;
  730. if (prop->name == PRO_FONT_WEIGHT) weight = prop->value->data;
  731. if (prop->name == PRO_FONT_STYLE) style = prop->value->data;
  732. if (prop->name == PRO_FONT_VARIANT) variant = prop->value->data;
  733. if (prop->name == PRO_SRC) src = prop->value->data;
  734. }
  735. if (!src)
  736. return;
  737. is_bold = is_bold_from_font_weight(weight);
  738. is_italic = is_italic_from_font_style(style);
  739. is_small_caps = !strcmp(variant, "small-caps");
  740. fz_strlcpy(path, base_uri, sizeof path);
  741. fz_strlcat(path, "/", sizeof path);
  742. fz_strlcat(path, src, sizeof path);
  743. fz_urldecode(path);
  744. for (custom = set->custom; custom; custom = custom->next)
  745. if (!strcmp(custom->src, path) && !strcmp(custom->family, family) &&
  746. custom->is_bold == is_bold &&
  747. custom->is_italic == is_italic &&
  748. custom->is_small_caps == is_small_caps)
  749. return; /* already loaded */
  750. fz_var(buf);
  751. fz_var(font);
  752. fz_var(stm);
  753. fz_try(ctx)
  754. {
  755. if (fz_has_archive_entry(ctx, zip, path))
  756. buf = fz_read_archive_entry(ctx, zip, path);
  757. else
  758. {
  759. stm = fz_try_open_file(ctx, src);
  760. if (stm == NULL)
  761. fz_throw(ctx, FZ_ERROR_FORMAT, "cannot locate font '%s' specified by css", src);
  762. buf = fz_read_all(ctx, stm, 0);
  763. }
  764. font = fz_new_font_from_buffer(ctx, NULL, buf, 0, 0);
  765. fz_add_html_font_face(ctx, set, family, is_bold, is_italic, is_small_caps, path, font);
  766. }
  767. fz_always(ctx)
  768. {
  769. fz_drop_buffer(ctx, buf);
  770. fz_drop_stream(ctx, stm);
  771. fz_drop_font(ctx, font);
  772. }
  773. fz_catch(ctx)
  774. {
  775. fz_rethrow_if(ctx, FZ_ERROR_TRYLATER);
  776. fz_rethrow_if(ctx, FZ_ERROR_SYSTEM);
  777. fz_report_error(ctx);
  778. fz_warn(ctx, "cannot load font-face: %s", src);
  779. }
  780. }
  781. void
  782. fz_add_css_font_faces(fz_context *ctx, fz_html_font_set *set, fz_archive *zip, const char *base_uri, fz_css *css)
  783. {
  784. fz_css_rule *rule;
  785. fz_css_selector *sel;
  786. for (rule = css->rule; rule; rule = rule->next)
  787. {
  788. if (!rule->loaded)
  789. {
  790. rule->loaded = 1;
  791. sel = rule->selector;
  792. while (sel)
  793. {
  794. if (sel->name && !strcmp(sel->name, "@font-face"))
  795. {
  796. fz_add_css_font_face(ctx, set, zip, base_uri, rule->declaration);
  797. break;
  798. }
  799. sel = sel->next;
  800. }
  801. }
  802. }
  803. }
  804. static int
  805. is_inheritable_property(int name)
  806. {
  807. return
  808. name == PRO_COLOR ||
  809. name == PRO_DIRECTION ||
  810. name == PRO_FONT_FAMILY ||
  811. name == PRO_FONT_STYLE ||
  812. name == PRO_FONT_VARIANT ||
  813. name == PRO_FONT_WEIGHT ||
  814. name == PRO_LEADING ||
  815. name == PRO_LETTER_SPACING ||
  816. name == PRO_LINE_HEIGHT ||
  817. name == PRO_LIST_STYLE_IMAGE ||
  818. name == PRO_LIST_STYLE_POSITION ||
  819. name == PRO_LIST_STYLE_TYPE ||
  820. name == PRO_ORPHANS ||
  821. name == PRO_OVERFLOW_WRAP ||
  822. name == PRO_QUOTES ||
  823. name == PRO_TEXT_ALIGN ||
  824. name == PRO_TEXT_INDENT ||
  825. name == PRO_TEXT_TRANSFORM ||
  826. name == PRO_VISIBILITY ||
  827. name == PRO_WHITE_SPACE ||
  828. name == PRO_WIDOWS ||
  829. name == PRO_WORD_SPACING ||
  830. // Strictly speaking, text-decoration is not an inherited property,
  831. // but since when drawing an underlined element, all children are also underlined,
  832. // we may as well make it inherited.
  833. name == PRO_TEXT_DECORATION;
  834. }
  835. static fz_css_value *
  836. value_from_inheritable_property(fz_css_match *match, int name)
  837. {
  838. fz_css_value *value = match->value[name];
  839. if (match->up)
  840. {
  841. if (value && !strcmp(value->data, "inherit"))
  842. return value_from_inheritable_property(match->up, name);
  843. if (!value)
  844. return value_from_inheritable_property(match->up, name);
  845. }
  846. return value;
  847. }
  848. static fz_css_value *
  849. value_from_property(fz_css_match *match, int name)
  850. {
  851. fz_css_value *value = match->value[name];
  852. if (match->up)
  853. {
  854. if (value && !strcmp(value->data, "inherit"))
  855. if (name != PRO_FONT_SIZE) /* never inherit 'font-size' textually */
  856. return value_from_property(match->up, name);
  857. if (!value && is_inheritable_property(name))
  858. return value_from_inheritable_property(match->up, name);
  859. }
  860. return value;
  861. }
  862. static const char *
  863. string_from_property(fz_css_match *match, int name, const char *initial)
  864. {
  865. fz_css_value *value = value_from_property(match, name);
  866. if (!value)
  867. return initial;
  868. return value->data;
  869. }
  870. static fz_css_number
  871. make_number(float v, int u)
  872. {
  873. fz_css_number n;
  874. n.value = v;
  875. n.unit = u;
  876. return n;
  877. }
  878. static fz_css_number
  879. make_undefined_number(void)
  880. {
  881. fz_css_number n;
  882. n.value = 0;
  883. n.unit = N_UNDEFINED;
  884. return n;
  885. }
  886. /* Fast but inaccurate strtof. */
  887. static float
  888. fz_css_strtof(char *s, char **endptr)
  889. {
  890. float sign = 1;
  891. float v = 0;
  892. float n = 0;
  893. float d = 1;
  894. if (*s == '-')
  895. {
  896. sign = -1;
  897. ++s;
  898. }
  899. while (*s >= '0' && *s <= '9')
  900. {
  901. v = v * 10 + (*s - '0');
  902. ++s;
  903. }
  904. if (*s == '.')
  905. {
  906. ++s;
  907. while (*s >= '0' && *s <= '9')
  908. {
  909. n = n * 10 + (*s - '0');
  910. d = d * 10;
  911. ++s;
  912. }
  913. v += n / d;
  914. }
  915. if (endptr)
  916. *endptr = s;
  917. return sign * v;
  918. }
  919. static fz_css_number
  920. number_from_value(fz_css_value *value, float initial, int initial_unit)
  921. {
  922. char *p;
  923. if (!value)
  924. return make_number(initial, initial_unit);
  925. if (value->type == CSS_PERCENT)
  926. return make_number(fz_css_strtof(value->data, NULL), N_PERCENT);
  927. if (value->type == CSS_NUMBER)
  928. return make_number(fz_css_strtof(value->data, NULL), N_NUMBER);
  929. if (value->type == CSS_LENGTH)
  930. {
  931. float x = fz_css_strtof(value->data, &p);
  932. if (p[0] == 'e' && p[1] == 'm' && p[2] == 0)
  933. return make_number(x, N_SCALE);
  934. if (p[0] == 'e' && p[1] == 'x' && p[2] == 0)
  935. return make_number(x / 2, N_SCALE);
  936. if (p[0] == 'i' && p[1] == 'n' && p[2] == 0)
  937. return make_number(x * 72, N_LENGTH);
  938. if (p[0] == 'c' && p[1] == 'm' && p[2] == 0)
  939. return make_number(x * 7200 / 254, N_LENGTH);
  940. if (p[0] == 'm' && p[1] == 'm' && p[2] == 0)
  941. return make_number(x * 720 / 254, N_LENGTH);
  942. if (p[0] == 'p' && p[1] == 'c' && p[2] == 0)
  943. return make_number(x * 12, N_LENGTH);
  944. if (p[0] == 'p' && p[1] == 't' && p[2] == 0)
  945. return make_number(x, N_LENGTH);
  946. if (p[0] == 'p' && p[1] == 'x' && p[2] == 0)
  947. return make_number(x, N_LENGTH);
  948. /* FIXME: 'rem' should be 'em' of root element. This is a bad approximation. */
  949. if (p[0] == 'r' && p[1] == 'e' && p[2] == 'm' && p[3] == 0)
  950. return make_number(x * 16, N_LENGTH);
  951. /* FIXME: 'ch' should be width of '0' character. This is an approximation. */
  952. if (p[0] == 'c' && p[1] == 'h' && p[2] == 0)
  953. return make_number(x / 2, N_LENGTH);
  954. return make_number(x, N_LENGTH);
  955. }
  956. if (value->type == CSS_KEYWORD)
  957. {
  958. if (!strcmp(value->data, "auto"))
  959. return make_number(0, N_AUTO);
  960. }
  961. return make_number(initial, initial_unit);
  962. }
  963. static fz_css_number
  964. number_from_property(fz_css_match *match, int property, float initial, int initial_unit)
  965. {
  966. return number_from_value(value_from_property(match, property), initial, initial_unit);
  967. }
  968. static fz_css_number
  969. border_width_from_property(fz_css_match *match, int property)
  970. {
  971. fz_css_value *value = value_from_property(match, property);
  972. if (value)
  973. {
  974. if (!strcmp(value->data, "thin"))
  975. return make_number(1, N_LENGTH);
  976. if (!strcmp(value->data, "medium"))
  977. return make_number(2, N_LENGTH);
  978. if (!strcmp(value->data, "thick"))
  979. return make_number(4, N_LENGTH);
  980. return number_from_value(value, 0, N_LENGTH);
  981. }
  982. return make_number(2, N_LENGTH); /* initial: 'medium' */
  983. }
  984. static int
  985. border_style_from_property(fz_css_match *match, int property)
  986. {
  987. fz_css_value *value = value_from_property(match, property);
  988. if (value)
  989. {
  990. if (!strcmp(value->data, "none")) return BS_NONE;
  991. else if (!strcmp(value->data, "hidden")) return BS_NONE;
  992. else if (!strcmp(value->data, "solid")) return BS_SOLID;
  993. }
  994. return BS_NONE;
  995. }
  996. int fz_css_number_defined(fz_css_number number)
  997. {
  998. return number.unit != N_UNDEFINED;
  999. }
  1000. float
  1001. fz_from_css_number(fz_css_number number, float em, float percent_value, float auto_value)
  1002. {
  1003. switch (number.unit) {
  1004. default:
  1005. case N_NUMBER: return number.value;
  1006. case N_LENGTH: return number.value;
  1007. case N_SCALE: return number.value * em;
  1008. case N_PERCENT: return number.value * 0.01f * percent_value;
  1009. case N_AUTO: return auto_value;
  1010. }
  1011. }
  1012. float
  1013. fz_from_css_number_scale(fz_css_number number, float scale)
  1014. {
  1015. switch (number.unit) {
  1016. default:
  1017. case N_NUMBER: return number.value * scale;
  1018. case N_LENGTH: return number.value;
  1019. case N_SCALE: return number.value * scale;
  1020. case N_PERCENT: return number.value * 0.01f * scale;
  1021. case N_AUTO: return scale;
  1022. }
  1023. }
  1024. static fz_css_color
  1025. make_color(int r, int g, int b, int a)
  1026. {
  1027. fz_css_color c;
  1028. c.r = r < 0 ? 0 : r > 255 ? 255 : r;
  1029. c.g = g < 0 ? 0 : g > 255 ? 255 : g;
  1030. c.b = b < 0 ? 0 : b > 255 ? 255 : b;
  1031. c.a = a < 0 ? 0 : a > 255 ? 255 : a;
  1032. return c;
  1033. }
  1034. static int tohex(int c)
  1035. {
  1036. if (c - '0' < 10)
  1037. return c - '0';
  1038. return (c | 32) - 'a' + 10;
  1039. }
  1040. static fz_css_color
  1041. color_from_value(fz_css_value *value, fz_css_color initial)
  1042. {
  1043. if (!value)
  1044. return initial;
  1045. if (value->type == CSS_HASH)
  1046. {
  1047. int r, g, b, a;
  1048. size_t n;
  1049. hex_color:
  1050. n = strlen(value->data);
  1051. if (n == 3)
  1052. {
  1053. r = tohex(value->data[0]) * 16 + tohex(value->data[0]);
  1054. g = tohex(value->data[1]) * 16 + tohex(value->data[1]);
  1055. b = tohex(value->data[2]) * 16 + tohex(value->data[2]);
  1056. a = 255;
  1057. }
  1058. else if (n == 4)
  1059. {
  1060. r = tohex(value->data[0]) * 16 + tohex(value->data[0]);
  1061. g = tohex(value->data[1]) * 16 + tohex(value->data[1]);
  1062. b = tohex(value->data[2]) * 16 + tohex(value->data[2]);
  1063. a = tohex(value->data[3]) * 16 + tohex(value->data[3]);
  1064. }
  1065. else if (n == 6)
  1066. {
  1067. r = tohex(value->data[0]) * 16 + tohex(value->data[1]);
  1068. g = tohex(value->data[2]) * 16 + tohex(value->data[3]);
  1069. b = tohex(value->data[4]) * 16 + tohex(value->data[5]);
  1070. a = 255;
  1071. }
  1072. else if (n == 8)
  1073. {
  1074. r = tohex(value->data[0]) * 16 + tohex(value->data[1]);
  1075. g = tohex(value->data[2]) * 16 + tohex(value->data[3]);
  1076. b = tohex(value->data[4]) * 16 + tohex(value->data[5]);
  1077. a = tohex(value->data[6]) * 16 + tohex(value->data[7]);
  1078. }
  1079. else
  1080. {
  1081. r = g = b = 0;
  1082. a = 255;
  1083. }
  1084. return make_color(r, g, b, a);
  1085. }
  1086. if (value->type == '(' && !strcmp(value->data, "rgb"))
  1087. {
  1088. fz_css_value *vr, *vg, *vb;
  1089. int r, g, b;
  1090. vr = value->args;
  1091. vg = vr && vr->next ? vr->next->next : NULL; /* skip the ',' nodes */
  1092. vb = vg && vg->next ? vg->next->next : NULL; /* skip the ',' nodes */
  1093. r = fz_from_css_number(number_from_value(vr, 0, N_NUMBER), 255, 255, 0);
  1094. g = fz_from_css_number(number_from_value(vg, 0, N_NUMBER), 255, 255, 0);
  1095. b = fz_from_css_number(number_from_value(vb, 0, N_NUMBER), 255, 255, 0);
  1096. return make_color(r, g, b, 255);
  1097. }
  1098. if (value->type == '(' && !strcmp(value->data, "rgba"))
  1099. {
  1100. fz_css_value *vr, *vg, *vb, *va;
  1101. int r, g, b, a;
  1102. vr = value->args;
  1103. vg = vr && vr->next ? vr->next->next : NULL; /* skip the ',' nodes */
  1104. vb = vg && vg->next ? vg->next->next : NULL; /* skip the ',' nodes */
  1105. va = vb && vb->next ? vb->next->next : NULL; /* skip the ',' nodes */
  1106. r = fz_from_css_number(number_from_value(vr, 0, N_NUMBER), 255, 255, 0);
  1107. g = fz_from_css_number(number_from_value(vg, 0, N_NUMBER), 255, 255, 0);
  1108. b = fz_from_css_number(number_from_value(vb, 0, N_NUMBER), 255, 255, 0);
  1109. a = fz_from_css_number(number_from_value(va, 0, N_NUMBER), 255, 255, 255);
  1110. return make_color(r, g, b, a);
  1111. }
  1112. if (value->type == CSS_KEYWORD)
  1113. {
  1114. if (!strcmp(value->data, "transparent"))
  1115. return make_color(0, 0, 0, 0);
  1116. if (!strcmp(value->data, "maroon"))
  1117. return make_color(0x80, 0x00, 0x00, 255);
  1118. if (!strcmp(value->data, "red"))
  1119. return make_color(0xFF, 0x00, 0x00, 255);
  1120. if (!strcmp(value->data, "orange"))
  1121. return make_color(0xFF, 0xA5, 0x00, 255);
  1122. if (!strcmp(value->data, "yellow"))
  1123. return make_color(0xFF, 0xFF, 0x00, 255);
  1124. if (!strcmp(value->data, "olive"))
  1125. return make_color(0x80, 0x80, 0x00, 255);
  1126. if (!strcmp(value->data, "purple"))
  1127. return make_color(0x80, 0x00, 0x80, 255);
  1128. if (!strcmp(value->data, "fuchsia"))
  1129. return make_color(0xFF, 0x00, 0xFF, 255);
  1130. if (!strcmp(value->data, "white"))
  1131. return make_color(0xFF, 0xFF, 0xFF, 255);
  1132. if (!strcmp(value->data, "lime"))
  1133. return make_color(0x00, 0xFF, 0x00, 255);
  1134. if (!strcmp(value->data, "green"))
  1135. return make_color(0x00, 0x80, 0x00, 255);
  1136. if (!strcmp(value->data, "navy"))
  1137. return make_color(0x00, 0x00, 0x80, 255);
  1138. if (!strcmp(value->data, "blue"))
  1139. return make_color(0x00, 0x00, 0xFF, 255);
  1140. if (!strcmp(value->data, "aqua"))
  1141. return make_color(0x00, 0xFF, 0xFF, 255);
  1142. if (!strcmp(value->data, "teal"))
  1143. return make_color(0x00, 0x80, 0x80, 255);
  1144. if (!strcmp(value->data, "black"))
  1145. return make_color(0x00, 0x00, 0x00, 255);
  1146. if (!strcmp(value->data, "silver"))
  1147. return make_color(0xC0, 0xC0, 0xC0, 255);
  1148. if (!strcmp(value->data, "gray"))
  1149. return make_color(0x80, 0x80, 0x80, 255);
  1150. goto hex_color; /* last ditch attempt: maybe it's a #XXXXXX color without the # */
  1151. }
  1152. return initial;
  1153. }
  1154. static fz_css_color
  1155. color_from_property(fz_css_match *match, int property, fz_css_color initial)
  1156. {
  1157. return color_from_value(value_from_property(match, property), initial);
  1158. }
  1159. static fz_css_color
  1160. color_from_properties(fz_css_match *match, int property, int property2, fz_css_color initial)
  1161. {
  1162. fz_css_value *value = value_from_property(match, property);
  1163. if (value == NULL)
  1164. value = value_from_property(match, property2);
  1165. return color_from_value(value, initial);
  1166. }
  1167. int
  1168. fz_get_css_match_display(fz_css_match *match)
  1169. {
  1170. fz_css_value *value = value_from_property(match, PRO_DISPLAY);
  1171. if (value)
  1172. {
  1173. if (!strcmp(value->data, "none"))
  1174. return DIS_NONE;
  1175. if (!strcmp(value->data, "inline"))
  1176. return DIS_INLINE;
  1177. if (!strcmp(value->data, "block"))
  1178. return DIS_BLOCK;
  1179. if (!strcmp(value->data, "list-item"))
  1180. return DIS_LIST_ITEM;
  1181. if (!strcmp(value->data, "inline-block"))
  1182. return DIS_INLINE_BLOCK;
  1183. if (!strcmp(value->data, "table"))
  1184. return DIS_TABLE;
  1185. if (!strcmp(value->data, "table-row"))
  1186. return DIS_TABLE_ROW;
  1187. if (!strcmp(value->data, "table-cell"))
  1188. return DIS_TABLE_CELL;
  1189. if (!strcmp(value->data, "table-row-group"))
  1190. return DIS_TABLE_GROUP;
  1191. if (!strcmp(value->data, "table-header-group"))
  1192. return DIS_TABLE_GROUP;
  1193. if (!strcmp(value->data, "table-footer-group"))
  1194. return DIS_TABLE_GROUP;
  1195. if (!strcmp(value->data, "table-column-group"))
  1196. return DIS_NONE;
  1197. if (!strcmp(value->data, "table-column"))
  1198. return DIS_NONE;
  1199. }
  1200. return DIS_INLINE;
  1201. }
  1202. static int
  1203. white_space_from_property(fz_css_match *match)
  1204. {
  1205. fz_css_value *value = value_from_property(match, PRO_WHITE_SPACE);
  1206. if (value)
  1207. {
  1208. if (!strcmp(value->data, "normal")) return WS_NORMAL;
  1209. else if (!strcmp(value->data, "pre")) return WS_PRE;
  1210. else if (!strcmp(value->data, "nowrap")) return WS_NOWRAP;
  1211. else if (!strcmp(value->data, "pre-wrap")) return WS_PRE_WRAP;
  1212. else if (!strcmp(value->data, "pre-line")) return WS_PRE_LINE;
  1213. }
  1214. return WS_NORMAL;
  1215. }
  1216. static int
  1217. text_decoration_from_property(fz_css_match *match)
  1218. {
  1219. fz_css_value *value = value_from_property(match, PRO_TEXT_DECORATION);
  1220. if (value)
  1221. {
  1222. if (!strcmp(value->data, "underline")) return TD_UNDERLINE;
  1223. if (!strcmp(value->data, "line-through")) return TD_LINE_THROUGH;
  1224. }
  1225. return TD_NONE;
  1226. }
  1227. static int
  1228. visibility_from_property(fz_css_match *match)
  1229. {
  1230. fz_css_value *value = value_from_property(match, PRO_VISIBILITY);
  1231. if (value)
  1232. {
  1233. if (!strcmp(value->data, "visible")) return V_VISIBLE;
  1234. else if (!strcmp(value->data, "hidden")) return V_HIDDEN;
  1235. else if (!strcmp(value->data, "collapse")) return V_COLLAPSE;
  1236. }
  1237. return V_VISIBLE;
  1238. }
  1239. static int
  1240. page_break_from_property(fz_css_match *match, int prop)
  1241. {
  1242. fz_css_value *value = value_from_property(match, prop);
  1243. if (value)
  1244. {
  1245. if (!strcmp(value->data, "auto")) return PB_AUTO;
  1246. else if (!strcmp(value->data, "always")) return PB_ALWAYS;
  1247. else if (!strcmp(value->data, "avoid")) return PB_AVOID;
  1248. else if (!strcmp(value->data, "left")) return PB_LEFT;
  1249. else if (!strcmp(value->data, "right")) return PB_RIGHT;
  1250. }
  1251. return PB_AUTO;
  1252. }
  1253. void
  1254. fz_default_css_style(fz_context *ctx, fz_css_style *style)
  1255. {
  1256. memset(style, 0, sizeof *style);
  1257. style->visibility = V_VISIBLE;
  1258. style->text_align = TA_LEFT;
  1259. style->vertical_align = VA_BASELINE;
  1260. style->white_space = WS_NORMAL;
  1261. style->list_style_type = LST_DISC;
  1262. style->font_size = make_number(1, N_SCALE);
  1263. style->width = make_number(0, N_AUTO);
  1264. style->height = make_number(0, N_AUTO);
  1265. style->leading = make_undefined_number();
  1266. }
  1267. void
  1268. fz_apply_css_style(fz_context *ctx, fz_html_font_set *set, fz_css_style *style, fz_css_match *match)
  1269. {
  1270. fz_css_value *value;
  1271. fz_css_color black = { 0, 0, 0, 255 };
  1272. fz_css_color transparent = { 0, 0, 0, 0 };
  1273. fz_default_css_style(ctx, style);
  1274. style->visibility = visibility_from_property(match);
  1275. style->white_space = white_space_from_property(match);
  1276. style->text_decoration = text_decoration_from_property(match);
  1277. style->page_break_before = page_break_from_property(match, PRO_PAGE_BREAK_BEFORE);
  1278. style->page_break_after = page_break_from_property(match, PRO_PAGE_BREAK_AFTER);
  1279. value = value_from_property(match, PRO_TEXT_ALIGN);
  1280. if (value)
  1281. {
  1282. if (!strcmp(value->data, "left")) style->text_align = TA_LEFT;
  1283. else if (!strcmp(value->data, "right")) style->text_align = TA_RIGHT;
  1284. else if (!strcmp(value->data, "center")) style->text_align = TA_CENTER;
  1285. else if (!strcmp(value->data, "justify")) style->text_align = TA_JUSTIFY;
  1286. }
  1287. value = value_from_property(match, PRO_VERTICAL_ALIGN);
  1288. if (value)
  1289. {
  1290. if (!strcmp(value->data, "baseline")) style->vertical_align = VA_BASELINE;
  1291. else if (!strcmp(value->data, "sub")) style->vertical_align = VA_SUB;
  1292. else if (!strcmp(value->data, "super")) style->vertical_align = VA_SUPER;
  1293. else if (!strcmp(value->data, "top")) style->vertical_align = VA_TOP;
  1294. else if (!strcmp(value->data, "bottom")) style->vertical_align = VA_BOTTOM;
  1295. else if (!strcmp(value->data, "text-top")) style->vertical_align = VA_TEXT_TOP;
  1296. else if (!strcmp(value->data, "text-bottom")) style->vertical_align = VA_TEXT_BOTTOM;
  1297. }
  1298. value = value_from_property(match, PRO_FONT_SIZE);
  1299. if (value)
  1300. {
  1301. /* absolute-size */
  1302. if (!strcmp(value->data, "xx-large")) style->font_size = make_number(1.73f, N_SCALE);
  1303. else if (!strcmp(value->data, "x-large")) style->font_size = make_number(1.44f, N_SCALE);
  1304. else if (!strcmp(value->data, "large")) style->font_size = make_number(1.2f, N_SCALE);
  1305. else if (!strcmp(value->data, "medium")) style->font_size = make_number(1.0f, N_SCALE);
  1306. else if (!strcmp(value->data, "small")) style->font_size = make_number(0.83f, N_SCALE);
  1307. else if (!strcmp(value->data, "x-small")) style->font_size = make_number(0.69f, N_SCALE);
  1308. else if (!strcmp(value->data, "xx-small")) style->font_size = make_number(0.69f, N_SCALE);
  1309. /* relative-size */
  1310. else if (!strcmp(value->data, "larger")) style->font_size = make_number(1.2f, N_SCALE);
  1311. else if (!strcmp(value->data, "smaller")) style->font_size = make_number(1/1.2f, N_SCALE);
  1312. /* percentage */
  1313. else if (value->type == CSS_PERCENT) style->font_size = number_from_value(value, 12, N_LENGTH);
  1314. /* length */
  1315. else if (value->type == CSS_LENGTH) style->font_size = number_from_value(value, 12, N_LENGTH);
  1316. /* default to 1em */
  1317. else style->font_size = make_number(1, N_SCALE);
  1318. }
  1319. else
  1320. {
  1321. style->font_size = make_number(1, N_SCALE);
  1322. }
  1323. value = value_from_property(match, PRO_LIST_STYLE_TYPE);
  1324. if (value)
  1325. {
  1326. if (!strcmp(value->data, "none")) style->list_style_type = LST_NONE;
  1327. else if (!strcmp(value->data, "disc")) style->list_style_type = LST_DISC;
  1328. else if (!strcmp(value->data, "circle")) style->list_style_type = LST_CIRCLE;
  1329. else if (!strcmp(value->data, "square")) style->list_style_type = LST_SQUARE;
  1330. else if (!strcmp(value->data, "decimal")) style->list_style_type = LST_DECIMAL;
  1331. else if (!strcmp(value->data, "decimal-leading-zero")) style->list_style_type = LST_DECIMAL_ZERO;
  1332. else if (!strcmp(value->data, "lower-roman")) style->list_style_type = LST_LC_ROMAN;
  1333. else if (!strcmp(value->data, "upper-roman")) style->list_style_type = LST_UC_ROMAN;
  1334. else if (!strcmp(value->data, "lower-greek")) style->list_style_type = LST_LC_GREEK;
  1335. else if (!strcmp(value->data, "upper-greek")) style->list_style_type = LST_UC_GREEK;
  1336. else if (!strcmp(value->data, "lower-latin")) style->list_style_type = LST_LC_LATIN;
  1337. else if (!strcmp(value->data, "upper-latin")) style->list_style_type = LST_UC_LATIN;
  1338. else if (!strcmp(value->data, "lower-alpha")) style->list_style_type = LST_LC_ALPHA;
  1339. else if (!strcmp(value->data, "upper-alpha")) style->list_style_type = LST_UC_ALPHA;
  1340. else if (!strcmp(value->data, "armenian")) style->list_style_type = LST_ARMENIAN;
  1341. else if (!strcmp(value->data, "georgian")) style->list_style_type = LST_GEORGIAN;
  1342. }
  1343. value = value_from_property(match, PRO_OVERFLOW_WRAP);
  1344. if (value)
  1345. {
  1346. if (!strcmp(value->data, "break-word")) style->overflow_wrap = OVERFLOW_WRAP_BREAK_WORD;
  1347. else style->overflow_wrap = OVERFLOW_WRAP_NORMAL;
  1348. }
  1349. style->line_height = number_from_property(match, PRO_LINE_HEIGHT, 1.2f, N_SCALE);
  1350. style->leading = number_from_property(match, PRO_LEADING, 0, N_UNDEFINED);
  1351. style->text_indent = number_from_property(match, PRO_TEXT_INDENT, 0, N_LENGTH);
  1352. style->text_stroke_width = number_from_property(match, PRO_TEXT_STROKE_WIDTH, 0, N_LENGTH);
  1353. style->width = number_from_property(match, PRO_WIDTH, 0, N_AUTO);
  1354. style->height = number_from_property(match, PRO_HEIGHT, 0, N_AUTO);
  1355. style->margin[0] = number_from_property(match, PRO_MARGIN_TOP, 0, N_LENGTH);
  1356. style->margin[1] = number_from_property(match, PRO_MARGIN_RIGHT, 0, N_LENGTH);
  1357. style->margin[2] = number_from_property(match, PRO_MARGIN_BOTTOM, 0, N_LENGTH);
  1358. style->margin[3] = number_from_property(match, PRO_MARGIN_LEFT, 0, N_LENGTH);
  1359. style->padding[0] = number_from_property(match, PRO_PADDING_TOP, 0, N_LENGTH);
  1360. style->padding[1] = number_from_property(match, PRO_PADDING_RIGHT, 0, N_LENGTH);
  1361. style->padding[2] = number_from_property(match, PRO_PADDING_BOTTOM, 0, N_LENGTH);
  1362. style->padding[3] = number_from_property(match, PRO_PADDING_LEFT, 0, N_LENGTH);
  1363. style->color = color_from_property(match, PRO_COLOR, black);
  1364. style->text_fill_color = color_from_properties(match, PRO_TEXT_FILL_COLOR, PRO_COLOR, black);
  1365. style->text_stroke_color = color_from_property(match, PRO_TEXT_STROKE_COLOR, transparent);
  1366. style->background_color = color_from_property(match, PRO_BACKGROUND_COLOR, transparent);
  1367. style->border_spacing = number_from_property(match, PRO_BORDER_SPACING, 0, N_LENGTH);
  1368. style->border_style_0 = border_style_from_property(match, PRO_BORDER_TOP_STYLE);
  1369. style->border_style_1 = border_style_from_property(match, PRO_BORDER_RIGHT_STYLE);
  1370. style->border_style_2 = border_style_from_property(match, PRO_BORDER_BOTTOM_STYLE);
  1371. style->border_style_3 = border_style_from_property(match, PRO_BORDER_LEFT_STYLE);
  1372. style->border_color[0] = color_from_property(match, PRO_BORDER_TOP_COLOR, style->color);
  1373. style->border_color[1] = color_from_property(match, PRO_BORDER_RIGHT_COLOR, style->color);
  1374. style->border_color[2] = color_from_property(match, PRO_BORDER_BOTTOM_COLOR, style->color);
  1375. style->border_color[3] = color_from_property(match, PRO_BORDER_LEFT_COLOR, style->color);
  1376. style->border_width[0] = border_width_from_property(match, PRO_BORDER_TOP_WIDTH);
  1377. style->border_width[1] = border_width_from_property(match, PRO_BORDER_RIGHT_WIDTH);
  1378. style->border_width[2] = border_width_from_property(match, PRO_BORDER_BOTTOM_WIDTH);
  1379. style->border_width[3] = border_width_from_property(match, PRO_BORDER_LEFT_WIDTH);
  1380. {
  1381. const char *font_weight = string_from_property(match, PRO_FONT_WEIGHT, "normal");
  1382. const char *font_style = string_from_property(match, PRO_FONT_STYLE, "normal");
  1383. const char *font_variant = string_from_property(match, PRO_FONT_VARIANT, "normal");
  1384. int is_bold = is_bold_from_font_weight(font_weight);
  1385. int is_italic = is_italic_from_font_style(font_style);
  1386. style->small_caps = !strcmp(font_variant, "small-caps");
  1387. value = value_from_property(match, PRO_FONT_FAMILY);
  1388. for (; value; value = value->next)
  1389. {
  1390. /* ignore numbers and keywords used in font short-hand syntax */
  1391. if (value->type == CSS_STRING || (value->type == CSS_KEYWORD && !keyword_in_list(value->data, font_family_ignore, nelem(font_family_ignore))))
  1392. {
  1393. style->font = fz_load_html_font(ctx, set, value->data, is_bold, is_italic, style->small_caps);
  1394. if (style->font)
  1395. break;
  1396. }
  1397. }
  1398. if (!style->font)
  1399. style->font = fz_load_html_font(ctx, set, "serif", is_bold, is_italic, style->small_caps);
  1400. }
  1401. }
  1402. #ifdef DEBUG_CSS_SPLAY
  1403. static void
  1404. do_verify_splay(const fz_css_style_splay *x)
  1405. {
  1406. printf("%x<", x);
  1407. if (x->lt)
  1408. {
  1409. assert(memcmp(&x->lt->style, &x->style, sizeof(x->style)) < 0);
  1410. assert(x->lt->up == x);
  1411. do_verify_splay(x->lt);
  1412. }
  1413. printf(",");
  1414. if (x->gt)
  1415. {
  1416. assert(memcmp(&x->gt->style, &x->style, sizeof(x->style)) > 0);
  1417. assert(x->gt->up == x);
  1418. do_verify_splay(x->gt);
  1419. }
  1420. printf(">\n");
  1421. }
  1422. static void
  1423. verify_splay(const fz_css_style_splay *x)
  1424. {
  1425. if (x == NULL)
  1426. return;
  1427. assert(x->up == NULL);
  1428. do_verify_splay(x);
  1429. printf("-----\n");
  1430. }
  1431. #endif
  1432. const fz_css_style *
  1433. fz_css_enlist(fz_context *ctx, const fz_css_style *style, fz_css_style_splay **tree, fz_pool *pool)
  1434. {
  1435. fz_css_style_splay **current = tree;
  1436. fz_css_style_splay *x;
  1437. fz_css_style_splay *y = NULL;
  1438. /* Search for a match in the tree, if there is one, or for
  1439. * the insertion point, if there is not. */
  1440. while (*current != NULL)
  1441. {
  1442. int cmp = memcmp(style, &(*current)->style, sizeof(*style));
  1443. if (cmp == 0)
  1444. {
  1445. /* We have a match - break out and do move to root. */
  1446. break;
  1447. }
  1448. y = (*current);
  1449. if (cmp < 0)
  1450. current = &y->lt;
  1451. else
  1452. current = &y->gt;
  1453. }
  1454. /* Create one if needed */
  1455. if (*current == NULL)
  1456. {
  1457. x = *current = fz_pool_alloc(ctx, pool, sizeof(*y));
  1458. x->style = *style;
  1459. x->up = y;
  1460. x->lt = NULL;
  1461. x->gt = NULL;
  1462. }
  1463. else
  1464. x = *current;
  1465. /* Now move to root */
  1466. /*
  1467. The splaying steps used:
  1468. Case 1: |a) z x b) z x
  1469. | y D => A y A y y D
  1470. | x C B z B x => z C
  1471. | A B C D C D A B
  1472. Case 2: |a) z x b) z x
  1473. | y D => y z A y => z y
  1474. | A x A B C D x D A B C D
  1475. | B C B C
  1476. Case 3: |a) y x b) y x
  1477. | x C => A y A x => y C
  1478. | A B B C B C A B
  1479. */
  1480. #ifdef DEBUG_CSS_SPLAY
  1481. printf("BEFORE\n");
  1482. verify_splay(*tree);
  1483. #endif
  1484. while ((y = x->up) != NULL ) /* While we're not at the root */
  1485. {
  1486. fz_css_style_splay *z = y->up;
  1487. y->up = x;
  1488. if (z == NULL)
  1489. {
  1490. if (y->lt == x) /* Case 3a */
  1491. {
  1492. y->lt = x->gt;
  1493. if (y->lt)
  1494. y->lt->up = y;
  1495. x->gt = y;
  1496. }
  1497. else /* Case 3b */
  1498. {
  1499. y->gt = x->lt;
  1500. if (y->gt)
  1501. y->gt->up = y;
  1502. x->lt = y;
  1503. }
  1504. x->up = NULL;
  1505. break;
  1506. }
  1507. x->up = z->up;
  1508. if (z->up)
  1509. {
  1510. if (z->up->lt == z)
  1511. z->up->lt = x;
  1512. else
  1513. z->up->gt = x;
  1514. }
  1515. if (z->lt == y)
  1516. {
  1517. if (y->lt == x) /* Case 1a */
  1518. {
  1519. z->lt = y->gt;
  1520. if (z->lt)
  1521. z->lt->up = z;
  1522. y->lt = x->gt;
  1523. if (y->lt)
  1524. y->lt->up = y;
  1525. y->gt = z;
  1526. z->up = y;
  1527. x->gt = y;
  1528. }
  1529. else /* Case 2a */
  1530. {
  1531. y->gt = x->lt;
  1532. if (y->gt)
  1533. y->gt->up = y;
  1534. z->lt = x->gt;
  1535. if (z->lt)
  1536. z->lt->up = z;
  1537. x->lt = y;
  1538. x->gt = z;
  1539. z->up = x;
  1540. }
  1541. }
  1542. else
  1543. {
  1544. if (y->gt == x) /* Case 1b */
  1545. {
  1546. z->gt = y->lt;
  1547. if (z->gt)
  1548. z->gt->up = z;
  1549. y->gt = x->lt;
  1550. if (y->gt)
  1551. y->gt->up = y;
  1552. y->lt = z;
  1553. z->up = y;
  1554. x->lt = y;
  1555. }
  1556. else /* Case 2b */
  1557. {
  1558. z->gt = x->lt;
  1559. if (z->gt)
  1560. z->gt->up = z;
  1561. y->lt = x->gt;
  1562. if (y->lt)
  1563. y->lt->up = y;
  1564. x->gt = y;
  1565. x->lt = z;
  1566. z->up = x;
  1567. }
  1568. }
  1569. }
  1570. *tree = x;
  1571. #ifdef DEBUG_CSS_SPLAY
  1572. printf("AFTER\n");
  1573. verify_splay(x);
  1574. #endif
  1575. return &x->style;
  1576. }
  1577. /*
  1578. * Pretty printing
  1579. */
  1580. static void print_value(fz_css_value *val)
  1581. {
  1582. printf("%s", val->data);
  1583. if (val->args)
  1584. {
  1585. printf("(");
  1586. print_value(val->args);
  1587. printf(")");
  1588. }
  1589. if (val->next)
  1590. {
  1591. printf(" ");
  1592. print_value(val->next);
  1593. }
  1594. }
  1595. static void print_property(fz_css_property *prop)
  1596. {
  1597. printf("\t%s: ", fz_css_property_name(prop->name));
  1598. print_value(prop->value);
  1599. if (prop->important)
  1600. printf(" !important");
  1601. printf(";\n");
  1602. }
  1603. static void print_condition(fz_css_condition *cond)
  1604. {
  1605. if (cond->type == '=')
  1606. printf("[%s=%s]", cond->key, cond->val);
  1607. else if (cond->type == '[')
  1608. printf("[%s]", cond->key);
  1609. else
  1610. printf("%c%s", cond->type, cond->val);
  1611. if (cond->next)
  1612. print_condition(cond->next);
  1613. }
  1614. static void print_selector(fz_css_selector *sel)
  1615. {
  1616. if (sel->combine)
  1617. {
  1618. print_selector(sel->left);
  1619. if (sel->combine == ' ')
  1620. printf(" ");
  1621. else
  1622. printf(" %c ", sel->combine);
  1623. print_selector(sel->right);
  1624. }
  1625. else if (sel->name)
  1626. printf("%s", sel->name);
  1627. else
  1628. printf("*");
  1629. if (sel->cond)
  1630. {
  1631. print_condition(sel->cond);
  1632. }
  1633. }
  1634. static void print_rule(fz_css_rule *rule)
  1635. {
  1636. fz_css_selector *sel;
  1637. fz_css_property *prop;
  1638. for (sel = rule->selector; sel; sel = sel->next)
  1639. {
  1640. print_selector(sel);
  1641. printf(" /* %d */", selector_specificity(sel, 0));
  1642. if (sel->next)
  1643. printf(", ");
  1644. }
  1645. printf("\n{\n");
  1646. for (prop = rule->declaration; prop; prop = prop->next)
  1647. {
  1648. print_property(prop);
  1649. }
  1650. printf("}\n");
  1651. }
  1652. void
  1653. fz_debug_css(fz_context *ctx, fz_css *css)
  1654. {
  1655. fz_css_rule *rule = css->rule;
  1656. while (rule)
  1657. {
  1658. print_rule(rule);
  1659. rule = rule->next;
  1660. }
  1661. }