////////////////////////////////////////////////////////////////////////////////
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////
package flashx.textLayout.conversion
{
import flashx.textLayout.elements.*;
/**
* Export filter for plain text format. This class provides an alternative to
* the TextConverter.export()
static method for exporting plain text,
* useful if you need to customize the export by changing the paragraphSeparator
* or stripDiscretionaryHyphens options. The PlainTextExporter class's
* export()
method results in the
* same output string as the TextConverter.export()
static method
* if the two properties of the PlainTextExporter class, the paragraphSeparator
* and the stripDiscretionaryHyphens
properties, contain their
* default values of "\n"
and true
, respectively.
* @includeExample examples\PlainTextExporter_example.as -noswf
* @playerversion Flash 10
* @playerversion AIR 1.5
* @langversion 3.0
*/
public class PlainTextExporter implements ITextExporter
{
private var _stripDiscretionaryHyphens:Boolean;
private var _paragraphSeparator:String;
static private var _discretionaryHyphen:String = String.fromCharCode(0x00AD);
/**
* Constructor
* @playerversion Flash 10
* @playerversion AIR 1.5
* @langversion 3.0
*/
public function PlainTextExporter()
{
_stripDiscretionaryHyphens = true;
_paragraphSeparator = "\n";
}
/** This flag indicates whether discretionary hyphens in the text should be stripped during the export process.
* Discretionary hyphens, also known as "soft hyphens", indicate where to break a word in case the word must be
* split between two lines. The Unicode character for discretionary hyphens is \u00AD
.
*
If the stripDiscretionaryHyphens
property is set to true
, discretionary hyphens that are in the original text will not be in the exported text,
* even if they are part of the original text. If false
, discretionary hyphens will be in the exported text,
* The default value is true
.
paragraphSeparator
* and the stripDiscretionaryHyphens
properties
* affect the output produced by this method.
* @param source the text flow object to export
* @param conversionType The type to return (STRING_TYPE). This
* parameter accepts only one value: ConversionType.STRING_TYPE
,
* but is necessary because this class implements the ITextExporter
* interface. The interface method, ITextExporter.export()
, requires
* this parameter.
* @return Object the exported content
*
* @see #paragraphSeparator
* @see #stripDiscretionaryHyphens
* @see ConversionType#STRING_TYPE
* @playerversion Flash 10
* @playerversion AIR 1.5
* @langversion 3.0
*/
public function export(source:TextFlow, conversionType:String):Object
{
if (conversionType == ConversionType.STRING_TYPE)
return exportToString(source);
return null;
}
/** Export text content as a string
* @param source the text to export
* @return String the exported content
*
* @private
*/
protected function exportToString(source:TextFlow):String
{
var rslt:String = "";
var leaf:FlowLeafElement = source.getFirstLeaf();
while (leaf)
{
var p:ParagraphElement = leaf.getParagraph();
while (true)
{
var curString:String = leaf.text;
//split out discretionary hyphen and put string back together
if (_stripDiscretionaryHyphens)
{
var temparray:Array = curString.split(_discretionaryHyphen);
curString = temparray.join("");
}
rslt += curString;
var nextLeaf:FlowLeafElement = leaf.getNextLeaf(p);
if (!nextLeaf)
break; // end of para
leaf = nextLeaf;
}
leaf = leaf.getNextLeaf();
if (leaf) // not the last para
rslt += _paragraphSeparator;
}
return rslt;
}
}
}