RTFTranslator: style clean up
This commit is contained in:
@@ -5,14 +5,17 @@
|
|||||||
|
|
||||||
|
|
||||||
#include "RTFTranslator.h"
|
#include "RTFTranslator.h"
|
||||||
#include "ConfigView.h"
|
|
||||||
#include "RTF.h"
|
#include <stdio.h>
|
||||||
#include "convert.h"
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include <Catalog.h>
|
#include <Catalog.h>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
#include "ConfigView.h"
|
||||||
#include <string.h>
|
#include "convert.h"
|
||||||
|
#include "RTF.h"
|
||||||
|
|
||||||
|
|
||||||
#undef B_TRANSLATION_CONTEXT
|
#undef B_TRANSLATION_CONTEXT
|
||||||
#define B_TRANSLATION_CONTEXT "RTFTranslator"
|
#define B_TRANSLATION_CONTEXT "RTFTranslator"
|
||||||
|
|||||||
@@ -5,28 +5,30 @@
|
|||||||
|
|
||||||
|
|
||||||
#include "convert.h"
|
#include "convert.h"
|
||||||
#include "Stack.h"
|
|
||||||
|
|
||||||
#include <TranslatorFormats.h>
|
#include <algorithm>
|
||||||
|
#include <set>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include <Application.h>
|
#include <Application.h>
|
||||||
#include <TextView.h>
|
|
||||||
#include <TypeConstants.h>
|
|
||||||
#include <ByteOrder.h>
|
#include <ByteOrder.h>
|
||||||
#include <File.h>
|
#include <File.h>
|
||||||
#include <Font.h>
|
#include <Font.h>
|
||||||
|
#include <fs_attr.h>
|
||||||
|
#include <TextView.h>
|
||||||
|
#include <TranslatorFormats.h>
|
||||||
|
#include <TypeConstants.h>
|
||||||
|
|
||||||
#include <AutoDeleter.h>
|
#include <AutoDeleter.h>
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include "Stack.h"
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <algorithm>
|
|
||||||
|
|
||||||
#include <set>
|
|
||||||
#include <fs_attr.h>
|
|
||||||
#define READ_BUFFER_SIZE 2048
|
#define READ_BUFFER_SIZE 2048
|
||||||
|
|
||||||
|
|
||||||
struct conversion_context {
|
struct conversion_context {
|
||||||
conversion_context()
|
conversion_context()
|
||||||
{
|
{
|
||||||
@@ -483,7 +485,8 @@ TextOutput::Command(RTF::Command *command)
|
|||||||
// missing font info will be replaced by the default font
|
// missing font info will be replaced by the default font
|
||||||
|
|
||||||
RTF::Command *info;
|
RTF::Command *info;
|
||||||
for (int32 index = 0; (info = fonts->FindDefinition("f", index)) != NULL; index++) {
|
for (int32 index = 0; (info = fonts->FindDefinition("f", index))
|
||||||
|
!= NULL; index++) {
|
||||||
if (info->Option() != command->Option())
|
if (info->Option() != command->Option())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
@@ -672,9 +675,9 @@ status_t convert_styled_text_to_rtf(
|
|||||||
|
|
||||||
// Read STXT and TEXT headers
|
// Read STXT and TEXT headers
|
||||||
if (source->Read(&stxtheader, kstxtsize) != kstxtsize)
|
if (source->Read(&stxtheader, kstxtsize) != kstxtsize)
|
||||||
return B_ERROR;
|
return B_ERROR;
|
||||||
if (source->Read(&txtheader, ktxtsize) != ktxtsize ||
|
if (source->Read(&txtheader, ktxtsize) != ktxtsize
|
||||||
swap_data(B_UINT32_TYPE, &txtheader,
|
|| swap_data(B_UINT32_TYPE, &txtheader,
|
||||||
sizeof(TranslatorStyledTextTextHeader),
|
sizeof(TranslatorStyledTextTextHeader),
|
||||||
B_SWAP_BENDIAN_TO_HOST) != B_OK)
|
B_SWAP_BENDIAN_TO_HOST) != B_OK)
|
||||||
return B_ERROR;
|
return B_ERROR;
|
||||||
@@ -699,38 +702,29 @@ status_t convert_styled_text_to_rtf(
|
|||||||
|
|
||||||
BString rtfFile =
|
BString rtfFile =
|
||||||
"{\\rtf1\\ansi";
|
"{\\rtf1\\ansi";
|
||||||
|
|
||||||
|
|
||||||
ssize_t read = 0;
|
ssize_t read = 0;
|
||||||
TranslatorStyledTextStyleHeader stylHeader;
|
TranslatorStyledTextStyleHeader stylHeader;
|
||||||
read = source->Read(buffer, sizeof(stylHeader));
|
read = source->Read(buffer, sizeof(stylHeader));
|
||||||
|
|
||||||
if (read < 0)
|
if (read < 0)
|
||||||
{
|
|
||||||
return B_ERROR;
|
return B_ERROR;
|
||||||
}
|
|
||||||
|
|
||||||
if (read != sizeof(stylHeader) && read != 0)
|
if (read != sizeof(stylHeader) && read != 0)
|
||||||
{
|
|
||||||
return B_NO_TRANSLATOR;
|
return B_NO_TRANSLATOR;
|
||||||
}
|
|
||||||
|
|
||||||
if (read == sizeof(stylHeader)) // There is a STYL section
|
if (read == sizeof(stylHeader)) { // There is a STYL section
|
||||||
{
|
|
||||||
memcpy(&stylHeader, buffer, sizeof(stylHeader));
|
memcpy(&stylHeader, buffer, sizeof(stylHeader));
|
||||||
if (swap_data(B_UINT32_TYPE, &stylHeader, sizeof(stylHeader),
|
if (swap_data(B_UINT32_TYPE, &stylHeader, sizeof(stylHeader),
|
||||||
B_SWAP_BENDIAN_TO_HOST) != B_OK)
|
B_SWAP_BENDIAN_TO_HOST) != B_OK) {
|
||||||
{
|
|
||||||
return B_ERROR;
|
return B_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stylHeader.header.magic != 'STYL'
|
if (stylHeader.header.magic != 'STYL'
|
||||||
|| stylHeader.header.header_size != sizeof(stylHeader))
|
|| stylHeader.header.header_size != sizeof(stylHeader)) {
|
||||||
{
|
|
||||||
return B_NO_TRANSLATOR;
|
return B_NO_TRANSLATOR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
uint8 unflattened[stylHeader.header.data_size];
|
uint8 unflattened[stylHeader.header.data_size];
|
||||||
source->Read(unflattened, stylHeader.header.data_size);
|
source->Read(unflattened, stylHeader.header.data_size);
|
||||||
text_run_array *styles = BTextView::UnflattenRunArray(unflattened);
|
text_run_array *styles = BTextView::UnflattenRunArray(unflattened);
|
||||||
@@ -741,8 +735,7 @@ status_t convert_styled_text_to_rtf(
|
|||||||
std::set<BString> fontTable;
|
std::set<BString> fontTable;
|
||||||
|
|
||||||
font_family out;
|
font_family out;
|
||||||
for (int i = 0; i < styles->count; i++)
|
for (int i = 0; i < styles->count; i++) {
|
||||||
{
|
|
||||||
colorTable.insert(styles->runs[i].color);
|
colorTable.insert(styles->runs[i].color);
|
||||||
styles->runs[i].font.GetFamilyAndStyle(&out, NULL);
|
styles->runs[i].font.GetFamilyAndStyle(&out, NULL);
|
||||||
fontTable.insert(BString(out));
|
fontTable.insert(BString(out));
|
||||||
@@ -753,16 +746,14 @@ status_t convert_styled_text_to_rtf(
|
|||||||
uint32 count = 0;
|
uint32 count = 0;
|
||||||
|
|
||||||
rtfFile << "{\\fonttbl";
|
rtfFile << "{\\fonttbl";
|
||||||
for (it = fontTable.begin(); it != fontTable.end(); it++)
|
for (it = fontTable.begin(); it != fontTable.end(); it++) {
|
||||||
{
|
|
||||||
rtfFile << "{\\f" << count << " " << *it << ";}";
|
rtfFile << "{\\f" << count << " " << *it << ";}";
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
rtfFile << "}{\\colortbl";
|
rtfFile << "}{\\colortbl";
|
||||||
|
|
||||||
std::set<rgb_color, color_compare>::iterator cit;
|
std::set<rgb_color, color_compare>::iterator cit;
|
||||||
for (cit = colorTable.begin(); cit != colorTable.end(); cit++)
|
for (cit = colorTable.begin(); cit != colorTable.end(); cit++) {
|
||||||
{
|
|
||||||
rtfFile << "\\red" << cit->red
|
rtfFile << "\\red" << cit->red
|
||||||
<< "\\green" << cit->green
|
<< "\\green" << cit->green
|
||||||
<< "\\blue" << cit->blue
|
<< "\\blue" << cit->blue
|
||||||
@@ -771,8 +762,7 @@ status_t convert_styled_text_to_rtf(
|
|||||||
rtfFile << "}";
|
rtfFile << "}";
|
||||||
|
|
||||||
// Now we put out the actual text with styling information run by run
|
// Now we put out the actual text with styling information run by run
|
||||||
for (int i = 0; i < styles->count; i++)
|
for (int i = 0; i < styles->count; i++) {
|
||||||
{
|
|
||||||
// Find font and color indices
|
// Find font and color indices
|
||||||
styles->runs[i].font.GetFamilyAndStyle(&out, NULL);
|
styles->runs[i].font.GetFamilyAndStyle(&out, NULL);
|
||||||
int fontIndex = std::distance(fontTable.begin(),
|
int fontIndex = std::distance(fontTable.begin(),
|
||||||
@@ -784,17 +774,11 @@ status_t convert_styled_text_to_rtf(
|
|||||||
// Apply various font styles
|
// Apply various font styles
|
||||||
uint16 fontFace = styles->runs[i].font.Face();
|
uint16 fontFace = styles->runs[i].font.Face();
|
||||||
if (fontFace & B_ITALIC_FACE)
|
if (fontFace & B_ITALIC_FACE)
|
||||||
{
|
|
||||||
rtfFile << "\\i";
|
rtfFile << "\\i";
|
||||||
}
|
|
||||||
if (fontFace & B_UNDERSCORE_FACE)
|
if (fontFace & B_UNDERSCORE_FACE)
|
||||||
{
|
|
||||||
rtfFile << "\\ul";
|
rtfFile << "\\ul";
|
||||||
}
|
|
||||||
if (fontFace & B_BOLD_FACE)
|
if (fontFace & B_BOLD_FACE)
|
||||||
{
|
|
||||||
rtfFile << "\\b";
|
rtfFile << "\\b";
|
||||||
}
|
|
||||||
|
|
||||||
// RTF font size unit is half-points, but BFont::Size() returns
|
// RTF font size unit is half-points, but BFont::Size() returns
|
||||||
// points
|
// points
|
||||||
@@ -802,12 +786,9 @@ status_t convert_styled_text_to_rtf(
|
|||||||
<< static_cast<int>(styles->runs[i].font.Size() * 2);
|
<< static_cast<int>(styles->runs[i].font.Size() * 2);
|
||||||
|
|
||||||
int length;
|
int length;
|
||||||
if (i < styles->count - 1)
|
if (i < styles->count - 1) {
|
||||||
{
|
|
||||||
length = styles->runs[i + 1].offset - styles->runs[i].offset;
|
length = styles->runs[i + 1].offset - styles->runs[i].offset;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
length = plainText.Length() - styles->runs[i].offset;
|
length = plainText.Length() - styles->runs[i].offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -824,9 +805,8 @@ status_t convert_styled_text_to_rtf(
|
|||||||
delete styles;
|
delete styles;
|
||||||
|
|
||||||
rtfFile << "}";
|
rtfFile << "}";
|
||||||
}
|
} else {
|
||||||
else // There is no STYL section
|
// There is no STYL section
|
||||||
{
|
|
||||||
// Just use a generic preamble
|
// Just use a generic preamble
|
||||||
rtfFile << "{\\fonttbl\\f0 DejaVu Sans;}\\f0\\pard " << plainText
|
rtfFile << "{\\fonttbl\\f0 DejaVu Sans;}\\f0\\pard " << plainText
|
||||||
<< "}";
|
<< "}";
|
||||||
|
|||||||
Reference in New Issue
Block a user