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
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
/* encoding.c - functions to manipulate encodings and fontmaps */
/*
 * Copyright (C) 2000, Matias Atria
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <config.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>

#include "mdvi.h"
#include "private.h"

#include <kpathsea/expand.h>
#include <kpathsea/pathsearch.h>

typedef struct _DviFontMap DviFontMap;

struct _DviFontMap {
	ListHead entries;
	DviHashTable fonts;
};

typedef struct _PSFontMap {
	struct _PSFontMap *next;
	struct _PSFontMap *prev;
	char	*psname;
	char	*mapname;
	char	*fullname;
} PSFontMap;

/* these variables control PS font maps */
static char *pslibdir = NULL;	/* path where we look for PS font maps */
static char *psfontdir = NULL;	/* PS font search path */
static int psinitialized = 0;	/* did we expand the path already? */

static ListHead psfonts = MDVI_EMPTY_LIST_HEAD;
static DviHashTable pstable = MDVI_EMPTY_HASH_TABLE;

static ListHead fontmaps;
static DviHashTable maptable;
static int fontmaps_loaded = 0;

#define MAP_HASH_SIZE	57
#define ENC_HASH_SIZE	31
#define PSMAP_HASH_SIZE	57

/* this hash table should be big enough to
 * hold (ideally) one glyph name per bucket */
#define ENCNAME_HASH_SIZE	131 /* most TeX fonts have 128 glyphs */

static ListHead	encodings = MDVI_EMPTY_LIST_HEAD;
static DviEncoding *tex_text_encoding = NULL;
static DviEncoding *default_encoding = NULL;

/* we keep two hash tables for encodings: one for their base files (e.g.
 * "8r.enc"), and another one for their names (e.g. "TeXBase1Encoding") */
static DviHashTable enctable = MDVI_EMPTY_HASH_TABLE;
static DviHashTable enctable_file = MDVI_EMPTY_HASH_TABLE;

/* the TeX text encoding, from dvips */
static char *tex_text_vector[256] = {
	"Gamma", "Delta", "Theta", "Lambda", "Xi", "Pi", "Sigma", "Upsilon",
	"Phi", "Psi", "Omega", "arrowup", "arrowdown", "quotesingle",
	"exclamdown", "questiondown", "dotlessi", "dotlessj", "grave",
	"acute", "caron", "breve", "macron", "ring", "cedilla",
	"germandbls", "ae", "oe", "oslash", "AE", "OE", "Oslash", "space",
	"exclam", "quotedbl", "numbersign", "dollar", "percent",
	"ampersand", "quoteright", "parenleft", "parenright", "asterisk",
	"plus", "comma", "hyphen", "period", "slash", "zero", "one", "two",
	"three", "four", "five", "six", "seven", "eight", "nine", "colon",
	"semicolon", "less", "equal", "greater", "question", "at", "A", "B",
	"C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
	"P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
	"bracketleft", "backslash", "bracketright", "circumflex",
	"underscore", "quoteleft", "a", "b", "c", "d", "e", "f", "g", "h",
	"i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
	"v", "w", "x", "y", "z", "braceleft", "bar", "braceright", "tilde",
	"dieresis", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

static void ps_init_default_paths __PROTO((void));
static int	mdvi_set_default_encoding __PROTO((const char *name));
static int	mdvi_init_fontmaps __PROTO((void));

/*
 * What we do here is allocate one block large enough to hold the entire
 * file (these files are small) minus the leading comments. This is much
 * better than allocating up to 256 tiny strings per encoding vector. */
static int read_encoding(DviEncoding *enc)
{
	FILE	*in;
	int	curr;
	char	*line;
	char	*name;
	char	*next;
	struct stat st;

	ASSERT(enc->private == NULL);

	in = fopen(enc->filename, "rb");
	if(in == NULL) {
		DEBUG((DBG_FMAP, "%s: could not read `%s' (%s)\n",
			enc->name, enc->filename, strerror(errno)));
		return -1;
	}
	if(fstat(fileno(in), &st) < 0) {
		/* should not happen */
		fclose(in);
		return -1;
	}
	st.st_size -= enc->offset;

	/* this will be one big string */
	enc->private = (char *)malloc(st.st_size + 1);
	/* setup the hash table */
	mdvi_hash_create(&enc->nametab, ENCNAME_HASH_SIZE);
	/* setup the encoding vector */
	enc->vector = (char **)mdvi_malloc(256 * sizeof(char *));

	/* jump to the beginning of the interesting part */
	fseek(in, enc->offset, SEEK_SET);
	/* and read everything */
	if(fread(enc->private, st.st_size, 1, in) != 1) {
		fclose(in);
		mdvi_free(enc->private);
		enc->private = NULL;
		return -1;
	}
	/* we don't need this anymore */
	fclose(in);
	curr = 0;

	next = name = NULL;
	DEBUG((DBG_FMAP, "%s: reading encoding vector\n", enc->name));
	for(line = enc->private; *line && curr < 256; line = next) {
		SKIPSP(line);
		if(*line == ']') {
			line++; SKIPSP(line);
			if(STRNEQ(line, "def", 3))
				break;
		}
		name = getword(line, " \t\n", &next);
		if(name == NULL)
			break;
		/* next > line */
		if(*name < ' ')
			continue;
		if(*name == '%') {
			while(*next && *next != '\n')
				next++;
			if(*next) next++; /* skip \n */
			continue;
		}

		/* got a name */
		if(*next) *next++ = 0;

		if(*name == '/')
			name++;
		enc->vector[curr] = name;
		/* add it to the hash table */
		if(!STREQ(name, ".notdef")) {
			mdvi_hash_add(&enc->nametab, MDVI_KEY(name),
				Int2Ptr(curr + 1), MDVI_HASH_REPLACE);
		}
		curr++;
	}
	if(curr == 0) {
		mdvi_hash_reset(&enc->nametab, 0);
		mdvi_free(enc->private);
		mdvi_free(enc);
		return -1;
	}
	while(curr < 256)
		enc->vector[curr++] = NULL;
	return 0;
}

static DviEncoding *find_encoding(const char *name)
{
	return (DviEncoding *)(encodings.count ?
		mdvi_hash_lookup(&enctable, MDVI_KEY(name)) : NULL);
}

static void destroy_encoding(DviEncoding *enc)
{
	if(enc == default_encoding) {
		default_encoding = tex_text_encoding;
		/* now we use reference counts again */
		mdvi_release_encoding(enc, 1);
	}
	if(enc != tex_text_encoding) {
		mdvi_hash_reset(&enc->nametab, 0);
		if(enc->private) {
			mdvi_free(enc->private);
			mdvi_free(enc->vector);
		}
		if(enc->name)
			mdvi_free(enc->name);
		if(enc->filename)
			mdvi_free(enc->filename);
		mdvi_free(enc);
	}
}

/* this is used for the `enctable_file' hash table */
static void file_hash_free(DviHashKey key, void *data)
{
	mdvi_free(key);
}

static DviEncoding *register_encoding(const char *basefile, int replace)
{
	DviEncoding *enc;
	FILE	*in;
	char	*filename;
	char	*name;
	Dstring	input;
	char	*line;
	long	offset;

	DEBUG((DBG_FMAP, "register_encoding(%s)\n", basefile));

	if(encodings.count) {
		enc = mdvi_hash_lookup(&enctable_file, MDVI_KEY(basefile));
		if(enc != NULL) {
			DEBUG((DBG_FMAP, "%s: already there\n", basefile));
			return enc; /* no error */
		}
	}

	/* try our own files first */
	filename = kpse_find_file(basefile,
		kpse_program_text_format, 0);

	/* then try the system-wide ones */
	if(filename == NULL)
		filename = kpse_find_file(basefile,
			kpse_tex_ps_header_format, 0);
	if(filename == NULL)
		filename = kpse_find_file(basefile,
			kpse_dvips_config_format, 0);

	/* finally try the given name */
	if(filename == NULL)
		filename = mdvi_strdup(basefile);

	in = fopen(filename, "rb");
	if(in == NULL) {
		mdvi_free(filename);
		return NULL;
	}

	/* just lookup the name of the encoding */
	name = NULL;
	dstring_init(&input);
	while((line = dgets(&input, in)) != NULL) {
		if(STRNEQ(line, "Encoding=", 9)) {
			name = getword(line + 9, " \t", &line);
			if(*line) *line++ = 0;
			break;
		} else if(*line == '/') {
			char	*label = getword(line + 1, " \t", &line);
			if(*line) {
				*line++ = 0;
				SKIPSP(line);
				if(*line == '[') {
					*line = 0;
					name = label;
					break;
				}
			}
		}
	}
	offset = ftell(in);
	fclose(in);
	if(name == NULL || *name == 0) {
		DEBUG((DBG_FMAP,
			"%s: could not determine name of encoding\n",
			basefile));
		mdvi_free(filename);
		return NULL;
	}

	/* check if the encoding is already there */
	enc = find_encoding(name);
	if(enc == tex_text_encoding) {
		/* A special case: if the vector we found is the static one,
		 * allow the user to override it with an external file */
		listh_remove(&encodings, LIST(enc));
		mdvi_hash_remove(&enctable, MDVI_KEY(enc->name));
		if(enc == default_encoding)
			default_encoding = NULL;
	} else if(enc) {
		/* if the encoding is being used, refuse to remove it */
		if(enc->links) {
			mdvi_free(filename);
			dstring_reset(&input);
			return NULL;
		}
		if(replace) {
			mdvi_hash_remove(&enctable, MDVI_KEY(name));
			mdvi_hash_remove(&enctable_file, MDVI_KEY(basefile));
			listh_remove(&encodings, LIST(enc));
			if(enc == default_encoding) {
				default_encoding = NULL;
				mdvi_release_encoding(enc, 1);
			}
			DEBUG((DBG_FMAP, "%s: overriding encoding\n", name));
			destroy_encoding(enc);
		} else {
			mdvi_free(filename);
			dstring_reset(&input);
			return enc; /* no error */
		}
	}
	enc = xalloc(DviEncoding);
	enc->name = mdvi_strdup(name);
	enc->filename = filename;
	enc->links = 0;
	enc->offset = offset;
	enc->private = NULL;
	enc->vector = NULL;
	mdvi_hash_init(&enc->nametab);
	dstring_reset(&input);
	if(default_encoding == NULL)
		default_encoding = enc;
	mdvi_hash_add(&enctable, MDVI_KEY(enc->name),
		enc, MDVI_HASH_UNCHECKED);
	mdvi_hash_add(&enctable_file, MDVI_KEY(mdvi_strdup(basefile)),
		enc, MDVI_HASH_REPLACE);
	listh_prepend(&encodings, LIST(enc));
	DEBUG((DBG_FMAP, "%s: encoding `%s' registered\n",
		basefile, enc->name));
	return enc;
}

DviEncoding *mdvi_request_encoding(const char *name)
{
	DviEncoding *enc = find_encoding(name);

	if(enc == NULL) {
		DEBUG((DBG_FMAP, "%s: encoding not found, returning default `%s'\n",
			name, default_encoding->name));
		return default_encoding;
	}
	/* we don't keep reference counts for this */
	if(enc == tex_text_encoding)
		return enc;
	if(!enc->private && read_encoding(enc) < 0)
		return NULL;
	enc->links++;

	/* if the hash table is empty, rebuild it */
	if(enc->nametab.nkeys == 0) {
		int	i;

		DEBUG((DBG_FMAP, "%s: rehashing\n", enc->name));
		for(i = 0; i < 256; i++) {
			if(enc->vector[i] == NULL)
				continue;
			mdvi_hash_add(&enc->nametab,
				MDVI_KEY(enc->vector[i]),
				(DviHashKey)Int2Ptr(i),
				MDVI_HASH_REPLACE);
		}
	}
	return enc;
}

void	mdvi_release_encoding(DviEncoding *enc, int should_free)
{
	/* ignore our static encoding */
	if(enc == tex_text_encoding)
		return;
	if(!enc->links || --enc->links > 0 || !should_free)
		return;
	DEBUG((DBG_FMAP, "%s: resetting encoding vector\n", enc->name));
	mdvi_hash_reset(&enc->nametab, 1); /* we'll reuse it */
}

int	mdvi_encode_glyph(DviEncoding *enc, const char *name)
{
	void	*data;

	data = mdvi_hash_lookup(&enc->nametab, MDVI_KEY(name));
	if(data == NULL)
		return -1;
	/* we added +1 to the hashed index just to distinguish
	 * a failed lookup from a zero index. Adjust it now. */
	return (Ptr2Int(data) - 1);
}

/****************
 * Fontmaps     *
 ****************/

static void parse_spec(DviFontMapEnt *ent, char *spec)
{
	char	*arg, *command;

	/* this is a ridiculously simple parser, and recognizes only
	 * things of the form <argument> <command>. Of these, only
	 * command=SlantFont, ExtendFont and ReEncodeFont are handled */
	while(*spec) {
		arg = getword(spec, " \t", &spec);
		if(*spec) *spec++ = 0;
		command = getword(spec, " \t", &spec);
		if(*spec) *spec++ = 0;
		if(!arg || !command)
			continue;
		if(STREQ(command, "SlantFont")) {
			double	x = 10000 * strtod(arg, 0);

			/* SFROUND evaluates arguments twice */
			ent->slant = SFROUND(x);
		} else if(STREQ(command, "ExtendFont")) {
			double	x = 10000 * strtod(arg, 0);

			ent->extend = SFROUND(x);
		} else if(STREQ(command, "ReEncodeFont")) {
			if(ent->encoding)
				mdvi_free(ent->encoding);
			ent->encoding = mdvi_strdup(arg);
		}
	}
}

#if 0
static void print_ent(DviFontMapEnt *ent)
{
	printf("Entry for `%s':\n", ent->fontname);
	printf("  PS name: %s\n", ent->psname ? ent->psname : "(none)");
	printf("  Encoding: %s\n", ent->encoding ? ent->encoding : "(default)");
	printf("  EncFile: %s\n", ent->encfile ? ent->encfile : "(none)");
	printf("  FontFile: %s\n", ent->fontfile ? ent->fontfile : "(same)");
	printf("  Extend: %ld\n", ent->extend);
	printf("  Slant: %ld\n", ent->slant);
}
#endif

DviFontMapEnt	*mdvi_load_fontmap(const char *file)
{
	char	*ptr;
	FILE	*in;
	int	lineno = 1;
	Dstring	input;
	ListHead list;
	DviFontMapEnt *ent;
	DviEncoding	*last_encoding;
	char	*last_encfile;

	ptr = kpse_find_file(file, kpse_program_text_format, 0);
	if(ptr == NULL)
		ptr = kpse_find_file(file, kpse_tex_ps_header_format, 0);
	if(ptr == NULL)
		ptr = kpse_find_file(file, kpse_dvips_config_format, 0);
	if(ptr == NULL)
		in = fopen(file, "rb");
	else {
		in = fopen(ptr, "rb");
		mdvi_free(ptr);
	}
	if(in == NULL)
		return NULL;

	ent = NULL;
	listh_init(&list);
	dstring_init(&input);
	last_encoding = NULL;
	last_encfile  = NULL;

	while((ptr = dgets(&input, in)) != NULL) {
		char	*font_file;
		char	*tex_name;
		char	*ps_name;
		char	*vec_name;
		int	is_encoding;
		DviEncoding *enc;

		lineno++;
		SKIPSP(ptr);

		/* we skip what dvips does */
		if(*ptr <= ' ' || *ptr == '*' || *ptr == '#' ||
		   *ptr == ';' || *ptr == '%')
			continue;

		font_file   = NULL;
		tex_name    = NULL;
		ps_name     = NULL;
		vec_name    = NULL;
		is_encoding = 0;

		if(ent == NULL) {
			ent = xalloc(DviFontMapEnt);
			ent->encoding = NULL;
			ent->slant = 0;
			ent->extend = 0;
		}
		while(*ptr) {
			char	*hdr_name = NULL;

			while(*ptr && *ptr <= ' ')
				ptr++;
			if(*ptr == 0)
				break;
			if(*ptr == '"') {
				char	*str;

				str = getstring(ptr, " \t", &ptr);
				if(*ptr) *ptr++ = 0;
				parse_spec(ent, str);
				continue;
			} else if(*ptr == '<') {
				ptr++;
				if(*ptr == '<')
					ptr++;
				else if(*ptr == '[') {
					is_encoding = 1;
					ptr++;
				}
				SKIPSP(ptr);
				hdr_name = ptr;
			} else if(!tex_name)
				tex_name = ptr;
			else if(!ps_name)
				ps_name = ptr;
			else
				hdr_name = ptr;

			/* get next word */
			getword(ptr, " \t", &ptr);
			if(*ptr) *ptr++ = 0;

			if(hdr_name) {
				const char *ext = file_extension(hdr_name);

				if(is_encoding || (ext && STRCEQ(ext, "enc")))
					vec_name = hdr_name;
				else
					font_file = hdr_name;
			}
		}

		if(tex_name == NULL)
			continue;
		ent->fontname = mdvi_strdup(tex_name);
		ent->psname   = ps_name   ? mdvi_strdup(ps_name)   : NULL;
		ent->fontfile = font_file ? mdvi_strdup(font_file) : NULL;
		ent->encfile  = vec_name  ? mdvi_strdup(vec_name)  : NULL;
		ent->fullfile = NULL;
		enc = NULL; /* we don't have this yet */

		/* if we have an encoding file, register it */
		if(ent->encfile) {
			/* register_encoding is smart enough not to load the
			 * same file twice */
			if(!last_encfile || !STREQ(last_encfile, ent->encfile)) {
				last_encfile  = ent->encfile;
				last_encoding = register_encoding(ent->encfile, 1);
			}
			enc = last_encoding;
		}
		if(ent->encfile && enc){
			if(ent->encoding && !STREQ(ent->encoding, enc->name)) {
				mdvi_warning(
	_("%s: %d: [%s] requested encoding `%s' does not match vector `%s'\n"),
					file, lineno, ent->encfile,
					ent->encoding, enc->name);
			} else if(!ent->encoding)
				ent->encoding = mdvi_strdup(enc->name);
		}

		/* add it to the list */
		/*print_ent(ent);*/
		listh_append(&list, LIST(ent));
		ent = NULL;
	}
	dstring_reset(&input);
	fclose(in);

	return (DviFontMapEnt *)list.head;
}

static void free_ent(DviFontMapEnt *ent)
{
	ASSERT(ent->fontname != NULL);
	mdvi_free(ent->fontname);
	if(ent->psname)
		mdvi_free(ent->psname);
	if(ent->fontfile)
		mdvi_free(ent->fontfile);
	if(ent->encoding)
		mdvi_free(ent->encoding);
	if(ent->encfile)
		mdvi_free(ent->encfile);
	if(ent->fullfile)
		mdvi_free(ent->fullfile);
	mdvi_free(ent);
}

void	mdvi_install_fontmap(DviFontMapEnt *head)
{
	DviFontMapEnt *ent, *next;

	for(ent = head; ent; ent = next) {
		/* add all the entries, overriding old ones */
		DviFontMapEnt *old;

		old = (DviFontMapEnt *)
			mdvi_hash_remove(&maptable, MDVI_KEY(ent->fontname));
		if(old != NULL) {
			DEBUG((DBG_FMAP, "%s: overriding fontmap entry\n",
				old->fontname));
			listh_remove(&fontmaps, LIST(old));
			free_ent(old);
		}
		next = ent->next;
		mdvi_hash_add(&maptable, MDVI_KEY(ent->fontname),
			ent, MDVI_HASH_UNCHECKED);
		listh_append(&fontmaps, LIST(ent));
	}
}

static void init_static_encoding()
{
	DviEncoding	*encoding;
	int	i;

	DEBUG((DBG_FMAP, "installing static TeX text encoding\n"));
	encoding = xalloc(DviEncoding);
	encoding->private  = "";
	encoding->filename = "";
	encoding->name     = "TeXTextEncoding";
	encoding->vector   = tex_text_vector;
	encoding->links    = 1;
	encoding->offset   = 0;
	mdvi_hash_create(&encoding->nametab, ENCNAME_HASH_SIZE);
	for(i = 0; i < 256; i++) {
		if(encoding->vector[i]) {
			mdvi_hash_add(&encoding->nametab,
				MDVI_KEY(encoding->vector[i]),
				(DviHashKey)Int2Ptr(i),
				MDVI_HASH_UNCHECKED);
		}
	}
	ASSERT_VALUE(encodings.count, 0);
	mdvi_hash_create(&enctable, ENC_HASH_SIZE);
	mdvi_hash_create(&enctable_file, ENC_HASH_SIZE);
	enctable_file.hash_free = file_hash_free;
	mdvi_hash_add(&enctable, MDVI_KEY(encoding->name),
		encoding, MDVI_HASH_UNCHECKED);
	listh_prepend(&encodings, LIST(encoding));
	tex_text_encoding = encoding;
	default_encoding = tex_text_encoding;
}

static int	mdvi_set_default_encoding(const char *name)
{
	DviEncoding *enc, *old;

	enc = find_encoding(name);
	if(enc == NULL)
		return -1;
	if(enc == default_encoding)
		return 0;
	/* this will read it from file if necessary,
	 * but it can fail if the file is corrupted */
	enc = mdvi_request_encoding(name);
	if(enc == NULL)
		return -1;
	old = default_encoding;
	default_encoding = enc;
	if(old != tex_text_encoding)
		mdvi_release_encoding(old, 1);
	return 0;
}

static int	mdvi_init_fontmaps(void)
{
	char	*file;
	char	*line;
	FILE	*in;
	Dstring	input;
	int	count = 0;
	const char *config;

	if(fontmaps_loaded)
		return 0;
	/* we will only try this once */
	fontmaps_loaded = 1;

	DEBUG((DBG_FMAP, "reading fontmaps\n"));

	/* make sure the static encoding is there */
	init_static_encoding();

	/* create the fontmap hash table */
	mdvi_hash_create(&maptable, MAP_HASH_SIZE);

	/* get the name of our configuration file */
	config = kpse_cnf_get("mdvi-config");
	if(config == NULL)
		config = MDVI_DEFAULT_CONFIG;
	/* let's ask kpathsea for the file first */
	file = kpse_find_file(config, kpse_program_text_format, 0);
	if(file == NULL)
		in = fopen(config, "rb");
	else {
		in = fopen(file, "rb");
		mdvi_free(file);
	}
	if(in == NULL)
		return -1;
	dstring_init(&input);
	while((line = dgets(&input, in)) != NULL) {
		char	*arg, *map_file;

		SKIPSP(line);
		if(*line < ' ' || *line == '#' || *line == '%')
			continue;
		if(STRNEQ(line, "fontmap", 7)) {
			DviFontMapEnt *ent;

			arg = getstring(line + 7, " \t", &line); *line = 0;
			DEBUG((DBG_FMAP, "%s: loading fontmap\n", arg));
			ent = mdvi_load_fontmap(arg);
			if(ent == NULL) {
				map_file = kpse_find_file(arg, kpse_fontmap_format, 0);
				if (map_file)
					ent = mdvi_load_fontmap(map_file);
			}
			if(ent == NULL)
				mdvi_warning(_("%s: could not load fontmap\n"), arg);
			else {
				DEBUG((DBG_FMAP,
					"%s: installing fontmap\n", arg));
				mdvi_install_fontmap(ent);
				count++;
			}
		} else if(STRNEQ(line, "encoding", 8)) {
			arg = getstring(line + 8, " \t", &line); *line = 0;
			if(arg && *arg)
				register_encoding(arg, 1);
		} else if(STRNEQ(line, "default-encoding", 16)) {
			arg = getstring(line + 16, " \t", &line); *line = 0;
			if(mdvi_set_default_encoding(arg) < 0)
				mdvi_warning(_("%s: could not set as default encoding\n"),
					     arg);
		} else if(STRNEQ(line, "psfontpath", 10)) {
			arg = getstring(line + 11, " \t", &line); *line = 0;
			if(!psinitialized)
				ps_init_default_paths();
			if(psfontdir)
				mdvi_free(psfontdir);
			psfontdir = kpse_path_expand(arg);
		} else if(STRNEQ(line, "pslibpath", 9)) {
			arg = getstring(line + 10, " \t", &line); *line = 0;
			if(!psinitialized)
				ps_init_default_paths();
			if(pslibdir)
				mdvi_free(pslibdir);
			pslibdir = kpse_path_expand(arg);
		} else if(STRNEQ(line, "psfontmap", 9)) {
			arg = getstring(line + 9, " \t", &line); *line = 0;
			if(mdvi_ps_read_fontmap(arg) < 0)
				mdvi_warning("%s: %s: could not read PS fontmap\n",
					     config, arg);
		}
	}
	fclose(in);
	dstring_reset(&input);
	fontmaps_loaded = 1;
	DEBUG((DBG_FMAP, "%d files installed, %d fontmaps\n",
		count, fontmaps.count));
	return count;
}

int	mdvi_query_fontmap(DviFontMapInfo *info, const char *fontname)
{
	DviFontMapEnt *ent;

	if(!fontmaps_loaded && mdvi_init_fontmaps() < 0)
		return -1;
	ent = (DviFontMapEnt *)mdvi_hash_lookup(&maptable, MDVI_KEY(fontname));

	if(ent == NULL)
		return -1;
	info->psname   = ent->psname;
	info->encoding = ent->encoding;
	info->fontfile = ent->fontfile;
	info->extend   = ent->extend;
	info->slant    = ent->slant;
	info->fullfile = ent->fullfile;

	return 0;
}

int	mdvi_add_fontmap_file(const char *name, const char *fullpath)
{
	DviFontMapEnt *ent;

	if(!fontmaps_loaded && mdvi_init_fontmaps() < 0)
		return -1;
	ent = (DviFontMapEnt *)mdvi_hash_lookup(&maptable, MDVI_KEY(name));
	if(ent == NULL)
		return -1;
	if(ent->fullfile)
		mdvi_free(ent->fullfile);
	ent->fullfile = mdvi_strdup(fullpath);
	return 0;
}


void	mdvi_flush_encodings(void)
{
	DviEncoding *enc;

	if(enctable.nbucks == 0)
		return;

	DEBUG((DBG_FMAP, "flushing %d encodings\n", encodings.count));
	/* asked to remove all encodings */
	for(; (enc = (DviEncoding *)encodings.head); ) {
		encodings.head = LIST(enc->next);
		if((enc != tex_text_encoding && enc->links) || enc->links > 1) {
			mdvi_warning(_("encoding vector `%s' is in use\n"),
				     enc->name);
		}
		destroy_encoding(enc);
	}
	/* destroy the static encoding */
	if(tex_text_encoding->nametab.buckets)
		mdvi_hash_reset(&tex_text_encoding->nametab, 0);
	mdvi_hash_reset(&enctable, 0);
	mdvi_hash_reset(&enctable_file, 0);
}

void	mdvi_flush_fontmaps(void)
{
	DviFontMapEnt *ent;

	if(!fontmaps_loaded)
		return;

	DEBUG((DBG_FMAP, "flushing %d fontmaps\n", fontmaps.count));
	for(; (ent = (DviFontMapEnt *)fontmaps.head); ) {
		fontmaps.head = LIST(ent->next);
		free_ent(ent);
	}
	mdvi_hash_reset(&maptable, 0);
	fontmaps_loaded = 0;
}

/* reading of PS fontmaps */

void	ps_init_default_paths(void)
{
	char	*kppath;
	char	*kfpath;

	ASSERT(psinitialized == 0);

	kppath = getenv("GS_LIB");
	kfpath = getenv("GS_FONTPATH");

	if(kppath != NULL)
		pslibdir = kpse_path_expand(kppath);
	if(kfpath != NULL)
		psfontdir = kpse_path_expand(kfpath);

	listh_init(&psfonts);
	mdvi_hash_create(&pstable, PSMAP_HASH_SIZE);
	psinitialized = 1;
}

int	mdvi_ps_read_fontmap(const char *name)
{
	char	*fullname;
	FILE	*in;
	Dstring	dstr;
	char	*line;
	int	count = 0;

	if(!psinitialized)
		ps_init_default_paths();
	if(pslibdir)
		fullname = kpse_path_search(pslibdir, name, 1);
	else
		fullname = (char *)name;
	in = fopen(fullname, "rb");
	if(in == NULL) {
		if(fullname != name)
			mdvi_free(fullname);
		return -1;
	}
	dstring_init(&dstr);

	while((line = dgets(&dstr, in)) != NULL) {
		char	*name;
		char	*mapname;
		const char *ext;
		PSFontMap *ps;

		SKIPSP(line);
		/* we're looking for lines of the form
		 *  /FONT-NAME    (fontfile)
		 *  /FONT-NAME    /FONT-ALIAS
		 */
		if(*line != '/')
			continue;
		name = getword(line + 1, " \t", &line);
		if(*line) *line++ = 0;
		mapname = getword(line, " \t", &line);
		if(*line) *line++ = 0;

		if(!name || !mapname || !*name)
			continue;
		if(*mapname == '(') {
			char	*end;

			mapname++;
			for(end = mapname; *end && *end != ')'; end++);
			*end = 0;
		}
		if(!*mapname)
			continue;
		/* dont add `.gsf' fonts, which require a full blown
		 * PostScript interpreter */
		ext = file_extension(mapname);
		if(ext && STREQ(ext, "gsf")) {
			DEBUG((DBG_FMAP, "(ps) %s: font `%s' ignored\n",
				name, mapname));
			continue;
		}
		ps = (PSFontMap *)mdvi_hash_lookup(&pstable, MDVI_KEY(name));
		if(ps != NULL) {
			if(STREQ(ps->mapname, mapname))
				continue;
			DEBUG((DBG_FMAP,
				"(ps) replacing font `%s' (%s) by `%s'\n",
				name, ps->mapname, mapname));
			mdvi_free(ps->mapname);
			ps->mapname = mdvi_strdup(mapname);
			if(ps->fullname) {
				mdvi_free(ps->fullname);
				ps->fullname = NULL;
			}
		} else {
			DEBUG((DBG_FMAP, "(ps) adding font `%s' as `%s'\n",
				name, mapname));
			ps = xalloc(PSFontMap);
			ps->psname   = mdvi_strdup(name);
			ps->mapname  = mdvi_strdup(mapname);
			ps->fullname = NULL;
			listh_append(&psfonts, LIST(ps));
			mdvi_hash_add(&pstable, MDVI_KEY(ps->psname),
				ps, MDVI_HASH_UNCHECKED);
			count++;
		}
	}
	fclose(in);
	dstring_reset(&dstr);

	DEBUG((DBG_FMAP, "(ps) %s: %d PostScript fonts registered\n",
		fullname, count));
	return 0;
}

void	mdvi_ps_flush_fonts(void)
{
	PSFontMap *map;

	if(!psinitialized)
		return;
	DEBUG((DBG_FMAP, "(ps) flushing PS font map (%d) entries\n",
		psfonts.count));
	mdvi_hash_reset(&pstable, 0);
	for(; (map = (PSFontMap *)psfonts.head); ) {
		psfonts.head = LIST(map->next);
		mdvi_free(map->psname);
		mdvi_free(map->mapname);
		if(map->fullname)
			mdvi_free(map->fullname);
		mdvi_free(map);
	}
	listh_init(&psfonts);
	if(pslibdir) {
		mdvi_free(pslibdir);
		pslibdir = NULL;
	}
	if(psfontdir) {
		mdvi_free(psfontdir);
		psfontdir = NULL;
	}
	psinitialized = 0;
}

char	*mdvi_ps_find_font(const char *psname)
{
	PSFontMap *map, *smap;
	char	*filename;
	int	recursion_limit = 32;

	DEBUG((DBG_FMAP, "(ps) resolving PS font `%s'\n", psname));
	if(!psinitialized)
		return NULL;
	map = (PSFontMap *)mdvi_hash_lookup(&pstable, MDVI_KEY(psname));
	if(map == NULL)
		return NULL;
	if(map->fullname)
		return mdvi_strdup(map->fullname);

	/* is it an alias? */
	smap = map;
	while(recursion_limit-- > 0 && smap && *smap->mapname == '/')
		smap = (PSFontMap *)mdvi_hash_lookup(&pstable,
			MDVI_KEY(smap->mapname + 1));
	if(smap == NULL) {
		if(recursion_limit == 0)
			DEBUG((DBG_FMAP,
				"(ps) %s: possible loop in PS font map\n",
				psname));
		return NULL;
	}

	if(psfontdir)
		filename = kpse_path_search(psfontdir, smap->mapname, 1);
	else if(file_exists(map->mapname))
		filename = mdvi_strdup(map->mapname);
	else
		filename = NULL;
	if(filename)
		map->fullname = mdvi_strdup(filename);

	return filename;
}

/*
 * To get metric info for a font, we proceed as follows:
 *  - We try to find NAME.<tfm,ofm,afm>.
 *  - We query the fontmap for NAME.
 *  - We get back a PSNAME, and use to find the file in the PS font map.
 *  - We get the PSFONT file name, replace its extension by "afm" and
 *  lookup the file in GS's font search path.
 *  - We finally read the data, transform it as specified in our font map,
 *  and return it to the caller. The new data is left in the font metrics
 *  cache, so the next time it will be found at the first step (when we look
 *  up NAME.afm).
 *
 * The name `_ps_' in this function is not meant to imply that it can be
 * used for Type1 fonts only. It should be usable for TrueType fonts as well.
 *
 * The returned metric info is subjected to the same caching mechanism as
 * all the other metric data, as returned by get_font_metrics(). One should
 * not modify the returned data at all, and it should be disposed with
 * free_font_metrics().
 */
TFMInfo *mdvi_ps_get_metrics(const char *fontname)
{
	TFMInfo *info;
	DviFontMapInfo map;
	char	buffer[64]; /* to avoid mallocs */
	char	*psfont;
	char	*basefile;
	char	*afmfile;
	char	*ext;
	int	baselen;
	int	nc;
	TFMChar	*ch;
	double	efactor;
	double	sfactor;

	DEBUG((DBG_FMAP, "(ps) %s: looking for metric data\n", fontname));
	info = get_font_metrics(fontname, DviFontAny, NULL);
	if(info != NULL)
		return info;

	/* query the fontmap */
	if(mdvi_query_fontmap(&map, fontname) < 0 || !map.psname)
		return NULL;

	/* get the PS font */
	psfont = mdvi_ps_find_font(map.psname);
	if(psfont == NULL)
		return NULL;
	DEBUG((DBG_FMAP, "(ps) %s: found as PS font `%s'\n",
		fontname, psfont));
	/* replace its extension */
	basefile = strrchr(psfont, '/');
	if(basefile == NULL)
		basefile = psfont;
	baselen = strlen(basefile);
	ext = strrchr(basefile, '.');
	if(ext != NULL)
		*ext = 0;
	if(baselen + 4 < 64)
		afmfile = &buffer[0];
	else
		afmfile = mdvi_malloc(baselen + 5);
	strcpy(afmfile, basefile);
	strcpy(afmfile + baselen, ".afm");
	/* we don't need this anymore */
	mdvi_free(psfont);
	DEBUG((DBG_FMAP, "(ps) %s: looking for `%s'\n",
		fontname, afmfile));
	/* lookup the file */
	psfont = kpse_path_search(psfontdir, afmfile, 1);
	/* don't need this anymore */
	if(afmfile != &buffer[0])
		mdvi_free(afmfile);
	if(psfont != NULL) {
		info = get_font_metrics(fontname, DviFontAFM, psfont);
		mdvi_free(psfont);
	} else
		info = NULL;
	if(info == NULL || (!map.extend && !map.slant))
		return info;

	/*
	 * transform the data as prescribed -- keep in mind that `info'
	 * points to CACHED data, so we're modifying the metric cache
	 * in place.
	 */

#define DROUND(x)	((x) >= 0 ? floor((x) + 0.5) : ceil((x) - 0.5))
#define TRANSFORM(x,y)	DROUND(efactor * (x) + sfactor * (y))

	efactor = (double)map.extend / 10000.0;
	sfactor = (double)map.slant / 10000.0;
	DEBUG((DBG_FMAP, "(ps) %s: applying extend=%f, slant=%f\n",
		efactor, sfactor));

	nc = info->hic - info->loc + 1;
	for(ch = info->chars; ch < info->chars + nc; ch++) {
		/* the AFM bounding box is:
		 *    wx = ch->advance
		 *   llx = ch->left
		 *   lly = -ch->depth
		 *   urx = ch->right
		 *   ury = ch->height
		 * what we do here is transform wx, llx, and urx by
		 *         newX = efactor * oldX + sfactor * oldY
		 * where for `wx' oldY = 0. Also, these numbers are all in
		 * TFM units (i.e. TFM's fix-words, which is just the actual
		 * number times 2^20, no need to do anything to it).
		 */
		if(ch->present) {
			ch->advance = TRANSFORM(ch->advance, 0);
			ch->left    = TRANSFORM(ch->left, -ch->depth);
			ch->right   = TRANSFORM(ch->right, ch->height);
		}
	}

	return info;
}