core/info.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Avisynth+ | ||
| 2 | // https://avs-plus.net | ||
| 3 | // | ||
| 4 | // This program is free software; you can redistribute it and/or modify | ||
| 5 | // it under the terms of the GNU General Public License as published by | ||
| 6 | // the Free Software Foundation; either version 2 of the License, or | ||
| 7 | // (at your option) any later version. | ||
| 8 | // | ||
| 9 | // This program is distributed in the hope that it will be useful, | ||
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | // GNU General Public License for more details. | ||
| 13 | // | ||
| 14 | // You should have received a copy of the GNU General Public License | ||
| 15 | // along with this program; if not, write to the Free Software | ||
| 16 | // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA, or visit | ||
| 17 | // http://www.gnu.org/copyleft/gpl.html . | ||
| 18 | // | ||
| 19 | // Linking Avisynth statically or dynamically with other modules is making a | ||
| 20 | // combined work based on Avisynth. Thus, the terms and conditions of the GNU | ||
| 21 | // General Public License cover the whole combination. | ||
| 22 | // | ||
| 23 | // As a special exception, the copyright holders of Avisynth give you | ||
| 24 | // permission to link Avisynth with independent modules that communicate with | ||
| 25 | // Avisynth solely through the interfaces defined in avisynth.h, regardless of the license | ||
| 26 | // terms of these independent modules, and to copy and distribute the | ||
| 27 | // resulting combined work under terms of your choice, provided that | ||
| 28 | // every copy of the combined work is accompanied by a complete copy of | ||
| 29 | // the source code of Avisynth (the version of Avisynth used to produce the | ||
| 30 | // combined work), being distributed under the terms of the GNU General | ||
| 31 | // Public License plus this exception. An independent module is a module | ||
| 32 | // which is not derived from or based on Avisynth, such as 3rd-party filters, | ||
| 33 | // import and export plugins, or graphical user interfaces. | ||
| 34 | |||
| 35 | #ifndef __INFO_H__ | ||
| 36 | #define __INFO_H__ | ||
| 37 | |||
| 38 | #ifdef AVS_LINUX | ||
| 39 | #include <uchar.h> | ||
| 40 | #endif | ||
| 41 | #include <sstream> | ||
| 42 | #include "internal.h" | ||
| 43 | #include <unordered_map> | ||
| 44 | #include <array> | ||
| 45 | #include <iomanip> | ||
| 46 | #include <vector> | ||
| 47 | #include <cstring> | ||
| 48 | #include "strings.h" | ||
| 49 | |||
| 50 | enum ChromaLocationMode { | ||
| 51 | CENTER_411, | ||
| 52 | CENTER_420, | ||
| 53 | LEFT_420, | ||
| 54 | CENTER_422, | ||
| 55 | LEFT_422 | ||
| 56 | }; | ||
| 57 | |||
| 58 | typedef struct BBX { | ||
| 59 | uint8_t width; // e.g. 8 | ||
| 60 | uint8_t height; // 16 | ||
| 61 | int8_t offset_x; // 0 | ||
| 62 | int8_t offset_y; // -4 | ||
| 63 | } BBX; | ||
| 64 | |||
| 65 | class PreRendered { | ||
| 66 | const bool useHalocolor; | ||
| 67 | const int vi_width; | ||
| 68 | const int vi_height; | ||
| 69 | |||
| 70 | public: | ||
| 71 | int x, y; | ||
| 72 | int len; | ||
| 73 | int xstart; | ||
| 74 | int text_width; // draw this amount of pixels starting from horizontal index xstart | ||
| 75 | int ystart; // vertical visibility: starting row in stringbitmap | ||
| 76 | int yend; // vertical visibility: ending row in stringbitmap | ||
| 77 | int stringbitmap_height; // font height plus optinally added top/bottom line | ||
| 78 | const int safety_bits_x_left; // extra leftside playground for chroma rendering | ||
| 79 | const int safety_bits_x_right; // extra rightside playground for chroma rendering | ||
| 80 | |||
| 81 | std::vector<std::vector<uint8_t>> stringbitmap; | ||
| 82 | std::vector<std::vector<uint8_t>> stringbitmap_outline; | ||
| 83 | |||
| 84 | PreRendered( | ||
| 85 | const uint8_t* fonts, | ||
| 86 | const int fontline_bytes, | ||
| 87 | const int _vi_width, const int _vi_height, | ||
| 88 | int _x, int _y, // they may change | ||
| 89 | std::vector<int>& s, // it may get shortened | ||
| 90 | const std::vector<BBX>& bbx_array, | ||
| 91 | const int align, | ||
| 92 | const bool _useHalocolor, | ||
| 93 | const int FONT_WIDTH_notused, const int FONT_HEIGHT, | ||
| 94 | const int _safety_bits_x_left, | ||
| 95 | const int _safety_bits_x_right | ||
| 96 | ); | ||
| 97 | |||
| 98 | void make_outline(); | ||
| 99 | }; | ||
| 100 | |||
| 101 | class BitmapFont { | ||
| 102 | |||
| 103 | int number_of_chars; | ||
| 104 | std::string font_name; | ||
| 105 | std::string font_filename; | ||
| 106 | |||
| 107 | public: | ||
| 108 | const BBX global_bbx; | ||
| 109 | |||
| 110 | const bool bold; | ||
| 111 | |||
| 112 | std::vector<uint8_t> font_bitmaps; | ||
| 113 | std::vector<BBX> bbx_array; // FontBoundingBox BBX array, can be individual for each character | ||
| 114 | const int fontline_bytes; | ||
| 115 | |||
| 116 | std::unordered_map<std::string, int> charReMapUtf8; // utf8 vs. font image index | ||
| 117 | |||
| 118 | void SaveAsC(const uint16_t* _codepoints); | ||
| 119 | |||
| 120 | 7 | BitmapFont(int _number_of_chars, | |
| 121 | const uint16_t* _src_font_bitmaps_internaluint16, | ||
| 122 | const uint8_t* _src_font_bitmaps, | ||
| 123 | const int _fontline_bytes, | ||
| 124 | const uint16_t* _codepoints, | ||
| 125 | const std::vector<BBX>* bbx_array_ptr, | ||
| 126 | const BBX& _global_bbx, | ||
| 127 | 7 | std::string _font_name, std::string _font_filename, bool _bold, bool debugSave) : | |
| 128 | 7 | number_of_chars(_number_of_chars), | |
| 129 | 7 | font_name(_font_name), | |
| 130 |
1/2✓ Branch 3 → 4 taken 7 times.
✗ Branch 3 → 48 not taken.
|
7 | font_filename(_font_filename), |
| 131 | 7 | global_bbx(_global_bbx), | |
| 132 | 7 | bold(_bold), | |
| 133 | 7 | fontline_bytes(_fontline_bytes) | |
| 134 | //font_bitmaps(_font_bitmaps), | ||
| 135 | { | ||
| 136 | //fixme: do not copy data | ||
| 137 | 7 | const int charline_count = global_bbx.height * number_of_chars; | |
| 138 |
1/2✓ Branch 7 → 8 taken 7 times.
✗ Branch 7 → 40 not taken.
|
7 | font_bitmaps.resize(charline_count * fontline_bytes); |
| 139 |
1/2✗ Branch 8 → 9 not taken.
✓ Branch 8 → 12 taken 7 times.
|
7 | if (_src_font_bitmaps != nullptr) |
| 140 | ✗ | std::memcpy(font_bitmaps.data(), _src_font_bitmaps, font_bitmaps.size()); | |
| 141 | else { | ||
| 142 | // this must be an internal, predefined array | ||
| 143 | // copy uint16_t array to byte array MSB-LSB order | ||
| 144 | // fontline_bytes is 2 | ||
| 145 | 7 | const uint16_t* src = _src_font_bitmaps_internaluint16; | |
| 146 | 7 | uint8_t* dst = font_bitmaps.data(); | |
| 147 |
2/2✓ Branch 15 → 14 taken 146232 times.
✓ Branch 15 → 16 taken 7 times.
|
146239 | for (auto i = 0; i < charline_count; i++) { |
| 148 | 146232 | uint16_t one_fontline = src[i]; | |
| 149 | 146232 | dst[i * 2] = (uint8_t)(one_fontline >> 8); | |
| 150 | 146232 | dst[i * 2 + 1] = (uint8_t)(one_fontline & 0xFF); | |
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | // We always display utf8 strings, so the reverse lookup must be utf8 character based, | ||
| 155 | // which returns the index to the font bitmap and bbx array. | ||
| 156 |
2/2✓ Branch 21 → 17 taken 9478 times.
✓ Branch 21 → 22 taken 7 times.
|
9485 | for (int i = 0; i < _number_of_chars; i++) { |
| 157 |
1/2✓ Branch 17 → 18 taken 9478 times.
✗ Branch 17 → 37 not taken.
|
9478 | std::string s_utf8 = U16_to_utf8(_codepoints[i]); |
| 158 |
1/2✓ Branch 18 → 19 taken 9478 times.
✗ Branch 18 → 35 not taken.
|
9478 | charReMapUtf8[s_utf8] = i; |
| 159 | 9478 | } | |
| 160 | |||
| 161 | |||
| 162 | // Many East Asian characters—especially CJK (Chinese, Japanese, Korean) ideographs | ||
| 163 | // are typically double-width compared to Latin characters when used in monospaced bitmap | ||
| 164 | // fonts like BDF. | ||
| 165 | // For Avisynth-embedded internal fonts (e.g., Terminus, Info_H), only a fixed global BBX is used, | ||
| 166 | // as these fonts do not include CJK characters, only Latin, Cyrillic, and Greek. | ||
| 167 | // The variable BBX array is optional and is intended for use with external fonts, | ||
| 168 | // such as unifont-16.0.04.bdf, where CJK characters have different BBX values | ||
| 169 | // compared to Latin characters. | ||
| 170 | |||
| 171 |
1/2✗ Branch 22 → 23 not taken.
✓ Branch 22 → 31 taken 7 times.
|
7 | if (bbx_array_ptr != nullptr) { |
| 172 | // copy BBX array if provided | ||
| 173 | ✗ | bbx_array.insert(bbx_array.end(), bbx_array_ptr->begin(), bbx_array_ptr->end()); | |
| 174 | } | ||
| 175 | else { | ||
| 176 | // When no BBX array was provided (bbx_array_ptr is nullptr) | ||
| 177 | // fill bbx_array with constant default global values | ||
| 178 |
1/2✓ Branch 31 → 32 taken 7 times.
✗ Branch 31 → 40 not taken.
|
7 | bbx_array.assign(_number_of_chars, global_bbx); |
| 179 | } | ||
| 180 | |||
| 181 |
1/2✗ Branch 32 → 33 not taken.
✓ Branch 32 → 34 taken 7 times.
|
7 | if (debugSave) |
| 182 | ✗ | SaveAsC(_codepoints); | |
| 183 | 7 | } | |
| 184 | |||
| 185 | // helper function for remapping an utf8 string to font index entry list | ||
| 186 | std::vector<int> remap(const std::string& s_utf8); | ||
| 187 | }; | ||
| 188 | |||
| 189 | std::unique_ptr<BitmapFont> GetBitmapFont(int size, const char* name, bool bold, bool debugSave); | ||
| 190 | |||
| 191 | void SimpleTextOutW(BitmapFont* current_font, const VideoInfo& vi, PVideoFrame& frame, int real_x, int real_y, std::string& text_utf8, | ||
| 192 | bool fadeBackground, int textcolor, int halocolor, bool useHaloColor, int align, int chromaplacement); | ||
| 193 | void SimpleTextOutW_multi(BitmapFont* current_font, const VideoInfo& vi, PVideoFrame& frame, int real_x, int real_y, std::string& text_utf8, | ||
| 194 | bool fadeBackground, int textcolor, int halocolor, bool useHaloColor, int align, int lsp, int chromaplacement); | ||
| 195 | |||
| 196 | // legacy function w/o outline, originally with ASCII input, background fading | ||
| 197 | void DrawStringPlanar(VideoInfo& vi, PVideoFrame& dst, int x, int y, const char* s); | ||
| 198 | void DrawStringYUY2(VideoInfo& vi, PVideoFrame& dst, int x, int y, const char* s); | ||
| 199 | void DrawStringRGB32(VideoInfo& vi, PVideoFrame& dst, int x, int y, const char* s); | ||
| 200 | void DrawStringRGB24(VideoInfo& vi, PVideoFrame& dst, int x, int y, const char* s); | ||
| 201 | |||
| 202 | #endif // __INFO_H__ | ||
| 203 |