generate.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!/usr/bin/env python3
  2. import os
  3. import re
  4. rootDir = os.path.join(os.path.dirname(__file__), '..')
  5. sourceList = [
  6. 'core/arithmetics.hpp',
  7. 'core/equation-solver.h',
  8. 'core/equation-solver.cpp',
  9. 'core/Vector2.h',
  10. 'core/Vector2.cpp',
  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.h',
  18. 'core/SignedDistance.cpp',
  19. 'core/Scanline.h',
  20. 'core/Scanline.cpp',
  21. 'core/EdgeColor.h',
  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/generator-config.h',
  40. 'core/msdf-error-correction.h',
  41. 'core/msdf-error-correction.cpp',
  42. 'core/MSDFErrorCorrection.h',
  43. 'core/MSDFErrorCorrection.cpp',
  44. 'core/msdfgen.cpp',
  45. 'ext/import-font.h',
  46. 'ext/import-font.cpp',
  47. 'ext/resolve-shape-geometry.h',
  48. 'ext/resolve-shape-geometry.cpp',
  49. 'msdfgen.h'
  50. ]
  51. header = """
  52. #pragma once
  53. #define MSDFGEN_USE_CPP11
  54. #define MSDFGEN_USE_FREETYPE
  55. #include <cstddef>
  56. #include <cstdlib>
  57. #include <cstring>
  58. #include <cmath>
  59. #include <vector>
  60. """
  61. source = """
  62. #include "msdfgen.h"
  63. #include <algorithm>
  64. #include <queue>
  65. #ifdef MSDFGEN_USE_FREETYPE
  66. #include <ft2build.h>
  67. #include FT_FREETYPE_H
  68. #include FT_OUTLINE_H
  69. #ifndef MSDFGEN_DISABLE_VARIABLE_FONTS
  70. #include FT_MULTIPLE_MASTERS_H
  71. #endif
  72. #endif
  73. #ifdef _MSC_VER
  74. #pragma warning(disable : 4456 4458)
  75. #endif
  76. #ifndef M_PI
  77. #define M_PI 3.1415926535897932384626433832795
  78. #endif
  79. """
  80. with open(os.path.join(rootDir, 'LICENSE.txt'), 'r') as file:
  81. license = file.read()
  82. license = '\n'.join([' * '+line for line in license.strip().split('\n')])
  83. for filename in sourceList:
  84. with open(os.path.join(rootDir, *filename.split('/')), 'r') as file:
  85. src = file.read()
  86. src = '\n'.join([line for line in src.split('\n') if not re.match(r'^\s*#(include\s.*|pragma\s+once)\s*$', line)])
  87. if filename.startswith('ext/import-font.'):
  88. src = '#ifdef MSDFGEN_USE_FREETYPE\n\n'+src+'\n\n#endif\n\n'
  89. if filename.endswith('.h') or filename.endswith('.hpp'):
  90. header += '\n\n'+src
  91. if filename.endswith('.cpp'):
  92. source += '\n\n'+src
  93. 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'
  94. 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'
  95. header = """
  96. /*
  97. * MULTI-CHANNEL SIGNED DISTANCE FIELD GENERATOR
  98. * ---------------------------------------------
  99. * A utility by Viktor Chlumsky, (c) 2014 - 2023
  100. * https://github.com/Chlumsky/msdfgen
  101. * Published under the MIT license
  102. *
  103. * The technique used to generate multi-channel distance fields in this code
  104. * was developed by Viktor Chlumsky in 2014 for his master's thesis,
  105. * "Shape Decomposition for Multi-Channel Distance Fields". It provides improved
  106. * quality of sharp corners in glyphs and other 2D shapes compared to monochrome
  107. * distance fields. To reconstruct an image of the shape, apply the median of three
  108. * operation on the triplet of sampled signed distance values.
  109. *
  110. */
  111. """+header
  112. source = """
  113. /*
  114. * MULTI-CHANNEL SIGNED DISTANCE FIELD GENERATOR
  115. * ---------------------------------------------
  116. * https://github.com/Chlumsky/msdfgen
  117. *
  118. """+license+"""
  119. *
  120. */
  121. """+source
  122. with open(os.path.join(os.path.dirname(__file__), 'msdfgen.h'), 'w') as file:
  123. file.write(header)
  124. with open(os.path.join(os.path.dirname(__file__), 'msdfgen.cpp'), 'w') as file:
  125. file.write(source)