diff --git a/nrtftree-examples/simple-demo/Demo.cs b/nrtftree-examples/simple-demo/Demo.cs index 91c7697..4a814a0 100644 --- a/nrtftree-examples/simple-demo/Demo.cs +++ b/nrtftree-examples/simple-demo/Demo.cs @@ -435,6 +435,7 @@ private static void ConvertToHtml() { RtfTree tree = new RtfTree(); tree.LoadRtfFile("..\\..\\testdocs\\test-doc2.rtf"); + //tree.LoadRtfFile("..\\..\\testdocs\\test-doc4.rtf"); //several character set Rtf2Html rtfToHtml = new Rtf2Html(); @@ -442,10 +443,12 @@ private static void ConvertToHtml() rtfToHtml.IncrustImages = false; string html = rtfToHtml.Convert(tree.Rtf); - StreamWriter sw = new StreamWriter("..\\..\\testdocs\\test.html", false); - sw.Write(html); - sw.Flush(); - sw.Close(); + using(StreamWriter sw = new StreamWriter("..\\..\\testdocs\\test.html", false, Encoding.Default, 10000)) + { + sw.Write(html); + sw.Flush(); + sw.Close(); + } Console.WriteLine("File 'test.html' created."); diff --git a/nrtftree-examples/simple-demo/Rtf2Html.cs b/nrtftree-examples/simple-demo/Rtf2Html.cs index f496f78..906f818 100644 --- a/nrtftree-examples/simple-demo/Rtf2Html.cs +++ b/nrtftree-examples/simple-demo/Rtf2Html.cs @@ -57,6 +57,8 @@ public class Rtf2Html /// private Format _currentFormat; + private Encoding _currentEncoding = Encoding.Default; + /// /// Formato de texto ya escrito en el código HTML /// @@ -339,7 +341,7 @@ private void ProcessChildNodes(RtfNodeCollection nodos, int inicio) if (nodo.NodeKey == "'") //Símbolos especiales, como tildes y "ñ" { - WriteText(Encoding.Default.GetString(new[] { (byte)nodo.Parameter })); + WriteText(_currentEncoding.GetString(new[] { (byte)nodo.Parameter })); } break; @@ -353,7 +355,10 @@ private void ProcessChildNodes(RtfNodeCollection nodos, int inicio) case "f": //Tipo de fuente if (nodo.Parameter < _fontTable.Count) - _currentFormat.FontName = _fontTable[nodo.Parameter]; + { + _currentFormat.FontName = _fontTable[nodo.Parameter].Name; + _currentEncoding = Encoding.GetEncoding(_fontTable[nodo.Parameter].CodePage); + } break; case "cf": //Color de fuente diff --git a/nrtftree-examples/simple-demo/testdocs/test-doc4.rtf b/nrtftree-examples/simple-demo/testdocs/test-doc4.rtf new file mode 100644 index 0000000..81488e4 --- /dev/null +++ b/nrtftree-examples/simple-demo/testdocs/test-doc4.rtf @@ -0,0 +1 @@ +{\rtf1\fbidis\ansi\ansicpg1251\deff0\deflang1049{\fonttbl{\f0\fswiss\fprq2\fcharset204{\*\fname Arial;}Arial CYR;}{\f1\froman\fprq2\fcharset204{\*\fname Times New Roman;}Times New Roman CYR;}{\f2\fswiss\fprq2\fcharset0 Arial;}}\viewkind4\uc1\pard\ltrpar\lang2057\f0\fs20 Asdf-\f1\'d4\'fb\'e2\'e0\'b8\f2 -\'c6\'d8\'e6\'f8\f0\fs30\par} \ No newline at end of file diff --git a/nrtftree-library/CharSetConvertor.cs b/nrtftree-library/CharSetConvertor.cs new file mode 100644 index 0000000..8488258 --- /dev/null +++ b/nrtftree-library/CharSetConvertor.cs @@ -0,0 +1,121 @@ +/******************************************************************************** + * This file is part of NRtfTree Library. + * + * NRtfTree Library is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * NRtfTree 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + ********************************************************************************/ + +/******************************************************************************** + * Library: NRtfTree + * Version: v0.4 + * Date: 29/06/2013 + * Copyright: 2006-2013 Salvador Gomez + * Home Page: http://www.sgoliver.net + * GitHub: https://github.com/sgolivernet/nrtftree + * Class: CharSetConvertor + * Description: Tabla de Fuentes de un documento RTF. + * ******************************************************************************/ + +using System; +using System.Collections.Generic; +using System.Collections; + +namespace Net.Sgoliver.NRtfTree +{ + namespace Util + { + /// + /// A converter between charset and codepage. + /// Based on RTF Specification version 1.9.1. + /// + public static class CharSetConvertor + { + /// + /// Get code page code. + /// + /// + /// + /// + public static int ToCodePage(int charSet, int defaultCodePage) + { + return Convert(CodeTableColumn.CharSet, charSet, defaultCodePage); + } + + /// + /// Get character set code. + /// + /// + /// + /// + public static int ToCharSet(int codePage, int defaultCharSet) + { + return Convert(CodeTableColumn.CodePage, codePage, defaultCharSet); + } + + private static int Convert(CodeTableColumn fromType, int fromValue, int defaultValue) + { + int ix_from = (int)fromType; + int ix_to = (int)(fromType == CodeTableColumn.CharSet ? CodeTableColumn.CodePage : CodeTableColumn.CharSet); + for(int i=0; i<_codeTable.GetLength(0); ++i) + { + if(fromValue == _codeTable[i, ix_from]) + return _codeTable[i, ix_to]; + } + return defaultValue; + } + + private enum CodeTableColumn { CharSet=IX_CHARSET, CodePage=IX_CODEPAGE } + private const int IX_CHARSET=0; + private const int IX_CODEPAGE=1; + private static int[,] _codeTable = new int[,] + {//charset codepage Win/Mac name + {0, 1252}, //ANSI + {1, 0}, //Default + {2, 42}, //Symbol + {77, 10000}, //Mac Roman + {78, 10001}, //Mac Shift Jis + {79, 10003}, //Mac Hangul + {80, 10008}, //Mac GB2312 + {81, 10002}, //Mac Big5 + //{82, }, //Mac Johab (old) + {83, 10005}, //Mac Hebrew + {84, 10004}, //Mac Arabic + {85, 10006}, //Mac Greek + {86, 10081}, //Mac Turkish + {87, 10021}, //Mac Thai + {88, 10029}, //Mac East Europe + {89, 10007}, //Mac Russian + {128, 932}, //Shift JIS + {129, 949}, //Hangul + {130, 1361}, //Johab + {134, 936}, //GB2312 + {136, 950}, //Big5 + {161, 1253}, //Greek + {162, 1254}, //Turkish + {163, 1258}, //Vietnamese + {177, 1255}, //Hebrew + {178, 1256}, //Arabic + //{179, }, //Arabic Traditional (old) + //{180, }, //Arabic user (old) + //{181, }, //Hebrew user (old) + {186, 1257}, //Baltic + {204, 1251}, //Russian + {222, 874}, //Thai + {238, 1250}, //Eastern European + {254, 437}, //PC 437 + {255, 850}, //OEM + }; + } + + } +} diff --git a/nrtftree-library/RtfFont.cs b/nrtftree-library/RtfFont.cs new file mode 100644 index 0000000..a2bedbc --- /dev/null +++ b/nrtftree-library/RtfFont.cs @@ -0,0 +1,66 @@ +/******************************************************************************** + * This file is part of NRtfTree Library. + * + * NRtfTree Library is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * NRtfTree 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + ********************************************************************************/ + +/******************************************************************************** + * Library: NRtfTree + * Version: v0.4 + * Date: 29/06/2013 + * Copyright: 2006-2013 Salvador Gomez + * Home Page: http://www.sgoliver.net + * GitHub: https://github.com/sgolivernet/nrtftree + * Class: RtfFont + * Description: Tabla de Fuentes de un documento RTF. + * ******************************************************************************/ + +using System; +using System.Collections.Generic; +using System.Collections; + +namespace Net.Sgoliver.NRtfTree +{ + namespace Util + { + /// + /// An item of font table. + /// + public class RtfFont + { + /// + /// Font name + /// + public string Name{get; private set;} + + /// + /// Code page + /// + public int CodePage{get; private set;} + + /// + /// + /// + /// + /// + public RtfFont(string name, int codePage) + { + this.Name = name; + this.CodePage = codePage; + } + + } + + } +} diff --git a/nrtftree-library/RtfFontTable.cs b/nrtftree-library/RtfFontTable.cs index 144b4b4..20a69a0 100644 --- a/nrtftree-library/RtfFontTable.cs +++ b/nrtftree-library/RtfFontTable.cs @@ -28,6 +28,8 @@ using System.Collections.Generic; using System.Collections; +using System; +using System.Text; namespace Net.Sgoliver.NRtfTree { @@ -41,14 +43,14 @@ public class RtfFontTable /// /// Lista interna de fuentes. /// - private Dictionary fonts; + private Dictionary fonts; /// /// Constructor de la clase RtfFontTable. /// public RtfFontTable() { - fonts = new Dictionary(); + fonts = new Dictionary(); } /// @@ -57,7 +59,7 @@ public RtfFontTable() /// Nueva fuente a insertar. public void AddFont(string name) { - fonts.Add(newFontIndex(),name); + fonts.Add(newFontIndex(), new RtfFont(name, Encoding.Default.CodePage)); } /// @@ -67,7 +69,17 @@ public void AddFont(string name) /// Nueva fuente a insertar. public void AddFont(int index, string name) { - fonts.Add(index, name); + fonts.Add(index, new RtfFont(name, Encoding.Default.CodePage)); + } + + /// + /// Inserta un nueva fuente en la tabla de fuentes. + /// + /// Indice de la fuente a insertar. + /// Nueva fuente a insertar. + public void AddFont(int index, RtfFont font) + { + fonts.Add(index, font); } /// @@ -75,7 +87,7 @@ public void AddFont(int index, string name) /// /// Indice de la fuente a recuperar. /// Fuente n-ésima de la tabla de fuentes. - public string this[int index] + public RtfFont this[int index] { get { @@ -107,9 +119,10 @@ public int IndexOf(string name) fntIndex.Reset(); while (fntIndex.MoveNext()) { - if (((KeyValuePair)fntIndex.Current).Value.Equals(name)) + KeyValuePair item = (KeyValuePair)fntIndex.Current; + if(String.Equals(name, ((RtfFont)item.Value).Name, System.StringComparison.OrdinalIgnoreCase)) { - intIndex = (int)((KeyValuePair)fntIndex.Current).Key; + intIndex = item.Key; break; } } @@ -127,10 +140,11 @@ private int newFontIndex() IEnumerator fntIndex = fonts.GetEnumerator(); fntIndex.Reset(); - while (fntIndex.MoveNext()) + while(fntIndex.MoveNext()) { - if ((int)((KeyValuePair)fntIndex.Current).Key > intIndex) - intIndex = (int)((KeyValuePair)fntIndex.Current).Key; + KeyValuePair item = (KeyValuePair)fntIndex.Current; + if (item.Key > intIndex) + intIndex = item.Key; } return (intIndex + 1); diff --git a/nrtftree-library/RtfMerger.cs b/nrtftree-library/RtfMerger.cs index 8708800..20b2740 100644 --- a/nrtftree-library/RtfMerger.cs +++ b/nrtftree-library/RtfMerger.cs @@ -396,7 +396,7 @@ private void adjustFontRecursive(RtfTreeNode parentNode, RtfFontTable fontDestTb parentNode.ChildNodes[iNdIndex].NodeKey == "af") && parentNode.ChildNodes[iNdIndex].HasParameter) { - parentNode.ChildNodes[iNdIndex].Parameter = getFontID(ref fontDestTbl, fontToCopyTbl[parentNode.ChildNodes[iNdIndex].Parameter]); + parentNode.ChildNodes[iNdIndex].Parameter = getFontID(ref fontDestTbl, fontToCopyTbl[parentNode.ChildNodes[iNdIndex].Parameter].Name); } adjustFontRecursive(parentNode.ChildNodes[iNdIndex], fontDestTbl, fontToCopyTbl); diff --git a/nrtftree-library/RtfTree.cs b/nrtftree-library/RtfTree.cs index 3832a6b..5e1a234 100644 --- a/nrtftree-library/RtfTree.cs +++ b/nrtftree-library/RtfTree.cs @@ -243,17 +243,22 @@ public RtfFontTable GetFontTable() int indiceFuente = -1; string nombreFuente = null; + int codePage = Encoding.Default.CodePage; foreach (RtfTreeNode nodo in fuente.ChildNodes) { - if (nodo.NodeKey == "f") - indiceFuente = nodo.Parameter; - - if (nodo.NodeType == RtfNodeType.Text) + if (nodo.NodeType == RtfNodeType.Keyword) + { + if (nodo.NodeKey == "f") + indiceFuente = nodo.Parameter; + else if(nodo.NodeKey == "fcharset") + codePage = CharSetConvertor.ToCodePage(nodo.Parameter, codePage); + } + else if (nodo.NodeType == RtfNodeType.Text) nombreFuente = nodo.NodeKey.Substring(0, nodo.NodeKey.Length - 1); } - tablaFuentes.AddFont(indiceFuente, nombreFuente); + tablaFuentes.AddFont(indiceFuente, new RtfFont(nombreFuente, codePage)); } return tablaFuentes; @@ -289,6 +294,9 @@ public RtfColorTable GetColorTable() i++; } + + if(null == ntc.ChildNodes) + return tablaColores; //Rellenamos el array de colores int rojo = 0; diff --git a/nrtftree-library/nrtftree-library.csproj b/nrtftree-library/nrtftree-library.csproj index e1eab4f..32e98a7 100644 --- a/nrtftree-library/nrtftree-library.csproj +++ b/nrtftree-library/nrtftree-library.csproj @@ -39,6 +39,8 @@ + +