generate.py 5.0 KB

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