Scale up bitmap size to fit in 300 dpi image to accomodate small fonts docs

This commit is contained in:
Anthony Tseng
2024-04-12 11:55:45 -07:00
parent cf0853f624
commit 957be1b9f1
4 changed files with 32 additions and 6 deletions
+1
View File
@@ -1,5 +1,6 @@
include_rules = [
"+pdf",
"+printing",
"+third_party/skia/include",
"+ui/gfx/geometry",
]
+15 -4
View File
@@ -11,6 +11,7 @@
#include "base/logging.h"
#include "base/memory/shared_memory_mapping.h"
#include "pdf/pdf.h"
#include "printing/units.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/geometry/size_conversions.h"
@@ -49,6 +50,8 @@ void PdfToBitmapConverter::GetBitmap(
}
auto pdf_buffer = pdf_map.GetMemoryAsSpan<const uint8_t>();
// Get the original size of the PDF page in points. Each point is 1/72 inch
// per chrome_pdf::CalculatePosition and printing::kPointsPerInch.
std::optional<gfx::SizeF> page_size =
chrome_pdf::GetPDFPageSizeByIndex(pdf_buffer, page_index);
if (!page_size.has_value()) {
@@ -56,8 +59,14 @@ void PdfToBitmapConverter::GetBitmap(
std::move(callback).Run(SkBitmap());
return;
}
gfx::Size size = gfx::ToCeiledSize(*page_size);
// Scale up the page size from 72 dpi to 300 dpi
gfx::SizeF size_300dpi = gfx::ScaleSize(
page_size.value(),
static_cast<float>(printing::kDefaultPdfDpi) / printing::kPointsPerInch);
gfx::Size size = gfx::ToCeiledSize(size_300dpi);
// Allocate a bitmap with the scaled up size so it can fit in later rendered
// 300 dpi PDF page image.
SkBitmap bitmap;
const SkImageInfo info = SkImageInfo::Make(
size.width(), size.height(), kBGRA_8888_SkColorType, kOpaque_SkAlphaType);
@@ -75,9 +84,11 @@ void PdfToBitmapConverter::GetBitmap(
.use_color = true,
.render_device_type = chrome_pdf::RenderDeviceType::kDisplay,
};
if (!chrome_pdf::RenderPDFPageToBitmap(pdf_buffer, page_index,
bitmap.getPixels(), size,
gfx::Size(300, 300), options)) {
// Render the PDF page to a bitmap with 300 dpi with scaled up size.
if (!chrome_pdf::RenderPDFPageToBitmap(
pdf_buffer, page_index, bitmap.getPixels(), size,
gfx::Size(printing::kDefaultPdfDpi, printing::kDefaultPdfDpi),
options)) {
DLOG(ERROR) << "Failed to render PDF buffer as bitmap image";
std::move(callback).Run(SkBitmap());
return;
+4 -1
View File
@@ -8,4 +8,7 @@ brave_services_printing_sources = [
"//brave/services/printing/pdf_to_bitmap_converter.h",
]
brave_services_printing_deps = [ "//components/discardable_memory/client" ]
brave_services_printing_deps = [
"//components/discardable_memory/client",
"//printing:printing_base",
]
+12 -1
View File
@@ -26,14 +26,25 @@
var context = canvas.getContext('2d');
context.font = '30px Arial';
context.fillText('This is the way.', 50, 100);
// leave page 2 empty
canvas = document.getElementById('page3');
context = canvas.getContext('2d');
context.font = '30px Arial';
context.fillText('I have spoken.', 50, 100);
// smaller text with higher DPI
canvas = document.getElementById('page4');
const dpr = 2;
const rect = canvas.getBoundingClientRect();
canvas.width = rect.width * dpr;
canvas.height = rect.height * dpr;
canvas.style.width = `${rect.width}px`;
canvas.style.height = `${rect.height}px`;
context = canvas.getContext('2d');
context.font = '30px Arial';
context.scale(dpr, dpr);
context.font = '15px Arial';
context.fillText('Wherever I Go, He Goes.', 50, 100);
</script>
</body>