1 #include "dl-dvi-parser.hh"
3 using namespace DviLib;
6 DVI_SETCHAR0 = 0, /* 128 of these */
51 DVI_FONTNUM0 = 171, /* 64 of these */
71 skip_font_definition (AbstractLoader& l, uint *count)
77 uint dl = l.get_uint8();
78 uint nl = l.get_uint8();
85 parse_command (AbstractLoader &l, uint *count, DviOpcode *opcode)
91 *opcode = (DviOpcode)l.get_uint8 ();
94 if (DVI_SETCHAR0 <= *opcode && *opcode <= DVI_SETCHAR127)
95 return new DviSetCharCommand (*opcode);
96 else if (DVI_FONTNUM0 <= *opcode && *opcode <= DVI_FONTNUM63)
97 return new DviFontNumCommand (*opcode - DVI_FONTNUM0);
98 else switch (*opcode) {
101 return new DviSetCharCommand (l.get_uint8());
105 return new DviSetCharCommand (l.get_uint16());
109 return new DviSetCharCommand (l.get_uint24());
113 return new DviSetCharCommand (l.get_uint32());
119 return new DviSetRuleCommand (h, w);
123 return new DviPutCharCommand (l.get_uint8());
127 return new DviPutCharCommand (l.get_uint16());
131 return new DviPutCharCommand (l.get_uint24());
135 return new DviPutCharCommand (l.get_uint32());
141 return new DviPutRuleCommand (h, w);
144 return new DviPushCommand();
147 return new DviPopCommand();
151 return new DviRightCommand (l.get_int8());
155 return new DviRightCommand (l.get_int16());
159 return new DviRightCommand (l.get_int24());
163 return new DviRightCommand (l.get_int32());
166 return new DviWRepCommand ();
170 return new DviWCommand (l.get_int8());
174 return new DviWCommand (l.get_int16());
178 return new DviWCommand (l.get_int24());
182 return new DviWCommand (l.get_int32());
185 return new DviXRepCommand ();
189 return new DviXCommand (l.get_int8());
193 return new DviXCommand (l.get_int16());
197 return new DviXCommand (l.get_int24());
201 return new DviXCommand (l.get_int32());
205 return new DviDownCommand (l.get_int8());
209 return new DviDownCommand (l.get_int16());
213 return new DviDownCommand (l.get_int24());
217 return new DviDownCommand (l.get_int32());
220 return new DviYRepCommand ();
224 return new DviYCommand (l.get_int8());
228 return new DviYCommand (l.get_int16());
232 return new DviYCommand (l.get_int24());
236 return new DviYCommand (l.get_int32());
239 return new DviZRepCommand ();
243 return new DviZCommand (l.get_int8());
247 return new DviZCommand (l.get_int16());
251 return new DviZCommand (l.get_int24());
255 return new DviZCommand (l.get_int32());
259 return new DviFontNumCommand (l.get_uint8());
263 return new DviFontNumCommand (l.get_uint16());
267 return new DviFontNumCommand (l.get_uint24());
271 return new DviFontNumCommand (l.get_uint32());
275 *count += s.length() + 1;
276 return new DviSpecialCommand (s);
279 s = l.get_string16();
280 *count += s.length() + 2;
281 return new DviSpecialCommand (s);
284 s = l.get_string24();
285 *count += s.length() + 3;
286 return new DviSpecialCommand (s);
289 s = l.get_string32();
290 *count += s.length() + 4;
291 return new DviSpecialCommand (s);
295 skip_font_definition (l, count);
299 skip_font_definition (l, count);
303 skip_font_definition (l, count);
307 skip_font_definition (l, count);
309 case DVI_BOP: // BOP and EOP are not considered commands
311 case DVI_NOP: // NOP is ignored
312 case DVI_PRE: // PRE, POST and POSTPOST are not supposed to happen
317 printf ("%u\n", *opcode);
318 throw string ("Unknown command");
325 DviParser::parse_program (void)
327 DviProgram *program = new DviProgram ();
335 cmd = parse_command (loader, &dummy, &opcode);
338 program->add_command (cmd);
342 } while (opcode != DVI_EOP);
348 DviParser::parse_program (uint n_bytes)
350 DviProgram *program = new DviProgram ();
353 while (count < n_bytes)
358 cmd = parse_command (loader, &count, &opcode);
361 cout << opcode << endl;
362 program->add_command (cmd);
371 DviParser::parse_page_header (uint *page_pointer)
375 DviPageHeader *header = new DviPageHeader();
377 header->address = *page_pointer;
379 c = (DviOpcode)loader.get_uint8();
381 throw string ("Expected BOP not found");
382 for (uint i=0; i<N_PAGE_COUNTERS; ++i)
383 header->count[i] = loader.get_uint32 ();
385 *page_pointer = loader.get_uint32 ();
391 DviParser::parse_fontdefinition (void)
393 DviFontdefinition *fontdef = new DviFontdefinition;
394 DviOpcode c = (DviOpcode)loader.get_uint8 ();
398 fontdef->fontnum = loader.get_uint8 ();
401 fontdef->fontnum = loader.get_uint16 ();
404 fontdef->fontnum = loader.get_uint24 ();
407 fontdef->fontnum = loader.get_uint32 ();
410 throw string ("DVI_FNTDEF? expected");
413 fontdef->checksum = loader.get_uint32 ();
414 fontdef->at_size = loader.get_uint32 ();
415 fontdef->design_size = loader.get_uint32 ();
417 uint dirlength = loader.get_uint8 ();
418 uint namelength = loader.get_uint8 ();
420 fontdef->directory = "";
423 for (uint i=0; i<dirlength; ++i)
424 fontdef->directory += loader.get_uint8();
425 for (uint i=0; i<namelength; ++i)
426 fontdef->name += loader.get_uint8();
428 cout << "parsed fd: " << fontdef->name << " " << fontdef->fontnum << endl;
434 DviParser::parse_preamble (void)
436 DviFilePreamble *preamble = new DviFilePreamble;
438 DviOpcode c = (DviOpcode)loader.get_uint8 ();
441 string asdf ("asdf");
442 throw string ("Corrupt .dvi file - first byte is not DVI_PRE" + asdf);
445 preamble->type = (DviType)loader.get_uint8 ();
446 if (preamble->type != NORMAL_DVI)
448 string asdf ("asdf");
450 throw string ("Unknown .dvi format" + asdf);
453 preamble->numerator = loader.get_uint32 ();
454 preamble->denominator = loader.get_uint32 ();
455 preamble->magnification = loader.get_uint32 ();
456 preamble->comment = loader.get_string8 ();
462 DviParser::parse_postamble (void)
464 DviFilePostamble *postamble = new DviFilePostamble;
466 loader.goto_from_end (-5);
470 i = loader.get_uint8 ();
471 loader.goto_from_current (-2);
474 postamble->type = (DviType)i;
476 loader.goto_from_current (-3);
477 loader.goto_from_start (loader.get_uint32() + 1);
479 postamble->last_page_address = loader.get_uint32();
480 postamble->numerator = loader.get_uint32();
481 postamble->denominator = loader.get_uint32();
482 postamble->magnification = loader.get_uint32();
483 postamble->max_height = loader.get_uint32();
484 postamble->max_width = loader.get_uint32();
485 postamble->stack_height = loader.get_uint16();
487 loader.get_uint16 (); // skip number of pages (we count them instead)
491 DviOpcode c = (DviOpcode)loader.get_uint8 ();
495 else if (DVI_FNTDEF1 <= c && c <= DVI_FNTDEF4)
497 loader.goto_from_current (-1);
498 DviFontdefinition *fd = parse_fontdefinition ();
500 postamble->fontdefinitions[fd->fontnum] = fd;
501 cout << fd->name << endl;
502 cout << postamble->fontdefinitions[fd->fontnum]->name;
506 loader.goto_from_current (-1);
514 DviParser::parse_vf_font_preamble (void)
517 VfFontPreamble *preamble = new VfFontPreamble;
519 c = (DviOpcode)loader.get_uint8 ();
521 throw string ("Not a .vf file");
522 c = (DviOpcode)loader.get_uint8 ();
524 throw string ("Not a .vf file");
526 preamble->comment = loader.get_string8 ();
527 preamble->checksum = loader.get_uint32 ();
528 preamble->design_size = loader.get_uint32 ();
532 DviOpcode c = (DviOpcode)loader.get_uint8 ();
534 if (DVI_FNTDEF1 <= c && c <= DVI_FNTDEF4)
536 loader.goto_from_current (-1);
537 DviFontdefinition *fd = parse_fontdefinition ();
539 preamble->fontdefinitions.push_back (fd);
543 loader.goto_from_current (-1);
551 DviParser::parse_vf_char (void)
555 c = (DviOpcode)loader.get_uint8 ();
557 VfChar *ch = new VfChar;
562 throw string ("Corrupt .vf file");
568 packet_length = loader.get_uint32 ();
569 ch->character_code = loader.get_uint32 ();
570 ch->tfm_width = loader.get_uint32 ();
575 ch->character_code = loader.get_uint8 ();
576 ch->tfm_width = loader.get_uint24 ();
578 ch->program = parse_program (packet_length);