git-svn-id: svn://svn.code.sf.net/p/itextsharp/code/trunk@12 820d3149-562b-4f88-9aa4-a8e61a3485cf
master
psoares33 2008-08-18 19:13:00 +00:00
parent 4141622d84
commit 8c97e0dd51
11 changed files with 599 additions and 495 deletions

View File

@ -445,6 +445,17 @@ namespace iTextSharp.text
/** a possible list attribute */
public const String LOWERCASE = "lowercase";
/**
* a possible list attribute
* @since 2.1.3
*/
public const String FACE = "face";
/** attribute of the image or iframe tag
* @since 2.1.3
*/
public const String SRC = "src";
// methods
/// <summary>

View File

@ -122,7 +122,7 @@ namespace iTextSharp.text.factories {
}
value = attributes[Markup.CSS_KEY_LINEHEIGHT];
if (value != null) {
phrase.Leading = Markup.ParseLength(value);
phrase.Leading = Markup.ParseLength(value, Markup.DEFAULT_FONT_SIZE);
}
value = attributes[ElementTags.ITEXT];
if (value != null) {

View File

@ -213,7 +213,7 @@ namespace iTextSharp.text.html {
this[peer.Alias] = peer;
peer = new HtmlPeer(ElementTags.IMAGE, HtmlTags.IMAGE);
peer.AddAlias(ElementTags.URL, HtmlTags.URL);
peer.AddAlias(ElementTags.URL, ElementTags.SRC); // contributed by Lubos Strapko
peer.AddAlias(ElementTags.ALT, HtmlTags.ALT);
peer.AddAlias(ElementTags.PLAINWIDTH, HtmlTags.PLAINWIDTH);
peer.AddAlias(ElementTags.PLAINHEIGHT, HtmlTags.PLAINHEIGHT);

View File

@ -321,5 +321,9 @@ namespace iTextSharp.text.html {
/** This is a possible HTML attribute. */
public const string STYLESHEET = "stylesheet";
/** This is a possible HTML attribute for auto-formated
* @since 2.1.3
*/
public const String PRE = "pre";
}
}

View File

@ -268,12 +268,79 @@ namespace iTextSharp.text.html {
/** a CSS value for text decoration */
public const string CSS_VALUE_UNDERLINE = "underline";
/** a default value for font-size
* @since 2.1.3
*/
public const float DEFAULT_FONT_SIZE = 12f;
/// <summary>
/// Parses a length.
/// </summary>
/// <param name="str">a length in the form of an optional + or -, followed by a number and a unit.</param>
/// <returns>a float</returns>
public static float ParseLength(string str) {
// TODO: Evaluate the effect of this.
// It may change the default behavour of the methd if this is changed.
// return ParseLength(string, Markup.DEFAULT_FONT_SIZE);
int pos = 0;
int length = str.Length;
bool ok = true;
while (ok && pos < length) {
switch (str[pos]) {
case '+':
case '-':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '.':
pos++;
break;
default:
ok = false;
break;
}
}
if (pos == 0)
return 0f;
if (pos == length)
return float.Parse(str, System.Globalization.NumberFormatInfo.InvariantInfo);
float f = float.Parse(str.Substring(0, pos), System.Globalization.NumberFormatInfo.InvariantInfo);
str = str.Substring(pos);
// inches
if (str.StartsWith("in")) {
return f * 72f;
}
// centimeters
if (str.StartsWith("cm")) {
return (f / 2.54f) * 72f;
}
// millimeters
if (str.StartsWith("mm")) {
return (f / 25.4f) * 72f;
}
// picas
if (str.StartsWith("pc")) {
return f * 12f;
}
// default: we assume the length was measured in points
return f;
}
/**
* New method contributed by: Lubos Strapko
*
* @since 2.1.3
*/
public static float ParseLength(String str, float actualFontSize) {
if (str == null)
return 0f;
int pos = 0;
int length = str.Length;
bool ok = true;
@ -319,6 +386,15 @@ namespace iTextSharp.text.html {
if (str.StartsWith("pc")) {
return f * 12f;
}
// 1em is equal to the current font size
if (str.StartsWith("em")) {
return f * actualFontSize;
}
// one ex is the x-height of a font (x-height is usually about half the
// font-size)
if (str.StartsWith("ex")) {
return f * actualFontSize / 2;
}
// default: we assume the length was measured in points
return f;
}

View File

@ -1,135 +1,135 @@
using System;
using System.Collections;
/*
* Copyright 2004 Paulo Soares
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (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.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.html.simpleparser {
public class ChainedProperties {
public static int[] fontSizes = {8, 10, 12, 14, 18, 24, 36};
public ArrayList chain = new ArrayList();
/** Creates a new instance of ChainedProperties */
public ChainedProperties() {
}
public String this[String key] {
get {
for (int k = chain.Count - 1; k >= 0; --k) {
Object[] obj = (Object[])chain[k];
Hashtable prop = (Hashtable)obj[1];
String ret = (String)prop[key];
if (ret != null)
return ret;
}
return null;
}
}
public bool HasProperty(String key) {
for (int k = chain.Count - 1; k >= 0; --k) {
Object[] obj = (Object[])chain[k];
Hashtable prop = (Hashtable)obj[1];
if (prop.ContainsKey(key))
return true;
}
return false;
}
public void AddToChain(String key, Hashtable prop) {
// adjust the font size
String value = (String)prop["size"];
if (value != null) {
if (value.EndsWith("px")) {
prop["size"] = value.Substring(0, value.Length - 2);
}
else {
int s = 0;
if (value.StartsWith("+") || value.StartsWith("-")) {
String old = this["basefontsize"];
if (old == null)
old = "12";
float f = float.Parse(old, System.Globalization.NumberFormatInfo.InvariantInfo);
int c = (int)f;
for (int k = fontSizes.Length - 1; k >= 0; --k) {
if (c >= fontSizes[k]) {
s = k;
break;
}
}
int inc = int.Parse(value.StartsWith("+") ? value.Substring(1) : value);
s += inc;
}
else {
try {
s = int.Parse(value) - 1;
}
catch {
s = 0;
}
}
if (s < 0)
s = 0;
else if (s >= fontSizes.Length)
s = fontSizes.Length - 1;
prop["size"] = fontSizes[s].ToString();
}
}
chain.Add(new Object[]{key, prop});
}
public void RemoveChain(String key) {
for (int k = chain.Count - 1; k >= 0; --k) {
if (key.Equals(((Object[])chain[k])[0])) {
chain.RemoveAt(k);
return;
}
}
}
}
using System;
using System.Collections;
/*
* Copyright 2004 Paulo Soares
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (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.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.html.simpleparser {
public class ChainedProperties {
public static int[] fontSizes = {8, 10, 12, 14, 18, 24, 36};
public ArrayList chain = new ArrayList();
/** Creates a new instance of ChainedProperties */
public ChainedProperties() {
}
public String this[String key] {
get {
for (int k = chain.Count - 1; k >= 0; --k) {
Object[] obj = (Object[])chain[k];
Hashtable prop = (Hashtable)obj[1];
String ret = (String)prop[key];
if (ret != null)
return ret;
}
return null;
}
}
public bool HasProperty(String key) {
for (int k = chain.Count - 1; k >= 0; --k) {
Object[] obj = (Object[])chain[k];
Hashtable prop = (Hashtable)obj[1];
if (prop.ContainsKey(key))
return true;
}
return false;
}
public void AddToChain(String key, Hashtable prop) {
// adjust the font size
String value = (String)prop[ElementTags.SIZE];
if (value != null) {
if (value.EndsWith("px")) {
prop[ElementTags.SIZE] = value.Substring(0, value.Length - 2);
}
else {
int s = 0;
if (value.StartsWith("+") || value.StartsWith("-")) {
String old = this["basefontsize"];
if (old == null)
old = "12";
float f = float.Parse(old, System.Globalization.NumberFormatInfo.InvariantInfo);
int c = (int)f;
for (int k = fontSizes.Length - 1; k >= 0; --k) {
if (c >= fontSizes[k]) {
s = k;
break;
}
}
int inc = int.Parse(value.StartsWith("+") ? value.Substring(1) : value);
s += inc;
}
else {
try {
s = int.Parse(value) - 1;
}
catch {
s = 0;
}
}
if (s < 0)
s = 0;
else if (s >= fontSizes.Length)
s = fontSizes.Length - 1;
prop[ElementTags.SIZE] = fontSizes[s].ToString();
}
}
chain.Add(new Object[]{key, prop});
}
public void RemoveChain(String key) {
for (int k = chain.Count - 1; k >= 0; --k) {
if (key.Equals(((Object[])chain[k])[0])) {
chain.RemoveAt(k);
return;
}
}
}
}
}

View File

@ -1,117 +1,117 @@
using System;
using System.Collections;
/*
* Copyright 2004 Paulo Soares
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (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.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.html.simpleparser {
public class StyleSheet {
public Hashtable classMap = new Hashtable();
public Hashtable tagMap = new Hashtable();
/** Creates a new instance of StyleSheet */
public StyleSheet() {
}
public void ApplyStyle(String tag, Hashtable props) {
Hashtable map = (Hashtable)tagMap[tag.ToLower(System.Globalization.CultureInfo.InvariantCulture)];
Hashtable temp;
if (map != null) {
temp = new Hashtable(map);
foreach (DictionaryEntry dc in props)
temp[dc.Key] = dc.Value;
foreach (DictionaryEntry dc in temp)
props[dc.Key] = dc.Value;
}
String cm = (String)props["class"];
if (cm == null)
return;
map = (Hashtable)classMap[cm.ToLower(System.Globalization.CultureInfo.InvariantCulture)];
if (map == null)
return;
props.Remove("class");
temp = new Hashtable(map);
foreach (DictionaryEntry dc in props)
temp[dc.Key] = dc.Value;
foreach (DictionaryEntry dc in temp)
props[dc.Key] = dc.Value;
}
private void ApplyMap(Hashtable map, Hashtable props) {
}
public void LoadStyle(String style, Hashtable props) {
classMap[style.ToLower(System.Globalization.CultureInfo.InvariantCulture)] = props;
}
public void LoadStyle(String style, String key, String value) {
style = style.ToLower(System.Globalization.CultureInfo.InvariantCulture);
Hashtable props = (Hashtable)classMap[style];
if (props == null) {
props = new Hashtable();
classMap[style] = props;
}
props[key] = value;
}
public void LoadTagStyle(String tag, Hashtable props) {
tagMap[tag.ToLower(System.Globalization.CultureInfo.InvariantCulture)] = props;
}
public void LoadTagStyle(String tag, String key, String value) {
tag = tag.ToLower(System.Globalization.CultureInfo.InvariantCulture);
Hashtable props = (Hashtable)tagMap[tag];
if (props == null) {
props = new Hashtable();
tagMap[tag] = props;
}
props[key] = value;
}
}
using System;
using System.Collections;
/*
* Copyright 2004 Paulo Soares
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (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.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.html.simpleparser {
public class StyleSheet {
public Hashtable classMap = new Hashtable();
public Hashtable tagMap = new Hashtable();
/** Creates a new instance of StyleSheet */
public StyleSheet() {
}
public void ApplyStyle(String tag, Hashtable props) {
Hashtable map = (Hashtable)tagMap[tag.ToLower(System.Globalization.CultureInfo.InvariantCulture)];
Hashtable temp;
if (map != null) {
temp = new Hashtable(map);
foreach (DictionaryEntry dc in props)
temp[dc.Key] = dc.Value;
foreach (DictionaryEntry dc in temp)
props[dc.Key] = dc.Value;
}
String cm = (String)props[Markup.HTML_ATTR_CSS_CLASS];
if (cm == null)
return;
map = (Hashtable)classMap[cm.ToLower(System.Globalization.CultureInfo.InvariantCulture)];
if (map == null)
return;
props.Remove(Markup.HTML_ATTR_CSS_CLASS);
temp = new Hashtable(map);
foreach (DictionaryEntry dc in props)
temp[dc.Key] = dc.Value;
foreach (DictionaryEntry dc in temp)
props[dc.Key] = dc.Value;
}
private void ApplyMap(Hashtable map, Hashtable props) {
}
public void LoadStyle(String style, Hashtable props) {
classMap[style.ToLower(System.Globalization.CultureInfo.InvariantCulture)] = props;
}
public void LoadStyle(String style, String key, String value) {
style = style.ToLower(System.Globalization.CultureInfo.InvariantCulture);
Hashtable props = (Hashtable)classMap[style];
if (props == null) {
props = new Hashtable();
classMap[style] = props;
}
props[key] = value;
}
public void LoadTagStyle(String tag, Hashtable props) {
tagMap[tag.ToLower(System.Globalization.CultureInfo.InvariantCulture)] = props;
}
public void LoadTagStyle(String tag, String key, String value) {
tag = tag.ToLower(System.Globalization.CultureInfo.InvariantCulture);
Hashtable props = (Hashtable)tagMap[tag];
if (props == null) {
props = new Hashtable();
tagMap[tag] = props;
}
props[key] = value;
}
}
}

View File

@ -105,6 +105,18 @@ public class PdfArray : PdfObject {
Add(values);
}
/**
* Constructs a PdfArray with the elements of an ArrayList.
* Throws a ClassCastException if the ArrayList contains something
* that isn't a PdfObject.
* @param l an ArrayList with PdfObjects
* @since 2.1.3
*/
public PdfArray(ArrayList l) : this() {
foreach (PdfObject o in l)
Add(o);
}
/**
* Constructs an <CODE>PdfArray</CODE>-object, containing all the <CODE>PdfObject</CODE>s in a given <CODE>PdfArray</CODE>.
*

View File

@ -478,7 +478,7 @@ namespace iTextSharp.text.pdf {
* Getter for property table.
* @return Value of property table.
*/
internal PdfPTable Table {
public PdfPTable Table {
get {
return table;
}

View File

@ -137,7 +137,7 @@ namespace iTextSharp.text.pdf {
PdfEncryption crypto = null;
if (writer != null)
crypto = writer.Encryption;
if (crypto != null) {
if (crypto != null && !crypto.IsEmbeddedFilesOnly()) {
b = crypto.EncryptByteArray(b);
}
if (hexWriting) {

View File

@ -4,244 +4,245 @@ using System.Text;
using System.Collections;
using System.util;
using iTextSharp.text.xml.simpleparser;
/*
* Copyright 2005 by Paulo Soares.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (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.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.pdf.hyphenation {
/** Parses the xml hyphenation pattern.
*
* @author Paulo Soares (psoares@consiste.pt)
*/
public class SimplePatternParser : ISimpleXMLDocHandler {
internal int currElement;
internal IPatternConsumer consumer;
internal StringBuilder token;
internal ArrayList exception;
internal char hyphenChar;
internal const int ELEM_CLASSES = 1;
internal const int ELEM_EXCEPTIONS = 2;
internal const int ELEM_PATTERNS = 3;
internal const int ELEM_HYPHEN = 4;
/** Creates a new instance of PatternParser2 */
public SimplePatternParser() {
token = new StringBuilder();
hyphenChar = '-'; // default
}
public void Parse(Stream stream, IPatternConsumer consumer) {
this.consumer = consumer;
try {
SimpleXMLParser.Parse(this, stream);
}
finally {
try{stream.Close();}catch{}
}
}
protected static String GetPattern(String word) {
StringBuilder pat = new StringBuilder();
int len = word.Length;
for (int i = 0; i < len; i++) {
if (!char.IsDigit(word[i])) {
pat.Append(word[i]);
}
}
return pat.ToString();
}
protected ArrayList NormalizeException(ArrayList ex) {
ArrayList res = new ArrayList();
for (int i = 0; i < ex.Count; i++) {
Object item = ex[i];
if (item is String) {
String str = (String)item;
StringBuilder buf = new StringBuilder();
for (int j = 0; j < str.Length; j++) {
char c = str[j];
if (c != hyphenChar) {
buf.Append(c);
} else {
res.Add(buf.ToString());
buf.Length = 0;
char[] h = new char[1];
h[0] = hyphenChar;
// we use here hyphenChar which is not necessarily
// the one to be printed
res.Add(new Hyphen(new String(h), null, null));
}
}
if (buf.Length > 0) {
res.Add(buf.ToString());
}
} else {
res.Add(item);
}
}
return res;
}
protected String GetExceptionWord(ArrayList ex) {
StringBuilder res = new StringBuilder();
for (int i = 0; i < ex.Count; i++) {
Object item = ex[i];
if (item is String) {
res.Append((String)item);
} else {
if (((Hyphen)item).noBreak != null) {
res.Append(((Hyphen)item).noBreak);
}
}
}
return res.ToString();
}
protected static String GetInterletterValues(String pat) {
StringBuilder il = new StringBuilder();
String word = pat + "a"; // add dummy letter to serve as sentinel
int len = word.Length;
for (int i = 0; i < len; i++) {
char c = word[i];
if (char.IsDigit(c)) {
il.Append(c);
i++;
} else {
il.Append('0');
}
}
return il.ToString();
}
public void EndDocument() {
}
public void EndElement(String tag) {
if (token.Length > 0) {
String word = token.ToString();
switch (currElement) {
case ELEM_CLASSES:
consumer.AddClass(word);
break;
case ELEM_EXCEPTIONS:
exception.Add(word);
exception = NormalizeException(exception);
consumer.AddException(GetExceptionWord(exception),
(ArrayList)exception.Clone());
break;
case ELEM_PATTERNS:
consumer.AddPattern(GetPattern(word),
GetInterletterValues(word));
break;
case ELEM_HYPHEN:
// nothing to do
break;
}
if (currElement != ELEM_HYPHEN) {
token.Length = 0;
}
}
if (currElement == ELEM_HYPHEN) {
currElement = ELEM_EXCEPTIONS;
} else {
currElement = 0;
}
}
public void StartDocument() {
}
public void StartElement(String tag, Hashtable h) {
if (tag.Equals("hyphen-char")) {
String hh = (String)h["value"];
if (hh != null && hh.Length == 1) {
hyphenChar = hh[0];
}
} else if (tag.Equals("classes")) {
currElement = ELEM_CLASSES;
} else if (tag.Equals("patterns")) {
currElement = ELEM_PATTERNS;
} else if (tag.Equals("exceptions")) {
currElement = ELEM_EXCEPTIONS;
exception = new ArrayList();
} else if (tag.Equals("hyphen")) {
if (token.Length > 0) {
exception.Add(token.ToString());
}
exception.Add(new Hyphen((String)h["pre"],
(String)h["no"],
(String)h["post"]));
currElement = ELEM_HYPHEN;
}
token.Length = 0;
}
public void Text(String str) {
StringTokenizer tk = new StringTokenizer(str);
while (tk.HasMoreTokens()) {
String word = tk.NextToken();
// System.out.Println("\"" + word + "\"");
switch (currElement) {
case ELEM_CLASSES:
consumer.AddClass(word);
break;
case ELEM_EXCEPTIONS:
exception.Add(word);
exception = NormalizeException(exception);
consumer.AddException(GetExceptionWord(exception),
(ArrayList)exception.Clone());
exception.Clear();
break;
case ELEM_PATTERNS:
consumer.AddPattern(GetPattern(word),
GetInterletterValues(word));
break;
}
}
}
}
}
using iTextSharp.text.html;
/*
* Copyright 2005 by Paulo Soares.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (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.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.pdf.hyphenation {
/** Parses the xml hyphenation pattern.
*
* @author Paulo Soares (psoares@consiste.pt)
*/
public class SimplePatternParser : ISimpleXMLDocHandler {
internal int currElement;
internal IPatternConsumer consumer;
internal StringBuilder token;
internal ArrayList exception;
internal char hyphenChar;
internal const int ELEM_CLASSES = 1;
internal const int ELEM_EXCEPTIONS = 2;
internal const int ELEM_PATTERNS = 3;
internal const int ELEM_HYPHEN = 4;
/** Creates a new instance of PatternParser2 */
public SimplePatternParser() {
token = new StringBuilder();
hyphenChar = '-'; // default
}
public void Parse(Stream stream, IPatternConsumer consumer) {
this.consumer = consumer;
try {
SimpleXMLParser.Parse(this, stream);
}
finally {
try{stream.Close();}catch{}
}
}
protected static String GetPattern(String word) {
StringBuilder pat = new StringBuilder();
int len = word.Length;
for (int i = 0; i < len; i++) {
if (!char.IsDigit(word[i])) {
pat.Append(word[i]);
}
}
return pat.ToString();
}
protected ArrayList NormalizeException(ArrayList ex) {
ArrayList res = new ArrayList();
for (int i = 0; i < ex.Count; i++) {
Object item = ex[i];
if (item is String) {
String str = (String)item;
StringBuilder buf = new StringBuilder();
for (int j = 0; j < str.Length; j++) {
char c = str[j];
if (c != hyphenChar) {
buf.Append(c);
} else {
res.Add(buf.ToString());
buf.Length = 0;
char[] h = new char[1];
h[0] = hyphenChar;
// we use here hyphenChar which is not necessarily
// the one to be printed
res.Add(new Hyphen(new String(h), null, null));
}
}
if (buf.Length > 0) {
res.Add(buf.ToString());
}
} else {
res.Add(item);
}
}
return res;
}
protected String GetExceptionWord(ArrayList ex) {
StringBuilder res = new StringBuilder();
for (int i = 0; i < ex.Count; i++) {
Object item = ex[i];
if (item is String) {
res.Append((String)item);
} else {
if (((Hyphen)item).noBreak != null) {
res.Append(((Hyphen)item).noBreak);
}
}
}
return res.ToString();
}
protected static String GetInterletterValues(String pat) {
StringBuilder il = new StringBuilder();
String word = pat + "a"; // add dummy letter to serve as sentinel
int len = word.Length;
for (int i = 0; i < len; i++) {
char c = word[i];
if (char.IsDigit(c)) {
il.Append(c);
i++;
} else {
il.Append('0');
}
}
return il.ToString();
}
public void EndDocument() {
}
public void EndElement(String tag) {
if (token.Length > 0) {
String word = token.ToString();
switch (currElement) {
case ELEM_CLASSES:
consumer.AddClass(word);
break;
case ELEM_EXCEPTIONS:
exception.Add(word);
exception = NormalizeException(exception);
consumer.AddException(GetExceptionWord(exception),
(ArrayList)exception.Clone());
break;
case ELEM_PATTERNS:
consumer.AddPattern(GetPattern(word),
GetInterletterValues(word));
break;
case ELEM_HYPHEN:
// nothing to do
break;
}
if (currElement != ELEM_HYPHEN) {
token.Length = 0;
}
}
if (currElement == ELEM_HYPHEN) {
currElement = ELEM_EXCEPTIONS;
} else {
currElement = 0;
}
}
public void StartDocument() {
}
public void StartElement(String tag, Hashtable h) {
if (tag.Equals("hyphen-char")) {
String hh = (String)h["value"];
if (hh != null && hh.Length == 1) {
hyphenChar = hh[0];
}
} else if (tag.Equals("classes")) {
currElement = ELEM_CLASSES;
} else if (tag.Equals("patterns")) {
currElement = ELEM_PATTERNS;
} else if (tag.Equals("exceptions")) {
currElement = ELEM_EXCEPTIONS;
exception = new ArrayList();
} else if (tag.Equals("hyphen")) {
if (token.Length > 0) {
exception.Add(token.ToString());
}
exception.Add(new Hyphen((String)h[HtmlTags.PRE],
(String)h["no"],
(String)h["post"]));
currElement = ELEM_HYPHEN;
}
token.Length = 0;
}
public void Text(String str) {
StringTokenizer tk = new StringTokenizer(str);
while (tk.HasMoreTokens()) {
String word = tk.NextToken();
// System.out.Println("\"" + word + "\"");
switch (currElement) {
case ELEM_CLASSES:
consumer.AddClass(word);
break;
case ELEM_EXCEPTIONS:
exception.Add(word);
exception = NormalizeException(exception);
consumer.AddException(GetExceptionWord(exception),
(ArrayList)exception.Clone());
exception.Clear();
break;
case ELEM_PATTERNS:
consumer.AddPattern(GetPattern(word),
GetInterletterValues(word));
break;
}
}
}
}
}