From 28e5604c86d54a0a3c9f513eae0fea87047078e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Duval?= Date: Sat, 20 Dec 2014 19:21:08 +0100 Subject: [PATCH] RTFTranslator: style clean up --- src/add-ons/translators/rtf/RTFTranslator.cpp | 15 ++-- src/add-ons/translators/rtf/convert.cpp | 76 +++++++------------ 2 files changed, 37 insertions(+), 54 deletions(-) diff --git a/src/add-ons/translators/rtf/RTFTranslator.cpp b/src/add-ons/translators/rtf/RTFTranslator.cpp index 6c9d7854ea..94e832ae19 100644 --- a/src/add-ons/translators/rtf/RTFTranslator.cpp +++ b/src/add-ons/translators/rtf/RTFTranslator.cpp @@ -5,14 +5,17 @@ #include "RTFTranslator.h" -#include "ConfigView.h" -#include "RTF.h" -#include "convert.h" + +#include +#include +#include #include -#include -#include -#include + +#include "ConfigView.h" +#include "convert.h" +#include "RTF.h" + #undef B_TRANSLATION_CONTEXT #define B_TRANSLATION_CONTEXT "RTFTranslator" diff --git a/src/add-ons/translators/rtf/convert.cpp b/src/add-ons/translators/rtf/convert.cpp index f8b8146351..6a6577dd7f 100644 --- a/src/add-ons/translators/rtf/convert.cpp +++ b/src/add-ons/translators/rtf/convert.cpp @@ -5,28 +5,30 @@ #include "convert.h" -#include "Stack.h" -#include +#include +#include +#include +#include +#include #include -#include -#include #include #include #include +#include +#include +#include +#include #include -#include -#include -#include -#include +#include "Stack.h" + -#include -#include #define READ_BUFFER_SIZE 2048 + struct conversion_context { conversion_context() { @@ -483,7 +485,8 @@ TextOutput::Command(RTF::Command *command) // missing font info will be replaced by the default font 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()) continue; @@ -672,9 +675,9 @@ status_t convert_styled_text_to_rtf( // Read STXT and TEXT headers if (source->Read(&stxtheader, kstxtsize) != kstxtsize) - return B_ERROR; - if (source->Read(&txtheader, ktxtsize) != ktxtsize || - swap_data(B_UINT32_TYPE, &txtheader, + return B_ERROR; + if (source->Read(&txtheader, ktxtsize) != ktxtsize + || swap_data(B_UINT32_TYPE, &txtheader, sizeof(TranslatorStyledTextTextHeader), B_SWAP_BENDIAN_TO_HOST) != B_OK) return B_ERROR; @@ -699,38 +702,29 @@ status_t convert_styled_text_to_rtf( BString rtfFile = "{\\rtf1\\ansi"; - ssize_t read = 0; TranslatorStyledTextStyleHeader stylHeader; read = source->Read(buffer, sizeof(stylHeader)); if (read < 0) - { return B_ERROR; - } if (read != sizeof(stylHeader) && read != 0) - { 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)); 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; } if (stylHeader.header.magic != 'STYL' - || stylHeader.header.header_size != sizeof(stylHeader)) - { + || stylHeader.header.header_size != sizeof(stylHeader)) { return B_NO_TRANSLATOR; } - uint8 unflattened[stylHeader.header.data_size]; source->Read(unflattened, stylHeader.header.data_size); text_run_array *styles = BTextView::UnflattenRunArray(unflattened); @@ -741,8 +735,7 @@ status_t convert_styled_text_to_rtf( std::set fontTable; 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); styles->runs[i].font.GetFamilyAndStyle(&out, NULL); fontTable.insert(BString(out)); @@ -753,16 +746,14 @@ status_t convert_styled_text_to_rtf( uint32 count = 0; rtfFile << "{\\fonttbl"; - for (it = fontTable.begin(); it != fontTable.end(); it++) - { + for (it = fontTable.begin(); it != fontTable.end(); it++) { rtfFile << "{\\f" << count << " " << *it << ";}"; count++; } rtfFile << "}{\\colortbl"; std::set::iterator cit; - for (cit = colorTable.begin(); cit != colorTable.end(); cit++) - { + for (cit = colorTable.begin(); cit != colorTable.end(); cit++) { rtfFile << "\\red" << cit->red << "\\green" << cit->green << "\\blue" << cit->blue @@ -771,8 +762,7 @@ status_t convert_styled_text_to_rtf( rtfFile << "}"; // 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 styles->runs[i].font.GetFamilyAndStyle(&out, NULL); int fontIndex = std::distance(fontTable.begin(), @@ -784,17 +774,11 @@ status_t convert_styled_text_to_rtf( // Apply various font styles uint16 fontFace = styles->runs[i].font.Face(); if (fontFace & B_ITALIC_FACE) - { rtfFile << "\\i"; - } if (fontFace & B_UNDERSCORE_FACE) - { rtfFile << "\\ul"; - } if (fontFace & B_BOLD_FACE) - { rtfFile << "\\b"; - } // RTF font size unit is half-points, but BFont::Size() returns // points @@ -802,12 +786,9 @@ status_t convert_styled_text_to_rtf( << static_cast(styles->runs[i].font.Size() * 2); int length; - if (i < styles->count - 1) - { + if (i < styles->count - 1) { length = styles->runs[i + 1].offset - styles->runs[i].offset; - } - else - { + } else { length = plainText.Length() - styles->runs[i].offset; } @@ -824,9 +805,8 @@ status_t convert_styled_text_to_rtf( delete styles; rtfFile << "}"; - } - else // There is no STYL section - { + } else { + // There is no STYL section // Just use a generic preamble rtfFile << "{\\fonttbl\\f0 DejaVu Sans;}\\f0\\pard " << plainText << "}";