diff --git a/samples/EASYTABLES.php b/samples/EASYTABLES.php new file mode 100644 index 0000000000..6d41c080b0 --- /dev/null +++ b/samples/EASYTABLES.php @@ -0,0 +1,420 @@ +addTable($TableStyle); +} + +function AddTableRow(){WordTable()->addRow();} + +function AddTableCell($Text,$FontStyle,$SpacingStyle) +{ +if(WordTableStyle('alignment')=='center'){WordTable()->addCell(WordTableStyle('width'),WordTableStyle())->addText($Text,$FontStyle,$SpacingStyle);return;} +$Padding='_____'; +$Invisible['color']='FFFFFF'; +$Invisible['size']=20/(WordTableStyle('numColumns')+2); +$Cell=WordTable()->addCell(WordTableStyle('width'),WordTableStyle()); +$TextRun=$Cell->addTextRun($SpacingStyle); +if(WordTableStyle('alignment')=='left') + { + if(WordTableStyle('numColumns')>3) $Invisible['size']=0; + $TextRun->addText($Padding,$Invisible);$TextRun->addText($Text,$FontStyle); + return; + } +$TextRun->addText($Text,$FontStyle); +$TextRun->addText($Padding,$Invisible); +} + +function TableCell($row,$column) +{ +$FontStyle=[]; +$TextStyle=[]; +$FontStyle['bold']=WordTableStyle('bold'); +$FontStyle['size']=WordTableStyle('size'); +$FontStyle['color']=WordTableStyle('color'); +$SpacingStyle['alignment']=WordTableStyle('alignment'); +$SpacingStyle['spaceAfter']=WordTableStyle('spaceAfter'); +$Text=WordTableText(); +$Text=$Text[$row-1][$column-1]; +if(mb_strpos($Text,WordTableStyle('fadeCue'))!==false) {$FontStyle['color']=WordTableStyle('colorFaded');$Text=str_replace(WordTableStyle('fadeCue'),'',$Text);} +$LetterThreshold=Round(WordTableStyle('width')/130); //Width 2000->Words longer than 15 letters will be hyphenated +if(WordTableStyle('abbreviate')) $Text=Abbreviated($Text,$LetterThreshold); //Width 2000, abbreviates to 10 letters. +if(!WordTableStyle('abbreviate')) if(WordTableStyle('hyphenate')) $Text=Hyphenated($Text,$LetterThreshold,WordTableStyle('language')); +AddTableCell($Text,$FontStyle,$SpacingStyle); +} + +function WordTableText($text=null) +{ +static $TextArray=[]; +static $Row=0; +static $Col=0; +if($text===null) return $TextArray;//Return the array. +if($text===row) if(empty($TextArray))return; //Start new row on on empty array. No go +if($text===row) {$Row++;$Col=0;return;}//Start new row +if(is_array($text)) + { + $text=array_filter($text,function($line){return trim($line)!=='';}); + $text=implode("\n",$text); + } +$TextArray[$Row][$Col]=$text;// Add text at current position +$Col++; +if($text!==clear) return;//Text was added. No need to return anything +$Row=0; +$Col=0; +$k=$TextArray; +$TextArray=null; +unset($TextArray); +return $k;//No argument=reset +} + +function WordTableStyle($ThisStyle='',$ThisValue='') +{ +static $StyleArray=[]; +if($ThisStyle==clear) {$StyleArray=null;unset($StyleArray);return;} +if($ThisStyle==='') return $StyleArray; +if($ThisValue===remove) {unset($StyleArray[$ThisStyle]);return;} +if($ThisValue==='') {return isset($StyleArray[$ThisStyle])?$StyleArray[$ThisStyle]:'';} +$StyleArray[$ThisStyle]=$ThisValue; +} + +function WordColumnWidth($Column='',$Width='') +{ +static $WidthArray=[]; +if($Column=='') {$WidthArray=null;unset($WidthArray);return;} +if($Column==clear) {$WidthArray=null;unset($WidthArray);return;} +if($Width=='') return isset($WidthArray[$Column])?$WidthArray[$Column]:''; +$WidthArray[$Column]=$Width; +} + +function WordColumnAlign($Column='',$Alignment='') +{ +static $AlignArray=[]; +if($Column=='') {$AlignArray=null;unset($AlignArray);return;} +if($Column==clear) {$AlignArray=null;unset($AlignArray);return;} +if($Alignment=='') return isset($AlignArray[$Column])?$AlignArray[$Column]:'left'; +$AlignArray[$Column]=$Alignment; +} + +function WordColumnBold($Column='',$IsBold='') +{ +static $BoldArray=[]; +if($Column=='') {$BoldArray=null;unset($BoldArray);return;} +if($Column==clear) {$BoldArray=null;unset($BoldArray);return;} +if($IsBold=='') return isset($BoldArray[$Column]); +$BoldArray[$Column]=$IsBold; +} + +function WordRowBold($Row='',$IsBold='') +{ +static $BoldArray=[]; +if($Row=='') {$BoldArray=null;unset($BoldArray);return;} +if($Row==clear) {$BoldArray=null;unset($BoldArray);return;} +if($IsBold=='') return isset($BoldArray[$Row]); +$BoldArray[$Row]=$IsBold; +} + +function Abbreviated($Word,$Theshold=10) +{ +if(mb_strpos($Word,'-')!==false) return $Word; +if(mb_strpos($Word,' ')!==false) return $Word; +if(mb_strlen($Word)<=$Theshold) return $Word; +if(preg_match('/^\d/',$Word)) return $Word; //Starts with a digit — looks like a number +$Word=mb_substr($Word,0,$Theshold-1).'...'; +echo "Abbreviated $Word
"; +return $Word; +} + +function ConsonantPairs($Language) +{ +if($Language=='de') return ['br','bl','ck','dr','ft','fr','gl','gr','kr','kl','nb','ng','nd','nk','nt','mp','pl','pr','pf','pz','rm','rp','rs','rt','sk','sl','st','sp','tg','tm','tr','tz']; +return ['ck','ct','mp','nd','nt','lt','rt','st','sp','sk','pl','pr','gr','bl','br','tr','cl','cr','dr','fr']; +} + +function Hyphenated($Word,$Theshold=10,$Language='en') +{ +$Middle = (int) floor($Length / 2); +$vowels = '/[aeiouy]/i'; +$Pairs=ConsonantPairs($Language); +$MaxOffset = 2; + +if(preg_match('/^\d/',$Word)) return $Word; //Starts with a digit — looks like a number +if(mb_strpos($Word, '-')!==false) return $Word; +if(mb_strpos($Word, ' ')!==false) return $Word; + +$Length=mb_strlen($Word); +if($Length<$Theshold) return $Word; +if(mb_strpos($Word,'name')!==false) return str_replace('name','-name',$Word); + +// Check for consonant pairs +for ($Offset = 0; $Offset <= $MaxOffset; $Offset++) + foreach (['back' => $Middle - $Offset - 1, 'forward' => $Middle + $Offset - 1] as $Pos) { + $Pair = mb_substr($Word, $Pos, 2); + if (in_array($Pair, $Pairs)) return mb_substr($Word, 0, $Pos + 1) . '-' . mb_substr($Word, $Pos + 1); + } + + +// Check for identical letters +for ($Offset = 0; $Offset <= $MaxOffset; $Offset++) + foreach (['back' => $Middle - $Offset - 1, 'forward' => $Middle + $Offset - 1] as $Pos) { + $Char = mb_substr($Word, $Pos, 1); + $Next = mb_substr($Word, $Pos + 1, 1); + if ($Char === $Next) return mb_substr($Word, 0, $Pos + 1) . '-' . mb_substr($Word, $Pos + 1); + } + +// Check for vowels near middle +for ($Offset = 0; $Offset < $Middle - 1; $Offset++) + foreach (['back' => $Middle - $Offset, 'forward' => $Middle + $Offset] as $Pos) { + $Char = mb_substr($Word, $Pos, 1); + if (preg_match($vowels, $Char)) return mb_substr($Word, 0, $Pos + 1) . '-' . mb_substr($Word, $Pos + 1); + } + +// Fallback split at middle +return mb_substr($Word, 0, $Middle) . '-' . mb_substr($Word, $Middle); +}