gfx/ots/src/ltsh.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 #include "ltsh.h"
michael@0 6
michael@0 7 #include "maxp.h"
michael@0 8
michael@0 9 // LTSH - Linear Threshold
michael@0 10 // http://www.microsoft.com/typography/otspec/ltsh.htm
michael@0 11
michael@0 12 #define TABLE_NAME "LTSH"
michael@0 13
michael@0 14 #define DROP_THIS_TABLE \
michael@0 15 do { \
michael@0 16 delete file->ltsh; \
michael@0 17 file->ltsh = 0; \
michael@0 18 OTS_FAILURE_MSG("Table discarded"); \
michael@0 19 } while (0)
michael@0 20
michael@0 21 namespace ots {
michael@0 22
michael@0 23 bool ots_ltsh_parse(OpenTypeFile *file, const uint8_t *data, size_t length) {
michael@0 24 Buffer table(data, length);
michael@0 25
michael@0 26 if (!file->maxp) {
michael@0 27 return OTS_FAILURE_MSG("Missing maxp table from font needed by ltsh");
michael@0 28 }
michael@0 29
michael@0 30 OpenTypeLTSH *ltsh = new OpenTypeLTSH;
michael@0 31 file->ltsh = ltsh;
michael@0 32
michael@0 33 uint16_t num_glyphs = 0;
michael@0 34 if (!table.ReadU16(&ltsh->version) ||
michael@0 35 !table.ReadU16(&num_glyphs)) {
michael@0 36 return OTS_FAILURE_MSG("Failed to read ltsh header");
michael@0 37 }
michael@0 38
michael@0 39 if (ltsh->version != 0) {
michael@0 40 OTS_WARNING("bad version: %u", ltsh->version);
michael@0 41 DROP_THIS_TABLE;
michael@0 42 return true;
michael@0 43 }
michael@0 44
michael@0 45 if (num_glyphs != file->maxp->num_glyphs) {
michael@0 46 OTS_WARNING("bad num_glyphs: %u", num_glyphs);
michael@0 47 DROP_THIS_TABLE;
michael@0 48 return true;
michael@0 49 }
michael@0 50
michael@0 51 ltsh->ypels.reserve(num_glyphs);
michael@0 52 for (unsigned i = 0; i < num_glyphs; ++i) {
michael@0 53 uint8_t pel = 0;
michael@0 54 if (!table.ReadU8(&pel)) {
michael@0 55 return OTS_FAILURE_MSG("Failed to read pixels for glyph %d", i);
michael@0 56 }
michael@0 57 ltsh->ypels.push_back(pel);
michael@0 58 }
michael@0 59
michael@0 60 return true;
michael@0 61 }
michael@0 62
michael@0 63 bool ots_ltsh_should_serialise(OpenTypeFile *file) {
michael@0 64 if (!file->glyf) return false; // this table is not for CFF fonts.
michael@0 65 return file->ltsh != NULL;
michael@0 66 }
michael@0 67
michael@0 68 bool ots_ltsh_serialise(OTSStream *out, OpenTypeFile *file) {
michael@0 69 const OpenTypeLTSH *ltsh = file->ltsh;
michael@0 70
michael@0 71 if (!out->WriteU16(ltsh->version) ||
michael@0 72 !out->WriteU16(ltsh->ypels.size())) {
michael@0 73 return OTS_FAILURE_MSG("Failed to write pels size");
michael@0 74 }
michael@0 75 for (unsigned i = 0; i < ltsh->ypels.size(); ++i) {
michael@0 76 if (!out->Write(&(ltsh->ypels[i]), 1)) {
michael@0 77 return OTS_FAILURE_MSG("Failed to write pixel size for glyph %d", i);
michael@0 78 }
michael@0 79 }
michael@0 80
michael@0 81 return true;
michael@0 82 }
michael@0 83
michael@0 84 void ots_ltsh_free(OpenTypeFile *file) {
michael@0 85 delete file->ltsh;
michael@0 86 }
michael@0 87
michael@0 88 } // namespace ots

mercurial