generate.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!/usr/bin/env python3
  2. import os
  3. import re
  4. rootDir = os.path.join(os.path.dirname(__file__), '..')
  5. sourceList = [
  6. 'core/base.h',
  7. 'core/arithmetics.hpp',
  8. 'core/equation-solver.h',
  9. 'core/equation-solver.cpp',
  10. 'core/Vector2.hpp',
  11. 'core/pixel-conversion.hpp',
  12. 'core/BitmapRef.hpp',
  13. 'core/Bitmap.h',
  14. 'core/Bitmap.hpp',
  15. 'core/Projection.h',
  16. 'core/Projection.cpp',
  17. 'core/SignedDistance.hpp',
  18. 'core/Scanline.h',
  19. 'core/Scanline.cpp',
  20. 'core/EdgeColor.h',
  21. 'core/bezier-solver.hpp',
  22. 'core/edge-segments.h',
  23. 'core/edge-segments.cpp',
  24. 'core/EdgeHolder.h',
  25. 'core/EdgeHolder.cpp',
  26. 'core/Contour.h',
  27. 'core/Contour.cpp',
  28. 'core/Shape.h',
  29. 'core/Shape.cpp',
  30. 'core/bitmap-interpolation.hpp',
  31. 'core/edge-coloring.h',
  32. 'core/edge-coloring.cpp',
  33. 'core/edge-selectors.h',
  34. 'core/edge-selectors.cpp',
  35. 'core/contour-combiners.h',
  36. 'core/contour-combiners.cpp',
  37. 'core/ShapeDistanceFinder.h',
  38. 'core/ShapeDistanceFinder.hpp',
  39. 'core/approximate-sdf.h',
  40. 'core/approximate-sdf.cpp',
  41. 'core/generator-config.h',
  42. 'core/msdf-error-correction.h',
  43. 'core/msdf-error-correction.cpp',
  44. 'core/MSDFErrorCorrection.h',
  45. 'core/MSDFErrorCorrection.cpp',
  46. 'core/msdfgen.cpp',
  47. 'ext/import-font.h',
  48. 'ext/import-font.cpp',
  49. 'ext/resolve-shape-geometry.h',
  50. 'ext/resolve-shape-geometry.cpp',
  51. 'msdfgen.h'
  52. ]
  53. header = """
  54. #pragma once
  55. #define MSDFGEN_USE_CPP11
  56. #ifndef MSDFGEN_NO_FREETYPE
  57. #define MSDFGEN_USE_FREETYPE
  58. #define MSDFGEN_DISABLE_VARIABLE_FONTS
  59. #endif
  60. #include <cstddef>
  61. #include <cstdlib>
  62. #include <cstring>
  63. #include <cmath>
  64. #include <vector>
  65. """
  66. source = """
  67. #include "msdfgen.h"
  68. #include <queue>
  69. #ifdef MSDFGEN_USE_FREETYPE
  70. #include <ft2build.h>
  71. #include FT_FREETYPE_H
  72. #include FT_OUTLINE_H
  73. #ifndef MSDFGEN_DISABLE_VARIABLE_FONTS
  74. #include FT_MULTIPLE_MASTERS_H
  75. #endif
  76. #endif
  77. #if defined(__GNUC__) || defined(__clang__)
  78. #pragma GCC diagnostic push
  79. #pragma GCC diagnostic ignored "-Wshadow"
  80. #elif defined(_MSC_VER)
  81. #pragma warning(push)
  82. #pragma warning(disable : 4456 4457 4458 6246)
  83. #endif
  84. #ifndef M_PI
  85. #define M_PI 3.1415926535897932384626433832795
  86. #endif
  87. """
  88. namespaceStart = """
  89. #ifdef MSDFGEN_PARENT_NAMESPACE
  90. namespace MSDFGEN_PARENT_NAMESPACE {
  91. #endif
  92. """
  93. namespaceEnd = """
  94. #ifdef MSDFGEN_PARENT_NAMESPACE
  95. } // namespace MSDFGEN_PARENT_NAMESPACE
  96. #endif
  97. """
  98. sourceAppendix = """
  99. #if defined(__GNUC__) || defined(__clang__)
  100. #pragma GCC diagnostic pop
  101. #elif defined(_MSC_VER)
  102. #pragma warning(pop)
  103. #endif
  104. """
  105. header += namespaceStart
  106. source += namespaceStart
  107. with open(os.path.join(rootDir, 'LICENSE.txt'), 'r') as file:
  108. license = file.read()
  109. license = '\n'.join([' * '+line for line in license.strip().split('\n')])
  110. for filename in sourceList:
  111. with open(os.path.join(rootDir, *filename.split('/')), 'r') as file:
  112. src = file.read()
  113. src = '\n'.join([line for line in src.split('\n') if not re.match(r'^\s*#(include\s.*|pragma\s+once)\s*$', line)])
  114. if filename.startswith('ext/import-font.'):
  115. src = '#ifdef MSDFGEN_USE_FREETYPE\n\n'+src+'\n\n#endif\n\n'
  116. if filename.endswith('.h') or filename.endswith('.hpp'):
  117. header += '\n\n'+src
  118. if filename.endswith('.cpp'):
  119. source += '\n\n'+src
  120. header = '\n'+re.sub(r'\n{3,}', '\n\n', re.sub(r'}\s*namespace\s+msdfgen\s*{', '', re.sub(r'\/\*[^\*].*?\*\/', '', header, flags=re.DOTALL))).strip()+'\n'
  121. source = '\n'+re.sub(r'\n{3,}', '\n\n', re.sub(r'}\s*namespace\s+msdfgen\s*{', '', re.sub(r'\/\*[^\*].*?\*\/', '', source, flags=re.DOTALL))).strip()+'\n'
  122. header += namespaceEnd
  123. source += namespaceEnd
  124. header = """
  125. /*
  126. * MULTI-CHANNEL SIGNED DISTANCE FIELD GENERATOR
  127. * ---------------------------------------------
  128. * A utility by Viktor Chlumsky, (c) 2014 - 2023
  129. * https://github.com/Chlumsky/msdfgen
  130. * Published under the MIT license
  131. *
  132. * The technique used to generate multi-channel distance fields in this code
  133. * was developed by Viktor Chlumsky in 2014 for his master's thesis,
  134. * "Shape Decomposition for Multi-Channel Distance Fields". It provides improved
  135. * quality of sharp corners in glyphs and other 2D shapes compared to monochrome
  136. * distance fields. To reconstruct an image of the shape, apply the median of three
  137. * operation on the triplet of sampled signed distance values.
  138. *
  139. */
  140. """+header
  141. source = """
  142. /*
  143. * MULTI-CHANNEL SIGNED DISTANCE FIELD GENERATOR
  144. * ---------------------------------------------
  145. * https://github.com/Chlumsky/msdfgen
  146. *
  147. """+license+"""
  148. *
  149. */
  150. """+source+sourceAppendix
  151. with open(os.path.join(os.path.dirname(__file__), 'msdfgen.h'), 'w') as file:
  152. file.write(header)
  153. with open(os.path.join(os.path.dirname(__file__), 'msdfgen.cpp'), 'w') as file:
  154. file.write(source)