2008-03-28

让xpdf-3.02具备输出目录结构的功能

每次看PDF格式的电子书都喜欢那种带目录的(就是书左边的那个树形控件,它列出了目录/章节),看起来舒服,但是看电子书的时候作笔记是一个问题因为我通常喜欢以一本书的目录结构作为我的笔记的骨架。所以我一直希望那些PDF阅读软件能提供一个功能把书的目录单独保存下来。当然了,不能指望Adobe了,虽然它越做越大但有用的功能却不多。以前在Windows下面我喜欢用Foxit reader,速度相当快,完全没有Adobe reader那种厚重的使用感受;在Ubuntu下我选用小巧的Xpdf。

这两天忽然想看看是否可以在Xpdf里面把我这个想法实现 - 让xpdf能保存电子书的目录。于是我下载了xpdf-3.02的源代码看了看,找到了改动的地方并作了如下改动(灰色部分):

void XPDFViewer::setupOutlineItems(GList *items, Widget parent,
UnicodeMap *uMap) {
OutlineItem *item;
GList *kids = NULL;
Widget label;
Arg args[20];
GString *title;
char buf[8];
XmString s;
int i, j, n;

int m;
static int nlevel = 0;

typedef struct LEN
{
int len;
struct LEN * prev;
struct LEN * next;
};

static LEN * head = NULL;
static LEN * tail = NULL;

for (i = 0; i < items->getLength(); ++i) {
item = (OutlineItem *)items->get(i);
title = new GString();
for (j = 0; j < item->getTitleLength(); ++j) {
n = uMap->mapUnicode(item->getTitle()[j], buf, sizeof(buf));
title->append(buf, n);
}
n = 0;
XtSetArg(args[n], XPDFNentryPosition, i); ++n;
if (parent) {
XtSetArg(args[n], XPDFNentryParent, parent); ++n;
}
XtSetArg(args[n], XPDFNentryExpanded, item->isOpen()); ++n;
s = XmStringCreateLocalized(title->getCString());

for(m = 0; m < nlevel; m++) {
fprintf(stdout,"-");
}
fprintf(stdout,"%s\n",title->getCString());

delete title;
XtSetArg(args[n], XmNlabelString, s); ++n;
XtSetArg(args[n], XmNuserData, item); ++n;
XtSetArg(args[n], XmNmarginWidth, 0); ++n;
XtSetArg(args[n], XmNmarginHeight, 2); ++n;
XtSetArg(args[n], XmNshadowThickness, 0); ++n;
XtSetArg(args[n], XmNforeground,
app->getReverseVideo() ? WhitePixel(display, screenNum)
: BlackPixel(display, screenNum)); ++n;
XtSetArg(args[n], XmNbackground, app->getPaperPixel()); ++n;
label = XmCreateLabelGadget(outlineTree, "label", args, n);
XmStringFree(s);
if (outlineLabelsLength == outlineLabelsSize) {
outlineLabelsSize += 64;
outlineLabels = (Widget *)greallocn(outlineLabels,
outlineLabelsSize, sizeof(Widget *));
}
outlineLabels[outlineLabelsLength++] = label;
item->open();
if ((kids = item->getKids())) {

if(kids != NULL &&
kids->getLength() > 0)
{
nlevel++;

LEN * newnode = new LEN;

newnode->len = kids->getLength();
newnode->prev = NULL;
newnode->next = NULL;

if(NULL == head)
{
head = newnode;
tail = head;
}
else // the link is exist
{
// append a node behind the link;the node presents the number of kid
tail->len -= 1;
tail->next = newnode;
newnode->prev = tail;
tail = tail->next;
}
}
else // no kids
{
if(NULL == head)
{
if(nlevel > 0)
nlevel--;
}
else
{
tail->len -= 1;

if(tail->len <= 0)
{
if(nlevel > 0)
nlevel--;

if(tail == head) // the link is empty
{
delete head;
head = NULL;
tail = NULL;
}
else // remove the node with zero 'len'
{
LEN * pp = NULL;

pp = tail;
tail = tail->prev;
delete pp;
}
}
}
}

setupOutlineItems(kids, label, uMap);
}
}
}

这样改完以后Xpdf一打开一个带目录的PDF就会自动输出它的目录结构,更好的方案是保存到一个文件或者干脆在Xpdf上加一个button来选择保存的地方和时机,但是时间是一个问题,还要花大量的时间带娃,所以只能先满足我最基本的需求了~,下面是产生的实例:

An Introduction to Genetic Algorithms
Table of Contents
Chapter 1: Genetic Algorithms: An Overview
-Overview
-1.1 A BRIEF HISTORY OF EVOLUTIONARY COMPUTATION
-1.2 THE APPEAL OF EVOLUTION
-1.3 BIOLOGICAL TERMINOLOGY
-1.4 SEARCH SPACES AND FITNESS LANDSCAPES
-1.5 ELEMENTS OF GENETIC ALGORITHMS
--Examples of Fitness Functions
--GA Operators
-1.6 A SIMPLE GENETIC ALGORITHM
-1.7 GENETIC ALGORITHMS AND TRADITIONAL SEARCH METHODS
-1.9 TWO BRIEF EXAMPLES
--Using GAs to Evolve Strategies for the Prisoner's Dilemma
--Hosts and Parasites: Using GAs to Evolve Sorting Networks
-1.10 HOW DO GENETIC ALGORITHMS WORK?
-THOUGHT EXERCISES
-COMPUTER EXERCISES
Chapter 2: Genetic Algorithms in Problem Solving
-Overview
-2.1 EVOLVING COMPUTER PROGRAMS
--Evolving Lisp Programs
--Evolving Cellular Automata
-2.2 DATA ANALYSIS AND PREDICTION
--Predicting Dynamical Systems
--Predicting Protein Structure
-2.3 EVOLVING NEURAL NETWORKS
--Evolving Weights in a Fixed Network
--Evolving Network Architectures
--Direct Encoding
--Grammatical Encoding
--Evolving a Learning Rule
-THOUGHT EXERCISES
-COMPUTER EXERCISES
Chapter 3: Genetic Algorithms in Scientific Models
-Overview
-3.1 MODELING INTERACTIONS BETWEEN LEARNING AND EVOLUTION
--The Baldwin Effect
--A Simple Model of the Baldwin Effect
--Evolutionary Reinforcement Learning
-3.2 MODELING SEXUAL SELECTION
--Simulation and Elaboration of a Mathematical Model for Sexual Selection
-3.3 MODELING ECOSYSTEMS
-3.4 MEASURING EVOLUTIONARY ACTIVITY
-Thought Exercises
-Computer Exercises
Chapter 4: Theoretical Foundations of Genetic Algorithms
-Overview
-4.1 SCHEMAS AND THE TWO-ARMED BANDIT PROBLEM
--The Two-Armed Bandit Problem
--Sketch of a Solution
--Interpretation of the Solution
--Implications for GA Performance
--Deceiving a Genetic Algorithm
--Limitations of "Static" Schema Analysis
-4.2 ROYAL ROADS
--Royal Road Functions
--Experimental Results
--Steepest-ascent hill climbing (SAHC)
--Next-ascent hill climbing (NAHC)
--Random-mutation hill climbing (RMHC)
--Analysis of Random-Mutation Hill Climbing
--Hitchhiking in the Genetic Algorithm
--An Idealized Genetic Algorithm
-4.3 EXACT MATHEMATICAL MODELS OF SIMPLE GENETIC ALGORITHMS
--Formalization of GAs
--Results of the Formalization
--A Finite-Population Model
-4.4 STATISTICAL-MECHANICS APPROACHES
-THOUGHT EXERCISES
-COMPUTER EXERCISES
-5.1 WHEN SHOULD A GENETIC ALGORITHM BE USED?
-5.2 ENCODING A PROBLEM FOR A GENETIC ALGORITHM
--Binary Encodings
--Many-Character and Real-Valued Encodings
--Tree Encodings
-5.3 ADAPTING THE ENCODING
--Inversion
--Evolving Crossover "Hot Spots"
--Messy Gas
-5.4 SELECTION METHODS
--Fitness-Proportionate Selection with "Roulette Wheel" and "Stochastic Universal" Sampling
--Sigma Scaling
--Elitism
--Boltzmann Selection
--Rank Selection
--Tournament Selection
--Steady-State Selection
-5.5 GENETIC OPERATORS
--Crossover
--Mutation
--Other Operators and Mating Strategies
-5.6 PARAMETERS FOR GENETIC ALGORITHMS
-THOUGHT EXERCISES
-COMPUTER EXERCISES
Chapter 6: Conclusions and Future Directions
-Overview
-Incorporating Ecological Interactions
-Incorporating New Ideas from Genetics
-Incorporating Development and Learning
-Adapting Encodings and Using Encodings That Permit Hierarchy and Open-Endedness
-Adapting Parameters
-Connections with the Mathematical Genetics Literature
-Extension of Statistical Mechanics Approaches
-Identifying and Overcoming Impediments to the Success of GAs
-Understanding the Role of Schemas in GAs
-Understanding the Role of Crossover
-Theory of GAs With Endogenous Fitness
Appendix A: Selected General References
Appendix B: Other Resources
-SELECTED JOURNALS PUBLISHING WORK ON GENETIC ALGORITHMS
-SELECTED ANNUAL OR BIANNUAL CONFERENCES INCLUDING WORK ON GENETIC ALGORITHMS
-INTERNET MAILING LISTS, WORLD WIDE WEB SITES, AND NEWS GROUPS WITH INFORMATION AND DISCUSSIONS ON GENETIC ALGORITHMS
-Bibliography

No comments: