1464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com/* 2464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * Copyright 2011 Google Inc. All Rights Reserved. 3464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * 4464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * Licensed under the Apache License, Version 2.0 (the "License"); 5464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * you may not use this file except in compliance with the License. 6464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * You may obtain a copy of the License at 7464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * 8464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * http://www.apache.org/licenses/LICENSE-2.0 9464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * 10464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * Unless required by applicable law or agreed to in writing, software 11464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * distributed under the License is distributed on an "AS IS" BASIS, 12464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * See the License for the specific language governing permissions and 14464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * limitations under the License. 15464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com */ 16464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com 17464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com// Remove VC++ nag on fopen. 18464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com#define _CRT_SECURE_NO_WARNINGS 19464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com 205af34fd773f8cfee82321393504f558ddf67c628arthurhsu@google.com#include "sample/subsetter/subset_util.h" 215af34fd773f8cfee82321393504f558ddf67c628arthurhsu@google.com 22464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com#include <stdio.h> 235af34fd773f8cfee82321393504f558ddf67c628arthurhsu@google.com 24464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com#include <vector> 25464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com#include <memory> 26464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com 27464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com#include "sfntly/font.h" 28464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com#include "sfntly/data/memory_byte_array.h" 29464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com#include "sfntly/port/memory_output_stream.h" 305af34fd773f8cfee82321393504f558ddf67c628arthurhsu@google.com#include "sfntly/port/type.h" 31464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com#include "sfntly/tag.h" 325af34fd773f8cfee82321393504f558ddf67c628arthurhsu@google.com#include "sfntly/tools/subsetter/subsetter.h" 33464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com 34464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.comnamespace sfntly { 35464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com 36464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.comSubsetUtil::SubsetUtil() { 37464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com} 38464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com 39464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.comSubsetUtil::~SubsetUtil() { 40464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com} 41464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com 42246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.comvoid SubsetUtil::Subset(const char *input_file_path, 43464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com const char *output_file_path) { 44464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com UNREFERENCED_PARAMETER(output_file_path); 45464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com ByteVector input_buffer; 46464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com FILE* input_file = fopen(input_file_path, "rb"); 47464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com if (input_file == NULL) { 48464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com fprintf(stderr, "file not found\n"); 49464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com return; 50464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com } 51464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com fseek(input_file, 0, SEEK_END); 52464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com size_t file_size = ftell(input_file); 53464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com fseek(input_file, 0, SEEK_SET); 54464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com input_buffer.resize(file_size); 5535a9bf28a889295528bfe46ab51902a460be5407arthurhsu@google.com size_t bytes_read = fread(&(input_buffer[0]), 1, file_size, input_file); 5635a9bf28a889295528bfe46ab51902a460be5407arthurhsu@google.com UNREFERENCED_PARAMETER(bytes_read); 57464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com fclose(input_file); 58464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com 59464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com FontFactoryPtr factory; 60246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com factory.Attach(FontFactory::GetInstance()); 61464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com 62464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com FontArray font_array; 63b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com factory->LoadFonts(&input_buffer, &font_array); 64464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com if (font_array.empty() || font_array[0] == NULL) 65464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com return; 66464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com 67464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com IntegerList glyphs; 68464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com for (int32_t i = 0; i < 10; i++) { 69464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com glyphs.push_back(i); 70464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com } 71464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com glyphs.push_back(11); 72464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com glyphs.push_back(10); 73464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com 74464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com Ptr<Subsetter> subsetter = new Subsetter(font_array[0], factory); 75246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com subsetter->SetGlyphs(&glyphs); 76464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com IntegerSet remove_tables; 77464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com remove_tables.insert(Tag::DSIG); 78246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com subsetter->SetRemoveTables(&remove_tables); 79464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com 80464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com FontBuilderPtr font_builder; 81246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com font_builder.Attach(subsetter->Subset()); 82464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com 83464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com FontPtr new_font; 84246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com new_font.Attach(font_builder->Build()); 85464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com 86464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com // TODO(arthurhsu): glyph renumbering/Loca table 87464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com // TODO(arthurhsu): alter CMaps 88464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com 89464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com MemoryOutputStream output_stream; 90246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com factory->SerializeFont(new_font, &output_stream); 91464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com 92464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com FILE* output_file = fopen(output_file_path, "wb"); 93246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com fwrite(output_stream.Get(), 1, output_stream.Size(), output_file); 94464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com fflush(output_file); 95464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com fclose(output_file); 96464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com} 97464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com 98464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com} // namespace sfntly 99