Added option to show bookmarks expanded or collapsed.
git-svn-id: file:///srv/svn/repos/haiku/trunk/current@3694 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
parent
9d1b8268b8
commit
61594003dd
@ -5,9 +5,10 @@
|
|||||||
#include "Report.h"
|
#include "Report.h"
|
||||||
|
|
||||||
|
|
||||||
Bookmark::Definition::Definition(int level, BFont* font)
|
Bookmark::Definition::Definition(int level, BFont* font, bool expanded)
|
||||||
: fLevel(level)
|
: fLevel(level)
|
||||||
, fFont(*font)
|
, fFont(*font)
|
||||||
|
, fExpanded(expanded)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,7 +36,7 @@ Bookmark::Bookmark(PDFWriter* writer)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool Bookmark::Find(BFont* font, int& level) const
|
Bookmark::Definition* Bookmark::Find(BFont* font) const
|
||||||
{
|
{
|
||||||
font_family family;
|
font_family family;
|
||||||
font_style style;
|
font_style style;
|
||||||
@ -45,30 +46,29 @@ bool Bookmark::Find(BFont* font, int& level) const
|
|||||||
size = font->Size();
|
size = font->Size();
|
||||||
|
|
||||||
for (int i = 0; i < fDefinitions.CountItems(); i++) {
|
for (int i = 0; i < fDefinitions.CountItems(); i++) {
|
||||||
Definition* d = fDefinitions.ItemAt(i);
|
Definition* definition = fDefinitions.ItemAt(i);
|
||||||
if (d->Matches(&family, &style, size)) {
|
if (definition->Matches(&family, &style, size)) {
|
||||||
level = d->fLevel;
|
return definition;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Bookmark::AddDefinition(int level, BFont* font)
|
void Bookmark::AddDefinition(int level, BFont* font, bool expanded)
|
||||||
{
|
{
|
||||||
ASSERT(1 <= level && level <= kMaxBookmarkLevels);
|
ASSERT(1 <= level && level <= kMaxBookmarkLevels);
|
||||||
if (!Find(font, level)) {
|
if (Find(font) == NULL) {
|
||||||
fDefinitions.AddItem(new Definition(level, font));
|
fDefinitions.AddItem(new Definition(level, font, expanded));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Bookmark::AddBookmark(BPoint start, const char* text, BFont* font)
|
void Bookmark::AddBookmark(BPoint start, float height, const char* text, BFont* font)
|
||||||
{
|
{
|
||||||
int level;
|
Definition* definition = Find(font);
|
||||||
if (Find(font, level)) {
|
if (definition != NULL) {
|
||||||
fOutlines.AddItem(new Outline(start, text, level));
|
fOutlines.AddItem(new Outline(start, height, text, definition));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,6 +79,7 @@ int Bookmark::AscendingByStart(const Outline** a, const Outline** b) {
|
|||||||
|
|
||||||
|
|
||||||
void Bookmark::CreateBookmarks() {
|
void Bookmark::CreateBookmarks() {
|
||||||
|
char optList[256];
|
||||||
fOutlines.SortItems(AscendingByStart);
|
fOutlines.SortItems(AscendingByStart);
|
||||||
|
|
||||||
for (int i = 0; i < fOutlines.CountItems(); i++) {
|
for (int i = 0; i < fOutlines.CountItems(); i++) {
|
||||||
@ -87,9 +88,13 @@ void Bookmark::CreateBookmarks() {
|
|||||||
Outline* o = fOutlines.ItemAt(i);
|
Outline* o = fOutlines.ItemAt(i);
|
||||||
REPORT(kInfo, fWriter->fPage, "Bookmark '%s' at level %d", o->Text(), o->Level());
|
REPORT(kInfo, fWriter->fPage, "Bookmark '%s' at level %d", o->Text(), o->Level());
|
||||||
|
|
||||||
|
sprintf(optList, "type=fixed left=%f top=%f", o->Start().x, o->Start().y + o->Height());
|
||||||
|
PDF_set_parameter(fWriter->fPdf, "bookmarkdest", optList);
|
||||||
|
|
||||||
fWriter->ToPDFUnicode(o->Text(), ucs2);
|
fWriter->ToPDFUnicode(o->Text(), ucs2);
|
||||||
|
|
||||||
int bookmark = PDF_add_bookmark(fWriter->fPdf, ucs2.String(), fLevels[o->Level()-1], 1);
|
int open = o->Expanded() ? 1 : 0;
|
||||||
|
int bookmark = PDF_add_bookmark(fWriter->fPdf, ucs2.String(), fLevels[o->Level()-1], open);
|
||||||
|
|
||||||
if (bookmark < 0) bookmark = 0;
|
if (bookmark < 0) bookmark = 0;
|
||||||
|
|
||||||
@ -97,6 +102,8 @@ void Bookmark::CreateBookmarks() {
|
|||||||
fLevels[i] = bookmark;
|
fLevels[i] = bookmark;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// reset to default
|
||||||
|
PDF_set_parameter(fWriter->fPdf, "bookmarkdest", "type=fitwindow");
|
||||||
|
|
||||||
fOutlines.MakeEmpty();
|
fOutlines.MakeEmpty();
|
||||||
}
|
}
|
||||||
@ -108,12 +115,13 @@ File Format: Definition.
|
|||||||
Line comment starts with '#'.
|
Line comment starts with '#'.
|
||||||
|
|
||||||
Definition = Version { Font }.
|
Definition = Version { Font }.
|
||||||
Version = "Bookmarks" "1.0".
|
Version = "Bookmarks" "2.0".
|
||||||
Font = Level Family Style Size.
|
Font = Level Family Style Size Expanded.
|
||||||
Level = int.
|
Level = int.
|
||||||
Family = String.
|
Family = String.
|
||||||
Style = String.
|
Style = String.
|
||||||
Size = float.
|
Size = float.
|
||||||
|
Expanded = "expanded" | "collapsed". // new in version 2.0, version 1.0 defaults to collapsed
|
||||||
String = '"' string '"'.
|
String = '"' string '"'.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -143,16 +151,17 @@ bool Bookmark::Exists(const char* f, const char* s) const {
|
|||||||
bool Bookmark::Read(const char* name) {
|
bool Bookmark::Read(const char* name) {
|
||||||
Scanner scnr(name);
|
Scanner scnr(name);
|
||||||
if (scnr.InitCheck() == B_OK) {
|
if (scnr.InitCheck() == B_OK) {
|
||||||
BString s; float f; bool ok;
|
BString s; float version; bool ok;
|
||||||
ok = scnr.ReadName(&s) && scnr.ReadFloat(&f);
|
ok = scnr.ReadName(&s) && scnr.ReadFloat(&version);
|
||||||
if (!ok || strcmp(s.String(), "Bookmarks") != 0 || f != 1.0) {
|
if (!ok || strcmp(s.String(), "Bookmarks") != 0 || (version != 1.0 && version != 2.0) ) {
|
||||||
REPORT(kError, 0, "Bookmarks (line %d, column %d): '%s' not a bookmarks file or wrong version!", scnr.Line(), scnr.Column(), name);
|
REPORT(kError, 0, "Bookmarks (line %d, column %d): '%s' not a bookmarks file or wrong version!", scnr.Line(), scnr.Column(), name);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (!scnr.IsEOF()) {
|
while (!scnr.IsEOF()) {
|
||||||
float level, size;
|
float level, size;
|
||||||
BString family, style;
|
bool expanded = false;
|
||||||
|
BString family, style, expand;
|
||||||
if (!(scnr.ReadFloat(&level) && level >= 1.0 && level <= 10.0)) {
|
if (!(scnr.ReadFloat(&level) && level >= 1.0 && level <= 10.0)) {
|
||||||
REPORT(kError, 0, "Bookmarks (line %d, column %d): Invalid level", scnr.Line(), scnr.Column());
|
REPORT(kError, 0, "Bookmarks (line %d, column %d): Invalid level", scnr.Line(), scnr.Column());
|
||||||
return false;
|
return false;
|
||||||
@ -169,13 +178,20 @@ bool Bookmark::Read(const char* name) {
|
|||||||
REPORT(kError, 0, "Bookmarks (line %d, column %d): Invalid font size", scnr.Line(), scnr.Column());
|
REPORT(kError, 0, "Bookmarks (line %d, column %d): Invalid font size", scnr.Line(), scnr.Column());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (version == 2.0) {
|
||||||
|
if (!scnr.ReadName(&expand) || (strcmp(expand.String(), "expanded") != 0 && strcmp(expand.String(), "collapsed") != 0)) {
|
||||||
|
REPORT(kError, 0, "Bookmarks (line %d, column %d): Invalid expanded value", scnr.Line(), scnr.Column());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
expanded = strcmp(expand.String(), "expanded") == 0;
|
||||||
|
}
|
||||||
|
|
||||||
if (Exists(family.String(), style.String())) {
|
if (Exists(family.String(), style.String())) {
|
||||||
BFont font;
|
BFont font;
|
||||||
font.SetFamilyAndStyle(family.String(), style.String());
|
font.SetFamilyAndStyle(family.String(), style.String());
|
||||||
font.SetSize(size);
|
font.SetSize(size);
|
||||||
|
|
||||||
AddDefinition((int)level, &font);
|
AddDefinition((int)level, &font, expanded);
|
||||||
} else {
|
} else {
|
||||||
REPORT(kWarning, 0, "Bookmarks (line %d, column %d): Font %s-%s not available!", scnr.Line(), scnr.Column(), family.String(), style.String());
|
REPORT(kWarning, 0, "Bookmarks (line %d, column %d): Font %s-%s not available!", scnr.Line(), scnr.Column(), family.String(), style.String());
|
||||||
}
|
}
|
||||||
|
@ -14,20 +14,26 @@ class Bookmark {
|
|||||||
public:
|
public:
|
||||||
int fLevel;
|
int fLevel;
|
||||||
BFont fFont;
|
BFont fFont;
|
||||||
Definition(int level, BFont* font);
|
bool fExpanded;
|
||||||
|
|
||||||
|
Definition(int level, BFont* font, bool expanded);
|
||||||
bool Matches(font_family* family, font_style* style, float size) const;
|
bool Matches(font_family* family, font_style* style, float size) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Outline {
|
class Outline {
|
||||||
public:
|
public:
|
||||||
BPoint fStart;
|
BPoint fStart;
|
||||||
BString fText;
|
float fHeight;
|
||||||
int fLevel;
|
BString fText;
|
||||||
|
Definition *fDefinition;
|
||||||
|
|
||||||
Outline(BPoint start, const char* text, int level) : fStart(start), fText(text), fLevel(level) { }
|
Outline(BPoint start, float height, const char* text, Definition* definition) : fStart(start), fHeight(height), fText(text), fDefinition(definition) { }
|
||||||
int Level() const { return fLevel; }
|
|
||||||
const char* Text() const { return fText.String(); }
|
int Level() const { return fDefinition->fLevel; }
|
||||||
BPoint Start() const { return fStart; }
|
bool Expanded() const { return fDefinition->fExpanded; }
|
||||||
|
const char* Text() const { return fText.String(); }
|
||||||
|
BPoint Start() const { return fStart; }
|
||||||
|
float Height() const { return fHeight; }
|
||||||
};
|
};
|
||||||
|
|
||||||
PDFWriter* fWriter;
|
PDFWriter* fWriter;
|
||||||
@ -37,7 +43,7 @@ class Bookmark {
|
|||||||
int fLevels[kMaxBookmarkLevels+1];
|
int fLevels[kMaxBookmarkLevels+1];
|
||||||
|
|
||||||
bool Exists(const char* family, const char* style) const;
|
bool Exists(const char* family, const char* style) const;
|
||||||
bool Find(BFont* font, int &level) const;
|
Definition* Find(BFont* font) const;
|
||||||
|
|
||||||
static int AscendingByStart(const Outline** a, const Outline** b);
|
static int AscendingByStart(const Outline** a, const Outline** b);
|
||||||
|
|
||||||
@ -46,8 +52,8 @@ public:
|
|||||||
Bookmark(PDFWriter* writer);
|
Bookmark(PDFWriter* writer);
|
||||||
|
|
||||||
// level starts with 1
|
// level starts with 1
|
||||||
void AddDefinition(int level, BFont* font);
|
void AddDefinition(int level, BFont* font, bool expanded);
|
||||||
void AddBookmark(BPoint start, const char* text, BFont* font);
|
void AddBookmark(BPoint start, float height, const char* text, BFont* font);
|
||||||
bool Read(const char* name); // adds definitions from file
|
bool Read(const char* name); // adds definitions from file
|
||||||
void CreateBookmarks();
|
void CreateBookmarks();
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user