| 1 | /* Copyright (C) 2025 FontForge Contributors |
| 2 | * |
| 3 | * Redistribution and use in source and binary forms, with or without |
| 4 | * modification, are permitted provided that the following conditions are met: |
| 5 | * |
| 6 | * Redistributions of source code must retain the above copyright notice, this |
| 7 | * list of conditions and the following disclaimer. |
| 8 | * |
| 9 | * Redistributions in binary form must reproduce the above copyright notice, |
| 10 | * this list of conditions and the following disclaimer in the documentation |
| 11 | * and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 14 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 15 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 16 | */ |
| 17 | |
| 18 | #ifndef FONTFORGE_FFPROCESS_H |
| 19 | #define FONTFORGE_FFPROCESS_H |
| 20 | |
| 21 | #include <stddef.h> /* for size_t */ |
| 22 | |
| 23 | #ifdef __cplusplus |
| 24 | extern "C" { |
| 25 | #endif |
| 26 | |
| 27 | /** |
| 28 | * Process spawning and file decompression utilities. |
| 29 | * |
| 30 | * Backend selection: |
| 31 | * - When running as a Python module: Uses Python's subprocess/gzip/etc. |
| 32 | * - When running as FontForge app: Tries POSIX first, falls back to Python |
| 33 | * - When Python unavailable: POSIX only |
| 34 | */ |
| 35 | |
| 36 | typedef enum { |
| 37 | FF_PROCESS_OK = 0, |
| 38 | FF_PROCESS_NOT_FOUND, /* Command not found in PATH */ |
| 39 | FF_PROCESS_FAILED, /* Execution failed */ |
| 40 | FF_PROCESS_NO_BACKEND /* No POSIX or Python available */ |
| 41 | } FFProcessResult; |
| 42 | |
| 43 | /** |
| 44 | * Run a command synchronously and capture output. |
| 45 | * |
| 46 | * @param argv NULL-terminated argument array (argv[0] is the command) |
| 47 | * @param workdir Working directory for child process (NULL = inherit) |
| 48 | * @param stdout_buf If non-NULL, receives malloc'd stdout (caller frees) |
| 49 | * @param stderr_buf If non-NULL, receives malloc'd stderr (caller frees) |
| 50 | * @return Result code |
| 51 | */ |
| 52 | FFProcessResult ff_run_command(char** argv, const char* workdir, |
| 53 | char** stdout_buf, char** stderr_buf); |
| 54 | |
| 55 | /** |
| 56 | * Run a command and capture binary stdout (may contain NUL bytes). |
| 57 | * |
| 58 | * @param argv NULL-terminated argument array |
| 59 | * @param workdir Working directory (NULL = inherit) |
| 60 | * @param out_data Receives malloc'd output data (caller frees) |
| 61 | * @param out_len Receives length of output data |
| 62 | * @return Result code |
| 63 | */ |
| 64 | FFProcessResult ff_run_command_binary(char** argv, const char* workdir, |
| 65 | unsigned char** out_data, |
| 66 | size_t* out_len); |
| 67 | |
| 68 | /** |
| 69 | * Decompress a file to a temporary file. |
| 70 | * |
| 71 | * Supports: .gz, .bz2, .lzma, .xz |
| 72 | * |
| 73 | * @param filename Path to compressed file |
| 74 | * @param out_tmpfile Receives malloc'd path to temp file (caller frees and |
| 75 | * unlinks) |
| 76 | * @return Result code |
| 77 | */ |
| 78 | FFProcessResult ff_decompress_to_temp(const char* filename, char** out_tmpfile); |
| 79 | |
| 80 | /** |
| 81 | * Extract a file from an archive to a temporary directory. |
| 82 | * |
| 83 | * Supports: .zip, .tar, .tar.gz, .tar.bz2, .tgz, .tbz2 |
| 84 | * |
| 85 | * @param archive Path to archive file |
| 86 | * @param member Specific member to extract (NULL = let user choose or |
| 87 | * extract all) |
| 88 | * @param out_tmpdir Receives malloc'd path to temp directory (caller |
| 89 | * frees/cleans) |
| 90 | * @param out_file Receives malloc'd path to extracted file (caller frees) |
| 91 | * @return Result code |
| 92 | */ |
| 93 | FFProcessResult ff_extract_from_archive(const char* archive, const char* member, |
| 94 | char** out_tmpdir, char** out_file); |
| 95 | |
| 96 | /** |
| 97 | * Check if we're running as a Python module (Python started us). |
| 98 | * Used internally to decide whether to prefer Python over POSIX. |
| 99 | */ |
| 100 | int ff_running_as_python_module(void); |
| 101 | |
| 102 | /* ==================== Compression API ==================== */ |
| 103 | |
| 104 | /** |
| 105 | * Compression types. |
| 106 | * |
| 107 | * Note: These values do NOT match the legacy SplineFont.compression field. |
| 108 | * Legacy mapping: 1=gz, 2=bz2, 3=bz(bz2), 4=Z, 5=lzma |
| 109 | * Use ff_compression_type() to detect from filename extension. |
| 110 | */ |
| 111 | typedef enum { |
| 112 | FF_COMPRESS_NONE = 0, |
| 113 | FF_COMPRESS_GZ, /* .gz - gzip */ |
| 114 | FF_COMPRESS_BZ2, /* .bz2, .bz - bzip2 */ |
| 115 | FF_COMPRESS_LZMA, /* .lzma - lzma */ |
| 116 | FF_COMPRESS_XZ, /* .xz - xz */ |
| 117 | FF_COMPRESS_Z, /* .Z - compress */ |
| 118 | FF_COMPRESS_COUNT |
| 119 | } FFCompressionType; |
| 120 | |
| 121 | /** |
| 122 | * Detect compression type from filename extension. |
| 123 | * |
| 124 | * @param filename Path to check |
| 125 | * @return Compression type, or FF_COMPRESS_NONE if not recognized |
| 126 | */ |
| 127 | FFCompressionType ff_compression_type(const char* filename); |
| 128 | |
| 129 | /** |
| 130 | * Convert legacy SplineFont.compression value to FFCompressionType. |
| 131 | * |
| 132 | * Legacy values: 0=none, 1=gz, 2=bz2, 3=bz(bz2), 4=Z, 5=lzma |
| 133 | * |
| 134 | * @param sf_compression SplineFont.compression field value |
| 135 | * @return Corresponding FFCompressionType |
| 136 | */ |
| 137 | FFCompressionType ff_compression_from_legacy(int sf_compression); |
| 138 | |
| 139 | /** |
| 140 | * Convert FFCompressionType to legacy SplineFont.compression value. |
| 141 | * |
| 142 | * @param type FFCompressionType value |
| 143 | * @return Legacy value suitable for SplineFont.compression field |
| 144 | */ |
| 145 | int ff_compression_to_legacy(FFCompressionType type); |
| 146 | |
| 147 | /** |
| 148 | * Get the file extension for a compression type. |
| 149 | * |
| 150 | * @param type Compression type |
| 151 | * @return Extension string including dot (e.g., ".gz"), or "" for NONE |
| 152 | */ |
| 153 | const char* ff_compression_ext(FFCompressionType type); |
| 154 | |
| 155 | /** |
| 156 | * Compress a file in place. |
| 157 | * The original file is replaced with the compressed version. |
| 158 | * |
| 159 | * @param filename Path to file (will have extension appended) |
| 160 | * @param type Compression type to use |
| 161 | * @return Result code |
| 162 | */ |
| 163 | FFProcessResult ff_compress_file(const char* filename, FFCompressionType type); |
| 164 | |
| 165 | /** |
| 166 | * Decompress a file in place. |
| 167 | * The compressed file is replaced with the decompressed version. |
| 168 | * |
| 169 | * @param filename Path to compressed file (extension will be removed) |
| 170 | * @return Result code |
| 171 | */ |
| 172 | FFProcessResult ff_decompress_in_place(const char* filename); |
| 173 | |
| 174 | /* ==================== MIME Type API ==================== */ |
| 175 | |
| 176 | /** |
| 177 | * Guess MIME type from filename extension. |
| 178 | * |
| 179 | * Uses Python's mimetypes module if available, otherwise falls back |
| 180 | * to a built-in extension table covering common image/font types. |
| 181 | * |
| 182 | * @param path Path or filename to check |
| 183 | * @return malloc'd MIME type string (caller frees), or NULL if unknown |
| 184 | */ |
| 185 | char* ff_guess_mime_type(const char* path); |
| 186 | |
| 187 | #ifdef __cplusplus |
| 188 | } |
| 189 | #endif |
| 190 | |
| 191 | #endif /* FONTFORGE_FFPROCESS_H */ |