Home | History | Annotate | Download | only in data
      1 /*
      2  * Copyright 2011 Google Inc. All Rights Reserved.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include "sfntly/data/font_output_stream.h"
     18 
     19 #include <algorithm>
     20 
     21 namespace sfntly {
     22 
     23 FontOutputStream::FontOutputStream(OutputStream* os)
     24     : stream_(os),
     25       position_(0) {
     26 }
     27 
     28 FontOutputStream::~FontOutputStream() {
     29   // Do not close, underlying stream shall clean up themselves.
     30 }
     31 
     32 void FontOutputStream::Write(byte_t b) {
     33   if (stream_) {
     34     stream_->Write(b);
     35     position_++;
     36   }
     37 }
     38 
     39 void FontOutputStream::Write(ByteVector* b) {
     40   if (b) {
     41     Write(b, 0, b->size());
     42     position_ += b->size();
     43   }
     44 }
     45 
     46 void FontOutputStream::Write(ByteVector* b, int32_t off, int32_t len) {
     47   assert(b);
     48   assert(stream_);
     49   if (off < 0 || len < 0 || off + len < 0 ||
     50       static_cast<size_t>(off + len) > b->size()) {
     51 #if !defined (SFNTLY_NO_EXCEPTION)
     52     throw IndexOutOfBoundException();
     53 #else
     54     return;
     55 #endif
     56   }
     57 
     58   stream_->Write(b, off, len);
     59   position_ += len;
     60 }
     61 
     62 void FontOutputStream::Write(byte_t* b, int32_t off, int32_t len) {
     63   assert(b);
     64   assert(stream_);
     65   if (off < 0 || len < 0 || off + len < 0) {
     66 #if !defined (SFNTLY_NO_EXCEPTION)
     67     throw IndexOutOfBoundException();
     68 #else
     69     return;
     70 #endif
     71   }
     72 
     73   stream_->Write(b, off, len);
     74   position_ += len;
     75 }
     76 
     77 void FontOutputStream::WriteChar(byte_t c) {
     78   Write(c);
     79 }
     80 
     81 void FontOutputStream::WriteUShort(int32_t us) {
     82   Write((byte_t)((us >> 8) & 0xff));
     83   Write((byte_t)(us & 0xff));
     84 }
     85 
     86 void FontOutputStream::WriteShort(int32_t s) {
     87   WriteUShort(s);
     88 }
     89 
     90 void FontOutputStream::WriteUInt24(int32_t ui) {
     91   Write((byte_t)(ui >> 16) & 0xff);
     92   Write((byte_t)(ui >> 8) & 0xff);
     93   Write((byte_t)ui & 0xff);
     94 }
     95 
     96 void FontOutputStream::WriteULong(int64_t ul) {
     97   Write((byte_t)((ul >> 24) & 0xff));
     98   Write((byte_t)((ul >> 16) & 0xff));
     99   Write((byte_t)((ul >> 8) & 0xff));
    100   Write((byte_t)(ul & 0xff));
    101 }
    102 
    103 void FontOutputStream::WriteLong(int64_t l) {
    104   WriteULong(l);
    105 }
    106 
    107 void FontOutputStream::WriteFixed(int32_t f) {
    108   WriteULong(f);
    109 }
    110 
    111 void FontOutputStream::WriteDateTime(int64_t date) {
    112   WriteULong((date >> 32) & 0xffffffff);
    113   WriteULong(date & 0xffffffff);
    114 }
    115 
    116 void FontOutputStream::Flush() {
    117   if (stream_) {
    118     stream_->Flush();
    119   }
    120 }
    121 
    122 void FontOutputStream::Close() {
    123   if (stream_) {
    124     stream_->Flush();
    125     stream_->Close();
    126     position_ = 0;
    127   }
    128 }
    129 
    130 }  // namespace sfntly
    131