summaryrefslogtreecommitdiff
path: root/lyt/textbox.cpp
blob: 8552c3da2b19f45f9226a56a69e3656369755bd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*******************************************************************************
  This file is part of LayoutStudio (http://github.com/Treeki/LayoutStudio)
  Copyright (c) 2010 Treeki (treeki@gmail.com)

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, version 2.0.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License 2.0 for more details.

  You should have received a copy of the GNU General Public License 2.0
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/

#include "textbox.h"
#include "layout.h"
#include "common.h"


LYTTextBox::LYTTextBox(LYTLayout &layout) : LYTPane(layout) {
}


void LYTTextBox::dumpToDebug(bool showHeading) {
	if (showHeading)
		qDebug() << "LYTTextBox" << name << "@" << (void*)this;

	LYTPane::dumpToDebug(false);

	qDebug() << "- Text:" << text;
	qDebug() << "- Buffer Length:" << bufferLength;
	qDebug() << "- Material:" << materialName << "- Font:" << fontName;
	qDebug() << "- Alignment:" << alignment << "- Alignment Override:" << alignmentOverride;
	qDebug() << "- Colours:" << colour1 << "--" << colour2;
	qDebug() << "- Font Size:" << fontSizeX << "x" << fontSizeY;
	qDebug() << "- Char Space:" << charSpace << "- Line Space:" << lineSpace;
}



void LYTTextBox::writeToDataStream(QDataStream &out) {
	LYTPane::writeToDataStream(out);

	// lengths are stored in bytes (including zero terminator) not characters
	out << (quint16)((bufferLength + 1) * 2);
	out << (quint16)((text.length() + 1) * 2);

	// calculate the material and font numbers
	int materialNum = m_layout.materials.keys().indexOf(materialName);
	int fontNum = m_layout.m_fontRefs.indexOf(fontName);

	out << (quint16)materialNum;
	out << (quint16)fontNum;

	out << (quint8)alignment;
	out << (quint8)alignmentOverride;

	out.skipRawData(2); // padding

	out << (quint32)0x74; // fixed offset to textbox contents

	WriteRGBA8Color(colour1, out);
	WriteRGBA8Color(colour2, out);

	out << (float)fontSizeX;
	out << (float)fontSizeY;
	out << (float)charSpace;
	out << (float)lineSpace;

	// write the textbox contents
	const ushort *convertedText = text.utf16();
	for (int i = 0; i < text.length(); i++)
		out << (quint16)convertedText[i];
}


void LYTTextBox::readFromDataStream(QDataStream &in) {
	qint64 saveStartPos = in.device()->pos();

	LYTPane::readFromDataStream(in);

	// the lengths are stored in bytes (not characters) and count the
	// zero terminator, and strings are UTF-16 (I think) so we need
	// to take it off here
	in >> (quint16&)bufferLength;
	bufferLength >>= 1;
	bufferLength--;

	quint16 stringLength;
	in >> (quint16&)stringLength;
	stringLength >>= 1;
	stringLength--;

	// read the material and font names
	quint16 materialNum, fontNum;
	in >> (quint16&)materialNum;
	in >> (quint16&)fontNum;

	materialName = m_layout.materials.keys().at(materialNum);
	fontName = m_layout.m_fontRefs.at(fontNum);

	in >> (quint8&)alignment;
	in >> (quint8&)alignmentOverride;

	in.skipRawData(2); // padding

	quint32 stringOffset;
	in >> (quint32&)stringOffset;

	ReadRGBA8Color(colour1, in);
	ReadRGBA8Color(colour2, in);

	in >> (float&)fontSizeX;
	in >> (float&)fontSizeY;
	in >> (float&)charSpace;
	in >> (float&)lineSpace;

	// read the textbox contents
	// subtract 8 to account for BinaryBlockHeader or whatever it's called
	in.device()->seek(saveStartPos + stringOffset - 8);

	ushort *rawText = new ushort[stringLength];

	for (int i = 0; i < stringLength; i++)
		in >> (quint16&)rawText[i];

	text.setUtf16(rawText, stringLength);
	delete[] rawText;
}