Support for characters below 32 (not supported as text or

entities by XML)
Support for symbol font


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@41707 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Julian Smart 2006-10-08 14:01:42 +00:00
parent 27507b618a
commit 7b907278ef

View File

@ -144,6 +144,30 @@ bool wxRichTextXMLHandler::ImportXML(wxRichTextBuffer* buffer, wxXmlNode* node)
para->AppendChild(textObject); para->AppendChild(textObject);
} }
else if (childName == wxT("symbol"))
{
// This is a symbol that XML can't read in the normal way
wxString text;
wxXmlNode* textChild = child->GetChildren();
while (textChild)
{
if (textChild->GetType() == wxXML_TEXT_NODE ||
textChild->GetType() == wxXML_CDATA_SECTION_NODE)
{
wxString text2 = textChild->GetContent();
text += text2;
}
textChild = textChild->GetNext();
}
wxString actualText;
actualText << (wxChar) wxAtoi(text);
wxRichTextPlainText* textObject = new wxRichTextPlainText(actualText, para);
GetStyle(textObject->GetAttributes(), child, false);
para->AppendChild(textObject);
}
else if (childName == wxT("image")) else if (childName == wxT("image"))
{ {
int imageType = wxBITMAP_TYPE_PNG; int imageType = wxBITMAP_TYPE_PNG;
@ -337,6 +361,16 @@ static void OutputStringEnt(wxOutputStream& stream, const wxString& str,
} }
last = i + 1; last = i + 1;
} }
else if (c > 127)
{
OutputString(stream, str.Mid(last, i - last), convMem, convFile);
wxString s(wxT("&#"));
s << (int) c;
s << wxT(";");
OutputString(stream, s, NULL, NULL);
last = i + 1;
}
} }
OutputString(stream, str.Mid(last, i - last), convMem, convFile); OutputString(stream, str.Mid(last, i - last), convMem, convFile);
} }
@ -451,27 +485,82 @@ bool wxRichTextXMLHandler::ExportXML(wxOutputStream& stream, wxMBConv* convMem,
objectName = wxT("image"); objectName = wxT("image");
else else
objectName = wxT("object"); objectName = wxT("object");
bool terminateTag = true;
if (obj.IsKindOf(CLASSINFO(wxRichTextPlainText))) if (obj.IsKindOf(CLASSINFO(wxRichTextPlainText)))
{ {
wxRichTextPlainText& text = (wxRichTextPlainText&) obj; wxRichTextPlainText& textObj = (wxRichTextPlainText&) obj;
OutputIndentation(stream, indent);
OutputString(stream, wxT("<") + objectName, convMem, convFile);
wxString style = CreateStyle(obj.GetAttributes(), false); wxString style = CreateStyle(obj.GetAttributes(), false);
OutputString(stream, style + wxT(">"), convMem, convFile); int i;
int last = 0;
wxString str = text.GetText(); const wxString& text = textObj.GetText();
if (!str.empty() && (str[0] == wxT(' ') || str[str.length()-1] == wxT(' '))) int len = (int) text.Length();
for (i = 0; i < len; i++)
{ {
OutputString(stream, wxT("\""), convMem, convFile); int c = (int) text[i];
OutputStringEnt(stream, str, convMem, convFile); if (c < 32 && c != 9 && c != 10 && c != 13)
OutputString(stream, wxT("\""), convMem, convFile); {
if (i > 0)
{
OutputIndentation(stream, indent);
OutputString(stream, wxT("<") + objectName, convMem, convFile);
OutputString(stream, style + wxT(">"), convMem, convFile);
wxString fragment(text.Mid(last, i-last));
if (!fragment.empty() && (fragment[0] == wxT(' ') || fragment[fragment.length()-1] == wxT(' ')))
{
OutputString(stream, wxT("\""), convMem, convFile);
OutputStringEnt(stream, fragment, convMem, convFile);
OutputString(stream, wxT("\""), convMem, convFile);
}
else
OutputStringEnt(stream, fragment, convMem, convFile);
OutputString(stream, wxT("</text>"), convMem, convFile);
}
// Output this character as a number in a separate tag, because XML can't cope
// with entities below 32 except for 9, 10 and 13
last = i + 1;
OutputIndentation(stream, indent);
OutputString(stream, wxT("<symbol"), convMem, convFile);
OutputString(stream, style + wxT(">"), convMem, convFile);
OutputString(stream, wxString::Format(wxT("%d"), c), convMem, convFile);
OutputString(stream, wxT("</symbol>"), convMem, convFile);
}
}
wxString fragment;
if (last == 0)
fragment = text;
else
fragment = text.Mid(last, i-last);
if (last < len)
{
OutputIndentation(stream, indent);
OutputString(stream, wxT("<") + objectName, convMem, convFile);
OutputString(stream, style + wxT(">"), convMem, convFile);
if (!fragment.empty() && (fragment[0] == wxT(' ') || fragment[fragment.length()-1] == wxT(' ')))
{
OutputString(stream, wxT("\""), convMem, convFile);
OutputStringEnt(stream, fragment, convMem, convFile);
OutputString(stream, wxT("\""), convMem, convFile);
}
else
OutputStringEnt(stream, fragment, convMem, convFile);
} }
else else
OutputStringEnt(stream, str, convMem, convFile); terminateTag = false;
} }
else if (obj.IsKindOf(CLASSINFO(wxRichTextImage))) else if (obj.IsKindOf(CLASSINFO(wxRichTextImage)))
{ {
@ -509,7 +598,7 @@ bool wxRichTextXMLHandler::ExportXML(wxOutputStream& stream, wxMBConv* convMem,
isPara = true; isPara = true;
wxString style = CreateStyle(obj.GetAttributes(), isPara); wxString style = CreateStyle(obj.GetAttributes(), isPara);
if (objectName == wxT("paragraphlayout") && ((wxRichTextParagraphLayoutBox&) obj).GetPartialParagraph()) if (objectName == wxT("paragraphlayout") && ((wxRichTextParagraphLayoutBox&) obj).GetPartialParagraph())
style << wxT(" partialparagraph=\"true\""); style << wxT(" partialparagraph=\"true\"");
@ -527,7 +616,8 @@ bool wxRichTextXMLHandler::ExportXML(wxOutputStream& stream, wxMBConv* convMem,
if (objectName != wxT("text")) if (objectName != wxT("text"))
OutputIndentation(stream, indent); OutputIndentation(stream, indent);
OutputString(stream, wxT("</") + objectName + wxT(">"), convMem, convFile); if (terminateTag)
OutputString(stream, wxT("</") + objectName + wxT(">"), convMem, convFile);
return true; return true;
} }
@ -549,7 +639,7 @@ wxString wxRichTextXMLHandler::CreateStyle(const wxTextAttrEx& attr, bool isPara
{ {
if (attr.HasSize()) if (attr.HasSize())
str << wxT(" fontsize=\"") << attr.GetFont().GetPointSize() << wxT("\""); str << wxT(" fontsize=\"") << attr.GetFont().GetPointSize() << wxT("\"");
//if (attr.HasFamily()) //if (attr.HasFamily())
// str << wxT(" fontfamily=\"") << attr.GetFont().GetFamily() << wxT("\""); // str << wxT(" fontfamily=\"") << attr.GetFont().GetFamily() << wxT("\"");
@ -599,11 +689,14 @@ wxString wxRichTextXMLHandler::CreateStyle(const wxTextAttrEx& attr, bool isPara
str << wxT(" bulletnumber=\"") << (int) attr.GetBulletNumber() << wxT("\""); str << wxT(" bulletnumber=\"") << (int) attr.GetBulletNumber() << wxT("\"");
if (attr.HasBulletSymbol()) if (attr.HasBulletSymbol())
str << wxT(" bulletsymbol=\"") << wxString(attr.GetBulletSymbol()) << wxT("\""); {
str << wxT(" bulletsymbol=\"") << (int) (attr.GetBulletSymbol()) << wxT("\"");
str << wxT(" bulletfont=\"") << attr.GetBulletFont() << wxT("\"");
}
if (!attr.GetParagraphStyleName().empty()) if (!attr.GetParagraphStyleName().empty())
str << wxT(" parstyle=\"") << wxString(attr.GetParagraphStyleName()) << wxT("\""); str << wxT(" parstyle=\"") << wxString(attr.GetParagraphStyleName()) << wxT("\"");
if (attr.HasTabs()) if (attr.HasTabs())
{ {
str << wxT(" tabs=\""); str << wxT(" tabs=\"");
@ -614,7 +707,7 @@ wxString wxRichTextXMLHandler::CreateStyle(const wxTextAttrEx& attr, bool isPara
str << wxT(","); str << wxT(",");
str << attr.GetTabs()[i]; str << attr.GetTabs()[i];
} }
str << wxT("\""); str << wxT("\"");
} }
} }
@ -630,7 +723,7 @@ bool wxRichTextXMLHandler::GetStyle(wxTextAttrEx& attr, wxXmlNode* node, bool is
int fontWeight = wxNORMAL; int fontWeight = wxNORMAL;
int fontStyle = wxNORMAL; int fontStyle = wxNORMAL;
bool fontUnderlined = false; bool fontUnderlined = false;
int fontFlags = 0; int fontFlags = 0;
fontFacename = node->GetPropVal(wxT("fontface"), wxEmptyString); fontFacename = node->GetPropVal(wxT("fontface"), wxEmptyString);
@ -669,15 +762,15 @@ bool wxRichTextXMLHandler::GetStyle(wxTextAttrEx& attr, wxXmlNode* node, bool is
fontUnderlined = wxAtoi(value) != 0; fontUnderlined = wxAtoi(value) != 0;
fontFlags |= wxTEXT_ATTR_FONT_UNDERLINE; fontFlags |= wxTEXT_ATTR_FONT_UNDERLINE;
} }
attr.SetFlags(fontFlags); attr.SetFlags(fontFlags);
if (attr.HasFlag(wxTEXT_ATTR_FONT)) if (attr.HasFlag(wxTEXT_ATTR_FONT))
attr.SetFont(* wxTheFontList->FindOrCreateFont(fontSize, fontFamily, fontStyle, fontWeight, fontUnderlined, fontFacename)); attr.SetFont(* wxTheFontList->FindOrCreateFont(fontSize, fontFamily, fontStyle, fontWeight, fontUnderlined, fontFacename));
// Restore correct font flags // Restore correct font flags
attr.SetFlags(fontFlags); attr.SetFlags(fontFlags);
value = node->GetPropVal(wxT("textcolor"), wxEmptyString); value = node->GetPropVal(wxT("textcolor"), wxEmptyString);
if (!value.empty()) if (!value.empty())
{ {
@ -710,7 +803,7 @@ bool wxRichTextXMLHandler::GetStyle(wxTextAttrEx& attr, wxXmlNode* node, bool is
int leftSubIndent = 0; int leftSubIndent = 0;
int leftIndent = 0; int leftIndent = 0;
bool hasLeftIndent = false; bool hasLeftIndent = false;
value = node->GetPropVal(wxT("leftindent"), wxEmptyString); value = node->GetPropVal(wxT("leftindent"), wxEmptyString);
if (!value.empty()) if (!value.empty())
{ {
@ -754,12 +847,16 @@ bool wxRichTextXMLHandler::GetStyle(wxTextAttrEx& attr, wxXmlNode* node, bool is
value = node->GetPropVal(wxT("bulletsymbol"), wxEmptyString); value = node->GetPropVal(wxT("bulletsymbol"), wxEmptyString);
if (!value.empty()) if (!value.empty())
attr.SetBulletSymbol(value[0]); attr.SetBulletSymbol(wxAtoi(value));
value = node->GetPropVal(wxT("bulletfont"), wxEmptyString);
if (!value.empty())
attr.SetBulletFont(value);
value = node->GetPropVal(wxT("parstyle"), wxEmptyString); value = node->GetPropVal(wxT("parstyle"), wxEmptyString);
if (!value.empty()) if (!value.empty())
attr.SetParagraphStyleName(value); attr.SetParagraphStyleName(value);
value = node->GetPropVal(wxT("tabs"), wxEmptyString); value = node->GetPropVal(wxT("tabs"), wxEmptyString);
if (!value.empty()) if (!value.empty())
{ {
@ -782,3 +879,4 @@ bool wxRichTextXMLHandler::GetStyle(wxTextAttrEx& attr, wxXmlNode* node, bool is
#endif #endif
// wxUSE_RICHTEXT && wxUSE_XML // wxUSE_RICHTEXT && wxUSE_XML