|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, Srimanta Barua <srimanta.barua1@gmail.com> |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above copyright notice, this |
| 9 | + * list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 18 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 19 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 20 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 21 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 22 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 23 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "TTFont.h" |
| 28 | +#include <AK/LogStream.h> |
| 29 | +#include <bits/stdint.h> |
| 30 | +#include <LibCore/File.h> |
| 31 | + |
| 32 | +namespace Gfx { |
| 33 | + |
| 34 | +static u16 be_u16(const u8* ptr) |
| 35 | +{ |
| 36 | + return (((u16) ptr[0]) << 8) | ((u16) ptr[1]); |
| 37 | +} |
| 38 | + |
| 39 | +static u32 be_u32(const u8* ptr) |
| 40 | +{ |
| 41 | + return (((u32) ptr[0]) << 24) | (((u32) ptr[1]) << 16) | (((u32) ptr[2]) << 8) | ((u32) ptr[3]); |
| 42 | +} |
| 43 | + |
| 44 | +static i16 be_i16(const u8* ptr) |
| 45 | +{ |
| 46 | + return (((i16) ptr[0]) << 8) | ((i16) ptr[1]); |
| 47 | +} |
| 48 | + |
| 49 | +static u32 tag_from_str(const char *str) |
| 50 | +{ |
| 51 | + return be_u32((const u8*) str); |
| 52 | +} |
| 53 | + |
| 54 | +u16 TTFHead::units_per_em() const |
| 55 | +{ |
| 56 | + return be_u16(m_slice.offset_pointer(18)); |
| 57 | +} |
| 58 | + |
| 59 | +i16 TTFHead::xmin() const |
| 60 | +{ |
| 61 | + return be_i16(m_slice.offset_pointer(36)); |
| 62 | +} |
| 63 | + |
| 64 | +i16 TTFHead::ymin() const |
| 65 | +{ |
| 66 | + return be_i16(m_slice.offset_pointer(38)); |
| 67 | +} |
| 68 | + |
| 69 | +i16 TTFHead::xmax() const |
| 70 | +{ |
| 71 | + return be_i16(m_slice.offset_pointer(40)); |
| 72 | +} |
| 73 | + |
| 74 | +i16 TTFHead::ymax() const |
| 75 | +{ |
| 76 | + return be_i16(m_slice.offset_pointer(42)); |
| 77 | +} |
| 78 | + |
| 79 | +u16 TTFHead::lowest_recommended_ppem() const |
| 80 | +{ |
| 81 | + return be_u16(m_slice.offset_pointer(46)); |
| 82 | +} |
| 83 | + |
| 84 | +Result<TTFIndexToLocFormat, i16> TTFHead::index_to_loc_format() const |
| 85 | +{ |
| 86 | + i16 raw = be_i16(m_slice.offset_pointer(50)); |
| 87 | + switch (raw) { |
| 88 | + case 0: |
| 89 | + return TTFIndexToLocFormat::Offset16; |
| 90 | + case 1: |
| 91 | + return TTFIndexToLocFormat::Offset32; |
| 92 | + default: |
| 93 | + return raw; |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +OwnPtr<TTFont> TTFont::load_from_file(const StringView& path, unsigned index) |
| 98 | +{ |
| 99 | + dbg() << "path: " << path << " | index: " << index; |
| 100 | + auto file_or_error = Core::File::open(String(path), Core::IODevice::ReadOnly); |
| 101 | + if (file_or_error.is_error()) { |
| 102 | + dbg() << "Could not open file: " << file_or_error.error(); |
| 103 | + return nullptr; |
| 104 | + } |
| 105 | + auto file = file_or_error.value(); |
| 106 | + if (!file->open(Core::IODevice::ReadOnly)) { |
| 107 | + dbg() << "Could not open file"; |
| 108 | + return nullptr; |
| 109 | + } |
| 110 | + auto buffer = file->read_all(); |
| 111 | + if (buffer.size() < 4) { |
| 112 | + dbg() << "Font file too small"; |
| 113 | + return nullptr; |
| 114 | + } |
| 115 | + u32 tag = be_u32(buffer.data()); |
| 116 | + if (tag == tag_from_str("ttcf")) { |
| 117 | + // It's a font collection |
| 118 | + if (buffer.size() < 12 + 4 * (index + 1)) { |
| 119 | + dbg() << "Font file too small"; |
| 120 | + return nullptr; |
| 121 | + } |
| 122 | + u32 offset = be_u32(buffer.offset_pointer(12 + 4 * index)); |
| 123 | + return OwnPtr(new TTFont(move(buffer), offset)); |
| 124 | + } else if (tag == tag_from_str("OTTO")) { |
| 125 | + dbg() << "CFF fonts not supported yet"; |
| 126 | + return nullptr; |
| 127 | + } else if (tag != 0x00010000) { |
| 128 | + dbg() << "Not a valid TTF font"; |
| 129 | + return nullptr; |
| 130 | + } else { |
| 131 | + return OwnPtr(new TTFont(move(buffer), 0)); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +TTFont::TTFont(AK::ByteBuffer&& buffer, u32 offset) |
| 136 | + : m_buffer(move(buffer)) |
| 137 | + { |
| 138 | + ASSERT(m_buffer.size() >= offset + 12); |
| 139 | + bool head_has_been_initialized = false; |
| 140 | + |
| 141 | + //auto sfnt_version = be_u32(data + offset); |
| 142 | + auto num_tables = be_u16(m_buffer.offset_pointer(offset + 4)); |
| 143 | + ASSERT(m_buffer.size() >= offset + 12 + num_tables * 16); |
| 144 | + |
| 145 | + for (auto i = 0; i < num_tables; i++) { |
| 146 | + u32 record_offset = offset + 12 + i * 16; |
| 147 | + u32 tag = be_u32(m_buffer.offset_pointer(record_offset)); |
| 148 | + u32 table_offset = be_u32(m_buffer.offset_pointer(record_offset + 8)); |
| 149 | + u32 table_length = be_u32(m_buffer.offset_pointer(record_offset + 12)); |
| 150 | + ASSERT(m_buffer.size() >= table_offset + table_length); |
| 151 | + |
| 152 | + // Get the tables we need |
| 153 | + if (tag == tag_from_str("head")) { |
| 154 | + auto buffer = ByteBuffer::wrap(m_buffer.offset_pointer(table_offset), table_length); |
| 155 | + m_head = TTFHead(move(buffer)); |
| 156 | + head_has_been_initialized = true; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + // Check that we've got everything we need |
| 161 | + ASSERT(head_has_been_initialized); |
| 162 | + } |
| 163 | +} |
0 commit comments