1<?php 2/* 3MINIMALRTF - A minimal set of RTF coding methods to produce Rich Text Format documents on the fly. 4v1.1 5 6Released through http://bibliophile.sourceforge.net under the GPL licence. 7Do whatever you like with this -- some credit to the author(s) would be appreciated. 8 9If you make improvements, please consider contacting the administrators at bibliophile.sourceforge.net so that your improvements can be added to the release package. 10 11Mark Grimshaw 2004 12http://bibliophile.sourceforge.net 13*/ 14 15// COMMAND LINE TESTS: 16// For a quick command-line test (php -f MINIMALRTF.php) after installation, uncomment the following: 17/************************************************** 18$centred = "This is some centred text."; 19$full = "This is some full justified and italicized text."; 20$weird = "Indented UNICODE: ¿ßŽŒ‰fl™ŁÞßØ€∑≠◊∝∞∅Ωπ¿"; 21$rtf = new MINIMALRTF(); 22$string = $rtf->openRtf(); 23$rtf->createFontBlock(0, "Arial"); 24$rtf->createFontBlock(1, "Times New Roman"); 25$string .= $rtf->setFontBlock(); 26$string .= $rtf->justify("centre"); 27$string .= $rtf->textBlock(0, 12, $centred); 28$string .= $rtf->justify("full"); 29$string .= $rtf->paragraph(); 30$string .= $rtf->textBlock(1, 12, $rtf->italics($full)); 31$string .= $rtf->justify("full", 2, 2); 32$string .= $rtf->paragraph(); 33// Depending on your character set, you may need to encode $weird as UTF-8 first using PHP's inbuilt utf8_encode() function: 34// $weird = $rtf->utf8_2_unicode(utf8_encode($weird)); 35$weird = $rtf->utf8_2_unicode($weird); 36$string .= $rtf->textBlock(1, 12, $weird); 37$string .= $rtf->closeRtf(); 38 39// Copy and paste the commandline output to a text editor, save with a .rtf extension and load in a word processor. 40print $string . "\n\n"; 41 42**************************************************/ 43 44class MINIMALRTF 45{ 46 /** 47 * Constructor method called by user. 48 */ 49 function MINIMALRTF() 50 { 51 /** 52 * some defaults 53 */ 54 $this->justify = array( 55 "centre" => "qc", 56 "left" => "qj", 57 "right" => "qr", 58 "full" => "qj", 59 ); 60 } 61 /** 62 * Create the RTF opening tag 63 * @return string 64 */ 65 function openRtf() 66 { 67 return "{\\rtf1\\ansi\\ansicpg1252\n\n"; 68 } 69 /** 70 * Create the RTF closing tag 71 * @return string 72 */ 73 function closeRtf() 74 { 75 return "\n}\n\n"; 76 } 77 /** 78 * Convert input text to bold text 79 * @parameter string $input - text to be converted 80 */ 81 function bold($input = "") 82 { 83 return "{\b $input }"; 84 } 85 /** 86 * Convert input text to italics text 87 * @parameter string $input - text to be converted 88 */ 89 function italics($input = "") 90 { 91 return "{\i $input }"; 92 } 93 /** 94 * Convert input text to underline text 95 * @parameter string $input - text to be converted 96 */ 97 function underline($input = "") 98 { 99 return "{\ul $input }"; 100 } 101 /** 102 * Set font size for each paragraph 103 * @parameter integer $number - number of this fontblock 104 * @parameter string $font - required font 105 */ 106 function createFontBlock($fontBlock = FALSE, $font = FALSE) 107 { 108 if(($fontBlock === FALSE) || ($font === FALSE)) 109 return FALSE; 110 $this->fontBlocks[] = "{\\f$fontBlock\\fcharset0 $font;}\n"; 111 return TRUE; 112 } 113 /** 114 * Set font blocks 115 * @return string fontblock string 116 */ 117 function setFontBlock() 118 { 119 if(!isset($this->fontBlocks)) 120 return FALSE; 121 $string = "{\\fonttbl\n"; 122 foreach($this->fontBlocks as $fontBlock) 123 $string .= $fontBlock; 124 $string .= "}\n\n"; 125 return $string; 126 } 127 /** 128 * Justify and indent 129 * Each TAB is equivalent to 720 units of indent 130 * @parameter string $justify - either "centre", "left", "right" or "full" 131 * @parameter integer $indentL - no. TABs to indent from the left 132 * @parameter integer $indentR - no. TABs to indent from the right 133 */ 134 function justify($justify = "full", $indentL = 0, $indentR = 0) 135 { 136 if(!array_key_exists($justify, $this->justify)) 137 $justifyC = "qj"; 138 else 139 $justifyC = $this->justify[$justify]; 140 $indentL *= 720; 141 $indentR *= 720; 142 return "\\$justifyC\\li$indentL\\ri$indentR\n"; 143 } 144 /** 145 * Create empty paragraph 146 * Font Size is twice what is shown in a word processor 147 * @return string 148 */ 149 function paragraph($fontBlock = 0, $fontSize = 12) 150 { 151 $fontSize *= 2; 152 return "{\\f$fontBlock\\fs$fontSize \\par }\n"; 153 } 154 /** 155 * Create text block 156 * @parameter string $input - input string 157 * @return string 158 */ 159 function textBlock($fontBlock = FALSE, $fontSize = FALSE, $input = FALSE) 160 { 161 if(($fontBlock === FALSE) || ($fontSize === FALSE) || ($input === FALSE)) 162 return FALSE; 163 $fontSize *= 2; 164 return "{\\f$fontBlock\\fs$fontSize $input \\par }\n"; 165 } 166 /** 167 * UTF-8 to unicode 168 * returns an array of unicode character codes 169 * Code adapted from opensource PHP code by Scott Reynen at: 170 * http://www.randomchaos.com/document.php?source=php_and_unicode 171 * 172 * @parameter string $string UTF-8 encoded string 173 * @return array unicode character code 174 */ 175 function utf8_2_unicode($string) 176 { 177 $unicode = array(); 178 $values = array(); 179 $lookingFor = 1; 180 for($i = 0; $i < strlen($string); $i++) 181 { 182 $thisValue = ord($string[$i]); 183 if($thisValue < 128) 184 $unicode[] = $string[$i]; 185 else 186 { 187 if(count($values) == 0) 188 $lookingFor = ($thisValue < 224) ? 2 : 3; 189 $values[] = $thisValue; 190 if(count($values) == $lookingFor) 191 { 192 $number = ($lookingFor == 3) ? 193 (($values[0] % 16) * 4096) + (($values[1] % 64) * 64) + ($values[2] % 64) : 194 (($values[0] % 32) * 64) + ($values[1] % 64); 195 $unicode[] = '\u' . $number . " ?"; 196 $values = array(); 197 $lookingFor = 1; 198 } 199 } 200 } 201 return join('', $unicode); 202 } 203} 204?> 205