You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

314 lines
8.9 KiB

  1. <?php
  2. /*
  3. MINIMALRTF - A minimal set of RTF coding methods to produce Rich Text Format documents on the fly.
  4. v1.5
  5. Released through http://bibliophile.sourceforge.net under the GPL licence.
  6. Do whatever you like with this -- some credit to the author(s) would be appreciated.
  7. If you make improvements, please consider contacting the administrators at bibliophile.sourceforge.net so that your improvements can be added to the release package.
  8. Mark Grimshaw 2006
  9. http://bibliophile.sourceforge.net
  10. */
  11. // COMMAND LINE TESTS:
  12. // For a quick command-line test (php -f MINIMALRTF.php) after installation, uncomment the following:
  13. /**************************************************
  14. $centred = "This is some centred text.";
  15. $full = "This is some full justified and italicized text.";
  16. $weird = "Indented UNICODE: ¿ßŽŒ‰fl™ŁÞßØ€∑≠◊∝∞∅Ωπ¿";
  17. $largeText = "Here's some large text (font size 20)";
  18. $urlText = "Here's a URL: ";
  19. $url = "http://bibliophile.sourceforge.net";
  20. $urlDisplayText = "Bibliophile's home page!";
  21. $emailText = "Here's an email: ";
  22. $email = "billgates@microsoft.com";
  23. $emailDisplayText = "I love SPAM";
  24. $colouredText = "This text is red text";
  25. $backToBlackText = "This text is black text again";
  26. $rtf = new MINIMALRTF();
  27. $string = $rtf->openRtf();
  28. $rtf->createFontBlock(0, "Arial");
  29. $rtf->createFontBlock(1, "Times New Roman");
  30. $string .= $rtf->setFontBlock();
  31. $string .= $rtf->justify("centre");
  32. $string .= $rtf->textBlock(0, 12, $centred);
  33. $string .= $rtf->justify("full");
  34. $string .= $rtf->paragraph();
  35. $string .= $rtf->textBlock(1, 12, $rtf->italics($full));
  36. $string .= $rtf->justify("full", 2, 2);
  37. $string .= $rtf->paragraph();
  38. // Depending on your character set, you may need to encode $weird as UTF-8 first using PHP's inbuilt utf8_encode() function:
  39. // $weird = $rtf->utf8_2_unicode(utf8_encode($weird));
  40. $weird = $rtf->utf8_2_unicode($weird);
  41. $string .= $rtf->textBlock(1, 12, $weird);
  42. $string .= $rtf->justify("full");
  43. $string .= $rtf->paragraph();
  44. $string .= $rtf->textBlock(1, 20, $largeText);
  45. $string .= $rtf->paragraph();
  46. $string .= $rtf->textBlock(1, 12, $urlText . $rtf->urlText($url, $urlDisplayText));
  47. $string .= $rtf->paragraph();
  48. $string .= $rtf->textBlock(1, 12, $emailText . $rtf->emailText($email, $emailDisplayText));
  49. $string .= $rtf->paragraph();
  50. $string .= $rtf->setFontColour('red');
  51. $string .= $rtf->textBlock(1, 12, $colouredText);
  52. $string .= $rtf->paragraph();
  53. $string .= $rtf->setFontColour(); // i.e. set it back to black
  54. $string .= $rtf->textBlock(1, 12, $backToBlackText);
  55. $string .= $rtf->closeRtf();
  56. // Copy and paste the commandline output to a text editor, save with a .rtf extension and load in a word processor.
  57. print $string . "\n\n";
  58. **************************************************/
  59. class MINIMALRTF
  60. {
  61. /**
  62. * Constructor method called by user.
  63. */
  64. function MINIMALRTF()
  65. {
  66. /**
  67. * some defaults
  68. */
  69. $this->justify = array(
  70. "centre" => "qc",
  71. "left" => "ql",
  72. "right" => "qr",
  73. "full" => "qj",
  74. );
  75. $this->colourTable = array(
  76. 'black' => "\\red0\\green0\\blue0;",
  77. 'maroon' => "\\red128\\green0\\blue0;",
  78. 'green' => "\\red0\\green128\\blue0;",
  79. 'olive' => "\\red128\\green128\\blue0;",
  80. 'navy' => "\\red0\\green0\\blue128;",
  81. 'purple' => "\\red128\\green0\\blue128;",
  82. 'teal' => "\\red0\\green128\\blue128;",
  83. 'gray' => "\\red128\\green128\\blue128;",
  84. 'silver' => "\\red192\\green192\\blue192;",
  85. 'red' => "\\red255\\green0\\blue0;",
  86. 'lime' => "\\red0\\green255\\blue0;",
  87. 'yellow' => "\\red255\\green255\\blue0;",
  88. 'blue' => "\\red0\\green0\\blue255;",
  89. 'fuchsia' => "\\red255\\green0\\blue255;",
  90. 'aqua' => "\\red0\\green255\\blue255;",
  91. 'white' => "\\red255\\green255\\blue255;",
  92. );
  93. }
  94. /**
  95. * Create the RTF opening tag and the colorTable
  96. * @return string
  97. */
  98. function openRtf()
  99. {
  100. $text = "{\\rtf1\\ansi\\ansicpg1252\n\n";
  101. $text .= "{\\colortbl;";
  102. $index = 1;
  103. foreach($this->colourTable as $colour => $colourCode)
  104. {
  105. $text .= $colourCode;
  106. $this->colours[$colour] = "\\s1\\cf$index";
  107. ++$index;
  108. }
  109. $text .= "}\n\n";
  110. unset($this->colourTable);
  111. return $text . "\n\n";
  112. }
  113. /**
  114. * Create the RTF closing tag
  115. * @return string
  116. */
  117. function closeRtf()
  118. {
  119. return "\n}\n\n";
  120. }
  121. /**
  122. * Convert input text to bold text
  123. * @parameter string $input - text to be converted
  124. */
  125. function bold($input = "")
  126. {
  127. return "{\b $input}";
  128. }
  129. /**
  130. * Convert input text to italics text
  131. * @parameter string $input - text to be converted
  132. */
  133. function italics($input = "")
  134. {
  135. return "{\i $input}";
  136. }
  137. /**
  138. * Convert input text to underline text
  139. * @parameter string $input - text to be converted
  140. */
  141. function underline($input = "")
  142. {
  143. return "{\ul $input}";
  144. }
  145. /**
  146. * Convert input text to superscript text
  147. * @parameter string $input - text to be converted
  148. */
  149. function superscript($input = "")
  150. {
  151. return "{\super $input}";
  152. }
  153. /**
  154. * Convert input text to subscript text
  155. * @parameter string $input - text to be converted
  156. */
  157. function subscript($input = "")
  158. {
  159. return "{\sub $input}";
  160. }
  161. /**
  162. * Set font size for each paragraph
  163. * @parameter integer $fontBlock - number of this fontblock
  164. * @parameter string $font - required font
  165. */
  166. function createFontBlock($fontBlock = FALSE, $font = FALSE)
  167. {
  168. if(($fontBlock === FALSE) || ($font === FALSE))
  169. return FALSE;
  170. $this->fontBlocks[] = "{\\f$fontBlock\\fcharset0 $font;}\n";
  171. return TRUE;
  172. }
  173. /**
  174. * Set font blocks
  175. * @return string fontblock string
  176. */
  177. function setFontBlock()
  178. {
  179. if(!isset($this->fontBlocks))
  180. return FALSE;
  181. $string = "{\\fonttbl\n";
  182. foreach($this->fontBlocks as $fontBlock)
  183. $string .= $fontBlock;
  184. $string .= "}\n\n";
  185. return $string;
  186. }
  187. /**
  188. * Justify and indent
  189. * Each TAB is equivalent to 720 units of indent
  190. * @parameter string $justify - either "centre", "left", "right" or "full"
  191. * @parameter integer $indentL - no. TABs to indent from the left
  192. * @parameter integer $indentR - no. TABs to indent from the right
  193. * @parameter integer $indentF - no. TABs to indent first line
  194. */
  195. function justify($justify = "full", $indentL = 0, $indentR = 0, $indentF = 0)
  196. {
  197. if(!array_key_exists($justify, $this->justify))
  198. $justifyC = "qj";
  199. else
  200. $justifyC = $this->justify[$justify];
  201. $indentL *= 720;
  202. $indentR *= 720;
  203. $indentF *= 720;
  204. return "\\$justifyC\\li$indentL\\ri$indentR\\fi$indentF\n";
  205. }
  206. /**
  207. * Create empty paragraph
  208. * Font Size is twice what is shown in a word processor
  209. * @return string
  210. */
  211. function paragraph($fontBlock = 0, $fontSize = 12)
  212. {
  213. $fontSize *= 2;
  214. return "{\\f$fontBlock\\fs$fontSize \\par}\n";
  215. }
  216. /**
  217. * Create text block
  218. * @parameter string $input - input string
  219. * @return string
  220. */
  221. function textBlock($fontBlock = FALSE, $fontSize = FALSE, $input = FALSE)
  222. {
  223. if(($fontBlock === FALSE) || ($fontSize === FALSE) || ($input === FALSE))
  224. return FALSE;
  225. $fontSize *= 2;
  226. return "{\\f$fontBlock\\fs$fontSize $input\\par}\n";
  227. }
  228. /**
  229. * Create email link
  230. * @parameter string $email - email address
  231. * @return string
  232. */
  233. function emailText($email, $displayText = FALSE)
  234. {
  235. if(!$displayText)
  236. $displayText = $email;
  237. return "{\\field{\\fldinst { HYPERLINK \"mailto:$email\" }}{\\fldrslt {\\cs1\\ul\\cf13 $displayText}}}";
  238. }
  239. /**
  240. * Create URL link
  241. * @parameter string $url - URL
  242. * @return string
  243. */
  244. function urlText($url, $displayText = FALSE)
  245. {
  246. if(!$displayText)
  247. $displayText = $url;
  248. return "{\\field{\\fldinst { HYPERLINK \"$url\" }}{\\fldrslt {\\cs1\\ul\\cf13 $displayText}}}";
  249. }
  250. /**
  251. * Set font color
  252. * @parameter string - colour
  253. * @return string
  254. */
  255. function setFontColour($colour = 'black')
  256. {
  257. if(!array_key_exists($colour, $this->colours))
  258. $colour = $this->colours['black'];
  259. else
  260. $colour = $this->colours[$colour];
  261. return "\n$colour\n";
  262. }
  263. /**
  264. * UTF-8 to unicode
  265. * returns an array of unicode character codes
  266. * Code adapted from opensource PHP code by Scott Reynen at:
  267. * http://www.randomchaos.com/document.php?source=php_and_unicode
  268. *
  269. * @parameter string $string UTF-8 encoded string
  270. * @return string unicode character code
  271. */
  272. function utf8_2_unicode($string)
  273. {
  274. $unicode = array();
  275. $values = array();
  276. $lookingFor = 1;
  277. for($i = 0; $i < strlen($string); $i++)
  278. {
  279. $thisValue = ord($string[$i]);
  280. if($thisValue < 128)
  281. $unicode[] = $string[$i];
  282. else
  283. {
  284. if(count($values) == 0)
  285. $lookingFor = ($thisValue < 224) ? 2 : 3;
  286. $values[] = $thisValue;
  287. if(count($values) == $lookingFor)
  288. {
  289. $number = ($lookingFor == 3) ?
  290. (($values[0] % 16) * 4096) + (($values[1] % 64) * 64) + ($values[2] % 64) :
  291. (($values[0] % 32) * 64) + ($values[1] % 64);
  292. // $unicode[] = '\u' . $number . " ?";
  293. // A better unicode function?
  294. $decModulus = $number % 256;
  295. $modulus = dechex($number % 256);
  296. if($decModulus < 16)
  297. $modulus = '0' . $modulus;
  298. $unicode[] = '\u' . $number . "\'$modulus";
  299. $values = array();
  300. $lookingFor = 1;
  301. }
  302. }
  303. }
  304. return join('', $unicode);
  305. }
  306. }
  307. ?>