Apple's open-source XNU kernel on Android phones: its own loader and drivers, Metal on the Adreno. Runs from RAM; a reboot gives the phone back to Android.
C
Colour emoji and Chinese text: the two big fonts, made smaller, in the system image
vcvkk27cea567feb4
diff --git a/README.md b/README.mdindex a44b9fc..8bbde78 100644--- a/README.md+++ b/README.md@@ -169,8 +169,12 @@ codec DMA TX_CDC_DMA_TX_3 и звуковой процессор в поток,'b3a8'). Таких наших поверхностей нет (ядро даёт вместо них BGRA, оконный сервер только её и показывает), иCoreGraphics в них не рисовал — плитки оставались нулями. Теперь загрузчик пишет в дерево sRGB, как и есть унашего кадрового буфера, и WebKit рисует в BGRA (`xnuloader/loader.c`, `dt_identity()`; проверка —- `RC_JOBS=rc.jobs.safari`). Знаки из шрифтов, которых нет в образе системы (цветные эмодзи, китайский:- `ios165/select-system.py` оставляет их снаружи), выходят квадратиками с вопросом.+ `RC_JOBS=rc.jobs.safari`). Цветные эмодзи и китайский шрифт PingFang (130 и 74 МБ) целиком в образ системы не+ помещаются, и их знаки выходили квадратиками с вопросом; теперь `ios165/fetch.sh` кладёт их в образ уменьшенными+ (`tools/font_trim.py`): эмодзи — без самых крупных картинок (остаются 40, 64 и 96 точек; 59 МБ), PingFang — с+ обычным и полужирным начертаниями упрощённого, традиционного и гонконгского письма (26 МБ). Проверка —+ `RC_JOBS=rc.jobs.fonts` (`font_test`: к какому шрифту система идёт за эмодзи, китайским, японским и корейским и+ сколько точек он рисует, сколько из них цветных; с `wifi=1` — китайская Википедия и страница эмодзи в Safari).Настройки: если собрать малый образ с `WIFID=1` (`WIFID=1 GL=1 REST=1 userland/build.sh`), в системе работает иwifid, служба Wi-Fi самой iOS, а ядро отвечает на её запросы (`bsd/dev/redwood_apple80211.c`). Тогда в строкесостояния есть значок Wi-Fi, в Настройках Wi-Fi включён, подключённая сеть названа, под ней список сетей,diff --git a/ios165/fetch.sh b/ios165/fetch.shindex 0187451..73a2717 100755--- a/ios165/fetch.sh+++ b/ios165/fetch.sh@@ -51,6 +51,11 @@ if [ ! -f .done-rootfs ] || [ select-system.py -nt .done-rootfs ]; thenecho "fetch.sh: extracting $(wc -l < system.files) files"rm -rf rootfs7z x -y -orootfs rootfs.dmg @system.files > extract-system.log+ # the two fonts too big for the image as they are, made smaller in place (see select-system.py)+ python3 ../tools/font_trim.py emoji rootfs/System/Library/Fonts/CoreAddition/AppleColorEmoji-160px.ttc \+ rootfs/System/Library/Fonts/CoreAddition/AppleColorEmoji-160px.ttc 40,64,96+ python3 ../tools/font_trim.py faces rootfs/System/Library/Fonts/LanguageSupport/PingFang.ttc \+ rootfs/System/Library/Fonts/LanguageSupport/PingFang.ttc '^\.?PingFang(SC|TC|HK)-(Regular|Semibold)$'touch .done-rootfs[ -n "$KEEP" ] || rm -f rootfs.dmgfidiff --git a/ios165/select-system.py b/ios165/select-system.pyindex 74e1632..48a8ba9 100755--- a/ios165/select-system.py+++ b/ios165/select-system.py@@ -25,7 +25,7 @@ The whole root is 7.9 GB and the kernel on the phone has about 4 GB of RAM for eare left out, and so are a few very big daemons that have nothing to do with the display;- /Library (the wallpapers, regional tables) without its sounds;- of pictures, what the user interface itself is drawn with: small images, asset catalogs of- moderate size; the fonts except the two biggest (colour emoji and Chinese);+ moderate size; the fonts, the two biggest (colour emoji and Chinese) made smaller by fetch.sh;- the empty LANGUAGE.lproj directories of the bundles whose strings are in one .loctable filefor all languages: they are how CoreFoundation knows which languages a bundle has, andwithout them every string comes out as its key.@@ -63,8 +63,14 @@ SKIP_APP_EXT = ('.mov', '.mp4', '.m4a', '.caf', '.wav', '.heic', '.aiff', '.meta# pictures the user interface is drawn with: small images, and asset catalogs up to this size# (the one with all the SF Symbols alone is 170 MB and is left out)MAX_ASSET_CATALOG = 4 << 20-# fonts: all but the very big ones (the colour emoji, 130 MB, and PingFang, 74 MB)+# fonts: all but the very big ones; those two -- the colour emoji, 130 MB, and PingFang, the Chinese faces, 74 MB --+# are taken and made smaller after extraction (fetch.sh, ../tools/font_trim.py): the emoji without their biggest+# bitmaps (59 MB), PingFang with the regular and semibold faces for simplified, traditional and Hong Kong Chinese,+# both those programs name and the hidden ones (".PingFangSC-Regular") the system font falls back to+# (26 MB). Without them Safari and the rest drew emoji and Chinese characters as boxes with a question mark.MAX_FONT = 25 << 20+TRIMMED_FONTS = ('System/Library/Fonts/CoreAddition/AppleColorEmoji-160px.ttc',+ 'System/Library/Fonts/LanguageSupport/PingFang.ttc')# string tables hold every language; the biggest (Shortcuts' actions, Home) are 5-12 MBMAX_STRING_TABLE = 16 << 20# applications waiting here to be installed by the first boot go to /Applications instead@@ -135,7 +141,7 @@ def wanted_file(path, size):if path.startswith(SKIP_PREFIX):return Falseif path.startswith('System/Library/Fonts/'):- return size <= MAX_FONT+ return size <= MAX_FONT or path in TRIMMED_FONTSif path.startswith(ALL_PICTURES) and path.lower().endswith('.png'):return Trueif path.startswith(('usr/libexec/', 'usr/sbin/')):diff --git a/tools/font_trim.py b/tools/font_trim.pynew file mode 100755index 0000000..62dd171--- /dev/null+++ b/tools/font_trim.py@@ -0,0 +1,164 @@+#!/usr/bin/env python3+"""+font_trim.py: smaller copies of the two fonts iOS has that are too big for a system image kept in+the phone's memory, made from the files themselves (nothing of them is in the repository).++ font_trim.py emoji SRC DST [PPEM,...]+ Apple Color Emoji (AppleColorEmoji-160px.ttc, 130 MB) with only the bitmap strikes asked for+ (default 40,64,96: what the screen's text sizes use at three pixels a point; the 160-pixel+ strike alone is 71 MB). The 'sbix' table is rebuilt with those strikes, every other table is+ kept as it is; CoreText scales the nearest strike for sizes in between.++ font_trim.py faces SRC DST REGEX+ A font collection (PingFang.ttc: 18 faces, 78 MB) with only the faces whose PostScript name+ matches REGEX, and only the tables they use.++Both write a TrueType collection that CoreText reads as it reads the original; DST may be SRC.+ios165/fetch.sh makes both for the system image this way.+"""+import os+import re+import struct+import sys+++def checksum(data):+ data += b'\0' * (-len(data) % 4)+ return sum(struct.unpack('>%dI' % (len(data) // 4), data)) & 0xffffffff+++def read_collection(d):+ """[(sfnt version, [(tag, offset, length), ...]), ...] for each face of a TTC"""+ if d[:4] != b'ttcf':+ raise SystemExit('font_trim.py: not a TrueType collection')+ count = struct.unpack('>I', d[8:12])[0]+ faces = []+ for at in struct.unpack('>%dI' % count, d[12:12 + 4 * count]):+ version, tables = struct.unpack('>IH', d[at:at + 6])+ records = []+ for i in range(tables):+ tag, _, offset, length = struct.unpack('>4sIII', d[at + 12 + 16 * i:at + 28 + 16 * i])+ records.append((tag, offset, length))+ faces.append((version, records))+ return faces+++def write_collection(faces, tables):+ """faces: [(version, [tag, ...])]; tables: {(face, tag): bytes}, identical bytes stored once"""+ header = 12 + 4 * len(faces)+ directories = [12 + 16 * len(tags) for _, tags in faces]+ at = header + sum(directories)+ at += -at % 4+ stored, blobs, place = {}, [], {}+ for f, (_, tags) in enumerate(faces):+ for tag in tags:+ blob = tables[(f, tag)]+ key = (len(blob), checksum(blob), blob[:64])+ if key not in stored or tables[stored[key]] != blob:+ stored[key] = (f, tag)+ place[(f, tag)] = at+ blobs.append(blob + b'\0' * (-len(blob) % 4))+ at += len(blobs[-1])+ else:+ place[(f, tag)] = place[stored[key]]+ out = bytearray(b'ttcf' + struct.pack('>HHI', 1, 0, len(faces)))+ face_at = header+ for size in directories:+ out += struct.pack('>I', face_at)+ face_at += size+ for f, (version, tags) in enumerate(faces):+ n = len(tags)+ power = 1 << (n.bit_length() - 1)+ out += struct.pack('>IHHHH', version, n, power * 16, power.bit_length() - 1, n * 16 - power * 16)+ for tag in sorted(tags):+ blob = tables[(f, tag)]+ out += struct.pack('>4sIII', tag, checksum(blob), place[(f, tag)], len(blob))+ out += b'\0' * (-len(out) % 4)+ for blob in blobs:+ out += blob+ return bytes(out)+++def save(dst, data):+ """dst replaced whole: written beside it and renamed, so that a dst that is the source, or a hard link shared+ with another copy of the tree, is never written into"""+ with open(dst + '.part', 'wb') as f:+ f.write(data)+ os.replace(dst + '.part', dst)+++def emoji(src, dst, keep):+ d = open(src, 'rb').read()+ faces = read_collection(d)+ tables = {}+ for f, (_, records) in enumerate(faces):+ for tag, offset, length in records:+ tables[(f, tag)] = d[offset:offset + length]+ maxp = tables[(0, b'maxp')]+ glyphs = struct.unpack('>H', maxp[4:6])[0]+ sbix = tables[(0, b'sbix')]+ version, flags, strikes = struct.unpack('>HHI', sbix[:8])+ offsets = struct.unpack('>%dI' % strikes, sbix[8:8 + 4 * strikes])+ kept = []+ for o in offsets:+ ppem = struct.unpack('>H', sbix[o:o + 2])[0]+ ends = struct.unpack('>%dI' % (glyphs + 1), sbix[o + 4:o + 8 + 4 * glyphs])+ if ppem in keep:+ kept.append(sbix[o:o + ends[-1]])+ print('font_trim.py: strike %3d px: %5.1f MB, %s' % (ppem, ends[-1] / 1048576, 'kept' if ppem in keep else 'left out'))+ if not kept:+ raise SystemExit('font_trim.py: none of the strikes asked for is there')+ head = 8 + 4 * len(kept)+ new = bytearray(struct.pack('>HHI', version, flags, len(kept)))+ at = head+ for strike in kept:+ new += struct.pack('>I', at)+ at += len(strike) + (-len(strike) % 4)+ for strike in kept:+ new += strike + b'\0' * (-len(strike) % 4)+ for f in range(len(faces)):+ tables[(f, b'sbix')] = bytes(new)+ out = write_collection([(v, [t for t, _, _ in r]) for v, r in faces], tables)+ save(dst, out)+ print('font_trim.py: %s: %.1f MB (was %.1f MB)' % (dst, len(out) / 1048576, len(d) / 1048576))+++def postscript_name(name_table):+ fmt, count, strings = struct.unpack('>HHH', name_table[:6])+ for i in range(count):+ platform, encoding, language, name_id, length, offset = struct.unpack('>6H', name_table[6 + 12 * i:18 + 12 * i])+ if name_id == 6:+ raw = name_table[strings + offset:strings + offset + length]+ return raw.decode('utf-16-be') if platform in (0, 3) else raw.decode('mac_roman')+ return ''+++def faces_of(src, dst, pattern):+ d = open(src, 'rb').read()+ faces = read_collection(d)+ chosen, tables = [], {}+ for version, records in faces:+ table = {tag: d[offset:offset + length] for tag, offset, length in records}+ name = postscript_name(table[b'name'])+ wanted = re.search(pattern, name) is not None+ print('font_trim.py: %-28s %s' % (name, 'kept' if wanted else 'left out'))+ if wanted:+ f = len(chosen)+ chosen.append((version, list(table)))+ for tag, blob in table.items():+ tables[(f, tag)] = blob+ if not chosen:+ raise SystemExit('font_trim.py: no face matches %s' % pattern)+ out = write_collection(chosen, tables)+ save(dst, out)+ print('font_trim.py: %s: %.1f MB (was %.1f MB)' % (dst, len(out) / 1048576, len(d) / 1048576))+++if __name__ == '__main__':+ if len(sys.argv) >= 4 and sys.argv[1] == 'emoji':+ emoji(sys.argv[2], sys.argv[3], set(int(x) for x in (sys.argv[4] if len(sys.argv) > 4 else '40,64,96').split(',')))+ elif len(sys.argv) == 5 and sys.argv[1] == 'faces':+ faces_of(sys.argv[2], sys.argv[3], sys.argv[4])+ else:+ print(__doc__)+ sys.exit(2)diff --git a/userland/build.sh b/userland/build.shindex d76cd95..835437b 100755--- a/userland/build.sh+++ b/userland/build.sh@@ -118,6 +118,7 @@ if [ -d $IOS/rootfs/usr/lib ]; then$DYN -framework CoreFoundation -framework CoreGraphics -framework IOSurface -o obj/cg_test src/cg_test.c$DYN -o obj/shm_test src/shm_test.c$DYN -framework CoreFoundation -framework CoreGraphics -framework ImageIO -o obj/snap_test src/snap_test.c+ $DYN -framework CoreFoundation -framework CoreGraphics -framework CoreText -o obj/font_test src/font_test.c$DYN -fobjc-arc -framework Foundation -framework SystemConfiguration -o obj/net_test src/net_test.m# (with an entitlement for "battery_test open URL", if ldid is there: src/battery_test.c)$DYN -framework CoreFoundation -framework IOKit -lsqlite3 -o obj/battery_test src/battery_test.c &&@@ -160,6 +161,7 @@ if [ -d $IOS/rootfs/usr/lib ]; then[ -f obj/cg_test ] && cp obj/cg_test root/bin/[ -f obj/shm_test ] && cp obj/shm_test root/bin/[ -f obj/snap_test ] && cp obj/snap_test root/bin/+ [ -f obj/font_test ] && cp obj/font_test root/bin/[ -f obj/net_test ] && cp obj/net_test root/bin/[ -f obj/battery_test ] && cp obj/battery_test root/bin/[ -f obj/keychain_test ] && cp obj/keychain_test root/bin/diff --git a/userland/rc.jobs.fonts b/userland/rc.jobs.fontsnew file mode 100644index 0000000..ab6aaee--- /dev/null+++ b/userland/rc.jobs.fonts@@ -0,0 +1,11 @@+# Commands run by the launchd job com.redwood.demo (see com.redwood.demo.plist).+# Text in scripts other than Latin: font_test says which font the system font falls back to for emoji, Chinese,+# Japanese and Korean and how many pixels drawing them paints (and in colour); then, with wifi=1, Safari opens the+# Chinese Wikipedia and a page of emoji as text, for screenshots.+/redwood/bin/sound_test springboard+/redwood/bin/rwtouch wait 10+/redwood/bin/font_test+/redwood/bin/battery_test open https://zh.m.wikipedia.org/+/redwood/bin/rwtouch wait 25+/redwood/bin/battery_test open https://getemoji.com/+/redwood/bin/rwtouch wait 25diff --git a/userland/src/font_test.c b/userland/src/font_test.cnew file mode 100644index 0000000..4b9d190--- /dev/null+++ b/userland/src/font_test.c@@ -0,0 +1,111 @@+/*+ * font_test: whether text in other scripts than Latin gets drawn: for each sample, the font CoreText+ * falls back to from the system font (CTFontCreateForString), and how many pixels drawing the sample+ * with that fallback (a CTLine, as UIKit and WebKit draw text) paints into a bitmap, how many of them+ * in colour. A sample with no font of its own falls back to LastResort or draws nothing; the colour+ * emoji come out in colour.+ *+ * usage: font_test [TEXT...] (default: emoji, Chinese, Japanese, Korean, Latin)+ */+#include <CoreFoundation/CoreFoundation.h>+#include <CoreGraphics/CoreGraphics.h>+#include <CoreText/CoreText.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>++#define WIDTH 900+#define HEIGHT 160+#define POINTS 51.0 /* 17 points at three pixels a point */++static void+sample(CTFontRef system, const char *text)+{+ CFStringRef string = CFStringCreateWithCString(NULL, text, kCFStringEncodingUTF8);+ CTFontRef fallback = CTFontCreateForString(system, string, CFRangeMake(0, CFStringGetLength(string)));+ CFStringRef name = CTFontCopyPostScriptName(fallback);+ char font[128] = "?";+ const void *keys[] = { kCTFontAttributeName };+ const void *values[] = { system };+ CFDictionaryRef attributes = CFDictionaryCreate(NULL, keys, values, 1, &kCFTypeDictionaryKeyCallBacks,+ &kCFTypeDictionaryValueCallBacks);+ CFAttributedStringRef attributed = CFAttributedStringCreate(NULL, string, attributes);+ CTLineRef line = CTLineCreateWithAttributedString(attributed);+ CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();+ uint8_t *pixels = calloc(WIDTH * HEIGHT, 4);+ CGContextRef context = CGBitmapContextCreate(pixels, WIDTH, HEIGHT, 8, WIDTH * 4, rgb,+ kCGImageAlphaPremultipliedLast);+ unsigned painted = 0, coloured = 0;+ CFArrayRef runs = CTLineGetGlyphRuns(line);+ char used[256] = "";++ CFStringGetCString(name, font, sizeof(font), kCFStringEncodingUTF8);+ /* which fonts the line itself drew with, after its own fallback */+ for (CFIndex i = 0; i < CFArrayGetCount(runs); i++) {+ CTFontRef run_font = CFDictionaryGetValue(CTRunGetAttributes(CFArrayGetValueAtIndex(runs, i)), kCTFontAttributeName);+ CFStringRef run_name = run_font ? CTFontCopyPostScriptName(run_font) : NULL;+ char buffer[96] = "?";++ if (run_name != NULL) {+ CFStringGetCString(run_name, buffer, sizeof(buffer), kCFStringEncodingUTF8);+ CFRelease(run_name);+ }+ if (strlen(used) + strlen(buffer) + 3 < sizeof(used)) {+ strcat(used, i ? ", " : "");+ strcat(used, buffer);+ }+ }+ CGContextSetRGBFillColor(context, 0, 0, 0, 1);+ CGContextSetTextPosition(context, 10, 50);+ CTLineDraw(line, context);+ CGContextFlush(context);+ for (int i = 0; i < WIDTH * HEIGHT; i++) {+ uint8_t *p = pixels + 4 * i;+ int high = p[0] > p[1] ? (p[0] > p[2] ? p[0] : p[2]) : (p[1] > p[2] ? p[1] : p[2]);+ int low = p[0] < p[1] ? (p[0] < p[2] ? p[0] : p[2]) : (p[1] < p[2] ? p[1] : p[2]);++ if (p[3] > 32) {+ painted++;+ coloured += high - low > 40;+ }+ }+ printf("font_test: \"%s\": the system font falls back to %s; drawn with %s: %u pixels painted, %u of them in colour\n",+ text, font, used, painted, coloured);+ CGContextRelease(context);+ free(pixels);+ CGColorSpaceRelease(rgb);+ CFRelease(line);+ CFRelease(attributed);+ CFRelease(attributes);+ CFRelease(name);+ CFRelease(fallback);+ CFRelease(string);+}++int+main(int argc, char **argv)+{+ static const char *const samples[] = { "\xF0\x9F\x98\x80\xF0\x9F\x91\x8D\xE2\x9D\xA4\xEF\xB8\x8F", /* grinning face, thumbs up, heart */+ "\xE4\xB8\xAD\xE6\x96\x87\xE5\xAD\x97\xE4\xBD\x93", /* Chinese: zhong wen zi ti */+ "\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E", /* Japanese: nihongo */+ "\xED\x95\x9C\xEA\xB5\xAD\xEC\x96\xB4", /* Korean: hangugeo */+ "Hello" };+ CTFontRef system = CTFontCreateUIFontForLanguage(kCTFontUIFontSystem, POINTS, NULL);++ setvbuf(stdout, NULL, _IONBF, 0);+ if (system == NULL) {+ printf("font_test: no system font\n");+ return 1;+ }+ if (argc > 1) {+ for (int i = 1; i < argc; i++) {+ sample(system, argv[i]);+ }+ } else {+ for (unsigned i = 0; i < sizeof(samples) / sizeof(samples[0]); i++) {+ sample(system, samples[i]);+ }+ }+ CFRelease(system);+ return 0;+}