diff --git a/src/tools/translation/stxtinfo/stxtinfo.cpp b/src/tools/translation/stxtinfo/stxtinfo.cpp index 7cac73adef..c50ff3a2e8 100644 --- a/src/tools/translation/stxtinfo/stxtinfo.cpp +++ b/src/tools/translation/stxtinfo/stxtinfo.cpp @@ -10,10 +10,11 @@ // information that this program is concerned with. This format is outlined // in TranslatorFormats.h. // -// Currently, this program can only work with files created with the -// BTranslationUtils::PutStyledText() function. In a future version, -// a feature may be added to extract the style information from the -// attribute that StyledEdit uses. +// This program prints out information from the "styles" attribute that +// StyledEdit uses. If that information is not available, it attempts +// to read the styles information from the contents of the file, assuming +// that it is the format that BTranslationUtils::PutStyledText() writes +// out. // // The intention of this program is to aid with the development and // debugging of the STXTTranslator. It may also be useful for debugging / @@ -51,6 +52,8 @@ #include #include // for B_UNICODE_UTF8 +#include + // for attr_info #define max(x,y) ((x > y) ? x : y) #define DATA_BUFFER_SIZE 64 @@ -76,245 +79,318 @@ struct Style { }; void -PrintStxtInfo(const char *filepath) +PrintStyle(Style &style, int32 i) { - BFile file(filepath, B_READ_ONLY); - - if (file.InitCheck() == B_OK) { - const uint32 kstxtsize = sizeof(TranslatorStyledTextStreamHeader); - const uint32 ktxtsize = sizeof(TranslatorStyledTextTextHeader); - const uint32 kstylsize = sizeof(TranslatorStyledTextStyleHeader); - const uint32 kStyleSize = sizeof(Style); - - uint8 buffer[max(max(max(kstxtsize, ktxtsize), kstylsize), kStyleSize)]; - - // Check STXT Header - status_t nread = 0; - nread = file.Read(buffer, kstxtsize); - if (nread != static_cast(kstxtsize)) { - printf("Error: Unable to read stream header\n"); - return; - } - TranslatorStyledTextStreamHeader stxtheader; - memcpy(&stxtheader, buffer, kstxtsize); - if (swap_data(B_UINT32_TYPE, &stxtheader, kstxtsize, - B_SWAP_BENDIAN_TO_HOST) != B_OK) { - printf("Error: Unable to swap byte order of stream header\n"); - return; - } - - printf("Be styled text information for: %s\n\n", filepath); - - // Print Stream Header (STXT) - printf("Stream Header (STXT section):\n\n"); - - printf("magic number: 0x%.8lx ", stxtheader.header.magic); - if (stxtheader.header.magic == B_STYLED_TEXT_FORMAT) - printf("(valid)\n"); - else - printf("(INVALID, should be %.8lx)\n", - static_cast(B_STYLED_TEXT_FORMAT)); - - printf("header size: %u ", - static_cast(stxtheader.header.header_size)); - if (stxtheader.header.header_size == kstxtsize) - printf("(valid)\n"); - else - printf("(INVALID, should be %u)\n", - static_cast(kstxtsize)); - - printf("data size: %u ", - static_cast(stxtheader.header.data_size)); - if (stxtheader.header.data_size == 0) - printf("(valid)\n"); - else - printf("(INVALID, should be 0)\n"); + style.offset = B_BENDIAN_TO_HOST_INT32(style.offset); + style.size = B_BENDIAN_TO_HOST_FLOAT(style.size); + style.shear = B_BENDIAN_TO_HOST_FLOAT(style.shear); + style.face = B_BENDIAN_TO_HOST_INT16(style.face); + style.reserved = B_BENDIAN_TO_HOST_INT16(style.reserved); - printf("version: %d ", - static_cast(stxtheader.version)); - if (stxtheader.version == 100) - printf("(valid)\n"); + printf("\nStyle %d:\n", static_cast(i + 1)); + printf("offset: %d\n", static_cast(style.offset)); + printf("family: %s\n", style.family); + printf("style: %s\n", style.style); + printf("size: %f\n", style.size); + printf("shear: %f (typically 90.0)\n", style.shear); + printf("face: %u (typically 0)\n", + static_cast(style.face)); + printf("RGBA: (%u, %u, %u, %u)\n", + static_cast(style.red), + static_cast(style.blue), + static_cast(style.green), + static_cast(style.alpha)); + printf("reserved: %u (should be 0)\n", + static_cast(style.reserved)); +} + +bool +PrintStylesAttribute(BFile &file) +{ + const char *kAttrName = "styles"; + attr_info info; + + if (file.GetAttrInfo(kAttrName, &info) != B_OK) + return false; + if (info.type != B_RAW_TYPE) { + printf("Error: styles attribute is of the wrong type\n"); + return false; + } + if (info.size < 160) { + printf("Error: styles attribute is missing information\n"); + return false; + } + + uint8 *pflatRunArray = new uint8[info.size]; + if (!pflatRunArray) { + printf("Error: Not enough memory available to read styles attribute\n"); + return false; + } + + ssize_t amtread = file.ReadAttr(kAttrName, B_RAW_TYPE, 0, + pflatRunArray, info.size); + if (amtread != info.size) { + printf("Error: Unable to read styles attribute\n"); + return false; + } + + // Check Styles + StylesHeader stylesheader; + memcpy(&stylesheader, pflatRunArray, sizeof(StylesHeader)); + if (swap_data(B_UINT32_TYPE, &stylesheader, sizeof(StylesHeader), + B_SWAP_BENDIAN_TO_HOST) != B_OK) { + printf("Error: Unable to swap byte order of styles header\n"); + return false; + } + + // Print StylesHeader info + printf("\"styles\" attribute data:\n\n"); + + printf("magic number: 0x%.8lx ", + static_cast(stylesheader.magic)); + if (stylesheader.magic == 'Ali!') + printf("(valid)\n"); + else + printf("(INVALID, should be 0x%.8lx)\n", + static_cast('Ali!')); + + printf("version: 0x%.8lx ", + static_cast(stylesheader.version)); + if (stylesheader.version == 0) + printf("(valid)\n"); + else + printf("(INVALID, should be 0x%.8lx)\n", 0UL); + + printf("number of styles: %d\n", + static_cast(stylesheader.count)); + + // Check and Print out each style + Style *pstyle = reinterpret_cast