Replacements for stl containers
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
diff --git a/include/FTCharToGlyphIndexMap.h b/include/FTCharToGlyphIndexMap.h
new file mode 100644
index 0000000..f6aa8c7
--- /dev/null
+++ b/include/FTCharToGlyphIndexMap.h
@@ -0,0 +1,149 @@
+#ifndef __FTCharToGlyphIndexMap__
+#define __FTCharToGlyphIndexMap__
+
+/*
+ Provides a non-STL alternative to the STL map<unsigned long, unsigned long>
+ which maps character codes to glyph indices inside FTCharmap.
+
+ Implementation:
+ - NumberOfBuckets buckets are considered.
+ - Each bucket has BucketSize entries.
+ - When the glyph index for the character code C has to be stored, the
+ bucket this character belongs to is found using 'C div BucketSize'.
+ If this bucket has not been allocated yet, do it now.
+ The entry in the bucked is found using 'C mod BucketSize'.
+ If it is set to IndexNotFound, then the glyph entry has not been set.
+ - Try to mimic the calls made to the STL map API.
+
+ Caveats:
+ - The glyph index is now a signed long instead of unsigned long, so
+ the special value IndexNotFound (= -1) can be used to specify that the
+ glyph index has not been stored yet.
+*/
+
+#include <cstdlib>
+
+#include "FTGL.h"
+
+class FTGL_EXPORT FTCharToGlyphIndexMap
+{
+ public:
+
+ typedef unsigned long CharacterCode;
+ typedef signed long GlyphIndex;
+
+ enum
+ {
+ NumberOfBuckets = 256,
+ BucketSize = 256,
+ IndexNotFound = -1
+ };
+
+ FTCharToGlyphIndexMap();
+
+ virtual ~FTCharToGlyphIndexMap();
+
+ void clear();
+
+ const GlyphIndex* find( CharacterCode c);
+
+ void insert( CharacterCode c, GlyphIndex g);
+
+ private:
+ GlyphIndex** Indices;
+};
+
+
+inline FTCharToGlyphIndexMap::FTCharToGlyphIndexMap()
+{
+ this->Indices = 0;
+}
+
+
+inline FTCharToGlyphIndexMap::~FTCharToGlyphIndexMap()
+{
+ if( this->Indices)
+ {
+ // Free all buckets
+ this->clear();
+
+ // Free main structure
+ delete [] this->Indices;
+ this->Indices = 0;
+ }
+}
+
+
+// Free all buckets
+inline void FTCharToGlyphIndexMap::clear()
+{
+ if(this->Indices)
+ {
+ for( int i = 0; i < FTCharToGlyphIndexMap::NumberOfBuckets; i++)
+ {
+ if( this->Indices[i])
+ {
+ delete [] this->Indices[i];
+ this->Indices[i] = 0;
+ }
+ }
+ }
+}
+
+
+// Find index corresponding to char code
+inline const FTCharToGlyphIndexMap::GlyphIndex* FTCharToGlyphIndexMap::find(FTCharToGlyphIndexMap::CharacterCode c)
+{
+ if( !this->Indices)
+ {
+ return 0;
+ }
+
+ // Find position of char code in buckets
+ div_t pos = div( c, FTCharToGlyphIndexMap::BucketSize);
+
+ if( !this->Indices[pos.quot])
+ {
+ return 0;
+ }
+
+ const FTCharToGlyphIndexMap::GlyphIndex *ptr = &this->Indices[pos.quot][pos.rem];
+ if( *ptr == FTCharToGlyphIndexMap::IndexNotFound)
+ {
+ return 0;
+ }
+
+ return ptr;
+}
+
+
+// Insert index corresponding to char code
+inline void FTCharToGlyphIndexMap::insert(FTCharToGlyphIndexMap::CharacterCode c,
+ FTCharToGlyphIndexMap::GlyphIndex g)
+{
+ if( !this->Indices)
+ {
+ this->Indices = new GlyphIndex* [FTCharToGlyphIndexMap::NumberOfBuckets];
+ for( int i = 0; i < FTCharToGlyphIndexMap::NumberOfBuckets; i++)
+ {
+ this->Indices[i] = 0;
+ }
+ }
+
+ // Find position of char code in buckets
+ div_t pos = div(c, FTCharToGlyphIndexMap::BucketSize);
+
+ // Allocate bucket if does not exist yet
+ if( !this->Indices[pos.quot])
+ {
+ this->Indices[pos.quot] = new GlyphIndex [FTCharToGlyphIndexMap::BucketSize];
+ for( int i = 0; i < FTCharToGlyphIndexMap::BucketSize; i++)
+ {
+ this->Indices[pos.quot][i] = FTCharToGlyphIndexMap::IndexNotFound;
+ }
+ }
+
+ this->Indices[pos.quot][pos.rem] = g;
+}
+
+#endif // __FTCharToGlyphIndexMap__
diff --git a/include/FTVector.h b/include/FTVector.h
new file mode 100644
index 0000000..4203e3e
--- /dev/null
+++ b/include/FTVector.h
@@ -0,0 +1,261 @@
+/*
+ Provides a non-STL alternative to the STL vector<...>
+ used inside FTvectorizer and FTGlyphContainer.
+
+ Implementation:
+ - Dynamically resizable container.
+ - Try to mimic the calls made to the STL vector API.
+
+ Caveats:
+ - No templates, use poor macro substition where :
+ FT_VECTOR_CLASS_NAME: is the name of the class
+ FT_VECTOR_ITEM_TYPE: is the type of the object to store
+*/
+
+#define FT_VECTOR_CLASS_DEBUG 0
+
+#include "FTGL.h"
+
+class FTGL_EXPORT FT_VECTOR_CLASS_NAME
+{
+public:
+
+ typedef FT_VECTOR_ITEM_TYPE value_type;
+ typedef value_type& reference;
+ typedef const value_type& const_reference;
+ typedef value_type* iterator;
+ typedef const value_type* const_iterator;
+ typedef size_t size_type;
+
+ FT_VECTOR_CLASS_NAME();
+
+ virtual ~FT_VECTOR_CLASS_NAME();
+
+ FT_VECTOR_CLASS_NAME& operator =(const FT_VECTOR_CLASS_NAME& v);
+
+ size_type size() const;
+ size_type capacity() const;
+ iterator begin();
+ iterator end();
+ const_iterator begin() const;
+ const_iterator end() const;
+ bool empty() const;
+
+ reference operator [](size_type pos);
+ const_reference operator [](size_type pos) const;
+
+ void clear();
+ void reserve(size_type);
+ void push_back(const value_type&);
+ void resize(size_type, value_type);
+
+protected:
+
+ void expand(size_type = 0);
+
+private:
+
+ size_type Size;
+ size_type NumberOfItems;
+ value_type* Items;
+};
+
+
+inline
+FT_VECTOR_CLASS_NAME::FT_VECTOR_CLASS_NAME()
+{
+ this->Size = this->NumberOfItems = 0;
+ this->Items = 0;
+}
+
+
+inline
+FT_VECTOR_CLASS_NAME::~FT_VECTOR_CLASS_NAME()
+{
+ this->clear();
+}
+
+
+inline
+FT_VECTOR_CLASS_NAME& FT_VECTOR_CLASS_NAME::operator =(const FT_VECTOR_CLASS_NAME& v)
+{
+ // Warning: the vector is not cleared and resized to v capacity for
+ // efficiency reasons.
+ // this->clear();
+ this->reserve(v.capacity());
+
+ iterator ptr = this->begin();
+ const_iterator vbegin = v.begin();
+ const_iterator vend = v.end();
+
+ while (vbegin != vend)
+ {
+ *ptr++ = *vbegin++;
+ }
+ this->NumberOfItems = v.size();
+ return *this;
+}
+
+
+inline
+FT_VECTOR_CLASS_NAME::size_type FT_VECTOR_CLASS_NAME::size() const
+{
+ return this->NumberOfItems;
+}
+
+
+inline
+FT_VECTOR_CLASS_NAME::size_type FT_VECTOR_CLASS_NAME::capacity() const
+{
+ return this->Size;
+}
+
+
+inline
+FT_VECTOR_CLASS_NAME::iterator FT_VECTOR_CLASS_NAME::begin()
+{
+ return this->Items;
+}
+
+
+inline
+FT_VECTOR_CLASS_NAME::iterator FT_VECTOR_CLASS_NAME::end()
+{
+ return this->begin() + this->size();
+}
+
+
+inline
+FT_VECTOR_CLASS_NAME::const_iterator FT_VECTOR_CLASS_NAME::begin() const
+{
+ return this->Items;
+}
+
+
+inline
+FT_VECTOR_CLASS_NAME::const_iterator FT_VECTOR_CLASS_NAME::end() const
+{
+ return this->begin() + this->size();
+}
+
+
+inline
+bool FT_VECTOR_CLASS_NAME::empty() const
+{
+ return this->size() == 0;
+}
+
+
+inline
+FT_VECTOR_CLASS_NAME::reference FT_VECTOR_CLASS_NAME::operator [](FT_VECTOR_CLASS_NAME::size_type pos)
+{
+ return (*(begin() + pos));
+}
+
+
+inline
+FT_VECTOR_CLASS_NAME::const_reference FT_VECTOR_CLASS_NAME::operator [](FT_VECTOR_CLASS_NAME::size_type pos) const
+{
+ return (*(begin() + pos));
+}
+
+
+inline
+void FT_VECTOR_CLASS_NAME::clear()
+{
+ if (this->Size)
+ {
+#if FT_VECTOR_CLASS_DEBUG
+ printf("FT_VECTOR_CLASS_NAME: clear() (%d / %d)\n",
+ this->size(), this->capacity());
+#endif
+ delete [] this->Items;
+ this->Size = this->NumberOfItems = 0;
+ this->Items = 0;
+ }
+}
+
+
+inline
+void FT_VECTOR_CLASS_NAME::expand(size_type size_hint)
+{
+#if FT_VECTOR_CLASS_DEBUG
+ printf("FT_VECTOR_CLASS_NAME: expand() (%d / %d) hint: %d\n",
+ this->size(), this->capacity(), size_hint);
+#endif
+
+ // Allocate new vector (size doubles)
+
+ size_type new_size = (this->size() == 0) ? 256 : this->size() * 2;
+ if (size_hint)
+ {
+ while (new_size < size_hint)
+ {
+ new_size *= 2;
+ }
+ }
+
+ value_type *ptr = new value_type[new_size];
+
+ // Copy values to new vector
+
+ iterator begin = this->begin();
+ iterator end = this->end();
+ while (begin != end)
+ {
+ *ptr++ = *begin++;
+ }
+
+ // Deallocate old vector and use new vector
+
+ delete [] this->Items;
+ this->Items = ptr;
+ this->Size = new_size;
+}
+
+
+inline
+void FT_VECTOR_CLASS_NAME::reserve(size_type n)
+{
+#if FT_VECTOR_CLASS_DEBUG
+ printf("FT_VECTOR_CLASS_NAME: reserve() (%d / %d) n: %d\n",
+ this->size(), this->capacity(), n);
+#endif
+ if (this->capacity() < n)
+ {
+ this->expand(n);
+ }
+}
+
+
+inline
+void FT_VECTOR_CLASS_NAME::push_back(const value_type& x)
+{
+#if FT_VECTOR_CLASS_DEBUG
+ printf("FT_VECTOR_CLASS_NAME: push_back() (%d / %d)\n",
+ this->size(), this->capacity());
+#endif
+ if (this->size() == this->capacity())
+ {
+ this->expand();
+ }
+ (*this)[this->size()] = x;
+ this->NumberOfItems++;
+}
+
+
+inline
+void FT_VECTOR_CLASS_NAME::resize(size_type n, value_type x)
+{
+#if FT_VECTOR_CLASS_DEBUG
+ printf("FT_VECTOR_CLASS_NAME: resize() (%d / %d) n: %d\n",
+ this->size(), this->capacity(), n);
+#endif
+ this->reserve(n);
+ iterator end = this->end();
+ iterator end_capacity = this->begin() + this->capacity();
+ while (end != end_capacity)
+ {
+ *end++ = x;
+ }
+}
diff --git a/src/FTContourVector.h b/src/FTContourVector.h
new file mode 100644
index 0000000..7365ae5
--- /dev/null
+++ b/src/FTContourVector.h
@@ -0,0 +1,21 @@
+#ifndef __FTContourVector__
+#define __FTContourVector__
+
+/*
+ Provides a non-STL alternative to the STL vector<...>
+ used inside FTVectoriser.
+*/
+
+#ifdef FT_VECTOR_CLASS_NAME
+#undef FT_VECTOR_CLASS_NAME
+#endif
+#define FT_VECTOR_CLASS_NAME FTContourVector
+
+#ifdef FT_VECTOR_ITEM_TYPE
+#undef FT_VECTOR_ITEM_TYPE
+#endif
+#define FT_VECTOR_ITEM_TYPE FTContour*
+
+#include "FTVector.h"
+
+#endif // __FTContourVector__
diff --git a/src/FTGlyphVector.h b/src/FTGlyphVector.h
new file mode 100644
index 0000000..53818a5
--- /dev/null
+++ b/src/FTGlyphVector.h
@@ -0,0 +1,21 @@
+#ifndef __FTGlyphVector__
+#define __FTGlyphVector__
+
+/*
+ Provides a non-STL alternative to the STL vector<...>
+ used inside FTGlyphContainer.
+*/
+
+#ifdef FT_VECTOR_CLASS_NAME
+#undef FT_VECTOR_CLASS_NAME
+#endif
+#define FT_VECTOR_CLASS_NAME FTGlyphVector
+
+#ifdef FT_VECTOR_ITEM_TYPE
+#undef FT_VECTOR_ITEM_TYPE
+#endif
+#define FT_VECTOR_ITEM_TYPE FTGlyph*
+
+#include "FTVector.h"
+
+#endif // __FTGlyphVector__
diff --git a/src/FTPointVector.h b/src/FTPointVector.h
new file mode 100644
index 0000000..e8e4244
--- /dev/null
+++ b/src/FTPointVector.h
@@ -0,0 +1,21 @@
+#ifndef __FTPointVector__
+#define __FTPointVector__
+
+/*
+ Provides a non-STL alternative to the STL vector<...>
+ used inside FTVectoriser.
+*/
+
+#ifdef FT_VECTOR_CLASS_NAME
+#undef FT_VECTOR_CLASS_NAME
+#endif
+#define FT_VECTOR_CLASS_NAME FTPointVector
+
+#ifdef FT_VECTOR_ITEM_TYPE
+#undef FT_VECTOR_ITEM_TYPE
+#endif
+#define FT_VECTOR_ITEM_TYPE ftPoint
+
+#include "FTVector.h"
+
+#endif // __FTPointVector__
diff --git a/src/FTTesselationVector.h b/src/FTTesselationVector.h
new file mode 100644
index 0000000..165975d
--- /dev/null
+++ b/src/FTTesselationVector.h
@@ -0,0 +1,21 @@
+#ifndef __FTTesselationVector__
+#define __FTTesselationVector__
+
+/*
+ Provides a non-STL alternative to the STL vector<...>
+ used inside FTVectoriser.
+*/
+
+#ifdef FT_VECTOR_CLASS_NAME
+#undef FT_VECTOR_CLASS_NAME
+#endif
+#define FT_VECTOR_CLASS_NAME FTTesselationVector
+
+#ifdef FT_VECTOR_ITEM_TYPE
+#undef FT_VECTOR_ITEM_TYPE
+#endif
+#define FT_VECTOR_ITEM_TYPE FTTesselation*
+
+#include "FTVector.h"
+
+#endif // __FTTesselationVector__