generate.py 5.1 KB

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