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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
|
/* Style Implementation for Hypertext HTStyle.c
** ==================================
**
** Styles allow the translation between a logical property
** of a piece of text and its physical representation.
**
** A StyleSheet is a collection of styles, defining the
** translation necessary to
** represent a document. It is a linked list of styles.
*/
#include "HTStyle.h"
#include "HTUtils.h"
/* Local definition of style
** -------------------------
*/
/* The Style Structure
** -------------------
*/
typedef float HTCoord;
typedef int HTColor;
typedef struct {
short kind; /* only NX_LEFTTAB implemented*/
HTCoord position; /* x coordinate for stop */
} HTTabStop;
struct _HTStyle {
/* Style management information
*/
struct _HTStyle *next; /* Link for putting into stylesheet */
char * name; /* Style name */
char * SGMLTag; /* Tag name to start */
/* Character attributes (a la NXRun)
*/
HTFont font; /* Font id */
HTCoord fontSize; /* The size of font, not independent */
HTColor color; /* text gray of current run */
int superscript; /* superscript (-sub) in points */
HTAnchor *anchor; /* Anchor id if any, else zero */
/* Paragraph Attribtes (a la NXTextStyle)
*/
HTCoord indent1st; /* how far first line in paragraph is
* indented */
HTCoord leftIndent; /* how far second line is indented */
HTCoord rightIndent; /* (Missing from NeXT version */
short alignment; /* quad justification */
HTCoord lineHt; /* line height */
HTCoord descentLine; /* descender bottom from baseline */
HTTabStop *tabs; /* array of tab stops, 0 terminated */
BOOL wordWrap; /* Yes means wrap at space not char */
BOOL freeFormat; /* Yes means \n is just white space */
HTCoord spaceBefore; /* Omissions from NXTextStyle */
HTCoord spaceAfter;
int paraFlags; /* Paragraph flags, bits as follows: */
#define PARA_KEEP 1 /* Do not break page within this paragraph */
#define PARA_WITH_NEXT 2 /* Do not break page after this paragraph */
#define HT_JUSTIFY 0 /* For alignment */
#define HT_LEFT 1
#define HT_RIGHT 2
#define HT_CENTER 3
};
/* Create a new style
*/
PUBLIC HTStyle* HTStyleNew NOARGS
{
return (HTStyle *)calloc(1, sizeof(HTStyle));
}
/* Create a new style with a name
*/
PUBLIC HTStyle* HTStyleNewNamed ARGS1 (CONST char *,name)
{
HTStyle * self = HTStyleNew();
StrAllocCopy(self->name, name);
return self;
}
/* Free a style
*/
PUBLIC HTStyle * HTStyleFree ARGS1 (HTStyle *,self)
{
if (self->name) free(self->name);
if (self->SGMLTag) free(self->SGMLTag);
free(self);
return 0;
}
#ifdef SUPPRESS /* Only on the NeXT */
/* Read a style from a stream (without its name)
** --------------------------
**
** Reads a style with paragraph information from a stream.
** The style name is not read or written by these routines.
*/
#define NONE_STRING "(None)"
#define HTStream NXStream
HTStyle * HTStyleRead (HTStyle * style, HTStream * stream)
{
char myTag[STYLE_NAME_LENGTH];
char fontName[STYLE_NAME_LENGTH];
NXTextStyle *p;
int tab;
int gotpara; /* flag: have we got a paragraph definition? */
NXScanf(stream, "%s%s%f%d",
myTag,
fontName,
&style->fontSize,
&gotpara);
if (gotpara) {
if (!style->paragraph) {
style->paragraph = malloc(sizeof(*(style->paragraph)));
style->paragraph->tabs = 0;
}
p = style->paragraph;
NXScanf(stream, "%f%f%f%f%hd%f%f%hd",
&p->indent1st,
&p->indent2nd,
&p->lineHt,
&p->descentLine,
&p->alignment,
&style->spaceBefore,
&style->spaceAfter,
&p->numTabs);
if (p->tabs) free(p->tabs);
p->tabs = malloc(p->numTabs * sizeof(p->tabs[0]));
for (tab=0; tab < p->numTabs; tab++) {
NXScanf(stream, "%hd%f",
&p->tabs[tab].kind,
&p->tabs[tab].x);
}
} else { /* No paragraph */
if (style->paragraph) {
free(style->paragraph);
style->paragraph = 0;
}
} /* if no paragraph */
StrAllocCopy(style->SGMLTag, myTag);
if (strcmp(fontName, NONE_STRING)==0)
style->font = 0;
else
style->font = [Font newFont:fontName size:style->fontSize];
return 0;
}
/* Write a style to a stream in a compatible way
*/
HTStyle * HTStyleWrite (HTStyle * style, NXStream * stream)
{
int tab;
NXTextStyle *p = style->paragraph;
NXPrintf(stream, "%s %s %f %d\n",
style->SGMLTag,
style->font ? [style->font name] : NONE_STRING,
style->fontSize,
p!=0);
if (p) {
NXPrintf(stream, "\t%f %f %f %f %d %f %f\t%d\n",
p->indent1st,
p->indent2nd,
p->lineHt,
p->descentLine,
p->alignment,
style->spaceBefore,
style->spaceAfter,
p->numTabs);
for (tab=0; tab < p->numTabs; tab++)
NXPrintf(stream, "\t%d %f\n",
p->tabs[tab].kind,
p->tabs[tab].x);
}
return style;
}
/* Write a style to stdout for diagnostics
*/
HTStyle * HTStyleDump (HTStyle * style)
{
int tab;
NXTextStyle *p = style->paragraph;
printf("Style %d `%s' SGML:%s. Font %s %.1f point.\n",
style,
style->name,
style->SGMLTag,
[style->font name],
style->fontSize);
if (p) {
printf(
"\tIndents: first=%.0f others=%.0f, Height=%.1f Desc=%.1f\n"
"\tAlign=%d, %d tabs. (%.0f before, %.0f after)\n",
p->indent1st,
p->indent2nd,
p->lineHt,
p->descentLine,
p->alignment,
p->numTabs,
style->spaceBefore,
style->spaceAfter);
for (tab=0; tab < p->numTabs; tab++) {
printf("\t\tTab kind=%d at %.0f\n",
p->tabs[tab].kind,
p->tabs[tab].x);
}
printf("\n");
} /* if paragraph */
return style;
}
#endif
/* StyleSheet Functions
** ====================
*/
/* Searching for styles:
*/
HTStyle * HTStyleNamed ARGS2 (HTStyleSheet *,self, CONST char *,name)
{
HTStyle * scan;
for (scan=self->styles; scan; scan=scan->next)
if (0==strcmp(scan->name, name)) return scan;
if (TRACE) fprintf(stderr, "StyleSheet: No style named `%s'\n", name);
return 0;
}
#ifdef NEXT_SUPRESS /* Not in general common code */
HTStyle * HTStyleMatching (HTStyleSheet * self, HTStyle *style)
{
HTStyle * scan;
for (scan=self->styles; scan; scan=scan->next)
if (scan->paragraph == para) return scan;
return 0;
}
/* Find the style which best fits a given run
** ------------------------------------------
**
** This heuristic is used for guessing the style for a run of
** text which has been pasted in. In order, we try:
**
** A style whose paragraph structure is actually used by the run.
** A style matching in font
** A style matching in paragraph style exactly
** A style matching in paragraph to a degree
*/
HTStyle * HTStyleForRun (HTStyleSheet *self, NXRun *run)
{
HTStyle * scan;
HTStyle * best = 0;
int bestMatch = 0;
NXTextStyle * rp = run->paraStyle;
for (scan=self->styles; scan; scan=scan->next)
if (scan->paragraph == run->paraStyle) return scan; /* Exact */
for (scan=self->styles; scan; scan=scan->next){
NXTextStyle * sp = scan->paragraph;
if (sp) {
int match = 0;
if (sp->indent1st == rp->indent1st) match = match+1;
if (sp->indent2nd == rp->indent2nd) match = match+2;
if (sp->lineHt == rp->lineHt) match = match+1;
if (sp->numTabs == rp->numTabs) match = match+1;
if (sp->alignment == rp->alignment) match = match+3;
if (scan->font == run->font) match = match+10;
if (match>bestMatch) {
best=scan;
bestMatch=match;
}
}
}
if (TRACE) fprintf(stderr, "HTStyleForRun: Best match for style is %d out of 18\n",
bestMatch);
return best;
}
#endif
/* Add a style to a sheet
** ----------------------
*/
HTStyleSheet * HTStyleSheetAddStyle ARGS2
(HTStyleSheet *,self, HTStyle *,style)
{
style->next = 0; /* The style will go on the end */
if (!self->styles) {
self->styles = style;
} else {
HTStyle * scan;
for(scan=self->styles; scan->next; scan=scan->next); /* Find end */
scan->next=style;
}
return self;
}
/* Remove the given object from a style sheet if it exists
*/
HTStyleSheet * HTStyleSheetRemoveStyle ARGS2
(HTStyleSheet *,self, HTStyle *,style)
{
if (self->styles == style) {
self->styles = style->next;
return self;
} else {
HTStyle * scan;
for(scan = self->styles; scan; scan = scan->next) {
if (scan->next == style) {
scan->next = style->next;
return self;
}
}
}
return 0;
}
/* Create new style sheet
*/
HTStyleSheet * HTStyleSheetNew NOARGS
{
HTStyleSheet * self = (HTStyleSheet *)malloc(sizeof(*self));
memset((void*)self, 0, sizeof(*self)); /* ANSI */
/* Harbison c ref man says (char*)self
but k&r ansii and abc books and Think_C say (void*) */
/* bzero(self, sizeof(*self)); */ /* BSD */
return self;
}
/* Free off a style sheet pointer
*/
HTStyleSheet * HTStyleSheetFree ARGS1 (HTStyleSheet *,self)
{
HTStyle * style;
while((style=self->styles)!=0) {
self->styles = style->next;
HTStyleFree(style);
}
free(self);
return 0;
}
/* Read a stylesheet from a typed stream
** -------------------------------------
**
** Reads a style sheet from a stream. If new styles have the same names
** as existing styles, they replace the old ones without changing the ids.
*/
#ifdef NEXT_SUPRESS /* Only on the NeXT */
HTStyleSheet * HTStyleSheetRead(HTStyleSheet * self, NXStream * stream)
{
int numStyles;
int i;
HTStyle * style;
char styleName[80];
NXScanf(stream, " %d ", &numStyles);
if (TRACE) fprintf(stderr, "Stylesheet: Reading %d styles\n", numStyles);
for (i=0; i<numStyles; i++) {
NXScanf(stream, "%s", styleName);
style = HTStyleNamed(self, styleName);
if (!style) {
style = HTStyleNewNamed(styleName);
(void) HTStyleSheetAddStyle(self, style);
}
(void) HTStyleRead(style, stream);
if (TRACE) HTStyleDump(style);
}
return self;
}
/* Write a stylesheet to a typed stream
** ------------------------------------
**
** Writes a style sheet to a stream.
*/
HTStyleSheet * HTStyleSheetWrite(HTStyleSheet * self, NXStream * stream)
{
int numStyles = 0;
HTStyle * style;
for(style=self->styles; style; style=style->next) numStyles++;
NXPrintf(stream, "%d\n", numStyles);
if (TRACE) fprintf(stderr, "StyleSheet: Writing %d styles\n", numStyles);
for (style=self->styles; style; style=style->next) {
NXPrintf(stream, "%s ", style->name);
(void) HTStyleWrite(style, stream);
}
return self;
}
#endif
|