From fb11b6e180349570bfdb853478ad59e527ab22bb Mon Sep 17 00:00:00 2001 From: Matthew Wilber Date: Sat, 28 Dec 2002 03:30:18 +0000 Subject: [PATCH] added code so that when text is translated from a plain BFile (with a styles attribute) the STXTTranslator casts the input to a BFile and uses the attributes to write the style information to the output stream git-svn-id: file:///srv/svn/repos/haiku/trunk/current@2308 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- .../stxttranslator/STXTTranslator.cpp | 68 ++++++++++++++++++- .../stxttranslator/STXTTranslator.h | 2 + 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/add-ons/translators/stxttranslator/STXTTranslator.cpp b/src/add-ons/translators/stxttranslator/STXTTranslator.cpp index a5620a9791..e04f2d95f9 100644 --- a/src/add-ons/translators/stxttranslator/STXTTranslator.cpp +++ b/src/add-ons/translators/stxttranslator/STXTTranslator.cpp @@ -544,6 +544,36 @@ output_headers(BPositionIO *outDestination, uint32 text_data_size) return result; } +status_t +output_styles(BPositionIO *outDestination, uint32 text_size, + uint8 *pflatRunArray, ssize_t data_size) +{ + uint8 buffer[20]; + + // output STYL header + TranslatorStyledTextStyleHeader stylheader; + stylheader.header.magic = 'STYL'; + stylheader.header.header_size = + sizeof(TranslatorStyledTextStyleHeader); + stylheader.header.data_size = data_size; + stylheader.apply_offset = 0; + stylheader.apply_length = text_size; + + memcpy(buffer, &stylheader, 20); + if (swap_data(B_UINT32_TYPE, buffer, 20, + B_SWAP_HOST_TO_BENDIAN) != B_OK) + return B_ERROR; + if (outDestination->Write(buffer, 20) != 20) + return B_ERROR; + + // output actual style information + if (outDestination->Write(pflatRunArray, + data_size) != data_size) + return B_ERROR; + + return B_OK; +} + // convert the plain text from inSource to plain or styled text // in outDestination status_t @@ -589,8 +619,44 @@ translate_from_text(BPositionIO *inSource, BPositionIO *outDestination, nread = inSource->Read(buffer, READ_BUFFER_SIZE); } + + // Read file attributes if outputting styled data + // and inSource is a BFile object + status_t result = B_OK; + if (!btoplain) { + BFile *pfile = NULL; + pfile = dynamic_cast(inSource); + if (pfile) { + const char *kAttrName = "styles"; + attr_info info; + if (pfile->GetAttrInfo(kAttrName, &info) == B_OK) { + if (info.type != B_RAW_TYPE) + return B_NO_TRANSLATOR; + if (info.size < 160) + return B_NO_TRANSLATOR; - return B_OK; + uint8 *pflatRunArray = new uint8[info.size]; + if (pflatRunArray) { + ssize_t amtread = pfile->ReadAttr(kAttrName, + B_RAW_TYPE, 0, pflatRunArray, info.size); + + // write style data + if (amtread == info.size) { + result = output_styles(outDestination, + size, pflatRunArray, info.size); + } else + result = B_ERROR; + + delete[] pflatRunArray; + pflatRunArray = NULL; + + } else + result = B_NO_MEMORY; + } + } + } + + return result; } // --------------------------------------------------------------- diff --git a/src/add-ons/translators/stxttranslator/STXTTranslator.h b/src/add-ons/translators/stxttranslator/STXTTranslator.h index b6aa9ccb32..05f8cf8fe5 100644 --- a/src/add-ons/translators/stxttranslator/STXTTranslator.h +++ b/src/add-ons/translators/stxttranslator/STXTTranslator.h @@ -38,7 +38,9 @@ #include #include #include +#include #include +#include #define STXT_TRANSLATOR_VERSION 100 #define STXT_IN_QUALITY 0.5