/** * Create new style by importing ODT style definition. * * @param $xmlCode Style definition in ODT XML format * @return ODTStyle New specific style */ public static function importODTStyle($xmlCode) { $style = new ODTTextStyle(); $attrs = 0; $open = XMLUtil::getElementOpenTag('style:style', $xmlCode); if (!empty($open)) { $attrs += $style->importODTStyleInternal(ODTStyleStyle::getStyleProperties(), $open); } else { $open = XMLUtil::getElementOpenTag('style:default-style', $xmlCode); if (!empty($open)) { $style->setDefault(true); $attrs += $style->importODTStyleInternal(ODTStyleStyle::getStyleProperties(), $open); } } $open = XMLUtil::getElementOpenTag('style:text-properties', $xmlCode); if (!empty($open)) { $attrs += $style->importODTStyleInternal(self::$text_fields, $open); } // If style has no meaningfull content then throw it away if ($attrs == 0) { return NULL; } return $style; }
/** * Create new style by importing ODT style definition. * * @param $xmlCode Style definition in ODT XML format * @return ODTStyle New specific style */ public static function importODTStyle($xmlCode) { $style = new ODTTextOutlineStyle(); $attrs = 0; $open = XMLUtil::getElementOpenTag('text:outline-style', $xmlCode); if (!empty($open)) { // This properties are stored in the properties of ODTStyle $attrs += $style->importODTStyleInternal(self::$outline_fields, $open); } $pos = 0; $end = 0; $max = strlen($xmlCode); $level = XMLUtil::getElement('text:outline-level-style', substr($xmlCode, $pos), $end); $pos += $end; $text_fields = ODTTextStyle::getTextProperties(); $check = 0; while ($level != NULL) { // We can have multiple level definitons with all the same properties. // So we store this in our own array. The "text:level" is the array key. if (!empty($level)) { $properties = array(); $attrs += $style->importODTStyleInternal($text_fields, $level, $properties); $attrs += $style->importODTStyleInternal(self::$outline_fields, $level, $properties); // Assign properties array to our level array $level_number = $style->getPropertyInternal('level', $properties); $style->outline_level_styles[$level_number] = $properties; } // Get XML code for next level. $level = XMLUtil::getElement('text:outline-level-style', substr($xmlCode, $pos), $end); $pos += $end; if ($pos >= $max) { break; } } // If style has no meaningfull content then throw it away if ($attrs == 0) { return NULL; } return $style; }
/** * Create new style by importing ODT style definition. * * @param $xmlCode Style definition in ODT XML format * @return ODTStyle New specific style */ public static function importODTStyle($xmlCode) { $style = new ODTParagraphStyle(); $attrs = 0; $open = XMLUtil::getElementOpenTag('style:style', $xmlCode); if (!empty($open)) { $attrs += $style->importODTStyleInternal(ODTStyleStyle::getStyleProperties(), $open, $style->style_properties); } else { $open = XMLUtil::getElementOpenTag('style:default-style', $xmlCode); if (!empty($open)) { $style->setDefault(true); $attrs += $style->importODTStyleInternal(ODTStyleStyle::getStyleProperties(), $open, $style->style_properties); } } $open = XMLUtil::getElementOpenTag('style:paragraph-properties', $xmlCode); if (!empty($open)) { $attrs += $style->importODTStyleInternal(self::$paragraph_fields, $xmlCode); } $open = XMLUtil::getElementOpenTag('style:text-properties', $xmlCode); if (!empty($open)) { $attrs += $style->importODTStyleInternal(ODTTextStyle::getTextProperties(), $open, $style->text_properties); } // Get all tab-stops. $tabs = XMLUtil::getElementContent('style:tab-stops', $xmlCode); if ($tabs != NULL) { $max = strlen($tabs); $pos = 0; $index = 0; $tab = XMLUtil::getElement('style:tab-stop', $tabs, $end); $pos = $end; while ($tab != NULL) { $style->tab_stops[$index] = array(); $attrs += $style->importODTStyleInternal(self::$tab_stop_fields, $tab, $style->tab_stops[$index]); $index++; $tab = XMLUtil::getElement('style:tab-stop', substr($tabs, $pos), $end); $pos += $end; } } // If style has no meaningfull content then throw it away if ($attrs == 0) { return NULL; } return $style; }
/** * This function creates a text style using the style as set in the assoziative array $properties. * The parameters in the array should be named as the CSS property names e.g. 'color' or 'background-color'. * Properties which shall not be used in the style can be disabled by setting the value in disabled_props * to 1 e.g. $disabled_props ['color'] = 1 would block the usage of the color property. * * The currently supported properties are: * background-color, color, font-style, font-weight, font-size, border, font-family, font-variant, letter-spacing, * vertical-align, background-image * * The function returns the name of the new style or NULL if all relevant properties are empty. * * @author LarsDW223 * @param $properties * @param null $disabled_props * @return ODTTextStyle or NULL */ public static function createTextStyle(array $properties, array $disabled_props = NULL) { // If the property 'vertical-align' has the value 'sub' or 'super' // then for ODT it needs to be converted to the corresponding 'text-position' property. // Replace sub and super with text-position. $valign = $properties['vertical-align']; if (!empty($valign)) { if ($valign == 'sub') { $properties['text-position'] = '-33% 100%'; unset($properties['vertical-align']); } elseif ($valign == 'super') { $properties['text-position'] = '33% 100%'; unset($properties['vertical-align']); } } // Separate country from language $lang = $properties['lang']; $country = $properties['country']; if (!empty($lang)) { $parts = preg_split('/-/', $lang); $lang = $parts[0]; $country = $parts[1]; $properties['country'] = trim($country); $properties['lang'] = trim($lang); } if (!empty($properties['country'])) { if (empty($properties['country-asian'])) { $properties['country-asian'] = $properties['country']; } if (empty($properties['country-complex'])) { $properties['country-complex'] = $properties['country']; } } // Create style name (if not given). $style_name = $properties['style-name']; if (empty($style_name)) { $style_name = self::getNewStylename('Text'); $properties['style-name'] = $style_name; } // Create empty text style. $object = new ODTTextStyle(); if ($object == NULL) { return NULL; } // Import our properties $object->importProperties($properties, $disabled_props); return $object; }
/** * Set a property for a specific level. * * @param $level The level for which to set the property (1 to 10) * @param $property The name of the property to set * @param $value New value to set */ public function setPropertyForLevel($level, $property, $value) { if ($property == 'list-level-style') { // Property 'list-level-style' is a special property just to remember // which element needs to be encoded on a call to toString(). // It may not be included in the output of toString()!!! $this->setPropertyInternal($property, 'list-level-style', $value, 'list-level-style', $this->list_level_styles[$level]['list-style']); } else { // First check fields/properties common to each list-level-style $text_fields = ODTTextStyle::getTextProperties(); if (array_key_exists($property, $text_fields)) { $this->setPropertyInternal($property, $text_fields[$property][0], $value, $text_fields[$property][1], $this->list_level_styles[$level]['text']); return; } if (array_key_exists($property, self::$list_level_props_fields)) { $this->setPropertyInternal($property, self::$list_level_props_fields[$property][0], $value, self::$list_level_props_fields[$property][1], $this->list_level_styles[$level]['list-level']); return; } if (array_key_exists($property, self::$label_align_fields)) { $this->setPropertyInternal($property, self::$label_align_fields[$property][0], $value, self::$label_align_fields[$property][1], $this->list_level_styles[$level]['label']); return; } // Now check fields specific to the list-level-style. $element = $this->getPropertyFromLevel($level, 'list-level-style'); switch ($element) { case 'number': $fields = self::$style_number_fields; break; case 'bullet': $fields = self::$style_bullet_fields; break; case 'image': $fields = self::$style_image_fields; break; } if (array_key_exists($property, $fields)) { $this->setPropertyInternal($property, $fields[$property][0], $value, $fields[$property][1], $this->list_level_styles[$level]['list-style']); } } }
/** * Create new style by importing ODT style definition. * * @param $xmlCode Style definition in ODT XML format * @return ODTStyle New specific style */ public static function importODTStyle($xmlCode) { $matches = array(); if (preg_match('/style:family="[^"]+"/', $xmlCode, $matches) !== 1) { return NULL; } $family = substr($matches[0], strlen('style:family=')); $family = trim($family, '"<>'); switch ($family) { case 'text': return ODTTextStyle::importODTStyle($xmlCode); case 'paragraph': return ODTParagraphStyle::importODTStyle($xmlCode); case 'table': return ODTTableStyle::importODTStyle($xmlCode); case 'table-column': return ODTTableColumnStyle::importODTStyle($xmlCode); case 'table-row': return ODTTableRowStyle::importODTStyle($xmlCode); case 'table-cell': return ODTTableCellStyle::importODTStyle($xmlCode); } // Unknown/not implemented style family. // Return NULL, in this case ODTStyle will create a generic unknown style. return NULL; }