1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef nsPaper_h__
7 #define nsPaper_h__
8 
9 #include "mozilla/dom/ToJSValue.h"
10 #include "mozilla/gfx/Point.h"
11 #include "mozilla/gfx/Rect.h"
12 #include "mozilla/Maybe.h"
13 #include "nsIPaper.h"
14 #include "nsISupportsImpl.h"
15 #include "js/TypeDecls.h"
16 #include "nsString.h"
17 
18 struct JSContext;
19 
20 namespace mozilla {
21 
22 // Simple struct that can be used off the main thread to hold all the info from
23 // an nsPaper instance.
24 struct PaperInfo {
25   using MarginDouble = mozilla::gfx::MarginDouble;
26   using SizeDouble = mozilla::gfx::SizeDouble;
27 
28   PaperInfo() = default;
PaperInfoPaperInfo29   PaperInfo(const nsAString& aId, const nsAString& aName,
30             const SizeDouble& aSize,
31             const Maybe<MarginDouble>& aUnwriteableMargin)
32       : mId(aId),
33         mName(aName),
34         mSize(aSize),
35         mUnwriteableMargin(aUnwriteableMargin) {}
36 
37   nsString mId;
38   nsString mName;
39 
40   SizeDouble mSize;
41 
42   // The margins may not be known by some back-ends.
43   Maybe<MarginDouble> mUnwriteableMargin{Nothing()};
44 };
45 
46 /**
47  * Plain struct used for commonly used, hard-coded paper sizes.
48  *
49  * Used to construct PaperInfo at runtime by localizing the name.
50  */
51 struct CommonPaperSize final {
52   // The standardized PWG name, which should be used as the PaperInfo id
53   nsLiteralString mPWGName;
54   // The name key to localize the name of this paper size using strings in
55   // printUI.ftl
56   nsLiteralCString mLocalizableNameKey;
57   // Size is in points, same as PaperInfo
58   gfx::SizeDouble mSize;
59 };
60 
61 }  // namespace mozilla
62 
63 class nsPrinterBase;
64 
65 class nsPaper final : public nsIPaper {
66   using Promise = mozilla::dom::Promise;
67   using CommonPaperSize = mozilla::CommonPaperSize;
68 
69  public:
70   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
71   NS_DECL_CYCLE_COLLECTION_CLASS(nsPaper)
72   NS_DECL_NSIPAPER
73 
74   nsPaper() = delete;
75   explicit nsPaper(const mozilla::PaperInfo&);
76   nsPaper(nsPrinterBase&, const mozilla::PaperInfo&);
77 
78   // This list is used for both our fallback paper sizes (used when a printer
79   // does not provide a list of available paper sizes), and for localizing
80   // common paper sizes to avoid querying the printer for extra information in
81   // the common case, as well as to provide more uniform paper names in the
82   // frontend.
83   // If we need to separate these two uses, we will need to either split this
84   // into two lists, or add a flag to indicate what the size is used for.
85 #define mm *72.0 / 25.4
86 #define in *72.0
87   static constexpr CommonPaperSize kCommonPaperSizes[] = {
88       CommonPaperSize{u"iso_a5"_ns, "a5"_ns, {148 mm, 210 mm}},
89       CommonPaperSize{u"iso_a4"_ns, "a4"_ns, {210 mm, 297 mm}},
90       CommonPaperSize{u"iso_a3"_ns, "a3"_ns, {297 mm, 420 mm}},
91       CommonPaperSize{u"iso_b5"_ns, "b5"_ns, {176 mm, 250 mm}},
92       CommonPaperSize{u"iso_b4"_ns, "b4"_ns, {250 mm, 353 mm}},
93       CommonPaperSize{u"jis_b5"_ns, "jis-b5"_ns, {182 mm, 257 mm}},
94       CommonPaperSize{u"jis_b4"_ns, "jis-b4"_ns, {257 mm, 364 mm}},
95       CommonPaperSize{u"na_letter"_ns, "letter"_ns, {8.5 in, 11 in}},
96       CommonPaperSize{u"na_legal"_ns, "legal"_ns, {8.5 in, 14 in}},
97       CommonPaperSize{u"na_ledger"_ns, "tabloid"_ns, {11 in, 17 in}}};
98 #undef mm
99 #undef in
100   static constexpr size_t kNumCommonPaperSizes =
101       mozilla::ArrayLength(kCommonPaperSizes);
102 
103  private:
104   ~nsPaper();
105 
106   // null if not associated with a printer (for "Save-to-PDF" paper sizes)
107   RefPtr<nsPrinterBase> mPrinter;
108 
109   RefPtr<Promise> mMarginPromise;
110   const mozilla::PaperInfo mInfo;
111 };
112 
113 namespace mozilla {
114 
115 // Used to allow fixed-sized arrays of PWG paper info to be ref-counted.
116 class CommonPaperInfoArray
117     : public Array<PaperInfo, nsPaper::kNumCommonPaperSizes> {
118  public:
119   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CommonPaperInfoArray);
120   CommonPaperInfoArray() = default;
121 
122  private:
123   ~CommonPaperInfoArray() = default;
124 };
125 
126 }  // namespace mozilla
127 
128 #endif /* nsPaper_h__ */
129