Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions nrtftree-examples/simple-demo/Demo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -435,17 +435,20 @@ 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();

Console.WriteLine("Processing...");
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.");

Expand Down
9 changes: 7 additions & 2 deletions nrtftree-examples/simple-demo/Rtf2Html.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public class Rtf2Html
/// </summary>
private Format _currentFormat;

private Encoding _currentEncoding = Encoding.Default;

/// <summary>
/// Formato de texto ya escrito en el c�digo HTML
/// </summary>
Expand Down Expand Up @@ -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;

Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions nrtftree-examples/simple-demo/testdocs/test-doc4.rtf
Original file line number Diff line number Diff line change
@@ -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}
121 changes: 121 additions & 0 deletions nrtftree-library/CharSetConvertor.cs
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
********************************************************************************/

/********************************************************************************
* 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
{
/// <summary>
/// A converter between charset and codepage.
/// Based on RTF Specification version 1.9.1.
/// </summary>
public static class CharSetConvertor
{
/// <summary>
/// Get code page code.
/// </summary>
/// <param name="charSet"></param>
/// <param name="defaultCodePage"></param>
/// <returns></returns>
public static int ToCodePage(int charSet, int defaultCodePage)
{
return Convert(CodeTableColumn.CharSet, charSet, defaultCodePage);
}

/// <summary>
/// Get character set code.
/// </summary>
/// <param name="codePage"></param>
/// <param name="defaultCharSet"></param>
/// <returns></returns>
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
};
}

}
}
66 changes: 66 additions & 0 deletions nrtftree-library/RtfFont.cs
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
********************************************************************************/

/********************************************************************************
* 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
{
/// <summary>
/// An item of font table.
/// </summary>
public class RtfFont
{
/// <summary>
/// Font name
/// </summary>
public string Name{get; private set;}

/// <summary>
/// Code page
/// </summary>
public int CodePage{get; private set;}

/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <param name="codePage"></param>
public RtfFont(string name, int codePage)
{
this.Name = name;
this.CodePage = codePage;
}

}

}
}
34 changes: 24 additions & 10 deletions nrtftree-library/RtfFontTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

using System.Collections.Generic;
using System.Collections;
using System;
using System.Text;

namespace Net.Sgoliver.NRtfTree
{
Expand All @@ -41,14 +43,14 @@ public class RtfFontTable
/// <summary>
/// Lista interna de fuentes.
/// </summary>
private Dictionary<int,string> fonts;
private Dictionary<int,RtfFont> fonts;

/// <summary>
/// Constructor de la clase RtfFontTable.
/// </summary>
public RtfFontTable()
{
fonts = new Dictionary<int,string>();
fonts = new Dictionary<int,RtfFont>();
}

/// <summary>
Expand All @@ -57,7 +59,7 @@ public RtfFontTable()
/// <param name="name">Nueva fuente a insertar.</param>
public void AddFont(string name)
{
fonts.Add(newFontIndex(),name);
fonts.Add(newFontIndex(), new RtfFont(name, Encoding.Default.CodePage));
}

/// <summary>
Expand All @@ -67,15 +69,25 @@ public void AddFont(string name)
/// <param name="name">Nueva fuente a insertar.</param>
public void AddFont(int index, string name)
{
fonts.Add(index, name);
fonts.Add(index, new RtfFont(name, Encoding.Default.CodePage));
}

/// <summary>
/// Inserta un nueva fuente en la tabla de fuentes.
/// </summary>
/// <param name="index">Indice de la fuente a insertar.</param>
/// <param name="RtfFont">Nueva fuente a insertar.</param>
public void AddFont(int index, RtfFont font)
{
fonts.Add(index, font);
}

/// <summary>
/// Obtiene la fuente n-�sima de la tabla de fuentes.
/// </summary>
/// <param name="index">Indice de la fuente a recuperar.</param>
/// <returns>Fuente n-�sima de la tabla de fuentes.</returns>
public string this[int index]
public RtfFont this[int index]
{
get
{
Expand Down Expand Up @@ -107,9 +119,10 @@ public int IndexOf(string name)
fntIndex.Reset();
while (fntIndex.MoveNext())
{
if (((KeyValuePair<int,string>)fntIndex.Current).Value.Equals(name))
KeyValuePair<int,RtfFont> item = (KeyValuePair<int, RtfFont>)fntIndex.Current;
if(String.Equals(name, ((RtfFont)item.Value).Name, System.StringComparison.OrdinalIgnoreCase))
{
intIndex = (int)((KeyValuePair<int, string>)fntIndex.Current).Key;
intIndex = item.Key;
break;
}
}
Expand All @@ -127,10 +140,11 @@ private int newFontIndex()
IEnumerator fntIndex = fonts.GetEnumerator();

fntIndex.Reset();
while (fntIndex.MoveNext())
while(fntIndex.MoveNext())
{
if ((int)((KeyValuePair<int, string>)fntIndex.Current).Key > intIndex)
intIndex = (int)((KeyValuePair<int, string>)fntIndex.Current).Key;
KeyValuePair<int,RtfFont> item = (KeyValuePair<int, RtfFont>)fntIndex.Current;
if (item.Key > intIndex)
intIndex = item.Key;
}

return (intIndex + 1);
Expand Down
2 changes: 1 addition & 1 deletion nrtftree-library/RtfMerger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 13 additions & 5 deletions nrtftree-library/RtfTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -289,6 +294,9 @@ public RtfColorTable GetColorTable()

i++;
}

if(null == ntc.ChildNodes)
return tablaColores;

//Rellenamos el array de colores
int rojo = 0;
Expand Down
2 changes: 2 additions & 0 deletions nrtftree-library/nrtftree-library.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="CharSetConvertor.cs" />
<Compile Include="RtfFont.cs" />
<Compile Include="ImageNode.cs" />
<Compile Include="InfoGroup.cs" />
<Compile Include="ObjectNode.cs" />
Expand Down