This repository has been archived by the owner on Aug 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1324739 - patch 1 - Import sfntly (C++ port) into the mozilla tre…
…e. r=lsalzman
- Loading branch information
Showing
189 changed files
with
33,678 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
This directory contains the C++ port of SFNTLY, | ||
from https://github.com/googlei18n/sfntly/tree/master/cpp. | ||
|
||
SFNTLY is distributed under the Apache license v2.0 | ||
(see cpp/COPYING.txt). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* Copyright 2011 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include <stdio.h> | ||
|
||
#include <vector> | ||
#include <string> | ||
#include <sstream> | ||
|
||
#include "sfntly/port/type.h" | ||
#include "font_subsetter.h" | ||
|
||
template <typename T> | ||
class HexTo { | ||
public: | ||
explicit HexTo(const char* in) { | ||
std::stringstream ss; | ||
ss << std::hex << in; | ||
ss >> value_; | ||
} | ||
operator T() const { return value_; } | ||
|
||
private: | ||
T value_; | ||
}; | ||
|
||
bool LoadFile(const char* input_file_path, sfntly::ByteVector* input_buffer) { | ||
assert(input_file_path); | ||
assert(input_buffer); | ||
|
||
FILE* input_file = NULL; | ||
#if defined WIN32 | ||
fopen_s(&input_file, input_file_path, "rb"); | ||
#else | ||
input_file = fopen(input_file_path, "rb"); | ||
#endif | ||
if (input_file == NULL) { | ||
return false; | ||
} | ||
fseek(input_file, 0, SEEK_END); | ||
size_t file_size = ftell(input_file); | ||
fseek(input_file, 0, SEEK_SET); | ||
input_buffer->resize(file_size); | ||
size_t bytes_read = fread(&((*input_buffer)[0]), 1, file_size, input_file); | ||
fclose(input_file); | ||
return bytes_read == file_size; | ||
} | ||
|
||
bool SaveFile(const char* output_file_path, const unsigned char* output_buffer, | ||
int buffer_length) { | ||
int byte_count = 0; | ||
if (buffer_length > 0) { | ||
FILE* output_file = NULL; | ||
#if defined WIN32 | ||
fopen_s(&output_file, output_file_path, "wb"); | ||
#else | ||
output_file = fopen(output_file_path, "wb"); | ||
#endif | ||
if (output_file) { | ||
byte_count = fwrite(output_buffer, 1, buffer_length, output_file); | ||
fflush(output_file); | ||
fclose(output_file); | ||
} | ||
return buffer_length == byte_count; | ||
} | ||
return false; | ||
} | ||
|
||
bool StringToGlyphId(const char* input, std::vector<unsigned int>* glyph_ids) { | ||
assert(input); | ||
std::string hex_csv = input; | ||
size_t start = 0; | ||
size_t end = hex_csv.find_first_of(","); | ||
while (end != std::string::npos) { | ||
glyph_ids->push_back( | ||
HexTo<unsigned int>(hex_csv.substr(start, end - start).c_str())); | ||
start = end + 1; | ||
end = hex_csv.find_first_of(",", start); | ||
} | ||
glyph_ids->push_back(HexTo<unsigned int>(hex_csv.substr(start).c_str())); | ||
return glyph_ids->size() > 0; | ||
} | ||
|
||
int main(int argc, char** argv) { | ||
if (argc < 5) { | ||
fprintf(stderr, | ||
"Usage: %s <input path> <output path> <font name> <glyph ids>\n", | ||
argv[0]); | ||
fprintf(stderr, "\tGlyph ids are comma separated hex values\n"); | ||
fprintf(stderr, "\te.g. 20,1a,3b,4f\n"); | ||
return 0; | ||
} | ||
|
||
sfntly::ByteVector input_buffer; | ||
if (!LoadFile(argv[1], &input_buffer)) { | ||
fprintf(stderr, "ERROR: unable to load font file %s\n", argv[1]); | ||
return 0; | ||
} | ||
|
||
std::vector<unsigned int> glyph_ids; | ||
if (!StringToGlyphId(argv[4], &glyph_ids)) { | ||
fprintf(stderr, "ERROR: unable to parse input glyph id\n"); | ||
return 0; | ||
} | ||
|
||
unsigned char* output_buffer = NULL; | ||
int output_length = | ||
SfntlyWrapper::SubsetFont(argv[3], | ||
&(input_buffer[0]), | ||
input_buffer.size(), | ||
&(glyph_ids[0]), | ||
glyph_ids.size(), | ||
&output_buffer); | ||
|
||
int result = SaveFile(argv[2], output_buffer, output_length) ? 1 : 0; | ||
delete[] output_buffer; | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2011 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "font_subsetter.h" | ||
|
||
#include "subsetter_impl.h" | ||
|
||
int SfntlyWrapper::SubsetFont(const char* font_name, | ||
const unsigned char* original_font, | ||
size_t font_size, | ||
const unsigned int* glyph_ids, | ||
size_t glyph_count, | ||
unsigned char** output_buffer) { | ||
if (output_buffer == NULL || | ||
original_font == NULL || font_size == 0 || | ||
glyph_ids == NULL || glyph_count == 0) { | ||
return 0; | ||
} | ||
|
||
sfntly::SubsetterImpl subsetter; | ||
if (!subsetter.LoadFont(font_name, original_font, font_size)) { | ||
return -1; // Load error or font not found. | ||
} | ||
|
||
return subsetter.SubsetFont(glyph_ids, glyph_count, output_buffer); | ||
} | ||
|
||
int SfntlyWrapper::SubsetFont(int font_index, | ||
const unsigned char* original_font, | ||
size_t font_size, | ||
const unsigned int* glyph_ids, | ||
size_t glyph_count, | ||
unsigned char** output_buffer) { | ||
if (output_buffer == NULL || | ||
original_font == NULL || font_size == 0 || | ||
glyph_ids == NULL || glyph_count == 0) { | ||
return 0; | ||
} | ||
|
||
sfntly::SubsetterImpl subsetter; | ||
if (!subsetter.LoadFont(font_index, original_font, font_size)) { | ||
return -1; // Load error or font not found. | ||
} | ||
|
||
return subsetter.SubsetFont(glyph_ids, glyph_count, output_buffer); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2011 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
// File is originally from Chromium third_party/sfntly/src/subsetter. | ||
// Use as test case in sfntly so that problems can be caught in upstream early. | ||
#ifndef SFNTLY_CPP_SRC_TEST_FONT_SUBSETTER_H_ | ||
#define SFNTLY_CPP_SRC_TEST_FONT_SUBSETTER_H_ | ||
|
||
#include <stddef.h> | ||
|
||
class SfntlyWrapper { | ||
public: | ||
|
||
// Font subsetting API | ||
// | ||
// Input TTF/TTC/OTF fonts, specify the glyph IDs to subset, and the subset | ||
// font is returned in |output_buffer| (caller to delete[]). Return value is | ||
// the length of output_buffer allocated. | ||
// | ||
// If subsetting fails, a negative value is returned. If none of the glyph | ||
// IDs specified is found, the function will return 0. | ||
// | ||
// |font_name| Font name, required for TTC files. If specified NULL, | ||
// the first available font is selected. | ||
// |original_font| Original font file contents. | ||
// |font_size| Size of |original_font| in bytes. | ||
// |glyph_ids| Glyph IDs to subset. If the specified glyph ID is not | ||
// found in the font file, it will be ignored silently. | ||
// |glyph_count| Number of glyph IDs in |glyph_ids| | ||
// |output_buffer| Generated subset font. Caller to delete[]. | ||
static int SubsetFont(const char* font_name, | ||
const unsigned char* original_font, | ||
size_t font_size, | ||
const unsigned int* glyph_ids, | ||
size_t glyph_count, | ||
unsigned char** output_buffer); | ||
|
||
|
||
// Font subsetting API | ||
// | ||
// Input TTF/TTC/OTF fonts, specify the glyph IDs to subset, and the subset | ||
// font is returned in |output_buffer| (caller to delete[]). Return value is | ||
// the length of output_buffer allocated. | ||
// | ||
// If subsetting fails, a negative value is returned. If none of the glyph | ||
// IDs specified is found, the function will return 0. | ||
// | ||
// |font_name| Font index, ignored for non-TTC files, 0-indexed. | ||
// |original_font| Original font file contents. | ||
// |font_size| Size of |original_font| in bytes. | ||
// |glyph_ids| Glyph IDs to subset. If the specified glyph ID is not | ||
// found in the font file, it will be ignored silently. | ||
// |glyph_count| Number of glyph IDs in |glyph_ids| | ||
// |output_buffer| Generated subset font. Caller to delete[]. | ||
static int SubsetFont(int font_index, | ||
const unsigned char* original_font, | ||
size_t font_size, | ||
const unsigned int* glyph_ids, | ||
size_t glyph_count, | ||
unsigned char** output_buffer); | ||
}; | ||
|
||
#endif // SFNTLY_CPP_SRC_TEST_FONT_SUBSETTER_H_ |
Oops, something went wrong.