Slide 1

Slide 1 text

Using PDF in iOS Using PDF in iOS iOS All Stars 2 – Nov. 2016 Kaz Yoshikawa

Slide 2

Slide 2 text

Using PDF in iOS Kaz Yoshikawa • Electricwoods LLC ୅ද / Digital Lynx Systems Inc. ෭୅ද • e-mail: [email protected] () • twitter: @codelynx • Working History • Adobe Systems (Tokyo) • Lionbridge (Tokyo) • Quark (Tokyo / Denver) • Hummingbird Communications (Mt. View, USA) • Fact International (Vancouver, Canada) • Perle Systems (Toronto, Canada), etc.

Slide 3

Slide 3 text

Using PDF in iOS What is PDF?

Slide 4

Slide 4 text

Using PDF in iOS PDF Reference sixth edition Adobe® Portable Document Format Version 1.7 November 2006 Adobe Systems Incorporated

Slide 5

Slide 5 text

Using PDF in iOS Characteristics • Resolution Independent • Multiple Pages • Printing Quality • Color Models: RGB, CMYK • Protection

Slide 6

Slide 6 text

Using PDF in iOS http://www.plda.cz/images/man/boxy-en.gif

Slide 7

Slide 7 text

Using PDF in iOS Model

Slide 8

Slide 8 text

Using PDF in iOS Outline entry Page Thumbnail image Annotations Bead Bead Thread Outline entry Content stream Thread Named destinations Article threads Interactive form Outline hierarchy Document catalog Page • • • • • • • • • • • • Page tree • • • • • • • • • • • •

Slide 9

Slide 9 text

Using PDF in iOS Hello World %PDF-1.4 1 0 obj <> endobj 2 0 obj <> endobj 3 0 obj<> endobj 4 0 obj<>>> endobj 5 0 obj<> endobj 6 0 obj <> stream BT /F1 24 Tf 175 720 Td (Hello World!)Tj ET endstream endobj trailer <> %%EOF Hello World!

Slide 10

Slide 10 text

Using PDF in iOS PDF in Core Graphics

Slide 11

Slide 11 text

Using PDF in iOS CGPDFDocument • init?(_ url: CFURL) • var numberOfPages: Int { get } • func page(at pageNumber: Int) -> CGPDFPage? • func unlockWithPassword(_ pw: UnsafePointer) -> Bool

Slide 12

Slide 12 text

Using PDF in iOS CGPDFPage • func getBoxRect(CGPDFBox) • var dictionary: CGPDFDictionaryRef? • var rotationAngle: Int32

Slide 13

Slide 13 text

Using PDF in iOS CGContext • func drawPDFPage(_ page: CGPDFPage)

Slide 14

Slide 14 text

Using PDF in iOS PDF Problems

Slide 15

Slide 15 text

Using PDF in iOS PDFs are drawn 
 upside down

Slide 16

Slide 16 text

Using PDF in iOS YOUCANDOIT Sophie Houlden hasn’t spent a penny on tools to make her indie games – she just rolled up her sleeves and got on with it. Here she explains how it’s done… 18 | JULY 2010

Slide 17

Slide 17 text

Using PDF in iOS BETA | GOING INDIE YOUCANDOIT Sophie Houlden hasn’t spent a penny on tools to make her indie games – she just rolled up her sleeves and got on with it. Here she explains how it’s done… 18 | JULY 2010 Core Graphics Coordinate Origin

Slide 18

Slide 18 text

Using PDF in iOS BETA | GOING INDIE YOUCANDOIT Sophie Houlden hasn’t spent a penny on tools to make her indie games – she just rolled up her sleeves and got on with it. Here she explains how it’s done… 18 | JULY 2010 Origin Origin UIView coordinate

Slide 19

Slide 19 text

Using PDF in iOS BETA | GOING INDIE YOUCANDOIT Sophie Houlden hasn’t spent a penny on tools to make her indie games – she just rolled up her sleeves and got on with it. Here she explains how it’s done… 18 | JULY 2010 Origin Origin ctx.translateBy(
 x: 0, 
 y: rect.height)

Slide 20

Slide 20 text

Using PDF in iOS 18 | JULY 2010 Origin Origin ctx.scaleBy(
 x: 1,
 y: -1)

Slide 21

Slide 21 text

Using PDF in iOS BET A | GOING INDIE Y O U C A N D O I T S o p h i e H o u l d e n h a s n ’ t s p e n t a p e n n y o n t o o l s t o m a k e h e r i n d i e g a m e s – s h e j u s t r o l l e d u p h e r s l e e v e s a n d g o t o n w i t h i t . H e r e s h e e x p l a i n s h o w i t ’ s d o n e … 18 | JUL Y 2010 Origin Origin ctx.scaleBy(
 x: 1,
 y: -1)

Slide 22

Slide 22 text

Using PDF in iOS BET A | GOING INDIE Y O U C A N D O I T S o p h i e H o u l d e n h a s n ’ t s p e n t a p e n n y o n t o o l s t o m a k e h e r i n d i e g a m e s – s h e j u s t r o l l e d u p h e r s l e e v e s a n d g o t o n w i t h i t . H e r e s h e e x p l a i n s h o w i t ’ s d o n e … 18 | JUL Y 2010 Origin Origin ctx.scaleBy(
 x: 1,
 y: -1)

Slide 23

Slide 23 text

Using PDF in iOS BETA | GOING INDIE YOUCANDOIT Sophie Houlden hasn’t spent a penny on tools to make her indie games – she just rolled up her sleeves and got on with it. Here she explains how it’s done… 18 | JULY 2010 Origin Origin

Slide 24

Slide 24 text

Using PDF in iOS BETA | GOING INDIE YOUCANDOIT Sophie Houlden hasn’t spent a penny on tools to make her indie games – she just rolled up her sleeves and got on with it. Here she explains how it’s done… 18 | JULY 2010 Origin Origin let scale = min(
 rect.width/box.width, 
 rect.height/box.height)
 
 ctx.scaleBy(
 x: scale, y: scale)

Slide 25

Slide 25 text

Using PDF in iOS BETA | GOING INDIE YOUCANDOIT Sophie Houlden hasn’t spent a penny on tools to make her indie games – she just rolled up her sleeves and got on with it. Here she explains how it’s done… 18 | JULY 2010 Origin Origin ctx.drawPDFPage(
 page)

Slide 26

Slide 26 text

Using PDF in iOS Code in Swift let box = page.getBoxRect(.cropBox) let rect = self.view.bounds ctx.translateBy(x: 0, y: rect.height) ctx.scaleBy(x: 1, y: -1) ctx.scaleBy(x: rect.width / box.width, y: rect.height / box.height) ctx.drawPDFPage(page)

Slide 27

Slide 27 text

Using PDF in iOS Problem Out of memory!?

Slide 28

Slide 28 text

Using PDF in iOS Dealing with CGPDFDocument • CGPDFDocument is originally designed for Desktop • It cashes internally, hard to control releasing caches… • Releasing CGPDFDocument on memory warning may be too late… • Releasing CGPDFDocument once in a while

Slide 29

Slide 29 text

Using PDF in iOS Problem
 Zooming PDF Page within UIScrollView

Slide 30

Slide 30 text

Using PDF in iOS Blurring when zoom!?

Slide 31

Slide 31 text

Using PDF in iOS contentScaleFactor • OK – Device size like small contents • NG – Magazine / Newspaper like large contents • eg. 2048x1536 12MB — (4x zoom) — 8192 x 6144 48MB ⚠

Slide 32

Slide 32 text

Using PDF in iOS Dummy Content View UIView PDFPageView UIScrollView Dummy
 ContentView Transparent

Slide 33

Slide 33 text

Using PDF in iOS PDF view draws based on dummy view coordinate Zoom → dummy view pdf view

Slide 34

Slide 34 text

Using PDF in iOS Convert rect from 
 content dummy view override func draw(_ layer: CALayer, in ctx: CGContext) { UIGraphicsPushContext(ctx) ctx.saveGState() let box = page.getBoxRect(.cropBox) let rect = self.contentView.convert(self.contentView.bounds, to: self) ctx.translateBy(x: rect.minX, y: rect.minY) ctx.translateBy(x: 0, y: rect.height) ctx.scaleBy(x: 1, y: -1) ctx.scaleBy(x: rect.width / box.width, y: rect.height / box.height) ctx.drawPDFPage(page) ctx.restoreGState() UIGraphicsPopContext() } →

Slide 35

Slide 35 text

Using PDF in iOS UIScrollView delegate func viewForZooming(in scrollView: UIScrollView) -> UIView? { return contentView } func scrollViewDidZoom(_ scrollView: UIScrollView) { self.pdfPageView.setNeedsDisplay() } func scrollViewDidScroll(_ scrollView: UIScrollView) { self.pdfPageView.setNeedsDisplay() }

Slide 36

Slide 36 text

Using PDF in iOS Use draw(layer, in context)
 not draw(_ rect) class PDFPageView: UIView { override func draw(_ layer: CALayer, in ctx: CGContext) { UIGraphicsPushContext(ctx) // drawing UIGraphicsPopContext() } override func setNeedsDisplay() { super.setNeedsDisplay() self.layer.setNeedsDisplay() } }

Slide 37

Slide 37 text

Using PDF in iOS Problem Drawing PDF is too slow

Slide 38

Slide 38 text

Using PDF in iOS 2.pdf Split a large PDF into 
 smaller multiple pages paper.pdf 1.pdf 3.pdf

Slide 39

Slide 39 text

Using PDF in iOS Double layers Low resolution bitmap image (fast) Native PDF
 drawing (slow)

Slide 40

Slide 40 text

Using PDF in iOS Dummy Content View UIView PDFPageView UIScrollView Dummy
 ContentView Low resolution bitmap image (fast) Native PDF
 drawing (slow) show/hide

Slide 41

Slide 41 text

Using PDF in iOS Use fileURL
 avoid CGDataProvider • PDF file includes cross references • Cross references includes file offsets to objects • Using file offset to access objects faster than we think • Do not read whole file and feed to CGPDFDocument

Slide 42

Slide 42 text

Using PDF in iOS Problem
 Bitmap looks different from rendered PDF

Slide 43

Slide 43 text

Using PDF in iOS Drawn Image slides BETA | GOING INDIE YOUCANDOIT Sophie Houlden hasn’t spent a penny on tools to make her indie games – she just rolled up her sleeves and got on with it. Here she explains how it’s done… 18 | JULY 2010 BETA | GOING INDIE YOUCANDOIT Sophie Houlden hasn’t spent a penny on tools to make her indie games – she just rolled up her sleeves and got on with it. Here she explains how it’s done… 18 | JULY 2010 Bitmap drawing Native PDF drawing

Slide 44

Slide 44 text

Using PDF in iOS Drawn Image slides BETA | GOING INDIE YOUCANDOIT Sophie Houlden hasn’t spent a penny on tools to make her indie games – she just rolled up her sleeves and got on with it. Here she explains how it’s done… 18 | JULY 2010 BETA | GOING INDIE YOUCANDOIT Sophie Houlden hasn’t spent a penny on tools to make her indie games – she just rolled up her sleeves and got on with it. Here she explains how it’s done… 18 | JULY 2010 Bitmap drawing Native PDF drawing

Slide 45

Slide 45 text

Using PDF in iOS BETA | GOING INDIE YOUCANDOIT Sophie Houlden hasn’t spent a penny on tools to make her indie games – she just rolled up her sleeves and got on with it. Here she explains how it’s done… 18 | JULY 2010 Check origin!! page.getBoxRect(.crop) Origin * Box's minX, minY may not be zero

Slide 46

Slide 46 text

Using PDF in iOS PDF in Depth

Slide 47

Slide 47 text

Using PDF in iOS Useful Tools

Slide 48

Slide 48 text

Using PDF in iOS PDF-Voyeur

Slide 49

Slide 49 text

Using PDF in iOS Voyeur

Slide 50

Slide 50 text

Using PDF in iOS PDF model

Slide 51

Slide 51 text

Using PDF in iOS CGPDFObjectRef .dictionary CGPDFDictionaryRef OpaquePointer .array CGPDFArrayRef OpaquePointer .stream CGPDFContentStreamRef OpaquePointer .string CGPDFStringRef OpaquePointer .name UnsafePointer UnsafePointer .integer CGPDFInteger Int .real CGPDFReal CGFloat .boolean CGPDFBoolean UInt8 .null

Slide 52

Slide 52 text

Using PDF in iOS Iterating CGPDFDictionary func PDFDictionary(_ dictionary: CGPDFDictionaryRef) -> [String: CGPDFObjectRef] { 
 var context = [String: CGPDFObjectRef]() 
 let contextRawPtr = UnsafeMutableRawPointer(&context) CGPDFDictionaryApplyFunction(dictionary, { (key, value, rawPtr) -> Void in if let key = String(validatingUTF8: key), let contextPtr = UnsafeMutablePointer<[String: CGPDFObjectRef]>( OpaquePointer(rawPtr)) { contextPtr.pointee[key] = value } }, contextRawPtr) 
 return context } for (key, object) in PDFDictionary(catalog).enumerated() { print("\(key), \(object)") }

Slide 53

Slide 53 text

Using PDF in iOS CGPDFObjectGetType WBSWBMVF$(1%'0CKFDU3FGǘ
 TXJUDI$(1%'0CKFDU(FU5ZQF WBMVF \ DBTFBSSBZ WBSPCKFDU$(1%'"SSBZ3FG OJM $(1%'0CKFDU(FU7BMVF WBMVF BSSBZ PCKFDU DBTFEJDUJPOBSZ WBSPCKFDU$(1%'%JDUJPOBSZ3FG OJM $(1%'0CKFDU(FU7BMVF WBMVF EJDUJPOBSZ PCKFDU DBTFTUSFBN DBTFTUSJOH DBTFOBNF DBTFJOUFHFS DBTFSFBM DBTFCPPMFBO DBTFOVMMCSFBL ^

Slide 54

Slide 54 text

Using PDF in iOS PDFScanner • Low level API • Scanner knows how to parse page content • PDF's operator

Slide 55

Slide 55 text

Using PDF in iOS Problem
 Like to Search Text in PDF

Slide 56

Slide 56 text

Using PDF in iOS Page –> Font MediaBox Resources Page Contents … Font … Tc1 Tc2 … subtype … …

Slide 57

Slide 57 text

Using PDF in iOS Font Contents

Slide 58

Slide 58 text

Using PDF in iOS Font Types Font Type Entries Type 1 Name, BaseFont, FirstChar, LastChar, Widths, FontDescriptor, Encoding, ToUnicode, ... True Type Name, BaseFont, FirstChar, LastChar, Widths, FontDescriptor, Encoding, ToUnicode, … Type 3 Name, FontBBox, FontMatrix, CharProcs, FisrChar, LastChar, Widths, FontDescriptor, ToUnicode … Type 0 Name, BaseFont, CIDSystemInfo, FontDescriptor, DW, DW2, CIDToGIDMap, ❌ ToUnicode

Slide 59

Slide 59 text

Using PDF in iOS A Page knows how to draw MediaBox Resources Page Contents Font Tc2 dictionary array stream Drawing operators

Slide 60

Slide 60 text

Using PDF in iOS Operators BT /F1 24 Tf 175 720 Td (Hello World!)Tj ET Begin Text End Text Font Location Draw Text

Slide 61

Slide 61 text

Using PDF in iOS Japanese Text BT /C2_0 1 Tf 0 Tc 175 720 Td <30533093306B3061306F> Tj ET Font Location Draw Text

Slide 62

Slide 62 text

Using PDF in iOS Decoding Method Case Decoding Method Has `ToUnicode` entry Parse `ToUnicode` entry No `ToUnicode` entry
 but WinAnsiEncoding Predefined Identity-H, Identity-V encoding
 No `ToUnicode` entry Using external CMap

Slide 63

Slide 63 text

Using PDF in iOS Parsing `ToUnicode` entry

Slide 64

Slide 64 text

Using PDF in iOS Adobe CMap and CIDFont Files Specification Version 1.0 11 June 1993 Adobe Developer Support Adobe Systems Incorporated ® ® ®

Slide 65

Slide 65 text

Using PDF in iOS ToUnicode = CMap %!PS-Adobe-3.0 Resource-CMap /CIDInit /ProcSet findresource begin 12 dict begin begincmap /CIDSystemInfo 3 dict dup begin /Registry (Adobe) def /Ordering (Japan1) def /Supplement 6 def end def /CMapName /Adobe-Japan1-6 def /CMapVersion 1.005 def /CMapType 1 def /XUID [1 10 25614] def /WMode 0 def /CIDCount 23058 def 1 begincodespacerange <0000> <5AFF> endcodespacerange 91 begincidrange <0000> <00ff> 0 <0100> <01ff> 256 <0200> <02ff> 512 <0300> <03ff> 768 <0400> <04ff> 1024 … <5600> <56ff> 22016 <5700> <57ff> 22272 <5800> <58ff> 22528 <5900> <59ff> 22784 <5a00> <5a11> 23040 endcidrange endcmap CMapName currentdict 
 /CMap defineresource pop end end ←Horizontal/Vertical ←code mapping

Slide 66

Slide 66 text

Using PDF in iOS Using external Cmap


Slide 67

Slide 67 text

Using PDF in iOS Adobe Japan 1-4 Identity-V No ToUnicode entry

Slide 68

Slide 68 text

Using PDF in iOS

Slide 69

Slide 69 text

Using PDF in iOS CMap: Adobe Japan 1-6

Slide 70

Slide 70 text

Using PDF in iOS

Slide 71

Slide 71 text

Using PDF in iOS

Slide 72

Slide 72 text

Using PDF in iOS Problem
 Table of contents !?

Slide 73

Slide 73 text

Using PDF in iOS Outline entry Page Thumbnail image Annotations Bead Bead Thread Outline entry Content stream Thread Named destinations Article threads Interactive form Outline hierarchy Document catalog Page • • • • • • • • • • • • Page tree • • • • • • • • • • • •

Slide 74

Slide 74 text

Using PDF in iOS Left Intentionally Blank

Slide 75

Slide 75 text

Using PDF in iOS Problem
 Tapping on Hyperlinks

Slide 76

Slide 76 text

Using PDF in iOS Left Intentionally Blank

Slide 77

Slide 77 text

Using PDF in iOS ZPDF

Slide 78

Slide 78 text

Using PDF in iOS ZPDF • Tentative name • Swift base PDF library • Parsing PDF • Extracting text and glyph positions • Will be open source when ready • sorry — this could take years

Slide 79

Slide 79 text

Using PDF in iOS

Slide 80

Slide 80 text

Using PDF in iOS Text Edit InDesign MS Word

Slide 81

Slide 81 text

Using PDF in iOS One more thing

Slide 82

Slide 82 text

Using PDF in iOS Tips & Tricks Animation with PDF

Slide 83

Slide 83 text

Using PDF in iOS Animation with PDF 0s 4s 6s

Slide 84

Slide 84 text

Using PDF in iOS Multiple Page PDF page 1 page 2 Timesheet

Slide 85

Slide 85 text

Using PDF in iOS

Slide 86

Slide 86 text

Using PDF in iOS

Slide 87

Slide 87 text

Using PDF in iOS

Slide 88

Slide 88 text

Using PDF in iOS

Slide 89

Slide 89 text

Using PDF in iOS

Slide 90

Slide 90 text

Using PDF in iOS Wrap Up

Slide 91

Slide 91 text

Using PDF in iOS Wrap Up • Basic PDF format structure • Core Graphics PDF API • Fixing drawing upside down • Fixing runout of memory • Zooming PDF within UIScrollView

Slide 92

Slide 92 text

Using PDF in iOS Wrap Up • Dealing with too slow drawing PDF page • Fixing drawing origin • Voyeur • Extracting text from PDF • ZKIT • Animation with PDF

Slide 93

Slide 93 text

Using PDF in iOS Last Words • Likely it's hard to remember that many tricks and knowledges • What you have to remember is … ask "Kaz" [email protected]

Slide 94

Slide 94 text

Using PDF in iOS Thanks