Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/pdf/xpdf/FontEncoding.cc
blob: 3dd2e3bd80575447924d4a2992a1b629b86bd1fb (plain)
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
//========================================================================
//
// FontEncoding.cc
//
// Copyright 1999 Derek B. Noonburg
//
//========================================================================

#ifdef __GNUC__
#pragma implementation
#endif

#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include "gmem.h"
#include "FontEncoding.h"

//------------------------------------------------------------------------
// FontEncoding
//------------------------------------------------------------------------

inline int FontEncoding::hash(char *name) {
  int h;

  h = name[0];
  if (name[1])
    h = h * 61 + name[1];
  return h % fontEncHashSize;
}

FontEncoding::FontEncoding() {
  int i;

  encoding = (char **)gmalloc(256 * sizeof(char *));
  size = 256;
  freeEnc = gTrue;
  for (i = 0; i < 256; ++i)
    encoding[i] = NULL;
  for (i = 0; i < fontEncHashSize; ++i)
    hashTab[i] = -1;
}

FontEncoding::FontEncoding(char **encoding, int size) {
  int i;

  this->encoding = encoding;
  this->size = size;
  freeEnc = gFalse;
  for (i = 0; i < fontEncHashSize; ++i)
    hashTab[i] = -1;
  for (i = 0; i < size; ++i) {
    if (encoding[i])
      addChar1(i, encoding[i]);
  }
}

FontEncoding::FontEncoding(FontEncoding *fontEnc) {
  int i;

  encoding = (char **)gmalloc(fontEnc->size * sizeof(char *));
  size = fontEnc->size;
  freeEnc = gTrue;
  for (i = 0; i < size; ++i) {
    encoding[i] =
      fontEnc->encoding[i] ? copyString(fontEnc->encoding[i]) : NULL;
  }
  memcpy(hashTab, fontEnc->hashTab, fontEncHashSize * sizeof(short));
}

void FontEncoding::addChar(int code, char *name) {
  int h, i;

  // replace character associated with code
  if (encoding[code]) {
    h = hash(encoding[code]);
    for (i = 0; i < fontEncHashSize; ++i) {
      if (hashTab[h] == code) {
	hashTab[h] = -2;
	break;
      }
      if (++h == fontEncHashSize)
	h = 0;
    }
    gfree(encoding[code]);
  }

  // associate name with code
  encoding[code] = name;

  // insert name in hash table
  addChar1(code, name);
}

void FontEncoding::addChar1(int code, char *name) {
  int h, i, code2;

  // insert name in hash table
  h = hash(name); 
  for (i = 0; i < fontEncHashSize; ++i) {
    code2 = hashTab[h];
    if (code2 < 0) {
      hashTab[h] = code;
      break;
    } else if (encoding[code2] && !strcmp(encoding[code2], name)) {
      // keep the highest code for each char -- this is needed because
      // X won't display chars with codes < 32
      if (code > code2)
	hashTab[h] = code;
      break;
    }
    if (++h == fontEncHashSize)
      h = 0;
  }
}

FontEncoding::~FontEncoding() {
  int i;

  if (freeEnc) {
    for (i = 0; i < size; ++i) {
      if (encoding[i])
	gfree(encoding[i]);
    }
    gfree(encoding);
  }
}

int FontEncoding::getCharCode(char *name) {
  int h, i, code;

  h = hash(name);
  for (i = 0; i < fontEncHashSize; ++i) {
    code = hashTab[h];
    if (code == -1 ||
	(code >= 0 && encoding[code] && !strcmp(encoding[code], name)))
      return code;
    if (++h >= fontEncHashSize)
      h = 0;
  }
  return -1;
}