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);
362 cout << opcode << endl;
364 program->add_command (cmd);
373 DviParser::parse_page_header (uint *page_pointer)
377 DviPageHeader *header = new DviPageHeader();
379 header->address = *page_pointer;
381 c = (DviOpcode)loader.get_uint8();
383 throw string ("Expected BOP not found");
384 for (uint i=0; i<N_PAGE_COUNTERS; ++i)
385 header->count[i] = loader.get_uint32 ();
387 *page_pointer = loader.get_uint32 ();
393 DviParser::parse_fontdefinition (void)
395 DviFontdefinition *fontdef = new DviFontdefinition;
396 DviOpcode c = (DviOpcode)loader.get_uint8 ();
400 fontdef->fontnum = loader.get_uint8 ();
403 fontdef->fontnum = loader.get_uint16 ();
406 fontdef->fontnum = loader.get_uint24 ();
409 fontdef->fontnum = loader.get_uint32 ();
412 throw string ("DVI_FNTDEF? expected");
415 fontdef->checksum = loader.get_uint32 ();
416 fontdef->at_size = loader.get_uint32 ();
417 fontdef->design_size = loader.get_uint32 ();
419 uint dirlength = loader.get_uint8 ();
420 uint namelength = loader.get_uint8 ();
422 fontdef->directory = "";
425 for (uint i=0; i < dirlength; ++i)
426 fontdef->directory += loader.get_uint8();
427 for (uint i=0; i < namelength; ++i)
428 fontdef->name += loader.get_uint8();
431 cout << "parsed fd: " << fontdef->name << " " << fontdef->fontnum << endl;
438 DviParser::parse_preamble (void)
440 DviFilePreamble *preamble = new DviFilePreamble;
442 DviOpcode c = (DviOpcode)loader.get_uint8 ();
445 string asdf ("asdf");
446 throw string ("Corrupt .dvi file - first byte is not DVI_PRE" + asdf);
449 preamble->type = (DviType)loader.get_uint8 ();
450 if (preamble->type != NORMAL_DVI)
452 string asdf ("asdf");
454 throw string ("Unknown .dvi format" + asdf);
457 preamble->numerator = loader.get_uint32 ();
458 preamble->denominator = loader.get_uint32 ();
459 preamble->magnification = loader.get_uint32 ();
460 preamble->comment = loader.get_string8 ();
466 DviParser::parse_postamble (void)
468 DviFilePostamble *postamble = new DviFilePostamble;
470 postamble->fontmap = new DviFontMap;
472 loader.goto_from_end (-5);
476 i = loader.get_uint8 ();
477 loader.goto_from_current (-2);
480 postamble->type = (DviType)i;
482 loader.goto_from_current (-3);
483 loader.goto_from_start (loader.get_uint32() + 1);
485 postamble->last_page_address = loader.get_uint32();
486 postamble->numerator = loader.get_uint32();
487 postamble->denominator = loader.get_uint32();
488 postamble->magnification = loader.get_uint32();
489 postamble->max_height = loader.get_uint32();
490 postamble->max_width = loader.get_uint32();
491 postamble->stack_height = loader.get_uint16();
493 loader.get_uint16 (); // skip number of pages (we count them instead)
497 DviOpcode c = (DviOpcode)loader.get_uint8 ();
501 else if (DVI_FNTDEF1 <= c && c <= DVI_FNTDEF4)
503 loader.goto_from_current (-1);
504 DviFontdefinition *fd = parse_fontdefinition ();
506 postamble->fontmap->set_fontdefinition (fd->fontnum, fd);
507 cout << fd->name << endl;
508 cout << postamble->fontmap->get_fontdefinition(fd->fontnum)->name;
512 loader.goto_from_current (-1);
520 DviParser::parse_vf_font_preamble (void)
523 VfFontPreamble *preamble = new VfFontPreamble;
525 preamble->fontmap = new DviFontMap;
527 c = (DviOpcode)loader.get_uint8 ();
529 throw string ("Not a .vf file");
530 c = (DviOpcode)loader.get_uint8 ();
532 throw string ("Not a .vf file");
534 preamble->comment = loader.get_string8 ();
535 preamble->checksum = loader.get_uint32 ();
536 preamble->design_size = loader.get_uint32 ();
541 DviOpcode c = (DviOpcode)loader.get_uint8 ();
543 if (DVI_FNTDEF1 <= c && c <= DVI_FNTDEF4)
545 loader.goto_from_current (-1);
546 DviFontdefinition *fd = parse_fontdefinition ();
548 preamble->fontmap->set_fontdefinition (i++, fd);
552 loader.goto_from_current (-1);
560 DviParser::parse_vf_char (void)
564 c = (DviOpcode)loader.get_uint8 ();
566 VfChar *ch = new VfChar;
571 throw string ("Corrupt .vf file");
577 packet_length = loader.get_uint32 ();
578 ch->character_code = loader.get_uint32 ();
579 ch->tfm_width = loader.get_uint32 ();
584 ch->character_code = loader.get_uint8 ();
585 ch->tfm_width = loader.get_uint24 ();
587 ch->program = parse_program (packet_length);