PageRenderTime 27ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/application/third_party/vendor/mpdf/mpdf/src/DirectWrite.php

https://gitlab.com/devdoblea/factutextil.local
PHP | 515 lines | 413 code | 51 blank | 51 comment | 120 complexity | 7bbb62ed4d2c91b9393f4807eee58551 MD5 | raw file
  1. <?php
  2. namespace Mpdf;
  3. use Mpdf\Color\ColorConverter;
  4. use Mpdf\Css\TextVars;
  5. class DirectWrite
  6. {
  7. /**
  8. * @var \Mpdf\Mpdf
  9. */
  10. private $mpdf;
  11. /**
  12. * @var \Mpdf\Otl
  13. */
  14. private $otl;
  15. /**
  16. * @var \Mpdf\SizeConverter
  17. */
  18. private $sizeConverter;
  19. /**
  20. * @var \Mpdf\Color\ColorConverter
  21. */
  22. private $colorConverter;
  23. public function __construct(Mpdf $mpdf, Otl $otl, SizeConverter $sizeConverter, ColorConverter $colorConverter)
  24. {
  25. $this->mpdf = $mpdf;
  26. $this->otl = $otl;
  27. $this->sizeConverter = $sizeConverter;
  28. $this->colorConverter = $colorConverter;
  29. }
  30. function Write($h, $txt, $currentx = 0, $link = '', $directionality = 'ltr', $align = '', $fill = 0)
  31. {
  32. if (!$align) {
  33. if ($directionality === 'rtl') {
  34. $align = 'R';
  35. } else {
  36. $align = 'L';
  37. }
  38. }
  39. if ($h == 0) {
  40. $this->mpdf->SetLineHeight();
  41. $h = $this->mpdf->lineheight;
  42. }
  43. //Output text in flowing mode
  44. $w = $this->mpdf->w - $this->mpdf->rMargin - $this->mpdf->x;
  45. $wmax = ($w - ($this->mpdf->cMarginL + $this->mpdf->cMarginR));
  46. $s = str_replace("\r", '', $txt);
  47. if ($this->mpdf->usingCoreFont) {
  48. $nb = strlen($s);
  49. } else {
  50. $nb = mb_strlen($s, $this->mpdf->mb_enc);
  51. // handle single space character
  52. if (($nb === 1) && $s === ' ') {
  53. $this->mpdf->x += $this->mpdf->GetStringWidth($s);
  54. return;
  55. }
  56. }
  57. $sep = -1;
  58. $i = 0;
  59. $j = 0;
  60. $l = 0;
  61. $nl = 1;
  62. if (!$this->mpdf->usingCoreFont) {
  63. if (preg_match('/([' . $this->mpdf->pregRTLchars . '])/u', $txt)) {
  64. $this->mpdf->biDirectional = true;
  65. } // *RTL*
  66. while ($i < $nb) {
  67. //Get next character
  68. $c = mb_substr($s, $i, 1, $this->mpdf->mb_enc);
  69. if ($c === "\n") {
  70. // WORD SPACING
  71. $this->mpdf->ResetSpacing();
  72. //Explicit line break
  73. $tmp = rtrim(mb_substr($s, $j, $i - $j, $this->mpdf->mb_enc));
  74. $this->mpdf->Cell($w, $h, $tmp, 0, 2, $align, $fill, $link);
  75. $i++;
  76. $sep = -1;
  77. $j = $i;
  78. $l = 0;
  79. if ($nl === 1) {
  80. if ($currentx != 0) {
  81. $this->mpdf->x = $currentx;
  82. } else {
  83. $this->mpdf->x = $this->mpdf->lMargin;
  84. }
  85. $w = $this->mpdf->w - $this->mpdf->rMargin - $this->mpdf->x;
  86. $wmax = ($w - ($this->mpdf->cMarginL + $this->mpdf->cMarginR));
  87. }
  88. $nl++;
  89. continue;
  90. }
  91. if ($c === ' ') {
  92. $sep = $i;
  93. }
  94. $l += $this->mpdf->GetCharWidthNonCore($c); // mPDF 5.3.04
  95. if ($l > $wmax) {
  96. //Automatic line break (word wrapping)
  97. if ($sep == -1) {
  98. // WORD SPACING
  99. $this->mpdf->ResetSpacing();
  100. if ($this->mpdf->x > $this->mpdf->lMargin) {
  101. //Move to next line
  102. if ($currentx != 0) {
  103. $this->mpdf->x = $currentx;
  104. } else {
  105. $this->mpdf->x = $this->mpdf->lMargin;
  106. }
  107. $this->mpdf->y+=$h;
  108. $w = $this->mpdf->w - $this->mpdf->rMargin - $this->mpdf->x;
  109. $wmax = ($w - ($this->mpdf->cMarginL + $this->mpdf->cMarginR));
  110. $i++;
  111. $nl++;
  112. continue;
  113. }
  114. if ($i == $j) {
  115. $i++;
  116. }
  117. $tmp = rtrim(mb_substr($s, $j, $i - $j, $this->mpdf->mb_enc));
  118. $this->mpdf->Cell($w, $h, $tmp, 0, 2, $align, $fill, $link);
  119. } else {
  120. $tmp = rtrim(mb_substr($s, $j, $sep - $j, $this->mpdf->mb_enc));
  121. if ($align === 'J') {
  122. //////////////////////////////////////////
  123. // JUSTIFY J using Unicode fonts (Word spacing doesn't work)
  124. // WORD SPACING
  125. // Change NON_BREAKING SPACE to spaces so they are 'spaced' properly
  126. $tmp = str_replace(chr(194) . chr(160), chr(32), $tmp);
  127. $len_ligne = $this->mpdf->GetStringWidth($tmp);
  128. $nb_carac = mb_strlen($tmp, $this->mpdf->mb_enc);
  129. $nb_spaces = mb_substr_count($tmp, ' ', $this->mpdf->mb_enc);
  130. $inclCursive = false;
  131. if (!empty($this->mpdf->CurrentFont['useOTL']) && preg_match('/([' . $this->mpdf->pregCURSchars . '])/u', $tmp)) {
  132. $inclCursive = true;
  133. }
  134. list($charspacing, $ws) = $this->mpdf->GetJspacing($nb_carac, $nb_spaces, (($w - 2) - $len_ligne) * Mpdf::SCALE, $inclCursive);
  135. $this->mpdf->SetSpacing($charspacing, $ws);
  136. //////////////////////////////////////////
  137. }
  138. $this->mpdf->Cell($w, $h, $tmp, 0, 2, $align, $fill, $link);
  139. $i = $sep + 1;
  140. }
  141. $sep = -1;
  142. $j = $i;
  143. $l = 0;
  144. if ($nl === 1) {
  145. if ($currentx != 0) {
  146. $this->mpdf->x = $currentx;
  147. } else {
  148. $this->mpdf->x = $this->mpdf->lMargin;
  149. }
  150. $w = $this->mpdf->w - $this->mpdf->rMargin - $this->mpdf->x;
  151. $wmax = ($w - ($this->mpdf->cMarginL + $this->mpdf->cMarginR));
  152. }
  153. $nl++;
  154. } else {
  155. $i++;
  156. }
  157. }
  158. //Last chunk
  159. // WORD SPACING
  160. $this->mpdf->ResetSpacing();
  161. } else {
  162. while ($i < $nb) {
  163. //Get next character
  164. $c = $s[$i];
  165. if ($c === "\n") {
  166. //Explicit line break
  167. // WORD SPACING
  168. $this->mpdf->ResetSpacing();
  169. $this->mpdf->Cell($w, $h, substr($s, $j, $i - $j), 0, 2, $align, $fill, $link);
  170. $i++;
  171. $sep = -1;
  172. $j = $i;
  173. $l = 0;
  174. if ($nl === 1) {
  175. if ($currentx != 0) {
  176. $this->mpdf->x = $currentx;
  177. } else {
  178. $this->mpdf->x = $this->mpdf->lMargin;
  179. }
  180. $w = $this->mpdf->w - $this->mpdf->rMargin - $this->mpdf->x;
  181. $wmax = $w - ($this->mpdf->cMarginL + $this->mpdf->cMarginR);
  182. }
  183. $nl++;
  184. continue;
  185. }
  186. if ($c === ' ') {
  187. $sep = $i;
  188. }
  189. $l += $this->mpdf->GetCharWidthCore($c); // mPDF 5.3.04
  190. if ($l > $wmax) {
  191. //Automatic line break (word wrapping)
  192. if ($sep == -1) {
  193. // WORD SPACING
  194. $this->mpdf->ResetSpacing();
  195. if ($this->mpdf->x > $this->mpdf->lMargin) {
  196. //Move to next line
  197. if ($currentx != 0) {
  198. $this->mpdf->x = $currentx;
  199. } else {
  200. $this->mpdf->x = $this->mpdf->lMargin;
  201. }
  202. $this->mpdf->y+=$h;
  203. $w = $this->mpdf->w - $this->mpdf->rMargin - $this->mpdf->x;
  204. $wmax = $w - ($this->mpdf->cMarginL + $this->mpdf->cMarginR);
  205. $i++;
  206. $nl++;
  207. continue;
  208. }
  209. if ($i == $j) {
  210. $i++;
  211. }
  212. $this->mpdf->Cell($w, $h, substr($s, $j, $i - $j), 0, 2, $align, $fill, $link);
  213. } else {
  214. $tmp = substr($s, $j, $sep - $j);
  215. if ($align === 'J') {
  216. //////////////////////////////////////////
  217. // JUSTIFY J using Unicode fonts
  218. // WORD SPACING is not fully supported for complex scripts
  219. // Change NON_BREAKING SPACE to spaces so they are 'spaced' properly
  220. $tmp = str_replace(chr(160), chr(32), $tmp);
  221. $len_ligne = $this->mpdf->GetStringWidth($tmp);
  222. $nb_carac = strlen($tmp);
  223. $nb_spaces = substr_count($tmp, ' ');
  224. list($charspacing, $ws) = $this->mpdf->GetJspacing($nb_carac, $nb_spaces, (($w - 2) - $len_ligne) * Mpdf::SCALE, $false);
  225. $this->mpdf->SetSpacing($charspacing, $ws);
  226. //////////////////////////////////////////
  227. }
  228. $this->mpdf->Cell($w, $h, $tmp, 0, 2, $align, $fill, $link);
  229. $i = $sep + 1;
  230. }
  231. $sep = -1;
  232. $j = $i;
  233. $l = 0;
  234. if ($nl === 1) {
  235. if ($currentx != 0) {
  236. $this->mpdf->x = $currentx;
  237. } else {
  238. $this->mpdf->x = $this->mpdf->lMargin;
  239. }
  240. $w = $this->mpdf->w - $this->mpdf->rMargin - $this->mpdf->x;
  241. $wmax = $w - ($this->mpdf->cMarginL + $this->mpdf->cMarginR);
  242. }
  243. $nl++;
  244. } else {
  245. $i++;
  246. }
  247. }
  248. // WORD SPACING
  249. $this->mpdf->ResetSpacing();
  250. }
  251. //Last chunk
  252. if ($i != $j) {
  253. if ($currentx != 0) {
  254. $this->mpdf->x = $currentx;
  255. } else {
  256. $this->mpdf->x = $this->mpdf->lMargin;
  257. }
  258. if ($this->mpdf->usingCoreFont) {
  259. $tmp = substr($s, $j, $i - $j);
  260. } else {
  261. $tmp = mb_substr($s, $j, $i - $j, $this->mpdf->mb_enc);
  262. }
  263. $this->mpdf->Cell($w, $h, $tmp, 0, 0, $align, $fill, $link);
  264. }
  265. }
  266. function CircularText($x, $y, $r, $text, $align = 'top', $fontfamily = '', $fontsizePt = 0, $fontstyle = '', $kerning = 120, $fontwidth = 100, $divider = '')
  267. {
  268. if ($fontfamily || $fontstyle || $fontsizePt) {
  269. $this->mpdf->SetFont($fontfamily, $fontstyle, $fontsizePt);
  270. }
  271. $kerning /= 100;
  272. $fontwidth /= 100;
  273. if ($kerning == 0) {
  274. throw new \Mpdf\MpdfException('Please use values unequal to zero for kerning (CircularText)');
  275. }
  276. if ($fontwidth == 0) {
  277. throw new \Mpdf\MpdfException('Please use values unequal to zero for font width (CircularText)');
  278. }
  279. $text = str_replace("\r", '', $text);
  280. // circumference
  281. $u = ($r * 2) * M_PI;
  282. $checking = true;
  283. $autoset = false;
  284. while ($checking) {
  285. $t = 0;
  286. $w = [];
  287. if ($this->mpdf->usingCoreFont) {
  288. $nb = strlen($text);
  289. for ($i = 0; $i < $nb; $i++) {
  290. $w[$i] = $this->mpdf->GetStringWidth($text[$i]);
  291. $w[$i]*=$kerning * $fontwidth;
  292. $t+=$w[$i];
  293. }
  294. } else {
  295. $nb = mb_strlen($text, $this->mpdf->mb_enc);
  296. $lastchar = '';
  297. $unicode = $this->mpdf->UTF8StringToArray($text);
  298. for ($i = 0; $i < $nb; $i++) {
  299. $c = mb_substr($text, $i, 1, $this->mpdf->mb_enc);
  300. $w[$i] = $this->mpdf->GetStringWidth($c);
  301. $w[$i]*=$kerning * $fontwidth;
  302. $char = $unicode[$i];
  303. if ($this->mpdf->useKerning && $lastchar && isset($this->mpdf->CurrentFont['kerninfo'][$lastchar][$char])) {
  304. $tk = $this->mpdf->CurrentFont['kerninfo'][$lastchar][$char] * ($this->mpdf->FontSize / 1000) * $kerning * $fontwidth;
  305. $w[$i] += $tk / 2;
  306. $w[$i - 1] += $tk / 2;
  307. $t+=$tk;
  308. }
  309. $lastchar = $char;
  310. $t+=$w[$i];
  311. }
  312. }
  313. if ($fontsizePt >= 0 || $autoset) {
  314. $checking = false;
  315. } else {
  316. $t+=$this->mpdf->GetStringWidth(' ');
  317. if ($divider) {
  318. $t+=$this->mpdf->GetStringWidth(' ');
  319. }
  320. if ($fontsizePt == -2) {
  321. $fontsizePt = $this->mpdf->FontSizePt * 0.5 * $u / $t;
  322. } else {
  323. $fontsizePt = $this->mpdf->FontSizePt * $u / $t;
  324. }
  325. $this->mpdf->SetFontSize($fontsizePt);
  326. $autoset = true;
  327. }
  328. }
  329. // total width of string in degrees
  330. $d = ($t / $u) * 360;
  331. $this->mpdf->StartTransform();
  332. // rotate matrix for the first letter to center the text
  333. // (half of total degrees)
  334. if ($align === 'top') {
  335. $this->mpdf->transformRotate(-$d / 2, $x, $y);
  336. } else {
  337. $this->mpdf->transformRotate($d / 2, $x, $y);
  338. }
  339. // run through the string
  340. for ($i = 0; $i < $nb; $i++) {
  341. if ($align === 'top') {
  342. // rotate matrix half of the width of current letter + half of the width of preceding letter
  343. if ($i === 0) {
  344. $this->mpdf->transformRotate((($w[$i] / 2) / $u) * 360, $x, $y);
  345. } else {
  346. $this->mpdf->transformRotate((($w[$i] / 2 + $w[$i - 1] / 2) / $u) * 360, $x, $y);
  347. }
  348. if ($fontwidth !== 1) {
  349. $this->mpdf->StartTransform();
  350. $this->mpdf->transformScale($fontwidth * 100, 100, $x, $y);
  351. }
  352. $this->mpdf->SetXY($x - $w[$i] / 2, $y - $r);
  353. } else {
  354. // rotate matrix half of the width of current letter + half of the width of preceding letter
  355. if ($i === 0) {
  356. $this->mpdf->transformRotate(-(($w[$i] / 2) / $u) * 360, $x, $y);
  357. } else {
  358. $this->mpdf->transformRotate(-(($w[$i] / 2 + $w[$i - 1] / 2) / $u) * 360, $x, $y);
  359. }
  360. if ($fontwidth !== 1) {
  361. $this->mpdf->StartTransform();
  362. $this->mpdf->transformScale($fontwidth * 100, 100, $x, $y);
  363. }
  364. $this->mpdf->SetXY($x - $w[$i] / 2, $y + $r - $this->mpdf->FontSize);
  365. }
  366. if ($this->mpdf->usingCoreFont) {
  367. $c = $text[$i];
  368. } else {
  369. $c = mb_substr($text, $i, 1, $this->mpdf->mb_enc);
  370. }
  371. $this->mpdf->Cell($w[$i], $this->mpdf->FontSize, $c, 0, 0, 'C'); // mPDF 5.3.53
  372. if ($fontwidth !== 1) {
  373. $this->mpdf->StopTransform();
  374. }
  375. }
  376. $this->mpdf->StopTransform();
  377. // mPDF 5.5.23
  378. if ($align === 'top' && $divider != '') {
  379. $wc = $this->mpdf->GetStringWidth($divider);
  380. $wc *= $kerning * $fontwidth;
  381. $this->mpdf->StartTransform();
  382. $this->mpdf->transformRotate(90, $x, $y);
  383. $this->mpdf->SetXY($x - $wc / 2, $y - $r);
  384. $this->mpdf->Cell($wc, $this->mpdf->FontSize, $divider, 0, 0, 'C');
  385. $this->mpdf->StopTransform();
  386. $this->mpdf->StartTransform();
  387. $this->mpdf->transformRotate(-90, $x, $y);
  388. $this->mpdf->SetXY($x - $wc / 2, $y - $r);
  389. $this->mpdf->Cell($wc, $this->mpdf->FontSize, $divider, 0, 0, 'C');
  390. $this->mpdf->StopTransform();
  391. }
  392. }
  393. function Shaded_box($text, $font = '', $fontstyle = 'B', $szfont = '', $width = '70%', $style = 'DF', $radius = 2.5, $fill = '#FFFFFF', $color = '#000000', $pad = 2)
  394. {
  395. // F (shading - no line),S (line, no shading),DF (both)
  396. if (!$font) {
  397. $font = $this->mpdf->default_font;
  398. }
  399. if (!$szfont) {
  400. $szfont = $this->mpdf->default_font_size * 1.8;
  401. }
  402. $text = ' ' . $text . ' ';
  403. $this->mpdf->SetFont($font, $fontstyle, $szfont, false);
  404. $text = $this->mpdf->purify_utf8_text($text);
  405. if ($this->mpdf->text_input_as_HTML) {
  406. $text = $this->mpdf->all_entities_to_utf8($text);
  407. }
  408. if ($this->mpdf->usingCoreFont) {
  409. $text = mb_convert_encoding($text, $this->mpdf->mb_enc, 'UTF-8');
  410. }
  411. // DIRECTIONALITY
  412. if (preg_match('/([' . $this->mpdf->pregRTLchars . '])/u', $text)) {
  413. $this->mpdf->biDirectional = true;
  414. } // *RTL*
  415. $textvar = 0;
  416. $save_OTLtags = $this->mpdf->OTLtags;
  417. $this->mpdf->OTLtags = [];
  418. if ($this->mpdf->useKerning) {
  419. if ($this->mpdf->CurrentFont['haskernGPOS']) {
  420. $this->mpdf->OTLtags['Plus'] .= ' kern';
  421. } else {
  422. $textvar |= TextVars::FC_KERNING;
  423. }
  424. }
  425. // Use OTL OpenType Table Layout - GSUB & GPOS
  426. if (!empty($this->mpdf->CurrentFont['useOTL'])) {
  427. $text = $this->otl->applyOTL($text, $this->mpdf->CurrentFont['useOTL']);
  428. $OTLdata = $this->otl->OTLdata;
  429. }
  430. $this->mpdf->OTLtags = $save_OTLtags;
  431. $this->mpdf->magic_reverse_dir($text, $this->mpdf->directionality, $OTLdata);
  432. if (!$width) {
  433. $width = $this->mpdf->pgwidth;
  434. } else {
  435. $width = $this->sizeConverter->convert($width, $this->mpdf->pgwidth);
  436. }
  437. $midpt = $this->mpdf->lMargin + ($this->mpdf->pgwidth / 2);
  438. $r1 = $midpt - ($width / 2); //($this->mpdf->w / 2) - 40;
  439. $r2 = $r1 + $width; //$r1 + 80;
  440. $y1 = $this->mpdf->y;
  441. $loop = 0;
  442. while ($loop === 0) {
  443. $this->mpdf->SetFont($font, $fontstyle, $szfont, false);
  444. $sz = $this->mpdf->GetStringWidth($text, true, $OTLdata, $textvar);
  445. if (($r1 + $sz) > $r2) {
  446. $szfont --;
  447. } else {
  448. $loop ++;
  449. }
  450. }
  451. $this->mpdf->SetFont($font, $fontstyle, $szfont, true, true);
  452. $y2 = $this->mpdf->FontSize + ($pad * 2);
  453. $this->mpdf->SetLineWidth(0.1);
  454. $fc = $this->colorConverter->convert($fill, $this->mpdf->PDFAXwarnings);
  455. $tc = $this->colorConverter->convert($color, $this->mpdf->PDFAXwarnings);
  456. $this->mpdf->SetFColor($fc);
  457. $this->mpdf->SetTColor($tc);
  458. $this->mpdf->RoundedRect($r1, $y1, $r2 - $r1, $y2, $radius, $style);
  459. $this->mpdf->SetX($r1);
  460. $this->mpdf->Cell($r2 - $r1, $y2, $text, 0, 1, 'C', 0, '', 0, 0, 0, 'M', 0, false, $OTLdata, $textvar);
  461. $this->mpdf->SetY($y1 + $y2 + 2); // +2 = mm margin below shaded box
  462. $this->mpdf->Reset();
  463. }
  464. }