| /** |
| * Copyright (C) 2020 The Android Open Source Project |
| * |
| * 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 <sys/stat.h> |
| #include <fcntl.h> |
| #include <unistd.h> |
| #include <stdio.h> |
| #include <sys/mman.h> |
| #include "SkDocument.h" |
| #include "SkStream.h" |
| #include "SkTypeface.h" |
| #include "SkCanvas.h" |
| |
| #define ALLOC_SIZE 191422 |
| void* g_mmap_ptr = NULL; |
| |
| void * operator new(size_t size) { |
| if (size != ALLOC_SIZE) { |
| return malloc(size); |
| } |
| void* mem_ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, |
| MAP_FILE | MAP_SHARED | MAP_ANONYMOUS, -1, 0); |
| if (mem_ptr == MAP_FAILED) { |
| return malloc(size); |
| } |
| g_mmap_ptr = mem_ptr; |
| return mem_ptr; |
| } |
| |
| void operator delete(void* mem_ptr) { |
| if(g_mmap_ptr == mem_ptr) { |
| munmap(mem_ptr, ALLOC_SIZE); |
| return; |
| } |
| return free(mem_ptr); |
| } |
| |
| struct NullWStream : public SkWStream { |
| NullWStream() |
| : fN(0) { |
| } |
| bool write(const void*, size_t n) override { |
| fN += n; |
| return true; |
| } |
| size_t bytesWritten() const override { |
| return fN; |
| } |
| size_t fN; |
| }; |
| |
| int main(int argc, char **argv) { |
| if (argc != 2) { |
| return EXIT_SUCCESS; |
| } |
| SkWStream* outputStream = new NullWStream(); |
| SkScalar pageWidth = 100; |
| SkScalar pageHeight = 100; |
| SkPaint paint; |
| char text[] = "C"; |
| sk_sp < SkDocument > pdfDocument(SkDocument::MakePDF(outputStream)); |
| SkCanvas* pageCanvas = pdfDocument->beginPage(pageWidth, pageHeight); |
| sk_sp < SkTypeface > typeFace = SkTypeface::MakeFromFile(argv[1]); |
| paint.setTypeface(typeFace); |
| pageCanvas->drawText(text, strlen(text), 0, 0, paint); |
| pdfDocument->endPage(); |
| pdfDocument->close(); |
| return EXIT_SUCCESS; |
| } |