summaryrefslogtreecommitdiffstats
path: root/Scripts/gen_atlas
blob: a3fac7243afb79bf37ee49532dffa5985e4a7a72 (plain)
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
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.10"
# dependencies = ["Pillow"]
# ///

import argparse
import subprocess
import os
import sys
import json
from PIL import Image, ImageDraw
import random

SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
MSDF_REPO = "https://github.com/Chlumsky/msdf-atlas-gen.git"
MSDF_DIR = os.path.join(SCRIPT_DIR, "msdf-atlas-gen")
MSDF_BUILD_DIR = os.path.join(MSDF_DIR, "build")

def get_msdf_binary():
    """Return path to msdf-atlas-gen binary, building from source if needed."""
    if sys.platform == "win32":
        binary = os.path.join(MSDF_BUILD_DIR, "bin", "Debug", "msdf-atlas-gen.exe")
    else:
        binary = os.path.join(MSDF_BUILD_DIR, "bin", "msdf-atlas-gen")
    if os.path.isfile(binary):
        return binary
    print("msdf-atlas-gen not found, acquiring from source...")
    if not os.path.isdir(MSDF_DIR):
        subprocess.run(
            ["git", "clone", "--recursive", MSDF_REPO, MSDF_DIR],
            check=True,
        )
    else:
        print("Source directory exists, skipping clone.")
    os.makedirs(MSDF_BUILD_DIR, exist_ok=True)
    subprocess.run(
        [
            "cmake", "..",
            "-DCMAKE_BUILD_TYPE=Release",
            "-DMSDF_ATLAS_USE_VCPKG=OFF",
            "-DMSDF_ATLAS_USE_SKIA=OFF",
            "-DMSDF_ATLAS_NO_ARTERY_FONT=ON",
        ],
        cwd=MSDF_BUILD_DIR,
        check=True,
    )
    subprocess.run(
        ["cmake", "--build", ".", "--config", "Release", "-j", str(os.cpu_count() or 4)],
        cwd=MSDF_BUILD_DIR,
        check=True,
    )
    if not os.path.isfile(binary):
        # Try Release subdir on Windows
        alt = os.path.join(MSDF_BUILD_DIR, "bin", "Release", "msdf-atlas-gen.exe")
        if os.path.isfile(alt):
            return alt
        raise FileNotFoundError(f"Build succeeded but binary not found at {binary}")
    return binary

# Define the character ranges we want to include
CHAR_RANGES = [
    (32, 126),  # Printable ASCII
    # Add more ranges here as needed, e.g.:
    (0x0080, 0x00ff), # latin
    (0x0100, 0x017f), # latin
    (0x0180, 0x024f), # latin
    (0x0250, 0x02af), # ipa extensions
    (0x0370, 0x03ff), # greek and coptic
    (0x0400, 0x04ff), # cyrillic
    (0x0500, 0x052f), # cyrillic supplement
    (0x0530, 0x058f), # armenian
    (0x0590, 0x05ff), # hebrew
    (0x0600, 0x06ff), # arabic
]

def calculate_grid_size(char_ranges):
    """Calculate the smallest square grid that fits the highest character code"""
    max_char = max(end for _, end in char_ranges)
    grid_size = 1
    while grid_size * grid_size < max_char:
        grid_size += 1
    return grid_size

ATLAS_TYPES = [
    "hardmask",  # binary image
    "softmask",  # anti-aliased image
    "sdf",       # signed distance field
    "psdf",      # perpendicular distance field
    "msdf",      # multi-channel signed distance field
    "mtsdf"      # combined MSDF and true SDF
]

def calculate_font_size(resolution, base_size=None):
    """Calculate the font size based on resolution, scaling from 512 resolution"""
    if base_size is None:
        base_size = 32  # Default size at 512 resolution
    return (base_size * resolution) // 512

def generate_atlas(font_path, resolution, draw_grid=False, type="msdf", font_size=None):
    """Generates a font atlas using various distance field techniques.

    This function creates a font atlas using msdf-atlas-gen, then rearranges the
    characters to include gaps for non-printable characters. The output is saved
    as 'atlas.png' in the current directory.

    Args:
        font_path (str): Path to the input font file (.ttf/.otf)
        resolution (int): Width and height of the output atlas in pixels
        draw_grid (bool, optional): If True, draws red grid lines on the output.
            Defaults to False.
        type (str, optional): Atlas type to generate. See ATLAS_TYPES.
        font_size (int, optional): Base font size in pixels at 512 resolution.
            Will be scaled for other resolutions.

    Returns:
        bool: True if atlas generation succeeded, False if an error occurred.

    Raises:
        subprocess.CalledProcessError: If msdf-atlas-gen fails to execute.
    """
    # Get font name from path
    font_name = os.path.splitext(os.path.basename(font_path))[0]

    # Calculate grid size based on character ranges
    grid_size = calculate_grid_size(CHAR_RANGES)
    cell_size = resolution // grid_size

    # Calculate font size if not specified, scaling from 512 resolution
    if font_size is None:
        font_size = calculate_font_size(resolution)
    else:
        font_size = calculate_font_size(resolution, font_size)

    # Convert character ranges to command-line format
    chars_str = ", ".join(f"[{start}, {end}]" for start, end in CHAR_RANGES)

    # Update the output filename to include resolution
    output_filename = f"atlas_{font_name}_{resolution}_{type}"

    cmd = [
        get_msdf_binary(),
        "-font", font_path,
        "-type", type,
        "-format", "png",
        "-imageout", f"{output_filename}.png",
        "-size", str(font_size),
        "-pxrange", "4",
        "-dimensions", str(resolution), str(resolution),
        "-chars", chars_str,
        "-uniformgrid",
        "-uniformcols", str(grid_size),
        "-uniformcell", str(cell_size), str(cell_size),
        "-errorcorrection", "auto-full",
        "-scanline",
        #"-angle", "15d",
        "-edgecoloring", "distance"
    ]

    try:
        print("Running msdf-atlas-gen...")
        print("Command:", end=" ")
        for arg in cmd:
            if arg.startswith('-'):
                print(f"\n    {arg}", end=" ")
            else:
                print(arg, end=" ")
        print()
        result = subprocess.run(cmd, check=True, capture_output=True, text=True)

        # Print the output
        if result.stdout:
            print("msdf-atlas-gen output:")
            print(result.stdout)

        # Rearrange the atlas to include gaps for non-printable characters
        print("Rearranging atlas...")
        rearrange_atlas(resolution, cell_size, cell_size, draw_grid, type, font_name)

        # Generate or update Unity meta file
        generate_unity_meta(output_filename, resolution)
        return True
    except subprocess.CalledProcessError as e:
        print(f"Error generating atlas: {e}")
        print(f"Error output: {e.stderr}")
        return False

def draw_grid_lines(image, resolution):
    """Draw red grid lines on the image"""
    draw = ImageDraw.Draw(image)
    grid_size = calculate_grid_size(CHAR_RANGES)

    # Draw vertical lines
    for x in range(grid_size):
        line_x = x * resolution // grid_size
        draw.line([(line_x, 0), (line_x, resolution-1)], fill=(255, 0, 0), width=1)

    # Draw horizontal lines
    for y in range(grid_size):
        line_y = y * resolution // grid_size
        draw.line([(0, line_y), (resolution-1, line_y)], fill=(255, 0, 0), width=1)

    # Draw the final borders
    draw.line([(resolution-1, 0), (resolution-1, resolution-1)], fill=(255, 0, 0), width=1)
    draw.line([(0, resolution-1), (resolution-1, resolution-1)], fill=(255, 0, 0), width=1)

def rearrange_atlas(resolution, cell_width, cell_height, draw_grid=False, type="msdf", font_name=""):
    """Rearrange the atlas to include gaps for non-printable characters"""
    # Update input and output filenames to match generate_atlas format
    input_filename = f"atlas_{font_name}_{resolution}_{type}.png"
    original = Image.open(input_filename)
    new_atlas = Image.new('RGBA', (resolution, resolution), (0, 0, 0, 255))

    grid_size = calculate_grid_size(CHAR_RANGES)
    cell_size = resolution // grid_size

    # Track current position in the source atlas
    source_index = 0

    # Process each character range
    for start, end in CHAR_RANGES:
        for ascii_code in range(start, end + 1):
            # Calculate source position (original atlas)
            source_x = (source_index % grid_size) * cell_size
            source_y = (source_index // grid_size) * cell_size

            # Calculate target position (new atlas)
            target_x = ((ascii_code + 1) % grid_size) * cell_size
            target_y = ((ascii_code + 1) // grid_size) * cell_size

            # Extract and paste the glyph
            glyph = original.crop((
                source_x,
                source_y,
                source_x + cell_size,
                source_y + cell_size
            ))
            new_atlas.paste(glyph, (target_x, target_y))
            source_index += 1

    # Draw the grid lines only if requested
    if draw_grid:
        draw_grid_lines(new_atlas, resolution)

    # Calculate actual used dimensions
    used_resolution = cell_size * grid_size
    # Crop to used dimensions and resize back to requested resolution
    used_atlas = new_atlas.crop((0, 0, used_resolution, used_resolution))
    final_atlas = used_atlas.resize((resolution, resolution), Image.LANCZOS)

    # Save with the same filename format (no change needed since input/output are the same)
    final_atlas.save(input_filename)
    print("Atlas rearranged successfully!")

def generate_unity_meta(basename, resolution):
    """Generate or update Unity meta file for the atlas texture."""
    meta_path = f"{basename}.png.meta"
    existing_guid = None

    # Try to read existing GUID if meta file exists
    if os.path.exists(meta_path):
        with open(meta_path, 'r') as f:
            for line in f:
                if 'guid: ' in line:
                    existing_guid = line.split('guid: ')[1].strip()
                    break

    # Generate new GUID if none exists
    guid = existing_guid or ''.join('%x' % random.randrange(16) for _ in range(32))

    meta_template = f'''fileFormatVersion: 2
guid: {guid}
TextureImporter:
  internalIDToNameTable: []
  externalObjects: {{}}
  serializedVersion: 12
  mipmaps:
    mipMapMode: 0
    enableMipMap: 1
    sRGBTexture: 1
    linearTexture: 0
    fadeOut: 0
    borderMipMap: 0
    mipMapsPreserveCoverage: 0
    alphaTestReferenceValue: 0.5
    mipMapFadeDistanceStart: 1
    mipMapFadeDistanceEnd: 3
  bumpmap:
    convertToNormalMap: 0
    externalNormalMap: 0
    heightScale: 0.25
    normalMapFilter: 0
    flipGreenChannel: 0
  isReadable: 0
  streamingMipmaps: 0
  streamingMipmapsPriority: 0
  vTOnly: 0
  ignoreMipmapLimit: 0
  grayScaleToAlpha: 0
  generateCubemap: 6
  cubemapConvolution: 0
  seamlessCubemap: 0
  textureFormat: 1
  maxTextureSize: {resolution}
  textureSettings:
    serializedVersion: 2
    filterMode: 1
    aniso: 1
    mipBias: 0
    wrapU: 0
    wrapV: 0
    wrapW: 0
  nPOTScale: 1
  lightmap: 0
  compressionQuality: 50
  spriteMode: 0
  spriteExtrude: 1
  spriteMeshType: 1
  alignment: 0
  spritePivot: {{x: 0.5, y: 0.5}}
  spritePixelsToUnits: 100
  spriteBorder: {{x: 0, y: 0, z: 0, w: 0}}
  spriteGenerateFallbackPhysicsShape: 1
  alphaUsage: 1
  alphaIsTransparency: 0
  spriteTessellationDetail: -1
  textureType: 0
  textureShape: 1
  singleChannelComponent: 0
  flipbookRows: 1
  flipbookColumns: 1
  maxTextureSizeSet: 0
  compressionQualitySet: 0
  textureFormatSet: 0
  ignorePngGamma: 0
  applyGammaDecoding: 0
  swizzle: 50462976
  cookieLightType: 0
  platformSettings:
  - serializedVersion: 3
    buildTarget: DefaultTexturePlatform
    maxTextureSize: {resolution}
    resizeAlgorithm: 0
    textureFormat: -1
    textureCompression: 2
    compressionQuality: 50
    crunchedCompression: 0
    allowsAlphaSplitting: 0
    overridden: 0
    ignorePlatformSupport: 0
    androidETC2FallbackOverride: 0
    forceMaximumCompressionQuality_BC6H_BC7: 0
  - serializedVersion: 3
    buildTarget: Standalone
    maxTextureSize: {resolution}
    resizeAlgorithm: 0
    textureFormat: 3
    textureCompression: 1
    compressionQuality: 50
    crunchedCompression: 0
    allowsAlphaSplitting: 0
    overridden: 1
    ignorePlatformSupport: 0
    androidETC2FallbackOverride: 0
    forceMaximumCompressionQuality_BC6H_BC7: 0
  - serializedVersion: 3
    buildTarget: Android
    maxTextureSize: {resolution}
    resizeAlgorithm: 0
    textureFormat: -1
    textureCompression: 1
    compressionQuality: 50
    crunchedCompression: 0
    allowsAlphaSplitting: 0
    overridden: 0
    ignorePlatformSupport: 0
    androidETC2FallbackOverride: 0
    forceMaximumCompressionQuality_BC6H_BC7: 0
  spriteSheet:
    serializedVersion: 2
    sprites: []
    outline: []
    physicsShape: []
    bones: []
    spriteID:
    internalID: 0
    vertices: []
    indices:
    edges: []
    weights: []
    secondaryTextures: []
    nameFileIdTable: {{}}
  mipmapLimitGroupName:
  pSDRemoveMatte: 0
  userData:
  assetBundleName:
  assetBundleVariant:
'''

    with open(meta_path, 'w') as f:
        f.write(meta_template)

def main():
    parser = argparse.ArgumentParser(description='Generate a font atlas using msdf-atlas-gen')
    parser.add_argument('font_path', help='Path to the font file (.ttf/.otf)')
    parser.add_argument('resolution', type=int, help='Total atlas resolution (width=height)')
    parser.add_argument('--grid', type=bool, default=False, help='Draw grid lines on the output atlas')
    parser.add_argument('--type', type=str, default="msdf", choices=ATLAS_TYPES,
                       help='Type of atlas to generate')
    parser.add_argument('--font-size', type=int, help='Base font size in pixels at 512 resolution. Will be scaled for other resolutions.')
    args = parser.parse_args()

    # Verify font file exists
    if not os.path.isfile(args.font_path):
        print(f"Error: Font file not found at {args.font_path}")
        return

    generate_atlas(args.font_path, args.resolution, draw_grid=args.grid, type=args.type, font_size=args.font_size)

if __name__ == "__main__":
    main()