Now, stxtinfo reads the "styles" attribute that StyleEdit uses. So, this program can now be used to see information about what text styles are used in a text file created by StyledEdit. Also, it still can read the Be styled text format that the STXTTranslator works with.

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@3389 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Matthew Wilber
2003-05-30 00:19:47 +00:00
parent d9d219a135
commit cb455797b8
+141 -52
View File
@@ -10,10 +10,11 @@
// information that this program is concerned with. This format is outlined // information that this program is concerned with. This format is outlined
// in TranslatorFormats.h. // in TranslatorFormats.h.
// //
// Currently, this program can only work with files created with the // This program prints out information from the "styles" attribute that
// BTranslationUtils::PutStyledText() function. In a future version, // StyledEdit uses. If that information is not available, it attempts
// a feature may be added to extract the style information from the // to read the styles information from the contents of the file, assuming
// attribute that StyledEdit uses. // that it is the format that BTranslationUtils::PutStyledText() writes
// out.
// //
// The intention of this program is to aid with the development and // The intention of this program is to aid with the development and
// debugging of the STXTTranslator. It may also be useful for debugging / // debugging of the STXTTranslator. It may also be useful for debugging /
@@ -51,6 +52,8 @@
#include <TranslatorFormats.h> #include <TranslatorFormats.h>
#include <Font.h> #include <Font.h>
// for B_UNICODE_UTF8 // for B_UNICODE_UTF8
#include <fs_attr.h>
// for attr_info
#define max(x,y) ((x > y) ? x : y) #define max(x,y) ((x > y) ? x : y)
#define DATA_BUFFER_SIZE 64 #define DATA_BUFFER_SIZE 64
@@ -76,11 +79,105 @@ struct Style {
}; };
void void
PrintStxtInfo(const char *filepath) PrintStyle(Style &style, int32 i)
{ {
BFile file(filepath, B_READ_ONLY); 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);
if (file.InitCheck() == B_OK) { printf("\nStyle %d:\n", static_cast<int>(i + 1));
printf("offset: %d\n", static_cast<int>(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<unsigned int>(style.face));
printf("RGBA: (%u, %u, %u, %u)\n",
static_cast<unsigned int>(style.red),
static_cast<unsigned int>(style.blue),
static_cast<unsigned int>(style.green),
static_cast<unsigned int>(style.alpha));
printf("reserved: %u (should be 0)\n",
static_cast<unsigned int>(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<unsigned long>(stylesheader.magic));
if (stylesheader.magic == 'Ali!')
printf("(valid)\n");
else
printf("(INVALID, should be 0x%.8lx)\n",
static_cast<unsigned long>('Ali!'));
printf("version: 0x%.8lx ",
static_cast<unsigned long>(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<int>(stylesheader.count));
// Check and Print out each style
Style *pstyle = reinterpret_cast<Style *>(pflatRunArray + sizeof(StylesHeader));
for (int32 i = 0; i < stylesheader.count; i++)
PrintStyle(*(pstyle + i), i);
delete[] pflatRunArray;
pflatRunArray = NULL;
return true;
}
bool
PrintStxtInfo(BFile &file)
{
const uint32 kstxtsize = sizeof(TranslatorStyledTextStreamHeader); const uint32 kstxtsize = sizeof(TranslatorStyledTextStreamHeader);
const uint32 ktxtsize = sizeof(TranslatorStyledTextTextHeader); const uint32 ktxtsize = sizeof(TranslatorStyledTextTextHeader);
const uint32 kstylsize = sizeof(TranslatorStyledTextStyleHeader); const uint32 kstylsize = sizeof(TranslatorStyledTextStyleHeader);
@@ -93,17 +190,20 @@ PrintStxtInfo(const char *filepath)
nread = file.Read(buffer, kstxtsize); nread = file.Read(buffer, kstxtsize);
if (nread != static_cast<status_t>(kstxtsize)) { if (nread != static_cast<status_t>(kstxtsize)) {
printf("Error: Unable to read stream header\n"); printf("Error: Unable to read stream header\n");
return; return false;
} }
TranslatorStyledTextStreamHeader stxtheader; TranslatorStyledTextStreamHeader stxtheader;
memcpy(&stxtheader, buffer, kstxtsize); memcpy(&stxtheader, buffer, kstxtsize);
if (swap_data(B_UINT32_TYPE, &stxtheader, kstxtsize, if (swap_data(B_UINT32_TYPE, &stxtheader, kstxtsize,
B_SWAP_BENDIAN_TO_HOST) != B_OK) { B_SWAP_BENDIAN_TO_HOST) != B_OK) {
printf("Error: Unable to swap byte order of stream header\n"); printf("Error: Unable to swap byte order of stream header\n");
return; return false;
} }
printf("Be styled text information for: %s\n\n", filepath); if (stxtheader.header.magic != B_STYLED_TEXT_FORMAT) {
printf("Styled text magic number is incorrect, aborting.\n");
return false;
}
// Print Stream Header (STXT) // Print Stream Header (STXT)
printf("Stream Header (STXT section):\n\n"); printf("Stream Header (STXT section):\n\n");
@@ -112,7 +212,7 @@ PrintStxtInfo(const char *filepath)
if (stxtheader.header.magic == B_STYLED_TEXT_FORMAT) if (stxtheader.header.magic == B_STYLED_TEXT_FORMAT)
printf("(valid)\n"); printf("(valid)\n");
else else
printf("(INVALID, should be %.8lx)\n", printf("(INVALID, should be 0x%.8lx)\n",
static_cast<unsigned long>(B_STYLED_TEXT_FORMAT)); static_cast<unsigned long>(B_STYLED_TEXT_FORMAT));
printf("header size: %u ", printf("header size: %u ",
@@ -142,13 +242,13 @@ PrintStxtInfo(const char *filepath)
TranslatorStyledTextTextHeader txtheader; TranslatorStyledTextTextHeader txtheader;
if (file.Read(buffer, ktxtsize) != static_cast<ssize_t>(ktxtsize)) { if (file.Read(buffer, ktxtsize) != static_cast<ssize_t>(ktxtsize)) {
printf("Error: Unable to read text header\n"); printf("Error: Unable to read text header\n");
return; return false;
} }
memcpy(&txtheader, buffer, ktxtsize); memcpy(&txtheader, buffer, ktxtsize);
if (swap_data(B_UINT32_TYPE, &txtheader, ktxtsize, if (swap_data(B_UINT32_TYPE, &txtheader, ktxtsize,
B_SWAP_BENDIAN_TO_HOST) != B_OK) { B_SWAP_BENDIAN_TO_HOST) != B_OK) {
printf("Error: Unable to swap byte order of text header\n"); printf("Error: Unable to swap byte order of text header\n");
return; return false;
} }
// Print Text Header (TEXT) // Print Text Header (TEXT)
@@ -158,7 +258,7 @@ PrintStxtInfo(const char *filepath)
if (txtheader.header.magic == 'TEXT') if (txtheader.header.magic == 'TEXT')
printf("(valid)\n"); printf("(valid)\n");
else else
printf("(INVALID, should be %.8lx)\n", printf("(INVALID, should be 0x%.8lx)\n",
static_cast<unsigned long>('TEXT')); static_cast<unsigned long>('TEXT'));
printf("header size: %u ", printf("header size: %u ",
@@ -188,11 +288,11 @@ PrintStxtInfo(const char *filepath)
if (seekresult < pos) { if (seekresult < pos) {
printf("Error: Unable to seek past text data. " \ printf("Error: Unable to seek past text data. " \
"Text data could be missing\n"); "Text data could be missing\n");
return; return false;
} }
if (seekresult > pos) { if (seekresult > pos) {
printf("Error: File position is beyond expected value\n"); printf("Error: File position is beyond expected value\n");
return; return false;
} }
// Check the STYL header (not all STXT files have this) // Check the STYL header (not all STXT files have this)
@@ -201,20 +301,20 @@ PrintStxtInfo(const char *filepath)
read = file.Read(buffer, kstylsize); read = file.Read(buffer, kstylsize);
if (read != static_cast<ssize_t>(kstylsize) && read != 0) { if (read != static_cast<ssize_t>(kstylsize) && read != 0) {
printf("Error: Unable to read entire style header\n"); printf("Error: Unable to read entire style header\n");
return; return false;
} }
// If there is no STYL header (and no errors) // If there is no STYL header (and no errors)
if (read != static_cast<ssize_t>(kstylsize)) { if (read != static_cast<ssize_t>(kstylsize)) {
printf("\nFile contains no Style Header (STYL section)\n"); printf("\nFile contains no Style Header (STYL section)\n");
return; return false;
} }
memcpy(&stylheader, buffer, kstylsize); memcpy(&stylheader, buffer, kstylsize);
if (swap_data(B_UINT32_TYPE, &stylheader, kstylsize, if (swap_data(B_UINT32_TYPE, &stylheader, kstylsize,
B_SWAP_BENDIAN_TO_HOST) != B_OK) { B_SWAP_BENDIAN_TO_HOST) != B_OK) {
printf("Error: Unable to swap byte order of style header\n"); printf("Error: Unable to swap byte order of style header\n");
return; return false;
} }
// Print Style Header (STYL) // Print Style Header (STYL)
@@ -224,7 +324,7 @@ PrintStxtInfo(const char *filepath)
if (stylheader.header.magic == 'STYL') if (stylheader.header.magic == 'STYL')
printf("(valid)\n"); printf("(valid)\n");
else else
printf("(INVALID, should be %.8lx)\n", printf("(INVALID, should be 0x%.8lx)\n",
static_cast<unsigned long>('STYL')); static_cast<unsigned long>('STYL'));
printf("header size: %u ", printf("header size: %u ",
@@ -242,19 +342,18 @@ PrintStxtInfo(const char *filepath)
printf("apply length: %u (usually the text data size)\n", printf("apply length: %u (usually the text data size)\n",
static_cast<unsigned int>(stylheader.apply_length)); static_cast<unsigned int>(stylheader.apply_length));
// Check Styles // Check Styles
StylesHeader stylesheader; StylesHeader stylesheader;
read = file.Read(buffer, sizeof(StylesHeader)); read = file.Read(buffer, sizeof(StylesHeader));
if (read != sizeof(StylesHeader)) { if (read != sizeof(StylesHeader)) {
printf("Error: Unable to read Styles header\n"); printf("Error: Unable to read Styles header\n");
return; return false;
} }
memcpy(&stylesheader, buffer, sizeof(StylesHeader)); memcpy(&stylesheader, buffer, sizeof(StylesHeader));
if (swap_data(B_UINT32_TYPE, &stylesheader, sizeof(StylesHeader), if (swap_data(B_UINT32_TYPE, &stylesheader, sizeof(StylesHeader),
B_SWAP_BENDIAN_TO_HOST) != B_OK) { B_SWAP_BENDIAN_TO_HOST) != B_OK) {
printf("Error: Unable to swap byte order of styles header\n"); printf("Error: Unable to swap byte order of styles header\n");
return; return false;
} }
// Print StylesHeader info // Print StylesHeader info
@@ -285,36 +384,13 @@ PrintStxtInfo(const char *filepath)
if (read != sizeof(Style)) { if (read != sizeof(Style)) {
printf("Error: Unable to read style %d\n", printf("Error: Unable to read style %d\n",
static_cast<int>(i + 1)); static_cast<int>(i + 1));
return; return false;
} }
style.offset = B_BENDIAN_TO_HOST_INT32(style.offset); PrintStyle(style, i);
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("\nStyle %d:\n", static_cast<int>(i + 1));
printf("offset: %d\n", static_cast<int>(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<unsigned int>(style.face));
printf("RGBA: (%u, %u, %u, %u)\n",
static_cast<unsigned int>(style.red),
static_cast<unsigned int>(style.blue),
static_cast<unsigned int>(style.green),
static_cast<unsigned int>(style.alpha));
printf("reserved: %u (should be 0)\n",
static_cast<unsigned int>(style.reserved));
} }
printf("\nstxtinfo: Done! completed without errors\n"); return true;
} else
printf("Error opening %s\n", filepath);
} }
int int
@@ -322,14 +398,27 @@ main(int argc, char **argv)
{ {
printf("\n"); printf("\n");
if (argc == 2) if (argc == 2) {
PrintStxtInfo(argv[1]); BFile file(argv[1], B_READ_ONLY);
if (file.InitCheck() != B_OK)
printf("Error opening %s\n", argv[1]);
else {
printf("Be styled text information for: %s\n\n", argv[1]);
if (PrintStylesAttribute(file) == false) {
printf("Unable to read styles attribute, attempting to read " \
"style information from file...\n\n");
PrintStxtInfo(file);
}
}
}
else { else {
printf("stxtinfo - reports information about a Be styled text file\n"); printf("stxtinfo - reports information about a Be styled text file\n");
printf("\nUsage:\n"); printf("\nUsage:\n");
printf("stxtinfo filename.stxt\n\n"); printf("stxtinfo filename.stxt\n");
} }
printf("\n");
return 0; return 0;
} }