Recent

Author Topic: TextRect and font size problem.  (Read 1071 times)

TomTom

  • Full Member
  • ***
  • Posts: 115
TextRect and font size problem.
« on: March 03, 2019, 08:13:39 pm »
I wrote program which generates images (fake scans of archival documentation). I use BGRABitmap and TextRect but I have problems with font size. It's not being set when I use TextRect (well, not always) and I just can't figure out why :|.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.SpeedButton1Click(Sender: TObject);
  2. var
  3.   FSyg, LastSyg, j, i, ml, rpn, d, rword: Integer;
  4.   Series, CollCD, Collnr, CollName, ArchName, ArchNr, Syg, TPath,
  5.     DigiHouse, DigiDate: String;
  6.   CurPages: LongInt;
  7.   Skan: TPicture;
  8.   scan, okladka, scan1: TBGRABitmap;
  9.   ArchRect, ZespRect, ZespNRRect, CDRect, SerRect, PCountRect,
  10.     SCountRect, SygnNRRect, DigiHouseRect, ScanDateRect, DigitRect,
  11.     QCheckDate, QCheckPers, MemoRect, CoverBodyRect: TRect;
  12.   styl, styl2,cstyl,pstyl,dstyl: TTextStyle;
  13.   Digiter, QCDate,memor, texts, content: String;
  14.   QCPers: TCaption;
  15.   Alignment: TAlignment;
  16.   jpgWriter: TFPWriterJPEG;
  17.   genMet, Gencov: Boolean;
  18.  
  19. begin
  20.   FSyg:=StrToInt(edStartSyg.text);
  21.   LastSyg:=FSyg+(StrToInt(edSygCount.text))-1;
  22.  
  23.   ArchNr:=edArchNr.Text;
  24.   ArchName:=edArchName.text;
  25.   CollName:=edCollName.text;
  26.   Collnr:=edCollNr.text;
  27.   CollCD:=edCollCD.text;
  28.   Series:=edSeries.text;
  29.   Syg:=edStartSyg.text;
  30.   DigiHouse:=edDigiHouse.text;
  31.   DigiDate:=edScanDate.text;
  32.   Digiter:=edCreator.text;
  33.   QCDate:=edQCDate.text;
  34.   QCPers:=LabeledEdit1.text;
  35.  
  36.   jpgWriter := TFPWriterJPEG.Create;
  37.   jpgWriter.CompressionQuality := 50;
  38.  
  39.  
  40.   TPath:=edTarget.text;
  41.  
  42.  
  43.   ArchRect:=TRect.Create(235,235,2244,590);
  44.   ZespRect:=TRect.Create(235,590,2244,945);
  45.  
  46.   ZespNRRect:=TRect.Create(235,945,881,1300);
  47.   CDRect:=TRect.Create    (881,945,1527,1300);
  48.   SerRect:=TRect.Create   (1527,945,2244,1300);
  49.  
  50.   SygnNRRect:=TRect.Create(235 ,1300,881,1655);
  51.   PCountRect:=TRect.Create(881 ,1300,1527,1655);
  52.   SCountRect:=TRect.Create(1527,1300,2244,1655);
  53.  
  54.   DigiHouseRect:=TRect.Create(235 ,1655,881,2010);
  55.   ScanDateRect:=TRect.Create (881 ,1655,1527,2010);
  56.   DigitRect:=TRect.Create    (1527,1655,2244,2010);
  57.  
  58.   QCheckDate:=TRect.Create   (235,2010,1122,2365);
  59.   QCheckPers:=TRect.Create   (1122,2010,2244,2365);
  60.  
  61.   MemoRect:=TRect.Create(235,2365,2244,3273);
  62.   // cover rects
  63.   CoverBodyRect:= TRect.Create(235,235,2244,3273);
  64.   //------------
  65.  
  66.   For j:=FSyg to LastSyg do
  67.   Begin
  68.     Randomize();
  69.     genMet:=false;
  70.     genCov:=false;
  71.     CurPages:=Random(StrToInt(edRndPageCount.text))+1;
  72.  
  73.     rpn:=0;
  74.     ForceDirectories(edTarget.text+ArchNr+'\'+CollNr+'\'+CollCD+'\'+Series+'\'+j.ToString+'\');
  75.     For i:=0 to CurPages-1 do Begin
  76.  
  77.       ForceDirectories(edTarget.text+ArchNr+'\'+CollNr+'\'+CollCD+'\'+Series+'\'+syg+'\');
  78.       memor:='';
  79.  
  80.       if genMet=false then
  81.       begin
  82.        mLog.lines.Add('METRYCZKA '+edTarget.text+ArchNr+'\'+CollNr+'\'+CollCD+'\'+Series+'\'+j.ToString+'\'+ArchNr+'_'+CollNr+'_'+CollCD+'_'+Series+'_'+j.ToString+'_'+Format('%.*d',[4,i+1]));
  83.       genmet:=true;
  84.  
  85.       scan := TBGRABitmap.Create(2480,3508,clWhite); //276, 306
  86.       scan.Canvas.Brush.Color:=clWhite;
  87.       scan.Canvas.FillRect(0,0,2200,3400);
  88.       styl.Alignment:=taCenter;
  89.       styl.Layout:=tlCenter;
  90.       styl.Clipping:=false;
  91.       Styl.SingleLine:=false;
  92.  
  93.       scan.Canvas.Rectangle(ArchRect);
  94.       scan.Canvas.Font.Name:='Calibri';
  95.       scan.Canvas.Font.Size:=32;
  96.       scan.Canvas.TextOut(ArchRect.left+20,ArchRect.top+20, '1. NR i NAZWA ARCHIWUM');
  97.       scan.Canvas.Font.Name:='Courier New';
  98.       scan.Canvas.Font.Size:=48;
  99.       scan.canvas.TextRect(ArchRect,ArchRect.left+20,120,ArchName,styl);
  100.  
  101.       scan.Canvas.Rectangle(ZespRect);
  102.       scan.Canvas.Font.Name:='Calibri';
  103.       scan.Canvas.Font.Size:=32;
  104.       scan.Canvas.TextOut(ZespRect.left+20,ZespRect.top+20, '2. NAZWA ZESPOLU');
  105.       scan.Canvas.Font.Name:='Courier New';
  106.       scan.Canvas.Font.Size:=48;
  107.       scan.canvas.TextRect(ZespRect,ZespRect.left+20,120,CollName,styl);
  108.  
  109.       scan.Canvas.Rectangle(ZespNRRect);
  110.       scan.Canvas.Font.Name:='Calibri';
  111.       scan.Canvas.Font.Size:=32;
  112.       scan.Canvas.TextOut(ZespNRRect.left+20,ZespNRRect.top+20, '3. NR ZESPOLU');
  113.       scan.Canvas.Font.Name:='Courier New';
  114.       scan.Canvas.Font.Size:=48;
  115.       scan.Canvas.TextRect(ZespNRRect,ZespNRRect.left+20,120,CollNr,styl);
  116.  
  117.       scan.Canvas.Rectangle(CDRect);
  118.       scan.Canvas.Font.Name:='Calibri';
  119.       scan.Canvas.Font.Size:=32;
  120.       scan.Canvas.TextOut(CDRect.left+20,CDRect.top+20, '4. CD NR ZESPOLU');
  121.       scan.Canvas.Font.Name:='Courier New';
  122.       scan.Canvas.Font.Size:=48;
  123.       scan.Canvas.TextRect(CDRect,CDRect.left+20,120,CollCD,styl);
  124.  
  125.       scan.Canvas.Rectangle(SerRect);
  126.       scan.Canvas.Font.Name:='Calibri';
  127.       scan.Canvas.Font.Size:=32;
  128.       scan.Canvas.TextOut(SerRect.left+20,SerRect.top+20, '5.SERIA');
  129.       scan.Canvas.Font.Name:='Courier New';
  130.       scan.Canvas.Font.Size:=48;
  131.       scan.Canvas.TextRect(SerRect, SerRect.left+20,120,Series,styl);
  132.  
  133.       scan.Canvas.Rectangle(SygnNRRect);
  134.       scan.Canvas.Font.Name:='Calibri';
  135.       scan.Canvas.Font.Size:=32;
  136.       scan.Canvas.TextOut(SygnNRRect.left+20,SygnNRRect.top+20, '6. NR SYGNATURY');
  137.       scan.Canvas.Font.Name:='Courier New';
  138.       scan.Canvas.Font.Size:=48;
  139.       scan.Canvas.TextRect(SygnNRRect,SygnNRRect.left+20,120,j.ToString,styl);
  140.  
  141.       scan.Canvas.Rectangle(PCountRect);
  142.       scan.Canvas.Font.Name:='Calibri';
  143.       scan.Canvas.Font.Size:=32;
  144.       scan.Canvas.TextOut(PCountRect.left+20,PCountRect.top+20, '7. LICZBA STRON');
  145.       scan.Canvas.Font.Name:='Courier New';
  146.       scan.Canvas.Font.Size:=48;
  147.       scan.Canvas.TextRECT(PCountRect,PCountRect.left+20,120, Curpages.ToString,styl);
  148.  
  149.       scan.Canvas.Rectangle(SCountRect);
  150.       scan.Canvas.Font.Name:='Calibri';
  151.       scan.Canvas.Font.Size:=32;
  152.       scan.Canvas.TextOut(SCountRect.left+20,SCountRect.top+20, '8. LICZBA SKANÓW');
  153.       scan.Canvas.Font.Name:='Courier New';
  154.       scan.Canvas.Font.Size:=48;
  155.       scan.Canvas.TextRect(SCountRect, SCountRect.left+20,120,Curpages.ToString,styl);
  156.      //------------------
  157.        styl2.Layout:=tlTop;
  158.        styl2.Alignment:=taLeftJustify;
  159.        styl2.SingleLine:=false;
  160.        styl2.Wordbreak:=true;
  161.  
  162.       scan.Canvas.Rectangle(DigiHouseRect);
  163.       scan.Canvas.Font.Name:='Calibri';
  164.       scan.Canvas.Font.Size:=32;
  165.       scan.Canvas.TextRect(DigiHouseRect, DigiHouseRect.left+20,DigiHouseRect.Top-50, '9. NAZWA PRACOWNI DIGITALIZACJI',styl2);
  166.       scan.Canvas.Font.Name:='Courier New';
  167.       scan.Canvas.Font.Size:=48;
  168.       scan.Canvas.TextRect(DigiHouseRect, DigiHouseRect.left+20,120,DigiHouse,styl);
  169.  
  170.       scan.Canvas.Rectangle(ScanDateRect);
  171.       scan.Canvas.Font.Name:='Calibri';
  172.       scan.Canvas.Font.Size:=32;
  173.       scan.Canvas.TextOut(ScanDateRect.left+20,ScanDateRect.top+20, '10. DATA SKANOWANIA');
  174.       scan.Canvas.Font.Name:='Courier New';
  175.       scan.Canvas.Font.Size:=48;
  176.       scan.Canvas.TextRect(ScanDateRect, ScanDateRect.left+20,120, dIGIDATE,styl);
  177.  
  178.       scan.Canvas.Rectangle(DigitRect);
  179.       scan.Canvas.Font.Name:='Calibri';
  180.       scan.Canvas.Font.Size:=32;
  181.       scan.Canvas.TextOut(DigitRect.left+20,DigitRect.top+20, '11. WYKONAWCA SKANÓW');
  182.       scan.Canvas.Font.Name:='Courier New';
  183.       scan.Canvas.Font.Size:=48;
  184.       scan.Canvas.TextRect(DigitRect, DigitRect.left+20,120,dIGITER,styl);
  185.  
  186.      // -----------------------
  187.       scan.Canvas.Rectangle(QCheckDate);
  188.       scan.Canvas.Font.Name:='Calibri';
  189.       scan.Canvas.Font.Size:=32;
  190.       scan.Canvas.TextOut(QCheckDate.left+20,QCheckDate.top+20, '12. DATA KONTROLI JAKOSCI');
  191.       scan.Canvas.Font.Name:='Courier New';
  192.       scan.Canvas.Font.Size:=48;
  193.       scan.Canvas.Textrect(QCheckDate, QCheckDate.left+20,QCheckDate.top+70,qcdate,styl);
  194.  
  195.       scan.Canvas.Rectangle(QCheckPers);
  196.       scan.Canvas.Font.Name:='Calibri';
  197.       scan.Canvas.Font.Size:=32;
  198.       scan.Canvas.TextOut(QCheckPers.left+20,QCheckPers.top+20, '13. KONTROLA JAKOSCI WYKONANA PRZEZ');
  199.       scan.Canvas.Font.Name:='Courier New';
  200.       scan.Canvas.Font.Size:=48;
  201.       scan.Canvas.Textrect(QCheckPers, QCheckPers.left+20,QCheckPers.top+70,qcPERS,styl);
  202.  
  203.       scan.Canvas.Rectangle(MemoRect);
  204.       scan.Canvas.Font.Name:='Calibri';
  205.       scan.Canvas.Font.Size:=32;
  206.       scan.Canvas.TextOut(MemoRect.left+20,MemoRect.top+20, '14. UWAGI');
  207.       scan.Canvas.Font.Name:='Courier New';
  208.       scan.Canvas.Font.Size:=48;
  209.  
  210.            for ml:=0 to mUwagi.lines.count-1 do
  211.            begin
  212.              memor:=memor+muwagi.lines.strings[ml];
  213.            end;
  214.  
  215.       scan.Canvas.Font.Name:='Courier New';
  216.       scan.Canvas.TextRect(MemoRect, MemoRect.left+20,0,memor,styl);
  217.       scan.SaveToFile(edTarget.text+ArchNr+'\'+CollNr+'\'+CollCD+'\'+Series+'\'+j.ToString+'\'+ArchNr+'_'+CollNr+'_'+CollCD+'_'+Series+'_'+j.ToString+'_0000_metryczka.jpg',jpgwriter);
  218.       scan.Free;
  219.      end;
  220.         // scan.Canvas.GradientFill(CoverBodyRect, RGBToColor(50,11,11),RGBToColor(266,30,30),gdHorizontal);
  221.      if GenCov=false then
  222.         begin
  223.         mLog.lines.Add('OKLADKA: '+edTarget.text+ArchNr+'\'+CollNr+'\'+CollCD+'\'+Series+'\'+j.ToString+'\'+ArchNr+'_'+CollNr+'_'+CollCD+'_'+Series+'_'+j.ToString+'_'+Format('%.*d',[4,i+1]));
  224.         ForceDirectories(edTarget.text+ArchNr+'\'+CollNr+'\'+CollCD+'\'+Series+'\'+syg+'\');
  225.         Gencov:=true;
  226.        okladka := TBGRABitmap.Create(2480,3508,RGBToColor(28, 28, 28)); //276, 306
  227.        okladka.Canvas.Brush.Color:=RGBToColor(123,30,30);
  228.        okladka.Canvas.Pen.Width:=10;
  229.        okladka.Canvas.pen.Color:=clWhite;
  230.        okladka.Canvas.FillRect(CoverBodyRect);
  231.        okladka.Canvas.Rectangle(CoverBodyRect);
  232.        okladka.Canvas.Font.Color:=clYellow;
  233.  
  234.        cstyl.Layout:=tlTop;
  235.        cstyl.Alignment:=taCenter;
  236.        cstyl.SingleLine:=false;
  237.        cstyl.Wordbreak:=true;
  238.        //scan.Canvas.TextOut(200,200,CollName+' '+collnr);
  239.        okladka.Canvas.Font.Name:='Calibri';
  240.        okladka.Canvas.Font.Size:=80;
  241.        okladka.Canvas.TextRect(CoverBodyRect,0,520,CollNr+' '+collName,cstyl);
  242.        okladka.Canvas.Font.Name:='Calibri';
  243.        okladka.Canvas.Font.Size:=40;
  244.        okladka.Canvas.TextRect(CoverBodyRect,0,650,'Czesc '+ j.ToString,cstyl);
  245.        okladka.SaveToFile(edTarget.text+ArchNr+'\'+CollNr+'\'+CollCD+'\'+Series+'\'+j.ToString+'\'+ArchNr+'_'+CollNr+'_'+CollCD+'_'+Series+'_'+j.ToString+'_0000_okladka.jpg',jpgwriter);
  246.        okladka.free;
  247.        end;
  248.  
  249.         ForceDirectories(edTarget.text+ArchNr+'\'+CollNr+'\'+CollCD+'\'+Series+'\'+syg+'\');
  250.         inc(rpn);
  251.         mLog.lines.Add('SKAN: '+edTarget.text+ArchNr+'\'+CollNr+'\'+CollCD+'\'+Series+'\'+j.ToString+'\'+ArchNr+'_'+CollNr+'_'+CollCD+'_'+Series+'_'+j.ToString+'_'+Format('%.*d',[4,i+1]));
  252.         pstyl.Layout:=tlTop;
  253.         pstyl.Alignment:=taCenter;
  254.         pstyl.SingleLine:=false;
  255.         pstyl.Wordbreak:=true;
  256.         dstyl.Layout:=tlTop;
  257.         dstyl.Alignment:=taLeftJustify;
  258.         dstyl.SingleLine:=false;
  259.         dstyl.Wordbreak:=true;
  260.  
  261.        scan1 := TBGRABitmap.Create(2480,3508,RGBToColor(28, 28, 28)); //276, 306
  262.  
  263.        d:=Random(100)+150;
  264.        scan1.Canvas.Brush.Color:=RGBToColor(d,d,d);
  265.        scan1.Canvas.Rectangle(CoverBodyRect);
  266.  
  267.  
  268.  
  269.        scan1.Canvas.Font.Name:='Calibri';
  270.        scan1.Canvas.font.size:=40;
  271.        scan1.Canvas.TextRect(CoverBodyRect,310,400,CollNr+' '+collName+' Czesc '+ j.ToString,cstyl);
  272.        scan1.Canvas.Font.Name:='Calibri';
  273.        scan1.Canvas.font.size:=71;
  274.        scan1.Canvas.TextRect(CoverBodyRect,310,600,ArchNr+'_'+CollNr+'_'+CollCD+'_'+Series+'_'+j.ToString+'_'+Format('%.*d',[4,rpn])+'.jpg',pstyl);
  275.        scan1.Canvas.Font.Name:='Courier New';
  276.        scan1.Canvas.font.size:=33;
  277.  
  278.        rword:=Random(3000)+800;
  279.        texts:='Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse orci elit, dapibus eget felis non, ullamcorper lobortis tortor. Morbi semper quis metus sed hendrerit. Nunc luctus semper ex, vel ultrices urna aliquet non. Morbi vulputate neque sodales ipsum tristique, at suscipit erat venenatis. Fusce blandit lobortis ligula, ut laoreet ante condimentum vitae. In hac habitasse platea dictumst. Vestibulum nec feugiat metus. Ut ullamcorper in felis a consequat. Ut ultrices euismod mi, in dapibus lectus finibus in. Mauris efficitur tempus velit sit amet posuere. Ut sodales sed odio id placerat. Duis venenatis velit eget venenatis congue. Ut sit amet eros placerat tortor rhoncus sodales. Praesent eget risus sed nisl facilisis pellentesque eu nec sem. Donec in pellentesque augue. Aenean sed tellus ornare, condimentum felis et, tincidunt sem. Integer volutpat commodo velit, nec venenatis diam sodales quis. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vivamus lacinia felis sit amet ex tincidunt, vel ultrices ipsum ullamcorper. In sit amet hendrerit mi. Phasellus finibus tellus eros, sit amet faucibus nulla placerat id. Nam nec felis sed lacus mollis elementum. Mauris non dictum mauris, mollis sodales massa. Maecenas sit amet iaculis enim. Fusce vulputate vulputate urna sed dapibus. Ut vulputate lacus at ipsum tempor elementum. In hac habitasse platea dictumst. Curabitur sit amet placerat ligula, a gravida lectus. Aliquam elementum metus eget placerat faucibus. Donec suscipit elit eu maximus venenatis. Proin enim nisi, dapibus id nulla vel, malesuada vulputate justo. Suspendisse vel fringilla diam. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nulla maximus mollis lectus, et ultricies leo. Aliquam non sapien suscipit, hendrerit tellus id, porta mauris. Nunc rhoncus odio nulla, sed volutpat metus viverra et. Fusce euismod molestie ante sed laoreet. Nullam luctus, lectus nec condimentum volutpat, magna orci hendrerit nibh, et faucibus odio ipsum eu ligula. Interdum et malesuada fames ac ante ipsum primis in faucibus. Nam dictum, augue eu scelerisque aliquam, est magna pharetra metus, nec faucibus eros ante sit amet nulla. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Nunc aliquet leo tempor turpis porttitor, sit amet luctus metus mollis. Curabitur a faucibus erat. Nunc dictum, ante at tempus lacinia, odio ligula faucibus orci, vel pharetra est ex et magna. Duis venenatis nunc ut rhoncus dapibus. Pellentesque tempus tempus finibus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Pellentesque fermentum bibendum nulla, ac suscipit eros mollis eu. Phasellus lacinia nisi risus, vitae convallis sem elementum id. Aliquam erat volutpat. Sed dignissim aliquam felis, nec malesuada dui molestie non. Vivamus pellentesque ex at erat consectetur, ut molestie eros lobortis. Suspendisse varius gravida lacus rutrum condimentum. Sed fermentum fringilla commodo. Sed et velit in sapien egestas aliquet ut ut ipsum. Ut eget consectetur diam. Curabitur quis elit vel felis tincidunt vulputate a ut arcu. Curabitur gravida nibh vitae purus suscipit, non tristique mi mollis. Etiam imperdiet pharetra sem quis condimentum. Morbi ac metus in tellus pellentesque euismod vel id ipsum. Nullam id.';
  280.  
  281.        content:=Copy(texts,0,rword);
  282.        scan1.Canvas.TextRect(CoverBodyRect,310,800,content,dstyl);
  283.        scan1.Canvas.Font.Name:='Courier New';
  284.        scan1.Canvas.font.size:=40;
  285.        scan1.Canvas.TextRect(CoverBodyRect,0,3200,'Strona '+ rpn.ToString+'/'+IntToStr(Curpages)+'  ',pstyl);
  286.        scan1.SaveToFile(edTarget.text+ArchNr+'\'+CollNr+'\'+CollCD+'\'+Series+'\'+j.ToString+'\'+ArchNr+'_'+CollNr+'_'+CollCD+'_'+Series+'_'+j.ToString+'_'+Format('%.*d',[4,rpn])+'.jpg',jpgwriter);
  287.        scan1.free;
  288.        end;
  289.  
  290.  
  291.   end;
  292.  
  293. end;
  294.  

Instructions how to use my program.


Input number of max scans into 'Losowa ilość stron (wpisz max)' for testing, 5 will be enough.
Set path where to save images. (i.e. C:\FakeScans\) additional directory structure will be created based on values in text fields on the right if the window.
As You will see not all font sizes are being set correctly

 

TinyPortal © 2005-2018