Added PDF Support (#13)
authorMatthew Leibowitz <mattleibow@live.com>
Fri, 3 Jun 2016 12:45:51 +0000 (14:45 +0200)
committerMatthew Leibowitz <mattleibow@live.com>
Fri, 3 Jun 2016 12:45:51 +0000 (14:45 +0200)
 - Added the SkDocument methods
 - Added the SkWStream methods, and the sub classes
 - Added destructors for the SkStream types

gyp/core.gypi
include/c/xamarin/sk_x_document.h [new file with mode: 0644]
include/c/xamarin/sk_x_stream.h
include/c/xamarin/sk_x_types.h
src/c/xamarin/sk_x_document.cpp [new file with mode: 0644]
src/c/xamarin/sk_x_stream.cpp
src/c/xamarin/sk_x_types_priv.h

index 7dec7960c1fcb54c3d0b64a5c9aa06183125b358..49631f252a618983c9b8d207a38908d3a6704b8c 100644 (file)
@@ -17,6 +17,7 @@
         '<(skia_include_path)/c/xamarin/sk_x_imagedecoder.h',
         '<(skia_include_path)/c/xamarin/sk_x_canvas.h',
         '<(skia_include_path)/c/xamarin/sk_x_data.h',
+        '<(skia_include_path)/c/xamarin/sk_x_document.h',
         '<(skia_include_path)/c/xamarin/sk_x_image.h',
         '<(skia_include_path)/c/xamarin/sk_x_maskfilter.h',
         '<(skia_include_path)/c/xamarin/sk_x_imagefilter.h',
@@ -32,6 +33,7 @@
         '<(skia_src_path)/c/xamarin/sk_x_imagedecoder.cpp',
         '<(skia_src_path)/c/xamarin/sk_x_canvas.cpp',
         '<(skia_src_path)/c/xamarin/sk_x_data.cpp',
+        '<(skia_src_path)/c/xamarin/sk_x_document.cpp',
         '<(skia_src_path)/c/xamarin/sk_x_image.cpp',
         '<(skia_src_path)/c/xamarin/sk_x_maskfilter.cpp',
         '<(skia_src_path)/c/xamarin/sk_x_imagefilter.cpp',
diff --git a/include/c/xamarin/sk_x_document.h b/include/c/xamarin/sk_x_document.h
new file mode 100644 (file)
index 0000000..28bf546
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2016 Xamarin Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+// EXPERIMENTAL EXPERIMENTAL EXPERIMENTAL EXPERIMENTAL
+// DO NOT USE -- FOR INTERNAL TESTING ONLY
+
+#ifndef sk_x_document_DEFINED
+#define sk_x_document_DEFINED
+
+#include "sk_types.h"
+#include "xamarin/sk_x_types.h"
+
+SK_C_PLUS_PLUS_BEGIN_GUARD
+
+SK_API void sk_document_unref(sk_document_t* document);
+
+SK_API sk_document_t* sk_document_create_pdf_from_stream(sk_wstream_t* stream, float dpi);
+SK_API sk_document_t* sk_document_create_pdf_from_filename(const char* path, float dpi);
+
+//SK_API sk_document_t* sk_document_create_xps_from_stream(sk_wstream_t* stream, float dpi);
+//SK_API sk_document_t* sk_document_create_xps_from_filename(const char* path, float dpi);
+
+SK_API sk_canvas_t* sk_document_begin_page(sk_document_t* document, float width, float height, const sk_rect_t* content);
+SK_API void sk_document_end_page(sk_document_t* document);
+SK_API bool sk_document_close(sk_document_t* document);
+SK_API void sk_document_abort(sk_document_t* document);
+
+// TODO: setMetadata
+
+SK_C_PLUS_PLUS_END_GUARD
+
+#endif
index 0d66de625df39a2d259dd87003f9caf491f5cdc7..93600f6279bdb2c31ceee1026d9c221059f48b35 100644 (file)
 
 SK_C_PLUS_PLUS_BEGIN_GUARD
 
+SK_API void sk_stream_asset_destroy(sk_stream_asset_t* cstream);
+
+////////////////////////////////////////////////////////////////////////////////
+
 SK_API sk_stream_filestream_t* sk_filestream_new(const char* path);
+SK_API void sk_filestream_destroy(sk_stream_filestream_t* cstream);
 
 ////////////////////////////////////////////////////////////////////////////////
 
@@ -25,6 +30,7 @@ SK_API sk_stream_memorystream_t* sk_memorystream_new_with_length(size_t length);
 SK_API sk_stream_memorystream_t* sk_memorystream_new_with_data(const void* data, size_t length, bool copyData);
 SK_API sk_stream_memorystream_t* sk_memorystream_new_with_skdata(sk_data_t* data);
 SK_API void sk_memorystream_set_memory(sk_stream_memorystream_t* cmemorystream, const void* data, size_t length, bool copyData);
+SK_API void sk_memorystream_destroy(sk_stream_memorystream_t* cstream);
 
 ////////////////////////////////////////////////////////////////////////////////
 
@@ -46,6 +52,36 @@ SK_API bool sk_stream_move(sk_stream_t* cstream, long offset);
 SK_API bool sk_stream_has_length(sk_stream_t* cstream);
 SK_API size_t sk_stream_get_length(sk_stream_t* cstream);
 
+////////////////////////////////////////////////////////////////////////////////
+
+SK_API sk_wstream_filestream_t* sk_filewstream_new(const char* path);
+SK_API void sk_filewstream_destroy(sk_wstream_filestream_t* cstream);
+
+SK_API sk_wstream_dynamicmemorystream_t* sk_dynamicmemorywstream_new();
+SK_API sk_data_t* sk_dynamicmemorywstream_copy_to_data(sk_wstream_dynamicmemorystream_t* cstream);
+SK_API sk_stream_asset_t* sk_dynamicmemorywstream_detach_as_stream(sk_wstream_dynamicmemorystream_t* cstream);
+SK_API void sk_dynamicmemorywstream_destroy(sk_wstream_dynamicmemorystream_t* cstream);
+
+////////////////////////////////////////////////////////////////////////////////
+
+SK_API bool sk_wstream_write(sk_wstream_t* cstream, const void* buffer, size_t size);
+SK_API void sk_wstream_newline(sk_wstream_t* cstream);
+SK_API void sk_wstream_flush(sk_wstream_t* cstream);
+SK_API size_t sk_wstream_bytes_written(sk_wstream_t* cstream);
+SK_API bool sk_wstream_write_8(sk_wstream_t* cstream, uint8_t value);
+SK_API bool sk_wstream_write_16(sk_wstream_t* cstream, uint16_t value);
+SK_API bool sk_wstream_write_32(sk_wstream_t* cstream, uint32_t value);
+SK_API bool sk_wstream_write_text(sk_wstream_t* cstream, const char* value);
+SK_API bool sk_wstream_write_dec_as_text(sk_wstream_t* cstream, int32_t value);
+SK_API bool sk_wstream_write_bigdec_as_text(sk_wstream_t* cstream, int64_t value, int minDigits);
+SK_API bool sk_wstream_write_hex_as_text(sk_wstream_t* cstream, uint32_t value, int minDigits);
+SK_API bool sk_wstream_write_scalar_as_text(sk_wstream_t* cstream, float value);
+SK_API bool sk_wstream_write_bool(sk_wstream_t* cstream, bool value);
+SK_API bool sk_wstream_write_scalar(sk_wstream_t* cstream, float value);
+SK_API bool sk_wstream_write_packed_uint(sk_wstream_t* cstream, size_t value);
+SK_API bool sk_wstream_write_stream(sk_wstream_t* cstream, sk_stream_t* input, size_t length);
+SK_API int sk_wstream_get_size_of_packed_uint(size_t value);
+
 SK_C_PLUS_PLUS_END_GUARD
 
 #endif
index 6c6c6ed1376b58c0e5077bc1f6171d8a3108e972..8d4e7f0f0b951786b68d84be8b4aaf6231755f51 100644 (file)
@@ -92,6 +92,13 @@ typedef struct sk_stream_filestream_t sk_stream_filestream_t;
 typedef struct sk_stream_asset_t sk_stream_asset_t;
 typedef struct sk_stream_memorystream_t sk_stream_memorystream_t;
 typedef struct sk_stream_streamrewindable_t sk_stream_streamrewindable_t;
+typedef struct sk_wstream_t sk_wstream_t;
+typedef struct sk_wstream_filestream_t sk_wstream_filestream_t;
+typedef struct sk_wstream_dynamicmemorystream_t sk_wstream_dynamicmemorystream_t;
+/**
+   High-level API for creating a document-based canvas.
+*/
+typedef struct sk_document_t sk_document_t;
 
 typedef enum {
     UTF8_ENCODING,
diff --git a/src/c/xamarin/sk_x_document.cpp b/src/c/xamarin/sk_x_document.cpp
new file mode 100644 (file)
index 0000000..fc69f91
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2016 Xamarin Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkDocument.h"
+
+#include "xamarin/sk_x_document.h"
+
+#include "../sk_types_priv.h"
+#include "sk_x_types_priv.h"
+
+void sk_document_unref(sk_document_t* document) {
+    AsDocument(document)->unref();
+}
+
+sk_document_t* sk_document_create_pdf_from_stream(sk_wstream_t* stream, float dpi) {
+    return ToDocument(SkDocument::CreatePDF(AsWStream(stream), dpi));
+}
+
+sk_document_t* sk_document_create_pdf_from_filename(const char* path, float dpi) {
+    return ToDocument(SkDocument::CreatePDF(path, dpi));
+}
+
+//sk_document_t* sk_document_create_xps_from_stream(sk_wstream_t* stream, float dpi) {
+//    return ToDocument(SkDocument::CreateXPS(AsWStream(stream), dpi));
+//}
+//
+//sk_document_t* sk_document_create_xps_from_filename(const char* path, float dpi) {
+//    return ToDocument(SkDocument::CreateXPS(path, dpi));
+//}
+
+sk_canvas_t* sk_document_begin_page(sk_document_t* document, float width, float height, const sk_rect_t* content) {
+    return ToCanvas(AsDocument(document)->beginPage(width, height, AsRect(content)));
+}
+
+void sk_document_end_page(sk_document_t* document) {
+    AsDocument(document)->endPage();
+}
+
+bool sk_document_close(sk_document_t* document) {
+    return AsDocument(document)->close();
+}
+
+void sk_document_abort(sk_document_t* document) {
+    AsDocument(document)->abort();
+}
index 9ed5a750724f001db3c438798bd9a580ff682168..fe8248a0179ebdce9d9607adc1ab46810c0cff41 100644 (file)
 #include "../sk_types_priv.h"
 #include "sk_x_types_priv.h"
 
+void sk_stream_asset_destroy(sk_stream_asset_t* cstream)
+{
+    delete AsStreamAsset(cstream);
+}
+
+void sk_filestream_destroy(sk_stream_filestream_t* cstream)
+{
+    delete AsFileStream(cstream);
+}
+
+void sk_memorystream_destroy(sk_stream_memorystream_t* cstream)
+{
+    delete AsMemoryStream(cstream);
+}
+
 sk_stream_filestream_t* sk_filestream_new (const char* path)
 {
     return (sk_stream_filestream_t*)new SkFILEStream(path);
@@ -115,3 +130,118 @@ size_t sk_stream_get_length (sk_stream_t* cstream)
 {
     return AsStream(cstream)->getLength();
 }
+
+void sk_filewstream_destroy(sk_wstream_filestream_t* cstream)
+{
+    delete AsFileWStream(cstream);
+}
+
+void sk_dynamicmemorywstream_destroy(sk_wstream_dynamicmemorystream_t* cstream)
+{
+    delete AsDynamicMemoryWStream(cstream);
+}
+
+sk_wstream_filestream_t* sk_filewstream_new(const char* path)
+{
+    return ToFileWStream(new SkFILEWStream(path));
+}
+
+sk_wstream_dynamicmemorystream_t* sk_dynamicmemorywstream_new()
+{
+    return ToDynamicMemoryWStream(new SkDynamicMemoryWStream());
+}
+
+sk_data_t* sk_dynamicmemorywstream_copy_to_data(sk_wstream_dynamicmemorystream_t* cstream)
+{
+    return ToData(AsDynamicMemoryWStream(cstream)->copyToData());
+}
+
+sk_stream_asset_t* sk_dynamicmemorywstream_detach_as_stream(sk_wstream_dynamicmemorystream_t* cstream)
+{
+    return ToStreamAsset(AsDynamicMemoryWStream(cstream)->detachAsStream());
+}
+
+bool sk_wstream_write(sk_wstream_t* cstream, const void* buffer, size_t size)
+{
+    return AsWStream(cstream)->write(buffer, size);
+}
+
+void sk_wstream_newline(sk_wstream_t* cstream)
+{
+    return AsWStream(cstream)->newline();
+}
+
+void sk_wstream_flush(sk_wstream_t* cstream)
+{
+    return AsWStream(cstream)->flush();
+}
+
+size_t sk_wstream_bytes_written(sk_wstream_t* cstream)
+{
+    return AsWStream(cstream)->bytesWritten();
+}
+
+bool sk_wstream_write_8(sk_wstream_t* cstream, uint8_t value)
+{
+    return AsWStream(cstream)->write8(value);
+}
+
+bool sk_wstream_write_16(sk_wstream_t* cstream, uint16_t value)
+{
+    return AsWStream(cstream)->write16(value);
+}
+
+bool sk_wstream_write_32(sk_wstream_t* cstream, uint32_t value)
+{
+    return AsWStream(cstream)->write32(value);
+}
+
+bool sk_wstream_write_text(sk_wstream_t* cstream, const char* value)
+{
+    return AsWStream(cstream)->writeText(value);
+}
+
+bool sk_wstream_write_dec_as_text(sk_wstream_t* cstream, int32_t value)
+{
+    return AsWStream(cstream)->writeDecAsText(value);
+}
+
+bool sk_wstream_write_bigdec_as_text(sk_wstream_t* cstream, int64_t value, int minDigits)
+{
+    return AsWStream(cstream)->writeBigDecAsText(value, minDigits);
+}
+
+bool sk_wstream_write_hex_as_text(sk_wstream_t* cstream, uint32_t value, int minDigits)
+{
+    return AsWStream(cstream)->writeHexAsText(value, minDigits);
+}
+
+bool sk_wstream_write_scalar_as_text(sk_wstream_t* cstream, float value)
+{
+    return AsWStream(cstream)->writeScalarAsText(value);
+}
+
+bool sk_wstream_write_bool(sk_wstream_t* cstream, bool value)
+{
+    return AsWStream(cstream)->writeBool(value);
+}
+
+bool sk_wstream_write_scalar(sk_wstream_t* cstream, float value)
+{
+    return AsWStream(cstream)->writeScalar(value);
+}
+
+bool sk_wstream_write_packed_uint(sk_wstream_t* cstream, size_t value)
+{
+    return AsWStream(cstream)->writePackedUInt(value);
+}
+
+bool sk_wstream_write_stream(sk_wstream_t* cstream, sk_stream_t* input, size_t length)
+{
+    return AsWStream(cstream)->writeStream(AsStream(input), length);
+}
+
+int sk_wstream_get_size_of_packed_uint(size_t value)
+{
+    return SkWStream::SizeOfPackedUInt(value);
+}
index afe4fb6122a48f43536e6418ba90111517869086..8b04744037e515d770c97ce6bed22e43410c3acb 100644 (file)
@@ -13,6 +13,7 @@
 
 #include "SkImageInfo.h"
 #include "SkBlurTypes.h"
+#include "SkDocument.h"
 #include "SkPaint.h"
 #include "SkPath.h"
 #include "SkImageDecoder.h"
@@ -597,6 +598,42 @@ static inline SkStream* AsStream(sk_stream_t* cstream) {
     return reinterpret_cast<SkStream*>(cstream);
 }
 
+static inline sk_stream_t* ToStream(SkStream* cstream) {
+    return reinterpret_cast<sk_stream_t*>(cstream);
+}
+
+static inline sk_stream_asset_t* ToStreamAsset(SkStreamAsset* cstream) {
+    return reinterpret_cast<sk_stream_asset_t*>(cstream);
+}
+
+static inline SkStreamAsset* AsStreamAsset(sk_stream_asset_t* cstream) {
+    return reinterpret_cast<SkStreamAsset*>(cstream);
+}
+
+static inline SkFILEWStream* AsFileWStream(sk_wstream_filestream_t* cfilestream) {
+    return reinterpret_cast<SkFILEWStream*>(cfilestream);
+}
+
+static inline SkDynamicMemoryWStream* AsDynamicMemoryWStream(sk_wstream_dynamicmemorystream_t* cmemorystream) {
+    return reinterpret_cast<SkDynamicMemoryWStream*>(cmemorystream);
+}
+
+static inline SkWStream* AsWStream(sk_wstream_t* cstream) {
+    return reinterpret_cast<SkWStream*>(cstream);
+}
+
+static inline sk_wstream_filestream_t* ToFileWStream(SkFILEWStream* filestream) {
+    return reinterpret_cast<sk_wstream_filestream_t*>(filestream);
+}
+
+static inline sk_wstream_dynamicmemorystream_t* ToDynamicMemoryWStream(SkDynamicMemoryWStream* memorystream) {
+    return reinterpret_cast<sk_wstream_dynamicmemorystream_t*>(memorystream);
+}
+
+static inline sk_wstream_t* ToWStream(SkWStream* stream) {
+    return reinterpret_cast<sk_wstream_t*>(stream);
+}
+
 static inline const SkPoint& AsPoint(const sk_point_t& p) {
     return reinterpret_cast<const SkPoint&>(p);
 }
@@ -661,5 +698,13 @@ static inline sk_string_t* ToString(SkString* data) {
     return reinterpret_cast<sk_string_t*>(data);
 }
 
+static inline SkDocument* AsDocument(sk_document_t* cdocument) {
+    return reinterpret_cast<SkDocument*>(cdocument);
+}
+
+static inline sk_document_t* ToDocument(SkDocument* document) {
+    return reinterpret_cast<sk_document_t*>(document);
+}
+
 
 #endif